context_vk.c 124 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright 2002-2004 Jason Edmeades
 * Copyright 2002-2004 Raphael Junqueira
 * Copyright 2004 Christian Costa
 * Copyright 2005 Oliver Stieber
 * Copyright 2006, 2008 Henri Verbeet
 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
 * Copyright 2009-2020 Henri Verbeet for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
#include "wine/port.h"

#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d);

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
VkCompareOp vk_compare_op_from_wined3d(enum wined3d_cmp_func op)
{
    switch (op)
    {
        case WINED3D_CMP_NEVER:
            return VK_COMPARE_OP_NEVER;
        case WINED3D_CMP_LESS:
            return VK_COMPARE_OP_LESS;
        case WINED3D_CMP_EQUAL:
            return VK_COMPARE_OP_EQUAL;
        case WINED3D_CMP_LESSEQUAL:
            return VK_COMPARE_OP_LESS_OR_EQUAL;
        case WINED3D_CMP_GREATER:
            return VK_COMPARE_OP_GREATER;
        case WINED3D_CMP_NOTEQUAL:
            return VK_COMPARE_OP_NOT_EQUAL;
        case WINED3D_CMP_GREATEREQUAL:
            return VK_COMPARE_OP_GREATER_OR_EQUAL;
        case WINED3D_CMP_ALWAYS:
            return VK_COMPARE_OP_ALWAYS;
        default:
            if (!op)
                WARN("Unhandled compare operation %#x.\n", op);
            else
                FIXME("Unhandled compare operation %#x.\n", op);
            return VK_COMPARE_OP_NEVER;
    }
}

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
VkShaderStageFlagBits vk_shader_stage_from_wined3d(enum wined3d_shader_type shader_type)
{
    switch (shader_type)
    {
        case WINED3D_SHADER_TYPE_VERTEX:
            return VK_SHADER_STAGE_VERTEX_BIT;
        case WINED3D_SHADER_TYPE_HULL:
            return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
        case WINED3D_SHADER_TYPE_DOMAIN:
            return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
        case WINED3D_SHADER_TYPE_GEOMETRY:
            return VK_SHADER_STAGE_GEOMETRY_BIT;
        case WINED3D_SHADER_TYPE_PIXEL:
            return VK_SHADER_STAGE_FRAGMENT_BIT;
        case WINED3D_SHADER_TYPE_COMPUTE:
            return VK_SHADER_STAGE_COMPUTE_BIT;
        default:
            ERR("Unhandled shader type %s.\n", debug_shader_type(shader_type));
            return 0;
    }
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
static VkBlendFactor vk_blend_factor_from_wined3d(enum wined3d_blend blend,
        const struct wined3d_format *dst_format, bool alpha)
{
    switch (blend)
    {
        case WINED3D_BLEND_ZERO:
            return VK_BLEND_FACTOR_ZERO;
        case WINED3D_BLEND_ONE:
            return VK_BLEND_FACTOR_ONE;
        case WINED3D_BLEND_SRCCOLOR:
            return VK_BLEND_FACTOR_SRC_COLOR;
        case WINED3D_BLEND_INVSRCCOLOR:
            return VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR;
        case WINED3D_BLEND_SRCALPHA:
            return VK_BLEND_FACTOR_SRC_ALPHA;
        case WINED3D_BLEND_INVSRCALPHA:
            return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
        case WINED3D_BLEND_DESTALPHA:
            if (dst_format->alpha_size)
                return VK_BLEND_FACTOR_DST_ALPHA;
            return VK_BLEND_FACTOR_ONE;
        case WINED3D_BLEND_INVDESTALPHA:
            if (dst_format->alpha_size)
                return VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA;
            return VK_BLEND_FACTOR_ZERO;
        case WINED3D_BLEND_DESTCOLOR:
            return VK_BLEND_FACTOR_DST_COLOR;
        case WINED3D_BLEND_INVDESTCOLOR:
            return VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;
        case WINED3D_BLEND_SRCALPHASAT:
            return VK_BLEND_FACTOR_SRC_ALPHA_SATURATE;
        case WINED3D_BLEND_BLENDFACTOR:
            if (alpha)
                return VK_BLEND_FACTOR_CONSTANT_ALPHA;
            return VK_BLEND_FACTOR_CONSTANT_COLOR;
        case WINED3D_BLEND_INVBLENDFACTOR:
            if (alpha)
                return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA;
            return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR;
        case WINED3D_BLEND_SRC1COLOR:
            return VK_BLEND_FACTOR_SRC1_COLOR;
        case WINED3D_BLEND_INVSRC1COLOR:
            return VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR;
        case WINED3D_BLEND_SRC1ALPHA:
            return VK_BLEND_FACTOR_SRC1_ALPHA;
        case WINED3D_BLEND_INVSRC1ALPHA:
            return VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA;
        default:
            FIXME("Unhandled blend %#x.\n", blend);
            return VK_BLEND_FACTOR_ZERO;
    }
}

static VkBlendOp vk_blend_op_from_wined3d(enum wined3d_blend_op op)
{
    switch (op)
    {
        case WINED3D_BLEND_OP_ADD:
            return VK_BLEND_OP_ADD;
        case WINED3D_BLEND_OP_SUBTRACT:
            return VK_BLEND_OP_SUBTRACT;
        case WINED3D_BLEND_OP_REVSUBTRACT:
            return VK_BLEND_OP_REVERSE_SUBTRACT;
        case WINED3D_BLEND_OP_MIN:
            return VK_BLEND_OP_MIN;
        case WINED3D_BLEND_OP_MAX:
            return VK_BLEND_OP_MAX;
        default:
            FIXME("Unhandled blend op %#x.\n", op);
            return VK_BLEND_OP_ADD;
    }
}

static VkColorComponentFlags vk_colour_write_mask_from_wined3d(uint32_t wined3d_mask)
{
    VkColorComponentFlags vk_mask = 0;

    if (wined3d_mask & WINED3DCOLORWRITEENABLE_RED)
        vk_mask |= VK_COLOR_COMPONENT_R_BIT;
    if (wined3d_mask & WINED3DCOLORWRITEENABLE_GREEN)
        vk_mask |= VK_COLOR_COMPONENT_G_BIT;
    if (wined3d_mask & WINED3DCOLORWRITEENABLE_BLUE)
        vk_mask |= VK_COLOR_COMPONENT_B_BIT;
    if (wined3d_mask & WINED3DCOLORWRITEENABLE_ALPHA)
        vk_mask |= VK_COLOR_COMPONENT_A_BIT;

    return vk_mask;
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static VkCullModeFlags vk_cull_mode_from_wined3d(enum wined3d_cull mode)
{
    switch (mode)
    {
        case WINED3D_CULL_NONE:
            return VK_CULL_MODE_NONE;
        case WINED3D_CULL_FRONT:
            return VK_CULL_MODE_FRONT_BIT;
        case WINED3D_CULL_BACK:
            return VK_CULL_MODE_BACK_BIT;
        default:
            FIXME("Unhandled cull mode %#x.\n", mode);
            return VK_CULL_MODE_NONE;
    }
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static VkPrimitiveTopology vk_topology_from_wined3d(enum wined3d_primitive_type t)
{
    switch (t)
    {
        case WINED3D_PT_POINTLIST:
            return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
        case WINED3D_PT_LINELIST:
            return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
        case WINED3D_PT_LINESTRIP:
            return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
        case WINED3D_PT_TRIANGLELIST:
            return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
        case WINED3D_PT_TRIANGLESTRIP:
            return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
        case WINED3D_PT_TRIANGLEFAN:
            return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
        case WINED3D_PT_LINELIST_ADJ:
            return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
        case WINED3D_PT_LINESTRIP_ADJ:
            return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY;
        case WINED3D_PT_TRIANGLELIST_ADJ:
            return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY;
        case WINED3D_PT_TRIANGLESTRIP_ADJ:
            return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY;
        case WINED3D_PT_PATCH:
            return VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
        default:
            FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(t));
        case WINED3D_PT_UNDEFINED:
            return ~0u;
    }
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
static VkStencilOp vk_stencil_op_from_wined3d(enum wined3d_stencil_op op)
{
    switch (op)
    {
        case WINED3D_STENCIL_OP_KEEP:
            return VK_STENCIL_OP_KEEP;
        case WINED3D_STENCIL_OP_ZERO:
            return VK_STENCIL_OP_ZERO;
        case WINED3D_STENCIL_OP_REPLACE:
            return VK_STENCIL_OP_REPLACE;
        case WINED3D_STENCIL_OP_INCR_SAT:
            return VK_STENCIL_OP_INCREMENT_AND_CLAMP;
        case WINED3D_STENCIL_OP_DECR_SAT:
            return VK_STENCIL_OP_DECREMENT_AND_CLAMP;
        case WINED3D_STENCIL_OP_INVERT:
            return VK_STENCIL_OP_INVERT;
        case WINED3D_STENCIL_OP_INCR:
            return VK_STENCIL_OP_INCREMENT_AND_WRAP;
        case WINED3D_STENCIL_OP_DECR:
            return VK_STENCIL_OP_DECREMENT_AND_WRAP;
        default:
            if (!op)
                WARN("Unhandled stencil operation %#x.\n", op);
            else
                FIXME("Unhandled stencil operation %#x.\n", op);
            return VK_STENCIL_OP_KEEP;
    }
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
void *wined3d_allocator_chunk_vk_map(struct wined3d_allocator_chunk_vk *chunk_vk,
        struct wined3d_context_vk *context_vk)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    VkResult vr;

    TRACE("chunk %p, memory 0x%s, map_ptr %p.\n", chunk_vk,
            wine_dbgstr_longlong(chunk_vk->vk_memory), chunk_vk->c.map_ptr);

    if (!chunk_vk->c.map_ptr && (vr = VK_CALL(vkMapMemory(device_vk->vk_device,
            chunk_vk->vk_memory, 0, VK_WHOLE_SIZE, 0, &chunk_vk->c.map_ptr))) < 0)
    {
        ERR("Failed to map chunk memory, vr %s.\n", wined3d_debug_vkresult(vr));
        return NULL;
    }

    ++chunk_vk->c.map_count;

    return chunk_vk->c.map_ptr;
}

void wined3d_allocator_chunk_vk_unmap(struct wined3d_allocator_chunk_vk *chunk_vk,
        struct wined3d_context_vk *context_vk)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;

    TRACE("chunk_vk %p, context_vk %p.\n", chunk_vk, context_vk);

    if (!--chunk_vk->c.map_count)
        VK_CALL(vkUnmapMemory(device_vk->vk_device, chunk_vk->vk_memory));
    chunk_vk->c.map_ptr = NULL;
}

VkDeviceMemory wined3d_context_vk_allocate_vram_chunk_memory(struct wined3d_context_vk *context_vk,
        unsigned int pool, size_t size)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    VkMemoryAllocateInfo allocate_info;
    VkDeviceMemory vk_memory;
    VkResult vr;

    allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    allocate_info.pNext = NULL;
    allocate_info.allocationSize = size;
    allocate_info.memoryTypeIndex = pool;
    if ((vr = VK_CALL(vkAllocateMemory(device_vk->vk_device, &allocate_info, NULL, &vk_memory))) < 0)
    {
        ERR("Failed to allocate memory, vr %s.\n", wined3d_debug_vkresult(vr));
        return VK_NULL_HANDLE;
    }

    return vk_memory;
}

307
struct wined3d_allocator_block *wined3d_context_vk_allocate_memory(struct wined3d_context_vk *context_vk,
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        unsigned int memory_type, VkDeviceSize size, VkDeviceMemory *vk_memory)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    struct wined3d_allocator *allocator = &device_vk->allocator;
    struct wined3d_allocator_block *block;

    if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
    {
        *vk_memory = wined3d_context_vk_allocate_vram_chunk_memory(context_vk, memory_type, size);
        return NULL;
    }

    if (!(block = wined3d_allocator_allocate(allocator, &context_vk->c, memory_type, size)))
    {
        *vk_memory = VK_NULL_HANDLE;
        return NULL;
    }

    *vk_memory = wined3d_allocator_chunk_vk(block->chunk)->vk_memory;

    return block;
}

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
static bool wined3d_context_vk_create_slab_bo(struct wined3d_context_vk *context_vk,
        VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags memory_type, struct wined3d_bo_vk *bo)
{
    const struct wined3d_adapter_vk *adapter_vk = wined3d_adapter_vk(context_vk->c.device->adapter);
    const VkPhysicalDeviceLimits *limits = &adapter_vk->device_limits;
    struct wined3d_bo_slab_vk_key key;
    struct wined3d_bo_slab_vk *slab;
    struct wine_rb_entry *entry;
    size_t object_size, idx;
    size_t alignment;

    if (size > WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 2)
        return false;

    alignment = WINED3D_SLAB_BO_MIN_OBJECT_ALIGN;
    if ((usage & (VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT))
            && limits->minTexelBufferOffsetAlignment > alignment)
        alignment = limits->minTexelBufferOffsetAlignment;
    if ((usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) && limits->minUniformBufferOffsetAlignment)
        alignment = limits->minUniformBufferOffsetAlignment;
    if ((usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) && limits->minStorageBufferOffsetAlignment)
        alignment = limits->minStorageBufferOffsetAlignment;

    object_size = (size + (alignment - 1)) & ~(alignment - 1);
    if (object_size < WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 32)
        object_size = WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 32;
    key.memory_type = memory_type;
    key.usage = usage;
    key.size = 32 * object_size;

    if ((entry = wine_rb_get(&context_vk->bo_slab_available, &key)))
    {
        slab = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
        TRACE("Using existing bo slab %p.\n", slab);
    }
    else
    {
        if (!(slab = heap_alloc_zero(sizeof(*slab))))
        {
            ERR("Failed to allocate bo slab.\n");
            return false;
        }

        if (!wined3d_context_vk_create_bo(context_vk, key.size, usage, memory_type, &slab->bo))
        {
            ERR("Failed to create slab bo.\n");
            heap_free(slab);
            return false;
        }
        slab->map = ~0u;

        if (wine_rb_put(&context_vk->bo_slab_available, &key, &slab->entry) < 0)
        {
            ERR("Failed to add slab to available tree.\n");
            wined3d_context_vk_destroy_bo(context_vk, &slab->bo);
            heap_free(slab);
            return false;
        }

        TRACE("Created new bo slab %p.\n", slab);
    }

    idx = wined3d_bit_scan(&slab->map);
    if (!slab->map)
    {
        if (slab->next)
        {
            wine_rb_replace(&context_vk->bo_slab_available, &slab->entry, &slab->next->entry);
            slab->next = NULL;
        }
        else
        {
            wine_rb_remove(&context_vk->bo_slab_available, &slab->entry);
        }
    }

    *bo = slab->bo;
    bo->memory = NULL;
    bo->slab = slab;
    bo->buffer_offset = idx * object_size;
    bo->memory_offset = slab->bo.memory_offset + bo->buffer_offset;
    bo->size = size;
413
    list_init(&bo->users);
414 415 416 417 418 419 420 421 422
    bo->command_buffer_id = 0;

    TRACE("Using buffer 0x%s, memory 0x%s, offset 0x%s for bo %p.\n",
            wine_dbgstr_longlong(bo->vk_buffer), wine_dbgstr_longlong(bo->vk_memory),
            wine_dbgstr_longlong(bo->buffer_offset), bo);

    return true;
}

