d2d1: Partially implement d2d_device_context_DrawImage().
[wine.git] / dlls / wined3d / context_vk.c
blob2d80c59ca32b7a966b556da3b391db42045d0a1f
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 static struct wined3d_retired_object_vk *wined3d_context_vk_get_retired_object_vk(struct wined3d_context_vk *context_vk)
506 struct wined3d_retired_objects_vk *retired = &context_vk->retired;
507 struct wined3d_retired_object_vk *o;
509 if (retired->free)
511 o = retired->free;
512 retired->free = o->u.next;
513 return o;
516 if (!wined3d_array_reserve((void **)&retired->objects, &retired->size,
517 retired->count + 1, sizeof(*retired->objects)))
518 return NULL;
520 return &retired->objects[retired->count++];
523 void wined3d_context_vk_destroy_framebuffer(struct wined3d_context_vk *context_vk,
524 VkFramebuffer vk_framebuffer, uint64_t command_buffer_id)
526 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
527 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
528 struct wined3d_retired_object_vk *o;
530 if (context_vk->completed_command_buffer_id > command_buffer_id)
532 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, vk_framebuffer, NULL));
533 TRACE("Destroyed framebuffer 0x%s.\n", wine_dbgstr_longlong(vk_framebuffer));
534 return;
537 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
539 ERR("Leaking framebuffer 0x%s.\n", wine_dbgstr_longlong(vk_framebuffer));
540 return;
543 o->type = WINED3D_RETIRED_FRAMEBUFFER_VK;
544 o->u.vk_framebuffer = vk_framebuffer;
545 o->command_buffer_id = command_buffer_id;
548 static void wined3d_context_vk_destroy_descriptor_pool(struct wined3d_context_vk *context_vk,
549 VkDescriptorPool vk_descriptor_pool, uint64_t command_buffer_id)
551 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
552 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
553 struct wined3d_retired_object_vk *o;
555 if (context_vk->completed_command_buffer_id > command_buffer_id)
557 VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, vk_descriptor_pool, NULL));
558 TRACE("Destroyed descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
559 return;
562 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
564 ERR("Leaking descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
565 return;
568 o->type = WINED3D_RETIRED_DESCRIPTOR_POOL_VK;
569 o->u.vk_descriptor_pool = vk_descriptor_pool;
570 o->command_buffer_id = command_buffer_id;
573 void wined3d_context_vk_destroy_memory(struct wined3d_context_vk *context_vk,
574 VkDeviceMemory vk_memory, uint64_t command_buffer_id)
576 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
577 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
578 struct wined3d_retired_object_vk *o;
580 if (context_vk->completed_command_buffer_id > command_buffer_id)
582 VK_CALL(vkFreeMemory(device_vk->vk_device, vk_memory, NULL));
583 TRACE("Freed memory 0x%s.\n", wine_dbgstr_longlong(vk_memory));
584 return;
587 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
589 ERR("Leaking memory 0x%s.\n", wine_dbgstr_longlong(vk_memory));
590 return;
593 o->type = WINED3D_RETIRED_MEMORY_VK;
594 o->u.vk_memory = vk_memory;
595 o->command_buffer_id = command_buffer_id;
598 void wined3d_context_vk_destroy_allocator_block(struct wined3d_context_vk *context_vk,
599 struct wined3d_allocator_block *block, uint64_t command_buffer_id)
601 struct wined3d_retired_object_vk *o;
603 if (context_vk->completed_command_buffer_id > command_buffer_id)
605 wined3d_allocator_block_free(block);
606 TRACE("Freed block %p.\n", block);
607 return;
610 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
612 ERR("Leaking block %p.\n", block);
613 return;
616 o->type = WINED3D_RETIRED_ALLOCATOR_BLOCK_VK;
617 o->u.block = block;
618 o->command_buffer_id = command_buffer_id;
621 static void wined3d_bo_slab_vk_free_slice(struct wined3d_bo_slab_vk *slab,
622 SIZE_T idx, struct wined3d_context_vk *context_vk)
624 struct wined3d_bo_slab_vk_key key;
625 struct wine_rb_entry *entry;
627 TRACE("slab %p, idx %lu, context_vk %p.\n", slab, idx, context_vk);
629 if (!slab->map)
631 key.memory_type = slab->requested_memory_type;
632 key.usage = slab->bo.usage;
633 key.size = slab->bo.size;
635 if ((entry = wine_rb_get(&context_vk->bo_slab_available, &key)))
637 slab->next = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
638 wine_rb_replace(&context_vk->bo_slab_available, entry, &slab->entry);
640 else if (wine_rb_put(&context_vk->bo_slab_available, &key, &slab->entry) < 0)
642 ERR("Unable to return slab %p (map 0x%08x) to available tree.\n", slab, slab->map);
645 slab->map |= 1u << idx;
648 static void wined3d_context_vk_destroy_bo_slab_slice(struct wined3d_context_vk *context_vk,
649 struct wined3d_bo_slab_vk *slab, SIZE_T idx, uint64_t command_buffer_id)
651 struct wined3d_retired_object_vk *o;
653 if (context_vk->completed_command_buffer_id > command_buffer_id)
655 wined3d_bo_slab_vk_free_slice(slab, idx, context_vk);
656 return;
659 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
661 ERR("Leaking slab %p, slice %#lx.\n", slab, idx);
662 return;
665 o->type = WINED3D_RETIRED_BO_SLAB_SLICE_VK;
666 o->u.slice.slab = slab;
667 o->u.slice.idx = idx;
668 o->command_buffer_id = command_buffer_id;
671 static void wined3d_context_vk_destroy_buffer(struct wined3d_context_vk *context_vk,
672 VkBuffer vk_buffer, uint64_t command_buffer_id)
674 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
675 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
676 struct wined3d_retired_object_vk *o;
678 if (context_vk->completed_command_buffer_id > command_buffer_id)
680 VK_CALL(vkDestroyBuffer(device_vk->vk_device, vk_buffer, NULL));
681 TRACE("Destroyed buffer 0x%s.\n", wine_dbgstr_longlong(vk_buffer));
682 return;
685 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
687 ERR("Leaking buffer 0x%s.\n", wine_dbgstr_longlong(vk_buffer));
688 return;
691 o->type = WINED3D_RETIRED_BUFFER_VK;
692 o->u.vk_buffer = vk_buffer;
693 o->command_buffer_id = command_buffer_id;
696 void wined3d_context_vk_destroy_image(struct wined3d_context_vk *context_vk,
697 VkImage vk_image, uint64_t command_buffer_id)
699 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
700 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
701 struct wined3d_retired_object_vk *o;
703 if (context_vk->completed_command_buffer_id > command_buffer_id)
705 VK_CALL(vkDestroyImage(device_vk->vk_device, vk_image, NULL));
706 TRACE("Destroyed image 0x%s.\n", wine_dbgstr_longlong(vk_image));
707 return;
710 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
712 ERR("Leaking image 0x%s.\n", wine_dbgstr_longlong(vk_image));
713 return;
716 o->type = WINED3D_RETIRED_IMAGE_VK;
717 o->u.vk_image = vk_image;
718 o->command_buffer_id = command_buffer_id;
721 void wined3d_context_vk_destroy_buffer_view(struct wined3d_context_vk *context_vk,
722 VkBufferView vk_view, uint64_t command_buffer_id)
724 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
725 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
726 struct wined3d_retired_object_vk *o;
728 if (context_vk->completed_command_buffer_id > command_buffer_id)
730 VK_CALL(vkDestroyBufferView(device_vk->vk_device, vk_view, NULL));
731 TRACE("Destroyed buffer view 0x%s.\n", wine_dbgstr_longlong(vk_view));
732 return;
735 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
737 ERR("Leaking buffer view 0x%s.\n", wine_dbgstr_longlong(vk_view));
738 return;
741 o->type = WINED3D_RETIRED_BUFFER_VIEW_VK;
742 o->u.vk_buffer_view = vk_view;
743 o->command_buffer_id = command_buffer_id;
746 void wined3d_context_vk_destroy_image_view(struct wined3d_context_vk *context_vk,
747 VkImageView vk_view, uint64_t command_buffer_id)
749 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
750 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
751 struct wined3d_retired_object_vk *o;
753 if (context_vk->completed_command_buffer_id > command_buffer_id)
755 VK_CALL(vkDestroyImageView(device_vk->vk_device, vk_view, NULL));
756 TRACE("Destroyed image view 0x%s.\n", wine_dbgstr_longlong(vk_view));
757 return;
760 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
762 ERR("Leaking image view 0x%s.\n", wine_dbgstr_longlong(vk_view));
763 return;
766 o->type = WINED3D_RETIRED_IMAGE_VIEW_VK;
767 o->u.vk_image_view = vk_view;
768 o->command_buffer_id = command_buffer_id;
771 void wined3d_context_vk_destroy_sampler(struct wined3d_context_vk *context_vk,
772 VkSampler vk_sampler, uint64_t command_buffer_id)
774 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
775 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
776 struct wined3d_retired_object_vk *o;
778 if (context_vk->completed_command_buffer_id > command_buffer_id)
780 VK_CALL(vkDestroySampler(device_vk->vk_device, vk_sampler, NULL));
781 TRACE("Destroyed sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
782 return;
785 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
787 ERR("Leaking sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
788 return;
791 o->type = WINED3D_RETIRED_SAMPLER_VK;
792 o->u.vk_sampler = vk_sampler;
793 o->command_buffer_id = command_buffer_id;
796 void wined3d_context_vk_destroy_bo(struct wined3d_context_vk *context_vk, const struct wined3d_bo_vk *bo)
798 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
799 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
800 struct wined3d_bo_slab_vk *slab_vk;
801 size_t object_size, idx;
803 TRACE("context_vk %p, bo %p.\n", context_vk, bo);
805 if ((slab_vk = bo->slab))
807 if (bo->map_ptr)
808 wined3d_bo_slab_vk_unmap(slab_vk, context_vk);
809 object_size = slab_vk->bo.size / 32;
810 idx = bo->buffer_offset / object_size;
811 wined3d_context_vk_destroy_bo_slab_slice(context_vk, slab_vk, idx, bo->command_buffer_id);
812 return;
815 wined3d_context_vk_destroy_buffer(context_vk, bo->vk_buffer, bo->command_buffer_id);
816 if (bo->memory)
818 if (bo->map_ptr)
819 wined3d_allocator_chunk_vk_unmap(wined3d_allocator_chunk_vk(bo->memory->chunk), context_vk);
820 wined3d_context_vk_destroy_allocator_block(context_vk, bo->memory, bo->command_buffer_id);
821 return;
824 if (bo->map_ptr)
825 VK_CALL(vkUnmapMemory(device_vk->vk_device, bo->vk_memory));
826 wined3d_context_vk_destroy_memory(context_vk, bo->vk_memory, bo->command_buffer_id);
829 void wined3d_context_vk_poll_command_buffers(struct wined3d_context_vk *context_vk)
831 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
832 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
833 struct wined3d_command_buffer_vk *buffer;
834 SIZE_T i = 0;
836 while (i < context_vk->submitted.buffer_count)
838 buffer = &context_vk->submitted.buffers[i];
839 if (VK_CALL(vkGetFenceStatus(device_vk->vk_device, buffer->vk_fence)) == VK_NOT_READY)
841 ++i;
842 continue;
845 TRACE("Command buffer %p with id 0x%s has finished.\n",
846 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
847 VK_CALL(vkDestroyFence(device_vk->vk_device, buffer->vk_fence, NULL));
848 VK_CALL(vkFreeCommandBuffers(device_vk->vk_device,
849 context_vk->vk_command_pool, 1, &buffer->vk_command_buffer));
851 if (buffer->id > context_vk->completed_command_buffer_id)
852 context_vk->completed_command_buffer_id = buffer->id;
853 *buffer = context_vk->submitted.buffers[--context_vk->submitted.buffer_count];
857 static void wined3d_context_vk_cleanup_resources(struct wined3d_context_vk *context_vk)
859 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
860 struct wined3d_retired_objects_vk *retired = &context_vk->retired;
861 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
862 struct wined3d_retired_object_vk *o;
863 uint64_t command_buffer_id;
864 SIZE_T i = 0;
866 wined3d_context_vk_poll_command_buffers(context_vk);
867 command_buffer_id = context_vk->completed_command_buffer_id;
869 retired->free = NULL;
870 for (i = retired->count; i; --i)
872 o = &retired->objects[i - 1];
874 if (o->type != WINED3D_RETIRED_FREE_VK && o->command_buffer_id > command_buffer_id)
875 continue;
877 switch (o->type)
879 case WINED3D_RETIRED_FREE_VK:
880 /* Nothing to do. */
881 break;
883 case WINED3D_RETIRED_FRAMEBUFFER_VK:
884 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, o->u.vk_framebuffer, NULL));
885 TRACE("Destroyed framebuffer 0x%s.\n", wine_dbgstr_longlong(o->u.vk_framebuffer));
886 break;
888 case WINED3D_RETIRED_DESCRIPTOR_POOL_VK:
889 VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, o->u.vk_descriptor_pool, NULL));
890 TRACE("Destroyed descriptor pool 0x%s.\n", wine_dbgstr_longlong(o->u.vk_descriptor_pool));
891 break;
893 case WINED3D_RETIRED_MEMORY_VK:
894 VK_CALL(vkFreeMemory(device_vk->vk_device, o->u.vk_memory, NULL));
895 TRACE("Freed memory 0x%s.\n", wine_dbgstr_longlong(o->u.vk_memory));
896 break;
898 case WINED3D_RETIRED_ALLOCATOR_BLOCK_VK:
899 TRACE("Destroying block %p.\n", o->u.block);
900 wined3d_allocator_block_free(o->u.block);
901 break;
903 case WINED3D_RETIRED_BO_SLAB_SLICE_VK:
904 wined3d_bo_slab_vk_free_slice(o->u.slice.slab, o->u.slice.idx, context_vk);
905 break;
907 case WINED3D_RETIRED_BUFFER_VK:
908 VK_CALL(vkDestroyBuffer(device_vk->vk_device, o->u.vk_buffer, NULL));
909 TRACE("Destroyed buffer 0x%s.\n", wine_dbgstr_longlong(o->u.vk_buffer));
910 break;
912 case WINED3D_RETIRED_IMAGE_VK:
913 VK_CALL(vkDestroyImage(device_vk->vk_device, o->u.vk_image, NULL));
914 TRACE("Destroyed image 0x%s.\n", wine_dbgstr_longlong(o->u.vk_image));
915 break;
917 case WINED3D_RETIRED_BUFFER_VIEW_VK:
918 VK_CALL(vkDestroyBufferView(device_vk->vk_device, o->u.vk_buffer_view, NULL));
919 TRACE("Destroyed buffer view 0x%s.\n", wine_dbgstr_longlong(o->u.vk_buffer_view));
920 break;
922 case WINED3D_RETIRED_IMAGE_VIEW_VK:
923 VK_CALL(vkDestroyImageView(device_vk->vk_device, o->u.vk_image_view, NULL));
924 TRACE("Destroyed image view 0x%s.\n", wine_dbgstr_longlong(o->u.vk_image_view));
925 break;
927 case WINED3D_RETIRED_SAMPLER_VK:
928 VK_CALL(vkDestroySampler(device_vk->vk_device, o->u.vk_sampler, NULL));
929 TRACE("Destroyed sampler 0x%s.\n", wine_dbgstr_longlong(o->u.vk_sampler));
930 break;
932 default:
933 ERR("Unhandled object type %#x.\n", o->type);
934 break;
937 if (i == retired->count)
939 --retired->count;
940 continue;
943 o->type = WINED3D_RETIRED_FREE_VK;
944 o->u.next = retired->free;
945 retired->free = o;
949 static void wined3d_context_vk_destroy_bo_slab(struct wine_rb_entry *entry, void *ctx)
951 struct wined3d_context_vk *context_vk = ctx;
952 struct wined3d_bo_slab_vk *slab, *next;
954 slab = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
955 while (slab)
957 next = slab->next;
958 wined3d_context_vk_destroy_bo(context_vk, &slab->bo);
959 heap_free(slab);
960 slab = next;
964 static void wined3d_context_vk_destroy_graphics_pipeline(struct wine_rb_entry *entry, void *ctx)
966 struct wined3d_graphics_pipeline_vk *pipeline_vk = WINE_RB_ENTRY_VALUE(entry,
967 struct wined3d_graphics_pipeline_vk, entry);
968 struct wined3d_context_vk *context_vk = ctx;
969 const struct wined3d_vk_info *vk_info;
970 struct wined3d_device_vk *device_vk;
972 vk_info = context_vk->vk_info;
973 device_vk = wined3d_device_vk(context_vk->c.device);
975 VK_CALL(vkDestroyPipeline(device_vk->vk_device, pipeline_vk->vk_pipeline, NULL));
976 heap_free(pipeline_vk);
979 static void wined3d_context_vk_destroy_pipeline_layout(struct wine_rb_entry *entry, void *ctx)
981 struct wined3d_pipeline_layout_vk *layout = WINE_RB_ENTRY_VALUE(entry,
982 struct wined3d_pipeline_layout_vk, entry);
983 struct wined3d_context_vk *context_vk = ctx;
984 const struct wined3d_vk_info *vk_info;
985 struct wined3d_device_vk *device_vk;
987 vk_info = context_vk->vk_info;
988 device_vk = wined3d_device_vk(context_vk->c.device);
990 VK_CALL(vkDestroyPipelineLayout(device_vk->vk_device, layout->vk_pipeline_layout, NULL));
991 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
992 heap_free(layout->key.bindings);
993 heap_free(layout);
996 static void wined3d_render_pass_key_vk_init(struct wined3d_render_pass_key_vk *key,
997 const struct wined3d_fb_state *fb, unsigned int rt_count, bool depth_stencil, uint32_t clear_flags)
999 struct wined3d_render_pass_attachment_vk *a;
1000 struct wined3d_rendertarget_view *view;
1001 unsigned int i;
1003 memset(key, 0, sizeof(*key));
1005 for (i = 0; i < rt_count; ++i)
1007 if (!(view = fb->render_targets[i]) || view->format->id == WINED3DFMT_NULL)
1008 continue;
1010 a = &key->rt[i];
1011 a->vk_format = wined3d_format_vk(view->format)->vk_format;
1012 a->vk_samples = max(1, wined3d_resource_get_sample_count(view->resource));
1013 a->vk_layout = wined3d_texture_vk(wined3d_texture_from_resource(view->resource))->layout;
1014 key->rt_mask |= 1u << i;
1017 if (depth_stencil && (view = fb->depth_stencil))
1019 a = &key->ds;
1020 a->vk_format = wined3d_format_vk(view->format)->vk_format;
1021 a->vk_samples = max(1, wined3d_resource_get_sample_count(view->resource));
1022 a->vk_layout = wined3d_texture_vk(wined3d_texture_from_resource(view->resource))->layout;
1023 key->rt_mask |= 1u << WINED3D_MAX_RENDER_TARGETS;
1026 key->clear_flags = clear_flags;
1029 static void wined3d_render_pass_vk_cleanup(struct wined3d_render_pass_vk *pass,
1030 struct wined3d_context_vk *context_vk)
1032 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1033 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1035 VK_CALL(vkDestroyRenderPass(device_vk->vk_device, pass->vk_render_pass, NULL));
1038 static bool wined3d_render_pass_vk_init(struct wined3d_render_pass_vk *pass,
1039 struct wined3d_context_vk *context_vk, const struct wined3d_render_pass_key_vk *key)
1041 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1042 VkAttachmentReference attachment_references[WINED3D_MAX_RENDER_TARGETS];
1043 VkAttachmentDescription attachments[WINED3D_MAX_RENDER_TARGETS + 1];
1044 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1045 const struct wined3d_render_pass_attachment_vk *a;
1046 VkAttachmentReference ds_attachment_reference;
1047 VkAttachmentReference *ds_reference = NULL;
1048 unsigned int attachment_count, rt_count, i;
1049 VkAttachmentDescription *attachment;
1050 VkSubpassDescription sub_pass_desc;
1051 VkRenderPassCreateInfo pass_desc;
1052 uint32_t mask;
1053 VkResult vr;
1055 rt_count = 0;
1056 attachment_count = 0;
1057 mask = key->rt_mask & ((1u << WINED3D_MAX_RENDER_TARGETS) - 1);
1058 while (mask)
1060 i = wined3d_bit_scan(&mask);
1061 a = &key->rt[i];
1063 attachment = &attachments[attachment_count];
1064 attachment->flags = 0;
1065 attachment->format = a->vk_format;
1066 attachment->samples = a->vk_samples;
1067 if (key->clear_flags & WINED3DCLEAR_TARGET)
1068 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1069 else
1070 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1071 attachment->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1072 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1073 attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
1074 attachment->initialLayout = a->vk_layout;
1075 attachment->finalLayout = a->vk_layout;
1077 attachment_references[i].attachment = attachment_count;
1078 attachment_references[i].layout = a->vk_layout;
1080 ++attachment_count;
1081 rt_count = i + 1;
1084 mask = ~key->rt_mask & ((1u << rt_count) - 1);
1085 while (mask)
1087 i = wined3d_bit_scan(&mask);
1088 attachment_references[i].attachment = VK_ATTACHMENT_UNUSED;
1089 attachment_references[i].layout = VK_IMAGE_LAYOUT_UNDEFINED;
1092 if (key->rt_mask & (1u << WINED3D_MAX_RENDER_TARGETS))
1094 a = &key->ds;
1096 attachment = &attachments[attachment_count];
1097 attachment->flags = 0;
1098 attachment->format = a->vk_format;
1099 attachment->samples = a->vk_samples;
1100 if (key->clear_flags & WINED3DCLEAR_ZBUFFER)
1101 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1102 else
1103 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1104 attachment->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1105 if (key->clear_flags & WINED3DCLEAR_STENCIL)
1106 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1107 else
1108 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1109 attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
1110 attachment->initialLayout = a->vk_layout;
1111 attachment->finalLayout = a->vk_layout;
1113 ds_reference = &ds_attachment_reference;
1114 ds_reference->attachment = attachment_count;
1115 ds_reference->layout = a->vk_layout;
1117 ++attachment_count;
1120 sub_pass_desc.flags = 0;
1121 sub_pass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
1122 sub_pass_desc.inputAttachmentCount = 0;
1123 sub_pass_desc.pInputAttachments = NULL;
1124 sub_pass_desc.colorAttachmentCount = rt_count;
1125 sub_pass_desc.pColorAttachments = attachment_references;
1126 sub_pass_desc.pResolveAttachments = NULL;
1127 sub_pass_desc.pDepthStencilAttachment = ds_reference;
1128 sub_pass_desc.preserveAttachmentCount = 0;
1129 sub_pass_desc.pPreserveAttachments = NULL;
1131 pass_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
1132 pass_desc.pNext = NULL;
1133 pass_desc.flags = 0;
1134 pass_desc.attachmentCount = attachment_count;
1135 pass_desc.pAttachments = attachments;
1136 pass_desc.subpassCount = 1;
1137 pass_desc.pSubpasses = &sub_pass_desc;
1138 pass_desc.dependencyCount = 0;
1139 pass_desc.pDependencies = NULL;
1141 pass->key = *key;
1142 if ((vr = VK_CALL(vkCreateRenderPass(device_vk->vk_device,
1143 &pass_desc, NULL, &pass->vk_render_pass))) < 0)
1145 WARN("Failed to create Vulkan render pass, vr %d.\n", vr);
1146 return false;
1149 return true;
1152 VkRenderPass wined3d_context_vk_get_render_pass(struct wined3d_context_vk *context_vk,
1153 const struct wined3d_fb_state *fb, unsigned int rt_count, bool depth_stencil, uint32_t clear_flags)
1155 struct wined3d_render_pass_key_vk key;
1156 struct wined3d_render_pass_vk *pass;
1157 struct wine_rb_entry *entry;
1159 wined3d_render_pass_key_vk_init(&key, fb, rt_count, depth_stencil, clear_flags);
1160 if ((entry = wine_rb_get(&context_vk->render_passes, &key)))
1161 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_render_pass_vk, entry)->vk_render_pass;
1163 if (!(pass = heap_alloc(sizeof(*pass))))
1164 return VK_NULL_HANDLE;
1166 if (!wined3d_render_pass_vk_init(pass, context_vk, &key))
1168 heap_free(pass);
1169 return VK_NULL_HANDLE;
1172 if (wine_rb_put(&context_vk->render_passes, &pass->key, &pass->entry) == -1)
1174 ERR("Failed to insert render pass.\n");
1175 wined3d_render_pass_vk_cleanup(pass, context_vk);
1176 heap_free(pass);
1177 return VK_NULL_HANDLE;
1180 return pass->vk_render_pass;
1183 void wined3d_context_vk_end_current_render_pass(struct wined3d_context_vk *context_vk)
1185 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1186 VkCommandBuffer vk_command_buffer;
1188 if (context_vk->vk_render_pass)
1190 vk_command_buffer = context_vk->current_command_buffer.vk_command_buffer;
1191 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
1192 context_vk->vk_render_pass = VK_NULL_HANDLE;
1193 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1194 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 0, NULL));
1197 if (context_vk->vk_framebuffer)
1199 wined3d_context_vk_destroy_framebuffer(context_vk,
1200 context_vk->vk_framebuffer, context_vk->current_command_buffer.id);
1201 context_vk->vk_framebuffer = VK_NULL_HANDLE;
1205 static void wined3d_context_vk_destroy_render_pass(struct wine_rb_entry *entry, void *ctx)
1207 struct wined3d_render_pass_vk *pass = WINE_RB_ENTRY_VALUE(entry,
1208 struct wined3d_render_pass_vk, entry);
1210 wined3d_render_pass_vk_cleanup(pass, ctx);
1211 heap_free(pass);
1214 static void wined3d_shader_descriptor_writes_vk_cleanup(struct wined3d_shader_descriptor_writes_vk *writes)
1216 heap_free(writes->writes);
1219 static void wined3d_context_vk_destroy_query_pools(struct wined3d_context_vk *context_vk, struct list *free_pools)
1221 struct wined3d_query_pool_vk *pool_vk, *entry;
1223 LIST_FOR_EACH_ENTRY_SAFE(pool_vk, entry, free_pools, struct wined3d_query_pool_vk, entry)
1225 wined3d_query_pool_vk_cleanup(pool_vk, context_vk);
1226 heap_free(pool_vk);
1230 bool wined3d_context_vk_allocate_query(struct wined3d_context_vk *context_vk,
1231 enum wined3d_query_type type, struct wined3d_query_pool_idx_vk *pool_idx)
1233 struct wined3d_query_pool_vk *pool_vk, *entry;
1234 struct list *free_pools;
1235 size_t idx;
1237 switch (type)
1239 case WINED3D_QUERY_TYPE_OCCLUSION:
1240 free_pools = &context_vk->free_occlusion_query_pools;
1241 break;
1243 case WINED3D_QUERY_TYPE_TIMESTAMP:
1244 free_pools = &context_vk->free_timestamp_query_pools;
1245 break;
1247 case WINED3D_QUERY_TYPE_PIPELINE_STATISTICS:
1248 free_pools = &context_vk->free_pipeline_statistics_query_pools;
1249 break;
1251 case WINED3D_QUERY_TYPE_SO_STATISTICS:
1252 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM0:
1253 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM1:
1254 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM2:
1255 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM3:
1256 free_pools = &context_vk->free_stream_output_statistics_query_pools;
1257 break;
1259 default:
1260 FIXME("Unhandled query type %#x.\n", type);
1261 return false;
1264 LIST_FOR_EACH_ENTRY_SAFE(pool_vk, entry, free_pools, struct wined3d_query_pool_vk, entry)
1266 if (wined3d_query_pool_vk_allocate_query(pool_vk, &idx))
1267 goto done;
1268 list_remove(&pool_vk->entry);
1271 if (!(pool_vk = heap_alloc_zero(sizeof(*pool_vk))))
1272 return false;
1273 if (!wined3d_query_pool_vk_init(pool_vk, context_vk, type, free_pools))
1275 heap_free(pool_vk);
1276 return false;
1279 if (!wined3d_query_pool_vk_allocate_query(pool_vk, &idx))
1281 wined3d_query_pool_vk_cleanup(pool_vk, context_vk);
1282 heap_free(pool_vk);
1283 return false;
1286 done:
1287 pool_idx->pool_vk = pool_vk;
1288 pool_idx->idx = idx;
1290 return true;
1293 void wined3d_context_vk_cleanup(struct wined3d_context_vk *context_vk)
1295 struct wined3d_command_buffer_vk *buffer = &context_vk->current_command_buffer;
1296 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1297 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1299 if (buffer->vk_command_buffer)
1301 VK_CALL(vkFreeCommandBuffers(device_vk->vk_device,
1302 context_vk->vk_command_pool, 1, &buffer->vk_command_buffer));
1303 buffer->vk_command_buffer = VK_NULL_HANDLE;
1306 wined3d_context_vk_wait_command_buffer(context_vk, buffer->id - 1);
1307 context_vk->completed_command_buffer_id = buffer->id;
1309 heap_free(context_vk->compute.bindings.bindings);
1310 heap_free(context_vk->graphics.bindings.bindings);
1311 if (context_vk->vk_descriptor_pool)
1312 VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, context_vk->vk_descriptor_pool, NULL));
1313 if (context_vk->vk_framebuffer)
1314 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, context_vk->vk_framebuffer, NULL));
1315 VK_CALL(vkDestroyCommandPool(device_vk->vk_device, context_vk->vk_command_pool, NULL));
1316 if (context_vk->vk_so_counter_bo.vk_buffer)
1317 wined3d_context_vk_destroy_bo(context_vk, &context_vk->vk_so_counter_bo);
1318 wined3d_context_vk_cleanup_resources(context_vk);
1319 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_occlusion_query_pools);
1320 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_timestamp_query_pools);
1321 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_pipeline_statistics_query_pools);
1322 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_stream_output_statistics_query_pools);
1323 wine_rb_destroy(&context_vk->bo_slab_available, wined3d_context_vk_destroy_bo_slab, context_vk);
1324 heap_free(context_vk->pending_queries.queries);
1325 heap_free(context_vk->submitted.buffers);
1326 heap_free(context_vk->retired.objects);
1328 wined3d_shader_descriptor_writes_vk_cleanup(&context_vk->descriptor_writes);
1329 wine_rb_destroy(&context_vk->graphics_pipelines, wined3d_context_vk_destroy_graphics_pipeline, context_vk);
1330 wine_rb_destroy(&context_vk->pipeline_layouts, wined3d_context_vk_destroy_pipeline_layout, context_vk);
1331 wine_rb_destroy(&context_vk->render_passes, wined3d_context_vk_destroy_render_pass, context_vk);
1333 wined3d_context_cleanup(&context_vk->c);
1336 void wined3d_context_vk_remove_pending_queries(struct wined3d_context_vk *context_vk,
1337 struct wined3d_query_vk *query_vk)
1339 struct wined3d_pending_queries_vk *pending = &context_vk->pending_queries;
1340 struct wined3d_pending_query_vk *p;
1341 size_t i;
1343 pending->free_idx = ~(size_t)0;
1344 for (i = pending->count; i; --i)
1346 p = &pending->queries[i - 1];
1348 if (p->query_vk)
1350 if (p->query_vk != query_vk && !wined3d_query_vk_accumulate_data(p->query_vk, context_vk, &p->pool_idx))
1351 continue;
1352 wined3d_query_pool_vk_free_query(p->pool_idx.pool_vk, p->pool_idx.idx);
1353 --p->query_vk->pending_count;
1356 if (i == pending->count)
1358 --pending->count;
1359 continue;
1362 p->query_vk = NULL;
1363 p->pool_idx.pool_vk = NULL;
1364 p->pool_idx.idx = pending->free_idx;
1365 pending->free_idx = i - 1;
1369 void wined3d_context_vk_accumulate_pending_queries(struct wined3d_context_vk *context_vk)
1371 wined3d_context_vk_remove_pending_queries(context_vk, NULL);
1374 void wined3d_context_vk_add_pending_query(struct wined3d_context_vk *context_vk, struct wined3d_query_vk *query_vk)
1376 struct wined3d_pending_queries_vk *pending = &context_vk->pending_queries;
1377 struct wined3d_pending_query_vk *p;
1379 if (pending->free_idx != ~(size_t)0)
1381 p = &pending->queries[pending->free_idx];
1382 pending->free_idx = p->pool_idx.idx;
1384 else
1386 if (!wined3d_array_reserve((void **)&pending->queries, &pending->size,
1387 pending->count + 1, sizeof(*pending->queries)))
1389 ERR("Failed to allocate entry.\n");
1390 return;
1392 p = &pending->queries[pending->count++];
1395 p->query_vk = query_vk;
1396 p->pool_idx = query_vk->pool_idx;
1397 ++query_vk->pending_count;
1400 VkCommandBuffer wined3d_context_vk_get_command_buffer(struct wined3d_context_vk *context_vk)
1402 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1403 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1404 VkCommandBufferAllocateInfo command_buffer_info;
1405 struct wined3d_command_buffer_vk *buffer;
1406 VkCommandBufferBeginInfo begin_info;
1407 struct wined3d_query_vk *query_vk;
1408 VkResult vr;
1410 TRACE("context_vk %p.\n", context_vk);
1412 buffer = &context_vk->current_command_buffer;
1413 if (buffer->vk_command_buffer)
1415 TRACE("Returning existing command buffer %p with id 0x%s.\n",
1416 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1417 return buffer->vk_command_buffer;
1420 command_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1421 command_buffer_info.pNext = NULL;
1422 command_buffer_info.commandPool = context_vk->vk_command_pool;
1423 command_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1424 command_buffer_info.commandBufferCount = 1;
1425 if ((vr = VK_CALL(vkAllocateCommandBuffers(device_vk->vk_device,
1426 &command_buffer_info, &buffer->vk_command_buffer))) < 0)
1428 WARN("Failed to allocate Vulkan command buffer, vr %s.\n", wined3d_debug_vkresult(vr));
1429 return VK_NULL_HANDLE;
1432 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1433 begin_info.pNext = NULL;
1434 begin_info.flags = 0;
1435 begin_info.pInheritanceInfo = NULL;
1436 if ((vr = VK_CALL(vkBeginCommandBuffer(buffer->vk_command_buffer, &begin_info))) < 0)
1438 WARN("Failed to begin command buffer, vr %s.\n", wined3d_debug_vkresult(vr));
1439 VK_CALL(vkFreeCommandBuffers(device_vk->vk_device, context_vk->vk_command_pool,
1440 1, &buffer->vk_command_buffer));
1441 return buffer->vk_command_buffer = VK_NULL_HANDLE;
1444 wined3d_context_vk_accumulate_pending_queries(context_vk);
1445 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->active_queries, struct wined3d_query_vk, entry)
1447 wined3d_query_vk_resume(query_vk, context_vk);
1450 TRACE("Created new command buffer %p with id 0x%s.\n",
1451 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1453 return buffer->vk_command_buffer;
1456 void wined3d_context_vk_submit_command_buffer(struct wined3d_context_vk *context_vk,
1457 unsigned int wait_semaphore_count, const VkSemaphore *wait_semaphores, const VkPipelineStageFlags *wait_stages,
1458 unsigned int signal_semaphore_count, const VkSemaphore *signal_semaphores)
1460 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1461 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1462 struct wined3d_command_buffer_vk *buffer;
1463 struct wined3d_query_vk *query_vk;
1464 VkFenceCreateInfo fence_desc;
1465 VkSubmitInfo submit_info;
1466 VkResult vr;
1468 TRACE("context_vk %p, wait_semaphore_count %u, wait_semaphores %p, wait_stages %p,"
1469 "signal_semaphore_count %u, signal_semaphores %p.\n",
1470 context_vk, wait_semaphore_count, wait_semaphores, wait_stages,
1471 signal_semaphore_count, signal_semaphores);
1473 buffer = &context_vk->current_command_buffer;
1474 if (!buffer->vk_command_buffer)
1475 return;
1477 TRACE("Submitting command buffer %p with id 0x%s.\n",
1478 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1480 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->active_queries, struct wined3d_query_vk, entry)
1482 wined3d_query_vk_suspend(query_vk, context_vk);
1485 wined3d_context_vk_end_current_render_pass(context_vk);
1486 context_vk->graphics.vk_pipeline = VK_NULL_HANDLE;
1487 context_vk->update_compute_pipeline = 1;
1488 context_vk->update_stream_output = 1;
1489 context_vk->c.update_shader_resource_bindings = 1;
1490 context_vk->c.update_compute_shader_resource_bindings = 1;
1491 context_vk->c.update_unordered_access_view_bindings = 1;
1492 context_vk->c.update_compute_unordered_access_view_bindings = 1;
1493 context_invalidate_state(&context_vk->c, STATE_STREAMSRC);
1494 context_invalidate_state(&context_vk->c, STATE_INDEXBUFFER);
1495 context_invalidate_state(&context_vk->c, STATE_BLEND_FACTOR);
1497 VK_CALL(vkEndCommandBuffer(buffer->vk_command_buffer));
1499 fence_desc.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1500 fence_desc.pNext = NULL;
1501 fence_desc.flags = 0;
1502 if ((vr = VK_CALL(vkCreateFence(device_vk->vk_device, &fence_desc, NULL, &buffer->vk_fence))) < 0)
1503 ERR("Failed to create fence, vr %s.\n", wined3d_debug_vkresult(vr));
1505 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1506 submit_info.pNext = NULL;
1507 submit_info.waitSemaphoreCount = wait_semaphore_count;
1508 submit_info.pWaitSemaphores = wait_semaphores;
1509 submit_info.pWaitDstStageMask = wait_stages;
1510 submit_info.commandBufferCount = 1;
1511 submit_info.pCommandBuffers = &buffer->vk_command_buffer;
1512 submit_info.signalSemaphoreCount = signal_semaphore_count;
1513 submit_info.pSignalSemaphores = signal_semaphores;
1515 if ((vr = VK_CALL(vkQueueSubmit(device_vk->vk_queue, 1, &submit_info, buffer->vk_fence))) < 0)
1516 ERR("Failed to submit command buffer %p, vr %s.\n",
1517 buffer->vk_command_buffer, wined3d_debug_vkresult(vr));
1519 if (!wined3d_array_reserve((void **)&context_vk->submitted.buffers, &context_vk->submitted.buffers_size,
1520 context_vk->submitted.buffer_count + 1, sizeof(*context_vk->submitted.buffers)))
1521 ERR("Failed to grow submitted command buffer array.\n");
1523 context_vk->submitted.buffers[context_vk->submitted.buffer_count++] = *buffer;
1525 buffer->vk_command_buffer = VK_NULL_HANDLE;
1526 /* We don't expect this to ever happen, but handle it anyway. */
1527 if (!++buffer->id)
1529 wined3d_context_vk_wait_command_buffer(context_vk, buffer->id - 1);
1530 context_vk->completed_command_buffer_id = 0;
1531 buffer->id = 1;
1533 wined3d_context_vk_cleanup_resources(context_vk);
1536 void wined3d_context_vk_wait_command_buffer(struct wined3d_context_vk *context_vk, uint64_t id)
1538 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1539 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1540 SIZE_T i;
1542 if (id <= context_vk->completed_command_buffer_id
1543 || id > context_vk->current_command_buffer.id) /* In case the buffer ID wrapped. */
1544 return;
1546 for (i = 0; i < context_vk->submitted.buffer_count; ++i)
1548 if (context_vk->submitted.buffers[i].id != id)
1549 continue;
1551 VK_CALL(vkWaitForFences(device_vk->vk_device, 1,
1552 &context_vk->submitted.buffers[i].vk_fence, VK_TRUE, UINT64_MAX));
1553 wined3d_context_vk_cleanup_resources(context_vk);
1554 return;
1557 ERR("Failed to find fence for command buffer with id 0x%s.\n", wine_dbgstr_longlong(id));
1560 void wined3d_context_vk_image_barrier(struct wined3d_context_vk *context_vk,
1561 VkCommandBuffer vk_command_buffer, VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
1562 VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask, VkImageLayout old_layout,
1563 VkImageLayout new_layout, VkImage image, VkImageAspectFlags aspect_mask)
1565 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1566 VkImageMemoryBarrier barrier;
1568 wined3d_context_vk_end_current_render_pass(context_vk);
1570 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1571 barrier.pNext = NULL;
1572 barrier.srcAccessMask = src_access_mask;
1573 barrier.dstAccessMask = dst_access_mask;
1574 barrier.oldLayout = old_layout;
1575 barrier.newLayout = new_layout;
1576 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1577 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1578 barrier.image = image;
1579 barrier.subresourceRange.aspectMask = aspect_mask;
1580 barrier.subresourceRange.baseMipLevel = 0;
1581 barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS;
1582 barrier.subresourceRange.baseArrayLayer = 0;
1583 barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
1585 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, src_stage_mask, dst_stage_mask, 0, 0, NULL, 0, NULL, 1, &barrier));
1588 static int wined3d_render_pass_vk_compare(const void *key, const struct wine_rb_entry *entry)
1590 const struct wined3d_render_pass_key_vk *k = key;
1591 const struct wined3d_render_pass_vk *pass = WINE_RB_ENTRY_VALUE(entry,
1592 const struct wined3d_render_pass_vk, entry);
1594 return memcmp(k, &pass->key, sizeof(*k));
1597 static int wined3d_pipeline_layout_vk_compare(const void *key, const struct wine_rb_entry *entry)
1599 const struct wined3d_pipeline_layout_key_vk *a = key;
1600 const struct wined3d_pipeline_layout_key_vk *b = &WINE_RB_ENTRY_VALUE(entry,
1601 const struct wined3d_pipeline_layout_vk, entry)->key;
1603 if (a->binding_count != b->binding_count)
1604 return a->binding_count - b->binding_count;
1605 return memcmp(a->bindings, b->bindings, a->binding_count * sizeof(*a->bindings));
1608 static int wined3d_graphics_pipeline_vk_compare(const void *key, const struct wine_rb_entry *entry)
1610 const struct wined3d_graphics_pipeline_key_vk *a = key;
1611 const struct wined3d_graphics_pipeline_key_vk *b = &WINE_RB_ENTRY_VALUE(entry,
1612 const struct wined3d_graphics_pipeline_vk, entry)->key;
1613 unsigned int i;
1614 int ret;
1616 if (a->pipeline_desc.stageCount != b->pipeline_desc.stageCount)
1617 return a->pipeline_desc.stageCount - b->pipeline_desc.stageCount;
1618 for (i = 0; i < a->pipeline_desc.stageCount; ++i)
1620 if (a->stages[i].module != b->stages[i].module)
1621 return a->stages[i].module - b->stages[i].module;
1624 if (a->divisor_desc.vertexBindingDivisorCount != b->divisor_desc.vertexBindingDivisorCount)
1625 return a->divisor_desc.vertexBindingDivisorCount - b->divisor_desc.vertexBindingDivisorCount;
1626 if ((ret = memcmp(a->divisors, b->divisors,
1627 a->divisor_desc.vertexBindingDivisorCount * sizeof(*a->divisors))))
1628 return ret;
1630 if (a->input_desc.vertexAttributeDescriptionCount != b->input_desc.vertexAttributeDescriptionCount)
1631 return a->input_desc.vertexAttributeDescriptionCount - b->input_desc.vertexAttributeDescriptionCount;
1632 if ((ret = memcmp(a->attributes, b->attributes,
1633 a->input_desc.vertexAttributeDescriptionCount * sizeof(*a->attributes))))
1634 return ret;
1635 if (a->input_desc.vertexBindingDescriptionCount != b->input_desc.vertexBindingDescriptionCount)
1636 return a->input_desc.vertexBindingDescriptionCount - b->input_desc.vertexBindingDescriptionCount;
1637 if ((ret = memcmp(a->bindings, b->bindings,
1638 a->input_desc.vertexBindingDescriptionCount * sizeof(*a->bindings))))
1639 return ret;
1641 if (a->ia_desc.topology != b->ia_desc.topology)
1642 return a->ia_desc.topology - b->ia_desc.topology;
1643 if (a->ia_desc.primitiveRestartEnable != b->ia_desc.primitiveRestartEnable)
1644 return a->ia_desc.primitiveRestartEnable - b->ia_desc.primitiveRestartEnable;
1646 if (a->ts_desc.patchControlPoints != b->ts_desc.patchControlPoints)
1647 return a->ts_desc.patchControlPoints - b->ts_desc.patchControlPoints;
1649 if ((ret = memcmp(&a->viewport, &b->viewport, sizeof(a->viewport))))
1650 return ret;
1652 if ((ret = memcmp(&a->scissor, &b->scissor, sizeof(a->scissor))))
1653 return ret;
1655 if ((ret = memcmp(&a->rs_desc, &b->rs_desc, sizeof(a->rs_desc))))
1656 return ret;
1658 if (a->ms_desc.rasterizationSamples != b->ms_desc.rasterizationSamples)
1659 return a->ms_desc.rasterizationSamples - b->ms_desc.rasterizationSamples;
1660 if (a->ms_desc.alphaToCoverageEnable != b->ms_desc.alphaToCoverageEnable)
1661 return a->ms_desc.alphaToCoverageEnable - b->ms_desc.alphaToCoverageEnable;
1662 if (a->sample_mask != b->sample_mask)
1663 return a->sample_mask - b->sample_mask;
1665 if ((ret = memcmp(&a->ds_desc, &b->ds_desc, sizeof(a->ds_desc))))
1666 return ret;
1668 if (a->blend_desc.attachmentCount != b->blend_desc.attachmentCount)
1669 return a->blend_desc.attachmentCount - b->blend_desc.attachmentCount;
1670 if ((ret = memcmp(a->blend_attachments, b->blend_attachments,
1671 a->blend_desc.attachmentCount * sizeof(*a->blend_attachments))))
1672 return ret;
1674 if (a->pipeline_desc.layout != b->pipeline_desc.layout)
1675 return a->pipeline_desc.layout - b->pipeline_desc.layout;
1677 if (a->pipeline_desc.renderPass != b->pipeline_desc.renderPass)
1678 return a->pipeline_desc.renderPass - b->pipeline_desc.renderPass;
1680 return 0;
1683 static int wined3d_bo_slab_vk_compare(const void *key, const struct wine_rb_entry *entry)
1685 const struct wined3d_bo_slab_vk *slab = WINE_RB_ENTRY_VALUE(entry, const struct wined3d_bo_slab_vk, entry);
1686 const struct wined3d_bo_slab_vk_key *k = key;
1688 if (k->memory_type != slab->requested_memory_type)
1689 return k->memory_type - slab->requested_memory_type;
1690 if (k->usage != slab->bo.usage)
1691 return k->usage - slab->bo.usage;
1692 return k->size - slab->bo.size;
1695 static void wined3d_context_vk_init_graphics_pipeline_key(struct wined3d_context_vk *context_vk)
1697 struct wined3d_graphics_pipeline_key_vk *key;
1698 VkPipelineShaderStageCreateInfo *stage;
1699 unsigned int i;
1701 static const VkDynamicState dynamic_states[] =
1703 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
1706 key = &context_vk->graphics.pipeline_key_vk;
1707 memset(key, 0, sizeof(*key));
1709 for (i = 0; i < ARRAY_SIZE(context_vk->graphics.vk_modules); ++i)
1711 stage = &key->stages[i];
1712 stage->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1713 stage->pName = "main";
1716 key->input_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1717 key->input_desc.pVertexBindingDescriptions = key->bindings;
1718 key->input_desc.pVertexAttributeDescriptions = key->attributes;
1720 key->divisor_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT;
1721 key->divisor_desc.pVertexBindingDivisors = key->divisors;
1723 key->ia_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
1725 key->ts_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO;
1727 key->vp_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
1728 key->vp_desc.viewportCount = 1;
1729 key->vp_desc.pViewports = &key->viewport;
1730 key->vp_desc.scissorCount = 1;
1731 key->vp_desc.pScissors = &key->scissor;
1733 key->rs_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1734 key->rs_desc.lineWidth = 1.0f;
1736 key->ms_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
1737 key->ms_desc.pSampleMask = &key->sample_mask;
1739 key->ds_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
1740 key->ds_desc.maxDepthBounds = 1.0f;
1742 key->blend_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1743 key->blend_desc.logicOp = VK_LOGIC_OP_COPY;
1744 key->blend_desc.pAttachments = key->blend_attachments;
1745 key->blend_desc.blendConstants[0] = 1.0f;
1746 key->blend_desc.blendConstants[1] = 1.0f;
1747 key->blend_desc.blendConstants[2] = 1.0f;
1748 key->blend_desc.blendConstants[3] = 1.0f;
1750 key->dynamic_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1751 key->dynamic_desc.dynamicStateCount = ARRAY_SIZE(dynamic_states);
1752 key->dynamic_desc.pDynamicStates = dynamic_states;
1754 key->pipeline_desc.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1755 key->pipeline_desc.pStages = key->stages;
1756 key->pipeline_desc.pVertexInputState = &key->input_desc;
1757 key->pipeline_desc.pInputAssemblyState = &key->ia_desc;
1758 key->pipeline_desc.pTessellationState = &key->ts_desc;
1759 key->pipeline_desc.pViewportState = &key->vp_desc;
1760 key->pipeline_desc.pRasterizationState = &key->rs_desc;
1761 key->pipeline_desc.pMultisampleState = &key->ms_desc;
1762 key->pipeline_desc.pDepthStencilState = &key->ds_desc;
1763 key->pipeline_desc.pColorBlendState = &key->blend_desc;
1764 key->pipeline_desc.pDynamicState = &key->dynamic_desc;
1765 key->pipeline_desc.basePipelineIndex = -1;
1768 static void wined3d_context_vk_update_rasterisation_state(const struct wined3d_context_vk *context_vk,
1769 const struct wined3d_state *state, struct wined3d_graphics_pipeline_key_vk *key)
1771 const struct wined3d_d3d_info *d3d_info = context_vk->c.d3d_info;
1772 VkPipelineRasterizationStateCreateInfo *desc = &key->rs_desc;
1773 const struct wined3d_rasterizer_state_desc *r;
1774 float scale_bias;
1775 union
1777 uint32_t u32;
1778 float f32;
1779 } const_bias;
1781 if (!state->rasterizer_state)
1783 desc->depthClampEnable = VK_FALSE;
1784 desc->rasterizerDiscardEnable = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
1785 desc->cullMode = VK_CULL_MODE_BACK_BIT;
1786 desc->frontFace = VK_FRONT_FACE_CLOCKWISE;
1787 desc->depthBiasEnable = VK_FALSE;
1788 desc->depthBiasConstantFactor = 0.0f;
1789 desc->depthBiasClamp = 0.0f;
1790 desc->depthBiasSlopeFactor = 0.0f;
1792 return;
1795 r = &state->rasterizer_state->desc;
1796 desc->depthClampEnable = !r->depth_clip;
1797 desc->rasterizerDiscardEnable = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
1798 desc->cullMode = vk_cull_mode_from_wined3d(r->cull_mode);
1799 desc->frontFace = r->front_ccw ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE;
1801 scale_bias = r->scale_bias;
1802 const_bias.f32 = r->depth_bias;
1803 if (!scale_bias && !const_bias.f32)
1805 desc->depthBiasEnable = VK_FALSE;
1806 desc->depthBiasConstantFactor = 0.0f;
1807 desc->depthBiasClamp = 0.0f;
1808 desc->depthBiasSlopeFactor = 0.0f;
1810 return;
1813 desc->depthBiasEnable = VK_TRUE;
1814 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_DEPTH_BIAS)
1816 const struct wined3d_rendertarget_view *dsv;
1818 if ((dsv = state->fb.depth_stencil))
1820 desc->depthBiasConstantFactor = -(float)const_bias.u32 / dsv->format->depth_bias_scale;
1821 desc->depthBiasSlopeFactor = -(float)const_bias.u32;
1823 else
1825 desc->depthBiasConstantFactor = 0.0f;
1826 desc->depthBiasSlopeFactor = 0.0f;
1829 else
1831 desc->depthBiasConstantFactor = const_bias.f32;
1832 desc->depthBiasSlopeFactor = scale_bias;
1834 desc->depthBiasClamp = r->depth_bias_clamp;
1837 static void wined3d_context_vk_update_blend_state(const struct wined3d_context_vk *context_vk,
1838 const struct wined3d_state *state, struct wined3d_graphics_pipeline_key_vk *key)
1840 VkPipelineColorBlendStateCreateInfo *desc = &key->blend_desc;
1841 const struct wined3d_blend_state_desc *b;
1842 unsigned int i;
1844 desc->attachmentCount = context_vk->rt_count;
1846 memset(key->blend_attachments, 0, sizeof(key->blend_attachments));
1847 if (!state->blend_state)
1849 for (i = 0; i < context_vk->rt_count; ++i)
1851 key->blend_attachments[i].colorWriteMask = VK_COLOR_COMPONENT_R_BIT
1852 | VK_COLOR_COMPONENT_G_BIT
1853 | VK_COLOR_COMPONENT_B_BIT
1854 | VK_COLOR_COMPONENT_A_BIT;
1857 return;
1860 b = &state->blend_state->desc;
1861 for (i = 0; i < context_vk->rt_count; ++i)
1863 const struct wined3d_rendertarget_blend_state_desc *rt = &b->rt[b->independent ? i : 0];
1864 const struct wined3d_rendertarget_view *rtv = state->fb.render_targets[i];
1865 VkPipelineColorBlendAttachmentState *a = &key->blend_attachments[i];
1866 enum wined3d_blend src_blend, dst_blend;
1867 const struct wined3d_format *rt_format;
1869 a->colorWriteMask = vk_colour_write_mask_from_wined3d(rt->writemask);
1870 if (!rt->enable)
1871 continue;
1873 if (rtv)
1874 rt_format = rtv->format;
1875 else
1876 rt_format = wined3d_get_format(context_vk->c.device->adapter, WINED3DFMT_NULL, 0);
1877 a->blendEnable = VK_TRUE;
1879 src_blend = rt->src;
1880 dst_blend = rt->dst;
1881 if (src_blend == WINED3D_BLEND_BOTHSRCALPHA)
1883 src_blend = WINED3D_BLEND_SRCALPHA;
1884 dst_blend = WINED3D_BLEND_INVSRCALPHA;
1886 else if (src_blend == WINED3D_BLEND_BOTHINVSRCALPHA)
1888 src_blend = WINED3D_BLEND_INVSRCALPHA;
1889 dst_blend = WINED3D_BLEND_SRCALPHA;
1891 a->srcColorBlendFactor = vk_blend_factor_from_wined3d(src_blend, rt_format, FALSE);
1892 a->dstColorBlendFactor = vk_blend_factor_from_wined3d(dst_blend, rt_format, FALSE);
1893 a->colorBlendOp = vk_blend_op_from_wined3d(rt->op);
1895 src_blend = rt->src_alpha;
1896 dst_blend = rt->dst_alpha;
1897 a->srcAlphaBlendFactor = vk_blend_factor_from_wined3d(src_blend, rt_format, TRUE);
1898 a->dstAlphaBlendFactor = vk_blend_factor_from_wined3d(dst_blend, rt_format, TRUE);
1899 a->alphaBlendOp = vk_blend_op_from_wined3d(rt->op_alpha);
1903 static bool wined3d_context_vk_update_graphics_pipeline_key(struct wined3d_context_vk *context_vk,
1904 const struct wined3d_state *state, VkPipelineLayout vk_pipeline_layout)
1906 unsigned int i, attribute_count, binding_count, divisor_count, stage_count;
1907 const struct wined3d_d3d_info *d3d_info = context_vk->c.d3d_info;
1908 struct wined3d_graphics_pipeline_key_vk *key;
1909 VkPipelineShaderStageCreateInfo *stage;
1910 struct wined3d_stream_info stream_info;
1911 VkPrimitiveTopology vk_topology;
1912 VkShaderModule module;
1913 bool update = false;
1914 uint32_t mask;
1916 key = &context_vk->graphics.pipeline_key_vk;
1918 if (context_vk->c.shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
1920 stage_count = 0;
1921 for (i = 0; i < ARRAY_SIZE(context_vk->graphics.vk_modules); ++i)
1923 if (!(module = context_vk->graphics.vk_modules[i]))
1924 continue;
1926 stage = &key->stages[stage_count++];
1927 stage->stage = vk_shader_stage_from_wined3d(i);
1928 stage->module = module;
1931 key->pipeline_desc.stageCount = stage_count;
1933 update = true;
1936 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_VDECL)
1937 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAMSRC)
1938 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
1940 wined3d_stream_info_from_declaration(&stream_info, state, d3d_info);
1941 divisor_count = 0;
1942 for (i = 0, mask = 0, attribute_count = 0, binding_count = 0; i < ARRAY_SIZE(stream_info.elements); ++i)
1944 VkVertexInputBindingDivisorDescriptionEXT *d;
1945 struct wined3d_stream_info_element *e;
1946 VkVertexInputAttributeDescription *a;
1947 VkVertexInputBindingDescription *b;
1948 uint32_t binding;
1950 if (!(stream_info.use_map & (1u << i)))
1951 continue;
1953 a = &key->attributes[attribute_count++];
1954 e = &stream_info.elements[i];
1955 binding = e->stream_idx;
1957 a->location = i;
1958 a->binding = binding;
1959 a->format = wined3d_format_vk(e->format)->vk_format;
1960 a->offset = (UINT_PTR)e->data.addr - state->streams[binding].offset;
1962 if (mask & (1u << binding))
1963 continue;
1964 mask |= 1u << binding;
1966 b = &key->bindings[binding_count++];
1967 b->binding = binding;
1968 b->stride = e->stride;
1969 b->inputRate = e->divisor ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX;
1971 if (e->divisor > 1)
1973 d = &key->divisors[divisor_count++];
1974 d->binding = binding;
1975 d->divisor = e->divisor;
1979 key->input_desc.pNext = NULL;
1980 key->input_desc.vertexBindingDescriptionCount = binding_count;
1981 key->input_desc.vertexAttributeDescriptionCount = attribute_count;
1983 if (divisor_count)
1985 key->input_desc.pNext = &key->divisor_desc;
1986 key->divisor_desc.vertexBindingDivisorCount = divisor_count;
1989 update = true;
1992 vk_topology = vk_topology_from_wined3d(state->primitive_type);
1993 if (key->ia_desc.topology != vk_topology)
1995 key->ia_desc.topology = vk_topology;
1996 key->ia_desc.primitiveRestartEnable = !(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART)
1997 && !wined3d_primitive_type_is_list(state->primitive_type);
1999 update = true;
2002 if (key->ts_desc.patchControlPoints != state->patch_vertex_count)
2004 key->ts_desc.patchControlPoints = state->patch_vertex_count;
2006 update = true;
2009 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_VIEWPORT)
2010 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SCISSORRECT)
2011 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_RASTERIZER))
2013 key->viewport.x = state->viewports[0].x;
2014 key->viewport.y = state->viewports[0].y;
2015 key->viewport.width = state->viewports[0].width;
2016 key->viewport.height = state->viewports[0].height;
2017 key->viewport.minDepth = state->viewports[0].min_z;
2018 key->viewport.maxDepth = state->viewports[0].max_z;
2020 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
2022 const RECT *r = &state->scissor_rects[0];
2024 key->scissor.offset.x = r->left;
2025 key->scissor.offset.y = r->top;
2026 key->scissor.extent.width = r->right - r->left;
2027 key->scissor.extent.height = r->bottom - r->top;
2029 else
2031 key->scissor.offset.x = key->viewport.x;
2032 key->scissor.offset.y = key->viewport.y;
2033 key->scissor.extent.width = key->viewport.width;
2034 key->scissor.extent.height = key->viewport.height;
2036 key->viewport.y += key->viewport.height;
2037 key->viewport.height = -key->viewport.height;
2039 update = true;
2042 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_RASTERIZER)
2043 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY)))
2045 wined3d_context_vk_update_rasterisation_state(context_vk, state, key);
2047 update = true;
2050 if (key->ms_desc.rasterizationSamples != context_vk->sample_count
2051 || isStateDirty(&context_vk->c, STATE_BLEND) || isStateDirty(&context_vk->c, STATE_SAMPLE_MASK))
2053 key->ms_desc.rasterizationSamples = context_vk->sample_count;
2054 key->ms_desc.alphaToCoverageEnable = state->blend_state && state->blend_state->desc.alpha_to_coverage;
2055 key->sample_mask = state->sample_mask;
2057 update = true;
2060 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_DEPTH_STENCIL)
2061 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2063 const struct wined3d_depth_stencil_state *d = state->depth_stencil_state;
2065 if (d)
2067 key->ds_desc.depthTestEnable = d->desc.depth;
2068 key->ds_desc.depthWriteEnable = d->desc.depth_write;
2069 key->ds_desc.depthCompareOp = vk_compare_op_from_wined3d(d->desc.depth_func);
2070 key->ds_desc.stencilTestEnable = state->fb.depth_stencil && d->desc.stencil;
2071 if (key->ds_desc.stencilTestEnable)
2073 key->ds_desc.front.failOp = vk_stencil_op_from_wined3d(d->desc.front.fail_op);
2074 key->ds_desc.front.passOp = vk_stencil_op_from_wined3d(d->desc.front.pass_op);
2075 key->ds_desc.front.depthFailOp = vk_stencil_op_from_wined3d(d->desc.front.depth_fail_op);
2076 key->ds_desc.front.compareOp = vk_compare_op_from_wined3d(d->desc.front.func);
2077 key->ds_desc.front.compareMask = d->desc.stencil_read_mask;
2078 key->ds_desc.front.writeMask = d->desc.stencil_write_mask;
2079 key->ds_desc.front.reference = state->render_states[WINED3D_RS_STENCILREF]
2080 & ((1 << state->fb.depth_stencil->format->stencil_size) - 1);
2082 key->ds_desc.back.failOp = vk_stencil_op_from_wined3d(d->desc.back.fail_op);
2083 key->ds_desc.back.passOp = vk_stencil_op_from_wined3d(d->desc.back.pass_op);
2084 key->ds_desc.back.depthFailOp = vk_stencil_op_from_wined3d(d->desc.back.depth_fail_op);
2085 key->ds_desc.back.compareOp = vk_compare_op_from_wined3d(d->desc.back.func);
2086 key->ds_desc.back.compareMask = d->desc.stencil_read_mask;
2087 key->ds_desc.back.writeMask = d->desc.stencil_write_mask;
2088 key->ds_desc.back.reference = state->render_states[WINED3D_RS_STENCILREF]
2089 & ((1 << state->fb.depth_stencil->format->stencil_size) - 1);
2091 else
2093 memset(&key->ds_desc.front, 0, sizeof(key->ds_desc.front));
2094 memset(&key->ds_desc.back, 0, sizeof(key->ds_desc.back));
2097 else
2099 key->ds_desc.depthTestEnable = VK_TRUE;
2100 key->ds_desc.depthWriteEnable = VK_TRUE;
2101 key->ds_desc.depthCompareOp = VK_COMPARE_OP_LESS;
2102 key->ds_desc.stencilTestEnable = VK_FALSE;
2105 update = true;
2108 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_BLEND)
2109 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2111 wined3d_context_vk_update_blend_state(context_vk, state, key);
2113 update = true;
2116 if (key->pipeline_desc.layout != vk_pipeline_layout)
2118 key->pipeline_desc.layout = vk_pipeline_layout;
2120 update = true;
2123 if (key->pipeline_desc.renderPass != context_vk->vk_render_pass)
2125 key->pipeline_desc.renderPass = context_vk->vk_render_pass;
2127 update = true;
2130 return update;
2133 static bool wined3d_context_vk_begin_render_pass(struct wined3d_context_vk *context_vk,
2134 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2136 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2137 VkImageView vk_views[WINED3D_MAX_RENDER_TARGETS + 1];
2138 unsigned int fb_width, fb_height, fb_layer_count;
2139 struct wined3d_rendertarget_view_vk *rtv_vk;
2140 struct wined3d_rendertarget_view *view;
2141 const VkPhysicalDeviceLimits *limits;
2142 VkRenderPassBeginInfo begin_info;
2143 unsigned int attachment_count, i;
2144 VkFramebufferCreateInfo fb_desc;
2145 VkResult vr;
2147 if (context_vk->vk_render_pass)
2148 return true;
2150 limits = &wined3d_adapter_vk(device_vk->d.adapter)->device_limits;
2151 fb_width = limits->maxFramebufferWidth;
2152 fb_height = limits->maxFramebufferHeight;
2153 fb_layer_count = limits->maxFramebufferLayers;
2154 attachment_count = 0;
2156 context_vk->rt_count = 0;
2157 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
2159 if (!(view = state->fb.render_targets[i]) || view->format->id == WINED3DFMT_NULL)
2160 continue;
2162 rtv_vk = wined3d_rendertarget_view_vk(view);
2163 vk_views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
2164 wined3d_context_vk_reference_rendertarget_view(context_vk, rtv_vk);
2166 if (view->width < fb_width)
2167 fb_width = view->width;
2168 if (view->height < fb_height)
2169 fb_height = view->height;
2170 if (view->layer_count < fb_layer_count)
2171 fb_layer_count = view->layer_count;
2172 context_vk->rt_count = i + 1;
2173 ++attachment_count;
2176 if (wined3d_state_uses_depth_buffer(state) && (view = state->fb.depth_stencil))
2178 rtv_vk = wined3d_rendertarget_view_vk(view);
2179 vk_views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
2180 wined3d_context_vk_reference_rendertarget_view(context_vk, rtv_vk);
2182 if (view->width < fb_width)
2183 fb_width = view->width;
2184 if (view->height < fb_height)
2185 fb_height = view->height;
2186 if (view->layer_count < fb_layer_count)
2187 fb_layer_count = view->layer_count;
2188 ++attachment_count;
2191 if (!(context_vk->vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, &state->fb,
2192 ARRAY_SIZE(state->fb.render_targets), wined3d_state_uses_depth_buffer(state), 0)))
2194 ERR("Failed to get render pass.\n");
2195 return false;
2198 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
2199 fb_desc.pNext = NULL;
2200 fb_desc.flags = 0;
2201 fb_desc.renderPass = context_vk->vk_render_pass;
2202 fb_desc.attachmentCount = attachment_count;
2203 fb_desc.pAttachments = vk_views;
2204 fb_desc.width = fb_width;
2205 fb_desc.height = fb_height;
2206 fb_desc.layers = fb_layer_count;
2208 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &context_vk->vk_framebuffer))) < 0)
2210 WARN("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
2211 return false;
2214 begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
2215 begin_info.pNext = NULL;
2216 begin_info.renderPass = context_vk->vk_render_pass;
2217 begin_info.framebuffer = context_vk->vk_framebuffer;
2218 begin_info.renderArea.offset.x = 0;
2219 begin_info.renderArea.offset.y = 0;
2220 begin_info.renderArea.extent.width = fb_width;
2221 begin_info.renderArea.extent.height = fb_height;
2222 begin_info.clearValueCount = 0;
2223 begin_info.pClearValues = NULL;
2224 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_info, VK_SUBPASS_CONTENTS_INLINE));
2226 return true;
2229 static void wined3d_context_vk_bind_vertex_buffers(struct wined3d_context_vk *context_vk,
2230 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2232 VkDeviceSize offsets[ARRAY_SIZE(state->streams)] = {0};
2233 VkBuffer buffers[ARRAY_SIZE(state->streams)];
2234 const struct wined3d_stream_state *stream;
2235 const VkDescriptorBufferInfo *buffer_info;
2236 struct wined3d_buffer_vk *buffer_vk;
2237 struct wined3d_buffer *buffer;
2238 unsigned int i, first, count;
2240 first = 0;
2241 count = 0;
2242 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
2244 stream = &state->streams[i];
2246 if ((buffer = stream->buffer))
2248 buffer_vk = wined3d_buffer_vk(buffer);
2249 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2250 wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
2251 buffers[count] = buffer_info->buffer;
2252 offsets[count] = buffer_info->offset + stream->offset;
2253 ++count;
2254 continue;
2257 if (count)
2258 VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, first, count, buffers, offsets));
2259 first = i + 1;
2260 count = 0;
2263 if (count)
2264 VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, first, count, buffers, offsets));
2267 static void wined3d_context_vk_bind_stream_output_buffers(struct wined3d_context_vk *context_vk,
2268 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2270 VkDeviceSize offsets[ARRAY_SIZE(state->stream_output)];
2271 VkDeviceSize sizes[ARRAY_SIZE(state->stream_output)];
2272 VkBuffer buffers[ARRAY_SIZE(state->stream_output)];
2273 const struct wined3d_stream_output *stream;
2274 const VkDescriptorBufferInfo *buffer_info;
2275 struct wined3d_buffer_vk *buffer_vk;
2276 struct wined3d_buffer *buffer;
2277 unsigned int i, first, count;
2279 first = 0;
2280 count = 0;
2281 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
2283 stream = &state->stream_output[i];
2285 if ((buffer = stream->buffer))
2287 buffer_vk = wined3d_buffer_vk(buffer);
2288 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2289 wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
2290 buffers[count] = buffer_info->buffer;
2291 if ((offsets[count] = stream->offset) == ~0u)
2293 FIXME("Appending to stream output buffers not implemented.\n");
2294 offsets[count] = 0;
2296 sizes[count] = buffer_info->range - offsets[count];
2297 offsets[count] += buffer_info->offset;
2298 ++count;
2299 continue;
2302 if (count)
2303 VK_CALL(vkCmdBindTransformFeedbackBuffersEXT(vk_command_buffer, first, count, buffers, offsets, sizes));
2304 first = i + 1;
2305 count = 0;
2308 if (count)
2309 VK_CALL(vkCmdBindTransformFeedbackBuffersEXT(vk_command_buffer, first, count, buffers, offsets, sizes));
2312 static VkResult wined3d_context_vk_create_descriptor_pool(struct wined3d_device_vk *device_vk,
2313 const struct wined3d_vk_info *vk_info, VkDescriptorPool *vk_pool)
2315 struct VkDescriptorPoolCreateInfo pool_desc;
2316 VkResult vr;
2318 static const VkDescriptorPoolSize pool_sizes[] =
2320 {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1024},
2321 {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1024},
2322 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024},
2323 {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1024},
2324 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024},
2325 {VK_DESCRIPTOR_TYPE_SAMPLER, 1024},
2328 pool_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
2329 pool_desc.pNext = NULL;
2330 pool_desc.flags = 0;
2331 pool_desc.maxSets = 512;
2332 pool_desc.poolSizeCount = ARRAY_SIZE(pool_sizes);
2333 pool_desc.pPoolSizes = pool_sizes;
2335 if ((vr = VK_CALL(vkCreateDescriptorPool(device_vk->vk_device, &pool_desc, NULL, vk_pool))) < 0)
2336 ERR("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
2338 return vr;
2341 static VkResult wined3d_context_vk_create_descriptor_set(struct wined3d_context_vk *context_vk,
2342 VkDescriptorSetLayout vk_set_layout, VkDescriptorSet *vk_descriptor_set)
2344 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2345 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2346 struct VkDescriptorSetAllocateInfo set_desc;
2347 VkResult vr;
2349 if (!context_vk->vk_descriptor_pool && (vr = wined3d_context_vk_create_descriptor_pool(device_vk,
2350 vk_info, &context_vk->vk_descriptor_pool)))
2352 WARN("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
2353 return vr;
2356 set_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
2357 set_desc.pNext = NULL;
2358 set_desc.descriptorPool = context_vk->vk_descriptor_pool;
2359 set_desc.descriptorSetCount = 1;
2360 set_desc.pSetLayouts = &vk_set_layout;
2361 if ((vr = VK_CALL(vkAllocateDescriptorSets(device_vk->vk_device, &set_desc, vk_descriptor_set))) >= 0)
2362 return vr;
2364 if (vr == VK_ERROR_FRAGMENTED_POOL || vr == VK_ERROR_OUT_OF_POOL_MEMORY)
2366 wined3d_context_vk_destroy_descriptor_pool(context_vk,
2367 context_vk->vk_descriptor_pool, context_vk->current_command_buffer.id);
2368 context_vk->vk_descriptor_pool = VK_NULL_HANDLE;
2369 if ((vr = wined3d_context_vk_create_descriptor_pool(device_vk, vk_info, &context_vk->vk_descriptor_pool)))
2371 WARN("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
2372 return vr;
2375 set_desc.descriptorPool = context_vk->vk_descriptor_pool;
2376 if ((vr = VK_CALL(vkAllocateDescriptorSets(device_vk->vk_device, &set_desc, vk_descriptor_set))) >= 0)
2377 return vr;
2380 WARN("Failed to allocate descriptor set, vr %s.\n", wined3d_debug_vkresult(vr));
2382 return vr;
2385 static bool wined3d_shader_descriptor_writes_vk_add_write(struct wined3d_shader_descriptor_writes_vk *writes,
2386 VkDescriptorSet vk_descriptor_set, size_t binding_idx, VkDescriptorType type,
2387 const VkDescriptorBufferInfo *buffer_info, const VkDescriptorImageInfo *image_info,
2388 const VkBufferView *buffer_view)
2390 SIZE_T write_count = writes->count;
2391 VkWriteDescriptorSet *write;
2393 if (!wined3d_array_reserve((void **)&writes->writes, &writes->size,
2394 write_count + 1, sizeof(*writes->writes)))
2395 return false;
2397 write = &writes->writes[write_count];
2398 write->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
2399 write->pNext = NULL;
2400 write->dstSet = vk_descriptor_set;
2401 write->dstBinding = binding_idx;
2402 write->dstArrayElement = 0;
2403 write->descriptorCount = 1;
2404 write->descriptorType = type;
2405 write->pImageInfo = image_info;
2406 write->pBufferInfo = buffer_info;
2407 write->pTexelBufferView = buffer_view;
2409 ++writes->count;
2411 return true;
2414 static bool wined3d_shader_resource_bindings_add_null_srv_binding(struct wined3d_shader_descriptor_writes_vk *writes,
2415 VkDescriptorSet vk_descriptor_set, size_t binding_idx, enum wined3d_shader_resource_type type,
2416 enum wined3d_data_type data_type, struct wined3d_context_vk *context_vk)
2418 const struct wined3d_null_views_vk *v = &wined3d_device_vk(context_vk->c.device)->null_views_vk;
2420 switch (type)
2422 case WINED3D_SHADER_RESOURCE_BUFFER:
2423 if (data_type == WINED3D_DATA_FLOAT)
2424 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding_idx,
2425 VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, NULL, NULL, &v->vk_view_buffer_float);
2426 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding_idx,
2427 VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, NULL, NULL, &v->vk_view_buffer_uint);
2429 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2430 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2431 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_1d, NULL);
2433 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2434 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2435 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2d, NULL);
2437 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2438 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2439 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2dms, NULL);
2441 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2442 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2443 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_3d, NULL);
2445 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2446 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2447 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_cube, NULL);
2449 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2450 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2451 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2d_array, NULL);
2453 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2454 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2455 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2dms_array, NULL);
2457 default:
2458 FIXME("Unhandled resource type %#x.\n", type);
2459 return false;
2463 static bool wined3d_context_vk_update_descriptors(struct wined3d_context_vk *context_vk,
2464 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, enum wined3d_pipeline pipeline)
2466 struct wined3d_shader_descriptor_writes_vk *writes = &context_vk->descriptor_writes;
2467 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2468 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2469 const struct wined3d_shader_resource_binding *binding;
2470 struct wined3d_shader_resource_bindings *bindings;
2471 struct wined3d_unordered_access_view_vk *uav_vk;
2472 struct wined3d_shader_resource_view_vk *srv_vk;
2473 struct wined3d_unordered_access_view *uav;
2474 const VkDescriptorBufferInfo *buffer_info;
2475 struct wined3d_shader_resource_view *srv;
2476 const VkDescriptorImageInfo *image_info;
2477 struct wined3d_buffer_vk *buffer_vk;
2478 VkDescriptorSetLayout vk_set_layout;
2479 VkPipelineLayout vk_pipeline_layout;
2480 struct wined3d_resource *resource;
2481 VkPipelineBindPoint vk_bind_point;
2482 VkDescriptorSet vk_descriptor_set;
2483 struct wined3d_view_vk *view_vk;
2484 struct wined3d_sampler *sampler;
2485 struct wined3d_buffer *buffer;
2486 VkBufferView *buffer_view;
2487 VkDescriptorType type;
2488 VkResult vr;
2489 size_t i;
2491 switch (pipeline)
2493 case WINED3D_PIPELINE_GRAPHICS:
2494 bindings = &context_vk->graphics.bindings;
2495 vk_bind_point = VK_PIPELINE_BIND_POINT_GRAPHICS;
2496 vk_set_layout = context_vk->graphics.vk_set_layout;
2497 vk_pipeline_layout = context_vk->graphics.vk_pipeline_layout;
2498 break;
2500 case WINED3D_PIPELINE_COMPUTE:
2501 bindings = &context_vk->compute.bindings;
2502 vk_bind_point = VK_PIPELINE_BIND_POINT_COMPUTE;
2503 vk_set_layout = context_vk->compute.vk_set_layout;
2504 vk_pipeline_layout = context_vk->compute.vk_pipeline_layout;
2505 break;
2507 default:
2508 ERR("Invalid pipeline %#x.\n", pipeline);
2509 return false;
2512 if ((vr = wined3d_context_vk_create_descriptor_set(context_vk, vk_set_layout, &vk_descriptor_set)))
2514 WARN("Failed to create descriptor set, vr %s.\n", wined3d_debug_vkresult(vr));
2515 return false;
2518 writes->count = 0;
2519 for (i = 0; i < bindings->count; ++i)
2521 binding = &bindings->bindings[i];
2523 switch (binding->shader_descriptor_type)
2525 case WINED3D_SHADER_DESCRIPTOR_TYPE_CBV:
2526 if (!(buffer = state->cb[binding->shader_type][binding->resource_idx]))
2528 FIXME("NULL constant buffer views not implemented.\n");
2529 return false;
2531 buffer_vk = wined3d_buffer_vk(buffer);
2532 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2533 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2534 binding->binding_idx, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info, NULL, NULL))
2535 return false;
2536 wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
2537 break;
2539 case WINED3D_SHADER_DESCRIPTOR_TYPE_SRV:
2540 if (!(srv = state->shader_resource_view[binding->shader_type][binding->resource_idx]))
2542 if (!wined3d_shader_resource_bindings_add_null_srv_binding(writes, vk_descriptor_set,
2543 binding->binding_idx, binding->resource_type, binding->resource_data_type, context_vk))
2544 return false;
2545 break;
2547 resource = srv->resource;
2549 srv_vk = wined3d_shader_resource_view_vk(srv);
2550 view_vk = &srv_vk->view_vk;
2551 if (resource->type == WINED3D_RTYPE_BUFFER)
2553 image_info = NULL;
2554 buffer_view = &view_vk->u.vk_buffer_view;
2555 type = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
2557 else
2559 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture_from_resource(resource));
2561 if (view_vk->u.vk_image_info.imageView)
2562 image_info = &view_vk->u.vk_image_info;
2563 else
2564 image_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk);
2565 buffer_view = NULL;
2566 type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
2569 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2570 binding->binding_idx, type, NULL, image_info, buffer_view))
2571 return false;
2572 wined3d_context_vk_reference_shader_resource_view(context_vk, srv_vk);
2573 break;
2575 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV:
2576 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2578 FIXME("NULL unordered access views not implemented.\n");
2579 return false;
2581 resource = uav->resource;
2583 uav_vk = wined3d_unordered_access_view_vk(uav);
2584 view_vk = &uav_vk->view_vk;
2585 if (resource->type == WINED3D_RTYPE_BUFFER)
2587 image_info = NULL;
2588 buffer_view = &view_vk->u.vk_buffer_view;
2589 type = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
2591 else
2593 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture_from_resource(resource));
2595 if (view_vk->u.vk_image_info.imageView)
2596 image_info = &view_vk->u.vk_image_info;
2597 else
2598 image_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk);
2599 buffer_view = NULL;
2600 type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
2603 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2604 binding->binding_idx, type, NULL, image_info, buffer_view))
2605 return false;
2606 wined3d_context_vk_reference_unordered_access_view(context_vk, uav_vk);
2607 break;
2609 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV_COUNTER:
2610 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2612 FIXME("NULL unordered access view counters not implemented.\n");
2613 return false;
2616 uav_vk = wined3d_unordered_access_view_vk(uav);
2617 if (!uav_vk->vk_counter_view || !wined3d_shader_descriptor_writes_vk_add_write(writes,
2618 vk_descriptor_set, binding->binding_idx, VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,
2619 NULL, NULL, &uav_vk->vk_counter_view))
2620 return false;
2621 break;
2623 case WINED3D_SHADER_DESCRIPTOR_TYPE_SAMPLER:
2624 if (!(sampler = state->sampler[binding->shader_type][binding->resource_idx]))
2625 sampler = context_vk->c.device->null_sampler;
2626 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding->binding_idx,
2627 VK_DESCRIPTOR_TYPE_SAMPLER, NULL, &wined3d_sampler_vk(sampler)->vk_image_info, NULL))
2628 return false;
2629 wined3d_context_vk_reference_sampler(context_vk, wined3d_sampler_vk(sampler));
2630 break;
2632 default:
2633 ERR("Invalid descriptor type %#x.\n", binding->shader_descriptor_type);
2634 return false;
2638 VK_CALL(vkUpdateDescriptorSets(device_vk->vk_device, writes->count, writes->writes, 0, NULL));
2639 VK_CALL(vkCmdBindDescriptorSets(vk_command_buffer, vk_bind_point,
2640 vk_pipeline_layout, 0, 1, &vk_descriptor_set, 0, NULL));
2642 return true;
2645 static VkResult wined3d_context_vk_create_descriptor_set_layout(struct wined3d_device_vk *device_vk,
2646 const struct wined3d_vk_info *vk_info, const struct wined3d_pipeline_layout_key_vk *key,
2647 VkDescriptorSetLayout *vk_set_layout)
2649 VkDescriptorSetLayoutCreateInfo layout_desc;
2650 VkResult vr;
2652 layout_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
2653 layout_desc.pNext = NULL;
2654 layout_desc.flags = 0;
2655 layout_desc.bindingCount = key->binding_count;
2656 layout_desc.pBindings = key->bindings;
2658 if ((vr = VK_CALL(vkCreateDescriptorSetLayout(device_vk->vk_device, &layout_desc, NULL, vk_set_layout))) < 0)
2659 WARN("Failed to create Vulkan descriptor set layout, vr %s.\n", wined3d_debug_vkresult(vr));
2661 return vr;
2664 struct wined3d_pipeline_layout_vk *wined3d_context_vk_get_pipeline_layout(
2665 struct wined3d_context_vk *context_vk, VkDescriptorSetLayoutBinding *bindings, SIZE_T binding_count)
2667 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2668 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2669 struct wined3d_pipeline_layout_key_vk key;
2670 struct wined3d_pipeline_layout_vk *layout;
2671 VkPipelineLayoutCreateInfo layout_desc;
2672 struct wine_rb_entry *entry;
2673 VkResult vr;
2675 key.bindings = bindings;
2676 key.binding_count = binding_count;
2677 if ((entry = wine_rb_get(&context_vk->pipeline_layouts, &key)))
2678 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_pipeline_layout_vk, entry);
2680 if (!(layout = heap_alloc(sizeof(*layout))))
2681 return NULL;
2683 if (!(layout->key.bindings = heap_alloc(sizeof(*layout->key.bindings) * key.binding_count)))
2685 heap_free(layout);
2686 return NULL;
2688 memcpy(layout->key.bindings, key.bindings, sizeof(*layout->key.bindings) * key.binding_count);
2689 layout->key.binding_count = key.binding_count;
2691 if ((vr = wined3d_context_vk_create_descriptor_set_layout(device_vk, vk_info, &key, &layout->vk_set_layout)))
2693 WARN("Failed to create descriptor set layout, vr %s.\n", wined3d_debug_vkresult(vr));
2694 goto fail;
2697 layout_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
2698 layout_desc.pNext = NULL;
2699 layout_desc.flags = 0;
2700 layout_desc.setLayoutCount = 1;
2701 layout_desc.pSetLayouts = &layout->vk_set_layout;
2702 layout_desc.pushConstantRangeCount = 0;
2703 layout_desc.pPushConstantRanges = NULL;
2705 if ((vr = VK_CALL(vkCreatePipelineLayout(device_vk->vk_device,
2706 &layout_desc, NULL, &layout->vk_pipeline_layout))) < 0)
2708 WARN("Failed to create Vulkan pipeline layout, vr %s.\n", wined3d_debug_vkresult(vr));
2709 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
2710 goto fail;
2713 if (wine_rb_put(&context_vk->pipeline_layouts, &layout->key, &layout->entry) == -1)
2715 ERR("Failed to insert pipeline layout.\n");
2716 VK_CALL(vkDestroyPipelineLayout(device_vk->vk_device, layout->vk_pipeline_layout, NULL));
2717 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
2718 goto fail;
2721 return layout;
2723 fail:
2724 heap_free(layout->key.bindings);
2725 heap_free(layout);
2726 return NULL;
2729 static VkPipeline wined3d_context_vk_get_graphics_pipeline(struct wined3d_context_vk *context_vk)
2731 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2732 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2733 struct wined3d_graphics_pipeline_vk *pipeline_vk;
2734 struct wined3d_graphics_pipeline_key_vk *key;
2735 struct wine_rb_entry *entry;
2736 VkResult vr;
2738 key = &context_vk->graphics.pipeline_key_vk;
2739 if ((entry = wine_rb_get(&context_vk->graphics_pipelines, key)))
2740 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_graphics_pipeline_vk, entry)->vk_pipeline;
2742 if (!(pipeline_vk = heap_alloc(sizeof(*pipeline_vk))))
2743 return VK_NULL_HANDLE;
2744 pipeline_vk->key = *key;
2746 if ((vr = VK_CALL(vkCreateGraphicsPipelines(device_vk->vk_device,
2747 VK_NULL_HANDLE, 1, &key->pipeline_desc, NULL, &pipeline_vk->vk_pipeline))) < 0)
2749 WARN("Failed to create graphics pipeline, vr %s.\n", wined3d_debug_vkresult(vr));
2750 heap_free(pipeline_vk);
2751 return VK_NULL_HANDLE;
2754 if (wine_rb_put(&context_vk->graphics_pipelines, &pipeline_vk->key, &pipeline_vk->entry) == -1)
2755 ERR("Failed to insert pipeline.\n");
2757 return pipeline_vk->vk_pipeline;
2760 static void wined3d_context_vk_load_shader_resources(struct wined3d_context_vk *context_vk,
2761 const struct wined3d_state *state, enum wined3d_pipeline pipeline)
2763 struct wined3d_shader_descriptor_writes_vk *writes = &context_vk->descriptor_writes;
2764 const struct wined3d_shader_resource_bindings *bindings;
2765 const struct wined3d_shader_resource_binding *binding;
2766 struct wined3d_unordered_access_view_vk *uav_vk;
2767 struct wined3d_shader_resource_view_vk *srv_vk;
2768 struct wined3d_unordered_access_view *uav;
2769 struct wined3d_shader_resource_view *srv;
2770 struct wined3d_buffer_vk *buffer_vk;
2771 struct wined3d_sampler *sampler;
2772 struct wined3d_buffer *buffer;
2773 size_t i;
2775 switch (pipeline)
2777 case WINED3D_PIPELINE_GRAPHICS:
2778 bindings = &context_vk->graphics.bindings;
2779 break;
2781 case WINED3D_PIPELINE_COMPUTE:
2782 bindings = &context_vk->compute.bindings;
2783 break;
2785 default:
2786 ERR("Invalid pipeline %#x.\n", pipeline);
2787 return;
2790 writes->count = 0;
2791 for (i = 0; i < bindings->count; ++i)
2793 binding = &bindings->bindings[i];
2795 switch (binding->shader_descriptor_type)
2797 case WINED3D_SHADER_DESCRIPTOR_TYPE_CBV:
2798 if (!(buffer = state->cb[binding->shader_type][binding->resource_idx]))
2799 break;
2801 buffer_vk = wined3d_buffer_vk(buffer);
2802 wined3d_buffer_load(buffer, &context_vk->c, state);
2803 if (!buffer_vk->bo_user.valid)
2805 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
2806 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_CONSTANT_BUFFER(binding->shader_type));
2807 else
2808 context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_CONSTANT_BUFFER);
2810 break;
2812 case WINED3D_SHADER_DESCRIPTOR_TYPE_SRV:
2813 if (!(srv = state->shader_resource_view[binding->shader_type][binding->resource_idx]))
2814 break;
2816 srv_vk = wined3d_shader_resource_view_vk(srv);
2817 if (srv->resource->type == WINED3D_RTYPE_BUFFER)
2819 if (!srv_vk->view_vk.bo_user.valid)
2821 wined3d_shader_resource_view_vk_update(srv_vk, context_vk);
2822 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
2823 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
2824 else
2825 context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
2827 wined3d_buffer_load(buffer_from_resource(srv->resource), &context_vk->c, state);
2829 else
2831 wined3d_texture_load(texture_from_resource(srv->resource), &context_vk->c, FALSE);
2833 break;
2835 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV:
2836 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
2837 break;
2839 uav_vk = wined3d_unordered_access_view_vk(uav);
2840 if (uav->resource->type == WINED3D_RTYPE_BUFFER)
2842 if (!uav_vk->view_vk.bo_user.valid)
2844 wined3d_unordered_access_view_vk_update(uav_vk, context_vk);
2845 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
2846 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING);
2847 else
2848 context_invalidate_compute_state(&context_vk->c,
2849 STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING);
2851 wined3d_buffer_load(buffer_from_resource(uav->resource), &context_vk->c, state);
2852 wined3d_unordered_access_view_invalidate_location(uav, ~WINED3D_LOCATION_BUFFER);
2854 else
2856 wined3d_texture_load(texture_from_resource(uav->resource), &context_vk->c, FALSE);
2857 wined3d_unordered_access_view_invalidate_location(uav, ~WINED3D_LOCATION_TEXTURE_RGB);
2859 break;
2861 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV_COUNTER:
2862 break;
2864 case WINED3D_SHADER_DESCRIPTOR_TYPE_SAMPLER:
2865 if (!(sampler = state->sampler[binding->shader_type][binding->resource_idx]))
2866 sampler = context_vk->c.device->null_sampler;
2867 break;
2869 default:
2870 ERR("Invalid descriptor type %#x.\n", binding->shader_descriptor_type);
2871 break;
2876 VkCommandBuffer wined3d_context_vk_apply_draw_state(struct wined3d_context_vk *context_vk,
2877 const struct wined3d_state *state, struct wined3d_buffer_vk *indirect_vk, bool indexed)
2879 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2880 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2881 struct wined3d_rendertarget_view *dsv;
2882 VkSampleCountFlagBits sample_count;
2883 VkCommandBuffer vk_command_buffer;
2884 struct wined3d_buffer *buffer;
2885 unsigned int i;
2887 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL))
2888 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2889 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL);
2890 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
2891 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_VERTEX);
2892 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY)))
2893 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_GEOMETRY);
2894 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_HULL)))
2895 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_HULL) | (1u << WINED3D_SHADER_TYPE_DOMAIN);
2896 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN)))
2897 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_DOMAIN);
2899 context_vk->sample_count = 0;
2900 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
2902 struct wined3d_rendertarget_view *rtv;
2904 if (!(rtv = state->fb.render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
2905 continue;
2907 if (wined3d_blend_state_get_writemask(state->blend_state, i))
2909 wined3d_rendertarget_view_load_location(rtv, &context_vk->c, rtv->resource->draw_binding);
2910 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
2912 else
2914 wined3d_rendertarget_view_prepare_location(rtv, &context_vk->c, rtv->resource->draw_binding);
2917 sample_count = max(1, wined3d_resource_get_sample_count(rtv->resource));
2918 if (!context_vk->sample_count)
2919 context_vk->sample_count = sample_count;
2920 else if (context_vk->sample_count != sample_count)
2921 FIXME("Inconsistent sample counts (%u != %u).\n", context_vk->sample_count, sample_count);
2924 if ((dsv = state->fb.depth_stencil))
2926 if (wined3d_state_uses_depth_buffer(state))
2927 wined3d_rendertarget_view_load_location(dsv, &context_vk->c, dsv->resource->draw_binding);
2928 else
2929 wined3d_rendertarget_view_prepare_location(dsv, &context_vk->c, dsv->resource->draw_binding);
2930 if (!state->depth_stencil_state || state->depth_stencil_state->desc.depth_write)
2931 wined3d_rendertarget_view_invalidate_location(dsv, ~dsv->resource->draw_binding);
2933 sample_count = max(1, wined3d_resource_get_sample_count(dsv->resource));
2934 if (!context_vk->sample_count)
2935 context_vk->sample_count = sample_count;
2936 else if (context_vk->sample_count != sample_count)
2937 FIXME("Inconsistent sample counts (%u != %u).\n", context_vk->sample_count, sample_count);
2940 if (!context_vk->sample_count)
2941 context_vk->sample_count = VK_SAMPLE_COUNT_1_BIT;
2942 if (context_vk->c.shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
2944 device_vk->d.shader_backend->shader_select(device_vk->d.shader_priv, &context_vk->c, state);
2945 if (!context_vk->graphics.vk_pipeline_layout)
2947 ERR("No pipeline layout set.\n");
2948 return VK_NULL_HANDLE;
2950 context_vk->c.update_shader_resource_bindings = 1;
2951 context_vk->c.update_unordered_access_view_bindings = 1;
2954 wined3d_context_vk_load_shader_resources(context_vk, state, WINED3D_PIPELINE_GRAPHICS);
2956 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
2958 if (!(buffer = state->streams[i].buffer))
2959 continue;
2961 wined3d_buffer_load(buffer, &context_vk->c, state);
2962 if (!wined3d_buffer_vk(buffer)->bo_user.valid)
2963 context_invalidate_state(&context_vk->c, STATE_STREAMSRC);
2966 if (use_transform_feedback(state) && vk_info->supported[WINED3D_VK_EXT_TRANSFORM_FEEDBACK])
2968 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
2970 if (!(buffer = state->stream_output[i].buffer))
2971 continue;
2973 wined3d_buffer_load(buffer, &context_vk->c, state);
2974 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2975 if (!wined3d_buffer_vk(buffer)->bo_user.valid)
2976 context_vk->update_stream_output = 1;
2978 context_vk->c.transform_feedback_active = 1;
2981 if (indexed || (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_INDEXBUFFER) && state->index_buffer))
2983 wined3d_buffer_load(state->index_buffer, &context_vk->c, state);
2984 if (!wined3d_buffer_vk(state->index_buffer)->bo_user.valid)
2985 context_invalidate_state(&context_vk->c, STATE_INDEXBUFFER);
2988 if (indirect_vk)
2989 wined3d_buffer_load(&indirect_vk->b, &context_vk->c, state);
2991 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
2993 ERR("Failed to get command buffer.\n");
2994 return VK_NULL_HANDLE;
2997 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2998 wined3d_context_vk_end_current_render_pass(context_vk);
2999 if (!wined3d_context_vk_begin_render_pass(context_vk, vk_command_buffer, state, vk_info))
3001 ERR("Failed to begin render pass.\n");
3002 return VK_NULL_HANDLE;
3005 if (wined3d_context_vk_update_graphics_pipeline_key(context_vk, state, context_vk->graphics.vk_pipeline_layout)
3006 || !context_vk->graphics.vk_pipeline)
3008 if (!(context_vk->graphics.vk_pipeline = wined3d_context_vk_get_graphics_pipeline(context_vk)))
3010 ERR("Failed to get graphics pipeline.\n");
3011 return VK_NULL_HANDLE;
3014 VK_CALL(vkCmdBindPipeline(vk_command_buffer,
3015 VK_PIPELINE_BIND_POINT_GRAPHICS, context_vk->graphics.vk_pipeline));
3018 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAMSRC))
3019 wined3d_context_vk_bind_vertex_buffers(context_vk, vk_command_buffer, state, vk_info);
3021 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAM_OUTPUT))
3023 context_vk->update_stream_output = 1;
3024 context_vk->c.transform_feedback_paused = 0;
3026 if (context_vk->c.transform_feedback_active && context_vk->update_stream_output)
3028 wined3d_context_vk_bind_stream_output_buffers(context_vk, vk_command_buffer, state, vk_info);
3029 context_vk->update_stream_output = 0;
3032 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_INDEXBUFFER) && state->index_buffer)
3034 struct wined3d_buffer_vk *buffer_vk = wined3d_buffer_vk(state->index_buffer);
3035 const VkDescriptorBufferInfo *buffer_info;
3036 VkIndexType idx_type;
3038 if (state->index_format == WINED3DFMT_R16_UINT)
3039 idx_type = VK_INDEX_TYPE_UINT16;
3040 else
3041 idx_type = VK_INDEX_TYPE_UINT32;
3042 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
3043 wined3d_context_vk_reference_bo(context_vk, &buffer_vk->bo);
3044 VK_CALL(vkCmdBindIndexBuffer(vk_command_buffer, buffer_info->buffer,
3045 buffer_info->offset + state->index_offset, idx_type));
3048 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL))
3049 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3050 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX))
3051 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3052 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY))
3053 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3054 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL))
3055 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3056 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN))
3057 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING))
3058 context_vk->c.update_shader_resource_bindings = 1;
3059 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING))
3060 context_vk->c.update_unordered_access_view_bindings = 1;
3062 if (context_vk->c.update_shader_resource_bindings || context_vk->c.update_unordered_access_view_bindings)
3064 if (!wined3d_context_vk_update_descriptors(context_vk, vk_command_buffer, state, WINED3D_PIPELINE_GRAPHICS))
3066 ERR("Failed to update shader descriptors.\n");
3067 return VK_NULL_HANDLE;
3070 context_vk->c.update_shader_resource_bindings = 0;
3071 context_vk->c.update_unordered_access_view_bindings = 0;
3074 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_BLEND_FACTOR))
3075 VK_CALL(vkCmdSetBlendConstants(vk_command_buffer, &state->blend_factor.r));
3077 memset(context_vk->c.dirty_graphics_states, 0, sizeof(context_vk->c.dirty_graphics_states));
3078 context_vk->c.shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3080 return vk_command_buffer;
3083 VkCommandBuffer wined3d_context_vk_apply_compute_state(struct wined3d_context_vk *context_vk,
3084 const struct wined3d_state *state, struct wined3d_buffer_vk *indirect_vk)
3086 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
3087 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
3088 VkCommandBuffer vk_command_buffer;
3090 wined3d_context_vk_end_current_render_pass(context_vk);
3092 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_SHADER))
3093 context_vk->c.shader_update_mask |= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3095 if (context_vk->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
3097 device_vk->d.shader_backend->shader_select_compute(device_vk->d.shader_priv, &context_vk->c, state);
3098 if (!context_vk->compute.vk_pipeline)
3100 ERR("No compute pipeline set.\n");
3101 return VK_NULL_HANDLE;
3103 context_vk->c.update_compute_shader_resource_bindings = 1;
3104 context_vk->c.update_compute_unordered_access_view_bindings = 1;
3105 context_vk->update_compute_pipeline = 1;
3108 wined3d_context_vk_load_shader_resources(context_vk, state, WINED3D_PIPELINE_COMPUTE);
3110 if (indirect_vk)
3111 wined3d_buffer_load_location(&indirect_vk->b, &context_vk->c, WINED3D_LOCATION_BUFFER);
3113 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
3115 ERR("Failed to get command buffer.\n");
3116 return VK_NULL_HANDLE;
3119 if (context_vk->update_compute_pipeline)
3121 VK_CALL(vkCmdBindPipeline(vk_command_buffer,
3122 VK_PIPELINE_BIND_POINT_COMPUTE, context_vk->compute.vk_pipeline));
3123 context_vk->update_compute_pipeline = 0;
3126 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_CONSTANT_BUFFER)
3127 || wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING))
3128 context_vk->c.update_compute_shader_resource_bindings = 1;
3129 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING))
3130 context_vk->c.update_compute_unordered_access_view_bindings = 1;
3132 if (context_vk->c.update_compute_shader_resource_bindings
3133 || context_vk->c.update_compute_unordered_access_view_bindings)
3135 if (!wined3d_context_vk_update_descriptors(context_vk, vk_command_buffer, state, WINED3D_PIPELINE_COMPUTE))
3137 ERR("Failed to update shader descriptors.\n");
3138 return VK_NULL_HANDLE;
3141 context_vk->c.update_compute_shader_resource_bindings = 0;
3142 context_vk->c.update_compute_unordered_access_view_bindings = 0;
3145 memset(context_vk->c.dirty_compute_states, 0, sizeof(context_vk->c.dirty_compute_states));
3146 context_vk->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
3148 return vk_command_buffer;
3151 HRESULT wined3d_context_vk_init(struct wined3d_context_vk *context_vk, struct wined3d_swapchain *swapchain)
3153 VkCommandPoolCreateInfo command_pool_info;
3154 const struct wined3d_vk_info *vk_info;
3155 struct wined3d_adapter_vk *adapter_vk;
3156 struct wined3d_device_vk *device_vk;
3157 VkResult vr;
3159 TRACE("context_vk %p, swapchain %p.\n", context_vk, swapchain);
3161 memset(context_vk, 0, sizeof(*context_vk));
3162 wined3d_context_init(&context_vk->c, swapchain);
3163 device_vk = wined3d_device_vk(swapchain->device);
3164 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
3165 context_vk->vk_info = vk_info = &adapter_vk->vk_info;
3167 command_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
3168 command_pool_info.pNext = NULL;
3169 command_pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
3170 command_pool_info.queueFamilyIndex = device_vk->vk_queue_family_index;
3171 if ((vr = VK_CALL(vkCreateCommandPool(device_vk->vk_device,
3172 &command_pool_info, NULL, &context_vk->vk_command_pool))) < 0)
3174 ERR("Failed to create Vulkan command pool, vr %s.\n", wined3d_debug_vkresult(vr));
3175 wined3d_context_cleanup(&context_vk->c);
3176 return E_FAIL;
3178 context_vk->current_command_buffer.id = 1;
3180 wined3d_context_vk_init_graphics_pipeline_key(context_vk);
3182 list_init(&context_vk->active_queries);
3183 list_init(&context_vk->free_occlusion_query_pools);
3184 list_init(&context_vk->free_timestamp_query_pools);
3185 list_init(&context_vk->free_pipeline_statistics_query_pools);
3186 list_init(&context_vk->free_stream_output_statistics_query_pools);
3188 wine_rb_init(&context_vk->render_passes, wined3d_render_pass_vk_compare);
3189 wine_rb_init(&context_vk->pipeline_layouts, wined3d_pipeline_layout_vk_compare);
3190 wine_rb_init(&context_vk->graphics_pipelines, wined3d_graphics_pipeline_vk_compare);
3191 wine_rb_init(&context_vk->bo_slab_available, wined3d_bo_slab_vk_compare);
3193 return WINED3D_OK;