mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / wined3d / context_vk.c
blob45133eabb6921cbdbe019102d419493648feef8e
1 /*
2 * Copyright 2002-2004 Jason Edmeades
3 * Copyright 2002-2004 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2006, 2008 Henri Verbeet
7 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
8 * Copyright 2009-2020 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 VkCompareOp vk_compare_op_from_wined3d(enum wined3d_cmp_func op)
34 switch (op)
36 case WINED3D_CMP_NEVER:
37 return VK_COMPARE_OP_NEVER;
38 case WINED3D_CMP_LESS:
39 return VK_COMPARE_OP_LESS;
40 case WINED3D_CMP_EQUAL:
41 return VK_COMPARE_OP_EQUAL;
42 case WINED3D_CMP_LESSEQUAL:
43 return VK_COMPARE_OP_LESS_OR_EQUAL;
44 case WINED3D_CMP_GREATER:
45 return VK_COMPARE_OP_GREATER;
46 case WINED3D_CMP_NOTEQUAL:
47 return VK_COMPARE_OP_NOT_EQUAL;
48 case WINED3D_CMP_GREATEREQUAL:
49 return VK_COMPARE_OP_GREATER_OR_EQUAL;
50 case WINED3D_CMP_ALWAYS:
51 return VK_COMPARE_OP_ALWAYS;
52 default:
53 if (!op)
54 WARN("Unhandled compare operation %#x.\n", op);
55 else
56 FIXME("Unhandled compare operation %#x.\n", op);
57 return VK_COMPARE_OP_NEVER;
61 VkShaderStageFlagBits vk_shader_stage_from_wined3d(enum wined3d_shader_type shader_type)
63 switch (shader_type)
65 case WINED3D_SHADER_TYPE_VERTEX:
66 return VK_SHADER_STAGE_VERTEX_BIT;
67 case WINED3D_SHADER_TYPE_HULL:
68 return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
69 case WINED3D_SHADER_TYPE_DOMAIN:
70 return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
71 case WINED3D_SHADER_TYPE_GEOMETRY:
72 return VK_SHADER_STAGE_GEOMETRY_BIT;
73 case WINED3D_SHADER_TYPE_PIXEL:
74 return VK_SHADER_STAGE_FRAGMENT_BIT;
75 case WINED3D_SHADER_TYPE_COMPUTE:
76 return VK_SHADER_STAGE_COMPUTE_BIT;
77 default:
78 ERR("Unhandled shader type %s.\n", debug_shader_type(shader_type));
79 return 0;
83 static VkBlendFactor vk_blend_factor_from_wined3d(enum wined3d_blend blend,
84 const struct wined3d_format *dst_format, bool alpha)
86 switch (blend)
88 case WINED3D_BLEND_ZERO:
89 return VK_BLEND_FACTOR_ZERO;
90 case WINED3D_BLEND_ONE:
91 return VK_BLEND_FACTOR_ONE;
92 case WINED3D_BLEND_SRCCOLOR:
93 return VK_BLEND_FACTOR_SRC_COLOR;
94 case WINED3D_BLEND_INVSRCCOLOR:
95 return VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR;
96 case WINED3D_BLEND_SRCALPHA:
97 return VK_BLEND_FACTOR_SRC_ALPHA;
98 case WINED3D_BLEND_INVSRCALPHA:
99 return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
100 case WINED3D_BLEND_DESTALPHA:
101 if (dst_format->alpha_size)
102 return VK_BLEND_FACTOR_DST_ALPHA;
103 return VK_BLEND_FACTOR_ONE;
104 case WINED3D_BLEND_INVDESTALPHA:
105 if (dst_format->alpha_size)
106 return VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA;
107 return VK_BLEND_FACTOR_ZERO;
108 case WINED3D_BLEND_DESTCOLOR:
109 return VK_BLEND_FACTOR_DST_COLOR;
110 case WINED3D_BLEND_INVDESTCOLOR:
111 return VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;
112 case WINED3D_BLEND_SRCALPHASAT:
113 return VK_BLEND_FACTOR_SRC_ALPHA_SATURATE;
114 case WINED3D_BLEND_BLENDFACTOR:
115 if (alpha)
116 return VK_BLEND_FACTOR_CONSTANT_ALPHA;
117 return VK_BLEND_FACTOR_CONSTANT_COLOR;
118 case WINED3D_BLEND_INVBLENDFACTOR:
119 if (alpha)
120 return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA;
121 return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR;
122 case WINED3D_BLEND_SRC1COLOR:
123 return VK_BLEND_FACTOR_SRC1_COLOR;
124 case WINED3D_BLEND_INVSRC1COLOR:
125 return VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR;
126 case WINED3D_BLEND_SRC1ALPHA:
127 return VK_BLEND_FACTOR_SRC1_ALPHA;
128 case WINED3D_BLEND_INVSRC1ALPHA:
129 return VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA;
130 default:
131 FIXME("Unhandled blend %#x.\n", blend);
132 return VK_BLEND_FACTOR_ZERO;
136 static VkBlendOp vk_blend_op_from_wined3d(enum wined3d_blend_op op)
138 switch (op)
140 case WINED3D_BLEND_OP_ADD:
141 return VK_BLEND_OP_ADD;
142 case WINED3D_BLEND_OP_SUBTRACT:
143 return VK_BLEND_OP_SUBTRACT;
144 case WINED3D_BLEND_OP_REVSUBTRACT:
145 return VK_BLEND_OP_REVERSE_SUBTRACT;
146 case WINED3D_BLEND_OP_MIN:
147 return VK_BLEND_OP_MIN;
148 case WINED3D_BLEND_OP_MAX:
149 return VK_BLEND_OP_MAX;
150 default:
151 FIXME("Unhandled blend op %#x.\n", op);
152 return VK_BLEND_OP_ADD;
156 static VkColorComponentFlags vk_colour_write_mask_from_wined3d(uint32_t wined3d_mask)
158 VkColorComponentFlags vk_mask = 0;
160 if (wined3d_mask & WINED3DCOLORWRITEENABLE_RED)
161 vk_mask |= VK_COLOR_COMPONENT_R_BIT;
162 if (wined3d_mask & WINED3DCOLORWRITEENABLE_GREEN)
163 vk_mask |= VK_COLOR_COMPONENT_G_BIT;
164 if (wined3d_mask & WINED3DCOLORWRITEENABLE_BLUE)
165 vk_mask |= VK_COLOR_COMPONENT_B_BIT;
166 if (wined3d_mask & WINED3DCOLORWRITEENABLE_ALPHA)
167 vk_mask |= VK_COLOR_COMPONENT_A_BIT;
169 return vk_mask;
172 static VkCullModeFlags vk_cull_mode_from_wined3d(enum wined3d_cull mode)
174 switch (mode)
176 case WINED3D_CULL_NONE:
177 return VK_CULL_MODE_NONE;
178 case WINED3D_CULL_FRONT:
179 return VK_CULL_MODE_FRONT_BIT;
180 case WINED3D_CULL_BACK:
181 return VK_CULL_MODE_BACK_BIT;
182 default:
183 FIXME("Unhandled cull mode %#x.\n", mode);
184 return VK_CULL_MODE_NONE;
188 static VkPrimitiveTopology vk_topology_from_wined3d(enum wined3d_primitive_type t)
190 switch (t)
192 case WINED3D_PT_POINTLIST:
193 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
194 case WINED3D_PT_LINELIST:
195 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
196 case WINED3D_PT_LINESTRIP:
197 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
198 case WINED3D_PT_TRIANGLELIST:
199 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
200 case WINED3D_PT_TRIANGLESTRIP:
201 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
202 case WINED3D_PT_TRIANGLEFAN:
203 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
204 case WINED3D_PT_LINELIST_ADJ:
205 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
206 case WINED3D_PT_LINESTRIP_ADJ:
207 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY;
208 case WINED3D_PT_TRIANGLELIST_ADJ:
209 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY;
210 case WINED3D_PT_TRIANGLESTRIP_ADJ:
211 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY;
212 case WINED3D_PT_PATCH:
213 return VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
214 default:
215 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(t));
216 case WINED3D_PT_UNDEFINED:
217 return ~0u;
221 static VkStencilOp vk_stencil_op_from_wined3d(enum wined3d_stencil_op op)
223 switch (op)
225 case WINED3D_STENCIL_OP_KEEP:
226 return VK_STENCIL_OP_KEEP;
227 case WINED3D_STENCIL_OP_ZERO:
228 return VK_STENCIL_OP_ZERO;
229 case WINED3D_STENCIL_OP_REPLACE:
230 return VK_STENCIL_OP_REPLACE;
231 case WINED3D_STENCIL_OP_INCR_SAT:
232 return VK_STENCIL_OP_INCREMENT_AND_CLAMP;
233 case WINED3D_STENCIL_OP_DECR_SAT:
234 return VK_STENCIL_OP_DECREMENT_AND_CLAMP;
235 case WINED3D_STENCIL_OP_INVERT:
236 return VK_STENCIL_OP_INVERT;
237 case WINED3D_STENCIL_OP_INCR:
238 return VK_STENCIL_OP_INCREMENT_AND_WRAP;
239 case WINED3D_STENCIL_OP_DECR:
240 return VK_STENCIL_OP_DECREMENT_AND_WRAP;
241 default:
242 if (!op)
243 WARN("Unhandled stencil operation %#x.\n", op);
244 else
245 FIXME("Unhandled stencil operation %#x.\n", op);
246 return VK_STENCIL_OP_KEEP;
250 void *wined3d_allocator_chunk_vk_map(struct wined3d_allocator_chunk_vk *chunk_vk,
251 struct wined3d_context_vk *context_vk)
253 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
254 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
255 VkResult vr;
257 TRACE("chunk %p, memory 0x%s, map_ptr %p.\n", chunk_vk,
258 wine_dbgstr_longlong(chunk_vk->vk_memory), chunk_vk->c.map_ptr);
260 if (!chunk_vk->c.map_ptr && (vr = VK_CALL(vkMapMemory(device_vk->vk_device,
261 chunk_vk->vk_memory, 0, VK_WHOLE_SIZE, 0, &chunk_vk->c.map_ptr))) < 0)
263 ERR("Failed to map chunk memory, vr %s.\n", wined3d_debug_vkresult(vr));
264 return NULL;
267 ++chunk_vk->c.map_count;
269 return chunk_vk->c.map_ptr;
272 void wined3d_allocator_chunk_vk_unmap(struct wined3d_allocator_chunk_vk *chunk_vk,
273 struct wined3d_context_vk *context_vk)
275 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
276 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
278 TRACE("chunk_vk %p, context_vk %p.\n", chunk_vk, context_vk);
280 if (--chunk_vk->c.map_count)
281 return;
283 VK_CALL(vkUnmapMemory(device_vk->vk_device, chunk_vk->vk_memory));
284 chunk_vk->c.map_ptr = NULL;
287 VkDeviceMemory wined3d_context_vk_allocate_vram_chunk_memory(struct wined3d_context_vk *context_vk,
288 unsigned int pool, size_t size)
290 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
291 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
292 VkMemoryAllocateInfo allocate_info;
293 VkDeviceMemory vk_memory;
294 VkResult vr;
296 allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
297 allocate_info.pNext = NULL;
298 allocate_info.allocationSize = size;
299 allocate_info.memoryTypeIndex = pool;
300 if ((vr = VK_CALL(vkAllocateMemory(device_vk->vk_device, &allocate_info, NULL, &vk_memory))) < 0)
302 ERR("Failed to allocate memory, vr %s.\n", wined3d_debug_vkresult(vr));
303 return VK_NULL_HANDLE;
306 return vk_memory;
309 struct wined3d_allocator_block *wined3d_context_vk_allocate_memory(struct wined3d_context_vk *context_vk,
310 unsigned int memory_type, VkDeviceSize size, VkDeviceMemory *vk_memory)
312 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
313 struct wined3d_allocator *allocator = &device_vk->allocator;
314 struct wined3d_allocator_block *block;
316 if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
318 *vk_memory = wined3d_context_vk_allocate_vram_chunk_memory(context_vk, memory_type, size);
319 return NULL;
322 if (!(block = wined3d_allocator_allocate(allocator, &context_vk->c, memory_type, size)))
324 *vk_memory = VK_NULL_HANDLE;
325 return NULL;
328 *vk_memory = wined3d_allocator_chunk_vk(block->chunk)->vk_memory;
330 return block;
333 static bool wined3d_context_vk_create_slab_bo(struct wined3d_context_vk *context_vk,
334 VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags memory_type, struct wined3d_bo_vk *bo)
336 const struct wined3d_adapter_vk *adapter_vk = wined3d_adapter_vk(context_vk->c.device->adapter);
337 const VkPhysicalDeviceLimits *limits = &adapter_vk->device_limits;
338 struct wined3d_bo_slab_vk_key key;
339 struct wined3d_bo_slab_vk *slab;
340 struct wine_rb_entry *entry;
341 size_t object_size, idx;
342 size_t alignment;
344 if (size > WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 2)
345 return false;
347 alignment = WINED3D_SLAB_BO_MIN_OBJECT_ALIGN;
348 if ((usage & (VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT))
349 && limits->minTexelBufferOffsetAlignment > alignment)
350 alignment = limits->minTexelBufferOffsetAlignment;
351 if ((usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) && limits->minUniformBufferOffsetAlignment)
352 alignment = limits->minUniformBufferOffsetAlignment;
353 if ((usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) && limits->minStorageBufferOffsetAlignment)
354 alignment = limits->minStorageBufferOffsetAlignment;
356 object_size = (size + (alignment - 1)) & ~(alignment - 1);
357 if (object_size < WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 32)
358 object_size = WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 32;
359 key.memory_type = memory_type;
360 key.usage = usage;
361 key.size = 32 * object_size;
363 if ((entry = wine_rb_get(&context_vk->bo_slab_available, &key)))
365 slab = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
366 TRACE("Using existing bo slab %p.\n", slab);
368 else
370 if (!(slab = heap_alloc_zero(sizeof(*slab))))
372 ERR("Failed to allocate bo slab.\n");
373 return false;
376 slab->requested_memory_type = memory_type;
377 if (!wined3d_context_vk_create_bo(context_vk, key.size, usage, memory_type, &slab->bo))
379 ERR("Failed to create slab bo.\n");
380 heap_free(slab);
381 return false;
383 slab->map = ~0u;
385 if (wine_rb_put(&context_vk->bo_slab_available, &key, &slab->entry) < 0)
387 ERR("Failed to add slab to available tree.\n");
388 wined3d_context_vk_destroy_bo(context_vk, &slab->bo);
389 heap_free(slab);
390 return false;
393 TRACE("Created new bo slab %p.\n", slab);
396 idx = wined3d_bit_scan(&slab->map);
397 if (!slab->map)
399 if (slab->next)
401 wine_rb_replace(&context_vk->bo_slab_available, &slab->entry, &slab->next->entry);
402 slab->next = NULL;
404 else
406 wine_rb_remove(&context_vk->bo_slab_available, &slab->entry);
410 *bo = slab->bo;
411 bo->memory = NULL;
412 bo->slab = slab;
413 bo->buffer_offset = idx * object_size;
414 bo->memory_offset = slab->bo.memory_offset + bo->buffer_offset;
415 bo->size = size;
416 list_init(&bo->users);
417 bo->command_buffer_id = 0;
419 TRACE("Using buffer 0x%s, memory 0x%s, offset 0x%s for bo %p.\n",
420 wine_dbgstr_longlong(bo->vk_buffer), wine_dbgstr_longlong(bo->vk_memory),
421 wine_dbgstr_longlong(bo->buffer_offset), bo);
423 return true;
426 BOOL wined3d_context_vk_create_bo(struct wined3d_context_vk *context_vk, VkDeviceSize size,
427 VkBufferUsageFlags usage, VkMemoryPropertyFlags memory_type, struct wined3d_bo_vk *bo)
429 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
430 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
431 VkMemoryRequirements memory_requirements;
432 struct wined3d_adapter_vk *adapter_vk;
433 VkBufferCreateInfo create_info;
434 unsigned int memory_type_idx;
435 VkResult vr;
437 if (wined3d_context_vk_create_slab_bo(context_vk, size, usage, memory_type, bo))
438 return TRUE;
440 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
442 create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
443 create_info.pNext = NULL;
444 create_info.flags = 0;
445 create_info.size = size;
446 create_info.usage = usage;
447 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
448 create_info.queueFamilyIndexCount = 0;
449 create_info.pQueueFamilyIndices = NULL;
451 if ((vr = VK_CALL(vkCreateBuffer(device_vk->vk_device, &create_info, NULL, &bo->vk_buffer))) < 0)
453 ERR("Failed to create Vulkan buffer, vr %s.\n", wined3d_debug_vkresult(vr));
454 return FALSE;
457 VK_CALL(vkGetBufferMemoryRequirements(device_vk->vk_device, bo->vk_buffer, &memory_requirements));
459 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
460 memory_requirements.memoryTypeBits, memory_type);
461 if (memory_type_idx == ~0u)
463 ERR("Failed to find suitable memory type.\n");
464 VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
465 return FALSE;
467 bo->memory = wined3d_context_vk_allocate_memory(context_vk,
468 memory_type_idx, memory_requirements.size, &bo->vk_memory);
469 if (!bo->vk_memory)
471 ERR("Failed to allocate buffer memory.\n");
472 VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
473 return FALSE;
475 bo->memory_offset = bo->memory ? bo->memory->offset : 0;
477 if ((vr = VK_CALL(vkBindBufferMemory(device_vk->vk_device, bo->vk_buffer,
478 bo->vk_memory, bo->memory_offset))) < 0)
480 ERR("Failed to bind buffer memory, vr %s.\n", wined3d_debug_vkresult(vr));
481 if (bo->memory)
482 wined3d_allocator_block_free(bo->memory);
483 else
484 VK_CALL(vkFreeMemory(device_vk->vk_device, bo->vk_memory, NULL));
485 VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
486 return FALSE;
489 bo->map_ptr = NULL;
490 bo->buffer_offset = 0;
491 bo->size = size;
492 bo->usage = usage;
493 bo->memory_type = adapter_vk->memory_properties.memoryTypes[memory_type_idx].propertyFlags;
494 list_init(&bo->users);
495 bo->command_buffer_id = 0;
496 bo->slab = NULL;
498 TRACE("Created buffer 0x%s, memory 0x%s for bo %p.\n",
499 wine_dbgstr_longlong(bo->vk_buffer), wine_dbgstr_longlong(bo->vk_memory), bo);
501 return TRUE;
504 BOOL wined3d_context_vk_create_image(struct wined3d_context_vk *context_vk, VkImageType vk_image_type,
505 VkImageUsageFlags usage, VkFormat vk_format, unsigned int width, unsigned int height, unsigned int depth,
506 unsigned int sample_count, unsigned int mip_levels, unsigned int layer_count, unsigned int flags,
507 struct wined3d_image_vk *image)
509 struct wined3d_adapter_vk *adapter_vk = wined3d_adapter_vk(context_vk->c.device->adapter);
510 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
511 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
512 VkMemoryRequirements memory_requirements;
513 VkImageCreateInfo create_info;
514 unsigned int memory_type_idx;
515 VkResult vr;
517 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
518 create_info.pNext = NULL;
519 create_info.flags = flags;
520 create_info.imageType = vk_image_type;
521 create_info.format = vk_format;
522 create_info.extent.width = width;
523 create_info.extent.height = height;
524 create_info.extent.depth = depth;
525 create_info.mipLevels = mip_levels;
526 create_info.arrayLayers = layer_count;
527 create_info.samples = sample_count;
528 create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
529 create_info.usage = usage;
530 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
531 create_info.queueFamilyIndexCount = 0;
532 create_info.pQueueFamilyIndices = NULL;
533 create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
535 image->command_buffer_id = 0;
537 vr = VK_CALL(vkCreateImage(device_vk->vk_device, &create_info, NULL, &image->vk_image));
538 if (vr != VK_SUCCESS)
540 ERR("Failed to create image, vr %s.\n", wined3d_debug_vkresult(vr));
541 image->vk_image = VK_NULL_HANDLE;
542 return FALSE;
545 VK_CALL(vkGetImageMemoryRequirements(device_vk->vk_device, image->vk_image,
546 &memory_requirements));
548 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
549 memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
550 if (memory_type_idx == ~0u)
552 ERR("Failed to find suitable image memory type.\n");
553 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
554 image->vk_image = VK_NULL_HANDLE;
555 return FALSE;
558 image->memory = wined3d_context_vk_allocate_memory(context_vk, memory_type_idx,
559 memory_requirements.size, &image->vk_memory);
560 if (!image->vk_memory)
562 ERR("Failed to allocate image memory.\n");
563 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
564 image->vk_image = VK_NULL_HANDLE;
565 return FALSE;
568 vr = VK_CALL(vkBindImageMemory(device_vk->vk_device, image->vk_image, image->vk_memory,
569 image->memory ? image->memory->offset : 0));
570 if (vr != VK_SUCCESS)
572 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
573 if (image->memory)
574 wined3d_allocator_block_free(image->memory);
575 else
576 VK_CALL(vkFreeMemory(device_vk->vk_device, image->vk_memory, NULL));
577 ERR("Failed to bind image memory, vr %s.\n", wined3d_debug_vkresult(vr));
578 image->memory = NULL;
579 image->vk_memory = VK_NULL_HANDLE;
580 image->vk_image = VK_NULL_HANDLE;
581 return FALSE;
584 return TRUE;
587 static struct wined3d_retired_object_vk *wined3d_context_vk_get_retired_object_vk(struct wined3d_context_vk *context_vk)
589 struct wined3d_retired_objects_vk *retired = &context_vk->retired;
590 struct wined3d_retired_object_vk *o;
592 if (retired->free)
594 o = retired->free;
595 retired->free = o->u.next;
596 return o;
599 if (!wined3d_array_reserve((void **)&retired->objects, &retired->size,
600 retired->count + 1, sizeof(*retired->objects)))
601 return NULL;
603 return &retired->objects[retired->count++];
606 void wined3d_context_vk_destroy_vk_framebuffer(struct wined3d_context_vk *context_vk,
607 VkFramebuffer vk_framebuffer, uint64_t command_buffer_id)
609 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
610 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
611 struct wined3d_retired_object_vk *o;
613 if (context_vk->completed_command_buffer_id > command_buffer_id)
615 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, vk_framebuffer, NULL));
616 TRACE("Destroyed framebuffer 0x%s.\n", wine_dbgstr_longlong(vk_framebuffer));
617 return;
620 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
622 ERR("Leaking framebuffer 0x%s.\n", wine_dbgstr_longlong(vk_framebuffer));
623 return;
626 o->type = WINED3D_RETIRED_FRAMEBUFFER_VK;
627 o->u.vk_framebuffer = vk_framebuffer;
628 o->command_buffer_id = command_buffer_id;
631 static void wined3d_context_vk_destroy_vk_descriptor_pool(struct wined3d_context_vk *context_vk,
632 VkDescriptorPool vk_descriptor_pool, uint64_t command_buffer_id)
634 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
635 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
636 struct wined3d_retired_object_vk *o;
638 if (context_vk->completed_command_buffer_id > command_buffer_id)
640 VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, vk_descriptor_pool, NULL));
641 TRACE("Destroyed descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
642 return;
645 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
647 ERR("Leaking descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
648 return;
651 o->type = WINED3D_RETIRED_DESCRIPTOR_POOL_VK;
652 o->u.vk_descriptor_pool = vk_descriptor_pool;
653 o->command_buffer_id = command_buffer_id;
656 void wined3d_context_vk_destroy_vk_memory(struct wined3d_context_vk *context_vk,
657 VkDeviceMemory vk_memory, uint64_t command_buffer_id)
659 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
660 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
661 struct wined3d_retired_object_vk *o;
663 if (context_vk->completed_command_buffer_id > command_buffer_id)
665 VK_CALL(vkFreeMemory(device_vk->vk_device, vk_memory, NULL));
666 TRACE("Freed memory 0x%s.\n", wine_dbgstr_longlong(vk_memory));
667 return;
670 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
672 ERR("Leaking memory 0x%s.\n", wine_dbgstr_longlong(vk_memory));
673 return;
676 o->type = WINED3D_RETIRED_MEMORY_VK;
677 o->u.vk_memory = vk_memory;
678 o->command_buffer_id = command_buffer_id;
681 void wined3d_context_vk_destroy_allocator_block(struct wined3d_context_vk *context_vk,
682 struct wined3d_allocator_block *block, uint64_t command_buffer_id)
684 struct wined3d_retired_object_vk *o;
686 if (context_vk->completed_command_buffer_id > command_buffer_id)
688 wined3d_allocator_block_free(block);
689 TRACE("Freed block %p.\n", block);
690 return;
693 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
695 ERR("Leaking block %p.\n", block);
696 return;
699 o->type = WINED3D_RETIRED_ALLOCATOR_BLOCK_VK;
700 o->u.block = block;
701 o->command_buffer_id = command_buffer_id;
704 static void wined3d_bo_slab_vk_free_slice(struct wined3d_bo_slab_vk *slab,
705 SIZE_T idx, struct wined3d_context_vk *context_vk)
707 struct wined3d_bo_slab_vk_key key;
708 struct wine_rb_entry *entry;
710 TRACE("slab %p, idx %lu, context_vk %p.\n", slab, idx, context_vk);
712 if (!slab->map)
714 key.memory_type = slab->requested_memory_type;
715 key.usage = slab->bo.usage;
716 key.size = slab->bo.size;
718 if ((entry = wine_rb_get(&context_vk->bo_slab_available, &key)))
720 slab->next = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
721 wine_rb_replace(&context_vk->bo_slab_available, entry, &slab->entry);
723 else if (wine_rb_put(&context_vk->bo_slab_available, &key, &slab->entry) < 0)
725 ERR("Unable to return slab %p (map 0x%08x) to available tree.\n", slab, slab->map);
728 slab->map |= 1u << idx;
731 static void wined3d_context_vk_destroy_bo_slab_slice(struct wined3d_context_vk *context_vk,
732 struct wined3d_bo_slab_vk *slab, SIZE_T idx, uint64_t command_buffer_id)
734 struct wined3d_retired_object_vk *o;
736 if (context_vk->completed_command_buffer_id > command_buffer_id)
738 wined3d_bo_slab_vk_free_slice(slab, idx, context_vk);
739 return;
742 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
744 ERR("Leaking slab %p, slice %#lx.\n", slab, idx);
745 return;
748 o->type = WINED3D_RETIRED_BO_SLAB_SLICE_VK;
749 o->u.slice.slab = slab;
750 o->u.slice.idx = idx;
751 o->command_buffer_id = command_buffer_id;
754 static void wined3d_context_vk_destroy_vk_buffer(struct wined3d_context_vk *context_vk,
755 VkBuffer vk_buffer, uint64_t command_buffer_id)
757 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
758 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
759 struct wined3d_retired_object_vk *o;
761 if (context_vk->completed_command_buffer_id > command_buffer_id)
763 VK_CALL(vkDestroyBuffer(device_vk->vk_device, vk_buffer, NULL));
764 TRACE("Destroyed buffer 0x%s.\n", wine_dbgstr_longlong(vk_buffer));
765 return;
768 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
770 ERR("Leaking buffer 0x%s.\n", wine_dbgstr_longlong(vk_buffer));
771 return;
774 o->type = WINED3D_RETIRED_BUFFER_VK;
775 o->u.vk_buffer = vk_buffer;
776 o->command_buffer_id = command_buffer_id;
779 void wined3d_context_vk_destroy_vk_image(struct wined3d_context_vk *context_vk,
780 VkImage vk_image, uint64_t command_buffer_id)
782 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
783 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
784 struct wined3d_retired_object_vk *o;
786 if (context_vk->completed_command_buffer_id > command_buffer_id)
788 VK_CALL(vkDestroyImage(device_vk->vk_device, vk_image, NULL));
789 TRACE("Destroyed image 0x%s.\n", wine_dbgstr_longlong(vk_image));
790 return;
793 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
795 ERR("Leaking image 0x%s.\n", wine_dbgstr_longlong(vk_image));
796 return;
799 o->type = WINED3D_RETIRED_IMAGE_VK;
800 o->u.vk_image = vk_image;
801 o->command_buffer_id = command_buffer_id;
804 void wined3d_context_vk_destroy_vk_buffer_view(struct wined3d_context_vk *context_vk,
805 VkBufferView vk_view, uint64_t command_buffer_id)
807 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
808 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
809 struct wined3d_retired_object_vk *o;
811 if (context_vk->completed_command_buffer_id > command_buffer_id)
813 VK_CALL(vkDestroyBufferView(device_vk->vk_device, vk_view, NULL));
814 TRACE("Destroyed buffer view 0x%s.\n", wine_dbgstr_longlong(vk_view));
815 return;
818 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
820 ERR("Leaking buffer view 0x%s.\n", wine_dbgstr_longlong(vk_view));
821 return;
824 o->type = WINED3D_RETIRED_BUFFER_VIEW_VK;
825 o->u.vk_buffer_view = vk_view;
826 o->command_buffer_id = command_buffer_id;
829 void wined3d_context_vk_destroy_vk_image_view(struct wined3d_context_vk *context_vk,
830 VkImageView vk_view, uint64_t command_buffer_id)
832 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
833 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
834 struct wined3d_retired_object_vk *o;
836 if (context_vk->completed_command_buffer_id > command_buffer_id)
838 VK_CALL(vkDestroyImageView(device_vk->vk_device, vk_view, NULL));
839 TRACE("Destroyed image view 0x%s.\n", wine_dbgstr_longlong(vk_view));
840 return;
843 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
845 ERR("Leaking image view 0x%s.\n", wine_dbgstr_longlong(vk_view));
846 return;
849 o->type = WINED3D_RETIRED_IMAGE_VIEW_VK;
850 o->u.vk_image_view = vk_view;
851 o->command_buffer_id = command_buffer_id;
854 void wined3d_context_vk_destroy_vk_sampler(struct wined3d_context_vk *context_vk,
855 VkSampler vk_sampler, uint64_t command_buffer_id)
857 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
858 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
859 struct wined3d_retired_object_vk *o;
861 if (context_vk->completed_command_buffer_id > command_buffer_id)
863 VK_CALL(vkDestroySampler(device_vk->vk_device, vk_sampler, NULL));
864 TRACE("Destroyed sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
865 return;
868 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
870 ERR("Leaking sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
871 return;
874 o->type = WINED3D_RETIRED_SAMPLER_VK;
875 o->u.vk_sampler = vk_sampler;
876 o->command_buffer_id = command_buffer_id;
879 void wined3d_context_vk_destroy_image(struct wined3d_context_vk *context_vk, struct wined3d_image_vk *image)
881 wined3d_context_vk_destroy_vk_image(context_vk, image->vk_image, image->command_buffer_id);
882 if (image->memory)
883 wined3d_context_vk_destroy_allocator_block(context_vk, image->memory,
884 image->command_buffer_id);
885 else
886 wined3d_context_vk_destroy_vk_memory(context_vk, image->vk_memory, image->command_buffer_id);
888 image->vk_image = VK_NULL_HANDLE;
889 image->vk_memory = VK_NULL_HANDLE;
890 image->memory = NULL;
893 void wined3d_context_vk_destroy_bo(struct wined3d_context_vk *context_vk, const struct wined3d_bo_vk *bo)
895 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
896 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
897 struct wined3d_bo_slab_vk *slab_vk;
898 size_t object_size, idx;
900 TRACE("context_vk %p, bo %p.\n", context_vk, bo);
902 if ((slab_vk = bo->slab))
904 if (bo->map_ptr)
905 wined3d_bo_slab_vk_unmap(slab_vk, context_vk);
906 object_size = slab_vk->bo.size / 32;
907 idx = bo->buffer_offset / object_size;
908 wined3d_context_vk_destroy_bo_slab_slice(context_vk, slab_vk, idx, bo->command_buffer_id);
909 return;
912 wined3d_context_vk_destroy_vk_buffer(context_vk, bo->vk_buffer, bo->command_buffer_id);
913 if (bo->memory)
915 if (bo->map_ptr)
916 wined3d_allocator_chunk_vk_unmap(wined3d_allocator_chunk_vk(bo->memory->chunk), context_vk);
917 wined3d_context_vk_destroy_allocator_block(context_vk, bo->memory, bo->command_buffer_id);
918 return;
921 if (bo->map_ptr)
922 VK_CALL(vkUnmapMemory(device_vk->vk_device, bo->vk_memory));
923 wined3d_context_vk_destroy_vk_memory(context_vk, bo->vk_memory, bo->command_buffer_id);
926 void wined3d_context_vk_poll_command_buffers(struct wined3d_context_vk *context_vk)
928 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
929 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
930 struct wined3d_command_buffer_vk *buffer;
931 SIZE_T i = 0;
933 while (i < context_vk->submitted.buffer_count)
935 buffer = &context_vk->submitted.buffers[i];
936 if (VK_CALL(vkGetFenceStatus(device_vk->vk_device, buffer->vk_fence)) == VK_NOT_READY)
938 ++i;
939 continue;
942 TRACE("Command buffer %p with id 0x%s has finished.\n",
943 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
944 VK_CALL(vkDestroyFence(device_vk->vk_device, buffer->vk_fence, NULL));
945 VK_CALL(vkFreeCommandBuffers(device_vk->vk_device,
946 context_vk->vk_command_pool, 1, &buffer->vk_command_buffer));
948 if (buffer->id > context_vk->completed_command_buffer_id)
949 context_vk->completed_command_buffer_id = buffer->id;
950 *buffer = context_vk->submitted.buffers[--context_vk->submitted.buffer_count];
954 static void wined3d_context_vk_cleanup_resources(struct wined3d_context_vk *context_vk)
956 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
957 struct wined3d_retired_objects_vk *retired = &context_vk->retired;
958 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
959 struct wined3d_retired_object_vk *o;
960 uint64_t command_buffer_id;
961 SIZE_T i = 0;
963 wined3d_context_vk_poll_command_buffers(context_vk);
964 command_buffer_id = context_vk->completed_command_buffer_id;
966 retired->free = NULL;
967 for (i = retired->count; i; --i)
969 o = &retired->objects[i - 1];
971 if (o->type != WINED3D_RETIRED_FREE_VK && o->command_buffer_id > command_buffer_id)
972 continue;
974 switch (o->type)
976 case WINED3D_RETIRED_FREE_VK:
977 /* Nothing to do. */
978 break;
980 case WINED3D_RETIRED_FRAMEBUFFER_VK:
981 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, o->u.vk_framebuffer, NULL));
982 TRACE("Destroyed framebuffer 0x%s.\n", wine_dbgstr_longlong(o->u.vk_framebuffer));
983 break;
985 case WINED3D_RETIRED_DESCRIPTOR_POOL_VK:
986 VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, o->u.vk_descriptor_pool, NULL));
987 TRACE("Destroyed descriptor pool 0x%s.\n", wine_dbgstr_longlong(o->u.vk_descriptor_pool));
988 break;
990 case WINED3D_RETIRED_MEMORY_VK:
991 VK_CALL(vkFreeMemory(device_vk->vk_device, o->u.vk_memory, NULL));
992 TRACE("Freed memory 0x%s.\n", wine_dbgstr_longlong(o->u.vk_memory));
993 break;
995 case WINED3D_RETIRED_ALLOCATOR_BLOCK_VK:
996 TRACE("Destroying block %p.\n", o->u.block);
997 wined3d_allocator_block_free(o->u.block);
998 break;
1000 case WINED3D_RETIRED_BO_SLAB_SLICE_VK:
1001 wined3d_bo_slab_vk_free_slice(o->u.slice.slab, o->u.slice.idx, context_vk);
1002 break;
1004 case WINED3D_RETIRED_BUFFER_VK:
1005 VK_CALL(vkDestroyBuffer(device_vk->vk_device, o->u.vk_buffer, NULL));
1006 TRACE("Destroyed buffer 0x%s.\n", wine_dbgstr_longlong(o->u.vk_buffer));
1007 break;
1009 case WINED3D_RETIRED_IMAGE_VK:
1010 VK_CALL(vkDestroyImage(device_vk->vk_device, o->u.vk_image, NULL));
1011 TRACE("Destroyed image 0x%s.\n", wine_dbgstr_longlong(o->u.vk_image));
1012 break;
1014 case WINED3D_RETIRED_BUFFER_VIEW_VK:
1015 VK_CALL(vkDestroyBufferView(device_vk->vk_device, o->u.vk_buffer_view, NULL));
1016 TRACE("Destroyed buffer view 0x%s.\n", wine_dbgstr_longlong(o->u.vk_buffer_view));
1017 break;
1019 case WINED3D_RETIRED_IMAGE_VIEW_VK:
1020 VK_CALL(vkDestroyImageView(device_vk->vk_device, o->u.vk_image_view, NULL));
1021 TRACE("Destroyed image view 0x%s.\n", wine_dbgstr_longlong(o->u.vk_image_view));
1022 break;
1024 case WINED3D_RETIRED_SAMPLER_VK:
1025 VK_CALL(vkDestroySampler(device_vk->vk_device, o->u.vk_sampler, NULL));
1026 TRACE("Destroyed sampler 0x%s.\n", wine_dbgstr_longlong(o->u.vk_sampler));
1027 break;
1029 default:
1030 ERR("Unhandled object type %#x.\n", o->type);
1031 break;
1034 if (i == retired->count)
1036 --retired->count;
1037 continue;
1040 o->type = WINED3D_RETIRED_FREE_VK;
1041 o->u.next = retired->free;
1042 retired->free = o;
1046 static void wined3d_context_vk_destroy_bo_slab(struct wine_rb_entry *entry, void *ctx)
1048 struct wined3d_context_vk *context_vk = ctx;
1049 struct wined3d_bo_slab_vk *slab, *next;
1051 slab = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
1052 while (slab)
1054 next = slab->next;
1055 wined3d_context_vk_destroy_bo(context_vk, &slab->bo);
1056 heap_free(slab);
1057 slab = next;
1061 static void wined3d_context_vk_destroy_graphics_pipeline(struct wine_rb_entry *entry, void *ctx)
1063 struct wined3d_graphics_pipeline_vk *pipeline_vk = WINE_RB_ENTRY_VALUE(entry,
1064 struct wined3d_graphics_pipeline_vk, entry);
1065 struct wined3d_context_vk *context_vk = ctx;
1066 const struct wined3d_vk_info *vk_info;
1067 struct wined3d_device_vk *device_vk;
1069 vk_info = context_vk->vk_info;
1070 device_vk = wined3d_device_vk(context_vk->c.device);
1072 VK_CALL(vkDestroyPipeline(device_vk->vk_device, pipeline_vk->vk_pipeline, NULL));
1073 heap_free(pipeline_vk);
1076 static void wined3d_context_vk_destroy_pipeline_layout(struct wine_rb_entry *entry, void *ctx)
1078 struct wined3d_pipeline_layout_vk *layout = WINE_RB_ENTRY_VALUE(entry,
1079 struct wined3d_pipeline_layout_vk, entry);
1080 struct wined3d_context_vk *context_vk = ctx;
1081 const struct wined3d_vk_info *vk_info;
1082 struct wined3d_device_vk *device_vk;
1084 vk_info = context_vk->vk_info;
1085 device_vk = wined3d_device_vk(context_vk->c.device);
1087 VK_CALL(vkDestroyPipelineLayout(device_vk->vk_device, layout->vk_pipeline_layout, NULL));
1088 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
1089 heap_free(layout->key.bindings);
1090 heap_free(layout);
1093 static void wined3d_render_pass_key_vk_init(struct wined3d_render_pass_key_vk *key,
1094 const struct wined3d_fb_state *fb, unsigned int rt_count, bool depth_stencil, uint32_t clear_flags)
1096 struct wined3d_render_pass_attachment_vk *a;
1097 struct wined3d_rendertarget_view *view;
1098 unsigned int i;
1100 memset(key, 0, sizeof(*key));
1102 for (i = 0; i < rt_count; ++i)
1104 if (!(view = fb->render_targets[i]) || view->format->id == WINED3DFMT_NULL)
1105 continue;
1107 a = &key->rt[i];
1108 a->vk_format = wined3d_format_vk(view->format)->vk_format;
1109 a->vk_samples = max(1, wined3d_resource_get_sample_count(view->resource));
1110 a->vk_layout = wined3d_texture_vk(wined3d_texture_from_resource(view->resource))->layout;
1111 key->rt_mask |= 1u << i;
1114 if (depth_stencil && (view = fb->depth_stencil))
1116 a = &key->ds;
1117 a->vk_format = wined3d_format_vk(view->format)->vk_format;
1118 a->vk_samples = max(1, wined3d_resource_get_sample_count(view->resource));
1119 a->vk_layout = wined3d_texture_vk(wined3d_texture_from_resource(view->resource))->layout;
1120 key->rt_mask |= 1u << WINED3D_MAX_RENDER_TARGETS;
1123 key->clear_flags = clear_flags;
1126 static void wined3d_render_pass_vk_cleanup(struct wined3d_render_pass_vk *pass,
1127 struct wined3d_context_vk *context_vk)
1129 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1130 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1132 VK_CALL(vkDestroyRenderPass(device_vk->vk_device, pass->vk_render_pass, NULL));
1135 static bool wined3d_render_pass_vk_init(struct wined3d_render_pass_vk *pass,
1136 struct wined3d_context_vk *context_vk, const struct wined3d_render_pass_key_vk *key)
1138 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1139 VkAttachmentReference attachment_references[WINED3D_MAX_RENDER_TARGETS];
1140 VkAttachmentDescription attachments[WINED3D_MAX_RENDER_TARGETS + 1];
1141 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1142 const struct wined3d_render_pass_attachment_vk *a;
1143 VkAttachmentReference ds_attachment_reference;
1144 VkAttachmentReference *ds_reference = NULL;
1145 unsigned int attachment_count, rt_count, i;
1146 VkAttachmentDescription *attachment;
1147 VkSubpassDescription sub_pass_desc;
1148 VkRenderPassCreateInfo pass_desc;
1149 uint32_t mask;
1150 VkResult vr;
1152 rt_count = 0;
1153 attachment_count = 0;
1154 mask = key->rt_mask & ((1u << WINED3D_MAX_RENDER_TARGETS) - 1);
1155 while (mask)
1157 i = wined3d_bit_scan(&mask);
1158 a = &key->rt[i];
1160 attachment = &attachments[attachment_count];
1161 attachment->flags = 0;
1162 attachment->format = a->vk_format;
1163 attachment->samples = a->vk_samples;
1164 if (key->clear_flags & WINED3DCLEAR_TARGET)
1165 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1166 else
1167 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1168 attachment->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1169 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1170 attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
1171 attachment->initialLayout = a->vk_layout;
1172 attachment->finalLayout = a->vk_layout;
1174 attachment_references[i].attachment = attachment_count;
1175 attachment_references[i].layout = a->vk_layout;
1177 ++attachment_count;
1178 rt_count = i + 1;
1181 mask = ~key->rt_mask & ((1u << rt_count) - 1);
1182 while (mask)
1184 i = wined3d_bit_scan(&mask);
1185 attachment_references[i].attachment = VK_ATTACHMENT_UNUSED;
1186 attachment_references[i].layout = VK_IMAGE_LAYOUT_UNDEFINED;
1189 if (key->rt_mask & (1u << WINED3D_MAX_RENDER_TARGETS))
1191 a = &key->ds;
1193 attachment = &attachments[attachment_count];
1194 attachment->flags = 0;
1195 attachment->format = a->vk_format;
1196 attachment->samples = a->vk_samples;
1197 if (key->clear_flags & WINED3DCLEAR_ZBUFFER)
1198 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1199 else
1200 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1201 attachment->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1202 if (key->clear_flags & WINED3DCLEAR_STENCIL)
1203 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1204 else
1205 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1206 attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
1207 attachment->initialLayout = a->vk_layout;
1208 attachment->finalLayout = a->vk_layout;
1210 ds_reference = &ds_attachment_reference;
1211 ds_reference->attachment = attachment_count;
1212 ds_reference->layout = a->vk_layout;
1214 ++attachment_count;
1217 sub_pass_desc.flags = 0;
1218 sub_pass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
1219 sub_pass_desc.inputAttachmentCount = 0;
1220 sub_pass_desc.pInputAttachments = NULL;
1221 sub_pass_desc.colorAttachmentCount = rt_count;
1222 sub_pass_desc.pColorAttachments = attachment_references;
1223 sub_pass_desc.pResolveAttachments = NULL;
1224 sub_pass_desc.pDepthStencilAttachment = ds_reference;
1225 sub_pass_desc.preserveAttachmentCount = 0;
1226 sub_pass_desc.pPreserveAttachments = NULL;
1228 pass_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
1229 pass_desc.pNext = NULL;
1230 pass_desc.flags = 0;
1231 pass_desc.attachmentCount = attachment_count;
1232 pass_desc.pAttachments = attachments;
1233 pass_desc.subpassCount = 1;
1234 pass_desc.pSubpasses = &sub_pass_desc;
1235 pass_desc.dependencyCount = 0;
1236 pass_desc.pDependencies = NULL;
1238 pass->key = *key;
1239 if ((vr = VK_CALL(vkCreateRenderPass(device_vk->vk_device,
1240 &pass_desc, NULL, &pass->vk_render_pass))) < 0)
1242 WARN("Failed to create Vulkan render pass, vr %d.\n", vr);
1243 return false;
1246 return true;
1249 VkRenderPass wined3d_context_vk_get_render_pass(struct wined3d_context_vk *context_vk,
1250 const struct wined3d_fb_state *fb, unsigned int rt_count, bool depth_stencil, uint32_t clear_flags)
1252 struct wined3d_render_pass_key_vk key;
1253 struct wined3d_render_pass_vk *pass;
1254 struct wine_rb_entry *entry;
1256 wined3d_render_pass_key_vk_init(&key, fb, rt_count, depth_stencil, clear_flags);
1257 if ((entry = wine_rb_get(&context_vk->render_passes, &key)))
1258 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_render_pass_vk, entry)->vk_render_pass;
1260 if (!(pass = heap_alloc(sizeof(*pass))))
1261 return VK_NULL_HANDLE;
1263 if (!wined3d_render_pass_vk_init(pass, context_vk, &key))
1265 heap_free(pass);
1266 return VK_NULL_HANDLE;
1269 if (wine_rb_put(&context_vk->render_passes, &pass->key, &pass->entry) == -1)
1271 ERR("Failed to insert render pass.\n");
1272 wined3d_render_pass_vk_cleanup(pass, context_vk);
1273 heap_free(pass);
1274 return VK_NULL_HANDLE;
1277 return pass->vk_render_pass;
1280 void wined3d_context_vk_end_current_render_pass(struct wined3d_context_vk *context_vk)
1282 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1283 VkCommandBuffer vk_command_buffer;
1285 if (context_vk->vk_render_pass)
1287 vk_command_buffer = context_vk->current_command_buffer.vk_command_buffer;
1288 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
1289 context_vk->vk_render_pass = VK_NULL_HANDLE;
1290 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1291 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 0, NULL));
1294 if (context_vk->vk_framebuffer)
1296 wined3d_context_vk_destroy_vk_framebuffer(context_vk,
1297 context_vk->vk_framebuffer, context_vk->current_command_buffer.id);
1298 context_vk->vk_framebuffer = VK_NULL_HANDLE;
1302 static void wined3d_context_vk_destroy_render_pass(struct wine_rb_entry *entry, void *ctx)
1304 struct wined3d_render_pass_vk *pass = WINE_RB_ENTRY_VALUE(entry,
1305 struct wined3d_render_pass_vk, entry);
1307 wined3d_render_pass_vk_cleanup(pass, ctx);
1308 heap_free(pass);
1311 static void wined3d_shader_descriptor_writes_vk_cleanup(struct wined3d_shader_descriptor_writes_vk *writes)
1313 heap_free(writes->writes);
1316 static void wined3d_context_vk_destroy_query_pools(struct wined3d_context_vk *context_vk, struct list *free_pools)
1318 struct wined3d_query_pool_vk *pool_vk, *entry;
1320 LIST_FOR_EACH_ENTRY_SAFE(pool_vk, entry, free_pools, struct wined3d_query_pool_vk, entry)
1322 wined3d_query_pool_vk_cleanup(pool_vk, context_vk);
1323 heap_free(pool_vk);
1327 bool wined3d_context_vk_allocate_query(struct wined3d_context_vk *context_vk,
1328 enum wined3d_query_type type, struct wined3d_query_pool_idx_vk *pool_idx)
1330 struct wined3d_query_pool_vk *pool_vk, *entry;
1331 struct list *free_pools;
1332 size_t idx;
1334 switch (type)
1336 case WINED3D_QUERY_TYPE_OCCLUSION:
1337 free_pools = &context_vk->free_occlusion_query_pools;
1338 break;
1340 case WINED3D_QUERY_TYPE_TIMESTAMP:
1341 free_pools = &context_vk->free_timestamp_query_pools;
1342 break;
1344 case WINED3D_QUERY_TYPE_PIPELINE_STATISTICS:
1345 free_pools = &context_vk->free_pipeline_statistics_query_pools;
1346 break;
1348 case WINED3D_QUERY_TYPE_SO_STATISTICS:
1349 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM0:
1350 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM1:
1351 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM2:
1352 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM3:
1353 free_pools = &context_vk->free_stream_output_statistics_query_pools;
1354 break;
1356 default:
1357 FIXME("Unhandled query type %#x.\n", type);
1358 return false;
1361 LIST_FOR_EACH_ENTRY_SAFE(pool_vk, entry, free_pools, struct wined3d_query_pool_vk, entry)
1363 if (wined3d_query_pool_vk_allocate_query(pool_vk, &idx))
1364 goto done;
1365 list_remove(&pool_vk->entry);
1368 if (!(pool_vk = heap_alloc_zero(sizeof(*pool_vk))))
1369 return false;
1370 if (!wined3d_query_pool_vk_init(pool_vk, context_vk, type, free_pools))
1372 heap_free(pool_vk);
1373 return false;
1376 if (!wined3d_query_pool_vk_allocate_query(pool_vk, &idx))
1378 wined3d_query_pool_vk_cleanup(pool_vk, context_vk);
1379 heap_free(pool_vk);
1380 return false;
1383 done:
1384 pool_idx->pool_vk = pool_vk;
1385 pool_idx->idx = idx;
1387 return true;
1390 void wined3d_context_vk_cleanup(struct wined3d_context_vk *context_vk)
1392 struct wined3d_command_buffer_vk *buffer = &context_vk->current_command_buffer;
1393 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1394 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1396 if (buffer->vk_command_buffer)
1398 VK_CALL(vkFreeCommandBuffers(device_vk->vk_device,
1399 context_vk->vk_command_pool, 1, &buffer->vk_command_buffer));
1400 buffer->vk_command_buffer = VK_NULL_HANDLE;
1403 wined3d_context_vk_wait_command_buffer(context_vk, buffer->id - 1);
1404 context_vk->completed_command_buffer_id = buffer->id;
1406 heap_free(context_vk->compute.bindings.bindings);
1407 heap_free(context_vk->graphics.bindings.bindings);
1408 if (context_vk->vk_descriptor_pool)
1409 VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, context_vk->vk_descriptor_pool, NULL));
1410 if (context_vk->vk_framebuffer)
1411 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, context_vk->vk_framebuffer, NULL));
1412 VK_CALL(vkDestroyCommandPool(device_vk->vk_device, context_vk->vk_command_pool, NULL));
1413 if (context_vk->vk_so_counter_bo.vk_buffer)
1414 wined3d_context_vk_destroy_bo(context_vk, &context_vk->vk_so_counter_bo);
1415 wined3d_context_vk_cleanup_resources(context_vk);
1416 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_occlusion_query_pools);
1417 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_timestamp_query_pools);
1418 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_pipeline_statistics_query_pools);
1419 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_stream_output_statistics_query_pools);
1420 wine_rb_destroy(&context_vk->bo_slab_available, wined3d_context_vk_destroy_bo_slab, context_vk);
1421 heap_free(context_vk->pending_queries.queries);
1422 heap_free(context_vk->submitted.buffers);
1423 heap_free(context_vk->retired.objects);
1425 wined3d_shader_descriptor_writes_vk_cleanup(&context_vk->descriptor_writes);
1426 wine_rb_destroy(&context_vk->graphics_pipelines, wined3d_context_vk_destroy_graphics_pipeline, context_vk);
1427 wine_rb_destroy(&context_vk->pipeline_layouts, wined3d_context_vk_destroy_pipeline_layout, context_vk);
1428 wine_rb_destroy(&context_vk->render_passes, wined3d_context_vk_destroy_render_pass, context_vk);
1430 wined3d_context_cleanup(&context_vk->c);
1433 void wined3d_context_vk_remove_pending_queries(struct wined3d_context_vk *context_vk,
1434 struct wined3d_query_vk *query_vk)
1436 struct wined3d_pending_queries_vk *pending = &context_vk->pending_queries;
1437 struct wined3d_pending_query_vk *p;
1438 size_t i;
1440 pending->free_idx = ~(size_t)0;
1441 for (i = pending->count; i; --i)
1443 p = &pending->queries[i - 1];
1445 if (p->query_vk)
1447 if (p->query_vk != query_vk && !wined3d_query_vk_accumulate_data(p->query_vk, context_vk, &p->pool_idx))
1448 continue;
1449 wined3d_query_pool_vk_free_query(p->pool_idx.pool_vk, p->pool_idx.idx);
1450 --p->query_vk->pending_count;
1453 if (i == pending->count)
1455 --pending->count;
1456 continue;
1459 p->query_vk = NULL;
1460 p->pool_idx.pool_vk = NULL;
1461 p->pool_idx.idx = pending->free_idx;
1462 pending->free_idx = i - 1;
1466 void wined3d_context_vk_accumulate_pending_queries(struct wined3d_context_vk *context_vk)
1468 wined3d_context_vk_remove_pending_queries(context_vk, NULL);
1471 void wined3d_context_vk_add_pending_query(struct wined3d_context_vk *context_vk, struct wined3d_query_vk *query_vk)
1473 struct wined3d_pending_queries_vk *pending = &context_vk->pending_queries;
1474 struct wined3d_pending_query_vk *p;
1476 if (pending->free_idx != ~(size_t)0)
1478 p = &pending->queries[pending->free_idx];
1479 pending->free_idx = p->pool_idx.idx;
1481 else
1483 if (!wined3d_array_reserve((void **)&pending->queries, &pending->size,
1484 pending->count + 1, sizeof(*pending->queries)))
1486 ERR("Failed to allocate entry.\n");
1487 return;
1489 p = &pending->queries[pending->count++];
1492 p->query_vk = query_vk;
1493 p->pool_idx = query_vk->pool_idx;
1494 ++query_vk->pending_count;
1497 VkCommandBuffer wined3d_context_vk_get_command_buffer(struct wined3d_context_vk *context_vk)
1499 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1500 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1501 VkCommandBufferAllocateInfo command_buffer_info;
1502 struct wined3d_command_buffer_vk *buffer;
1503 VkCommandBufferBeginInfo begin_info;
1504 struct wined3d_query_vk *query_vk;
1505 VkResult vr;
1507 TRACE("context_vk %p.\n", context_vk);
1509 buffer = &context_vk->current_command_buffer;
1510 if (buffer->vk_command_buffer)
1512 TRACE("Returning existing command buffer %p with id 0x%s.\n",
1513 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1514 return buffer->vk_command_buffer;
1517 command_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1518 command_buffer_info.pNext = NULL;
1519 command_buffer_info.commandPool = context_vk->vk_command_pool;
1520 command_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1521 command_buffer_info.commandBufferCount = 1;
1522 if ((vr = VK_CALL(vkAllocateCommandBuffers(device_vk->vk_device,
1523 &command_buffer_info, &buffer->vk_command_buffer))) < 0)
1525 WARN("Failed to allocate Vulkan command buffer, vr %s.\n", wined3d_debug_vkresult(vr));
1526 return VK_NULL_HANDLE;
1529 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1530 begin_info.pNext = NULL;
1531 begin_info.flags = 0;
1532 begin_info.pInheritanceInfo = NULL;
1533 if ((vr = VK_CALL(vkBeginCommandBuffer(buffer->vk_command_buffer, &begin_info))) < 0)
1535 WARN("Failed to begin command buffer, vr %s.\n", wined3d_debug_vkresult(vr));
1536 VK_CALL(vkFreeCommandBuffers(device_vk->vk_device, context_vk->vk_command_pool,
1537 1, &buffer->vk_command_buffer));
1538 return buffer->vk_command_buffer = VK_NULL_HANDLE;
1541 wined3d_context_vk_accumulate_pending_queries(context_vk);
1542 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->active_queries, struct wined3d_query_vk, entry)
1544 wined3d_query_vk_resume(query_vk, context_vk);
1547 TRACE("Created new command buffer %p with id 0x%s.\n",
1548 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1550 return buffer->vk_command_buffer;
1553 void wined3d_context_vk_submit_command_buffer(struct wined3d_context_vk *context_vk,
1554 unsigned int wait_semaphore_count, const VkSemaphore *wait_semaphores, const VkPipelineStageFlags *wait_stages,
1555 unsigned int signal_semaphore_count, const VkSemaphore *signal_semaphores)
1557 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1558 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1559 struct wined3d_command_buffer_vk *buffer;
1560 struct wined3d_query_vk *query_vk;
1561 VkFenceCreateInfo fence_desc;
1562 VkSubmitInfo submit_info;
1563 VkResult vr;
1565 TRACE("context_vk %p, wait_semaphore_count %u, wait_semaphores %p, wait_stages %p,"
1566 "signal_semaphore_count %u, signal_semaphores %p.\n",
1567 context_vk, wait_semaphore_count, wait_semaphores, wait_stages,
1568 signal_semaphore_count, signal_semaphores);
1570 buffer = &context_vk->current_command_buffer;
1571 if (!buffer->vk_command_buffer)
1572 return;
1574 TRACE("Submitting command buffer %p with id 0x%s.\n",
1575 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1577 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->active_queries, struct wined3d_query_vk, entry)
1579 wined3d_query_vk_suspend(query_vk, context_vk);
1582 wined3d_context_vk_end_current_render_pass(context_vk);
1583 context_vk->graphics.vk_pipeline = VK_NULL_HANDLE;
1584 context_vk->update_compute_pipeline = 1;
1585 context_vk->update_stream_output = 1;
1586 context_vk->c.update_shader_resource_bindings = 1;
1587 context_vk->c.update_compute_shader_resource_bindings = 1;
1588 context_vk->c.update_unordered_access_view_bindings = 1;
1589 context_vk->c.update_compute_unordered_access_view_bindings = 1;
1590 context_invalidate_state(&context_vk->c, STATE_STREAMSRC);
1591 context_invalidate_state(&context_vk->c, STATE_INDEXBUFFER);
1592 context_invalidate_state(&context_vk->c, STATE_BLEND_FACTOR);
1593 context_invalidate_state(&context_vk->c, STATE_STENCIL_REF);
1595 VK_CALL(vkEndCommandBuffer(buffer->vk_command_buffer));
1597 fence_desc.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1598 fence_desc.pNext = NULL;
1599 fence_desc.flags = 0;
1600 if ((vr = VK_CALL(vkCreateFence(device_vk->vk_device, &fence_desc, NULL, &buffer->vk_fence))) < 0)
1601 ERR("Failed to create fence, vr %s.\n", wined3d_debug_vkresult(vr));
1603 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1604 submit_info.pNext = NULL;
1605 submit_info.waitSemaphoreCount = wait_semaphore_count;
1606 submit_info.pWaitSemaphores = wait_semaphores;
1607 submit_info.pWaitDstStageMask = wait_stages;
1608 submit_info.commandBufferCount = 1;
1609 submit_info.pCommandBuffers = &buffer->vk_command_buffer;
1610 submit_info.signalSemaphoreCount = signal_semaphore_count;
1611 submit_info.pSignalSemaphores = signal_semaphores;
1613 if ((vr = VK_CALL(vkQueueSubmit(device_vk->vk_queue, 1, &submit_info, buffer->vk_fence))) < 0)
1614 ERR("Failed to submit command buffer %p, vr %s.\n",
1615 buffer->vk_command_buffer, wined3d_debug_vkresult(vr));
1617 if (!wined3d_array_reserve((void **)&context_vk->submitted.buffers, &context_vk->submitted.buffers_size,
1618 context_vk->submitted.buffer_count + 1, sizeof(*context_vk->submitted.buffers)))
1619 ERR("Failed to grow submitted command buffer array.\n");
1621 context_vk->submitted.buffers[context_vk->submitted.buffer_count++] = *buffer;
1623 buffer->vk_command_buffer = VK_NULL_HANDLE;
1624 /* We don't expect this to ever happen, but handle it anyway. */
1625 if (!++buffer->id)
1627 wined3d_context_vk_wait_command_buffer(context_vk, buffer->id - 1);
1628 context_vk->completed_command_buffer_id = 0;
1629 buffer->id = 1;
1631 wined3d_context_vk_cleanup_resources(context_vk);
1634 void wined3d_context_vk_wait_command_buffer(struct wined3d_context_vk *context_vk, uint64_t id)
1636 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1637 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1638 SIZE_T i;
1640 if (id <= context_vk->completed_command_buffer_id
1641 || id > context_vk->current_command_buffer.id) /* In case the buffer ID wrapped. */
1642 return;
1644 for (i = 0; i < context_vk->submitted.buffer_count; ++i)
1646 if (context_vk->submitted.buffers[i].id != id)
1647 continue;
1649 VK_CALL(vkWaitForFences(device_vk->vk_device, 1,
1650 &context_vk->submitted.buffers[i].vk_fence, VK_TRUE, UINT64_MAX));
1651 wined3d_context_vk_cleanup_resources(context_vk);
1652 return;
1655 ERR("Failed to find fence for command buffer with id 0x%s.\n", wine_dbgstr_longlong(id));
1658 void wined3d_context_vk_image_barrier(struct wined3d_context_vk *context_vk,
1659 VkCommandBuffer vk_command_buffer, VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
1660 VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask, VkImageLayout old_layout,
1661 VkImageLayout new_layout, VkImage image, const VkImageSubresourceRange *range)
1663 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1664 VkImageMemoryBarrier barrier;
1666 wined3d_context_vk_end_current_render_pass(context_vk);
1668 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1669 barrier.pNext = NULL;
1670 barrier.srcAccessMask = src_access_mask;
1671 barrier.dstAccessMask = dst_access_mask;
1672 barrier.oldLayout = old_layout;
1673 barrier.newLayout = new_layout;
1674 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1675 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1676 barrier.image = image;
1677 barrier.subresourceRange = *range;
1679 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, src_stage_mask, dst_stage_mask, 0, 0, NULL, 0, NULL, 1, &barrier));
1682 static int wined3d_render_pass_vk_compare(const void *key, const struct wine_rb_entry *entry)
1684 const struct wined3d_render_pass_key_vk *k = key;
1685 const struct wined3d_render_pass_vk *pass = WINE_RB_ENTRY_VALUE(entry,
1686 const struct wined3d_render_pass_vk, entry);
1688 return memcmp(k, &pass->key, sizeof(*k));
1691 static int wined3d_pipeline_layout_vk_compare(const void *key, const struct wine_rb_entry *entry)
1693 const struct wined3d_pipeline_layout_key_vk *a = key;
1694 const struct wined3d_pipeline_layout_key_vk *b = &WINE_RB_ENTRY_VALUE(entry,
1695 const struct wined3d_pipeline_layout_vk, entry)->key;
1697 if (a->binding_count != b->binding_count)
1698 return a->binding_count - b->binding_count;
1699 return memcmp(a->bindings, b->bindings, a->binding_count * sizeof(*a->bindings));
1702 static int wined3d_graphics_pipeline_vk_compare(const void *key, const struct wine_rb_entry *entry)
1704 const struct wined3d_graphics_pipeline_key_vk *a = key;
1705 const struct wined3d_graphics_pipeline_key_vk *b = &WINE_RB_ENTRY_VALUE(entry,
1706 const struct wined3d_graphics_pipeline_vk, entry)->key;
1707 unsigned int i;
1708 int ret;
1710 if (a->pipeline_desc.stageCount != b->pipeline_desc.stageCount)
1711 return a->pipeline_desc.stageCount - b->pipeline_desc.stageCount;
1712 for (i = 0; i < a->pipeline_desc.stageCount; ++i)
1714 if (a->stages[i].module != b->stages[i].module)
1715 return a->stages[i].module - b->stages[i].module;
1718 if (a->divisor_desc.vertexBindingDivisorCount != b->divisor_desc.vertexBindingDivisorCount)
1719 return a->divisor_desc.vertexBindingDivisorCount - b->divisor_desc.vertexBindingDivisorCount;
1720 if ((ret = memcmp(a->divisors, b->divisors,
1721 a->divisor_desc.vertexBindingDivisorCount * sizeof(*a->divisors))))
1722 return ret;
1724 if (a->input_desc.vertexAttributeDescriptionCount != b->input_desc.vertexAttributeDescriptionCount)
1725 return a->input_desc.vertexAttributeDescriptionCount - b->input_desc.vertexAttributeDescriptionCount;
1726 if ((ret = memcmp(a->attributes, b->attributes,
1727 a->input_desc.vertexAttributeDescriptionCount * sizeof(*a->attributes))))
1728 return ret;
1729 if (a->input_desc.vertexBindingDescriptionCount != b->input_desc.vertexBindingDescriptionCount)
1730 return a->input_desc.vertexBindingDescriptionCount - b->input_desc.vertexBindingDescriptionCount;
1731 if ((ret = memcmp(a->bindings, b->bindings,
1732 a->input_desc.vertexBindingDescriptionCount * sizeof(*a->bindings))))
1733 return ret;
1735 if (a->ia_desc.topology != b->ia_desc.topology)
1736 return a->ia_desc.topology - b->ia_desc.topology;
1737 if (a->ia_desc.primitiveRestartEnable != b->ia_desc.primitiveRestartEnable)
1738 return a->ia_desc.primitiveRestartEnable - b->ia_desc.primitiveRestartEnable;
1740 if (a->ts_desc.patchControlPoints != b->ts_desc.patchControlPoints)
1741 return a->ts_desc.patchControlPoints - b->ts_desc.patchControlPoints;
1743 if ((ret = memcmp(&a->viewport, &b->viewport, sizeof(a->viewport))))
1744 return ret;
1746 if ((ret = memcmp(&a->scissor, &b->scissor, sizeof(a->scissor))))
1747 return ret;
1749 if ((ret = memcmp(&a->rs_desc, &b->rs_desc, sizeof(a->rs_desc))))
1750 return ret;
1752 if (a->ms_desc.rasterizationSamples != b->ms_desc.rasterizationSamples)
1753 return a->ms_desc.rasterizationSamples - b->ms_desc.rasterizationSamples;
1754 if (a->ms_desc.alphaToCoverageEnable != b->ms_desc.alphaToCoverageEnable)
1755 return a->ms_desc.alphaToCoverageEnable - b->ms_desc.alphaToCoverageEnable;
1756 if (a->sample_mask != b->sample_mask)
1757 return a->sample_mask - b->sample_mask;
1759 if ((ret = memcmp(&a->ds_desc, &b->ds_desc, sizeof(a->ds_desc))))
1760 return ret;
1762 if (a->blend_desc.attachmentCount != b->blend_desc.attachmentCount)
1763 return a->blend_desc.attachmentCount - b->blend_desc.attachmentCount;
1764 if ((ret = memcmp(a->blend_attachments, b->blend_attachments,
1765 a->blend_desc.attachmentCount * sizeof(*a->blend_attachments))))
1766 return ret;
1768 if (a->pipeline_desc.layout != b->pipeline_desc.layout)
1769 return a->pipeline_desc.layout - b->pipeline_desc.layout;
1771 if (a->pipeline_desc.renderPass != b->pipeline_desc.renderPass)
1772 return a->pipeline_desc.renderPass - b->pipeline_desc.renderPass;
1774 return 0;
1777 static int wined3d_bo_slab_vk_compare(const void *key, const struct wine_rb_entry *entry)
1779 const struct wined3d_bo_slab_vk *slab = WINE_RB_ENTRY_VALUE(entry, const struct wined3d_bo_slab_vk, entry);
1780 const struct wined3d_bo_slab_vk_key *k = key;
1782 if (k->memory_type != slab->requested_memory_type)
1783 return k->memory_type - slab->requested_memory_type;
1784 if (k->usage != slab->bo.usage)
1785 return k->usage - slab->bo.usage;
1786 return k->size - slab->bo.size;
1789 static void wined3d_context_vk_init_graphics_pipeline_key(struct wined3d_context_vk *context_vk)
1791 struct wined3d_graphics_pipeline_key_vk *key;
1792 VkPipelineShaderStageCreateInfo *stage;
1793 unsigned int i;
1795 static const VkDynamicState dynamic_states[] =
1797 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
1798 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
1801 key = &context_vk->graphics.pipeline_key_vk;
1802 memset(key, 0, sizeof(*key));
1804 for (i = 0; i < ARRAY_SIZE(context_vk->graphics.vk_modules); ++i)
1806 stage = &key->stages[i];
1807 stage->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1808 stage->pName = "main";
1811 key->input_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1812 key->input_desc.pVertexBindingDescriptions = key->bindings;
1813 key->input_desc.pVertexAttributeDescriptions = key->attributes;
1815 key->divisor_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT;
1816 key->divisor_desc.pVertexBindingDivisors = key->divisors;
1818 key->ia_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
1820 key->ts_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO;
1822 key->vp_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
1823 key->vp_desc.viewportCount = 1;
1824 key->vp_desc.pViewports = &key->viewport;
1825 key->vp_desc.scissorCount = 1;
1826 key->vp_desc.pScissors = &key->scissor;
1828 key->rs_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1829 key->rs_desc.lineWidth = 1.0f;
1831 key->ms_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
1832 key->ms_desc.pSampleMask = &key->sample_mask;
1834 key->ds_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
1835 key->ds_desc.maxDepthBounds = 1.0f;
1837 key->blend_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1838 key->blend_desc.logicOp = VK_LOGIC_OP_COPY;
1839 key->blend_desc.pAttachments = key->blend_attachments;
1840 key->blend_desc.blendConstants[0] = 1.0f;
1841 key->blend_desc.blendConstants[1] = 1.0f;
1842 key->blend_desc.blendConstants[2] = 1.0f;
1843 key->blend_desc.blendConstants[3] = 1.0f;
1845 key->dynamic_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1846 key->dynamic_desc.dynamicStateCount = ARRAY_SIZE(dynamic_states);
1847 key->dynamic_desc.pDynamicStates = dynamic_states;
1849 key->pipeline_desc.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1850 key->pipeline_desc.pStages = key->stages;
1851 key->pipeline_desc.pVertexInputState = &key->input_desc;
1852 key->pipeline_desc.pInputAssemblyState = &key->ia_desc;
1853 key->pipeline_desc.pTessellationState = &key->ts_desc;
1854 key->pipeline_desc.pViewportState = &key->vp_desc;
1855 key->pipeline_desc.pRasterizationState = &key->rs_desc;
1856 key->pipeline_desc.pMultisampleState = &key->ms_desc;
1857 key->pipeline_desc.pDepthStencilState = &key->ds_desc;
1858 key->pipeline_desc.pColorBlendState = &key->blend_desc;
1859 key->pipeline_desc.pDynamicState = &key->dynamic_desc;
1860 key->pipeline_desc.basePipelineIndex = -1;
1863 static void wined3d_context_vk_update_rasterisation_state(const struct wined3d_context_vk *context_vk,
1864 const struct wined3d_state *state, struct wined3d_graphics_pipeline_key_vk *key)
1866 const struct wined3d_d3d_info *d3d_info = context_vk->c.d3d_info;
1867 VkPipelineRasterizationStateCreateInfo *desc = &key->rs_desc;
1868 const struct wined3d_rasterizer_state_desc *r;
1869 float scale_bias;
1870 union
1872 uint32_t u32;
1873 float f32;
1874 } const_bias;
1876 if (!state->rasterizer_state)
1878 desc->depthClampEnable = VK_FALSE;
1879 desc->rasterizerDiscardEnable = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
1880 desc->cullMode = VK_CULL_MODE_BACK_BIT;
1881 desc->frontFace = VK_FRONT_FACE_CLOCKWISE;
1882 desc->depthBiasEnable = VK_FALSE;
1883 desc->depthBiasConstantFactor = 0.0f;
1884 desc->depthBiasClamp = 0.0f;
1885 desc->depthBiasSlopeFactor = 0.0f;
1887 return;
1890 r = &state->rasterizer_state->desc;
1891 desc->depthClampEnable = !r->depth_clip;
1892 desc->rasterizerDiscardEnable = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
1893 desc->cullMode = vk_cull_mode_from_wined3d(r->cull_mode);
1894 desc->frontFace = r->front_ccw ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE;
1896 scale_bias = r->scale_bias;
1897 const_bias.f32 = r->depth_bias;
1898 if (!scale_bias && !const_bias.f32)
1900 desc->depthBiasEnable = VK_FALSE;
1901 desc->depthBiasConstantFactor = 0.0f;
1902 desc->depthBiasClamp = 0.0f;
1903 desc->depthBiasSlopeFactor = 0.0f;
1905 return;
1908 desc->depthBiasEnable = VK_TRUE;
1909 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_DEPTH_BIAS)
1911 const struct wined3d_rendertarget_view *dsv;
1913 if ((dsv = state->fb.depth_stencil))
1915 desc->depthBiasConstantFactor = -(float)const_bias.u32 / dsv->format->depth_bias_scale;
1916 desc->depthBiasSlopeFactor = -(float)const_bias.u32;
1918 else
1920 desc->depthBiasConstantFactor = 0.0f;
1921 desc->depthBiasSlopeFactor = 0.0f;
1924 else
1926 desc->depthBiasConstantFactor = const_bias.f32;
1927 desc->depthBiasSlopeFactor = scale_bias;
1929 desc->depthBiasClamp = r->depth_bias_clamp;
1932 static void wined3d_context_vk_update_blend_state(const struct wined3d_context_vk *context_vk,
1933 const struct wined3d_state *state, struct wined3d_graphics_pipeline_key_vk *key)
1935 VkPipelineColorBlendStateCreateInfo *desc = &key->blend_desc;
1936 const struct wined3d_blend_state_desc *b;
1937 unsigned int i;
1939 desc->attachmentCount = context_vk->rt_count;
1941 memset(key->blend_attachments, 0, sizeof(key->blend_attachments));
1942 if (!state->blend_state)
1944 for (i = 0; i < context_vk->rt_count; ++i)
1946 key->blend_attachments[i].colorWriteMask = VK_COLOR_COMPONENT_R_BIT
1947 | VK_COLOR_COMPONENT_G_BIT
1948 | VK_COLOR_COMPONENT_B_BIT
1949 | VK_COLOR_COMPONENT_A_BIT;
1952 return;
1955 b = &state->blend_state->desc;
1956 for (i = 0; i < context_vk->rt_count; ++i)
1958 const struct wined3d_rendertarget_blend_state_desc *rt = &b->rt[b->independent ? i : 0];
1959 const struct wined3d_rendertarget_view *rtv = state->fb.render_targets[i];
1960 VkPipelineColorBlendAttachmentState *a = &key->blend_attachments[i];
1961 enum wined3d_blend src_blend, dst_blend;
1962 const struct wined3d_format *rt_format;
1964 a->colorWriteMask = vk_colour_write_mask_from_wined3d(rt->writemask);
1965 if (!rt->enable)
1966 continue;
1968 if (rtv)
1969 rt_format = rtv->format;
1970 else
1971 rt_format = wined3d_get_format(context_vk->c.device->adapter, WINED3DFMT_NULL, 0);
1972 a->blendEnable = VK_TRUE;
1974 src_blend = rt->src;
1975 dst_blend = rt->dst;
1976 if (src_blend == WINED3D_BLEND_BOTHSRCALPHA)
1978 src_blend = WINED3D_BLEND_SRCALPHA;
1979 dst_blend = WINED3D_BLEND_INVSRCALPHA;
1981 else if (src_blend == WINED3D_BLEND_BOTHINVSRCALPHA)
1983 src_blend = WINED3D_BLEND_INVSRCALPHA;
1984 dst_blend = WINED3D_BLEND_SRCALPHA;
1986 a->srcColorBlendFactor = vk_blend_factor_from_wined3d(src_blend, rt_format, FALSE);
1987 a->dstColorBlendFactor = vk_blend_factor_from_wined3d(dst_blend, rt_format, FALSE);
1988 a->colorBlendOp = vk_blend_op_from_wined3d(rt->op);
1990 src_blend = rt->src_alpha;
1991 dst_blend = rt->dst_alpha;
1992 a->srcAlphaBlendFactor = vk_blend_factor_from_wined3d(src_blend, rt_format, TRUE);
1993 a->dstAlphaBlendFactor = vk_blend_factor_from_wined3d(dst_blend, rt_format, TRUE);
1994 a->alphaBlendOp = vk_blend_op_from_wined3d(rt->op_alpha);
1998 static bool wined3d_context_vk_update_graphics_pipeline_key(struct wined3d_context_vk *context_vk,
1999 const struct wined3d_state *state, VkPipelineLayout vk_pipeline_layout)
2001 unsigned int i, attribute_count, binding_count, divisor_count, stage_count;
2002 const struct wined3d_d3d_info *d3d_info = context_vk->c.d3d_info;
2003 struct wined3d_graphics_pipeline_key_vk *key;
2004 VkPipelineShaderStageCreateInfo *stage;
2005 struct wined3d_stream_info stream_info;
2006 VkPrimitiveTopology vk_topology;
2007 VkShaderModule module;
2008 bool update = false;
2009 uint32_t mask;
2011 key = &context_vk->graphics.pipeline_key_vk;
2013 if (context_vk->c.shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
2015 stage_count = 0;
2016 for (i = 0; i < ARRAY_SIZE(context_vk->graphics.vk_modules); ++i)
2018 if (!(module = context_vk->graphics.vk_modules[i]))
2019 continue;
2021 stage = &key->stages[stage_count++];
2022 stage->stage = vk_shader_stage_from_wined3d(i);
2023 stage->module = module;
2026 key->pipeline_desc.stageCount = stage_count;
2028 update = true;
2031 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_VDECL)
2032 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAMSRC)
2033 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
2035 wined3d_stream_info_from_declaration(&stream_info, state, d3d_info);
2036 divisor_count = 0;
2037 for (i = 0, mask = 0, attribute_count = 0, binding_count = 0; i < ARRAY_SIZE(stream_info.elements); ++i)
2039 VkVertexInputBindingDivisorDescriptionEXT *d;
2040 struct wined3d_stream_info_element *e;
2041 VkVertexInputAttributeDescription *a;
2042 VkVertexInputBindingDescription *b;
2043 uint32_t binding;
2045 if (!(stream_info.use_map & (1u << i)))
2046 continue;
2048 a = &key->attributes[attribute_count++];
2049 e = &stream_info.elements[i];
2050 binding = e->stream_idx;
2052 a->location = i;
2053 a->binding = binding;
2054 a->format = wined3d_format_vk(e->format)->vk_format;
2055 a->offset = (UINT_PTR)e->data.addr - state->streams[binding].offset;
2057 if (mask & (1u << binding))
2058 continue;
2059 mask |= 1u << binding;
2061 b = &key->bindings[binding_count++];
2062 b->binding = binding;
2063 b->stride = e->stride;
2064 b->inputRate = e->instanced ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX;
2066 if (e->instanced)
2068 d = &key->divisors[divisor_count++];
2069 d->binding = binding;
2070 d->divisor = e->divisor;
2074 key->input_desc.pNext = NULL;
2075 key->input_desc.vertexBindingDescriptionCount = binding_count;
2076 key->input_desc.vertexAttributeDescriptionCount = attribute_count;
2078 if (divisor_count)
2080 key->input_desc.pNext = &key->divisor_desc;
2081 key->divisor_desc.vertexBindingDivisorCount = divisor_count;
2084 update = true;
2087 vk_topology = vk_topology_from_wined3d(state->primitive_type);
2088 if (key->ia_desc.topology != vk_topology)
2090 key->ia_desc.topology = vk_topology;
2091 key->ia_desc.primitiveRestartEnable = !(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART)
2092 && !wined3d_primitive_type_is_list(state->primitive_type);
2094 update = true;
2097 if (key->ts_desc.patchControlPoints != state->patch_vertex_count)
2099 key->ts_desc.patchControlPoints = state->patch_vertex_count;
2101 update = true;
2104 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_VIEWPORT)
2105 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SCISSORRECT)
2106 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_RASTERIZER))
2108 key->viewport.x = state->viewports[0].x;
2109 key->viewport.y = state->viewports[0].y;
2110 key->viewport.width = state->viewports[0].width;
2111 key->viewport.height = state->viewports[0].height;
2112 key->viewport.minDepth = state->viewports[0].min_z;
2113 key->viewport.maxDepth = state->viewports[0].max_z;
2115 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
2117 const RECT *r = &state->scissor_rects[0];
2119 key->scissor.offset.x = r->left;
2120 key->scissor.offset.y = r->top;
2121 key->scissor.extent.width = r->right - r->left;
2122 key->scissor.extent.height = r->bottom - r->top;
2124 else
2126 key->scissor.offset.x = key->viewport.x;
2127 key->scissor.offset.y = key->viewport.y;
2128 key->scissor.extent.width = key->viewport.width;
2129 key->scissor.extent.height = key->viewport.height;
2131 key->viewport.y += key->viewport.height;
2132 key->viewport.height = -key->viewport.height;
2134 update = true;
2137 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_RASTERIZER)
2138 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY)))
2140 wined3d_context_vk_update_rasterisation_state(context_vk, state, key);
2142 update = true;
2145 if (key->ms_desc.rasterizationSamples != context_vk->sample_count
2146 || isStateDirty(&context_vk->c, STATE_BLEND) || isStateDirty(&context_vk->c, STATE_SAMPLE_MASK))
2148 key->ms_desc.rasterizationSamples = context_vk->sample_count;
2149 key->ms_desc.alphaToCoverageEnable = state->blend_state && state->blend_state->desc.alpha_to_coverage;
2150 key->sample_mask = state->sample_mask;
2152 update = true;
2155 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_DEPTH_STENCIL)
2156 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2158 const struct wined3d_depth_stencil_state *d = state->depth_stencil_state;
2160 if (d)
2162 key->ds_desc.depthTestEnable = d->desc.depth;
2163 key->ds_desc.depthWriteEnable = d->desc.depth_write;
2164 key->ds_desc.depthCompareOp = vk_compare_op_from_wined3d(d->desc.depth_func);
2165 key->ds_desc.stencilTestEnable = state->fb.depth_stencil && d->desc.stencil;
2166 if (key->ds_desc.stencilTestEnable)
2168 key->ds_desc.front.failOp = vk_stencil_op_from_wined3d(d->desc.front.fail_op);
2169 key->ds_desc.front.passOp = vk_stencil_op_from_wined3d(d->desc.front.pass_op);
2170 key->ds_desc.front.depthFailOp = vk_stencil_op_from_wined3d(d->desc.front.depth_fail_op);
2171 key->ds_desc.front.compareOp = vk_compare_op_from_wined3d(d->desc.front.func);
2172 key->ds_desc.front.compareMask = d->desc.stencil_read_mask;
2173 key->ds_desc.front.writeMask = d->desc.stencil_write_mask;
2175 key->ds_desc.back.failOp = vk_stencil_op_from_wined3d(d->desc.back.fail_op);
2176 key->ds_desc.back.passOp = vk_stencil_op_from_wined3d(d->desc.back.pass_op);
2177 key->ds_desc.back.depthFailOp = vk_stencil_op_from_wined3d(d->desc.back.depth_fail_op);
2178 key->ds_desc.back.compareOp = vk_compare_op_from_wined3d(d->desc.back.func);
2179 key->ds_desc.back.compareMask = d->desc.stencil_read_mask;
2180 key->ds_desc.back.writeMask = d->desc.stencil_write_mask;
2182 else
2184 memset(&key->ds_desc.front, 0, sizeof(key->ds_desc.front));
2185 memset(&key->ds_desc.back, 0, sizeof(key->ds_desc.back));
2188 else
2190 key->ds_desc.depthTestEnable = VK_TRUE;
2191 key->ds_desc.depthWriteEnable = VK_TRUE;
2192 key->ds_desc.depthCompareOp = VK_COMPARE_OP_LESS;
2193 key->ds_desc.stencilTestEnable = VK_FALSE;
2196 update = true;
2199 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_BLEND)
2200 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2202 wined3d_context_vk_update_blend_state(context_vk, state, key);
2204 update = true;
2207 if (key->pipeline_desc.layout != vk_pipeline_layout)
2209 key->pipeline_desc.layout = vk_pipeline_layout;
2211 update = true;
2214 if (key->pipeline_desc.renderPass != context_vk->vk_render_pass)
2216 key->pipeline_desc.renderPass = context_vk->vk_render_pass;
2218 update = true;
2221 return update;
2224 static bool wined3d_context_vk_begin_render_pass(struct wined3d_context_vk *context_vk,
2225 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2227 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2228 VkImageView vk_views[WINED3D_MAX_RENDER_TARGETS + 1];
2229 unsigned int fb_width, fb_height, fb_layer_count;
2230 struct wined3d_rendertarget_view_vk *rtv_vk;
2231 struct wined3d_rendertarget_view *view;
2232 const VkPhysicalDeviceLimits *limits;
2233 VkRenderPassBeginInfo begin_info;
2234 unsigned int attachment_count, i;
2235 VkFramebufferCreateInfo fb_desc;
2236 VkResult vr;
2238 if (context_vk->vk_render_pass)
2239 return true;
2241 limits = &wined3d_adapter_vk(device_vk->d.adapter)->device_limits;
2242 fb_width = limits->maxFramebufferWidth;
2243 fb_height = limits->maxFramebufferHeight;
2244 fb_layer_count = limits->maxFramebufferLayers;
2245 attachment_count = 0;
2247 context_vk->rt_count = 0;
2248 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
2250 if (!(view = state->fb.render_targets[i]) || view->format->id == WINED3DFMT_NULL)
2251 continue;
2253 rtv_vk = wined3d_rendertarget_view_vk(view);
2254 vk_views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
2255 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
2256 wined3d_context_vk_reference_rendertarget_view(context_vk, rtv_vk);
2258 if (view->width < fb_width)
2259 fb_width = view->width;
2260 if (view->height < fb_height)
2261 fb_height = view->height;
2262 if (view->layer_count < fb_layer_count)
2263 fb_layer_count = view->layer_count;
2264 context_vk->rt_count = i + 1;
2265 ++attachment_count;
2268 if ((view = state->fb.depth_stencil))
2270 rtv_vk = wined3d_rendertarget_view_vk(view);
2271 vk_views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
2272 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
2273 wined3d_context_vk_reference_rendertarget_view(context_vk, rtv_vk);
2275 if (view->width < fb_width)
2276 fb_width = view->width;
2277 if (view->height < fb_height)
2278 fb_height = view->height;
2279 if (view->layer_count < fb_layer_count)
2280 fb_layer_count = view->layer_count;
2281 ++attachment_count;
2284 if (!(context_vk->vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, &state->fb,
2285 ARRAY_SIZE(state->fb.render_targets), !!state->fb.depth_stencil, 0)))
2287 ERR("Failed to get render pass.\n");
2288 return false;
2291 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
2292 fb_desc.pNext = NULL;
2293 fb_desc.flags = 0;
2294 fb_desc.renderPass = context_vk->vk_render_pass;
2295 fb_desc.attachmentCount = attachment_count;
2296 fb_desc.pAttachments = vk_views;
2297 fb_desc.width = fb_width;
2298 fb_desc.height = fb_height;
2299 fb_desc.layers = fb_layer_count;
2301 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &context_vk->vk_framebuffer))) < 0)
2303 WARN("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
2304 return false;
2307 begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
2308 begin_info.pNext = NULL;
2309 begin_info.renderPass = context_vk->vk_render_pass;
2310 begin_info.framebuffer = context_vk->vk_framebuffer;
2311 begin_info.renderArea.offset.x = 0;
2312 begin_info.renderArea.offset.y = 0;
2313 begin_info.renderArea.extent.width = fb_width;
2314 begin_info.renderArea.extent.height = fb_height;
2315 begin_info.clearValueCount = 0;
2316 begin_info.pClearValues = NULL;
2317 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_info, VK_SUBPASS_CONTENTS_INLINE));
2319 return true;
2322 static void wined3d_context_vk_bind_vertex_buffers(struct wined3d_context_vk *context_vk,
2323 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2325 VkDeviceSize offsets[ARRAY_SIZE(state->streams)] = {0};
2326 VkBuffer buffers[ARRAY_SIZE(state->streams)];
2327 const struct wined3d_stream_state *stream;
2328 const VkDescriptorBufferInfo *buffer_info;
2329 struct wined3d_buffer_vk *buffer_vk;
2330 struct wined3d_buffer *buffer;
2331 unsigned int i, first, count;
2333 first = 0;
2334 count = 0;
2335 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
2337 stream = &state->streams[i];
2339 if ((buffer = stream->buffer))
2341 buffer_vk = wined3d_buffer_vk(buffer);
2342 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2343 wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
2344 buffers[count] = buffer_info->buffer;
2345 offsets[count] = buffer_info->offset + stream->offset;
2346 ++count;
2347 continue;
2350 if (count)
2351 VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, first, count, buffers, offsets));
2352 first = i + 1;
2353 count = 0;
2356 if (count)
2357 VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, first, count, buffers, offsets));
2360 static void wined3d_context_vk_bind_stream_output_buffers(struct wined3d_context_vk *context_vk,
2361 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2363 VkDeviceSize offsets[ARRAY_SIZE(state->stream_output)];
2364 VkDeviceSize sizes[ARRAY_SIZE(state->stream_output)];
2365 VkBuffer buffers[ARRAY_SIZE(state->stream_output)];
2366 const struct wined3d_stream_output *stream;
2367 const VkDescriptorBufferInfo *buffer_info;
2368 struct wined3d_buffer_vk *buffer_vk;
2369 struct wined3d_buffer *buffer;
2370 unsigned int i, first, count;
2372 first = 0;
2373 count = 0;
2374 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
2376 stream = &state->stream_output[i];
2378 if ((buffer = stream->buffer))
2380 buffer_vk = wined3d_buffer_vk(buffer);
2381 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2382 wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
2383 buffers[count] = buffer_info->buffer;
2384 if ((offsets[count] = stream->offset) == ~0u)
2386 FIXME("Appending to stream output buffers not implemented.\n");
2387 offsets[count] = 0;
2389 sizes[count] = buffer_info->range - offsets[count];
2390 offsets[count] += buffer_info->offset;
2391 ++count;
2392 continue;
2395 if (count)
2396 VK_CALL(vkCmdBindTransformFeedbackBuffersEXT(vk_command_buffer, first, count, buffers, offsets, sizes));
2397 first = i + 1;
2398 count = 0;
2401 if (count)
2402 VK_CALL(vkCmdBindTransformFeedbackBuffersEXT(vk_command_buffer, first, count, buffers, offsets, sizes));
2405 static VkResult wined3d_context_vk_create_vk_descriptor_pool(struct wined3d_device_vk *device_vk,
2406 const struct wined3d_vk_info *vk_info, VkDescriptorPool *vk_pool)
2408 struct VkDescriptorPoolCreateInfo pool_desc;
2409 VkResult vr;
2411 static const VkDescriptorPoolSize pool_sizes[] =
2413 {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1024},
2414 {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1024},
2415 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024},
2416 {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1024},
2417 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024},
2418 {VK_DESCRIPTOR_TYPE_SAMPLER, 1024},
2421 pool_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
2422 pool_desc.pNext = NULL;
2423 pool_desc.flags = 0;
2424 pool_desc.maxSets = 512;
2425 pool_desc.poolSizeCount = ARRAY_SIZE(pool_sizes);
2426 pool_desc.pPoolSizes = pool_sizes;
2428 if ((vr = VK_CALL(vkCreateDescriptorPool(device_vk->vk_device, &pool_desc, NULL, vk_pool))) < 0)
2429 ERR("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
2431 return vr;
2434 static VkResult wined3d_context_vk_create_vk_descriptor_set(struct wined3d_context_vk *context_vk,
2435 VkDescriptorSetLayout vk_set_layout, VkDescriptorSet *vk_descriptor_set)
2437 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2438 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2439 struct VkDescriptorSetAllocateInfo set_desc;
2440 VkResult vr;
2442 if (!context_vk->vk_descriptor_pool && (vr = wined3d_context_vk_create_vk_descriptor_pool(device_vk,
2443 vk_info, &context_vk->vk_descriptor_pool)))
2445 WARN("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
2446 return vr;
2449 set_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
2450 set_desc.pNext = NULL;
2451 set_desc.descriptorPool = context_vk->vk_descriptor_pool;
2452 set_desc.descriptorSetCount = 1;
2453 set_desc.pSetLayouts = &vk_set_layout;
2454 if ((vr = VK_CALL(vkAllocateDescriptorSets(device_vk->vk_device, &set_desc, vk_descriptor_set))) >= 0)
2455 return vr;
2457 if (vr == VK_ERROR_FRAGMENTED_POOL || vr == VK_ERROR_OUT_OF_POOL_MEMORY)
2459 wined3d_context_vk_destroy_vk_descriptor_pool(context_vk,
2460 context_vk->vk_descriptor_pool, context_vk->current_command_buffer.id);
2461 context_vk->vk_descriptor_pool = VK_NULL_HANDLE;
2462 if ((vr = wined3d_context_vk_create_vk_descriptor_pool(device_vk, vk_info, &context_vk->vk_descriptor_pool)))
2464 WARN("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
2465 return vr;
2468 set_desc.descriptorPool = context_vk->vk_descriptor_pool;
2469 if ((vr = VK_CALL(vkAllocateDescriptorSets(device_vk->vk_device, &set_desc, vk_descriptor_set))) >= 0)
2470 return vr;
2473 WARN("Failed to allocate descriptor set, vr %s.\n", wined3d_debug_vkresult(vr));
2475 return vr;
2478 static bool wined3d_shader_descriptor_writes_vk_add_write(struct wined3d_shader_descriptor_writes_vk *writes,
2479 VkDescriptorSet vk_descriptor_set, size_t binding_idx, VkDescriptorType type,
2480 const VkDescriptorBufferInfo *buffer_info, const VkDescriptorImageInfo *image_info,
2481 const VkBufferView *buffer_view)
2483 SIZE_T write_count = writes->count;
2484 VkWriteDescriptorSet *write;
2486 if (!wined3d_array_reserve((void **)&writes->writes, &writes->size,
2487 write_count + 1, sizeof(*writes->writes)))
2488 return false;
2490 write = &writes->writes[write_count];
2491 write->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
2492 write->pNext = NULL;
2493 write->dstSet = vk_descriptor_set;
2494 write->dstBinding = binding_idx;
2495 write->dstArrayElement = 0;
2496 write->descriptorCount = 1;
2497 write->descriptorType = type;
2498 write->pImageInfo = image_info;
2499 write->pBufferInfo = buffer_info;
2500 write->pTexelBufferView = buffer_view;
2502 ++writes->count;
2504 return true;
2507 static bool wined3d_shader_resource_bindings_add_null_srv_binding(struct wined3d_shader_descriptor_writes_vk *writes,
2508 VkDescriptorSet vk_descriptor_set, size_t binding_idx, enum wined3d_shader_resource_type type,
2509 enum wined3d_data_type data_type, struct wined3d_context_vk *context_vk)
2511 const struct wined3d_null_views_vk *v = &wined3d_device_vk(context_vk->c.device)->null_views_vk;
2513 switch (type)
2515 case WINED3D_SHADER_RESOURCE_BUFFER:
2516 if (data_type == WINED3D_DATA_FLOAT)
2517 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding_idx,
2518 VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, NULL, NULL, &v->vk_view_buffer_float);
2519 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding_idx,
2520 VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, NULL, NULL, &v->vk_view_buffer_uint);
2522 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2523 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2524 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_1d, NULL);
2526 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2527 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2528 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2d, NULL);
2530 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2531 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2532 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2dms, NULL);
2534 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2535 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2536 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_3d, NULL);
2538 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2539 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2540 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_cube, NULL);
2542 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2543 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2544 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2d_array, NULL);
2546 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2547 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2548 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2dms_array, NULL);
2550 default:
2551 FIXME("Unhandled resource type %#x.\n", type);
2552 return false;
2556 static bool wined3d_context_vk_update_descriptors(struct wined3d_context_vk *context_vk,
2557 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, enum wined3d_pipeline pipeline)
2559 struct wined3d_shader_descriptor_writes_vk *writes = &context_vk->descriptor_writes;
2560 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2561 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2562 const struct wined3d_shader_resource_binding *binding;
2563 struct wined3d_shader_resource_bindings *bindings;
2564 struct wined3d_unordered_access_view_vk *uav_vk;
2565 struct wined3d_shader_resource_view_vk *srv_vk;
2566 struct wined3d_unordered_access_view *uav;
2567 const VkDescriptorBufferInfo *buffer_info;
2568 struct wined3d_shader_resource_view *srv;
2569 const VkDescriptorImageInfo *image_info;
2570 struct wined3d_buffer_vk *buffer_vk;
2571 VkDescriptorSetLayout vk_set_layout;
2572 VkPipelineLayout vk_pipeline_layout;
2573 struct wined3d_resource *resource;
2574 VkPipelineBindPoint vk_bind_point;
2575 VkDescriptorSet vk_descriptor_set;
2576 struct wined3d_view_vk *view_vk;
2577 struct wined3d_sampler *sampler;
2578 struct wined3d_buffer *buffer;
2579 VkBufferView *buffer_view;
2580 VkDescriptorType type;
2581 VkResult vr;
2582 size_t i;
2584 switch (pipeline)
2586 case WINED3D_PIPELINE_GRAPHICS:
2587 bindings = &context_vk->graphics.bindings;
2588 vk_bind_point = VK_PIPELINE_BIND_POINT_GRAPHICS;
2589 vk_set_layout = context_vk->graphics.vk_set_layout;
2590 vk_pipeline_layout = context_vk->graphics.vk_pipeline_layout;
2591 break;
2593 case WINED3D_PIPELINE_COMPUTE:
2594 bindings = &context_vk->compute.bindings;
2595 vk_bind_point = VK_PIPELINE_BIND_POINT_COMPUTE;
2596 vk_set_layout = context_vk->compute.vk_set_layout;
2597 vk_pipeline_layout = context_vk->compute.vk_pipeline_layout;
2598 break;
2600 default:
2601 ERR("Invalid pipeline %#x.\n", pipeline);
2602 return false;
2605 if ((vr = wined3d_context_vk_create_vk_descriptor_set(context_vk, vk_set_layout, &vk_descriptor_set)))
2607 WARN("Failed to create descriptor set, vr %s.\n", wined3d_debug_vkresult(vr));
2608 return false;
2611 writes->count = 0;
2612 for (i = 0; i < bindings->count; ++i)
2614 binding = &bindings->bindings[i];
2616 switch (binding->shader_descriptor_type)
2618 case WINED3D_SHADER_DESCRIPTOR_TYPE_CBV:
2619 if (!(buffer = state->cb[binding->shader_type][binding->resource_idx]))
2621 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2622 binding->binding_idx, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2623 &device_vk->null_resources_vk.buffer_info, NULL, NULL))
2624 return false;
2625 break;
2627 buffer_vk = wined3d_buffer_vk(buffer);
2628 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2629 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2630 binding->binding_idx, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info, NULL, NULL))
2631 return false;
2632 wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
2633 break;
2635 case WINED3D_SHADER_DESCRIPTOR_TYPE_SRV:
2636 if (!(srv = state->shader_resource_view[binding->shader_type][binding->resource_idx]))
2638 if (!wined3d_shader_resource_bindings_add_null_srv_binding(writes, vk_descriptor_set,
2639 binding->binding_idx, binding->resource_type, binding->resource_data_type, context_vk))
2640 return false;
2641 break;
2643 resource = srv->resource;
2645 srv_vk = wined3d_shader_resource_view_vk(srv);
2646 view_vk = &srv_vk->view_vk;
2647 if (resource->type == WINED3D_RTYPE_BUFFER)
2649 image_info = NULL;
2650 buffer_view = &view_vk->u.vk_buffer_view;
2651 type = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
2653 else
2655 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture_from_resource(resource));
2657 if (view_vk->u.vk_image_info.imageView)
2658 image_info = &view_vk->u.vk_image_info;
2659 else
2660 image_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk);
2661 buffer_view = NULL;
2662 type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
2665 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2666 binding->binding_idx, type, NULL, image_info, buffer_view))
2667 return false;
2668 wined3d_context_vk_reference_shader_resource_view(context_vk, srv_vk);
2669 break;
2671 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV:
2672 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2674 FIXME("NULL unordered access views not implemented.\n");
2675 return false;
2677 resource = uav->resource;
2679 uav_vk = wined3d_unordered_access_view_vk(uav);
2680 view_vk = &uav_vk->view_vk;
2681 if (resource->type == WINED3D_RTYPE_BUFFER)
2683 image_info = NULL;
2684 buffer_view = &view_vk->u.vk_buffer_view;
2685 type = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
2687 else
2689 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture_from_resource(resource));
2691 if (view_vk->u.vk_image_info.imageView)
2692 image_info = &view_vk->u.vk_image_info;
2693 else
2694 image_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk);
2695 buffer_view = NULL;
2696 type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
2699 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2700 binding->binding_idx, type, NULL, image_info, buffer_view))
2701 return false;
2702 wined3d_context_vk_reference_unordered_access_view(context_vk, uav_vk);
2703 break;
2705 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV_COUNTER:
2706 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2708 FIXME("NULL unordered access view counters not implemented.\n");
2709 return false;
2712 uav_vk = wined3d_unordered_access_view_vk(uav);
2713 if (!uav_vk->vk_counter_view || !wined3d_shader_descriptor_writes_vk_add_write(writes,
2714 vk_descriptor_set, binding->binding_idx, VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,
2715 NULL, NULL, &uav_vk->vk_counter_view))
2716 return false;
2717 break;
2719 case WINED3D_SHADER_DESCRIPTOR_TYPE_SAMPLER:
2720 if (!(sampler = state->sampler[binding->shader_type][binding->resource_idx]))
2721 sampler = context_vk->c.device->null_sampler;
2722 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding->binding_idx,
2723 VK_DESCRIPTOR_TYPE_SAMPLER, NULL, &wined3d_sampler_vk(sampler)->vk_image_info, NULL))
2724 return false;
2725 wined3d_context_vk_reference_sampler(context_vk, wined3d_sampler_vk(sampler));
2726 break;
2728 default:
2729 ERR("Invalid descriptor type %#x.\n", binding->shader_descriptor_type);
2730 return false;
2734 VK_CALL(vkUpdateDescriptorSets(device_vk->vk_device, writes->count, writes->writes, 0, NULL));
2735 VK_CALL(vkCmdBindDescriptorSets(vk_command_buffer, vk_bind_point,
2736 vk_pipeline_layout, 0, 1, &vk_descriptor_set, 0, NULL));
2738 return true;
2741 static VkResult wined3d_context_vk_create_vk_descriptor_set_layout(struct wined3d_device_vk *device_vk,
2742 const struct wined3d_vk_info *vk_info, const struct wined3d_pipeline_layout_key_vk *key,
2743 VkDescriptorSetLayout *vk_set_layout)
2745 VkDescriptorSetLayoutCreateInfo layout_desc;
2746 VkResult vr;
2748 layout_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
2749 layout_desc.pNext = NULL;
2750 layout_desc.flags = 0;
2751 layout_desc.bindingCount = key->binding_count;
2752 layout_desc.pBindings = key->bindings;
2754 if ((vr = VK_CALL(vkCreateDescriptorSetLayout(device_vk->vk_device, &layout_desc, NULL, vk_set_layout))) < 0)
2755 WARN("Failed to create Vulkan descriptor set layout, vr %s.\n", wined3d_debug_vkresult(vr));
2757 return vr;
2760 struct wined3d_pipeline_layout_vk *wined3d_context_vk_get_pipeline_layout(
2761 struct wined3d_context_vk *context_vk, VkDescriptorSetLayoutBinding *bindings, SIZE_T binding_count)
2763 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2764 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2765 struct wined3d_pipeline_layout_key_vk key;
2766 struct wined3d_pipeline_layout_vk *layout;
2767 VkPipelineLayoutCreateInfo layout_desc;
2768 struct wine_rb_entry *entry;
2769 VkResult vr;
2771 key.bindings = bindings;
2772 key.binding_count = binding_count;
2773 if ((entry = wine_rb_get(&context_vk->pipeline_layouts, &key)))
2774 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_pipeline_layout_vk, entry);
2776 if (!(layout = heap_alloc(sizeof(*layout))))
2777 return NULL;
2779 if (!(layout->key.bindings = heap_alloc(sizeof(*layout->key.bindings) * key.binding_count)))
2781 heap_free(layout);
2782 return NULL;
2784 memcpy(layout->key.bindings, key.bindings, sizeof(*layout->key.bindings) * key.binding_count);
2785 layout->key.binding_count = key.binding_count;
2787 if ((vr = wined3d_context_vk_create_vk_descriptor_set_layout(device_vk, vk_info, &key, &layout->vk_set_layout)))
2789 WARN("Failed to create descriptor set layout, vr %s.\n", wined3d_debug_vkresult(vr));
2790 goto fail;
2793 layout_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
2794 layout_desc.pNext = NULL;
2795 layout_desc.flags = 0;
2796 layout_desc.setLayoutCount = 1;
2797 layout_desc.pSetLayouts = &layout->vk_set_layout;
2798 layout_desc.pushConstantRangeCount = 0;
2799 layout_desc.pPushConstantRanges = NULL;
2801 if ((vr = VK_CALL(vkCreatePipelineLayout(device_vk->vk_device,
2802 &layout_desc, NULL, &layout->vk_pipeline_layout))) < 0)
2804 WARN("Failed to create Vulkan pipeline layout, vr %s.\n", wined3d_debug_vkresult(vr));
2805 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
2806 goto fail;
2809 if (wine_rb_put(&context_vk->pipeline_layouts, &layout->key, &layout->entry) == -1)
2811 ERR("Failed to insert pipeline layout.\n");
2812 VK_CALL(vkDestroyPipelineLayout(device_vk->vk_device, layout->vk_pipeline_layout, NULL));
2813 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
2814 goto fail;
2817 return layout;
2819 fail:
2820 heap_free(layout->key.bindings);
2821 heap_free(layout);
2822 return NULL;
2825 static VkPipeline wined3d_context_vk_get_graphics_pipeline(struct wined3d_context_vk *context_vk)
2827 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2828 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2829 struct wined3d_graphics_pipeline_vk *pipeline_vk;
2830 struct wined3d_graphics_pipeline_key_vk *key;
2831 struct wine_rb_entry *entry;
2832 VkResult vr;
2834 key = &context_vk->graphics.pipeline_key_vk;
2835 if ((entry = wine_rb_get(&context_vk->graphics_pipelines, key)))
2836 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_graphics_pipeline_vk, entry)->vk_pipeline;
2838 if (!(pipeline_vk = heap_alloc(sizeof(*pipeline_vk))))
2839 return VK_NULL_HANDLE;
2840 pipeline_vk->key = *key;
2842 if ((vr = VK_CALL(vkCreateGraphicsPipelines(device_vk->vk_device,
2843 VK_NULL_HANDLE, 1, &key->pipeline_desc, NULL, &pipeline_vk->vk_pipeline))) < 0)
2845 WARN("Failed to create graphics pipeline, vr %s.\n", wined3d_debug_vkresult(vr));
2846 heap_free(pipeline_vk);
2847 return VK_NULL_HANDLE;
2850 if (wine_rb_put(&context_vk->graphics_pipelines, &pipeline_vk->key, &pipeline_vk->entry) == -1)
2851 ERR("Failed to insert pipeline.\n");
2853 return pipeline_vk->vk_pipeline;
2856 static void wined3d_context_vk_load_shader_resources(struct wined3d_context_vk *context_vk,
2857 const struct wined3d_state *state, enum wined3d_pipeline pipeline)
2859 struct wined3d_shader_descriptor_writes_vk *writes = &context_vk->descriptor_writes;
2860 const struct wined3d_shader_resource_bindings *bindings;
2861 const struct wined3d_shader_resource_binding *binding;
2862 struct wined3d_unordered_access_view_vk *uav_vk;
2863 struct wined3d_shader_resource_view_vk *srv_vk;
2864 struct wined3d_unordered_access_view *uav;
2865 struct wined3d_shader_resource_view *srv;
2866 struct wined3d_buffer_vk *buffer_vk;
2867 struct wined3d_sampler *sampler;
2868 struct wined3d_buffer *buffer;
2869 size_t i;
2871 switch (pipeline)
2873 case WINED3D_PIPELINE_GRAPHICS:
2874 bindings = &context_vk->graphics.bindings;
2875 break;
2877 case WINED3D_PIPELINE_COMPUTE:
2878 bindings = &context_vk->compute.bindings;
2879 break;
2881 default:
2882 ERR("Invalid pipeline %#x.\n", pipeline);
2883 return;
2886 writes->count = 0;
2887 for (i = 0; i < bindings->count; ++i)
2889 binding = &bindings->bindings[i];
2891 switch (binding->shader_descriptor_type)
2893 case WINED3D_SHADER_DESCRIPTOR_TYPE_CBV:
2894 if (!(buffer = state->cb[binding->shader_type][binding->resource_idx]))
2895 break;
2897 buffer_vk = wined3d_buffer_vk(buffer);
2898 wined3d_buffer_load(buffer, &context_vk->c, state);
2899 if (!buffer_vk->bo_user.valid)
2901 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
2902 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_CONSTANT_BUFFER(binding->shader_type));
2903 else
2904 context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_CONSTANT_BUFFER);
2906 wined3d_buffer_vk_barrier(buffer_vk, context_vk, WINED3D_BIND_CONSTANT_BUFFER);
2907 break;
2909 case WINED3D_SHADER_DESCRIPTOR_TYPE_SRV:
2910 if (!(srv = state->shader_resource_view[binding->shader_type][binding->resource_idx]))
2911 break;
2913 srv_vk = wined3d_shader_resource_view_vk(srv);
2914 if (srv->resource->type == WINED3D_RTYPE_BUFFER)
2916 if (!srv_vk->view_vk.bo_user.valid)
2918 wined3d_shader_resource_view_vk_update(srv_vk, context_vk);
2919 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
2920 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
2921 else
2922 context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
2924 wined3d_buffer_load(buffer_from_resource(srv->resource), &context_vk->c, state);
2926 else
2928 wined3d_texture_load(texture_from_resource(srv->resource), &context_vk->c, FALSE);
2930 wined3d_shader_resource_view_vk_barrier(srv_vk, context_vk, WINED3D_BIND_SHADER_RESOURCE);
2931 break;
2933 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV:
2934 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2935 break;
2937 uav_vk = wined3d_unordered_access_view_vk(uav);
2938 if (uav->resource->type == WINED3D_RTYPE_BUFFER)
2940 if (!uav_vk->view_vk.bo_user.valid)
2942 wined3d_unordered_access_view_vk_update(uav_vk, context_vk);
2943 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
2944 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING);
2945 else
2946 context_invalidate_compute_state(&context_vk->c,
2947 STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING);
2949 wined3d_buffer_load(buffer_from_resource(uav->resource), &context_vk->c, state);
2950 wined3d_unordered_access_view_invalidate_location(uav, ~WINED3D_LOCATION_BUFFER);
2952 else
2954 wined3d_texture_load(texture_from_resource(uav->resource), &context_vk->c, FALSE);
2955 wined3d_unordered_access_view_invalidate_location(uav, ~WINED3D_LOCATION_TEXTURE_RGB);
2957 wined3d_unordered_access_view_vk_barrier(uav_vk, context_vk, WINED3D_BIND_UNORDERED_ACCESS);
2958 break;
2960 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV_COUNTER:
2961 break;
2963 case WINED3D_SHADER_DESCRIPTOR_TYPE_SAMPLER:
2964 if (!(sampler = state->sampler[binding->shader_type][binding->resource_idx]))
2965 sampler = context_vk->c.device->null_sampler;
2966 break;
2968 default:
2969 ERR("Invalid descriptor type %#x.\n", binding->shader_descriptor_type);
2970 break;
2975 VkCommandBuffer wined3d_context_vk_apply_draw_state(struct wined3d_context_vk *context_vk,
2976 const struct wined3d_state *state, struct wined3d_buffer_vk *indirect_vk, bool indexed)
2978 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2979 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2980 struct wined3d_rendertarget_view *dsv;
2981 struct wined3d_buffer_vk *buffer_vk;
2982 VkSampleCountFlagBits sample_count;
2983 VkCommandBuffer vk_command_buffer;
2984 struct wined3d_buffer *buffer;
2985 unsigned int i;
2987 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL))
2988 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2989 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL);
2990 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
2991 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_VERTEX);
2992 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY)))
2993 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_GEOMETRY);
2994 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_HULL)))
2995 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_HULL) | (1u << WINED3D_SHADER_TYPE_DOMAIN);
2996 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN)))
2997 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_DOMAIN);
2999 context_vk->sample_count = 0;
3000 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
3002 struct wined3d_rendertarget_view *rtv;
3004 if (!(rtv = state->fb.render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
3005 continue;
3007 if (wined3d_blend_state_get_writemask(state->blend_state, i))
3009 wined3d_rendertarget_view_load_location(rtv, &context_vk->c, rtv->resource->draw_binding);
3010 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
3012 else
3014 wined3d_rendertarget_view_prepare_location(rtv, &context_vk->c, rtv->resource->draw_binding);
3017 sample_count = max(1, wined3d_resource_get_sample_count(rtv->resource));
3018 if (!context_vk->sample_count)
3019 context_vk->sample_count = sample_count;
3020 else if (context_vk->sample_count != sample_count)
3021 FIXME("Inconsistent sample counts (%u != %u).\n", context_vk->sample_count, sample_count);
3024 if ((dsv = state->fb.depth_stencil))
3026 if (wined3d_state_uses_depth_buffer(state))
3027 wined3d_rendertarget_view_load_location(dsv, &context_vk->c, dsv->resource->draw_binding);
3028 else
3029 wined3d_rendertarget_view_prepare_location(dsv, &context_vk->c, dsv->resource->draw_binding);
3030 if (!state->depth_stencil_state || state->depth_stencil_state->desc.depth_write)
3031 wined3d_rendertarget_view_invalidate_location(dsv, ~dsv->resource->draw_binding);
3033 sample_count = max(1, wined3d_resource_get_sample_count(dsv->resource));
3034 if (!context_vk->sample_count)
3035 context_vk->sample_count = sample_count;
3036 else if (context_vk->sample_count != sample_count)
3037 FIXME("Inconsistent sample counts (%u != %u).\n", context_vk->sample_count, sample_count);
3040 if (!context_vk->sample_count)
3041 context_vk->sample_count = VK_SAMPLE_COUNT_1_BIT;
3042 if (context_vk->c.shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
3044 device_vk->d.shader_backend->shader_select(device_vk->d.shader_priv, &context_vk->c, state);
3045 if (!context_vk->graphics.vk_pipeline_layout)
3047 ERR("No pipeline layout set.\n");
3048 return VK_NULL_HANDLE;
3050 context_vk->c.update_shader_resource_bindings = 1;
3051 context_vk->c.update_unordered_access_view_bindings = 1;
3054 wined3d_context_vk_load_shader_resources(context_vk, state, WINED3D_PIPELINE_GRAPHICS);
3056 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
3058 if (!(buffer = state->streams[i].buffer))
3059 continue;
3061 buffer_vk = wined3d_buffer_vk(buffer);
3062 wined3d_buffer_load(&buffer_vk->b, &context_vk->c, state);
3063 wined3d_buffer_vk_barrier(buffer_vk, context_vk, WINED3D_BIND_VERTEX_BUFFER);
3064 if (!buffer_vk->bo_user.valid)
3065 context_invalidate_state(&context_vk->c, STATE_STREAMSRC);
3068 if (use_transform_feedback(state) && vk_info->supported[WINED3D_VK_EXT_TRANSFORM_FEEDBACK])
3070 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3072 if (!(buffer = state->stream_output[i].buffer))
3073 continue;
3075 buffer_vk = wined3d_buffer_vk(buffer);
3076 wined3d_buffer_load(&buffer_vk->b, &context_vk->c, state);
3077 wined3d_buffer_vk_barrier(buffer_vk, context_vk, WINED3D_BIND_STREAM_OUTPUT);
3078 wined3d_buffer_invalidate_location(&buffer_vk->b, ~WINED3D_LOCATION_BUFFER);
3079 if (!buffer_vk->bo_user.valid)
3080 context_vk->update_stream_output = 1;
3082 context_vk->c.transform_feedback_active = 1;
3085 if (indexed || (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_INDEXBUFFER) && state->index_buffer))
3087 buffer_vk = wined3d_buffer_vk(state->index_buffer);
3088 wined3d_buffer_load(&buffer_vk->b, &context_vk->c, state);
3089 wined3d_buffer_vk_barrier(buffer_vk, context_vk, WINED3D_BIND_INDEX_BUFFER);
3090 if (!buffer_vk->bo_user.valid)
3091 context_invalidate_state(&context_vk->c, STATE_INDEXBUFFER);
3094 if (indirect_vk)
3096 wined3d_buffer_load(&indirect_vk->b, &context_vk->c, state);
3097 wined3d_buffer_vk_barrier(indirect_vk, context_vk, WINED3D_BIND_INDIRECT_BUFFER);
3100 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
3102 ERR("Failed to get command buffer.\n");
3103 return VK_NULL_HANDLE;
3106 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
3107 wined3d_context_vk_end_current_render_pass(context_vk);
3108 if (!wined3d_context_vk_begin_render_pass(context_vk, vk_command_buffer, state, vk_info))
3110 ERR("Failed to begin render pass.\n");
3111 return VK_NULL_HANDLE;
3114 if (wined3d_context_vk_update_graphics_pipeline_key(context_vk, state, context_vk->graphics.vk_pipeline_layout)
3115 || !context_vk->graphics.vk_pipeline)
3117 if (!(context_vk->graphics.vk_pipeline = wined3d_context_vk_get_graphics_pipeline(context_vk)))
3119 ERR("Failed to get graphics pipeline.\n");
3120 return VK_NULL_HANDLE;
3123 VK_CALL(vkCmdBindPipeline(vk_command_buffer,
3124 VK_PIPELINE_BIND_POINT_GRAPHICS, context_vk->graphics.vk_pipeline));
3127 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STENCIL_REF) && dsv)
3129 VK_CALL(vkCmdSetStencilReference(vk_command_buffer, VK_STENCIL_FACE_FRONT_AND_BACK,
3130 state->stencil_ref & ((1 << dsv->format->stencil_size) - 1)));
3133 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAMSRC))
3134 wined3d_context_vk_bind_vertex_buffers(context_vk, vk_command_buffer, state, vk_info);
3136 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAM_OUTPUT))
3138 context_vk->update_stream_output = 1;
3139 context_vk->c.transform_feedback_paused = 0;
3141 if (context_vk->c.transform_feedback_active && context_vk->update_stream_output)
3143 wined3d_context_vk_bind_stream_output_buffers(context_vk, vk_command_buffer, state, vk_info);
3144 context_vk->update_stream_output = 0;
3147 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_INDEXBUFFER) && state->index_buffer)
3149 const VkDescriptorBufferInfo *buffer_info;
3150 VkIndexType idx_type;
3152 if (state->index_format == WINED3DFMT_R16_UINT)
3153 idx_type = VK_INDEX_TYPE_UINT16;
3154 else
3155 idx_type = VK_INDEX_TYPE_UINT32;
3156 buffer_vk = wined3d_buffer_vk(state->index_buffer);
3157 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
3158 wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
3159 VK_CALL(vkCmdBindIndexBuffer(vk_command_buffer, buffer_info->buffer,
3160 buffer_info->offset + state->index_offset, idx_type));
3163 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL))
3164 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3165 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX))
3166 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3167 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY))
3168 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3169 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL))
3170 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3171 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN))
3172 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING))
3173 context_vk->c.update_shader_resource_bindings = 1;
3174 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING))
3175 context_vk->c.update_unordered_access_view_bindings = 1;
3177 if (context_vk->c.update_shader_resource_bindings || context_vk->c.update_unordered_access_view_bindings)
3179 if (!wined3d_context_vk_update_descriptors(context_vk, vk_command_buffer, state, WINED3D_PIPELINE_GRAPHICS))
3181 ERR("Failed to update shader descriptors.\n");
3182 return VK_NULL_HANDLE;
3185 context_vk->c.update_shader_resource_bindings = 0;
3186 context_vk->c.update_unordered_access_view_bindings = 0;
3189 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_BLEND_FACTOR))
3190 VK_CALL(vkCmdSetBlendConstants(vk_command_buffer, &state->blend_factor.r));
3192 memset(context_vk->c.dirty_graphics_states, 0, sizeof(context_vk->c.dirty_graphics_states));
3193 context_vk->c.shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3195 return vk_command_buffer;
3198 VkCommandBuffer wined3d_context_vk_apply_compute_state(struct wined3d_context_vk *context_vk,
3199 const struct wined3d_state *state, struct wined3d_buffer_vk *indirect_vk)
3201 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
3202 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
3203 VkCommandBuffer vk_command_buffer;
3205 wined3d_context_vk_end_current_render_pass(context_vk);
3207 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_SHADER))
3208 context_vk->c.shader_update_mask |= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3210 if (context_vk->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
3212 device_vk->d.shader_backend->shader_select_compute(device_vk->d.shader_priv, &context_vk->c, state);
3213 if (!context_vk->compute.vk_pipeline)
3215 ERR("No compute pipeline set.\n");
3216 return VK_NULL_HANDLE;
3218 context_vk->c.update_compute_shader_resource_bindings = 1;
3219 context_vk->c.update_compute_unordered_access_view_bindings = 1;
3220 context_vk->update_compute_pipeline = 1;
3223 wined3d_context_vk_load_shader_resources(context_vk, state, WINED3D_PIPELINE_COMPUTE);
3225 if (indirect_vk)
3227 wined3d_buffer_load_location(&indirect_vk->b, &context_vk->c, WINED3D_LOCATION_BUFFER);
3228 wined3d_buffer_vk_barrier(indirect_vk, context_vk, WINED3D_BIND_INDIRECT_BUFFER);
3231 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
3233 ERR("Failed to get command buffer.\n");
3234 return VK_NULL_HANDLE;
3237 if (context_vk->update_compute_pipeline)
3239 VK_CALL(vkCmdBindPipeline(vk_command_buffer,
3240 VK_PIPELINE_BIND_POINT_COMPUTE, context_vk->compute.vk_pipeline));
3241 context_vk->update_compute_pipeline = 0;
3244 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_CONSTANT_BUFFER)
3245 || wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING))
3246 context_vk->c.update_compute_shader_resource_bindings = 1;
3247 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING))
3248 context_vk->c.update_compute_unordered_access_view_bindings = 1;
3250 if (context_vk->c.update_compute_shader_resource_bindings
3251 || context_vk->c.update_compute_unordered_access_view_bindings)
3253 if (!wined3d_context_vk_update_descriptors(context_vk, vk_command_buffer, state, WINED3D_PIPELINE_COMPUTE))
3255 ERR("Failed to update shader descriptors.\n");
3256 return VK_NULL_HANDLE;
3259 context_vk->c.update_compute_shader_resource_bindings = 0;
3260 context_vk->c.update_compute_unordered_access_view_bindings = 0;
3263 memset(context_vk->c.dirty_compute_states, 0, sizeof(context_vk->c.dirty_compute_states));
3264 context_vk->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
3266 return vk_command_buffer;
3269 HRESULT wined3d_context_vk_init(struct wined3d_context_vk *context_vk, struct wined3d_swapchain *swapchain)
3271 VkCommandPoolCreateInfo command_pool_info;
3272 const struct wined3d_vk_info *vk_info;
3273 struct wined3d_adapter_vk *adapter_vk;
3274 struct wined3d_device_vk *device_vk;
3275 VkResult vr;
3277 TRACE("context_vk %p, swapchain %p.\n", context_vk, swapchain);
3279 memset(context_vk, 0, sizeof(*context_vk));
3280 wined3d_context_init(&context_vk->c, swapchain);
3281 device_vk = wined3d_device_vk(swapchain->device);
3282 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
3283 context_vk->vk_info = vk_info = &adapter_vk->vk_info;
3285 command_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
3286 command_pool_info.pNext = NULL;
3287 command_pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
3288 command_pool_info.queueFamilyIndex = device_vk->vk_queue_family_index;
3289 if ((vr = VK_CALL(vkCreateCommandPool(device_vk->vk_device,
3290 &command_pool_info, NULL, &context_vk->vk_command_pool))) < 0)
3292 ERR("Failed to create Vulkan command pool, vr %s.\n", wined3d_debug_vkresult(vr));
3293 wined3d_context_cleanup(&context_vk->c);
3294 return E_FAIL;
3296 context_vk->current_command_buffer.id = 1;
3298 wined3d_context_vk_init_graphics_pipeline_key(context_vk);
3300 list_init(&context_vk->active_queries);
3301 list_init(&context_vk->free_occlusion_query_pools);
3302 list_init(&context_vk->free_timestamp_query_pools);
3303 list_init(&context_vk->free_pipeline_statistics_query_pools);
3304 list_init(&context_vk->free_stream_output_statistics_query_pools);
3306 wine_rb_init(&context_vk->render_passes, wined3d_render_pass_vk_compare);
3307 wine_rb_init(&context_vk->pipeline_layouts, wined3d_pipeline_layout_vk_compare);
3308 wine_rb_init(&context_vk->graphics_pipelines, wined3d_graphics_pipeline_vk_compare);
3309 wine_rb_init(&context_vk->bo_slab_available, wined3d_bo_slab_vk_compare);
3311 return WINED3D_OK;