423 424 425 426 427 428 429 430
BOOL wined3d_context_vk_create_bo(struct wined3d_context_vk *context_vk, VkDeviceSize size,
        VkBufferUsageFlags usage, VkMemoryPropertyFlags memory_type, struct wined3d_bo_vk *bo)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    VkMemoryRequirements memory_requirements;
    struct wined3d_adapter_vk *adapter_vk;
    VkBufferCreateInfo create_info;
431
    unsigned int memory_type_idx;
432 433
    VkResult vr;

434 435 436
    if (wined3d_context_vk_create_slab_bo(context_vk, size, usage, memory_type, bo))
        return TRUE;

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);

    create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
    create_info.pNext = NULL;
    create_info.flags = 0;
    create_info.size = size;
    create_info.usage = usage;
    create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
    create_info.queueFamilyIndexCount = 0;
    create_info.pQueueFamilyIndices = NULL;

    if ((vr = VK_CALL(vkCreateBuffer(device_vk->vk_device, &create_info, NULL, &bo->vk_buffer))) < 0)
    {
        ERR("Failed to create Vulkan buffer, vr %s.\n", wined3d_debug_vkresult(vr));
        return FALSE;
    }

    VK_CALL(vkGetBufferMemoryRequirements(device_vk->vk_device, bo->vk_buffer, &memory_requirements));

456
    memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
457
            memory_requirements.memoryTypeBits, memory_type);
458
    if (memory_type_idx == ~0u)
459 460 461 462 463
    {
        ERR("Failed to find suitable memory type.\n");
        VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
        return FALSE;
    }
464 465 466
    bo->memory = wined3d_context_vk_allocate_memory(context_vk,
            memory_type_idx, memory_requirements.size, &bo->vk_memory);
    if (!bo->vk_memory)
467
    {
468
        ERR("Failed to allocate buffer memory.\n");
469 470 471
        VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
        return FALSE;
    }
472
    bo->memory_offset = bo->memory ? bo->memory->offset : 0;
473

474 475
    if ((vr = VK_CALL(vkBindBufferMemory(device_vk->vk_device, bo->vk_buffer,
            bo->vk_memory, bo->memory_offset))) < 0)
476 477
    {
        ERR("Failed to bind buffer memory, vr %s.\n", wined3d_debug_vkresult(vr));
478 479 480 481
        if (bo->memory)
            wined3d_allocator_block_free(bo->memory);
        else
            VK_CALL(vkFreeMemory(device_vk->vk_device, bo->vk_memory, NULL));
482 483 484 485
        VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
        return FALSE;
    }

486
    bo->map_ptr = NULL;
487 488 489
    bo->buffer_offset = 0;
    bo->size = size;
    bo->usage = usage;
490
    bo->memory_type = adapter_vk->memory_properties.memoryTypes[memory_type_idx].propertyFlags;
491
    list_init(&bo->users);
492
    bo->command_buffer_id = 0;
493
    bo->slab = NULL;
494 495 496

    TRACE("Created buffer 0x%s, memory 0x%s for bo %p.\n",
            wine_dbgstr_longlong(bo->vk_buffer), wine_dbgstr_longlong(bo->vk_memory), bo);
497

498 499 500
    return TRUE;
}

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
static struct wined3d_retired_object_vk *wined3d_context_vk_get_retired_object_vk(struct wined3d_context_vk *context_vk)
{
    struct wined3d_retired_objects_vk *retired = &context_vk->retired;
    struct wined3d_retired_object_vk *o;

    if (retired->free)
    {
        o = retired->free;
        retired->free = o->u.next;
        return o;
    }

    if (!wined3d_array_reserve((void **)&retired->objects, &retired->size,
            retired->count + 1, sizeof(*retired->objects)))
        return NULL;

    return &retired->objects[retired->count++];
}

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
void wined3d_context_vk_destroy_framebuffer(struct wined3d_context_vk *context_vk,
        VkFramebuffer vk_framebuffer, uint64_t command_buffer_id)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, vk_framebuffer, NULL));
        TRACE("Destroyed framebuffer 0x%s.\n", wine_dbgstr_longlong(vk_framebuffer));
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking framebuffer 0x%s.\n", wine_dbgstr_longlong(vk_framebuffer));
        return;
    }

    o->type = WINED3D_RETIRED_FRAMEBUFFER_VK;
    o->u.vk_framebuffer = vk_framebuffer;
    o->command_buffer_id = command_buffer_id;
}

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
static void wined3d_context_vk_destroy_descriptor_pool(struct wined3d_context_vk *context_vk,
        VkDescriptorPool vk_descriptor_pool, uint64_t command_buffer_id)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, vk_descriptor_pool, NULL));
        TRACE("Destroyed descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
        return;
    }

    o->type = WINED3D_RETIRED_DESCRIPTOR_POOL_VK;
    o->u.vk_descriptor_pool = vk_descriptor_pool;
    o->command_buffer_id = command_buffer_id;
}

570
void wined3d_context_vk_destroy_memory(struct wined3d_context_vk *context_vk,
571
        VkDeviceMemory vk_memory, uint64_t command_buffer_id)
572 573 574
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
575 576 577 578 579 580 581 582
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        VK_CALL(vkFreeMemory(device_vk->vk_device, vk_memory, NULL));
        TRACE("Freed memory 0x%s.\n", wine_dbgstr_longlong(vk_memory));
        return;
    }
583

584 585 586 587 588 589 590 591 592 593 594
    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking memory 0x%s.\n", wine_dbgstr_longlong(vk_memory));
        return;
    }

    o->type = WINED3D_RETIRED_MEMORY_VK;
    o->u.vk_memory = vk_memory;
    o->command_buffer_id = command_buffer_id;
}

595
void wined3d_context_vk_destroy_allocator_block(struct wined3d_context_vk *context_vk,
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
        struct wined3d_allocator_block *block, uint64_t command_buffer_id)
{
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        wined3d_allocator_block_free(block);
        TRACE("Freed block %p.\n", block);
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking block %p.\n", block);
        return;
    }

    o->type = WINED3D_RETIRED_ALLOCATOR_BLOCK_VK;
    o->u.block = block;
    o->command_buffer_id = command_buffer_id;
}

618
static void wined3d_bo_slab_vk_free_slice(struct wined3d_bo_slab_vk *slab,
619
        SIZE_T idx, struct wined3d_context_vk *context_vk)
620 621 622 623
{
    struct wined3d_bo_slab_vk_key key;
    struct wine_rb_entry *entry;

624
    TRACE("slab %p, idx %lu, context_vk %p.\n", slab, idx, context_vk);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

    if (!slab->map)
    {
        key.memory_type = slab->bo.memory_type;
        key.usage = slab->bo.usage;
        key.size = slab->bo.size;

        if ((entry = wine_rb_get(&context_vk->bo_slab_available, &key)))
        {
            slab->next = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
            wine_rb_replace(&context_vk->bo_slab_available, entry, &slab->entry);
        }
        else if (wine_rb_put(&context_vk->bo_slab_available, &key, &slab->entry) < 0)
        {
            ERR("Unable to return slab %p (map 0x%08x) to available tree.\n", slab, slab->map);
        }
    }
    slab->map |= 1u << idx;
}

static void wined3d_context_vk_destroy_bo_slab_slice(struct wined3d_context_vk *context_vk,
646
        struct wined3d_bo_slab_vk *slab, SIZE_T idx, uint64_t command_buffer_id)
647 648 649 650 651 652 653 654 655 656 657
{
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        wined3d_bo_slab_vk_free_slice(slab, idx, context_vk);
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
658
        ERR("Leaking slab %p, slice %#lx.\n", slab, idx);
659 660 661 662 663 664 665 666 667
        return;
    }

    o->type = WINED3D_RETIRED_BO_SLAB_SLICE_VK;
    o->u.slice.slab = slab;
    o->u.slice.idx = idx;
    o->command_buffer_id = command_buffer_id;
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
static void wined3d_context_vk_destroy_buffer(struct wined3d_context_vk *context_vk,
        VkBuffer vk_buffer, uint64_t command_buffer_id)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        VK_CALL(vkDestroyBuffer(device_vk->vk_device, vk_buffer, NULL));
        TRACE("Destroyed buffer 0x%s.\n", wine_dbgstr_longlong(vk_buffer));
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking buffer 0x%s.\n", wine_dbgstr_longlong(vk_buffer));
        return;
    }

    o->type = WINED3D_RETIRED_BUFFER_VK;
    o->u.vk_buffer = vk_buffer;
    o->command_buffer_id = command_buffer_id;
}

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
void wined3d_context_vk_destroy_image(struct wined3d_context_vk *context_vk,
        VkImage vk_image, uint64_t command_buffer_id)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        VK_CALL(vkDestroyImage(device_vk->vk_device, vk_image, NULL));
        TRACE("Destroyed image 0x%s.\n", wine_dbgstr_longlong(vk_image));
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking image 0x%s.\n", wine_dbgstr_longlong(vk_image));
        return;
    }

    o->type = WINED3D_RETIRED_IMAGE_VK;
    o->u.vk_image = vk_image;
    o->command_buffer_id = command_buffer_id;
}

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
void wined3d_context_vk_destroy_buffer_view(struct wined3d_context_vk *context_vk,
        VkBufferView vk_view, uint64_t command_buffer_id)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        VK_CALL(vkDestroyBufferView(device_vk->vk_device, vk_view, NULL));
        TRACE("Destroyed buffer view 0x%s.\n", wine_dbgstr_longlong(vk_view));
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking buffer view 0x%s.\n", wine_dbgstr_longlong(vk_view));
        return;
    }

    o->type = WINED3D_RETIRED_BUFFER_VIEW_VK;
    o->u.vk_buffer_view = vk_view;
    o->command_buffer_id = command_buffer_id;
}

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
void wined3d_context_vk_destroy_image_view(struct wined3d_context_vk *context_vk,
        VkImageView vk_view, uint64_t command_buffer_id)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        VK_CALL(vkDestroyImageView(device_vk->vk_device, vk_view, NULL));
        TRACE("Destroyed image view 0x%s.\n", wine_dbgstr_longlong(vk_view));
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking image view 0x%s.\n", wine_dbgstr_longlong(vk_view));
        return;
    }

    o->type = WINED3D_RETIRED_IMAGE_VIEW_VK;
    o->u.vk_image_view = vk_view;
    o->command_buffer_id = command_buffer_id;
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
void wined3d_context_vk_destroy_sampler(struct wined3d_context_vk *context_vk,
        VkSampler vk_sampler, uint64_t command_buffer_id)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_retired_object_vk *o;

    if (context_vk->completed_command_buffer_id > command_buffer_id)
    {
        VK_CALL(vkDestroySampler(device_vk->vk_device, vk_sampler, NULL));
        TRACE("Destroyed sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
        return;
    }

    if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
    {
        ERR("Leaking sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
        return;
    }

    o->type = WINED3D_RETIRED_SAMPLER_VK;
    o->u.vk_sampler = vk_sampler;
    o->command_buffer_id = command_buffer_id;
}

793 794
void wined3d_context_vk_destroy_bo(struct wined3d_context_vk *context_vk, const struct wined3d_bo_vk *bo)
{
795 796
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
797 798
    size_t object_size, idx;

799 800
    TRACE("context_vk %p, bo %p.\n", context_vk, bo);

801 802 803 804 805 806 807 808
    if (bo->slab)
    {
        object_size = bo->slab->bo.size / 32;
        idx = bo->buffer_offset / object_size;
        wined3d_context_vk_destroy_bo_slab_slice(context_vk, bo->slab, idx, bo->command_buffer_id);
        return;
    }

809
    wined3d_context_vk_destroy_buffer(context_vk, bo->vk_buffer, bo->command_buffer_id);
810 811 812 813 814 815
    if (bo->memory)
    {
        wined3d_context_vk_destroy_allocator_block(context_vk, bo->memory, bo->command_buffer_id);
        return;
    }

816 817
    if (bo->map_ptr)
        VK_CALL(vkUnmapMemory(device_vk->vk_device, bo->vk_memory));
818
    wined3d_context_vk_destroy_memory(context_vk, bo->vk_memory, bo->command_buffer_id);
819 820
}

821
void wined3d_context_vk_poll_command_buffers(struct wined3d_context_vk *context_vk)
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_command_buffer_vk *buffer;
    SIZE_T i = 0;

    while (i < context_vk->submitted.buffer_count)
    {
        buffer = &context_vk->submitted.buffers[i];
        if (VK_CALL(vkGetFenceStatus(device_vk->vk_device, buffer->vk_fence)) == VK_NOT_READY)
        {
            ++i;
            continue;
        }

        TRACE("Command buffer %p with id 0x%s has finished.\n",
                buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
        VK_CALL(vkDestroyFence(device_vk->vk_device, buffer->vk_fence, NULL));
        VK_CALL(vkFreeCommandBuffers(device_vk->vk_device,
                context_vk->vk_command_pool, 1, &buffer->vk_command_buffer));

        if (buffer->id > context_vk->completed_command_buffer_id)
            context_vk->completed_command_buffer_id = buffer->id;
        *buffer = context_vk->submitted.buffers[--context_vk->submitted.buffer_count];
    }
847 848 849 850 851 852 853 854 855 856 857 858
}

static void wined3d_context_vk_cleanup_resources(struct wined3d_context_vk *context_vk)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    struct wined3d_retired_objects_vk *retired = &context_vk->retired;
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_retired_object_vk *o;
    uint64_t command_buffer_id;
    SIZE_T i = 0;

    wined3d_context_vk_poll_command_buffers(context_vk);
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
    command_buffer_id = context_vk->completed_command_buffer_id;

    retired->free = NULL;
    for (i = retired->count; i; --i)
    {
        o = &retired->objects[i - 1];

        if (o->type != WINED3D_RETIRED_FREE_VK && o->command_buffer_id > command_buffer_id)
            continue;

        switch (o->type)
        {
            case WINED3D_RETIRED_FREE_VK:
                /* Nothing to do. */
                break;

875 876 877 878 879
            case WINED3D_RETIRED_FRAMEBUFFER_VK:
                VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, o->u.vk_framebuffer, NULL));
                TRACE("Destroyed framebuffer 0x%s.\n", wine_dbgstr_longlong(o->u.vk_framebuffer));
                break;

880 881 882 883 884
            case WINED3D_RETIRED_DESCRIPTOR_POOL_VK:
                VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, o->u.vk_descriptor_pool, NULL));
                TRACE("Destroyed descriptor pool 0x%s.\n", wine_dbgstr_longlong(o->u.vk_descriptor_pool));
                break;

885 886 887 888 889
            case WINED3D_RETIRED_MEMORY_VK:
                VK_CALL(vkFreeMemory(device_vk->vk_device, o->u.vk_memory, NULL));
                TRACE("Freed memory 0x%s.\n", wine_dbgstr_longlong(o->u.vk_memory));
                break;

890 891 892 893 894
            case WINED3D_RETIRED_ALLOCATOR_BLOCK_VK:
                TRACE("Destroying block %p.\n", o->u.block);
                wined3d_allocator_block_free(o->u.block);
                break;

895 896 897 898
            case WINED3D_RETIRED_BO_SLAB_SLICE_VK:
                wined3d_bo_slab_vk_free_slice(o->u.slice.slab, o->u.slice.idx, context_vk);
                break;

899 900 901 902 903
            case WINED3D_RETIRED_BUFFER_VK:
                VK_CALL(vkDestroyBuffer(device_vk->vk_device, o->u.vk_buffer, NULL));
                TRACE("Destroyed buffer 0x%s.\n", wine_dbgstr_longlong(o->u.vk_buffer));
                break;

904 905 906 907 908
            case WINED3D_RETIRED_IMAGE_VK:
                VK_CALL(vkDestroyImage(device_vk->vk_device, o->u.vk_image, NULL));
                TRACE("Destroyed image 0x%s.\n", wine_dbgstr_longlong(o->u.vk_image));
                break;

909 910 911 912 913
            case WINED3D_RETIRED_BUFFER_VIEW_VK:
                VK_CALL(vkDestroyBufferView(device_vk->vk_device, o->u.vk_buffer_view, NULL));
                TRACE("Destroyed buffer view 0x%s.\n", wine_dbgstr_longlong(o->u.vk_buffer_view));
                break;

914 915 916 917 918
            case WINED3D_RETIRED_IMAGE_VIEW_VK:
                VK_CALL(vkDestroyImageView(device_vk->vk_device, o->u.vk_image_view, NULL));
                TRACE("Destroyed image view 0x%s.\n", wine_dbgstr_longlong(o->u.vk_image_view));
                break;

919 920 921 922 923
            case WINED3D_RETIRED_SAMPLER_VK:
                VK_CALL(vkDestroySampler(device_vk->vk_device, o->u.vk_sampler, NULL));
                TRACE("Destroyed sampler 0x%s.\n", wine_dbgstr_longlong(o->u.vk_sampler));
                break;

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
            default:
                ERR("Unhandled object type %#x.\n", o->type);
                break;
        }

        if (i == retired->count)
        {
            --retired->count;
            continue;
        }

        o->type = WINED3D_RETIRED_FREE_VK;
        o->u.next = retired->free;
        retired->free = o;
    }
939 940
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
static void wined3d_context_vk_destroy_bo_slab(struct wine_rb_entry *entry, void *ctx)
{
    struct wined3d_context_vk *context_vk = ctx;
    struct wined3d_bo_slab_vk *slab, *next;

    slab = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
    while (slab)
    {
        next = slab->next;
        wined3d_context_vk_destroy_bo(context_vk, &slab->bo);
        heap_free(slab);
        slab = next;
    }
}

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
static void wined3d_context_vk_destroy_graphics_pipeline(struct wine_rb_entry *entry, void *ctx)
{
    struct wined3d_graphics_pipeline_vk *pipeline_vk = WINE_RB_ENTRY_VALUE(entry,
            struct wined3d_graphics_pipeline_vk, entry);
    struct wined3d_context_vk *context_vk = ctx;
    const struct wined3d_vk_info *vk_info;
    struct wined3d_device_vk *device_vk;

    vk_info = context_vk->vk_info;
    device_vk = wined3d_device_vk(context_vk->c.device);

    VK_CALL(vkDestroyPipeline(device_vk->vk_device, pipeline_vk->vk_pipeline, NULL));
    heap_free(pipeline_vk);
}

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
static void wined3d_context_vk_destroy_pipeline_layout(struct wine_rb_entry *entry, void *ctx)
{
    struct wined3d_pipeline_layout_vk *layout = WINE_RB_ENTRY_VALUE(entry,
            struct wined3d_pipeline_layout_vk, entry);
    struct wined3d_context_vk *context_vk = ctx;
    const struct wined3d_vk_info *vk_info;
    struct wined3d_device_vk *device_vk;

    vk_info = context_vk->vk_info;
    device_vk = wined3d_device_vk(context_vk->c.device);

    VK_CALL(vkDestroyPipelineLayout(device_vk->vk_device, layout->vk_pipeline_layout, NULL));
    VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
    heap_free(layout->key.bindings);
    heap_free(layout);
}

988
static void wined3d_render_pass_key_vk_init(struct wined3d_render_pass_key_vk *key,
989
        const struct wined3d_fb_state *fb, unsigned int rt_count, bool depth_stencil, uint32_t clear_flags)
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
{
    struct wined3d_render_pass_attachment_vk *a;
    struct wined3d_rendertarget_view *view;
    unsigned int i;

    memset(key, 0, sizeof(*key));

    for (i = 0; i < rt_count; ++i)
    {
        if (!(view = fb->render_targets[i]) || view->format->id == WINED3DFMT_NULL)
            continue;

        a = &key->rt[i];
        a->vk_format = wined3d_format_vk(view->format)->vk_format;
        a->vk_samples = max(1, wined3d_resource_get_sample_count(view->resource));
        a->vk_layout = wined3d_texture_vk(wined3d_texture_from_resource(view->resource))->layout;
        key->rt_mask |= 1u << i;
    }
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

    if (depth_stencil && (view = fb->depth_stencil))
    {
        a = &key->ds;
        a->vk_format = wined3d_format_vk(view->format)->vk_format;
        a->vk_samples = max(1, wined3d_resource_get_sample_count(view->resource));
        a->vk_layout = wined3d_texture_vk(wined3d_texture_from_resource(view->resource))->layout;
        key->rt_mask |= 1u << WINED3D_MAX_RENDER_TARGETS;
    }

    key->clear_flags = clear_flags;
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
}

static void wined3d_render_pass_vk_cleanup(struct wined3d_render_pass_vk *pass,
        struct wined3d_context_vk *context_vk)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;

    VK_CALL(vkDestroyRenderPass(device_vk->vk_device, pass->vk_render_pass, NULL));
}

static bool wined3d_render_pass_vk_init(struct wined3d_render_pass_vk *pass,
        struct wined3d_context_vk *context_vk, const struct wined3d_render_pass_key_vk *key)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    VkAttachmentReference attachment_references[WINED3D_MAX_RENDER_TARGETS];
    VkAttachmentDescription attachments[WINED3D_MAX_RENDER_TARGETS + 1];
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    const struct wined3d_render_pass_attachment_vk *a;
1038 1039
    VkAttachmentReference ds_attachment_reference;
    VkAttachmentReference *ds_reference = NULL;
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    unsigned int attachment_count, rt_count, i;
    VkAttachmentDescription *attachment;
    VkSubpassDescription sub_pass_desc;
    VkRenderPassCreateInfo pass_desc;
    uint32_t mask;
    VkResult vr;

    rt_count = 0;
    attachment_count = 0;
    mask = key->rt_mask & ((1u << WINED3D_MAX_RENDER_TARGETS) - 1);
    while (mask)
    {
        i = wined3d_bit_scan(&mask);
        a = &key->rt[i];

        attachment = &attachments[attachment_count];
        attachment->flags = 0;
        attachment->format = a->vk_format;
        attachment->samples = a->vk_samples;
1059 1060 1061 1062
        if (key->clear_flags & WINED3DCLEAR_TARGET)
            attachment->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
        else
            attachment->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        attachment->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
        attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
        attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
        attachment->initialLayout = a->vk_layout;
        attachment->finalLayout = a->vk_layout;

        attachment_references[i].attachment = attachment_count;
        attachment_references[i].layout = a->vk_layout;

        ++attachment_count;
        rt_count = i + 1;
    }

    mask = ~key->rt_mask & ((1u << rt_count) - 1);
    while (mask)
    {
        i = wined3d_bit_scan(&mask);
        attachment_references[i].attachment = VK_ATTACHMENT_UNUSED;
        attachment_references[i].layout = VK_IMAGE_LAYOUT_UNDEFINED;
    }

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    if (key->rt_mask & (1u << WINED3D_MAX_RENDER_TARGETS))
    {
        a = &key->ds;

        attachment = &attachments[attachment_count];
        attachment->flags = 0;
        attachment->format = a->vk_format;
        attachment->samples = a->vk_samples;
        if (key->clear_flags & WINED3DCLEAR_ZBUFFER)
            attachment->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
        else
            attachment->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
        attachment->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
        if (key->clear_flags & WINED3DCLEAR_STENCIL)
            attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
        else
            attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
        attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
        attachment->initialLayout = a->vk_layout;
        attachment->finalLayout = a->vk_layout;

        ds_reference = &ds_attachment_reference;
        ds_reference->attachment = attachment_count;
        ds_reference->layout = a->vk_layout;

        ++attachment_count;
    }

1112 1113 1114 1115 1116 1117 1118
    sub_pass_desc.flags = 0;
    sub_pass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
    sub_pass_desc.inputAttachmentCount = 0;
    sub_pass_desc.pInputAttachments = NULL;
    sub_pass_desc.colorAttachmentCount = rt_count;
    sub_pass_desc.pColorAttachments = attachment_references;
    sub_pass_desc.pResolveAttachments = NULL;
1119
    sub_pass_desc.pDepthStencilAttachment = ds_reference;
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    sub_pass_desc.preserveAttachmentCount = 0;
    sub_pass_desc.pPreserveAttachments = NULL;

    pass_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
    pass_desc.pNext = NULL;
    pass_desc.flags = 0;
    pass_desc.attachmentCount = attachment_count;
    pass_desc.pAttachments = attachments;
    pass_desc.subpassCount = 1;
    pass_desc.pSubpasses = &sub_pass_desc;
    pass_desc.dependencyCount = 0;
    pass_desc.pDependencies = NULL;

    pass->key = *key;
    if ((vr = VK_CALL(vkCreateRenderPass(device_vk->vk_device,
            &pass_desc, NULL, &pass->vk_render_pass))) < 0)
    {
        WARN("Failed to create Vulkan render pass, vr %d.\n", vr);
        return false;
    }

    return true;
}

VkRenderPass wined3d_context_vk_get_render_pass(struct wined3d_context_vk *context_vk,
1145
        const struct wined3d_fb_state *fb, unsigned int rt_count, bool depth_stencil, uint32_t clear_flags)
1146 1147 1148 1149 1150
{
    struct wined3d_render_pass_key_vk key;
    struct wined3d_render_pass_vk *pass;
    struct wine_rb_entry *entry;

1151
    wined3d_render_pass_key_vk_init(&key, fb, rt_count, depth_stencil, clear_flags);
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
    if ((entry = wine_rb_get(&context_vk->render_passes, &key)))
        return WINE_RB_ENTRY_VALUE(entry, struct wined3d_render_pass_vk, entry)->vk_render_pass;

    if (!(pass = heap_alloc(sizeof(*pass))))
        return VK_NULL_HANDLE;

    if (!wined3d_render_pass_vk_init(pass, context_vk, &key))
    {
        heap_free(pass);
        return VK_NULL_HANDLE;
    }

    if (wine_rb_put(&context_vk->render_passes, &pass->key, &pass->entry) == -1)
    {
        ERR("Failed to insert render pass.\n");
        wined3d_render_pass_vk_cleanup(pass, context_vk);
        heap_free(pass);
        return VK_NULL_HANDLE;
    }

    return pass->vk_render_pass;
}

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
void wined3d_context_vk_end_current_render_pass(struct wined3d_context_vk *context_vk)
{
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    VkCommandBuffer vk_command_buffer;

    if (context_vk->vk_render_pass)
    {
        vk_command_buffer = context_vk->current_command_buffer.vk_command_buffer;
        VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
        context_vk->vk_render_pass = VK_NULL_HANDLE;
        VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
                VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 0, NULL));
    }

    if (context_vk->vk_framebuffer)
    {
        wined3d_context_vk_destroy_framebuffer(context_vk,
                context_vk->vk_framebuffer, context_vk->current_command_buffer.id);
        context_vk->vk_framebuffer = VK_NULL_HANDLE;
    }
}

1197 1198 1199 1200 1201 1202 1203 1204 1205
static void wined3d_context_vk_destroy_render_pass(struct wine_rb_entry *entry, void *ctx)
{
    struct wined3d_render_pass_vk *pass = WINE_RB_ENTRY_VALUE(entry,
            struct wined3d_render_pass_vk, entry);

    wined3d_render_pass_vk_cleanup(pass, ctx);
    heap_free(pass);
}

1206 1207 1208 1209 1210
static void wined3d_shader_descriptor_writes_vk_cleanup(struct wined3d_shader_descriptor_writes_vk *writes)
{
    heap_free(writes->writes);
}

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
static void wined3d_context_vk_destroy_query_pools(struct wined3d_context_vk *context_vk, struct list *free_pools)
{
    struct wined3d_query_pool_vk *pool_vk, *entry;

    LIST_FOR_EACH_ENTRY_SAFE(pool_vk, entry, free_pools, struct wined3d_query_pool_vk, entry)
    {
        wined3d_query_pool_vk_cleanup(pool_vk, context_vk);
        heap_free(pool_vk);
    }
}

bool wined3d_context_vk_allocate_query(struct wined3d_context_vk *context_vk,
        enum wined3d_query_type type, struct wined3d_query_pool_idx_vk *pool_idx)
{
    struct wined3d_query_pool_vk *pool_vk, *entry;
    struct list *free_pools;
    size_t idx;

    switch (type)
    {
        case WINED3D_QUERY_TYPE_OCCLUSION:
            free_pools = &context_vk->free_occlusion_query_pools;
            break;

1235 1236 1237 1238
        case WINED3D_QUERY_TYPE_TIMESTAMP:
            free_pools = &context_vk->free_timestamp_query_pools;
            break;

1239 1240 1241 1242
        case WINED3D_QUERY_TYPE_PIPELINE_STATISTICS:
            free_pools = &context_vk->free_pipeline_statistics_query_pools;
            break;

1243 1244 1245 1246 1247 1248 1249 1250
        case WINED3D_QUERY_TYPE_SO_STATISTICS:
        case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM0:
        case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM1:
        case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM2:
        case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM3:
            free_pools = &context_vk->free_stream_output_statistics_query_pools;
            break;

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
        default:
            FIXME("Unhandled query type %#x.\n", type);
            return false;
    }

    LIST_FOR_EACH_ENTRY_SAFE(pool_vk, entry, free_pools, struct wined3d_query_pool_vk, entry)
    {
        if (wined3d_query_pool_vk_allocate_query(pool_vk, &idx))
            goto done;
        list_remove(&pool_vk->entry);
    }

    if (!(pool_vk = heap_alloc_zero(sizeof(*pool_vk))))
        return false;
    if (!wined3d_query_pool_vk_init(pool_vk, context_vk, type, free_pools))
    {
        heap_free(pool_vk);
        return false;
    }

    if (!wined3d_query_pool_vk_allocate_query(pool_vk, &idx))
    {
        wined3d_query_pool_vk_cleanup(pool_vk, context_vk);
        heap_free(pool_vk);
        return false;
    }

done:
    pool_idx->pool_vk = pool_vk;
    pool_idx->idx = idx;

    return true;
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
void wined3d_context_vk_cleanup(struct wined3d_context_vk *context_vk)
{
    struct wined3d_command_buffer_vk *buffer = &context_vk->current_command_buffer;
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;

    if (buffer->vk_command_buffer)
    {
        VK_CALL(vkFreeCommandBuffers(device_vk->vk_device,
                context_vk->vk_command_pool, 1, &buffer->vk_command_buffer));
        buffer->vk_command_buffer = VK_NULL_HANDLE;
    }

    wined3d_context_vk_wait_command_buffer(context_vk, buffer->id - 1);
    context_vk->completed_command_buffer_id = buffer->id;
1300 1301

    heap_free(context_vk->compute.bindings.bindings);
1302
    heap_free(context_vk->graphics.bindings.bindings);
1303 1304
    if (context_vk->vk_descriptor_pool)
        VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, context_vk->vk_descriptor_pool, NULL));
1305 1306 1307
    if (context_vk->vk_framebuffer)
        VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, context_vk->vk_framebuffer, NULL));
    VK_CALL(vkDestroyCommandPool(device_vk->vk_device, context_vk->vk_command_pool, NULL));
1308 1309
    if (context_vk->vk_so_counter_bo.vk_buffer)
        wined3d_context_vk_destroy_bo(context_vk, &context_vk->vk_so_counter_bo);
1310
    wined3d_context_vk_cleanup_resources(context_vk);
1311
    wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_occlusion_query_pools);
1312
    wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_timestamp_query_pools);
1313
    wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_pipeline_statistics_query_pools);
1314
    wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_stream_output_statistics_query_pools);
1315
    wine_rb_destroy(&context_vk->bo_slab_available, wined3d_context_vk_destroy_bo_slab, context_vk);
1316
    heap_free(context_vk->pending_queries.queries);
1317
    heap_free(context_vk->submitted.buffers);
1318 1319
    heap_free(context_vk->retired.objects);

1320
    wined3d_shader_descriptor_writes_vk_cleanup(&context_vk->descriptor_writes);
1321
    wine_rb_destroy(&context_vk->graphics_pipelines, wined3d_context_vk_destroy_graphics_pipeline, context_vk);
1322
    wine_rb_destroy(&context_vk->pipeline_layouts, wined3d_context_vk_destroy_pipeline_layout, context_vk);
1323 1324
    wine_rb_destroy(&context_vk->render_passes, wined3d_context_vk_destroy_render_pass, context_vk);

1325 1326 1327
    wined3d_context_cleanup(&context_vk->c);
}

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
void wined3d_context_vk_remove_pending_queries(struct wined3d_context_vk *context_vk,
        struct wined3d_query_vk *query_vk)
{
    struct wined3d_pending_queries_vk *pending = &context_vk->pending_queries;
    struct wined3d_pending_query_vk *p;
    size_t i;

    pending->free_idx = ~(size_t)0;
    for (i = pending->count; i; --i)
    {
        p = &pending->queries[i - 1];

        if (p->query_vk)
        {
            if (p->query_vk != query_vk && !wined3d_query_vk_accumulate_data(p->query_vk, context_vk, &p->pool_idx))
                continue;
            wined3d_query_pool_vk_free_query(p->pool_idx.pool_vk, p->pool_idx.idx);
            --p->query_vk->pending_count;
        }

        if (i == pending->count)
        {
            --pending->count;
            continue;
        }

        p->query_vk = NULL;
        p->pool_idx.pool_vk = NULL;
        p->pool_idx.idx = pending->free_idx;
        pending->free_idx = i - 1;
    }
}

void wined3d_context_vk_accumulate_pending_queries(struct wined3d_context_vk *context_vk)
{
    wined3d_context_vk_remove_pending_queries(context_vk, NULL);
}

void wined3d_context_vk_add_pending_query(struct wined3d_context_vk *context_vk, struct wined3d_query_vk *query_vk)
{
    struct wined3d_pending_queries_vk *pending = &context_vk->pending_queries;
    struct wined3d_pending_query_vk *p;

    if (pending->free_idx != ~(size_t)0)
    {
        p = &pending->queries[pending->free_idx];
        pending->free_idx = p->pool_idx.idx;
    }
    else
    {
        if (!wined3d_array_reserve((void **)&pending->queries, &pending->size,
                pending->count + 1, sizeof(*pending->queries)))
        {
            ERR("Failed to allocate entry.\n");
            return;
        }
        p = &pending->queries[pending->count++];
    }

    p->query_vk = query_vk;
    p->pool_idx = query_vk->pool_idx;
    ++query_vk->pending_count;
}

1392 1393 1394 1395 1396 1397 1398
VkCommandBuffer wined3d_context_vk_get_command_buffer(struct wined3d_context_vk *context_vk)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    VkCommandBufferAllocateInfo command_buffer_info;
    struct wined3d_command_buffer_vk *buffer;
    VkCommandBufferBeginInfo begin_info;
1399
    struct wined3d_query_vk *query_vk;
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    VkResult vr;

    TRACE("context_vk %p.\n", context_vk);

    buffer = &context_vk->current_command_buffer;
    if (buffer->vk_command_buffer)
    {
        TRACE("Returning existing command buffer %p with id 0x%s.\n",
                buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
        return buffer->vk_command_buffer;
    }

    command_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
    command_buffer_info.pNext = NULL;
    command_buffer_info.commandPool = context_vk->vk_command_pool;
    command_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    command_buffer_info.commandBufferCount = 1;
    if ((vr = VK_CALL(vkAllocateCommandBuffers(device_vk->vk_device,
            &command_buffer_info, &buffer->vk_command_buffer))) < 0)
    {
        WARN("Failed to allocate Vulkan command buffer, vr %s.\n", wined3d_debug_vkresult(vr));
        return VK_NULL_HANDLE;
    }

    begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    begin_info.pNext = NULL;
    begin_info.flags = 0;
    begin_info.pInheritanceInfo = NULL;
    if ((vr = VK_CALL(vkBeginCommandBuffer(buffer->vk_command_buffer, &begin_info))) < 0)
    {
        WARN("Failed to begin command buffer, vr %s.\n", wined3d_debug_vkresult(vr));
        VK_CALL(vkFreeCommandBuffers(device_vk->vk_device, context_vk->vk_command_pool,
                1, &buffer->vk_command_buffer));
        return buffer->vk_command_buffer = VK_NULL_HANDLE;
    }

1436 1437 1438 1439 1440 1441
    wined3d_context_vk_accumulate_pending_queries(context_vk);
    LIST_FOR_EACH_ENTRY(query_vk, &context_vk->active_queries, struct wined3d_query_vk, entry)
    {
        wined3d_query_vk_resume(query_vk, context_vk);
    }

1442 1443 1444 1445 1446 1447
    TRACE("Created new command buffer %p with id 0x%s.\n",
            buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));

    return buffer->vk_command_buffer;
}

1448 1449 1450
void wined3d_context_vk_submit_command_buffer(struct wined3d_context_vk *context_vk,
        unsigned int wait_semaphore_count, const VkSemaphore *wait_semaphores, const VkPipelineStageFlags *wait_stages,
        unsigned int signal_semaphore_count, const VkSemaphore *signal_semaphores)
1451 1452 1453 1454
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_command_buffer_vk *buffer;
1455
    struct wined3d_query_vk *query_vk;
1456 1457 1458 1459
    VkFenceCreateInfo fence_desc;
    VkSubmitInfo submit_info;
    VkResult vr;

1460 1461 1462 1463
    TRACE("context_vk %p, wait_semaphore_count %u, wait_semaphores %p, wait_stages %p,"
            "signal_semaphore_count %u, signal_semaphores %p.\n",
            context_vk, wait_semaphore_count, wait_semaphores, wait_stages,
            signal_semaphore_count, signal_semaphores);
1464 1465 1466 1467 1468 1469 1470 1471

    buffer = &context_vk->current_command_buffer;
    if (!buffer->vk_command_buffer)
        return;

    TRACE("Submitting command buffer %p with id 0x%s.\n",
            buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));

1472 1473 1474 1475 1476
    LIST_FOR_EACH_ENTRY(query_vk, &context_vk->active_queries, struct wined3d_query_vk, entry)
    {
        wined3d_query_vk_suspend(query_vk, context_vk);
    }

1477 1478
    wined3d_context_vk_end_current_render_pass(context_vk);
    context_vk->graphics.vk_pipeline = VK_NULL_HANDLE;
1479
    context_vk->update_compute_pipeline = 1;
1480
    context_vk->update_stream_output = 1;
1481
    context_vk->c.update_shader_resource_bindings = 1;
1482
    context_vk->c.update_compute_shader_resource_bindings = 1;
1483
    context_vk->c.update_unordered_access_view_bindings = 1;
1484
    context_vk->c.update_compute_unordered_access_view_bindings = 1;
1485
    context_invalidate_state(&context_vk->c, STATE_STREAMSRC);
1486
    context_invalidate_state(&context_vk->c, STATE_INDEXBUFFER);
1487
    context_invalidate_state(&context_vk->c, STATE_BLEND_FACTOR);
1488

1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    VK_CALL(vkEndCommandBuffer(buffer->vk_command_buffer));

    fence_desc.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
    fence_desc.pNext = NULL;
    fence_desc.flags = 0;
    if ((vr = VK_CALL(vkCreateFence(device_vk->vk_device, &fence_desc, NULL, &buffer->vk_fence))) < 0)
        ERR("Failed to create fence, vr %s.\n", wined3d_debug_vkresult(vr));

    submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    submit_info.pNext = NULL;
1499 1500 1501
    submit_info.waitSemaphoreCount = wait_semaphore_count;
    submit_info.pWaitSemaphores = wait_semaphores;
    submit_info.pWaitDstStageMask = wait_stages;
1502 1503
    submit_info.commandBufferCount = 1;
    submit_info.pCommandBuffers = &buffer->vk_command_buffer;
1504 1505
    submit_info.signalSemaphoreCount = signal_semaphore_count;
    submit_info.pSignalSemaphores = signal_semaphores;
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533

    if ((vr = VK_CALL(vkQueueSubmit(device_vk->vk_queue, 1, &submit_info, buffer->vk_fence))) < 0)
        ERR("Failed to submit command buffer %p, vr %s.\n",
                buffer->vk_command_buffer, wined3d_debug_vkresult(vr));

    if (!wined3d_array_reserve((void **)&context_vk->submitted.buffers, &context_vk->submitted.buffers_size,
            context_vk->submitted.buffer_count + 1, sizeof(*context_vk->submitted.buffers)))
        ERR("Failed to grow submitted command buffer array.\n");

    context_vk->submitted.buffers[context_vk->submitted.buffer_count++] = *buffer;

    buffer->vk_command_buffer = VK_NULL_HANDLE;
    /* We don't expect this to ever happen, but handle it anyway. */
    if (!++buffer->id)
    {
        wined3d_context_vk_wait_command_buffer(context_vk, buffer->id - 1);
        context_vk->completed_command_buffer_id = 0;
        buffer->id = 1;
    }
    wined3d_context_vk_cleanup_resources(context_vk);
}

void wined3d_context_vk_wait_command_buffer(struct wined3d_context_vk *context_vk, uint64_t id)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    SIZE_T i;

1534 1535
    if (id <= context_vk->completed_command_buffer_id
            || id > context_vk->current_command_buffer.id) /* In case the buffer ID wrapped. */
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
        return;

    for (i = 0; i < context_vk->submitted.buffer_count; ++i)
    {
        if (context_vk->submitted.buffers[i].id != id)
            continue;

        VK_CALL(vkWaitForFences(device_vk->vk_device, 1,
                &context_vk->submitted.buffers[i].vk_fence, VK_TRUE, UINT64_MAX));
        wined3d_context_vk_cleanup_resources(context_vk);
        return;
    }

    ERR("Failed to find fence for command buffer with id 0x%s.\n", wine_dbgstr_longlong(id));
}

1552 1553 1554 1555 1556 1557 1558 1559
void wined3d_context_vk_image_barrier(struct wined3d_context_vk *context_vk,
        VkCommandBuffer vk_command_buffer, VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
        VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask, VkImageLayout old_layout,
        VkImageLayout new_layout, VkImage image, VkImageAspectFlags aspect_mask)
{
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    VkImageMemoryBarrier barrier;

1560 1561
    wined3d_context_vk_end_current_render_pass(context_vk);

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
    barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
    barrier.pNext = NULL;
    barrier.srcAccessMask = src_access_mask;
    barrier.dstAccessMask = dst_access_mask;
    barrier.oldLayout = old_layout;
    barrier.newLayout = new_layout;
    barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    barrier.image = image;
    barrier.subresourceRange.aspectMask = aspect_mask;
    barrier.subresourceRange.baseMipLevel = 0;
    barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS;
    barrier.subresourceRange.baseArrayLayer = 0;
    barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;

    VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, src_stage_mask, dst_stage_mask, 0, 0, NULL, 0, NULL, 1, &barrier));
}

1580 1581 1582 1583 1584 1585 1586 1587 1588
static int wined3d_render_pass_vk_compare(const void *key, const struct wine_rb_entry *entry)
{
    const struct wined3d_render_pass_key_vk *k = key;
    const struct wined3d_render_pass_vk *pass = WINE_RB_ENTRY_VALUE(entry,
            const struct wined3d_render_pass_vk, entry);

    return memcmp(k, &pass->key, sizeof(*k));
}

1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
static int wined3d_pipeline_layout_vk_compare(const void *key, const struct wine_rb_entry *entry)
{
    const struct wined3d_pipeline_layout_key_vk *a = key;
    const struct wined3d_pipeline_layout_key_vk *b = &WINE_RB_ENTRY_VALUE(entry,
            const struct wined3d_pipeline_layout_vk, entry)->key;

    if (a->binding_count != b->binding_count)
        return a->binding_count - b->binding_count;
    return memcmp(a->bindings, b->bindings, a->binding_count * sizeof(*a->bindings));
}

1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
static int wined3d_graphics_pipeline_vk_compare(const void *key, const struct wine_rb_entry *entry)
{
    const struct wined3d_graphics_pipeline_key_vk *a = key;
    const struct wined3d_graphics_pipeline_key_vk *b = &WINE_RB_ENTRY_VALUE(entry,
            const struct wined3d_graphics_pipeline_vk, entry)->key;
    unsigned int i;
    int ret;

    if (a->pipeline_desc.stageCount != b->pipeline_desc.stageCount)
        return a->pipeline_desc.stageCount - b->pipeline_desc.stageCount;
    for (i = 0; i < a->pipeline_desc.stageCount; ++i)
    {
        if (a->stages[i].module != b->stages[i].module)
            return a->stages[i].module - b->stages[i].module;
    }

1616 1617 1618 1619 1620 1621
    if (a->divisor_desc.vertexBindingDivisorCount != b->divisor_desc.vertexBindingDivisorCount)
        return a->divisor_desc.vertexBindingDivisorCount - b->divisor_desc.vertexBindingDivisorCount;
    if ((ret = memcmp(a->divisors, b->divisors,
            a->divisor_desc.vertexBindingDivisorCount * sizeof(*a->divisors))))
        return ret;

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
    if (a->input_desc.vertexAttributeDescriptionCount != b->input_desc.vertexAttributeDescriptionCount)
        return a->input_desc.vertexAttributeDescriptionCount - b->input_desc.vertexAttributeDescriptionCount;
    if ((ret = memcmp(a->attributes, b->attributes,
            a->input_desc.vertexAttributeDescriptionCount * sizeof(*a->attributes))))
        return ret;
    if (a->input_desc.vertexBindingDescriptionCount != b->input_desc.vertexBindingDescriptionCount)
        return a->input_desc.vertexBindingDescriptionCount - b->input_desc.vertexBindingDescriptionCount;
    if ((ret = memcmp(a->bindings, b->bindings,
            a->input_desc.vertexBindingDescriptionCount * sizeof(*a->bindings))))
        return ret;

    if (a->ia_desc.topology != b->ia_desc.topology)
        return a->ia_desc.topology - b->ia_desc.topology;
    if (a->ia_desc.primitiveRestartEnable != b->ia_desc.primitiveRestartEnable)
        return a->ia_desc.primitiveRestartEnable - b->ia_desc.primitiveRestartEnable;

1638 1639 1640
    if (a->ts_desc.patchControlPoints != b->ts_desc.patchControlPoints)
        return a->ts_desc.patchControlPoints - b->ts_desc.patchControlPoints;

1641 1642 1643 1644 1645 1646 1647 1648 1649
    if ((ret = memcmp(&a->viewport, &b->viewport, sizeof(a->viewport))))
        return ret;

    if ((ret = memcmp(&a->scissor, &b->scissor, sizeof(a->scissor))))
        return ret;

    if ((ret = memcmp(&a->rs_desc, &b->rs_desc, sizeof(a->rs_desc))))
        return ret;

1650 1651 1652 1653 1654 1655
    if (a->ms_desc.rasterizationSamples != b->ms_desc.rasterizationSamples)
        return a->ms_desc.rasterizationSamples - b->ms_desc.rasterizationSamples;
    if (a->ms_desc.alphaToCoverageEnable != b->ms_desc.alphaToCoverageEnable)
        return a->ms_desc.alphaToCoverageEnable - b->ms_desc.alphaToCoverageEnable;
    if (a->sample_mask != b->sample_mask)
        return a->sample_mask - b->sample_mask;
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674

    if ((ret = memcmp(&a->ds_desc, &b->ds_desc, sizeof(a->ds_desc))))
        return ret;

    if (a->blend_desc.attachmentCount != b->blend_desc.attachmentCount)
        return a->blend_desc.attachmentCount - b->blend_desc.attachmentCount;
    if ((ret = memcmp(a->blend_attachments, b->blend_attachments,
            a->blend_desc.attachmentCount * sizeof(*a->blend_attachments))))
        return ret;

    if (a->pipeline_desc.layout != b->pipeline_desc.layout)
        return a->pipeline_desc.layout - b->pipeline_desc.layout;

    if (a->pipeline_desc.renderPass != b->pipeline_desc.renderPass)
        return a->pipeline_desc.renderPass - b->pipeline_desc.renderPass;

    return 0;
}

1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
static int wined3d_bo_slab_vk_compare(const void *key, const struct wine_rb_entry *entry)
{
    const struct wined3d_bo_slab_vk *slab = WINE_RB_ENTRY_VALUE(entry, const struct wined3d_bo_slab_vk, entry);
    const struct wined3d_bo_slab_vk_key *k = key;

    if (k->memory_type != slab->bo.memory_type)
        return k->memory_type - slab->bo.memory_type;
    if (k->usage != slab->bo.usage)
        return k->usage - slab->bo.usage;
    return k->size - slab->bo.size;
}

1687 1688 1689 1690 1691 1692
static void wined3d_context_vk_init_graphics_pipeline_key(struct wined3d_context_vk *context_vk)
{
    struct wined3d_graphics_pipeline_key_vk *key;
    VkPipelineShaderStageCreateInfo *stage;
    unsigned int i;

1693 1694 1695 1696 1697
    static const VkDynamicState dynamic_states[] =
    {
        VK_DYNAMIC_STATE_BLEND_CONSTANTS,
    };

1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
    key = &context_vk->graphics.pipeline_key_vk;
    memset(key, 0, sizeof(*key));

    for (i = 0; i < ARRAY_SIZE(context_vk->graphics.vk_modules); ++i)
    {
        stage = &key->stages[i];
        stage->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
        stage->pName = "main";
    }

    key->input_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
    key->input_desc.pVertexBindingDescriptions = key->bindings;
    key->input_desc.pVertexAttributeDescriptions = key->attributes;

1712 1713 1714
    key->divisor_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT;
    key->divisor_desc.pVertexBindingDivisors = key->divisors;

1715 1716
    key->ia_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;

1717 1718
    key->ts_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO;

1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
    key->vp_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
    key->vp_desc.viewportCount = 1;
    key->vp_desc.pViewports = &key->viewport;
    key->vp_desc.scissorCount = 1;
    key->vp_desc.pScissors = &key->scissor;

    key->rs_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
    key->rs_desc.lineWidth = 1.0f;

    key->ms_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
1729
    key->ms_desc.pSampleMask = &key->sample_mask;
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742

    key->ds_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
    key->ds_desc.maxDepthBounds = 1.0f;

    key->blend_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
    key->blend_desc.logicOp = VK_LOGIC_OP_COPY;
    key->blend_desc.pAttachments = key->blend_attachments;
    key->blend_desc.blendConstants[0] = 1.0f;
    key->blend_desc.blendConstants[1] = 1.0f;
    key->blend_desc.blendConstants[2] = 1.0f;
    key->blend_desc.blendConstants[3] = 1.0f;

    key->dynamic_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1743 1744
    key->dynamic_desc.dynamicStateCount = ARRAY_SIZE(dynamic_states);
    key->dynamic_desc.pDynamicStates = dynamic_states;
1745 1746 1747 1748 1749

    key->pipeline_desc.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
    key->pipeline_desc.pStages = key->stages;
    key->pipeline_desc.pVertexInputState = &key->input_desc;
    key->pipeline_desc.pInputAssemblyState = &key->ia_desc;
1750
    key->pipeline_desc.pTessellationState = &key->ts_desc;
1751 1752 1753 1754 1755 1756 1757 1758 1759
    key->pipeline_desc.pViewportState = &key->vp_desc;
    key->pipeline_desc.pRasterizationState = &key->rs_desc;
    key->pipeline_desc.pMultisampleState = &key->ms_desc;
    key->pipeline_desc.pDepthStencilState = &key->ds_desc;
    key->pipeline_desc.pColorBlendState = &key->blend_desc;
    key->pipeline_desc.pDynamicState = &key->dynamic_desc;
    key->pipeline_desc.basePipelineIndex = -1;
}

1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
static void wined3d_context_vk_update_rasterisation_state(const struct wined3d_context_vk *context_vk,
        const struct wined3d_state *state, struct wined3d_graphics_pipeline_key_vk *key)
{
    const struct wined3d_d3d_info *d3d_info = context_vk->c.d3d_info;
    VkPipelineRasterizationStateCreateInfo *desc = &key->rs_desc;
    const struct wined3d_rasterizer_state_desc *r;
    float scale_bias;
    union
    {
        uint32_t u32;
        float f32;
    } const_bias;

    if (!state->rasterizer_state)
    {
        desc->depthClampEnable = VK_FALSE;
1776
        desc->rasterizerDiscardEnable = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
        desc->cullMode = VK_CULL_MODE_BACK_BIT;
        desc->frontFace = VK_FRONT_FACE_CLOCKWISE;
        desc->depthBiasEnable = VK_FALSE;
        desc->depthBiasConstantFactor = 0.0f;
        desc->depthBiasClamp = 0.0f;
        desc->depthBiasSlopeFactor = 0.0f;

        return;
    }

    r = &state->rasterizer_state->desc;
    desc->depthClampEnable = !r->depth_clip;
1789
    desc->rasterizerDiscardEnable = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
    desc->cullMode = vk_cull_mode_from_wined3d(r->cull_mode);
    desc->frontFace = r->front_ccw ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE;

    scale_bias = r->scale_bias;
    const_bias.f32 = r->depth_bias;
    if (!scale_bias && !const_bias.f32)
    {
        desc->depthBiasEnable = VK_FALSE;
        desc->depthBiasConstantFactor = 0.0f;
        desc->depthBiasClamp = 0.0f;
        desc->depthBiasSlopeFactor = 0.0f;

        return;
    }

    desc->depthBiasEnable = VK_TRUE;
    if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_DEPTH_BIAS)
    {
        const struct wined3d_rendertarget_view *dsv;

        if ((dsv = state->fb.depth_stencil))
        {
            desc->depthBiasConstantFactor = -(float)const_bias.u32 / dsv->format->depth_bias_scale;
            desc->depthBiasSlopeFactor = -(float)const_bias.u32;
        }
        else
        {
            desc->depthBiasConstantFactor = 0.0f;
            desc->depthBiasSlopeFactor = 0.0f;
        }
    }
    else
    {
        desc->depthBiasConstantFactor = const_bias.f32;
        desc->depthBiasSlopeFactor = scale_bias;
    }
    desc->depthBiasClamp = r->depth_bias_clamp;
}

1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
static void wined3d_context_vk_update_blend_state(const struct wined3d_context_vk *context_vk,
        const struct wined3d_state *state, struct wined3d_graphics_pipeline_key_vk *key)
{
    VkPipelineColorBlendStateCreateInfo *desc = &key->blend_desc;
    const struct wined3d_blend_state_desc *b;
    unsigned int i;

    desc->attachmentCount = context_vk->rt_count;

    memset(key->blend_attachments, 0, sizeof(key->blend_attachments));
    if (!state->blend_state)
    {
        for (i = 0; i < context_vk->rt_count; ++i)
        {
            key->blend_attachments[i].colorWriteMask = VK_COLOR_COMPONENT_R_BIT
                    | VK_COLOR_COMPONENT_G_BIT
                    | VK_COLOR_COMPONENT_B_BIT
                    | VK_COLOR_COMPONENT_A_BIT;
        }

        return;
    }

    b = &state->blend_state->desc;
    for (i = 0; i < context_vk->rt_count; ++i)
    {
        const struct wined3d_rendertarget_blend_state_desc *rt = &b->rt[b->independent ? i : 0];
        const struct wined3d_rendertarget_view *rtv = state->fb.render_targets[i];
        VkPipelineColorBlendAttachmentState *a = &key->blend_attachments[i];
        enum wined3d_blend src_blend, dst_blend;
        const struct wined3d_format *rt_format;

        a->colorWriteMask = vk_colour_write_mask_from_wined3d(rt->writemask);
        if (!rt->enable)
            continue;

        if (rtv)
            rt_format = rtv->format;
        else
            rt_format = wined3d_get_format(context_vk->c.device->adapter, WINED3DFMT_NULL, 0);
        a->blendEnable = VK_TRUE;

        src_blend = rt->src;
        dst_blend = rt->dst;
        if (src_blend == WINED3D_BLEND_BOTHSRCALPHA)
        {
            src_blend = WINED3D_BLEND_SRCALPHA;
            dst_blend = WINED3D_BLEND_INVSRCALPHA;
        }
        else if (src_blend == WINED3D_BLEND_BOTHINVSRCALPHA)
        {
            src_blend = WINED3D_BLEND_INVSRCALPHA;
            dst_blend = WINED3D_BLEND_SRCALPHA;
        }
        a->srcColorBlendFactor = vk_blend_factor_from_wined3d(src_blend, rt_format, FALSE);
        a->dstColorBlendFactor = vk_blend_factor_from_wined3d(dst_blend, rt_format, FALSE);
        a->colorBlendOp = vk_blend_op_from_wined3d(rt->op);

        src_blend = rt->src_alpha;
        dst_blend = rt->dst_alpha;
        a->srcAlphaBlendFactor = vk_blend_factor_from_wined3d(src_blend, rt_format, TRUE);
        a->dstAlphaBlendFactor = vk_blend_factor_from_wined3d(dst_blend, rt_format, TRUE);
        a->alphaBlendOp = vk_blend_op_from_wined3d(rt->op_alpha);
    }
}

1895 1896 1897
static bool wined3d_context_vk_update_graphics_pipeline_key(struct wined3d_context_vk *context_vk,
        const struct wined3d_state *state, VkPipelineLayout vk_pipeline_layout)
{
1898
    unsigned int i, attribute_count, binding_count, divisor_count, stage_count;
1899 1900 1901 1902
    const struct wined3d_d3d_info *d3d_info = context_vk->c.d3d_info;
    struct wined3d_graphics_pipeline_key_vk *key;
    VkPipelineShaderStageCreateInfo *stage;
    struct wined3d_stream_info stream_info;
1903
    VkPrimitiveTopology vk_topology;
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
    VkShaderModule module;
    bool update = false;
    uint32_t mask;

    key = &context_vk->graphics.pipeline_key_vk;

    if (context_vk->c.shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
    {
        stage_count = 0;
        for (i = 0; i < ARRAY_SIZE(context_vk->graphics.vk_modules); ++i)
        {
            if (!(module = context_vk->graphics.vk_modules[i]))
                continue;

            stage = &key->stages[stage_count++];
            stage->stage = vk_shader_stage_from_wined3d(i);
            stage->module = module;
        }

        key->pipeline_desc.stageCount = stage_count;

        update = true;
    }

    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_VDECL)
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAMSRC)
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
    {
        wined3d_stream_info_from_declaration(&stream_info, state, d3d_info);
1933
        divisor_count = 0;
1934 1935
        for (i = 0, mask = 0, attribute_count = 0, binding_count = 0; i < ARRAY_SIZE(stream_info.elements); ++i)
        {
1936
            VkVertexInputBindingDivisorDescriptionEXT *d;
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
            struct wined3d_stream_info_element *e;
            VkVertexInputAttributeDescription *a;
            VkVertexInputBindingDescription *b;
            uint32_t binding;

            if (!(stream_info.use_map & (1u << i)))
                continue;

            a = &key->attributes[attribute_count++];
            e = &stream_info.elements[i];
            binding = e->stream_idx;

            a->location = i;
            a->binding = binding;
            a->format = wined3d_format_vk(e->format)->vk_format;
            a->offset = (UINT_PTR)e->data.addr - state->streams[binding].offset;

            if (mask & (1u << binding))
                continue;
            mask |= 1u << binding;

            b = &key->bindings[binding_count++];
            b->binding = binding;
            b->stride = e->stride;
            b->inputRate = e->divisor ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX;
1962 1963 1964 1965 1966 1967 1968

            if (e->divisor > 1)
            {
                d = &key->divisors[divisor_count++];
                d->binding = binding;
                d->divisor = e->divisor;
            }
1969 1970
        }

1971
        key->input_desc.pNext = NULL;
1972 1973 1974
        key->input_desc.vertexBindingDescriptionCount = binding_count;
        key->input_desc.vertexAttributeDescriptionCount = attribute_count;

1975 1976 1977 1978 1979 1980
        if (divisor_count)
        {
            key->input_desc.pNext = &key->divisor_desc;
            key->divisor_desc.vertexBindingDivisorCount = divisor_count;
        }

1981 1982 1983
        update = true;
    }

1984 1985 1986 1987
    vk_topology = vk_topology_from_wined3d(state->primitive_type);
    if (key->ia_desc.topology != vk_topology)
    {
        key->ia_desc.topology = vk_topology;
1988 1989
        key->ia_desc.primitiveRestartEnable = !(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART)
                && !wined3d_primitive_type_is_list(state->primitive_type);
1990 1991 1992 1993

        update = true;
    }

1994 1995 1996 1997 1998 1999 2000
    if (key->ts_desc.patchControlPoints != state->patch_vertex_count)
    {
        key->ts_desc.patchControlPoints = state->patch_vertex_count;

        update = true;
    }

2001 2002 2003
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_VIEWPORT)
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SCISSORRECT)
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_RASTERIZER))
2004 2005 2006 2007 2008 2009 2010 2011
    {
        key->viewport.x = state->viewports[0].x;
        key->viewport.y = state->viewports[0].y;
        key->viewport.width = state->viewports[0].width;
        key->viewport.height = state->viewports[0].height;
        key->viewport.minDepth = state->viewports[0].min_z;
        key->viewport.maxDepth = state->viewports[0].max_z;

2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
        if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
        {
            const RECT *r = &state->scissor_rects[0];

            key->scissor.offset.x = r->left;
            key->scissor.offset.y = r->top;
            key->scissor.extent.width =  r->right - r->left;
            key->scissor.extent.height = r->bottom - r->top;
        }
        else
        {
            key->scissor.offset.x = key->viewport.x;
            key->scissor.offset.y = key->viewport.y;
            key->scissor.extent.width = key->viewport.width;
            key->scissor.extent.height = key->viewport.height;
        }
2028 2029 2030 2031 2032 2033
        key->viewport.y += key->viewport.height;
        key->viewport.height = -key->viewport.height;

        update = true;
    }

2034 2035
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_RASTERIZER)
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY)))
2036 2037 2038 2039 2040 2041
    {
        wined3d_context_vk_update_rasterisation_state(context_vk, state, key);

        update = true;
    }

2042
    if (key->ms_desc.rasterizationSamples != context_vk->sample_count
2043
            || isStateDirty(&context_vk->c, STATE_BLEND) || isStateDirty(&context_vk->c, STATE_SAMPLE_MASK))
2044 2045
    {
        key->ms_desc.rasterizationSamples = context_vk->sample_count;
2046
        key->ms_desc.alphaToCoverageEnable = state->blend_state && state->blend_state->desc.alpha_to_coverage;
2047
        key->sample_mask = state->sample_mask;
2048 2049 2050 2051

        update = true;
    }

2052
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_DEPTH_STENCIL)
2053
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2054
    {
2055 2056 2057 2058 2059
        const struct wined3d_depth_stencil_state *d = state->depth_stencil_state;

        if (d)
        {
            key->ds_desc.depthTestEnable = d->desc.depth;
2060
            key->ds_desc.depthWriteEnable = d->desc.depth_write;
2061
            key->ds_desc.depthCompareOp = vk_compare_op_from_wined3d(d->desc.depth_func);
2062 2063 2064
            key->ds_desc.stencilTestEnable = state->fb.depth_stencil && d->desc.stencil;
            if (key->ds_desc.stencilTestEnable)
            {
2065
                key->ds_desc.front.failOp = vk_stencil_op_from_wined3d(d->desc.front.fail_op);
2066
                key->ds_desc.front.passOp = vk_stencil_op_from_wined3d(d->desc.front.pass_op);
2067
                key->ds_desc.front.depthFailOp = vk_stencil_op_from_wined3d(d->desc.front.depth_fail_op);
2068
                key->ds_desc.front.compareOp = vk_compare_op_from_wined3d(d->desc.front.func);
2069
                key->ds_desc.front.compareMask = d->desc.stencil_read_mask;
2070
                key->ds_desc.front.writeMask = d->desc.stencil_write_mask;
2071 2072 2073
                key->ds_desc.front.reference = state->render_states[WINED3D_RS_STENCILREF]
                        & ((1 << state->fb.depth_stencil->format->stencil_size) - 1);

2074
                key->ds_desc.back.failOp = vk_stencil_op_from_wined3d(d->desc.back.fail_op);
2075
                key->ds_desc.back.passOp = vk_stencil_op_from_wined3d(d->desc.back.pass_op);
2076
                key->ds_desc.back.depthFailOp = vk_stencil_op_from_wined3d(d->desc.back.depth_fail_op);
2077 2078 2079 2080 2081
                key->ds_desc.back.compareOp = vk_compare_op_from_wined3d(d->desc.back.func);
                key->ds_desc.back.compareMask = d->desc.stencil_read_mask;
                key->ds_desc.back.writeMask = d->desc.stencil_write_mask;
                key->ds_desc.back.reference = state->render_states[WINED3D_RS_STENCILREF]
                        & ((1 << state->fb.depth_stencil->format->stencil_size) - 1);
2082
            }
2083 2084 2085 2086 2087
            else
            {
                memset(&key->ds_desc.front, 0, sizeof(key->ds_desc.front));
                memset(&key->ds_desc.back, 0, sizeof(key->ds_desc.back));
            }
2088 2089 2090 2091
        }
        else
        {
            key->ds_desc.depthTestEnable = VK_TRUE;
2092
            key->ds_desc.depthWriteEnable = VK_TRUE;
2093
            key->ds_desc.depthCompareOp = VK_COMPARE_OP_LESS;
2094
            key->ds_desc.stencilTestEnable = VK_FALSE;
2095
        }
2096 2097 2098 2099

        update = true;
    }

2100 2101
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_BLEND)
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2102
    {
2103
        wined3d_context_vk_update_blend_state(context_vk, state, key);
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124

        update = true;
    }

    if (key->pipeline_desc.layout != vk_pipeline_layout)
    {
        key->pipeline_desc.layout = vk_pipeline_layout;

        update = true;
    }

    if (key->pipeline_desc.renderPass != context_vk->vk_render_pass)
    {
        key->pipeline_desc.renderPass = context_vk->vk_render_pass;

        update = true;
    }

    return update;
}

2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
static bool wined3d_context_vk_begin_render_pass(struct wined3d_context_vk *context_vk,
        VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    VkImageView vk_views[WINED3D_MAX_RENDER_TARGETS + 1];
    unsigned int fb_width, fb_height, fb_layer_count;
    struct wined3d_rendertarget_view_vk *rtv_vk;
    struct wined3d_rendertarget_view *view;
    const VkPhysicalDeviceLimits *limits;
    VkRenderPassBeginInfo begin_info;
    unsigned int attachment_count, i;
    VkFramebufferCreateInfo fb_desc;
    VkResult vr;

    if (context_vk->vk_render_pass)
        return true;

    limits = &wined3d_adapter_vk(device_vk->d.adapter)->device_limits;
    fb_width = limits->maxFramebufferWidth;
    fb_height = limits->maxFramebufferHeight;
    fb_layer_count = limits->maxFramebufferLayers;
    attachment_count = 0;

2148
    context_vk->rt_count = 0;
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
    for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
    {
        if (!(view = state->fb.render_targets[i]) || view->format->id == WINED3DFMT_NULL)
            continue;

        rtv_vk = wined3d_rendertarget_view_vk(view);
        vk_views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
        wined3d_context_vk_reference_rendertarget_view(context_vk, rtv_vk);

        if (view->width < fb_width)
            fb_width = view->width;
        if (view->height < fb_height)
            fb_height = view->height;
        if (view->layer_count < fb_layer_count)
            fb_layer_count = view->layer_count;
2164
        context_vk->rt_count = i + 1;
2165 2166 2167
        ++attachment_count;
    }

2168
    if (wined3d_state_uses_depth_buffer(state) && (view = state->fb.depth_stencil))
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
    {
        rtv_vk = wined3d_rendertarget_view_vk(view);
        vk_views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
        wined3d_context_vk_reference_rendertarget_view(context_vk, rtv_vk);

        if (view->width < fb_width)
            fb_width = view->width;
        if (view->height < fb_height)
            fb_height = view->height;
        if (view->layer_count < fb_layer_count)
            fb_layer_count = view->layer_count;
        ++attachment_count;
    }

    if (!(context_vk->vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, &state->fb,
2184
            ARRAY_SIZE(state->fb.render_targets), wined3d_state_uses_depth_buffer(state), 0)))
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
    {
        ERR("Failed to get render pass.\n");
        return false;
    }

    fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    fb_desc.pNext = NULL;
    fb_desc.flags = 0;
    fb_desc.renderPass = context_vk->vk_render_pass;
    fb_desc.attachmentCount = attachment_count;
    fb_desc.pAttachments = vk_views;
    fb_desc.width = fb_width;
    fb_desc.height = fb_height;
    fb_desc.layers = fb_layer_count;

    if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &context_vk->vk_framebuffer))) < 0)
    {
        WARN("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
        return false;
    }

    begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
    begin_info.pNext = NULL;
    begin_info.renderPass = context_vk->vk_render_pass;
    begin_info.framebuffer = context_vk->vk_framebuffer;
    begin_info.renderArea.offset.x = 0;
    begin_info.renderArea.offset.y = 0;
    begin_info.renderArea.extent.width = fb_width;
    begin_info.renderArea.extent.height = fb_height;
    begin_info.clearValueCount = 0;
    begin_info.pClearValues = NULL;
    VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_info, VK_SUBPASS_CONTENTS_INLINE));

    return true;
}

2221 2222 2223 2224 2225 2226
static void wined3d_context_vk_bind_vertex_buffers(struct wined3d_context_vk *context_vk,
        VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
{
    VkDeviceSize offsets[ARRAY_SIZE(state->streams)] = {0};
    VkBuffer buffers[ARRAY_SIZE(state->streams)];
    const struct wined3d_stream_state *stream;
2227 2228
    const VkDescriptorBufferInfo *buffer_info;
    struct wined3d_buffer_vk *buffer_vk;
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
    struct wined3d_buffer *buffer;
    unsigned int i, first, count;

    first = 0;
    count = 0;
    for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
    {
        stream = &state->streams[i];

        if ((buffer = stream->buffer))
        {
2240 2241 2242 2243 2244
            buffer_vk = wined3d_buffer_vk(buffer);
            buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
            wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
            buffers[count] = buffer_info->buffer;
            offsets[count] = buffer_info->offset + stream->offset;
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
            ++count;
            continue;
        }

        if (count)
            VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, first, count, buffers, offsets));
        first = i + 1;
        count = 0;
    }

    if (count)
        VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, first, count, buffers, offsets));
}

2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
static void wined3d_context_vk_bind_stream_output_buffers(struct wined3d_context_vk *context_vk,
        VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
{
    VkDeviceSize offsets[ARRAY_SIZE(state->stream_output)];
    VkDeviceSize sizes[ARRAY_SIZE(state->stream_output)];
    VkBuffer buffers[ARRAY_SIZE(state->stream_output)];
    const struct wined3d_stream_output *stream;
    const VkDescriptorBufferInfo *buffer_info;
    struct wined3d_buffer_vk *buffer_vk;
    struct wined3d_buffer *buffer;
    unsigned int i, first, count;

    first = 0;
    count = 0;
    for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
    {
        stream = &state->stream_output[i];

        if ((buffer = stream->buffer))
        {
            buffer_vk = wined3d_buffer_vk(buffer);
            buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
            wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
            buffers[count] = buffer_info->buffer;
            if ((offsets[count] = stream->offset) == ~0u)
            {
                FIXME("Appending to stream output buffers not implemented.\n");
                offsets[count] = 0;
            }
            sizes[count] = buffer_info->range - offsets[count];
            offsets[count] += buffer_info->offset;
            ++count;
            continue;
        }

        if (count)
            VK_CALL(vkCmdBindTransformFeedbackBuffersEXT(vk_command_buffer, first, count, buffers, offsets, sizes));
        first = i + 1;
        count = 0;
    }

    if (count)
        VK_CALL(vkCmdBindTransformFeedbackBuffersEXT(vk_command_buffer, first, count, buffers, offsets, sizes));
}

2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
static VkResult wined3d_context_vk_create_descriptor_pool(struct wined3d_device_vk *device_vk,
        const struct wined3d_vk_info *vk_info, VkDescriptorPool *vk_pool)
{
    struct VkDescriptorPoolCreateInfo pool_desc;
    VkResult vr;

    static const VkDescriptorPoolSize pool_sizes[] =
    {
        {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1024},
        {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1024},
        {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024},
        {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1024},
        {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024},
        {VK_DESCRIPTOR_TYPE_SAMPLER, 1024},
    };

    pool_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
    pool_desc.pNext = NULL;
    pool_desc.flags = 0;
    pool_desc.maxSets = 512;
    pool_desc.poolSizeCount = ARRAY_SIZE(pool_sizes);
    pool_desc.pPoolSizes = pool_sizes;

    if ((vr = VK_CALL(vkCreateDescriptorPool(device_vk->vk_device, &pool_desc, NULL, vk_pool))) < 0)
        ERR("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));

    return vr;
}

static VkResult wined3d_context_vk_create_descriptor_set(struct wined3d_context_vk *context_vk,
        VkDescriptorSetLayout vk_set_layout, VkDescriptorSet *vk_descriptor_set)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct VkDescriptorSetAllocateInfo set_desc;
    VkResult vr;

    if (!context_vk->vk_descriptor_pool && (vr = wined3d_context_vk_create_descriptor_pool(device_vk,
            vk_info, &context_vk->vk_descriptor_pool)))
    {
        WARN("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
        return vr;
    }

    set_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
    set_desc.pNext = NULL;
    set_desc.descriptorPool = context_vk->vk_descriptor_pool;
    set_desc.descriptorSetCount = 1;
    set_desc.pSetLayouts = &vk_set_layout;
    if ((vr = VK_CALL(vkAllocateDescriptorSets(device_vk->vk_device, &set_desc, vk_descriptor_set))) >= 0)
        return vr;

    if (vr == VK_ERROR_FRAGMENTED_POOL || vr == VK_ERROR_OUT_OF_POOL_MEMORY)
    {
        wined3d_context_vk_destroy_descriptor_pool(context_vk,
                context_vk->vk_descriptor_pool, context_vk->current_command_buffer.id);
        context_vk->vk_descriptor_pool = VK_NULL_HANDLE;
        if ((vr = wined3d_context_vk_create_descriptor_pool(device_vk, vk_info, &context_vk->vk_descriptor_pool)))
        {
            WARN("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
            return vr;
        }

        set_desc.descriptorPool = context_vk->vk_descriptor_pool;
        if ((vr = VK_CALL(vkAllocateDescriptorSets(device_vk->vk_device, &set_desc, vk_descriptor_set))) >= 0)
            return vr;
    }

    WARN("Failed to allocate descriptor set, vr %s.\n", wined3d_debug_vkresult(vr));

    return vr;
}

2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
static bool wined3d_shader_descriptor_writes_vk_add_write(struct wined3d_shader_descriptor_writes_vk *writes,
        VkDescriptorSet vk_descriptor_set, size_t binding_idx, VkDescriptorType type,
        const VkDescriptorBufferInfo *buffer_info, const VkDescriptorImageInfo *image_info,
        const VkBufferView *buffer_view)
{
    SIZE_T write_count = writes->count;
    VkWriteDescriptorSet *write;

    if (!wined3d_array_reserve((void **)&writes->writes, &writes->size,
            write_count + 1, sizeof(*writes->writes)))
        return false;

    write = &writes->writes[write_count];
    write->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
    write->pNext = NULL;
    write->dstSet = vk_descriptor_set;
    write->dstBinding = binding_idx;
    write->dstArrayElement = 0;
    write->descriptorCount = 1;
    write->descriptorType = type;
    write->pImageInfo = image_info;
    write->pBufferInfo = buffer_info;
    write->pTexelBufferView = buffer_view;

    ++writes->count;

    return true;
}

2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
static bool wined3d_shader_resource_bindings_add_null_srv_binding(struct wined3d_shader_descriptor_writes_vk *writes,
        VkDescriptorSet vk_descriptor_set, size_t binding_idx, enum wined3d_shader_resource_type type,
        enum wined3d_data_type data_type, struct wined3d_context_vk *context_vk)
{
    const struct wined3d_null_views_vk *v = &wined3d_device_vk(context_vk->c.device)->null_views_vk;

    switch (type)
    {
        case WINED3D_SHADER_RESOURCE_BUFFER:
            if (data_type == WINED3D_DATA_FLOAT)
                return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding_idx,
                        VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, NULL, NULL, &v->vk_view_buffer_float);
            return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding_idx,
                    VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, NULL, NULL, &v->vk_view_buffer_uint);

2421 2422 2423 2424
        case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
            return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                    binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_1d, NULL);

2425 2426 2427 2428 2429 2430 2431 2432
        case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
            return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                    binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2d, NULL);

        case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
            return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                    binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2dms, NULL);

2433 2434 2435 2436
        case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
            return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                    binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_3d, NULL);

2437 2438 2439 2440
        case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
            return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                    binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_cube, NULL);

2441 2442 2443 2444 2445 2446 2447 2448
        case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
            return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                    binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2d_array, NULL);

        case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
            return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                    binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2dms_array, NULL);

2449 2450 2451 2452 2453 2454
        default:
            FIXME("Unhandled resource type %#x.\n", type);
            return false;
    }
}

2455
static bool wined3d_context_vk_update_descriptors(struct wined3d_context_vk *context_vk,
2456
        VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, enum wined3d_pipeline pipeline)
2457
{
2458 2459
    struct wined3d_shader_descriptor_writes_vk *writes = &context_vk->descriptor_writes;
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2460 2461 2462
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    const struct wined3d_shader_resource_binding *binding;
    struct wined3d_shader_resource_bindings *bindings;
2463
    struct wined3d_unordered_access_view_vk *uav_vk;
2464
    struct wined3d_shader_resource_view_vk *srv_vk;
2465
    struct wined3d_unordered_access_view *uav;
2466
    const VkDescriptorBufferInfo *buffer_info;
2467 2468
    struct wined3d_shader_resource_view *srv;
    const VkDescriptorImageInfo *image_info;
2469
    struct wined3d_buffer_vk *buffer_vk;
2470 2471
    VkDescriptorSetLayout vk_set_layout;
    VkPipelineLayout vk_pipeline_layout;
2472
    struct wined3d_resource *resource;
2473
    VkPipelineBindPoint vk_bind_point;
2474
    VkDescriptorSet vk_descriptor_set;
2475
    struct wined3d_view_vk *view_vk;
2476
    struct wined3d_sampler *sampler;
2477
    struct wined3d_buffer *buffer;
2478
    VkBufferView *buffer_view;
2479
    VkDescriptorType type;
2480
    VkResult vr;
2481
    size_t i;
2482

2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504
    switch (pipeline)
    {
        case WINED3D_PIPELINE_GRAPHICS:
            bindings = &context_vk->graphics.bindings;
            vk_bind_point = VK_PIPELINE_BIND_POINT_GRAPHICS;
            vk_set_layout = context_vk->graphics.vk_set_layout;
            vk_pipeline_layout = context_vk->graphics.vk_pipeline_layout;
            break;

        case WINED3D_PIPELINE_COMPUTE:
            bindings = &context_vk->compute.bindings;
            vk_bind_point = VK_PIPELINE_BIND_POINT_COMPUTE;
            vk_set_layout = context_vk->compute.vk_set_layout;
            vk_pipeline_layout = context_vk->compute.vk_pipeline_layout;
            break;

        default:
            ERR("Invalid pipeline %#x.\n", pipeline);
            return false;
    }

    if ((vr = wined3d_context_vk_create_descriptor_set(context_vk, vk_set_layout, &vk_descriptor_set)))
2505 2506 2507 2508 2509
    {
        WARN("Failed to create descriptor set, vr %s.\n", wined3d_debug_vkresult(vr));
        return false;
    }

2510 2511
    writes->count = 0;
    for (i = 0; i < bindings->count; ++i)
2512
    {
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
        binding = &bindings->bindings[i];

        switch (binding->shader_descriptor_type)
        {
            case WINED3D_SHADER_DESCRIPTOR_TYPE_CBV:
                if (!(buffer = state->cb[binding->shader_type][binding->resource_idx]))
                {
                    FIXME("NULL constant buffer views not implemented.\n");
                    return false;
                }
2523 2524
                buffer_vk = wined3d_buffer_vk(buffer);
                buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2525 2526 2527
                if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                        binding->binding_idx, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info, NULL, NULL))
                    return false;
2528
                wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
2529 2530
                break;

2531 2532 2533
            case WINED3D_SHADER_DESCRIPTOR_TYPE_SRV:
                if (!(srv = state->shader_resource_view[binding->shader_type][binding->resource_idx]))
                {
2534 2535 2536 2537
                    if (!wined3d_shader_resource_bindings_add_null_srv_binding(writes, vk_descriptor_set,
                            binding->binding_idx, binding->resource_type, binding->resource_data_type, context_vk))
                        return false;
                    break;
2538 2539 2540
                }
                resource = srv->resource;

2541 2542
                srv_vk = wined3d_shader_resource_view_vk(srv);
                view_vk = &srv_vk->view_vk;
2543 2544
                if (resource->type == WINED3D_RTYPE_BUFFER)
                {
2545 2546 2547
                    image_info = NULL;
                    buffer_view = &view_vk->u.vk_buffer_view;
                    type = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
2548 2549 2550 2551 2552 2553 2554 2555 2556
                }
                else
                {
                    struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture_from_resource(resource));

                    if (view_vk->u.vk_image_info.imageView)
                        image_info = &view_vk->u.vk_image_info;
                    else
                        image_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk);
2557
                    buffer_view = NULL;
2558 2559 2560 2561
                    type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
                }

                if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2562
                        binding->binding_idx, type, NULL, image_info, buffer_view))
2563
                    return false;
2564
                wined3d_context_vk_reference_shader_resource_view(context_vk, srv_vk);
2565 2566
                break;

2567
            case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV:
2568
                if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2569 2570 2571 2572 2573 2574
                {
                    FIXME("NULL unordered access views not implemented.\n");
                    return false;
                }
                resource = uav->resource;

2575 2576
                uav_vk = wined3d_unordered_access_view_vk(uav);
                view_vk = &uav_vk->view_vk;
2577 2578 2579 2580 2581 2582 2583 2584
                if (resource->type == WINED3D_RTYPE_BUFFER)
                {
                    image_info = NULL;
                    buffer_view = &view_vk->u.vk_buffer_view;
                    type = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
                }
                else
                {
2585 2586 2587 2588 2589 2590 2591 2592
                    struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture_from_resource(resource));

                    if (view_vk->u.vk_image_info.imageView)
                        image_info = &view_vk->u.vk_image_info;
                    else
                        image_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk);
                    buffer_view = NULL;
                    type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
2593 2594 2595 2596 2597
                }

                if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
                        binding->binding_idx, type, NULL, image_info, buffer_view))
                    return false;
2598
                wined3d_context_vk_reference_unordered_access_view(context_vk, uav_vk);
2599 2600
                break;

2601
            case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV_COUNTER:
2602
                if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
                {
                    FIXME("NULL unordered access view counters not implemented.\n");
                    return false;
                }

                uav_vk = wined3d_unordered_access_view_vk(uav);
                if (!uav_vk->vk_counter_view || !wined3d_shader_descriptor_writes_vk_add_write(writes,
                        vk_descriptor_set, binding->binding_idx, VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,
                        NULL, NULL, &uav_vk->vk_counter_view))
                    return false;
                break;

2615 2616 2617 2618 2619 2620
            case WINED3D_SHADER_DESCRIPTOR_TYPE_SAMPLER:
                if (!(sampler = state->sampler[binding->shader_type][binding->resource_idx]))
                    sampler = context_vk->c.device->null_sampler;
                if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding->binding_idx,
                        VK_DESCRIPTOR_TYPE_SAMPLER, NULL, &wined3d_sampler_vk(sampler)->vk_image_info, NULL))
                    return false;
2621
                wined3d_context_vk_reference_sampler(context_vk, wined3d_sampler_vk(sampler));
2622 2623
                break;

2624
            default:
2625
                ERR("Invalid descriptor type %#x.\n", binding->shader_descriptor_type);
2626 2627
                return false;
        }
2628 2629
    }

2630
    VK_CALL(vkUpdateDescriptorSets(device_vk->vk_device, writes->count, writes->writes, 0, NULL));
2631 2632
    VK_CALL(vkCmdBindDescriptorSets(vk_command_buffer, vk_bind_point,
            vk_pipeline_layout, 0, 1, &vk_descriptor_set, 0, NULL));
2633 2634 2635 2636

    return true;
}

2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
static VkResult wined3d_context_vk_create_descriptor_set_layout(struct wined3d_device_vk *device_vk,
        const struct wined3d_vk_info *vk_info, const struct wined3d_pipeline_layout_key_vk *key,
        VkDescriptorSetLayout *vk_set_layout)
{
    VkDescriptorSetLayoutCreateInfo layout_desc;
    VkResult vr;

    layout_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
    layout_desc.pNext = NULL;
    layout_desc.flags = 0;
    layout_desc.bindingCount = key->binding_count;
    layout_desc.pBindings = key->bindings;

    if ((vr = VK_CALL(vkCreateDescriptorSetLayout(device_vk->vk_device, &layout_desc, NULL, vk_set_layout))) < 0)
        WARN("Failed to create Vulkan descriptor set layout, vr %s.\n", wined3d_debug_vkresult(vr));

    return vr;
}

struct wined3d_pipeline_layout_vk *wined3d_context_vk_get_pipeline_layout(
        struct wined3d_context_vk *context_vk, VkDescriptorSetLayoutBinding *bindings, SIZE_T binding_count)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_pipeline_layout_key_vk key;
    struct wined3d_pipeline_layout_vk *layout;
    VkPipelineLayoutCreateInfo layout_desc;
    struct wine_rb_entry *entry;
    VkResult vr;

    key.bindings = bindings;
    key.binding_count = binding_count;
    if ((entry = wine_rb_get(&context_vk->pipeline_layouts, &key)))
        return WINE_RB_ENTRY_VALUE(entry, struct wined3d_pipeline_layout_vk, entry);

    if (!(layout = heap_alloc(sizeof(*layout))))
        return NULL;

    if (!(layout->key.bindings = heap_alloc(sizeof(*layout->key.bindings) * key.binding_count)))
    {
        heap_free(layout);
        return NULL;
    }
    memcpy(layout->key.bindings, key.bindings, sizeof(*layout->key.bindings) * key.binding_count);
    layout->key.binding_count = key.binding_count;

    if ((vr = wined3d_context_vk_create_descriptor_set_layout(device_vk, vk_info, &key, &layout->vk_set_layout)))
    {
        WARN("Failed to create descriptor set layout, vr %s.\n", wined3d_debug_vkresult(vr));
        goto fail;
    }

    layout_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    layout_desc.pNext = NULL;
    layout_desc.flags = 0;
    layout_desc.setLayoutCount = 1;
    layout_desc.pSetLayouts = &layout->vk_set_layout;
    layout_desc.pushConstantRangeCount = 0;
    layout_desc.pPushConstantRanges = NULL;

    if ((vr = VK_CALL(vkCreatePipelineLayout(device_vk->vk_device,
            &layout_desc, NULL, &layout->vk_pipeline_layout))) < 0)
    {
        WARN("Failed to create Vulkan pipeline layout, vr %s.\n", wined3d_debug_vkresult(vr));
        VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
        goto fail;
    }

    if (wine_rb_put(&context_vk->pipeline_layouts, &layout->key, &layout->entry) == -1)
    {
        ERR("Failed to insert pipeline layout.\n");
        VK_CALL(vkDestroyPipelineLayout(device_vk->vk_device, layout->vk_pipeline_layout, NULL));
        VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
        goto fail;
    }

    return layout;

fail:
    heap_free(layout->key.bindings);
    heap_free(layout);
    return NULL;
}

2721 2722
static VkPipeline wined3d_context_vk_get_graphics_pipeline(struct wined3d_context_vk *context_vk)
{
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_graphics_pipeline_vk *pipeline_vk;
    struct wined3d_graphics_pipeline_key_vk *key;
    struct wine_rb_entry *entry;
    VkResult vr;

    key = &context_vk->graphics.pipeline_key_vk;
    if ((entry = wine_rb_get(&context_vk->graphics_pipelines, key)))
        return WINE_RB_ENTRY_VALUE(entry, struct wined3d_graphics_pipeline_vk, entry)->vk_pipeline;
2733

2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
    if (!(pipeline_vk = heap_alloc(sizeof(*pipeline_vk))))
        return VK_NULL_HANDLE;
    pipeline_vk->key = *key;

    if ((vr = VK_CALL(vkCreateGraphicsPipelines(device_vk->vk_device,
            VK_NULL_HANDLE, 1, &key->pipeline_desc, NULL, &pipeline_vk->vk_pipeline))) < 0)
    {
        WARN("Failed to create graphics pipeline, vr %s.\n", wined3d_debug_vkresult(vr));
        heap_free(pipeline_vk);
        return VK_NULL_HANDLE;
    }

    if (wine_rb_put(&context_vk->graphics_pipelines, &pipeline_vk->key, &pipeline_vk->entry) == -1)
        ERR("Failed to insert pipeline.\n");

    return pipeline_vk->vk_pipeline;
2750 2751
}

2752
static void wined3d_context_vk_load_shader_resources(struct wined3d_context_vk *context_vk,
2753
        const struct wined3d_state *state, enum wined3d_pipeline pipeline)
2754 2755
{
    struct wined3d_shader_descriptor_writes_vk *writes = &context_vk->descriptor_writes;
2756
    const struct wined3d_shader_resource_bindings *bindings;
2757
    const struct wined3d_shader_resource_binding *binding;
2758
    struct wined3d_unordered_access_view_vk *uav_vk;
2759
    struct wined3d_shader_resource_view_vk *srv_vk;
2760
    struct wined3d_unordered_access_view *uav;
2761
    struct wined3d_shader_resource_view *srv;
2762
    struct wined3d_buffer_vk *buffer_vk;
2763
    struct wined3d_sampler *sampler;
2764 2765 2766
    struct wined3d_buffer *buffer;
    size_t i;

2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
    switch (pipeline)
    {
        case WINED3D_PIPELINE_GRAPHICS:
            bindings = &context_vk->graphics.bindings;
            break;

        case WINED3D_PIPELINE_COMPUTE:
            bindings = &context_vk->compute.bindings;
            break;

        default:
            ERR("Invalid pipeline %#x.\n", pipeline);
            return;
    }

2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
    writes->count = 0;
    for (i = 0; i < bindings->count; ++i)
    {
        binding = &bindings->bindings[i];

        switch (binding->shader_descriptor_type)
        {
            case WINED3D_SHADER_DESCRIPTOR_TYPE_CBV:
                if (!(buffer = state->cb[binding->shader_type][binding->resource_idx]))
                    break;

                buffer_vk = wined3d_buffer_vk(buffer);
                wined3d_buffer_load(buffer, &context_vk->c, state);
                if (!buffer_vk->bo_user.valid)
2796 2797 2798 2799 2800 2801
                {
                    if (pipeline == WINED3D_PIPELINE_GRAPHICS)
                        context_invalidate_state(&context_vk->c, STATE_GRAPHICS_CONSTANT_BUFFER(binding->shader_type));
                    else
                        context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_CONSTANT_BUFFER);
                }
2802 2803
                break;

2804 2805 2806 2807
            case WINED3D_SHADER_DESCRIPTOR_TYPE_SRV:
                if (!(srv = state->shader_resource_view[binding->shader_type][binding->resource_idx]))
                    break;

2808 2809 2810 2811 2812 2813
                srv_vk = wined3d_shader_resource_view_vk(srv);
                if (srv->resource->type == WINED3D_RTYPE_BUFFER)
                {
                    if (!srv_vk->view_vk.bo_user.valid)
                    {
                        wined3d_shader_resource_view_vk_update(srv_vk, context_vk);
2814 2815 2816 2817
                        if (pipeline == WINED3D_PIPELINE_GRAPHICS)
                            context_invalidate_state(&context_vk->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
                        else
                            context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
2818 2819 2820 2821 2822
                    }
                    wined3d_buffer_load(buffer_from_resource(srv->resource), &context_vk->c, state);
                }
                else
                {
2823
                    wined3d_texture_load(texture_from_resource(srv->resource), &context_vk->c, FALSE);
2824
                }
2825 2826
                break;

2827
            case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV:
2828
                if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2829 2830 2831 2832 2833 2834 2835 2836
                    break;

                uav_vk = wined3d_unordered_access_view_vk(uav);
                if (uav->resource->type == WINED3D_RTYPE_BUFFER)
                {
                    if (!uav_vk->view_vk.bo_user.valid)
                    {
                        wined3d_unordered_access_view_vk_update(uav_vk, context_vk);
2837 2838 2839 2840 2841
                        if (pipeline == WINED3D_PIPELINE_GRAPHICS)
                            context_invalidate_state(&context_vk->c, STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING);
                        else
                            context_invalidate_compute_state(&context_vk->c,
                                    STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING);
2842 2843 2844 2845
                    }
                    wined3d_buffer_load(buffer_from_resource(uav->resource), &context_vk->c, state);
                    wined3d_unordered_access_view_invalidate_location(uav, ~WINED3D_LOCATION_BUFFER);
                }
2846 2847 2848 2849 2850
                else
                {
                    wined3d_texture_load(texture_from_resource(uav->resource), &context_vk->c, FALSE);
                    wined3d_unordered_access_view_invalidate_location(uav, ~WINED3D_LOCATION_TEXTURE_RGB);
                }
2851 2852
                break;

2853 2854 2855
            case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV_COUNTER:
                break;

2856 2857 2858 2859 2860
            case WINED3D_SHADER_DESCRIPTOR_TYPE_SAMPLER:
                if (!(sampler = state->sampler[binding->shader_type][binding->resource_idx]))
                    sampler = context_vk->c.device->null_sampler;
                break;

2861
            default:
2862
                ERR("Invalid descriptor type %#x.\n", binding->shader_descriptor_type);
2863 2864 2865 2866 2867
                break;
        }
    }
}

2868
VkCommandBuffer wined3d_context_vk_apply_draw_state(struct wined3d_context_vk *context_vk,
2869
        const struct wined3d_state *state, struct wined3d_buffer_vk *indirect_vk, bool indexed)
2870 2871 2872 2873
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    struct wined3d_rendertarget_view *dsv;
2874
    VkSampleCountFlagBits sample_count;
2875
    VkCommandBuffer vk_command_buffer;
2876
    struct wined3d_buffer *buffer;
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
    unsigned int i;

    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL))
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
        context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL);
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
        context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_VERTEX);
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY)))
        context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_GEOMETRY);
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_HULL)))
        context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_HULL) | (1u << WINED3D_SHADER_TYPE_DOMAIN);
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN)))
        context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_DOMAIN);

2891
    context_vk->sample_count = 0;
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
    for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
    {
        struct wined3d_rendertarget_view *rtv;

        if (!(rtv = state->fb.render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
            continue;

        if (wined3d_blend_state_get_writemask(state->blend_state, i))
        {
            wined3d_rendertarget_view_load_location(rtv, &context_vk->c, rtv->resource->draw_binding);
            wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
        }
        else
        {
            wined3d_rendertarget_view_prepare_location(rtv, &context_vk->c, rtv->resource->draw_binding);
        }
2908 2909 2910 2911 2912 2913

        sample_count = max(1, wined3d_resource_get_sample_count(rtv->resource));
        if (!context_vk->sample_count)
            context_vk->sample_count = sample_count;
        else if (context_vk->sample_count != sample_count)
            FIXME("Inconsistent sample counts (%u != %u).\n", context_vk->sample_count, sample_count);
2914 2915 2916 2917
    }

    if ((dsv = state->fb.depth_stencil))
    {
2918
        if (wined3d_state_uses_depth_buffer(state))
2919 2920 2921
            wined3d_rendertarget_view_load_location(dsv, &context_vk->c, dsv->resource->draw_binding);
        else
            wined3d_rendertarget_view_prepare_location(dsv, &context_vk->c, dsv->resource->draw_binding);
2922
        if (!state->depth_stencil_state || state->depth_stencil_state->desc.depth_write)
2923
            wined3d_rendertarget_view_invalidate_location(dsv, ~dsv->resource->draw_binding);
2924 2925 2926 2927 2928 2929

        sample_count = max(1, wined3d_resource_get_sample_count(dsv->resource));
        if (!context_vk->sample_count)
            context_vk->sample_count = sample_count;
        else if (context_vk->sample_count != sample_count)
            FIXME("Inconsistent sample counts (%u != %u).\n", context_vk->sample_count, sample_count);
2930 2931
    }

2932 2933
    if (!context_vk->sample_count)
        context_vk->sample_count = VK_SAMPLE_COUNT_1_BIT;
2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947
    if (context_vk->c.shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
    {
        device_vk->d.shader_backend->shader_select(device_vk->d.shader_priv, &context_vk->c, state);
        if (!context_vk->graphics.vk_pipeline_layout)
        {
            ERR("No pipeline layout set.\n");
            return VK_NULL_HANDLE;
        }
        context_vk->c.update_shader_resource_bindings = 1;
        context_vk->c.update_unordered_access_view_bindings = 1;
    }

    wined3d_context_vk_load_shader_resources(context_vk, state, WINED3D_PIPELINE_GRAPHICS);

2948 2949 2950 2951 2952 2953 2954 2955 2956 2957
    for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
    {
        if (!(buffer = state->streams[i].buffer))
            continue;

        wined3d_buffer_load(buffer, &context_vk->c, state);
        if (!wined3d_buffer_vk(buffer)->bo_user.valid)
            context_invalidate_state(&context_vk->c, STATE_STREAMSRC);
    }

2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
    if (use_transform_feedback(state) && vk_info->supported[WINED3D_VK_EXT_TRANSFORM_FEEDBACK])
    {
        for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
        {
            if (!(buffer = state->stream_output[i].buffer))
                continue;

            wined3d_buffer_load(buffer, &context_vk->c, state);
            wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
            if (!wined3d_buffer_vk(buffer)->bo_user.valid)
                context_vk->update_stream_output = 1;
        }
        context_vk->c.transform_feedback_active = 1;
    }

2973
    if (indexed || (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_INDEXBUFFER) && state->index_buffer))
2974 2975 2976 2977 2978 2979
    {
        wined3d_buffer_load(state->index_buffer, &context_vk->c, state);
        if (!wined3d_buffer_vk(state->index_buffer)->bo_user.valid)
            context_invalidate_state(&context_vk->c, STATE_INDEXBUFFER);
    }

2980 2981 2982
    if (indirect_vk)
        wined3d_buffer_load(&indirect_vk->b, &context_vk->c, state);

2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996
    if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
    {
        ERR("Failed to get command buffer.\n");
        return VK_NULL_HANDLE;
    }

    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
        wined3d_context_vk_end_current_render_pass(context_vk);
    if (!wined3d_context_vk_begin_render_pass(context_vk, vk_command_buffer, state, vk_info))
    {
        ERR("Failed to begin render pass.\n");
        return VK_NULL_HANDLE;
    }

2997 2998
    if (wined3d_context_vk_update_graphics_pipeline_key(context_vk, state, context_vk->graphics.vk_pipeline_layout)
            || !context_vk->graphics.vk_pipeline)
2999
    {
3000 3001 3002 3003 3004 3005 3006 3007
        if (!(context_vk->graphics.vk_pipeline = wined3d_context_vk_get_graphics_pipeline(context_vk)))
        {
            ERR("Failed to get graphics pipeline.\n");
            return VK_NULL_HANDLE;
        }

        VK_CALL(vkCmdBindPipeline(vk_command_buffer,
                VK_PIPELINE_BIND_POINT_GRAPHICS, context_vk->graphics.vk_pipeline));
3008 3009
    }

3010 3011 3012
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAMSRC))
        wined3d_context_vk_bind_vertex_buffers(context_vk, vk_command_buffer, state, vk_info);

3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAM_OUTPUT))
    {
        context_vk->update_stream_output = 1;
        context_vk->c.transform_feedback_paused = 0;
    }
    if (context_vk->c.transform_feedback_active && context_vk->update_stream_output)
    {
        wined3d_context_vk_bind_stream_output_buffers(context_vk, vk_command_buffer, state, vk_info);
        context_vk->update_stream_output = 0;
    }

3024 3025
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_INDEXBUFFER) && state->index_buffer)
    {
3026 3027
        struct wined3d_buffer_vk *buffer_vk = wined3d_buffer_vk(state->index_buffer);
        const VkDescriptorBufferInfo *buffer_info;
3028 3029 3030 3031 3032 3033
        VkIndexType idx_type;

        if (state->index_format == WINED3DFMT_R16_UINT)
            idx_type = VK_INDEX_TYPE_UINT16;
        else
            idx_type = VK_INDEX_TYPE_UINT32;
3034 3035 3036 3037
        buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
        wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
        VK_CALL(vkCmdBindIndexBuffer(vk_command_buffer, buffer_info->buffer,
                buffer_info->offset + state->index_offset, idx_type));
3038 3039
    }

3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL))
            || wined3d_context_is_graphics_state_dirty(&context_vk->c,
            STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX))
            || wined3d_context_is_graphics_state_dirty(&context_vk->c,
            STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY))
            || wined3d_context_is_graphics_state_dirty(&context_vk->c,
            STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL))
            || wined3d_context_is_graphics_state_dirty(&context_vk->c,
            STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN))
            || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING))
        context_vk->c.update_shader_resource_bindings = 1;
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING))
        context_vk->c.update_unordered_access_view_bindings = 1;

    if (context_vk->c.update_shader_resource_bindings || context_vk->c.update_unordered_access_view_bindings)
    {
        if (!wined3d_context_vk_update_descriptors(context_vk, vk_command_buffer, state, WINED3D_PIPELINE_GRAPHICS))
        {
            ERR("Failed to update shader descriptors.\n");
            return VK_NULL_HANDLE;
        }

        context_vk->c.update_shader_resource_bindings = 0;
        context_vk->c.update_unordered_access_view_bindings = 0;
    }

3066 3067 3068
    if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_BLEND_FACTOR))
        VK_CALL(vkCmdSetBlendConstants(vk_command_buffer, &state->blend_factor.r));

3069
    memset(context_vk->c.dirty_graphics_states, 0, sizeof(context_vk->c.dirty_graphics_states));
3070
    context_vk->c.shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3071 3072 3073 3074

    return vk_command_buffer;
}

3075 3076 3077 3078 3079 3080 3081
VkCommandBuffer wined3d_context_vk_apply_compute_state(struct wined3d_context_vk *context_vk,
        const struct wined3d_state *state, struct wined3d_buffer_vk *indirect_vk)
{
    struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
    const struct wined3d_vk_info *vk_info = context_vk->vk_info;
    VkCommandBuffer vk_command_buffer;

3082 3083
    wined3d_context_vk_end_current_render_pass(context_vk);

3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
    if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_SHADER))
        context_vk->c.shader_update_mask |= 1u << WINED3D_SHADER_TYPE_COMPUTE;

    if (context_vk->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
    {
        device_vk->d.shader_backend->shader_select_compute(device_vk->d.shader_priv, &context_vk->c, state);
        if (!context_vk->compute.vk_pipeline)
        {
            ERR("No compute pipeline set.\n");
            return VK_NULL_HANDLE;
        }
        context_vk->c.update_compute_shader_resource_bindings = 1;
        context_vk->c.update_compute_unordered_access_view_bindings = 1;
        context_vk->update_compute_pipeline = 1;
    }

3100
    wined3d_context_vk_load_shader_resources(context_vk, state, WINED3D_PIPELINE_COMPUTE);
3101

3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
    if (indirect_vk)
        wined3d_buffer_load_location(&indirect_vk->b, &context_vk->c, WINED3D_LOCATION_BUFFER);

    if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
    {
        ERR("Failed to get command buffer.\n");
        return VK_NULL_HANDLE;
    }

    if (context_vk->update_compute_pipeline)
    {
        VK_CALL(vkCmdBindPipeline(vk_command_buffer,
                VK_PIPELINE_BIND_POINT_COMPUTE, context_vk->compute.vk_pipeline));
        context_vk->update_compute_pipeline = 0;
    }

    if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_CONSTANT_BUFFER)
            || wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING))
        context_vk->c.update_compute_shader_resource_bindings = 1;
    if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING))
        context_vk->c.update_compute_unordered_access_view_bindings = 1;

    if (context_vk->c.update_compute_shader_resource_bindings
            || context_vk->c.update_compute_unordered_access_view_bindings)
    {
3127
        if (!wined3d_context_vk_update_descriptors(context_vk, vk_command_buffer, state, WINED3D_PIPELINE_COMPUTE))
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137
        {
            ERR("Failed to update shader descriptors.\n");
            return VK_NULL_HANDLE;
        }

        context_vk->c.update_compute_shader_resource_bindings = 0;
        context_vk->c.update_compute_unordered_access_view_bindings = 0;
    }

    memset(context_vk->c.dirty_compute_states, 0, sizeof(context_vk->c.dirty_compute_states));
3138
    context_vk->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
3139 3140 3141 3142

    return vk_command_buffer;
}

3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
HRESULT wined3d_context_vk_init(struct wined3d_context_vk *context_vk, struct wined3d_swapchain *swapchain)
{
    VkCommandPoolCreateInfo command_pool_info;
    const struct wined3d_vk_info *vk_info;
    struct wined3d_adapter_vk *adapter_vk;
    struct wined3d_device_vk *device_vk;
    VkResult vr;

    TRACE("context_vk %p, swapchain %p.\n", context_vk, swapchain);

    wined3d_context_init(&context_vk->c, swapchain);
    device_vk = wined3d_device_vk(swapchain->device);
    adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
    context_vk->vk_info = vk_info = &adapter_vk->vk_info;

    command_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
    command_pool_info.pNext = NULL;
    command_pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
    command_pool_info.queueFamilyIndex = device_vk->vk_queue_family_index;
    if ((vr = VK_CALL(vkCreateCommandPool(device_vk->vk_device,
            &command_pool_info, NULL, &context_vk->vk_command_pool))) < 0)
    {
        ERR("Failed to create Vulkan command pool, vr %s.\n", wined3d_debug_vkresult(vr));
        wined3d_context_cleanup(&context_vk->c);
        return E_FAIL;
    }
    context_vk->current_command_buffer.id = 1;

3171 3172
    wined3d_context_vk_init_graphics_pipeline_key(context_vk);

3173 3174
    list_init(&context_vk->active_queries);
    list_init(&context_vk->free_occlusion_query_pools);
3175
    list_init(&context_vk->free_timestamp_query_pools);
3176
    list_init(&context_vk->free_pipeline_statistics_query_pools);
3177
    list_init(&context_vk->free_stream_output_statistics_query_pools);
3178

3179
    wine_rb_init(&context_vk->render_passes, wined3d_render_pass_vk_compare);
3180
    wine_rb_init(&context_vk->pipeline_layouts, wined3d_pipeline_layout_vk_compare);
3181
    wine_rb_init(&context_vk->graphics_pipelines, wined3d_graphics_pipeline_vk_compare);
3182 3183
    wine_rb_init(&context_vk->bo_slab_available, wined3d_bo_slab_vk_compare);

3184 3185
    return WINED3D_OK;
}