wined3d: Remove BO users from the list when invalidating them.
[wine.git] / dlls / wined3d / context_vk.c
blob932d2f5aa779cd315ef2ca04d2f72439bd3ed071
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 "wined3d_private.h"
26 #include "wined3d_vk.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30 VkCompareOp vk_compare_op_from_wined3d(enum wined3d_cmp_func op)
32 switch (op)
34 case WINED3D_CMP_NEVER:
35 return VK_COMPARE_OP_NEVER;
36 case WINED3D_CMP_LESS:
37 return VK_COMPARE_OP_LESS;
38 case WINED3D_CMP_EQUAL:
39 return VK_COMPARE_OP_EQUAL;
40 case WINED3D_CMP_LESSEQUAL:
41 return VK_COMPARE_OP_LESS_OR_EQUAL;
42 case WINED3D_CMP_GREATER:
43 return VK_COMPARE_OP_GREATER;
44 case WINED3D_CMP_NOTEQUAL:
45 return VK_COMPARE_OP_NOT_EQUAL;
46 case WINED3D_CMP_GREATEREQUAL:
47 return VK_COMPARE_OP_GREATER_OR_EQUAL;
48 case WINED3D_CMP_ALWAYS:
49 return VK_COMPARE_OP_ALWAYS;
50 default:
51 if (!op)
52 WARN("Unhandled compare operation %#x.\n", op);
53 else
54 FIXME("Unhandled compare operation %#x.\n", op);
55 return VK_COMPARE_OP_NEVER;
59 VkShaderStageFlagBits vk_shader_stage_from_wined3d(enum wined3d_shader_type shader_type)
61 switch (shader_type)
63 case WINED3D_SHADER_TYPE_VERTEX:
64 return VK_SHADER_STAGE_VERTEX_BIT;
65 case WINED3D_SHADER_TYPE_HULL:
66 return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
67 case WINED3D_SHADER_TYPE_DOMAIN:
68 return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
69 case WINED3D_SHADER_TYPE_GEOMETRY:
70 return VK_SHADER_STAGE_GEOMETRY_BIT;
71 case WINED3D_SHADER_TYPE_PIXEL:
72 return VK_SHADER_STAGE_FRAGMENT_BIT;
73 case WINED3D_SHADER_TYPE_COMPUTE:
74 return VK_SHADER_STAGE_COMPUTE_BIT;
75 default:
76 ERR("Unhandled shader type %s.\n", debug_shader_type(shader_type));
77 return 0;
81 static VkBlendFactor vk_blend_factor_from_wined3d(enum wined3d_blend blend,
82 const struct wined3d_format *dst_format, bool alpha)
84 switch (blend)
86 case WINED3D_BLEND_ZERO:
87 return VK_BLEND_FACTOR_ZERO;
88 case WINED3D_BLEND_ONE:
89 return VK_BLEND_FACTOR_ONE;
90 case WINED3D_BLEND_SRCCOLOR:
91 return VK_BLEND_FACTOR_SRC_COLOR;
92 case WINED3D_BLEND_INVSRCCOLOR:
93 return VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR;
94 case WINED3D_BLEND_SRCALPHA:
95 return VK_BLEND_FACTOR_SRC_ALPHA;
96 case WINED3D_BLEND_INVSRCALPHA:
97 return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
98 case WINED3D_BLEND_DESTALPHA:
99 if (dst_format->alpha_size)
100 return VK_BLEND_FACTOR_DST_ALPHA;
101 return VK_BLEND_FACTOR_ONE;
102 case WINED3D_BLEND_INVDESTALPHA:
103 if (dst_format->alpha_size)
104 return VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA;
105 return VK_BLEND_FACTOR_ZERO;
106 case WINED3D_BLEND_DESTCOLOR:
107 return VK_BLEND_FACTOR_DST_COLOR;
108 case WINED3D_BLEND_INVDESTCOLOR:
109 return VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;
110 case WINED3D_BLEND_SRCALPHASAT:
111 return VK_BLEND_FACTOR_SRC_ALPHA_SATURATE;
112 case WINED3D_BLEND_BLENDFACTOR:
113 if (alpha)
114 return VK_BLEND_FACTOR_CONSTANT_ALPHA;
115 return VK_BLEND_FACTOR_CONSTANT_COLOR;
116 case WINED3D_BLEND_INVBLENDFACTOR:
117 if (alpha)
118 return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA;
119 return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR;
120 case WINED3D_BLEND_SRC1COLOR:
121 return VK_BLEND_FACTOR_SRC1_COLOR;
122 case WINED3D_BLEND_INVSRC1COLOR:
123 return VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR;
124 case WINED3D_BLEND_SRC1ALPHA:
125 return VK_BLEND_FACTOR_SRC1_ALPHA;
126 case WINED3D_BLEND_INVSRC1ALPHA:
127 return VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA;
128 default:
129 FIXME("Unhandled blend %#x.\n", blend);
130 return VK_BLEND_FACTOR_ZERO;
134 static VkBlendOp vk_blend_op_from_wined3d(enum wined3d_blend_op op)
136 switch (op)
138 case WINED3D_BLEND_OP_ADD:
139 return VK_BLEND_OP_ADD;
140 case WINED3D_BLEND_OP_SUBTRACT:
141 return VK_BLEND_OP_SUBTRACT;
142 case WINED3D_BLEND_OP_REVSUBTRACT:
143 return VK_BLEND_OP_REVERSE_SUBTRACT;
144 case WINED3D_BLEND_OP_MIN:
145 return VK_BLEND_OP_MIN;
146 case WINED3D_BLEND_OP_MAX:
147 return VK_BLEND_OP_MAX;
148 default:
149 FIXME("Unhandled blend op %#x.\n", op);
150 return VK_BLEND_OP_ADD;
154 static VkColorComponentFlags vk_colour_write_mask_from_wined3d(uint32_t wined3d_mask)
156 VkColorComponentFlags vk_mask = 0;
158 if (wined3d_mask & WINED3DCOLORWRITEENABLE_RED)
159 vk_mask |= VK_COLOR_COMPONENT_R_BIT;
160 if (wined3d_mask & WINED3DCOLORWRITEENABLE_GREEN)
161 vk_mask |= VK_COLOR_COMPONENT_G_BIT;
162 if (wined3d_mask & WINED3DCOLORWRITEENABLE_BLUE)
163 vk_mask |= VK_COLOR_COMPONENT_B_BIT;
164 if (wined3d_mask & WINED3DCOLORWRITEENABLE_ALPHA)
165 vk_mask |= VK_COLOR_COMPONENT_A_BIT;
167 return vk_mask;
170 static VkCullModeFlags vk_cull_mode_from_wined3d(enum wined3d_cull mode)
172 switch (mode)
174 case WINED3D_CULL_NONE:
175 return VK_CULL_MODE_NONE;
176 case WINED3D_CULL_FRONT:
177 return VK_CULL_MODE_FRONT_BIT;
178 case WINED3D_CULL_BACK:
179 return VK_CULL_MODE_BACK_BIT;
180 default:
181 FIXME("Unhandled cull mode %#x.\n", mode);
182 return VK_CULL_MODE_NONE;
186 static VkPrimitiveTopology vk_topology_from_wined3d(enum wined3d_primitive_type t)
188 switch (t)
190 case WINED3D_PT_POINTLIST:
191 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
192 case WINED3D_PT_LINELIST:
193 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
194 case WINED3D_PT_LINESTRIP:
195 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
196 case WINED3D_PT_TRIANGLELIST:
197 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
198 case WINED3D_PT_TRIANGLESTRIP:
199 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
200 case WINED3D_PT_TRIANGLEFAN:
201 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
202 case WINED3D_PT_LINELIST_ADJ:
203 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
204 case WINED3D_PT_LINESTRIP_ADJ:
205 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY;
206 case WINED3D_PT_TRIANGLELIST_ADJ:
207 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY;
208 case WINED3D_PT_TRIANGLESTRIP_ADJ:
209 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY;
210 case WINED3D_PT_PATCH:
211 return VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
212 default:
213 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(t));
214 case WINED3D_PT_UNDEFINED:
215 return ~0u;
219 static VkStencilOp vk_stencil_op_from_wined3d(enum wined3d_stencil_op op)
221 switch (op)
223 case WINED3D_STENCIL_OP_KEEP:
224 return VK_STENCIL_OP_KEEP;
225 case WINED3D_STENCIL_OP_ZERO:
226 return VK_STENCIL_OP_ZERO;
227 case WINED3D_STENCIL_OP_REPLACE:
228 return VK_STENCIL_OP_REPLACE;
229 case WINED3D_STENCIL_OP_INCR_SAT:
230 return VK_STENCIL_OP_INCREMENT_AND_CLAMP;
231 case WINED3D_STENCIL_OP_DECR_SAT:
232 return VK_STENCIL_OP_DECREMENT_AND_CLAMP;
233 case WINED3D_STENCIL_OP_INVERT:
234 return VK_STENCIL_OP_INVERT;
235 case WINED3D_STENCIL_OP_INCR:
236 return VK_STENCIL_OP_INCREMENT_AND_WRAP;
237 case WINED3D_STENCIL_OP_DECR:
238 return VK_STENCIL_OP_DECREMENT_AND_WRAP;
239 default:
240 if (!op)
241 WARN("Unhandled stencil operation %#x.\n", op);
242 else
243 FIXME("Unhandled stencil operation %#x.\n", op);
244 return VK_STENCIL_OP_KEEP;
248 static bool wined3d_get_unused_stream_index(const struct wined3d_state *state, uint32_t *index)
250 uint32_t i;
252 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
254 if (!state->streams[i].buffer)
256 *index = i;
257 return true;
261 return false;
264 static void wined3d_allocator_chunk_vk_lock(struct wined3d_allocator_chunk_vk *chunk_vk)
266 wined3d_device_vk_allocator_lock(wined3d_device_vk_from_allocator(chunk_vk->c.allocator));
269 static void wined3d_allocator_chunk_vk_unlock(struct wined3d_allocator_chunk_vk *chunk_vk)
271 wined3d_device_vk_allocator_unlock(wined3d_device_vk_from_allocator(chunk_vk->c.allocator));
274 void *wined3d_allocator_chunk_vk_map(struct wined3d_allocator_chunk_vk *chunk_vk,
275 struct wined3d_context_vk *context_vk)
277 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
278 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
279 void *map_ptr;
280 VkResult vr;
282 TRACE("chunk %p, memory 0x%s, map_ptr %p.\n", chunk_vk,
283 wine_dbgstr_longlong(chunk_vk->vk_memory), chunk_vk->c.map_ptr);
285 wined3d_allocator_chunk_vk_lock(chunk_vk);
287 if (!chunk_vk->c.map_ptr)
289 if ((vr = VK_CALL(vkMapMemory(device_vk->vk_device,
290 chunk_vk->vk_memory, 0, VK_WHOLE_SIZE, 0, &chunk_vk->c.map_ptr))) < 0)
292 ERR("Failed to map chunk memory, vr %s.\n", wined3d_debug_vkresult(vr));
293 wined3d_allocator_chunk_vk_unlock(chunk_vk);
294 return NULL;
297 adapter_adjust_mapped_memory(device_vk->d.adapter, WINED3D_ALLOCATOR_CHUNK_SIZE);
300 ++chunk_vk->c.map_count;
301 map_ptr = chunk_vk->c.map_ptr;
303 wined3d_allocator_chunk_vk_unlock(chunk_vk);
305 return map_ptr;
308 void wined3d_allocator_chunk_vk_unmap(struct wined3d_allocator_chunk_vk *chunk_vk,
309 struct wined3d_context_vk *context_vk)
311 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
312 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
314 TRACE("chunk_vk %p, context_vk %p.\n", chunk_vk, context_vk);
316 wined3d_allocator_chunk_vk_lock(chunk_vk);
318 if (--chunk_vk->c.map_count)
320 wined3d_allocator_chunk_vk_unlock(chunk_vk);
321 return;
324 VK_CALL(vkUnmapMemory(device_vk->vk_device, chunk_vk->vk_memory));
325 chunk_vk->c.map_ptr = NULL;
327 wined3d_allocator_chunk_vk_unlock(chunk_vk);
329 adapter_adjust_mapped_memory(device_vk->d.adapter, -WINED3D_ALLOCATOR_CHUNK_SIZE);
332 VkDeviceMemory wined3d_context_vk_allocate_vram_chunk_memory(struct wined3d_context_vk *context_vk,
333 unsigned int pool, size_t size)
335 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
336 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
337 VkMemoryAllocateInfo allocate_info;
338 VkDeviceMemory vk_memory;
339 VkResult vr;
341 allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
342 allocate_info.pNext = NULL;
343 allocate_info.allocationSize = size;
344 allocate_info.memoryTypeIndex = pool;
345 if ((vr = VK_CALL(vkAllocateMemory(device_vk->vk_device, &allocate_info, NULL, &vk_memory))) < 0)
347 ERR("Failed to allocate memory, vr %s.\n", wined3d_debug_vkresult(vr));
348 return VK_NULL_HANDLE;
351 return vk_memory;
354 static struct wined3d_allocator_block *wined3d_context_vk_allocate_memory(struct wined3d_context_vk *context_vk,
355 unsigned int memory_type, VkDeviceSize size, VkDeviceMemory *vk_memory)
357 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
358 struct wined3d_allocator *allocator = &device_vk->allocator;
359 struct wined3d_allocator_block *block;
361 wined3d_device_vk_allocator_lock(device_vk);
363 if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
365 *vk_memory = wined3d_context_vk_allocate_vram_chunk_memory(context_vk, memory_type, size);
366 wined3d_device_vk_allocator_unlock(device_vk);
367 return NULL;
370 if (!(block = wined3d_allocator_allocate(allocator, &context_vk->c, memory_type, size)))
372 wined3d_device_vk_allocator_unlock(device_vk);
373 *vk_memory = VK_NULL_HANDLE;
374 return NULL;
377 *vk_memory = wined3d_allocator_chunk_vk(block->chunk)->vk_memory;
379 wined3d_device_vk_allocator_unlock(device_vk);
380 return block;
383 static void wined3d_context_vk_free_memory(struct wined3d_context_vk *context_vk, struct wined3d_allocator_block *block)
385 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
387 assert(block->chunk->allocator == &device_vk->allocator);
388 wined3d_device_vk_allocator_lock(device_vk);
389 wined3d_allocator_block_free(block);
390 wined3d_device_vk_allocator_unlock(device_vk);
393 static bool wined3d_context_vk_create_slab_bo(struct wined3d_context_vk *context_vk,
394 VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags memory_type, struct wined3d_bo_vk *bo)
396 const struct wined3d_adapter_vk *adapter_vk = wined3d_adapter_vk(context_vk->c.device->adapter);
397 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
398 const VkPhysicalDeviceLimits *limits = &adapter_vk->device_limits;
399 struct wined3d_bo_slab_vk_key key;
400 struct wined3d_bo_slab_vk *slab;
401 struct wine_rb_entry *entry;
402 size_t object_size, idx;
403 size_t alignment;
405 if (size > WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 2)
406 return false;
408 alignment = WINED3D_SLAB_BO_MIN_OBJECT_ALIGN;
409 if ((usage & (VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT))
410 && limits->minTexelBufferOffsetAlignment > alignment)
411 alignment = limits->minTexelBufferOffsetAlignment;
412 if ((usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) && limits->minUniformBufferOffsetAlignment)
413 alignment = limits->minUniformBufferOffsetAlignment;
414 if ((usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) && limits->minStorageBufferOffsetAlignment)
415 alignment = limits->minStorageBufferOffsetAlignment;
417 object_size = (size + (alignment - 1)) & ~(alignment - 1);
418 if (object_size < WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 32)
419 object_size = WINED3D_ALLOCATOR_MIN_BLOCK_SIZE / 32;
420 key.memory_type = memory_type;
421 key.usage = usage;
422 key.size = 32 * object_size;
424 wined3d_device_vk_allocator_lock(device_vk);
426 if ((entry = wine_rb_get(&context_vk->bo_slab_available, &key)))
428 slab = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
429 TRACE("Using existing bo slab %p.\n", slab);
431 else
433 if (!(slab = heap_alloc_zero(sizeof(*slab))))
435 wined3d_device_vk_allocator_unlock(device_vk);
436 ERR("Failed to allocate bo slab.\n");
437 return false;
440 if (wine_rb_put(&context_vk->bo_slab_available, &key, &slab->entry) < 0)
442 wined3d_device_vk_allocator_unlock(device_vk);
443 ERR("Failed to add slab to available tree.\n");
444 heap_free(slab);
445 return false;
448 slab->requested_memory_type = memory_type;
449 if (!wined3d_context_vk_create_bo(context_vk, key.size, usage, memory_type, &slab->bo))
451 wined3d_device_vk_allocator_unlock(device_vk);
452 ERR("Failed to create slab bo.\n");
453 wine_rb_remove(&context_vk->bo_slab_available, &slab->entry);
454 heap_free(slab);
455 return false;
457 slab->map = ~0u;
459 TRACE("Created new bo slab %p.\n", slab);
462 idx = wined3d_bit_scan(&slab->map);
463 if (!slab->map)
465 if (slab->next)
467 wine_rb_replace(&context_vk->bo_slab_available, &slab->entry, &slab->next->entry);
468 slab->next = NULL;
470 else
472 wine_rb_remove(&context_vk->bo_slab_available, &slab->entry);
476 wined3d_device_vk_allocator_unlock(device_vk);
478 *bo = slab->bo;
479 bo->memory = NULL;
480 bo->slab = slab;
481 bo->b.client_map_count = 0;
482 bo->b.map_ptr = NULL;
483 bo->b.buffer_offset = idx * object_size;
484 bo->b.memory_offset = slab->bo.b.memory_offset + bo->b.buffer_offset;
485 bo->size = size;
486 list_init(&bo->b.users);
487 bo->command_buffer_id = 0;
488 bo->host_synced = false;
490 TRACE("Using buffer 0x%s, memory 0x%s, offset 0x%s for bo %p.\n",
491 wine_dbgstr_longlong(bo->vk_buffer), wine_dbgstr_longlong(bo->vk_memory),
492 wine_dbgstr_longlong(bo->b.buffer_offset), bo);
494 return true;
497 BOOL wined3d_context_vk_create_bo(struct wined3d_context_vk *context_vk, VkDeviceSize size,
498 VkBufferUsageFlags usage, VkMemoryPropertyFlags memory_type, struct wined3d_bo_vk *bo)
500 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
501 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
502 VkMemoryRequirements memory_requirements;
503 struct wined3d_adapter_vk *adapter_vk;
504 VkBufferCreateInfo create_info;
505 unsigned int memory_type_idx;
506 VkResult vr;
508 if (wined3d_context_vk_create_slab_bo(context_vk, size, usage, memory_type, bo))
509 return TRUE;
511 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
513 create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
514 create_info.pNext = NULL;
515 create_info.flags = 0;
516 create_info.size = size;
517 create_info.usage = usage;
518 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
519 create_info.queueFamilyIndexCount = 0;
520 create_info.pQueueFamilyIndices = NULL;
522 if ((vr = VK_CALL(vkCreateBuffer(device_vk->vk_device, &create_info, NULL, &bo->vk_buffer))) < 0)
524 ERR("Failed to create Vulkan buffer, vr %s.\n", wined3d_debug_vkresult(vr));
525 return FALSE;
528 VK_CALL(vkGetBufferMemoryRequirements(device_vk->vk_device, bo->vk_buffer, &memory_requirements));
530 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
531 memory_requirements.memoryTypeBits, memory_type);
532 if (memory_type_idx == ~0u)
534 ERR("Failed to find suitable memory type.\n");
535 VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
536 return FALSE;
538 bo->memory = wined3d_context_vk_allocate_memory(context_vk,
539 memory_type_idx, memory_requirements.size, &bo->vk_memory);
540 if (!bo->vk_memory)
542 ERR("Failed to allocate buffer memory.\n");
543 VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
544 return FALSE;
546 bo->b.memory_offset = bo->memory ? bo->memory->offset : 0;
548 if ((vr = VK_CALL(vkBindBufferMemory(device_vk->vk_device, bo->vk_buffer,
549 bo->vk_memory, bo->b.memory_offset))) < 0)
551 ERR("Failed to bind buffer memory, vr %s.\n", wined3d_debug_vkresult(vr));
552 if (bo->memory)
553 wined3d_context_vk_free_memory(context_vk, bo->memory);
554 else
555 VK_CALL(vkFreeMemory(device_vk->vk_device, bo->vk_memory, NULL));
556 VK_CALL(vkDestroyBuffer(device_vk->vk_device, bo->vk_buffer, NULL));
557 return FALSE;
560 bo->b.client_map_count = 0;
561 bo->b.map_ptr = NULL;
562 bo->b.buffer_offset = 0;
563 bo->size = size;
564 bo->usage = usage;
565 bo->memory_type = adapter_vk->memory_properties.memoryTypes[memory_type_idx].propertyFlags;
566 bo->b.coherent = !!(bo->memory_type & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
567 list_init(&bo->b.users);
568 bo->command_buffer_id = 0;
569 bo->slab = NULL;
570 bo->host_synced = false;
572 TRACE("Created buffer 0x%s, memory 0x%s for bo %p.\n",
573 wine_dbgstr_longlong(bo->vk_buffer), wine_dbgstr_longlong(bo->vk_memory), bo);
575 return TRUE;
578 BOOL wined3d_context_vk_create_image(struct wined3d_context_vk *context_vk, VkImageType vk_image_type,
579 VkImageUsageFlags usage, VkFormat vk_format, unsigned int width, unsigned int height, unsigned int depth,
580 unsigned int sample_count, unsigned int mip_levels, unsigned int layer_count, unsigned int flags,
581 struct wined3d_image_vk *image)
583 struct wined3d_adapter_vk *adapter_vk = wined3d_adapter_vk(context_vk->c.device->adapter);
584 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
585 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
586 VkMemoryRequirements memory_requirements;
587 VkImageCreateInfo create_info;
588 unsigned int memory_type_idx;
589 VkResult vr;
591 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
592 create_info.pNext = NULL;
593 create_info.flags = flags;
594 create_info.imageType = vk_image_type;
595 create_info.format = vk_format;
596 create_info.extent.width = width;
597 create_info.extent.height = height;
598 create_info.extent.depth = depth;
599 create_info.mipLevels = mip_levels;
600 create_info.arrayLayers = layer_count;
601 create_info.samples = sample_count;
602 create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
603 create_info.usage = usage;
604 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
605 create_info.queueFamilyIndexCount = 0;
606 create_info.pQueueFamilyIndices = NULL;
607 create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
609 image->command_buffer_id = 0;
611 vr = VK_CALL(vkCreateImage(device_vk->vk_device, &create_info, NULL, &image->vk_image));
612 if (vr != VK_SUCCESS)
614 ERR("Failed to create image, vr %s.\n", wined3d_debug_vkresult(vr));
615 image->vk_image = VK_NULL_HANDLE;
616 return FALSE;
619 VK_CALL(vkGetImageMemoryRequirements(device_vk->vk_device, image->vk_image,
620 &memory_requirements));
622 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
623 memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
624 if (memory_type_idx == ~0u)
626 ERR("Failed to find suitable image memory type.\n");
627 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
628 image->vk_image = VK_NULL_HANDLE;
629 return FALSE;
632 image->memory = wined3d_context_vk_allocate_memory(context_vk, memory_type_idx,
633 memory_requirements.size, &image->vk_memory);
634 if (!image->vk_memory)
636 ERR("Failed to allocate image memory.\n");
637 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
638 image->vk_image = VK_NULL_HANDLE;
639 return FALSE;
642 vr = VK_CALL(vkBindImageMemory(device_vk->vk_device, image->vk_image, image->vk_memory,
643 image->memory ? image->memory->offset : 0));
644 if (vr != VK_SUCCESS)
646 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
647 if (image->memory)
648 wined3d_context_vk_free_memory(context_vk, image->memory);
649 else
650 VK_CALL(vkFreeMemory(device_vk->vk_device, image->vk_memory, NULL));
651 ERR("Failed to bind image memory, vr %s.\n", wined3d_debug_vkresult(vr));
652 image->memory = NULL;
653 image->vk_memory = VK_NULL_HANDLE;
654 image->vk_image = VK_NULL_HANDLE;
655 return FALSE;
658 return TRUE;
661 static struct wined3d_retired_object_vk *wined3d_context_vk_get_retired_object_vk(struct wined3d_context_vk *context_vk)
663 struct wined3d_retired_objects_vk *retired = &context_vk->retired;
664 struct wined3d_retired_object_vk *o;
666 if (retired->free)
668 o = retired->free;
669 retired->free = o->u.next;
670 return o;
673 if (!wined3d_array_reserve((void **)&retired->objects, &retired->size,
674 retired->count + 1, sizeof(*retired->objects)))
675 return NULL;
677 return &retired->objects[retired->count++];
680 void wined3d_context_vk_destroy_vk_framebuffer(struct wined3d_context_vk *context_vk,
681 VkFramebuffer vk_framebuffer, uint64_t command_buffer_id)
683 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
684 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
685 struct wined3d_retired_object_vk *o;
687 if (context_vk->completed_command_buffer_id >= command_buffer_id)
689 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, vk_framebuffer, NULL));
690 TRACE("Destroyed framebuffer 0x%s.\n", wine_dbgstr_longlong(vk_framebuffer));
691 return;
694 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
696 ERR("Leaking framebuffer 0x%s.\n", wine_dbgstr_longlong(vk_framebuffer));
697 return;
700 o->type = WINED3D_RETIRED_FRAMEBUFFER_VK;
701 o->u.vk_framebuffer = vk_framebuffer;
702 o->command_buffer_id = command_buffer_id;
705 static void wined3d_context_vk_return_vk_descriptor_pool(struct wined3d_context_vk *context_vk,
706 VkDescriptorPool vk_descriptor_pool)
708 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
709 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
711 if (!wined3d_array_reserve((void **)&context_vk->vk_descriptor_pools, &context_vk->vk_descriptor_pools_size,
712 context_vk->vk_descriptor_pool_count + 1, sizeof(*context_vk->vk_descriptor_pools)))
714 VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, vk_descriptor_pool, NULL));
715 TRACE("Destroying descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
716 return;
719 VK_CALL(vkResetDescriptorPool(device_vk->vk_device, vk_descriptor_pool, 0));
720 context_vk->vk_descriptor_pools[context_vk->vk_descriptor_pool_count++] = vk_descriptor_pool;
723 static void wined3d_context_vk_reset_vk_descriptor_pool(struct wined3d_context_vk *context_vk,
724 VkDescriptorPool vk_descriptor_pool, uint64_t command_buffer_id)
726 struct wined3d_retired_object_vk *o;
728 if (context_vk->completed_command_buffer_id >= command_buffer_id)
730 wined3d_context_vk_return_vk_descriptor_pool(context_vk, vk_descriptor_pool);
731 TRACE("Reset descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
732 return;
735 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
737 ERR("Leaking descriptor pool 0x%s.\n", wine_dbgstr_longlong(vk_descriptor_pool));
738 return;
741 o->type = WINED3D_RETIRED_DESCRIPTOR_POOL_VK;
742 o->u.vk_descriptor_pool = vk_descriptor_pool;
743 o->command_buffer_id = command_buffer_id;
746 void wined3d_context_vk_destroy_vk_memory(struct wined3d_context_vk *context_vk,
747 VkDeviceMemory vk_memory, 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(vkFreeMemory(device_vk->vk_device, vk_memory, NULL));
756 TRACE("Freed memory 0x%s.\n", wine_dbgstr_longlong(vk_memory));
757 return;
760 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
762 ERR("Leaking memory 0x%s.\n", wine_dbgstr_longlong(vk_memory));
763 return;
766 o->type = WINED3D_RETIRED_MEMORY_VK;
767 o->u.vk_memory = vk_memory;
768 o->command_buffer_id = command_buffer_id;
771 void wined3d_context_vk_destroy_allocator_block(struct wined3d_context_vk *context_vk,
772 struct wined3d_allocator_block *block, uint64_t command_buffer_id)
774 struct wined3d_retired_object_vk *o;
776 if (context_vk->completed_command_buffer_id >= command_buffer_id)
778 wined3d_context_vk_free_memory(context_vk, block);
779 TRACE("Freed block %p.\n", block);
780 return;
783 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
785 ERR("Leaking block %p.\n", block);
786 return;
789 o->type = WINED3D_RETIRED_ALLOCATOR_BLOCK_VK;
790 o->u.block = block;
791 o->command_buffer_id = command_buffer_id;
794 static void wined3d_bo_slab_vk_free_slice(struct wined3d_bo_slab_vk *slab,
795 SIZE_T idx, struct wined3d_context_vk *context_vk)
797 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
798 struct wined3d_bo_slab_vk_key key;
799 struct wine_rb_entry *entry;
801 TRACE("slab %p, idx %Iu, context_vk %p.\n", slab, idx, context_vk);
803 wined3d_device_vk_allocator_lock(device_vk);
805 if (!slab->map)
807 key.memory_type = slab->requested_memory_type;
808 key.usage = slab->bo.usage;
809 key.size = slab->bo.size;
811 if ((entry = wine_rb_get(&context_vk->bo_slab_available, &key)))
813 slab->next = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
814 wine_rb_replace(&context_vk->bo_slab_available, entry, &slab->entry);
816 else if (wine_rb_put(&context_vk->bo_slab_available, &key, &slab->entry) < 0)
818 ERR("Unable to return slab %p (map 0x%08x) to available tree.\n", slab, slab->map);
821 slab->map |= 1u << idx;
823 wined3d_device_vk_allocator_unlock(device_vk);
826 static void wined3d_context_vk_destroy_bo_slab_slice(struct wined3d_context_vk *context_vk,
827 struct wined3d_bo_slab_vk *slab, SIZE_T idx, uint64_t command_buffer_id)
829 struct wined3d_retired_object_vk *o;
831 if (context_vk->completed_command_buffer_id >= command_buffer_id)
833 wined3d_bo_slab_vk_free_slice(slab, idx, context_vk);
834 return;
837 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
839 ERR("Leaking slab %p, slice %#Ix.\n", slab, idx);
840 return;
843 o->type = WINED3D_RETIRED_BO_SLAB_SLICE_VK;
844 o->u.slice.slab = slab;
845 o->u.slice.idx = idx;
846 o->command_buffer_id = command_buffer_id;
849 static void wined3d_context_vk_destroy_vk_buffer(struct wined3d_context_vk *context_vk,
850 VkBuffer vk_buffer, uint64_t command_buffer_id)
852 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
853 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
854 struct wined3d_retired_object_vk *o;
856 if (context_vk->completed_command_buffer_id >= command_buffer_id)
858 VK_CALL(vkDestroyBuffer(device_vk->vk_device, vk_buffer, NULL));
859 TRACE("Destroyed buffer 0x%s.\n", wine_dbgstr_longlong(vk_buffer));
860 return;
863 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
865 ERR("Leaking buffer 0x%s.\n", wine_dbgstr_longlong(vk_buffer));
866 return;
869 o->type = WINED3D_RETIRED_BUFFER_VK;
870 o->u.vk_buffer = vk_buffer;
871 o->command_buffer_id = command_buffer_id;
874 void wined3d_context_vk_destroy_vk_image(struct wined3d_context_vk *context_vk,
875 VkImage vk_image, uint64_t command_buffer_id)
877 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
878 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
879 struct wined3d_retired_object_vk *o;
881 if (context_vk->completed_command_buffer_id >= command_buffer_id)
883 VK_CALL(vkDestroyImage(device_vk->vk_device, vk_image, NULL));
884 TRACE("Destroyed image 0x%s.\n", wine_dbgstr_longlong(vk_image));
885 return;
888 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
890 ERR("Leaking image 0x%s.\n", wine_dbgstr_longlong(vk_image));
891 return;
894 o->type = WINED3D_RETIRED_IMAGE_VK;
895 o->u.vk_image = vk_image;
896 o->command_buffer_id = command_buffer_id;
899 void wined3d_context_vk_destroy_vk_buffer_view(struct wined3d_context_vk *context_vk,
900 VkBufferView vk_view, uint64_t command_buffer_id)
902 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
903 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
904 struct wined3d_retired_object_vk *o;
906 if (context_vk->completed_command_buffer_id >= command_buffer_id)
908 VK_CALL(vkDestroyBufferView(device_vk->vk_device, vk_view, NULL));
909 TRACE("Destroyed buffer view 0x%s.\n", wine_dbgstr_longlong(vk_view));
910 return;
913 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
915 ERR("Leaking buffer view 0x%s.\n", wine_dbgstr_longlong(vk_view));
916 return;
919 o->type = WINED3D_RETIRED_BUFFER_VIEW_VK;
920 o->u.vk_buffer_view = vk_view;
921 o->command_buffer_id = command_buffer_id;
924 void wined3d_context_vk_destroy_vk_image_view(struct wined3d_context_vk *context_vk,
925 VkImageView vk_view, uint64_t command_buffer_id)
927 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
928 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
929 struct wined3d_retired_object_vk *o;
931 if (context_vk->completed_command_buffer_id >= command_buffer_id)
933 VK_CALL(vkDestroyImageView(device_vk->vk_device, vk_view, NULL));
934 TRACE("Destroyed image view 0x%s.\n", wine_dbgstr_longlong(vk_view));
935 return;
938 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
940 ERR("Leaking image view 0x%s.\n", wine_dbgstr_longlong(vk_view));
941 return;
944 o->type = WINED3D_RETIRED_IMAGE_VIEW_VK;
945 o->u.vk_image_view = vk_view;
946 o->command_buffer_id = command_buffer_id;
949 static void wined3d_context_vk_reset_completed_queries(struct wined3d_context_vk *context_vk,
950 struct wined3d_query_pool_vk *pool_vk, struct wined3d_command_buffer_vk *buffer)
952 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
953 struct wined3d_retired_object_vk *o;
954 struct wined3d_range range;
955 unsigned int start = 0;
957 for (;;)
959 if (!wined3d_bitmap_get_range(pool_vk->completed, WINED3D_QUERY_POOL_SIZE, start, &range))
960 break;
962 VK_CALL(vkCmdResetQueryPool(buffer->vk_command_buffer, pool_vk->vk_query_pool, range.offset, range.size));
964 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
966 ERR("Freeing query range %u+%u in pool %p.\n", range.offset, range.size, pool_vk);
967 wined3d_query_pool_vk_mark_free(context_vk, pool_vk, range.offset, range.size);
969 else
971 o->type = WINED3D_RETIRED_QUERY_POOL_VK;
972 o->u.queries.pool_vk = pool_vk;
973 o->u.queries.start = range.offset;
974 o->u.queries.count = range.size;
975 o->command_buffer_id = buffer->id;
978 start = range.offset + range.size;
981 memset(pool_vk->completed, 0, sizeof(pool_vk->completed));
984 void wined3d_context_vk_destroy_vk_pipeline(struct wined3d_context_vk *context_vk,
985 VkPipeline vk_pipeline, uint64_t command_buffer_id)
987 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
988 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
989 struct wined3d_retired_object_vk *o;
991 if (context_vk->completed_command_buffer_id >= command_buffer_id)
993 VK_CALL(vkDestroyPipeline(device_vk->vk_device, vk_pipeline, NULL));
994 TRACE("Destroyed pipeline 0x%s.\n", wine_dbgstr_longlong(vk_pipeline));
995 return;
998 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
1000 ERR("Leaking pipeline 0x%s.\n", wine_dbgstr_longlong(vk_pipeline));
1001 return;
1004 o->type = WINED3D_RETIRED_PIPELINE_VK;
1005 o->u.vk_pipeline = vk_pipeline;
1006 o->command_buffer_id = command_buffer_id;
1010 void wined3d_context_vk_destroy_vk_sampler(struct wined3d_context_vk *context_vk,
1011 VkSampler vk_sampler, uint64_t command_buffer_id)
1013 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1014 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1015 struct wined3d_retired_object_vk *o;
1017 if (context_vk->completed_command_buffer_id >= command_buffer_id)
1019 VK_CALL(vkDestroySampler(device_vk->vk_device, vk_sampler, NULL));
1020 TRACE("Destroyed sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
1021 return;
1024 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
1026 ERR("Leaking sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
1027 return;
1030 o->type = WINED3D_RETIRED_SAMPLER_VK;
1031 o->u.vk_sampler = vk_sampler;
1032 o->command_buffer_id = command_buffer_id;
1035 void wined3d_context_vk_destroy_vk_event(struct wined3d_context_vk *context_vk,
1036 VkEvent vk_event, uint64_t command_buffer_id)
1038 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1039 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1040 struct wined3d_retired_object_vk *o;
1042 if (context_vk->completed_command_buffer_id >= command_buffer_id)
1044 VK_CALL(vkDestroyEvent(device_vk->vk_device, vk_event, NULL));
1045 TRACE("Destroyed event 0x%s.\n", wine_dbgstr_longlong(vk_event));
1046 return;
1049 if (!(o = wined3d_context_vk_get_retired_object_vk(context_vk)))
1051 ERR("Leaking event 0x%s.\n", wine_dbgstr_longlong(vk_event));
1052 return;
1055 o->type = WINED3D_RETIRED_EVENT_VK;
1056 o->u.vk_event = vk_event;
1057 o->command_buffer_id = command_buffer_id;
1060 void wined3d_context_vk_destroy_image(struct wined3d_context_vk *context_vk, struct wined3d_image_vk *image)
1062 wined3d_context_vk_destroy_vk_image(context_vk, image->vk_image, image->command_buffer_id);
1063 if (image->memory)
1064 wined3d_context_vk_destroy_allocator_block(context_vk, image->memory,
1065 image->command_buffer_id);
1066 else
1067 wined3d_context_vk_destroy_vk_memory(context_vk, image->vk_memory, image->command_buffer_id);
1069 image->vk_image = VK_NULL_HANDLE;
1070 image->vk_memory = VK_NULL_HANDLE;
1071 image->memory = NULL;
1074 void wined3d_context_vk_destroy_bo(struct wined3d_context_vk *context_vk, const struct wined3d_bo_vk *bo)
1076 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1077 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1078 struct wined3d_bo_slab_vk *slab_vk;
1079 size_t object_size, idx;
1081 TRACE("context_vk %p, bo %p.\n", context_vk, bo);
1083 assert(list_empty(&bo->b.users));
1085 if (bo->command_buffer_id == context_vk->current_command_buffer.id)
1086 context_vk->retired_bo_size += bo->size;
1088 if ((slab_vk = bo->slab))
1090 if (bo->b.map_ptr)
1091 wined3d_bo_slab_vk_unmap(slab_vk, context_vk);
1092 object_size = slab_vk->bo.size / 32;
1093 idx = bo->b.buffer_offset / object_size;
1094 wined3d_context_vk_destroy_bo_slab_slice(context_vk, slab_vk, idx, bo->command_buffer_id);
1095 return;
1098 wined3d_context_vk_destroy_vk_buffer(context_vk, bo->vk_buffer, bo->command_buffer_id);
1099 if (bo->memory)
1101 if (bo->b.map_ptr)
1102 wined3d_allocator_chunk_vk_unmap(wined3d_allocator_chunk_vk(bo->memory->chunk), context_vk);
1103 wined3d_context_vk_destroy_allocator_block(context_vk, bo->memory, bo->command_buffer_id);
1104 return;
1107 if (bo->b.map_ptr)
1109 VK_CALL(vkUnmapMemory(device_vk->vk_device, bo->vk_memory));
1110 adapter_adjust_mapped_memory(device_vk->d.adapter, -bo->size);
1112 wined3d_context_vk_destroy_vk_memory(context_vk, bo->vk_memory, bo->command_buffer_id);
1115 static void free_command_buffer(struct wined3d_context_vk *context_vk, struct wined3d_command_buffer_vk *buffer)
1117 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1118 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1120 VK_CALL(vkDestroyFence(device_vk->vk_device, buffer->vk_fence, NULL));
1121 VK_CALL(vkFreeCommandBuffers(device_vk->vk_device,
1122 context_vk->vk_command_pool, 1, &buffer->vk_command_buffer));
1125 static void wined3d_context_vk_remove_command_buffer(struct wined3d_context_vk *context_vk,
1126 unsigned int submit_index)
1128 struct wined3d_command_buffer_vk *buffer = &context_vk->submitted.buffers[submit_index];
1130 if (buffer->id > context_vk->completed_command_buffer_id)
1131 context_vk->completed_command_buffer_id = buffer->id;
1133 if (wined3d_array_reserve((void **)&context_vk->completed.buffers, &context_vk->completed.buffers_size,
1134 context_vk->completed.buffer_count + 1, sizeof(*context_vk->completed.buffers)))
1135 context_vk->completed.buffers[context_vk->completed.buffer_count++] = *buffer;
1136 else
1137 free_command_buffer(context_vk, buffer);
1139 *buffer = context_vk->submitted.buffers[--context_vk->submitted.buffer_count];
1142 static void wined3d_context_vk_cleanup_resources(struct wined3d_context_vk *context_vk, VkFence vk_fence)
1144 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1145 struct wined3d_retired_objects_vk *retired = &context_vk->retired;
1146 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1147 struct wined3d_command_buffer_vk *buffer;
1148 struct wined3d_retired_object_vk *o;
1149 uint64_t command_buffer_id;
1150 SIZE_T i = 0;
1152 while (i < context_vk->submitted.buffer_count)
1154 buffer = &context_vk->submitted.buffers[i];
1155 if (VK_CALL(vkGetFenceStatus(device_vk->vk_device, buffer->vk_fence)) == VK_NOT_READY)
1157 ++i;
1158 continue;
1161 TRACE("Command buffer %p with id 0x%s has finished.\n",
1162 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1163 if (buffer->vk_fence == vk_fence)
1164 return;
1165 wined3d_context_vk_remove_command_buffer(context_vk, i);
1168 command_buffer_id = context_vk->completed_command_buffer_id;
1170 retired->free = NULL;
1171 for (i = retired->count; i; --i)
1173 o = &retired->objects[i - 1];
1175 if (o->type != WINED3D_RETIRED_FREE_VK && o->command_buffer_id > command_buffer_id)
1176 continue;
1178 switch (o->type)
1180 case WINED3D_RETIRED_FREE_VK:
1181 /* Nothing to do. */
1182 break;
1184 case WINED3D_RETIRED_FRAMEBUFFER_VK:
1185 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, o->u.vk_framebuffer, NULL));
1186 TRACE("Destroyed framebuffer 0x%s.\n", wine_dbgstr_longlong(o->u.vk_framebuffer));
1187 break;
1189 case WINED3D_RETIRED_DESCRIPTOR_POOL_VK:
1190 wined3d_context_vk_return_vk_descriptor_pool(context_vk, o->u.vk_descriptor_pool);
1191 TRACE("Reset descriptor pool 0x%s.\n", wine_dbgstr_longlong(o->u.vk_descriptor_pool));
1192 break;
1194 case WINED3D_RETIRED_MEMORY_VK:
1195 VK_CALL(vkFreeMemory(device_vk->vk_device, o->u.vk_memory, NULL));
1196 TRACE("Freed memory 0x%s.\n", wine_dbgstr_longlong(o->u.vk_memory));
1197 break;
1199 case WINED3D_RETIRED_ALLOCATOR_BLOCK_VK:
1200 TRACE("Destroying block %p.\n", o->u.block);
1201 wined3d_context_vk_free_memory(context_vk, o->u.block);
1202 break;
1204 case WINED3D_RETIRED_BO_SLAB_SLICE_VK:
1205 wined3d_bo_slab_vk_free_slice(o->u.slice.slab, o->u.slice.idx, context_vk);
1206 break;
1208 case WINED3D_RETIRED_BUFFER_VK:
1209 VK_CALL(vkDestroyBuffer(device_vk->vk_device, o->u.vk_buffer, NULL));
1210 TRACE("Destroyed buffer 0x%s.\n", wine_dbgstr_longlong(o->u.vk_buffer));
1211 break;
1213 case WINED3D_RETIRED_IMAGE_VK:
1214 VK_CALL(vkDestroyImage(device_vk->vk_device, o->u.vk_image, NULL));
1215 TRACE("Destroyed image 0x%s.\n", wine_dbgstr_longlong(o->u.vk_image));
1216 break;
1218 case WINED3D_RETIRED_BUFFER_VIEW_VK:
1219 VK_CALL(vkDestroyBufferView(device_vk->vk_device, o->u.vk_buffer_view, NULL));
1220 TRACE("Destroyed buffer view 0x%s.\n", wine_dbgstr_longlong(o->u.vk_buffer_view));
1221 break;
1223 case WINED3D_RETIRED_IMAGE_VIEW_VK:
1224 VK_CALL(vkDestroyImageView(device_vk->vk_device, o->u.vk_image_view, NULL));
1225 TRACE("Destroyed image view 0x%s.\n", wine_dbgstr_longlong(o->u.vk_image_view));
1226 break;
1228 case WINED3D_RETIRED_SAMPLER_VK:
1229 VK_CALL(vkDestroySampler(device_vk->vk_device, o->u.vk_sampler, NULL));
1230 TRACE("Destroyed sampler 0x%s.\n", wine_dbgstr_longlong(o->u.vk_sampler));
1231 break;
1233 case WINED3D_RETIRED_QUERY_POOL_VK:
1234 wined3d_query_pool_vk_mark_free(context_vk, o->u.queries.pool_vk, o->u.queries.start, o->u.queries.count);
1235 TRACE("Freed query range %u+%u in pool %p.\n", o->u.queries.start, o->u.queries.count, o->u.queries.pool_vk);
1236 break;
1238 case WINED3D_RETIRED_EVENT_VK:
1239 VK_CALL(vkDestroyEvent(device_vk->vk_device, o->u.vk_event, NULL));
1240 TRACE("Destroyed event 0x%s.\n", wine_dbgstr_longlong(o->u.vk_event));
1241 break;
1243 case WINED3D_RETIRED_PIPELINE_VK:
1244 VK_CALL(vkDestroyPipeline(device_vk->vk_device, o->u.vk_pipeline, NULL));
1245 TRACE("Destroyed pipeline 0x%s.\n", wine_dbgstr_longlong(o->u.vk_pipeline));
1246 break;
1248 default:
1249 ERR("Unhandled object type %#x.\n", o->type);
1250 break;
1253 if (i == retired->count)
1255 --retired->count;
1256 continue;
1259 o->type = WINED3D_RETIRED_FREE_VK;
1260 o->u.next = retired->free;
1261 retired->free = o;
1263 if (vk_fence && VK_CALL(vkGetFenceStatus(device_vk->vk_device, vk_fence)) != VK_NOT_READY)
1264 break;
1268 static void wined3d_context_vk_destroy_bo_slab(struct wine_rb_entry *entry, void *ctx)
1270 struct wined3d_context_vk *context_vk = ctx;
1271 struct wined3d_bo_slab_vk *slab, *next;
1273 slab = WINE_RB_ENTRY_VALUE(entry, struct wined3d_bo_slab_vk, entry);
1274 while (slab)
1276 next = slab->next;
1277 wined3d_context_vk_destroy_bo(context_vk, &slab->bo);
1278 heap_free(slab);
1279 slab = next;
1283 static void wined3d_context_vk_destroy_graphics_pipeline(struct wine_rb_entry *entry, void *ctx)
1285 struct wined3d_graphics_pipeline_vk *pipeline_vk = WINE_RB_ENTRY_VALUE(entry,
1286 struct wined3d_graphics_pipeline_vk, entry);
1287 struct wined3d_context_vk *context_vk = ctx;
1288 const struct wined3d_vk_info *vk_info;
1289 struct wined3d_device_vk *device_vk;
1291 vk_info = context_vk->vk_info;
1292 device_vk = wined3d_device_vk(context_vk->c.device);
1294 VK_CALL(vkDestroyPipeline(device_vk->vk_device, pipeline_vk->vk_pipeline, NULL));
1295 heap_free(pipeline_vk);
1298 static void wined3d_context_vk_destroy_pipeline_layout(struct wine_rb_entry *entry, void *ctx)
1300 struct wined3d_pipeline_layout_vk *layout = WINE_RB_ENTRY_VALUE(entry,
1301 struct wined3d_pipeline_layout_vk, entry);
1302 struct wined3d_context_vk *context_vk = ctx;
1303 const struct wined3d_vk_info *vk_info;
1304 struct wined3d_device_vk *device_vk;
1306 vk_info = context_vk->vk_info;
1307 device_vk = wined3d_device_vk(context_vk->c.device);
1309 VK_CALL(vkDestroyPipelineLayout(device_vk->vk_device, layout->vk_pipeline_layout, NULL));
1310 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
1311 heap_free(layout->key.bindings);
1312 heap_free(layout);
1315 static void wined3d_render_pass_key_vk_init(struct wined3d_render_pass_key_vk *key,
1316 const struct wined3d_fb_state *fb, unsigned int rt_count, bool depth_stencil, uint32_t clear_flags)
1318 struct wined3d_render_pass_attachment_vk *a;
1319 struct wined3d_rendertarget_view *view;
1320 unsigned int i;
1321 DWORD location;
1323 memset(key, 0, sizeof(*key));
1325 for (i = 0; i < rt_count; ++i)
1327 if (!(view = fb->render_targets[i]) || view->format->id == WINED3DFMT_NULL)
1328 continue;
1330 a = &key->rt[i];
1331 a->vk_format = wined3d_format_vk(view->format)->vk_format;
1332 a->vk_samples = max(1, wined3d_resource_get_sample_count(view->resource));
1333 a->vk_layout = wined3d_texture_vk(wined3d_texture_from_resource(view->resource))->layout;
1334 location = wined3d_rendertarget_view_get_locations(view);
1336 if (clear_flags & WINED3DCLEAR_TARGET)
1337 a->flags = WINED3D_FB_ATTACHMENT_FLAG_CLEAR_C;
1338 else if (location & WINED3D_LOCATION_DISCARDED)
1339 a->flags = WINED3D_FB_ATTACHMENT_FLAG_DISCARDED;
1340 else if (location & WINED3D_LOCATION_CLEARED)
1341 a->flags = WINED3D_FB_ATTACHMENT_FLAG_CLEAR_C;
1343 key->rt_mask |= 1u << i;
1346 if (depth_stencil && (view = fb->depth_stencil))
1348 a = &key->ds;
1349 a->vk_format = wined3d_format_vk(view->format)->vk_format;
1350 a->vk_samples = max(1, wined3d_resource_get_sample_count(view->resource));
1351 a->vk_layout = wined3d_texture_vk(wined3d_texture_from_resource(view->resource))->layout;
1352 location = wined3d_rendertarget_view_get_locations(view);
1353 key->rt_mask |= 1u << WINED3D_MAX_RENDER_TARGETS;
1355 if (clear_flags & WINED3DCLEAR_STENCIL)
1356 a->flags = WINED3D_FB_ATTACHMENT_FLAG_CLEAR_S;
1357 if (clear_flags & WINED3DCLEAR_ZBUFFER)
1358 a->flags |= WINED3D_FB_ATTACHMENT_FLAG_CLEAR_Z;
1360 if (!a->flags && (location & WINED3D_LOCATION_DISCARDED))
1361 a->flags = WINED3D_FB_ATTACHMENT_FLAG_DISCARDED;
1362 else if (location & WINED3D_LOCATION_CLEARED)
1363 a->flags = WINED3D_FB_ATTACHMENT_FLAG_CLEAR_S | WINED3D_FB_ATTACHMENT_FLAG_CLEAR_Z;
1367 static void wined3d_render_pass_vk_cleanup(struct wined3d_render_pass_vk *pass,
1368 struct wined3d_context_vk *context_vk)
1370 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1371 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1373 VK_CALL(vkDestroyRenderPass(device_vk->vk_device, pass->vk_render_pass, NULL));
1376 static bool wined3d_render_pass_vk_init(struct wined3d_render_pass_vk *pass,
1377 struct wined3d_context_vk *context_vk, const struct wined3d_render_pass_key_vk *key)
1379 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1380 VkAttachmentReference attachment_references[WINED3D_MAX_RENDER_TARGETS];
1381 VkAttachmentDescription attachments[WINED3D_MAX_RENDER_TARGETS + 1];
1382 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1383 const struct wined3d_render_pass_attachment_vk *a;
1384 VkAttachmentReference ds_attachment_reference;
1385 VkAttachmentReference *ds_reference = NULL;
1386 unsigned int attachment_count, rt_count, i;
1387 VkAttachmentDescription *attachment;
1388 VkSubpassDescription sub_pass_desc;
1389 VkRenderPassCreateInfo pass_desc;
1390 uint32_t mask;
1391 VkResult vr;
1393 rt_count = 0;
1394 attachment_count = 0;
1395 mask = key->rt_mask & wined3d_mask_from_size(WINED3D_MAX_RENDER_TARGETS);
1396 while (mask)
1398 i = wined3d_bit_scan(&mask);
1399 a = &key->rt[i];
1401 attachment = &attachments[attachment_count];
1402 attachment->flags = 0;
1403 attachment->format = a->vk_format;
1404 attachment->samples = a->vk_samples;
1406 if (a->flags & WINED3D_FB_ATTACHMENT_FLAG_DISCARDED)
1407 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1408 else if (a->flags & WINED3D_FB_ATTACHMENT_FLAG_CLEAR_C)
1409 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1410 else
1411 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1413 attachment->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1414 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1415 attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
1416 attachment->initialLayout = a->vk_layout;
1417 attachment->finalLayout = a->vk_layout;
1419 attachment_references[i].attachment = attachment_count;
1420 attachment_references[i].layout = a->vk_layout;
1422 ++attachment_count;
1423 rt_count = i + 1;
1426 mask = ~key->rt_mask & wined3d_mask_from_size(rt_count);
1427 while (mask)
1429 i = wined3d_bit_scan(&mask);
1430 attachment_references[i].attachment = VK_ATTACHMENT_UNUSED;
1431 attachment_references[i].layout = VK_IMAGE_LAYOUT_UNDEFINED;
1434 if (key->rt_mask & (1u << WINED3D_MAX_RENDER_TARGETS))
1436 a = &key->ds;
1438 attachment = &attachments[attachment_count];
1439 attachment->flags = 0;
1440 attachment->format = a->vk_format;
1441 attachment->samples = a->vk_samples;
1443 if (a->flags & WINED3D_FB_ATTACHMENT_FLAG_DISCARDED)
1444 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1445 else if (a->flags & WINED3D_FB_ATTACHMENT_FLAG_CLEAR_Z)
1446 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1447 else
1448 attachment->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1450 if (a->flags & WINED3D_FB_ATTACHMENT_FLAG_DISCARDED)
1451 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1452 else if (a->flags & WINED3D_FB_ATTACHMENT_FLAG_CLEAR_S)
1453 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1454 else
1455 attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
1457 attachment->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1458 attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
1459 attachment->initialLayout = a->vk_layout;
1460 attachment->finalLayout = a->vk_layout;
1462 ds_reference = &ds_attachment_reference;
1463 ds_reference->attachment = attachment_count;
1464 ds_reference->layout = a->vk_layout;
1466 ++attachment_count;
1469 sub_pass_desc.flags = 0;
1470 sub_pass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
1471 sub_pass_desc.inputAttachmentCount = 0;
1472 sub_pass_desc.pInputAttachments = NULL;
1473 sub_pass_desc.colorAttachmentCount = rt_count;
1474 sub_pass_desc.pColorAttachments = attachment_references;
1475 sub_pass_desc.pResolveAttachments = NULL;
1476 sub_pass_desc.pDepthStencilAttachment = ds_reference;
1477 sub_pass_desc.preserveAttachmentCount = 0;
1478 sub_pass_desc.pPreserveAttachments = NULL;
1480 pass_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
1481 pass_desc.pNext = NULL;
1482 pass_desc.flags = 0;
1483 pass_desc.attachmentCount = attachment_count;
1484 pass_desc.pAttachments = attachments;
1485 pass_desc.subpassCount = 1;
1486 pass_desc.pSubpasses = &sub_pass_desc;
1487 pass_desc.dependencyCount = 0;
1488 pass_desc.pDependencies = NULL;
1490 pass->key = *key;
1491 if ((vr = VK_CALL(vkCreateRenderPass(device_vk->vk_device,
1492 &pass_desc, NULL, &pass->vk_render_pass))) < 0)
1494 WARN("Failed to create Vulkan render pass, vr %d.\n", vr);
1495 return false;
1498 return true;
1501 VkRenderPass wined3d_context_vk_get_render_pass(struct wined3d_context_vk *context_vk,
1502 const struct wined3d_fb_state *fb, unsigned int rt_count, bool depth_stencil, uint32_t clear_flags)
1504 struct wined3d_render_pass_key_vk key;
1505 struct wined3d_render_pass_vk *pass;
1506 struct wine_rb_entry *entry;
1508 wined3d_render_pass_key_vk_init(&key, fb, rt_count, depth_stencil, clear_flags);
1509 if ((entry = wine_rb_get(&context_vk->render_passes, &key)))
1510 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_render_pass_vk, entry)->vk_render_pass;
1512 if (!(pass = heap_alloc(sizeof(*pass))))
1513 return VK_NULL_HANDLE;
1515 if (!wined3d_render_pass_vk_init(pass, context_vk, &key))
1517 heap_free(pass);
1518 return VK_NULL_HANDLE;
1521 if (wine_rb_put(&context_vk->render_passes, &pass->key, &pass->entry) == -1)
1523 ERR("Failed to insert render pass.\n");
1524 wined3d_render_pass_vk_cleanup(pass, context_vk);
1525 heap_free(pass);
1526 return VK_NULL_HANDLE;
1529 return pass->vk_render_pass;
1532 void wined3d_context_vk_end_current_render_pass(struct wined3d_context_vk *context_vk)
1534 VkCommandBuffer vk_command_buffer = context_vk->current_command_buffer.vk_command_buffer;
1535 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1536 struct wined3d_query_vk *query_vk;
1538 if (context_vk->vk_render_pass)
1540 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->render_pass_queries, struct wined3d_query_vk, entry)
1541 wined3d_query_vk_suspend(query_vk, context_vk);
1543 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
1544 context_vk->vk_render_pass = VK_NULL_HANDLE;
1545 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1546 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 0, NULL));
1548 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->render_pass_queries, struct wined3d_query_vk, entry)
1550 if (!wined3d_context_vk_allocate_query(context_vk, query_vk->q.type, &query_vk->pool_idx))
1552 ERR("Failed to allocate new query.\n");
1553 break;
1558 if (context_vk->vk_framebuffer)
1560 wined3d_context_vk_destroy_vk_framebuffer(context_vk,
1561 context_vk->vk_framebuffer, context_vk->current_command_buffer.id);
1562 context_vk->vk_framebuffer = VK_NULL_HANDLE;
1566 static void wined3d_context_vk_destroy_render_pass(struct wine_rb_entry *entry, void *ctx)
1568 struct wined3d_render_pass_vk *pass = WINE_RB_ENTRY_VALUE(entry,
1569 struct wined3d_render_pass_vk, entry);
1571 wined3d_render_pass_vk_cleanup(pass, ctx);
1572 heap_free(pass);
1575 static void wined3d_shader_descriptor_writes_vk_cleanup(struct wined3d_shader_descriptor_writes_vk *writes)
1577 heap_free(writes->writes);
1580 static void wined3d_context_vk_destroy_query_pools(struct wined3d_context_vk *context_vk, struct list *free_pools)
1582 struct wined3d_query_pool_vk *pool_vk, *entry;
1584 LIST_FOR_EACH_ENTRY_SAFE(pool_vk, entry, free_pools, struct wined3d_query_pool_vk, entry)
1586 wined3d_query_pool_vk_cleanup(pool_vk, context_vk);
1587 heap_free(pool_vk);
1591 bool wined3d_context_vk_allocate_query(struct wined3d_context_vk *context_vk,
1592 enum wined3d_query_type type, struct wined3d_query_pool_idx_vk *pool_idx)
1594 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1595 struct wined3d_query_pool_vk *pool_vk, *entry;
1596 struct wined3d_device_vk *device_vk;
1597 struct list *free_pools;
1598 VkResult vr;
1599 size_t idx;
1601 switch (type)
1603 case WINED3D_QUERY_TYPE_OCCLUSION:
1604 free_pools = &context_vk->free_occlusion_query_pools;
1605 break;
1607 case WINED3D_QUERY_TYPE_TIMESTAMP:
1608 free_pools = &context_vk->free_timestamp_query_pools;
1609 break;
1611 case WINED3D_QUERY_TYPE_PIPELINE_STATISTICS:
1612 free_pools = &context_vk->free_pipeline_statistics_query_pools;
1613 break;
1615 case WINED3D_QUERY_TYPE_SO_STATISTICS:
1616 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM0:
1617 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM1:
1618 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM2:
1619 case WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM3:
1620 free_pools = &context_vk->free_stream_output_statistics_query_pools;
1621 break;
1623 default:
1624 FIXME("Unhandled query type %#x.\n", type);
1625 return false;
1628 LIST_FOR_EACH_ENTRY_SAFE(pool_vk, entry, free_pools, struct wined3d_query_pool_vk, entry)
1630 if (wined3d_query_pool_vk_allocate_query(pool_vk, &idx))
1631 goto done;
1632 list_remove(&pool_vk->entry);
1633 list_init(&pool_vk->entry);
1636 if (!(pool_vk = heap_alloc_zero(sizeof(*pool_vk))))
1637 return false;
1638 if (!wined3d_query_pool_vk_init(pool_vk, context_vk, type, free_pools))
1640 heap_free(pool_vk);
1641 return false;
1644 device_vk = wined3d_device_vk(context_vk->c.device);
1646 if (vk_info->supported[WINED3D_VK_EXT_HOST_QUERY_RESET])
1648 VK_CALL(vkResetQueryPoolEXT(device_vk->vk_device,
1649 pool_vk->vk_query_pool, 0, WINED3D_QUERY_POOL_SIZE));
1651 else
1653 VkEventCreateInfo event_create_info;
1655 wined3d_context_vk_end_current_render_pass(context_vk);
1656 VK_CALL(vkCmdResetQueryPool(wined3d_context_vk_get_command_buffer(context_vk),
1657 pool_vk->vk_query_pool, 0, WINED3D_QUERY_POOL_SIZE));
1659 event_create_info.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
1660 event_create_info.pNext = NULL;
1661 event_create_info.flags = 0;
1663 /* We probably shouldn't call vkGetQueryPoolResults() without synchronizing with vkCmdResetQueryPool()
1664 * even if the query pool is freshly allocated. wined3d_query_vk_accumulate_data() will check this event
1665 * before returning results. */
1666 vr = VK_CALL(vkCreateEvent(device_vk->vk_device, &event_create_info, NULL, &pool_vk->vk_event));
1667 if (vr == VK_SUCCESS)
1669 /* At which stage vkCmdResetQueryPool() executes? */
1670 VK_CALL(vkCmdSetEvent(wined3d_context_vk_get_command_buffer(context_vk), pool_vk->vk_event,
1671 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT));
1673 else
1675 ERR("Failed to create event, vr %s.\n", wined3d_debug_vkresult(vr));
1679 if (!wined3d_query_pool_vk_allocate_query(pool_vk, &idx))
1681 wined3d_query_pool_vk_cleanup(pool_vk, context_vk);
1682 heap_free(pool_vk);
1683 return false;
1686 done:
1687 pool_idx->pool_vk = pool_vk;
1688 pool_idx->idx = idx;
1690 return true;
1693 void wined3d_context_vk_cleanup(struct wined3d_context_vk *context_vk)
1695 struct wined3d_command_buffer_vk *buffer = &context_vk->current_command_buffer;
1696 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1697 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1698 unsigned int i;
1700 if (buffer->vk_command_buffer)
1702 free_command_buffer(context_vk, buffer);
1703 buffer->vk_command_buffer = VK_NULL_HANDLE;
1706 wined3d_context_vk_wait_command_buffer(context_vk, buffer->id - 1);
1707 context_vk->completed_command_buffer_id = buffer->id;
1708 for (i = 0; i < context_vk->completed.buffer_count; ++i)
1709 free_command_buffer(context_vk, &context_vk->completed.buffers[i]);
1711 heap_free(context_vk->compute.bindings.bindings);
1712 heap_free(context_vk->graphics.bindings.bindings);
1713 for (i = 0; i < context_vk->vk_descriptor_pool_count; ++i)
1714 VK_CALL(vkDestroyDescriptorPool(device_vk->vk_device, context_vk->vk_descriptor_pools[i], NULL));
1715 heap_free(context_vk->vk_descriptor_pools);
1716 if (context_vk->vk_framebuffer)
1717 VK_CALL(vkDestroyFramebuffer(device_vk->vk_device, context_vk->vk_framebuffer, NULL));
1718 if (context_vk->vk_so_counter_bo.vk_buffer)
1719 wined3d_context_vk_destroy_bo(context_vk, &context_vk->vk_so_counter_bo);
1720 wined3d_context_vk_cleanup_resources(context_vk, VK_NULL_HANDLE);
1721 /* Destroy the command pool after cleaning up resources. In particular,
1722 * this needs to happen after all command buffers are freed, because
1723 * vkFreeCommandBuffers() requires a valid pool handle. */
1724 VK_CALL(vkDestroyCommandPool(device_vk->vk_device, context_vk->vk_command_pool, NULL));
1725 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_occlusion_query_pools);
1726 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_timestamp_query_pools);
1727 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_pipeline_statistics_query_pools);
1728 wined3d_context_vk_destroy_query_pools(context_vk, &context_vk->free_stream_output_statistics_query_pools);
1729 wine_rb_destroy(&context_vk->bo_slab_available, wined3d_context_vk_destroy_bo_slab, context_vk);
1730 heap_free(context_vk->submitted.buffers);
1731 heap_free(context_vk->completed.buffers);
1732 heap_free(context_vk->retired.objects);
1734 wined3d_shader_descriptor_writes_vk_cleanup(&context_vk->descriptor_writes);
1735 wine_rb_destroy(&context_vk->graphics_pipelines, wined3d_context_vk_destroy_graphics_pipeline, context_vk);
1736 wine_rb_destroy(&context_vk->pipeline_layouts, wined3d_context_vk_destroy_pipeline_layout, context_vk);
1737 wine_rb_destroy(&context_vk->render_passes, wined3d_context_vk_destroy_render_pass, context_vk);
1739 wined3d_context_cleanup(&context_vk->c);
1742 VkCommandBuffer wined3d_context_vk_get_command_buffer(struct wined3d_context_vk *context_vk)
1744 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1745 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1746 struct wined3d_command_buffer_vk *buffer;
1747 VkCommandBufferBeginInfo begin_info;
1748 struct wined3d_query_vk *query_vk;
1749 VkResult vr;
1751 TRACE("context_vk %p.\n", context_vk);
1753 buffer = &context_vk->current_command_buffer;
1754 if (buffer->vk_command_buffer)
1756 if (context_vk->retired_bo_size > WINED3D_RETIRED_BO_SIZE_THRESHOLD)
1757 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
1758 else
1760 TRACE("Returning existing command buffer %p with id 0x%s.\n",
1761 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1762 return buffer->vk_command_buffer;
1766 if (context_vk->completed.buffer_count)
1768 const struct wined3d_command_buffer_vk *old_buffer;
1770 old_buffer = &context_vk->completed.buffers[--context_vk->completed.buffer_count];
1771 buffer->vk_command_buffer = old_buffer->vk_command_buffer;
1772 buffer->vk_fence = old_buffer->vk_fence;
1774 else
1776 VkCommandBufferAllocateInfo command_buffer_info;
1777 VkFenceCreateInfo fence_desc;
1779 fence_desc.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1780 fence_desc.pNext = NULL;
1781 fence_desc.flags = 0;
1782 if ((vr = VK_CALL(vkCreateFence(device_vk->vk_device, &fence_desc, NULL, &buffer->vk_fence))) < 0)
1784 ERR("Failed to create fence, vr %s.\n", wined3d_debug_vkresult(vr));
1785 return VK_NULL_HANDLE;
1788 command_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1789 command_buffer_info.pNext = NULL;
1790 command_buffer_info.commandPool = context_vk->vk_command_pool;
1791 command_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1792 command_buffer_info.commandBufferCount = 1;
1793 if ((vr = VK_CALL(vkAllocateCommandBuffers(device_vk->vk_device,
1794 &command_buffer_info, &buffer->vk_command_buffer))) < 0)
1796 WARN("Failed to allocate Vulkan command buffer, vr %s.\n", wined3d_debug_vkresult(vr));
1797 VK_CALL(vkDestroyFence(device_vk->vk_device, buffer->vk_fence, NULL));
1798 return VK_NULL_HANDLE;
1802 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1803 begin_info.pNext = NULL;
1804 begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1805 begin_info.pInheritanceInfo = NULL;
1806 if ((vr = VK_CALL(vkBeginCommandBuffer(buffer->vk_command_buffer, &begin_info))) < 0)
1808 WARN("Failed to begin command buffer, vr %s.\n", wined3d_debug_vkresult(vr));
1809 VK_CALL(vkFreeCommandBuffers(device_vk->vk_device, context_vk->vk_command_pool,
1810 1, &buffer->vk_command_buffer));
1811 return buffer->vk_command_buffer = VK_NULL_HANDLE;
1814 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->active_queries, struct wined3d_query_vk, entry)
1816 if (!wined3d_context_vk_allocate_query(context_vk, query_vk->q.type, &query_vk->pool_idx))
1818 ERR("Failed to allocate new query.\n");
1819 break;
1822 wined3d_query_vk_resume(query_vk, context_vk);
1825 TRACE("Created new command buffer %p with id 0x%s.\n",
1826 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1828 return buffer->vk_command_buffer;
1831 void wined3d_context_vk_submit_command_buffer(struct wined3d_context_vk *context_vk,
1832 unsigned int wait_semaphore_count, const VkSemaphore *wait_semaphores, const VkPipelineStageFlags *wait_stages,
1833 unsigned int signal_semaphore_count, const VkSemaphore *signal_semaphores)
1835 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1836 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1837 struct wined3d_query_pool_vk *pool_vk, *pool_vk_next;
1838 struct wined3d_command_buffer_vk *buffer;
1839 struct wined3d_query_vk *query_vk;
1840 VkSubmitInfo submit_info;
1841 VkResult vr;
1843 TRACE("context_vk %p, wait_semaphore_count %u, wait_semaphores %p, wait_stages %p,"
1844 "signal_semaphore_count %u, signal_semaphores %p.\n",
1845 context_vk, wait_semaphore_count, wait_semaphores, wait_stages,
1846 signal_semaphore_count, signal_semaphores);
1848 buffer = &context_vk->current_command_buffer;
1849 if (!buffer->vk_command_buffer)
1850 return;
1852 TRACE("Submitting command buffer %p with id 0x%s.\n",
1853 buffer->vk_command_buffer, wine_dbgstr_longlong(buffer->id));
1855 wined3d_context_vk_end_current_render_pass(context_vk);
1857 LIST_FOR_EACH_ENTRY_SAFE(pool_vk, pool_vk_next, &context_vk->completed_query_pools,
1858 struct wined3d_query_pool_vk, completed_entry)
1860 list_remove(&pool_vk->completed_entry);
1861 list_init(&pool_vk->completed_entry);
1863 wined3d_context_vk_reset_completed_queries(context_vk, pool_vk, buffer);
1866 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->active_queries, struct wined3d_query_vk, entry)
1867 wined3d_query_vk_suspend(query_vk, context_vk);
1869 context_vk->graphics.vk_pipeline = VK_NULL_HANDLE;
1870 context_vk->update_compute_pipeline = 1;
1871 context_vk->update_stream_output = 1;
1872 context_vk->c.update_shader_resource_bindings = 1;
1873 context_vk->c.update_compute_shader_resource_bindings = 1;
1874 context_vk->c.update_unordered_access_view_bindings = 1;
1875 context_vk->c.update_compute_unordered_access_view_bindings = 1;
1876 context_invalidate_state(&context_vk->c, STATE_STREAMSRC);
1877 context_invalidate_state(&context_vk->c, STATE_INDEXBUFFER);
1878 context_invalidate_state(&context_vk->c, STATE_BLEND_FACTOR);
1879 context_invalidate_state(&context_vk->c, STATE_STENCIL_REF);
1881 VK_CALL(vkEndCommandBuffer(buffer->vk_command_buffer));
1883 VK_CALL(vkResetFences(device_vk->vk_device, 1, &buffer->vk_fence));
1885 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1886 submit_info.pNext = NULL;
1887 submit_info.waitSemaphoreCount = wait_semaphore_count;
1888 submit_info.pWaitSemaphores = wait_semaphores;
1889 submit_info.pWaitDstStageMask = wait_stages;
1890 submit_info.commandBufferCount = 1;
1891 submit_info.pCommandBuffers = &buffer->vk_command_buffer;
1892 submit_info.signalSemaphoreCount = signal_semaphore_count;
1893 submit_info.pSignalSemaphores = signal_semaphores;
1895 if ((vr = VK_CALL(vkQueueSubmit(device_vk->vk_queue, 1, &submit_info, buffer->vk_fence))) < 0)
1896 ERR("Failed to submit command buffer %p, vr %s.\n",
1897 buffer->vk_command_buffer, wined3d_debug_vkresult(vr));
1899 if (!wined3d_array_reserve((void **)&context_vk->submitted.buffers, &context_vk->submitted.buffers_size,
1900 context_vk->submitted.buffer_count + 1, sizeof(*context_vk->submitted.buffers)))
1901 ERR("Failed to grow submitted command buffer array.\n");
1903 context_vk->submitted.buffers[context_vk->submitted.buffer_count++] = *buffer;
1905 buffer->vk_command_buffer = VK_NULL_HANDLE;
1906 /* We don't expect this to ever happen, but handle it anyway. */
1907 if (!++buffer->id)
1909 wined3d_context_vk_wait_command_buffer(context_vk, buffer->id - 1);
1910 context_vk->completed_command_buffer_id = 0;
1911 buffer->id = 1;
1913 context_vk->retired_bo_size = 0;
1914 wined3d_context_vk_cleanup_resources(context_vk, VK_NULL_HANDLE);
1917 void wined3d_context_vk_wait_command_buffer(struct wined3d_context_vk *context_vk, uint64_t id)
1919 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
1920 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1921 VkFence vk_fence;
1922 SIZE_T i;
1924 if (id <= context_vk->completed_command_buffer_id
1925 || id > context_vk->current_command_buffer.id) /* In case the buffer ID wrapped. */
1926 return;
1928 for (i = 0; i < context_vk->submitted.buffer_count; ++i)
1930 if (context_vk->submitted.buffers[i].id != id)
1931 continue;
1933 vk_fence = context_vk->submitted.buffers[i].vk_fence;
1934 wined3d_context_vk_cleanup_resources(context_vk, vk_fence);
1935 for (i = 0; i < context_vk->submitted.buffer_count; ++i)
1937 if (context_vk->submitted.buffers[i].id != id)
1938 continue;
1940 VK_CALL(vkWaitForFences(device_vk->vk_device, 1, &vk_fence, VK_TRUE, UINT64_MAX));
1941 wined3d_context_vk_remove_command_buffer(context_vk, i);
1942 return;
1946 ERR("Failed to find fence for command buffer with id 0x%s.\n", wine_dbgstr_longlong(id));
1949 void wined3d_context_vk_image_barrier(struct wined3d_context_vk *context_vk,
1950 VkCommandBuffer vk_command_buffer, VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
1951 VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask, VkImageLayout old_layout,
1952 VkImageLayout new_layout, VkImage image, const VkImageSubresourceRange *range)
1954 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1955 VkImageMemoryBarrier barrier;
1957 wined3d_context_vk_end_current_render_pass(context_vk);
1959 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1960 barrier.pNext = NULL;
1961 barrier.srcAccessMask = src_access_mask;
1962 barrier.dstAccessMask = dst_access_mask;
1963 barrier.oldLayout = old_layout;
1964 barrier.newLayout = new_layout;
1965 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1966 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1967 barrier.image = image;
1968 barrier.subresourceRange = *range;
1970 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, src_stage_mask, dst_stage_mask, 0, 0, NULL, 0, NULL, 1, &barrier));
1973 static int wined3d_render_pass_vk_compare(const void *key, const struct wine_rb_entry *entry)
1975 const struct wined3d_render_pass_key_vk *k = key;
1976 const struct wined3d_render_pass_vk *pass = WINE_RB_ENTRY_VALUE(entry,
1977 const struct wined3d_render_pass_vk, entry);
1979 return memcmp(k, &pass->key, sizeof(*k));
1982 static int wined3d_pipeline_layout_vk_compare(const void *key, const struct wine_rb_entry *entry)
1984 const struct wined3d_pipeline_layout_key_vk *a = key;
1985 const struct wined3d_pipeline_layout_key_vk *b = &WINE_RB_ENTRY_VALUE(entry,
1986 const struct wined3d_pipeline_layout_vk, entry)->key;
1987 int ret;
1989 if ((ret = wined3d_uint32_compare(a->binding_count, b->binding_count)))
1990 return ret;
1991 return memcmp(a->bindings, b->bindings, a->binding_count * sizeof(*a->bindings));
1994 static int wined3d_graphics_pipeline_vk_compare(const void *key, const struct wine_rb_entry *entry)
1996 const struct wined3d_graphics_pipeline_key_vk *a = key;
1997 const struct wined3d_graphics_pipeline_key_vk *b = &WINE_RB_ENTRY_VALUE(entry,
1998 const struct wined3d_graphics_pipeline_vk, entry)->key;
1999 unsigned int i;
2000 int ret;
2002 if ((ret = wined3d_uint32_compare(a->pipeline_desc.stageCount, b->pipeline_desc.stageCount)))
2003 return ret;
2004 for (i = 0; i < a->pipeline_desc.stageCount; ++i)
2006 if ((ret = wined3d_uint64_compare(a->stages[i].module, b->stages[i].module)))
2007 return ret;
2010 if ((ret = wined3d_uint32_compare(a->divisor_desc.vertexBindingDivisorCount,
2011 b->divisor_desc.vertexBindingDivisorCount)))
2012 return ret;
2013 if ((ret = memcmp(a->divisors, b->divisors,
2014 a->divisor_desc.vertexBindingDivisorCount * sizeof(*a->divisors))))
2015 return ret;
2017 if ((ret = wined3d_uint32_compare(a->input_desc.vertexAttributeDescriptionCount,
2018 b->input_desc.vertexAttributeDescriptionCount)))
2019 return ret;
2020 if ((ret = memcmp(a->attributes, b->attributes,
2021 a->input_desc.vertexAttributeDescriptionCount * sizeof(*a->attributes))))
2022 return ret;
2023 if ((ret = wined3d_uint32_compare(a->input_desc.vertexBindingDescriptionCount,
2024 b->input_desc.vertexBindingDescriptionCount)))
2025 return ret;
2026 if ((ret = memcmp(a->bindings, b->bindings,
2027 a->input_desc.vertexBindingDescriptionCount * sizeof(*a->bindings))))
2028 return ret;
2030 if ((ret = wined3d_uint32_compare(a->ia_desc.topology, b->ia_desc.topology)))
2031 return ret;
2032 if ((ret = wined3d_uint32_compare(a->ia_desc.primitiveRestartEnable, b->ia_desc.primitiveRestartEnable)))
2033 return ret;
2035 if ((ret = wined3d_uint32_compare(a->ts_desc.patchControlPoints, b->ts_desc.patchControlPoints)))
2036 return ret;
2038 if ((ret = memcmp(a->viewports, b->viewports, sizeof(a->viewports))))
2039 return ret;
2040 if ((ret = memcmp(a->scissors, b->scissors, sizeof(a->scissors))))
2041 return ret;
2043 if ((ret = memcmp(&a->rs_desc, &b->rs_desc, sizeof(a->rs_desc))))
2044 return ret;
2046 if ((ret = wined3d_uint32_compare(a->ms_desc.rasterizationSamples, b->ms_desc.rasterizationSamples)))
2047 return ret;
2048 if ((ret = wined3d_uint32_compare(a->ms_desc.alphaToCoverageEnable, b->ms_desc.alphaToCoverageEnable)))
2049 return ret;
2050 if ((ret = wined3d_uint32_compare(a->sample_mask, b->sample_mask)))
2051 return ret;
2053 if ((ret = memcmp(&a->ds_desc, &b->ds_desc, sizeof(a->ds_desc))))
2054 return ret;
2056 if ((ret = wined3d_uint32_compare(a->blend_desc.attachmentCount, b->blend_desc.attachmentCount)))
2057 return ret;
2058 if ((ret = memcmp(a->blend_attachments, b->blend_attachments,
2059 a->blend_desc.attachmentCount * sizeof(*a->blend_attachments))))
2060 return ret;
2062 if ((ret = wined3d_uint64_compare(a->pipeline_desc.layout, b->pipeline_desc.layout)))
2063 return ret;
2065 if ((ret = wined3d_uint64_compare(a->pipeline_desc.renderPass, b->pipeline_desc.renderPass)))
2066 return ret;
2068 return 0;
2071 static int wined3d_bo_slab_vk_compare(const void *key, const struct wine_rb_entry *entry)
2073 const struct wined3d_bo_slab_vk *slab = WINE_RB_ENTRY_VALUE(entry, const struct wined3d_bo_slab_vk, entry);
2074 const struct wined3d_bo_slab_vk_key *k = key;
2075 int ret;
2077 if ((ret = wined3d_uint32_compare(k->memory_type, slab->requested_memory_type)))
2078 return ret;
2079 if ((ret = wined3d_uint32_compare(k->usage, slab->bo.usage)))
2080 return ret;
2081 return wined3d_uint64_compare(k->size, slab->bo.size);
2084 static void wined3d_context_vk_init_graphics_pipeline_key(struct wined3d_context_vk *context_vk)
2086 struct wined3d_graphics_pipeline_key_vk *key;
2087 VkPipelineShaderStageCreateInfo *stage;
2088 unsigned int i;
2090 static const VkDynamicState dynamic_states[] =
2092 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
2093 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
2096 key = &context_vk->graphics.pipeline_key_vk;
2097 memset(key, 0, sizeof(*key));
2099 for (i = 0; i < ARRAY_SIZE(context_vk->graphics.vk_modules); ++i)
2101 stage = &key->stages[i];
2102 stage->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2103 stage->pName = "main";
2106 key->input_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2107 key->input_desc.pVertexBindingDescriptions = key->bindings;
2108 key->input_desc.pVertexAttributeDescriptions = key->attributes;
2110 key->divisor_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT;
2111 key->divisor_desc.pVertexBindingDivisors = key->divisors;
2113 key->ia_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
2115 key->ts_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO;
2117 key->vp_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
2118 key->vp_desc.pViewports = key->viewports;
2119 key->vp_desc.pScissors = key->scissors;
2121 key->rs_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2122 key->rs_desc.lineWidth = 1.0f;
2124 key->ms_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
2125 key->ms_desc.pSampleMask = &key->sample_mask;
2127 key->ds_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
2128 key->ds_desc.maxDepthBounds = 1.0f;
2130 key->blend_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2131 key->blend_desc.logicOp = VK_LOGIC_OP_COPY;
2132 key->blend_desc.pAttachments = key->blend_attachments;
2133 key->blend_desc.blendConstants[0] = 1.0f;
2134 key->blend_desc.blendConstants[1] = 1.0f;
2135 key->blend_desc.blendConstants[2] = 1.0f;
2136 key->blend_desc.blendConstants[3] = 1.0f;
2138 key->dynamic_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2139 key->dynamic_desc.dynamicStateCount = ARRAY_SIZE(dynamic_states);
2140 key->dynamic_desc.pDynamicStates = dynamic_states;
2142 key->pipeline_desc.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
2143 key->pipeline_desc.pStages = key->stages;
2144 key->pipeline_desc.pVertexInputState = &key->input_desc;
2145 key->pipeline_desc.pInputAssemblyState = &key->ia_desc;
2146 key->pipeline_desc.pTessellationState = &key->ts_desc;
2147 key->pipeline_desc.pViewportState = &key->vp_desc;
2148 key->pipeline_desc.pRasterizationState = &key->rs_desc;
2149 key->pipeline_desc.pMultisampleState = &key->ms_desc;
2150 key->pipeline_desc.pDepthStencilState = &key->ds_desc;
2151 key->pipeline_desc.pColorBlendState = &key->blend_desc;
2152 key->pipeline_desc.pDynamicState = &key->dynamic_desc;
2153 key->pipeline_desc.basePipelineIndex = -1;
2156 static void wined3d_context_vk_update_rasterisation_state(const struct wined3d_context_vk *context_vk,
2157 const struct wined3d_state *state, struct wined3d_graphics_pipeline_key_vk *key)
2159 const struct wined3d_d3d_info *d3d_info = context_vk->c.d3d_info;
2160 VkPipelineRasterizationStateCreateInfo *desc = &key->rs_desc;
2161 const struct wined3d_rasterizer_state_desc *r;
2162 float scale_bias;
2163 union
2165 uint32_t u32;
2166 float f32;
2167 } const_bias;
2169 if (!state->rasterizer_state)
2171 desc->depthClampEnable = VK_FALSE;
2172 desc->rasterizerDiscardEnable = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
2173 desc->cullMode = VK_CULL_MODE_BACK_BIT;
2174 desc->frontFace = VK_FRONT_FACE_CLOCKWISE;
2175 desc->depthBiasEnable = VK_FALSE;
2176 desc->depthBiasConstantFactor = 0.0f;
2177 desc->depthBiasClamp = 0.0f;
2178 desc->depthBiasSlopeFactor = 0.0f;
2180 return;
2183 r = &state->rasterizer_state->desc;
2184 desc->depthClampEnable = !r->depth_clip;
2185 desc->rasterizerDiscardEnable = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
2186 desc->cullMode = vk_cull_mode_from_wined3d(r->cull_mode);
2187 desc->frontFace = r->front_ccw ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE;
2189 scale_bias = r->scale_bias;
2190 const_bias.f32 = r->depth_bias;
2191 if (!scale_bias && !const_bias.f32)
2193 desc->depthBiasEnable = VK_FALSE;
2194 desc->depthBiasConstantFactor = 0.0f;
2195 desc->depthBiasClamp = 0.0f;
2196 desc->depthBiasSlopeFactor = 0.0f;
2198 return;
2201 desc->depthBiasEnable = VK_TRUE;
2202 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_DEPTH_BIAS)
2204 const struct wined3d_rendertarget_view *dsv;
2206 if ((dsv = state->fb.depth_stencil))
2208 desc->depthBiasConstantFactor = -(float)const_bias.u32 / dsv->format->depth_bias_scale;
2209 desc->depthBiasSlopeFactor = -(float)const_bias.u32;
2211 else
2213 desc->depthBiasConstantFactor = 0.0f;
2214 desc->depthBiasSlopeFactor = 0.0f;
2217 else
2219 desc->depthBiasConstantFactor = const_bias.f32;
2220 desc->depthBiasSlopeFactor = scale_bias;
2222 desc->depthBiasClamp = r->depth_bias_clamp;
2225 static void wined3d_context_vk_update_blend_state(const struct wined3d_context_vk *context_vk,
2226 const struct wined3d_state *state, struct wined3d_graphics_pipeline_key_vk *key)
2228 VkPipelineColorBlendStateCreateInfo *desc = &key->blend_desc;
2229 const struct wined3d_blend_state_desc *b;
2230 unsigned int i;
2232 desc->attachmentCount = context_vk->rt_count;
2234 memset(key->blend_attachments, 0, sizeof(key->blend_attachments));
2235 if (!state->blend_state)
2237 for (i = 0; i < context_vk->rt_count; ++i)
2239 key->blend_attachments[i].colorWriteMask = VK_COLOR_COMPONENT_R_BIT
2240 | VK_COLOR_COMPONENT_G_BIT
2241 | VK_COLOR_COMPONENT_B_BIT
2242 | VK_COLOR_COMPONENT_A_BIT;
2245 return;
2248 b = &state->blend_state->desc;
2249 for (i = 0; i < context_vk->rt_count; ++i)
2251 const struct wined3d_rendertarget_blend_state_desc *rt = &b->rt[b->independent ? i : 0];
2252 const struct wined3d_rendertarget_view *rtv = state->fb.render_targets[i];
2253 VkPipelineColorBlendAttachmentState *a = &key->blend_attachments[i];
2254 enum wined3d_blend src_blend, dst_blend;
2255 const struct wined3d_format *rt_format;
2257 a->colorWriteMask = vk_colour_write_mask_from_wined3d(rt->writemask);
2258 if (!rt->enable)
2259 continue;
2261 if (rtv)
2262 rt_format = rtv->format;
2263 else
2264 rt_format = wined3d_get_format(context_vk->c.device->adapter, WINED3DFMT_NULL, 0);
2265 a->blendEnable = VK_TRUE;
2267 src_blend = rt->src;
2268 dst_blend = rt->dst;
2269 if (src_blend == WINED3D_BLEND_BOTHSRCALPHA)
2271 src_blend = WINED3D_BLEND_SRCALPHA;
2272 dst_blend = WINED3D_BLEND_INVSRCALPHA;
2274 else if (src_blend == WINED3D_BLEND_BOTHINVSRCALPHA)
2276 src_blend = WINED3D_BLEND_INVSRCALPHA;
2277 dst_blend = WINED3D_BLEND_SRCALPHA;
2279 a->srcColorBlendFactor = vk_blend_factor_from_wined3d(src_blend, rt_format, FALSE);
2280 a->dstColorBlendFactor = vk_blend_factor_from_wined3d(dst_blend, rt_format, FALSE);
2281 a->colorBlendOp = vk_blend_op_from_wined3d(rt->op);
2283 src_blend = rt->src_alpha;
2284 dst_blend = rt->dst_alpha;
2285 a->srcAlphaBlendFactor = vk_blend_factor_from_wined3d(src_blend, rt_format, TRUE);
2286 a->dstAlphaBlendFactor = vk_blend_factor_from_wined3d(dst_blend, rt_format, TRUE);
2287 a->alphaBlendOp = vk_blend_op_from_wined3d(rt->op_alpha);
2291 static VkFormat vk_format_from_component_type(enum wined3d_component_type component_type)
2293 switch (component_type)
2295 case WINED3D_TYPE_UINT:
2296 return VK_FORMAT_R32G32B32A32_UINT;
2297 case WINED3D_TYPE_INT:
2298 return VK_FORMAT_R32G32B32A32_SINT;
2299 case WINED3D_TYPE_UNKNOWN:
2300 case WINED3D_TYPE_FLOAT:
2301 return VK_FORMAT_R32G32B32A32_SFLOAT;
2303 return VK_FORMAT_UNDEFINED;
2306 static bool wined3d_context_vk_update_graphics_pipeline_key(struct wined3d_context_vk *context_vk,
2307 const struct wined3d_state *state, VkPipelineLayout vk_pipeline_layout, uint32_t *null_buffer_binding)
2309 unsigned int i, attribute_count, binding_count, divisor_count, stage_count;
2310 const struct wined3d_d3d_info *d3d_info = context_vk->c.d3d_info;
2311 struct wined3d_graphics_pipeline_key_vk *key;
2312 VkPipelineShaderStageCreateInfo *stage;
2313 struct wined3d_stream_info stream_info;
2314 struct wined3d_shader *vertex_shader;
2315 VkPrimitiveTopology vk_topology;
2316 VkShaderModule module;
2317 bool update = false;
2318 uint32_t mask;
2320 *null_buffer_binding = ~0u;
2321 key = &context_vk->graphics.pipeline_key_vk;
2323 if (context_vk->c.shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
2325 stage_count = 0;
2326 for (i = 0; i < ARRAY_SIZE(context_vk->graphics.vk_modules); ++i)
2328 if (!(module = context_vk->graphics.vk_modules[i]))
2329 continue;
2331 stage = &key->stages[stage_count++];
2332 stage->stage = vk_shader_stage_from_wined3d(i);
2333 stage->module = module;
2336 key->pipeline_desc.stageCount = stage_count;
2338 update = true;
2341 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_VDECL)
2342 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAMSRC)
2343 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
2345 VkVertexInputAttributeDescription *a;
2346 VkVertexInputBindingDescription *b;
2348 wined3d_stream_info_from_declaration(&stream_info, state, d3d_info);
2349 divisor_count = 0;
2350 for (i = 0, mask = 0, attribute_count = 0, binding_count = 0; i < ARRAY_SIZE(stream_info.elements); ++i)
2352 VkVertexInputBindingDivisorDescriptionEXT *d;
2353 struct wined3d_stream_info_element *e;
2354 uint32_t binding;
2356 if (!(stream_info.use_map & (1u << i)))
2357 continue;
2359 a = &key->attributes[attribute_count++];
2360 e = &stream_info.elements[i];
2361 binding = e->stream_idx;
2363 a->location = i;
2364 a->binding = binding;
2365 a->format = wined3d_format_vk(e->format)->vk_format;
2366 a->offset = (UINT_PTR)e->data.addr - state->streams[binding].offset;
2368 if (mask & (1u << binding))
2369 continue;
2370 mask |= 1u << binding;
2372 b = &key->bindings[binding_count++];
2373 b->binding = binding;
2374 b->stride = e->stride;
2375 b->inputRate = e->instanced ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX;
2377 if (e->instanced)
2379 d = &key->divisors[divisor_count++];
2380 d->binding = binding;
2381 d->divisor = e->divisor;
2385 vertex_shader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
2386 if (vertex_shader && (mask = ~stream_info.use_map & vertex_shader->reg_maps.input_registers))
2388 struct wined3d_shader_signature_element *element;
2389 struct wined3d_shader_signature *signature;
2390 uint32_t null_binding, location;
2392 if (!wined3d_get_unused_stream_index(state, &null_binding))
2394 ERR("No streams left for a null buffer binding.\n");
2396 else
2398 signature = &vertex_shader->input_signature;
2399 for (i = 0; i < signature->element_count; ++i)
2401 element = &signature->elements[i];
2402 location = element->register_idx;
2404 if (!(mask & (1u << location)) || element->sysval_semantic)
2405 continue;
2406 mask &= ~(1u << location);
2408 a = &key->attributes[attribute_count++];
2409 a->location = location;
2410 a->binding = null_binding;
2411 a->format = vk_format_from_component_type(element->component_type);
2412 a->offset = 0;
2415 if (mask != (~stream_info.use_map & vertex_shader->reg_maps.input_registers))
2417 b = &key->bindings[binding_count++];
2418 *null_buffer_binding = b->binding = null_binding;
2419 b->stride = 0;
2420 b->inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
2425 key->input_desc.pNext = NULL;
2426 key->input_desc.vertexBindingDescriptionCount = binding_count;
2427 key->input_desc.vertexAttributeDescriptionCount = attribute_count;
2429 if (divisor_count)
2431 key->input_desc.pNext = &key->divisor_desc;
2432 key->divisor_desc.vertexBindingDivisorCount = divisor_count;
2435 update = true;
2438 vk_topology = vk_topology_from_wined3d(state->primitive_type);
2439 if (key->ia_desc.topology != vk_topology)
2441 key->ia_desc.topology = vk_topology;
2442 key->ia_desc.primitiveRestartEnable = !(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART)
2443 && !wined3d_primitive_type_is_list(state->primitive_type);
2445 update = true;
2448 if (key->ts_desc.patchControlPoints != state->patch_vertex_count)
2450 key->ts_desc.patchControlPoints = state->patch_vertex_count;
2452 update = true;
2455 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_VIEWPORT)
2456 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SCISSORRECT)
2457 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_RASTERIZER))
2459 key->vp_desc.viewportCount = (context_vk->vk_info->multiple_viewports ? WINED3D_MAX_VIEWPORTS : 1);
2460 key->vp_desc.scissorCount = key->vp_desc.viewportCount;
2462 for (i = 0; i < key->vp_desc.viewportCount; ++i)
2464 const struct wined3d_viewport *src_viewport = &state->viewports[i];
2465 VkViewport *viewport = &key->viewports[i];
2466 VkRect2D *scissor = &key->scissors[i];
2468 if (i >= state->viewport_count)
2470 viewport->x = 0.0f;
2471 viewport->y = 0.0f;
2472 viewport->width = 1.0f;
2473 viewport->height = 1.0f;
2474 viewport->minDepth = 0.0f;
2475 viewport->maxDepth = 0.0f;
2477 memset(scissor, 0, sizeof(*scissor));
2478 continue;
2481 viewport->x = src_viewport->x;
2482 viewport->y = src_viewport->y;
2483 viewport->width = src_viewport->width;
2484 viewport->height = src_viewport->height;
2485 viewport->minDepth = src_viewport->min_z;
2486 viewport->maxDepth = src_viewport->max_z;
2488 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
2490 const RECT *r = &state->scissor_rects[i];
2492 if (i >= state->scissor_rect_count)
2494 memset(scissor, 0, sizeof(*scissor));
2495 continue;
2498 scissor->offset.x = r->left;
2499 scissor->offset.y = r->top;
2500 scissor->extent.width = r->right - r->left;
2501 scissor->extent.height = r->bottom - r->top;
2503 else
2505 scissor->offset.x = viewport->x;
2506 scissor->offset.y = viewport->y;
2507 scissor->extent.width = viewport->width;
2508 scissor->extent.height = viewport->height;
2510 /* Scissor offsets need to be non-negative (VUID-VkPipelineViewportStateCreateInfo-x-02821) */
2511 if (scissor->offset.x < 0)
2512 scissor->offset.x = 0;
2513 if (scissor->offset.y < 0)
2514 scissor->offset.y = 0;
2515 viewport->y += viewport->height;
2516 viewport->height = -viewport->height;
2519 update = true;
2522 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_RASTERIZER)
2523 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY)))
2525 wined3d_context_vk_update_rasterisation_state(context_vk, state, key);
2527 update = true;
2530 if (key->ms_desc.rasterizationSamples != context_vk->sample_count
2531 || isStateDirty(&context_vk->c, STATE_BLEND) || isStateDirty(&context_vk->c, STATE_SAMPLE_MASK))
2533 key->ms_desc.rasterizationSamples = context_vk->sample_count;
2534 key->ms_desc.alphaToCoverageEnable = state->blend_state && state->blend_state->desc.alpha_to_coverage;
2535 key->sample_mask = state->sample_mask;
2537 update = true;
2540 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_DEPTH_STENCIL)
2541 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2543 const struct wined3d_depth_stencil_state *d = state->depth_stencil_state;
2545 if (d)
2547 key->ds_desc.depthTestEnable = d->desc.depth;
2548 key->ds_desc.depthWriteEnable = d->desc.depth_write;
2549 key->ds_desc.depthCompareOp = vk_compare_op_from_wined3d(d->desc.depth_func);
2550 key->ds_desc.stencilTestEnable = state->fb.depth_stencil && d->desc.stencil;
2551 if (key->ds_desc.stencilTestEnable)
2553 key->ds_desc.front.failOp = vk_stencil_op_from_wined3d(d->desc.front.fail_op);
2554 key->ds_desc.front.passOp = vk_stencil_op_from_wined3d(d->desc.front.pass_op);
2555 key->ds_desc.front.depthFailOp = vk_stencil_op_from_wined3d(d->desc.front.depth_fail_op);
2556 key->ds_desc.front.compareOp = vk_compare_op_from_wined3d(d->desc.front.func);
2557 key->ds_desc.front.compareMask = d->desc.stencil_read_mask;
2558 key->ds_desc.front.writeMask = d->desc.stencil_write_mask;
2560 key->ds_desc.back.failOp = vk_stencil_op_from_wined3d(d->desc.back.fail_op);
2561 key->ds_desc.back.passOp = vk_stencil_op_from_wined3d(d->desc.back.pass_op);
2562 key->ds_desc.back.depthFailOp = vk_stencil_op_from_wined3d(d->desc.back.depth_fail_op);
2563 key->ds_desc.back.compareOp = vk_compare_op_from_wined3d(d->desc.back.func);
2564 key->ds_desc.back.compareMask = d->desc.stencil_read_mask;
2565 key->ds_desc.back.writeMask = d->desc.stencil_write_mask;
2567 else
2569 memset(&key->ds_desc.front, 0, sizeof(key->ds_desc.front));
2570 memset(&key->ds_desc.back, 0, sizeof(key->ds_desc.back));
2573 else
2575 key->ds_desc.depthTestEnable = VK_TRUE;
2576 key->ds_desc.depthWriteEnable = VK_TRUE;
2577 key->ds_desc.depthCompareOp = VK_COMPARE_OP_LESS;
2578 key->ds_desc.stencilTestEnable = VK_FALSE;
2581 update = true;
2584 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_BLEND)
2585 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
2587 wined3d_context_vk_update_blend_state(context_vk, state, key);
2589 update = true;
2592 if (key->pipeline_desc.layout != vk_pipeline_layout)
2594 key->pipeline_desc.layout = vk_pipeline_layout;
2596 update = true;
2599 if (key->pipeline_desc.renderPass != context_vk->vk_render_pass)
2601 key->pipeline_desc.renderPass = context_vk->vk_render_pass;
2603 update = true;
2606 return update;
2609 static bool wined3d_context_vk_begin_render_pass(struct wined3d_context_vk *context_vk,
2610 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2612 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2613 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
2614 VkImageView vk_views[WINED3D_MAX_RENDER_TARGETS + 1];
2615 unsigned int fb_width, fb_height, fb_layer_count;
2616 struct wined3d_rendertarget_view_vk *rtv_vk;
2617 struct wined3d_rendertarget_view *view;
2618 const VkPhysicalDeviceLimits *limits;
2619 struct wined3d_query_vk *query_vk;
2620 VkRenderPassBeginInfo begin_info;
2621 unsigned int attachment_count, i;
2622 struct wined3d_texture *texture;
2623 VkFramebufferCreateInfo fb_desc;
2624 VkResult vr;
2626 if (context_vk->vk_render_pass)
2627 return true;
2629 limits = &wined3d_adapter_vk(device_vk->d.adapter)->device_limits;
2630 fb_width = limits->maxFramebufferWidth;
2631 fb_height = limits->maxFramebufferHeight;
2632 fb_layer_count = limits->maxFramebufferLayers;
2633 attachment_count = 0;
2635 context_vk->rt_count = 0;
2636 begin_info.clearValueCount = 0;
2637 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
2639 if (!(view = state->fb.render_targets[i]) || view->format->id == WINED3DFMT_NULL)
2640 continue;
2642 rtv_vk = wined3d_rendertarget_view_vk(view);
2644 if (rtv_vk->v.resource->bind_count)
2646 struct wined3d_texture_vk *texture_vk;
2647 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(rtv_vk->v.resource));
2648 wined3d_texture_vk_make_generic(texture_vk, context_vk);
2651 vk_views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
2652 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
2653 wined3d_context_vk_reference_rendertarget_view(context_vk, rtv_vk);
2655 if (view->width < fb_width)
2656 fb_width = view->width;
2657 if (view->height < fb_height)
2658 fb_height = view->height;
2659 if (view->layer_count < fb_layer_count)
2660 fb_layer_count = view->layer_count;
2661 context_vk->rt_count = i + 1;
2663 if (wined3d_rendertarget_view_get_locations(view) & WINED3D_LOCATION_CLEARED)
2665 VkClearColorValue *c = &clear_values[attachment_count].color;
2667 if (view->resource->type == WINED3D_RTYPE_BUFFER)
2669 c->int32[0] = c->int32[1] = c->int32[2] = c->int32[3] = 0;
2671 else
2673 texture = texture_from_resource(view->resource);
2674 wined3d_format_colour_to_vk(view->format,
2675 &texture->sub_resources[view->sub_resource_idx].clear_value.colour, c);
2678 begin_info.clearValueCount = attachment_count + 1;
2680 ++attachment_count;
2683 if ((view = state->fb.depth_stencil))
2685 rtv_vk = wined3d_rendertarget_view_vk(view);
2687 if (rtv_vk->v.resource->bind_count)
2689 struct wined3d_texture_vk *texture_vk;
2690 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(rtv_vk->v.resource));
2691 wined3d_texture_vk_make_generic(texture_vk, context_vk);
2694 vk_views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
2695 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
2696 wined3d_context_vk_reference_rendertarget_view(context_vk, rtv_vk);
2698 if (view->width < fb_width)
2699 fb_width = view->width;
2700 if (view->height < fb_height)
2701 fb_height = view->height;
2702 if (view->layer_count < fb_layer_count)
2703 fb_layer_count = view->layer_count;
2705 if (wined3d_rendertarget_view_get_locations(view) & WINED3D_LOCATION_CLEARED)
2707 VkClearDepthStencilValue *c = &clear_values[attachment_count].depthStencil;
2709 if (view->resource->type == WINED3D_RTYPE_BUFFER)
2711 c->depth = 0.0f;
2712 c->stencil = 0;
2714 else
2716 texture = texture_from_resource(view->resource);
2717 c->depth = texture->sub_resources[view->sub_resource_idx].clear_value.depth;
2718 c->stencil = texture->sub_resources[view->sub_resource_idx].clear_value.stencil;
2720 begin_info.clearValueCount = attachment_count + 1;
2722 ++attachment_count;
2725 if (!(context_vk->vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, &state->fb,
2726 ARRAY_SIZE(state->fb.render_targets), !!state->fb.depth_stencil, 0)))
2728 ERR("Failed to get render pass.\n");
2729 return false;
2732 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
2733 fb_desc.pNext = NULL;
2734 fb_desc.flags = 0;
2735 fb_desc.renderPass = context_vk->vk_render_pass;
2736 fb_desc.attachmentCount = attachment_count;
2737 fb_desc.pAttachments = vk_views;
2738 fb_desc.width = fb_width;
2739 fb_desc.height = fb_height;
2740 fb_desc.layers = fb_layer_count;
2742 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &context_vk->vk_framebuffer))) < 0)
2744 WARN("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
2745 return false;
2748 begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
2749 begin_info.pNext = NULL;
2750 begin_info.renderPass = context_vk->vk_render_pass;
2751 begin_info.framebuffer = context_vk->vk_framebuffer;
2752 begin_info.renderArea.offset.x = 0;
2753 begin_info.renderArea.offset.y = 0;
2754 begin_info.renderArea.extent.width = fb_width;
2755 begin_info.renderArea.extent.height = fb_height;
2756 begin_info.pClearValues = clear_values;
2757 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_info, VK_SUBPASS_CONTENTS_INLINE));
2759 LIST_FOR_EACH_ENTRY(query_vk, &context_vk->render_pass_queries, struct wined3d_query_vk, entry)
2760 wined3d_query_vk_resume(query_vk, context_vk);
2761 return true;
2764 static void wined3d_context_vk_bind_vertex_buffers(struct wined3d_context_vk *context_vk,
2765 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2767 VkDeviceSize offsets[ARRAY_SIZE(state->streams)] = {0};
2768 VkBuffer buffers[ARRAY_SIZE(state->streams)];
2769 const struct wined3d_stream_state *stream;
2770 const VkDescriptorBufferInfo *buffer_info;
2771 struct wined3d_buffer_vk *buffer_vk;
2772 struct wined3d_buffer *buffer;
2773 unsigned int i, first, count;
2775 first = 0;
2776 count = 0;
2777 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
2779 stream = &state->streams[i];
2781 if ((buffer = stream->buffer))
2783 buffer_vk = wined3d_buffer_vk(buffer);
2784 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2785 wined3d_context_vk_reference_bo(context_vk, wined3d_bo_vk(buffer->buffer_object));
2786 buffers[count] = buffer_info->buffer;
2787 offsets[count] = buffer_info->offset + stream->offset;
2788 ++count;
2789 continue;
2792 if (count)
2793 VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, first, count, buffers, offsets));
2794 first = i + 1;
2795 count = 0;
2798 if (count)
2799 VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, first, count, buffers, offsets));
2802 static void wined3d_context_vk_bind_stream_output_buffers(struct wined3d_context_vk *context_vk,
2803 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, const struct wined3d_vk_info *vk_info)
2805 VkDeviceSize offsets[ARRAY_SIZE(state->stream_output)];
2806 VkDeviceSize sizes[ARRAY_SIZE(state->stream_output)];
2807 VkBuffer buffers[ARRAY_SIZE(state->stream_output)];
2808 const struct wined3d_stream_output *stream;
2809 const VkDescriptorBufferInfo *buffer_info;
2810 struct wined3d_buffer_vk *buffer_vk;
2811 struct wined3d_buffer *buffer;
2812 unsigned int i, first, count;
2814 first = 0;
2815 count = 0;
2816 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
2818 stream = &state->stream_output[i];
2820 if ((buffer = stream->buffer))
2822 buffer_vk = wined3d_buffer_vk(buffer);
2823 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
2824 wined3d_context_vk_reference_bo(context_vk, wined3d_bo_vk(buffer->buffer_object));
2825 buffers[count] = buffer_info->buffer;
2826 if ((offsets[count] = stream->offset) == ~0u)
2828 FIXME("Appending to stream output buffers not implemented.\n");
2829 offsets[count] = 0;
2831 sizes[count] = buffer_info->range - offsets[count];
2832 offsets[count] += buffer_info->offset;
2833 ++count;
2834 continue;
2837 if (count)
2838 VK_CALL(vkCmdBindTransformFeedbackBuffersEXT(vk_command_buffer, first, count, buffers, offsets, sizes));
2839 first = i + 1;
2840 count = 0;
2843 if (count)
2844 VK_CALL(vkCmdBindTransformFeedbackBuffersEXT(vk_command_buffer, first, count, buffers, offsets, sizes));
2847 static VkResult wined3d_context_vk_create_vk_descriptor_pool(struct wined3d_device_vk *device_vk,
2848 const struct wined3d_vk_info *vk_info, VkDescriptorPool *vk_pool)
2850 struct VkDescriptorPoolCreateInfo pool_desc;
2851 VkResult vr;
2853 static const VkDescriptorPoolSize pool_sizes[] =
2855 {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1024},
2856 {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1024},
2857 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024},
2858 {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1024},
2859 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024},
2860 {VK_DESCRIPTOR_TYPE_SAMPLER, 1024},
2863 pool_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
2864 pool_desc.pNext = NULL;
2865 pool_desc.flags = 0;
2866 pool_desc.maxSets = 512;
2867 pool_desc.poolSizeCount = ARRAY_SIZE(pool_sizes);
2868 pool_desc.pPoolSizes = pool_sizes;
2870 if ((vr = VK_CALL(vkCreateDescriptorPool(device_vk->vk_device, &pool_desc, NULL, vk_pool))) < 0)
2871 ERR("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
2873 return vr;
2876 VkDescriptorPool wined3d_context_vk_get_vk_descriptor_pool(struct wined3d_context_vk *context_vk)
2878 VkResult vr;
2880 if (context_vk->vk_descriptor_pool_count)
2881 return context_vk->vk_descriptor_pools[0];
2883 if (!wined3d_array_reserve((void **)&context_vk->vk_descriptor_pools, &context_vk->vk_descriptor_pools_size,
2884 1, sizeof(*context_vk->vk_descriptor_pools)))
2886 ERR("Failed to allocate array.\n");
2887 return VK_NULL_HANDLE;
2890 if ((vr = wined3d_context_vk_create_vk_descriptor_pool(wined3d_device_vk(context_vk->c.device),
2891 context_vk->vk_info, context_vk->vk_descriptor_pools)))
2893 WARN("Failed to create descriptor pool, vr %s.\n", wined3d_debug_vkresult(vr));
2894 return VK_NULL_HANDLE;
2897 context_vk->vk_descriptor_pool_count = 1;
2898 return context_vk->vk_descriptor_pools[0];
2901 VkDescriptorSet wined3d_context_vk_create_vk_descriptor_set(struct wined3d_context_vk *context_vk,
2902 VkDescriptorSetLayout vk_set_layout)
2904 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
2905 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
2906 struct VkDescriptorSetAllocateInfo set_desc;
2907 VkDescriptorPool vk_descriptor_pool;
2908 VkDescriptorSet vk_descriptor_set;
2909 VkResult vr;
2911 if (!(vk_descriptor_pool = wined3d_context_vk_get_vk_descriptor_pool(context_vk)))
2912 return VK_NULL_HANDLE;
2914 set_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
2915 set_desc.pNext = NULL;
2916 set_desc.descriptorPool = vk_descriptor_pool;
2917 set_desc.descriptorSetCount = 1;
2918 set_desc.pSetLayouts = &vk_set_layout;
2919 if ((vr = VK_CALL(vkAllocateDescriptorSets(device_vk->vk_device, &set_desc, &vk_descriptor_set))) >= 0)
2920 return vk_descriptor_set;
2922 if (vr == VK_ERROR_FRAGMENTED_POOL || vr == VK_ERROR_OUT_OF_POOL_MEMORY)
2924 wined3d_context_vk_reset_vk_descriptor_pool(context_vk,
2925 vk_descriptor_pool, context_vk->current_command_buffer.id);
2926 context_vk->vk_descriptor_pools[0] = context_vk->vk_descriptor_pools[--context_vk->vk_descriptor_pool_count];
2927 if (!(vk_descriptor_pool = wined3d_context_vk_get_vk_descriptor_pool(context_vk)))
2929 WARN("Failed to create descriptor pool.\n");
2930 return VK_NULL_HANDLE;
2933 set_desc.descriptorPool = vk_descriptor_pool;
2934 if ((vr = VK_CALL(vkAllocateDescriptorSets(device_vk->vk_device, &set_desc, &vk_descriptor_set))) >= 0)
2935 return vk_descriptor_set;
2938 WARN("Failed to allocate descriptor set, vr %s.\n", wined3d_debug_vkresult(vr));
2940 return VK_NULL_HANDLE;
2943 static bool wined3d_shader_descriptor_writes_vk_add_write(struct wined3d_shader_descriptor_writes_vk *writes,
2944 VkDescriptorSet vk_descriptor_set, size_t binding_idx, VkDescriptorType type,
2945 const VkDescriptorBufferInfo *buffer_info, const VkDescriptorImageInfo *image_info,
2946 const VkBufferView *buffer_view)
2948 SIZE_T write_count = writes->count;
2949 VkWriteDescriptorSet *write;
2951 if (!wined3d_array_reserve((void **)&writes->writes, &writes->size,
2952 write_count + 1, sizeof(*writes->writes)))
2953 return false;
2955 write = &writes->writes[write_count];
2956 write->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
2957 write->pNext = NULL;
2958 write->dstSet = vk_descriptor_set;
2959 write->dstBinding = binding_idx;
2960 write->dstArrayElement = 0;
2961 write->descriptorCount = 1;
2962 write->descriptorType = type;
2963 write->pImageInfo = image_info;
2964 write->pBufferInfo = buffer_info;
2965 write->pTexelBufferView = buffer_view;
2967 ++writes->count;
2969 return true;
2972 static bool wined3d_shader_resource_bindings_add_null_srv_binding(struct wined3d_shader_descriptor_writes_vk *writes,
2973 VkDescriptorSet vk_descriptor_set, size_t binding_idx, enum wined3d_shader_resource_type type,
2974 enum wined3d_data_type data_type, struct wined3d_context_vk *context_vk)
2976 const struct wined3d_null_views_vk *v = &wined3d_device_vk(context_vk->c.device)->null_views_vk;
2978 switch (type)
2980 case WINED3D_SHADER_RESOURCE_BUFFER:
2981 if (data_type == WINED3D_DATA_FLOAT)
2982 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding_idx,
2983 VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, NULL, NULL, &v->vk_view_buffer_float);
2984 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding_idx,
2985 VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, NULL, NULL, &v->vk_view_buffer_uint);
2987 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2988 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2989 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_1d, NULL);
2991 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2992 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2993 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2d, NULL);
2995 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2996 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
2997 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2dms, NULL);
2999 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
3000 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3001 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_3d, NULL);
3003 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
3004 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3005 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_cube, NULL);
3007 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
3008 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3009 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_1d_array, NULL);
3011 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
3012 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3013 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2d_array, NULL);
3015 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
3016 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3017 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_2dms_array, NULL);
3019 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
3020 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3021 binding_idx, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, NULL, &v->vk_info_cube_array, NULL);
3023 default:
3024 FIXME("Unhandled resource type %#x.\n", type);
3025 return false;
3029 static bool wined3d_shader_descriptor_writes_vk_add_cbv_write(struct wined3d_shader_descriptor_writes_vk *writes,
3030 struct wined3d_context_vk *context_vk, VkDescriptorSet vk_descriptor_set, const struct wined3d_state *state,
3031 const struct wined3d_shader_resource_binding *binding, VkDescriptorBufferInfo *buffer_info)
3033 const struct wined3d_constant_buffer_state *cb_state = &state->cb[binding->shader_type][binding->resource_idx];
3034 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
3035 struct wined3d_buffer_vk *buffer_vk;
3036 struct wined3d_buffer *buffer;
3038 if (!(buffer = cb_state->buffer))
3039 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding->binding_idx,
3040 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &device_vk->null_resources_vk.buffer_info, NULL, NULL);
3042 buffer_vk = wined3d_buffer_vk(buffer);
3043 *buffer_info = *wined3d_buffer_vk_get_buffer_info(buffer_vk);
3044 buffer_info->offset += cb_state->offset;
3045 buffer_info->range = min(cb_state->size, buffer_info->range);
3046 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3047 binding->binding_idx, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info, NULL, NULL))
3048 return false;
3049 wined3d_context_vk_reference_bo(context_vk, wined3d_bo_vk(buffer->buffer_object));
3050 return true;
3053 static bool wined3d_shader_descriptor_writes_vk_add_srv_write(struct wined3d_shader_descriptor_writes_vk *writes,
3054 struct wined3d_context_vk *context_vk, VkDescriptorSet vk_descriptor_set, const struct wined3d_state *state,
3055 const struct wined3d_shader_resource_binding *binding)
3057 struct wined3d_shader_resource_view_vk *srv_vk;
3058 struct wined3d_shader_resource_view *srv;
3059 const VkDescriptorImageInfo *image_info;
3060 struct wined3d_resource *resource;
3061 struct wined3d_view_vk *view_vk;
3062 VkBufferView *buffer_view;
3063 VkDescriptorType type;
3065 if (!(srv = state->shader_resource_view[binding->shader_type][binding->resource_idx]))
3066 return wined3d_shader_resource_bindings_add_null_srv_binding(writes, vk_descriptor_set,
3067 binding->binding_idx, binding->resource_type, binding->resource_data_type, context_vk);
3069 resource = srv->resource;
3070 srv_vk = wined3d_shader_resource_view_vk(srv);
3071 view_vk = &srv_vk->view_vk;
3072 if (resource->type == WINED3D_RTYPE_BUFFER)
3074 image_info = NULL;
3075 buffer_view = &view_vk->u.vk_buffer_view;
3076 type = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
3078 else
3080 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture_from_resource(resource));
3082 if (view_vk->u.vk_image_info.imageView)
3084 image_info = &view_vk->u.vk_image_info;
3085 if (image_info->imageLayout != texture_vk->layout)
3086 wined3d_shader_resource_view_vk_update_layout(srv_vk, texture_vk->layout);
3088 else
3089 image_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk);
3090 buffer_view = NULL;
3091 type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
3094 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3095 binding->binding_idx, type, NULL, image_info, buffer_view))
3096 return false;
3097 wined3d_context_vk_reference_shader_resource_view(context_vk, srv_vk);
3098 return true;
3101 static bool wined3d_shader_descriptor_writes_vk_add_uav_write(struct wined3d_shader_descriptor_writes_vk *writes,
3102 struct wined3d_context_vk *context_vk, enum wined3d_pipeline pipeline, VkDescriptorSet vk_descriptor_set,
3103 const struct wined3d_state *state, const struct wined3d_shader_resource_binding *binding)
3105 struct wined3d_unordered_access_view_vk *uav_vk;
3106 struct wined3d_unordered_access_view *uav;
3107 const VkDescriptorImageInfo *image_info;
3108 struct wined3d_resource *resource;
3109 struct wined3d_view_vk *view_vk;
3110 VkBufferView *buffer_view;
3111 VkDescriptorType type;
3113 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
3115 FIXME("NULL unordered access views not implemented.\n");
3116 return false;
3118 resource = uav->resource;
3120 uav_vk = wined3d_unordered_access_view_vk(uav);
3121 view_vk = &uav_vk->view_vk;
3122 if (resource->type == WINED3D_RTYPE_BUFFER)
3124 image_info = NULL;
3125 buffer_view = &view_vk->u.vk_buffer_view;
3126 type = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
3128 else
3130 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture_from_resource(resource));
3132 if (view_vk->u.vk_image_info.imageView)
3133 image_info = &view_vk->u.vk_image_info;
3134 else
3135 image_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk);
3136 buffer_view = NULL;
3137 type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
3140 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set,
3141 binding->binding_idx, type, NULL, image_info, buffer_view))
3142 return false;
3143 wined3d_context_vk_reference_unordered_access_view(context_vk, uav_vk);
3144 return true;
3147 static bool wined3d_shader_descriptor_writes_vk_add_uav_counter_write(
3148 struct wined3d_shader_descriptor_writes_vk *writes, struct wined3d_context_vk *context_vk,
3149 enum wined3d_pipeline pipeline, VkDescriptorSet vk_descriptor_set,
3150 const struct wined3d_state *state, const struct wined3d_shader_resource_binding *binding)
3152 struct wined3d_unordered_access_view_vk *uav_vk;
3153 struct wined3d_unordered_access_view *uav;
3155 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
3157 FIXME("NULL unordered access view counters not implemented.\n");
3158 return false;
3161 uav_vk = wined3d_unordered_access_view_vk(uav);
3162 if (!uav_vk->vk_counter_view)
3163 return false;
3165 return wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding->binding_idx,
3166 VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, NULL, NULL, &uav_vk->vk_counter_view);
3169 static bool wined3d_shader_descriptor_writes_vk_add_sampler_write(struct wined3d_shader_descriptor_writes_vk *writes,
3170 struct wined3d_context_vk *context_vk, VkDescriptorSet vk_descriptor_set, const struct wined3d_state *state,
3171 const struct wined3d_shader_resource_binding *binding)
3173 struct wined3d_sampler *sampler;
3175 if (!(sampler = state->sampler[binding->shader_type][binding->resource_idx]))
3176 sampler = context_vk->c.device->null_sampler;
3177 if (!wined3d_shader_descriptor_writes_vk_add_write(writes, vk_descriptor_set, binding->binding_idx,
3178 VK_DESCRIPTOR_TYPE_SAMPLER, NULL, &wined3d_sampler_vk(sampler)->vk_image_info, NULL))
3179 return false;
3180 wined3d_context_vk_reference_sampler(context_vk, wined3d_sampler_vk(sampler));
3181 return true;
3184 static bool wined3d_context_vk_update_descriptors(struct wined3d_context_vk *context_vk,
3185 VkCommandBuffer vk_command_buffer, const struct wined3d_state *state, enum wined3d_pipeline pipeline)
3187 struct wined3d_shader_descriptor_writes_vk *writes = &context_vk->descriptor_writes;
3188 VkDescriptorBufferInfo buffers[WINED3D_SHADER_TYPE_COUNT][MAX_CONSTANT_BUFFERS];
3189 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
3190 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
3191 const struct wined3d_shader_resource_binding *binding;
3192 struct wined3d_shader_resource_bindings *bindings;
3193 VkDescriptorSetLayout vk_set_layout;
3194 VkPipelineLayout vk_pipeline_layout;
3195 VkPipelineBindPoint vk_bind_point;
3196 VkDescriptorSet vk_descriptor_set;
3197 size_t i;
3199 switch (pipeline)
3201 case WINED3D_PIPELINE_GRAPHICS:
3202 bindings = &context_vk->graphics.bindings;
3203 vk_bind_point = VK_PIPELINE_BIND_POINT_GRAPHICS;
3204 vk_set_layout = context_vk->graphics.vk_set_layout;
3205 vk_pipeline_layout = context_vk->graphics.vk_pipeline_layout;
3206 break;
3208 case WINED3D_PIPELINE_COMPUTE:
3209 bindings = &context_vk->compute.bindings;
3210 vk_bind_point = VK_PIPELINE_BIND_POINT_COMPUTE;
3211 vk_set_layout = context_vk->compute.vk_set_layout;
3212 vk_pipeline_layout = context_vk->compute.vk_pipeline_layout;
3213 break;
3215 default:
3216 ERR("Invalid pipeline %#x.\n", pipeline);
3217 return false;
3220 if (!(vk_descriptor_set = wined3d_context_vk_create_vk_descriptor_set(context_vk, vk_set_layout)))
3222 WARN("Failed to create descriptor set.\n");
3223 return false;
3226 writes->count = 0;
3227 for (i = 0; i < bindings->count; ++i)
3229 binding = &bindings->bindings[i];
3231 switch (binding->shader_descriptor_type)
3233 case WINED3D_SHADER_DESCRIPTOR_TYPE_CBV:
3234 if (!wined3d_shader_descriptor_writes_vk_add_cbv_write(writes, context_vk, vk_descriptor_set,
3235 state, binding, &buffers[binding->shader_type][binding->resource_idx]))
3236 return false;
3237 break;
3239 case WINED3D_SHADER_DESCRIPTOR_TYPE_SRV:
3240 if (!wined3d_shader_descriptor_writes_vk_add_srv_write(writes,
3241 context_vk, vk_descriptor_set, state, binding))
3242 return false;
3243 break;
3245 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV:
3246 if (!wined3d_shader_descriptor_writes_vk_add_uav_write(writes,
3247 context_vk, pipeline, vk_descriptor_set, state, binding))
3248 return false;
3249 break;
3251 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV_COUNTER:
3252 if (!wined3d_shader_descriptor_writes_vk_add_uav_counter_write(writes,
3253 context_vk, pipeline, vk_descriptor_set, state, binding))
3254 return false;
3255 break;
3257 case WINED3D_SHADER_DESCRIPTOR_TYPE_SAMPLER:
3258 if (!wined3d_shader_descriptor_writes_vk_add_sampler_write(writes,
3259 context_vk, vk_descriptor_set, state, binding))
3260 return false;
3261 break;
3263 default:
3264 ERR("Invalid descriptor type %#x.\n", binding->shader_descriptor_type);
3265 return false;
3269 VK_CALL(vkUpdateDescriptorSets(device_vk->vk_device, writes->count, writes->writes, 0, NULL));
3270 VK_CALL(vkCmdBindDescriptorSets(vk_command_buffer, vk_bind_point,
3271 vk_pipeline_layout, 0, 1, &vk_descriptor_set, 0, NULL));
3273 return true;
3276 static VkResult wined3d_context_vk_create_vk_descriptor_set_layout(struct wined3d_device_vk *device_vk,
3277 const struct wined3d_vk_info *vk_info, const struct wined3d_pipeline_layout_key_vk *key,
3278 VkDescriptorSetLayout *vk_set_layout)
3280 VkDescriptorSetLayoutCreateInfo layout_desc;
3281 VkResult vr;
3283 layout_desc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
3284 layout_desc.pNext = NULL;
3285 layout_desc.flags = 0;
3286 layout_desc.bindingCount = key->binding_count;
3287 layout_desc.pBindings = key->bindings;
3289 if ((vr = VK_CALL(vkCreateDescriptorSetLayout(device_vk->vk_device, &layout_desc, NULL, vk_set_layout))) < 0)
3290 WARN("Failed to create Vulkan descriptor set layout, vr %s.\n", wined3d_debug_vkresult(vr));
3292 return vr;
3295 struct wined3d_pipeline_layout_vk *wined3d_context_vk_get_pipeline_layout(
3296 struct wined3d_context_vk *context_vk, VkDescriptorSetLayoutBinding *bindings, SIZE_T binding_count)
3298 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
3299 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
3300 struct wined3d_pipeline_layout_key_vk key;
3301 struct wined3d_pipeline_layout_vk *layout;
3302 VkPipelineLayoutCreateInfo layout_desc;
3303 struct wine_rb_entry *entry;
3304 VkResult vr;
3306 key.bindings = bindings;
3307 key.binding_count = binding_count;
3308 if ((entry = wine_rb_get(&context_vk->pipeline_layouts, &key)))
3309 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_pipeline_layout_vk, entry);
3311 if (!(layout = heap_alloc(sizeof(*layout))))
3312 return NULL;
3314 if (!(layout->key.bindings = heap_alloc(sizeof(*layout->key.bindings) * key.binding_count)))
3316 heap_free(layout);
3317 return NULL;
3319 memcpy(layout->key.bindings, key.bindings, sizeof(*layout->key.bindings) * key.binding_count);
3320 layout->key.binding_count = key.binding_count;
3322 if ((vr = wined3d_context_vk_create_vk_descriptor_set_layout(device_vk, vk_info, &key, &layout->vk_set_layout)))
3324 WARN("Failed to create descriptor set layout, vr %s.\n", wined3d_debug_vkresult(vr));
3325 goto fail;
3328 layout_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
3329 layout_desc.pNext = NULL;
3330 layout_desc.flags = 0;
3331 layout_desc.setLayoutCount = 1;
3332 layout_desc.pSetLayouts = &layout->vk_set_layout;
3333 layout_desc.pushConstantRangeCount = 0;
3334 layout_desc.pPushConstantRanges = NULL;
3336 if ((vr = VK_CALL(vkCreatePipelineLayout(device_vk->vk_device,
3337 &layout_desc, NULL, &layout->vk_pipeline_layout))) < 0)
3339 WARN("Failed to create Vulkan pipeline layout, vr %s.\n", wined3d_debug_vkresult(vr));
3340 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
3341 goto fail;
3344 if (wine_rb_put(&context_vk->pipeline_layouts, &layout->key, &layout->entry) == -1)
3346 ERR("Failed to insert pipeline layout.\n");
3347 VK_CALL(vkDestroyPipelineLayout(device_vk->vk_device, layout->vk_pipeline_layout, NULL));
3348 VK_CALL(vkDestroyDescriptorSetLayout(device_vk->vk_device, layout->vk_set_layout, NULL));
3349 goto fail;
3352 return layout;
3354 fail:
3355 heap_free(layout->key.bindings);
3356 heap_free(layout);
3357 return NULL;
3360 static VkPipeline wined3d_context_vk_get_graphics_pipeline(struct wined3d_context_vk *context_vk)
3362 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
3363 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
3364 struct wined3d_graphics_pipeline_vk *pipeline_vk;
3365 struct wined3d_graphics_pipeline_key_vk *key;
3366 struct wine_rb_entry *entry;
3367 VkResult vr;
3369 key = &context_vk->graphics.pipeline_key_vk;
3370 if ((entry = wine_rb_get(&context_vk->graphics_pipelines, key)))
3371 return WINE_RB_ENTRY_VALUE(entry, struct wined3d_graphics_pipeline_vk, entry)->vk_pipeline;
3373 if (!(pipeline_vk = heap_alloc(sizeof(*pipeline_vk))))
3374 return VK_NULL_HANDLE;
3375 pipeline_vk->key = *key;
3377 if ((vr = VK_CALL(vkCreateGraphicsPipelines(device_vk->vk_device,
3378 VK_NULL_HANDLE, 1, &key->pipeline_desc, NULL, &pipeline_vk->vk_pipeline))) < 0)
3380 WARN("Failed to create graphics pipeline, vr %s.\n", wined3d_debug_vkresult(vr));
3381 heap_free(pipeline_vk);
3382 return VK_NULL_HANDLE;
3385 if (wine_rb_put(&context_vk->graphics_pipelines, &pipeline_vk->key, &pipeline_vk->entry) == -1)
3386 ERR("Failed to insert pipeline.\n");
3388 return pipeline_vk->vk_pipeline;
3391 static void wined3d_context_vk_load_shader_resources(struct wined3d_context_vk *context_vk,
3392 const struct wined3d_state *state, enum wined3d_pipeline pipeline)
3394 struct wined3d_shader_descriptor_writes_vk *writes = &context_vk->descriptor_writes;
3395 const struct wined3d_shader_resource_bindings *bindings;
3396 const struct wined3d_shader_resource_binding *binding;
3397 struct wined3d_unordered_access_view_vk *uav_vk;
3398 struct wined3d_shader_resource_view_vk *srv_vk;
3399 struct wined3d_unordered_access_view *uav;
3400 struct wined3d_shader_resource_view *srv;
3401 struct wined3d_buffer_vk *buffer_vk;
3402 struct wined3d_sampler *sampler;
3403 struct wined3d_buffer *buffer;
3404 size_t i;
3406 switch (pipeline)
3408 case WINED3D_PIPELINE_GRAPHICS:
3409 bindings = &context_vk->graphics.bindings;
3410 break;
3412 case WINED3D_PIPELINE_COMPUTE:
3413 bindings = &context_vk->compute.bindings;
3414 break;
3416 default:
3417 ERR("Invalid pipeline %#x.\n", pipeline);
3418 return;
3421 writes->count = 0;
3422 for (i = 0; i < bindings->count; ++i)
3424 binding = &bindings->bindings[i];
3426 switch (binding->shader_descriptor_type)
3428 case WINED3D_SHADER_DESCRIPTOR_TYPE_CBV:
3429 if (!(buffer = state->cb[binding->shader_type][binding->resource_idx].buffer))
3430 break;
3432 buffer_vk = wined3d_buffer_vk(buffer);
3433 wined3d_buffer_load(buffer, &context_vk->c, state);
3434 if (!buffer_vk->b.bo_user.valid)
3436 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
3437 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_CONSTANT_BUFFER(binding->shader_type));
3438 else
3439 context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_CONSTANT_BUFFER);
3441 wined3d_buffer_vk_barrier(buffer_vk, context_vk, WINED3D_BIND_CONSTANT_BUFFER);
3442 break;
3444 case WINED3D_SHADER_DESCRIPTOR_TYPE_SRV:
3445 if (!(srv = state->shader_resource_view[binding->shader_type][binding->resource_idx]))
3446 break;
3448 srv_vk = wined3d_shader_resource_view_vk(srv);
3449 if (srv->resource->type == WINED3D_RTYPE_BUFFER)
3451 if (!srv_vk->view_vk.bo_user.valid)
3453 wined3d_shader_resource_view_vk_update_buffer(srv_vk, context_vk);
3454 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
3455 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
3456 else
3457 context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
3459 wined3d_buffer_load(buffer_from_resource(srv->resource), &context_vk->c, state);
3461 else
3463 wined3d_texture_load(texture_from_resource(srv->resource), &context_vk->c, FALSE);
3465 wined3d_shader_resource_view_vk_barrier(srv_vk, context_vk, WINED3D_BIND_SHADER_RESOURCE);
3466 break;
3468 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV:
3469 if (!(uav = state->unordered_access_view[pipeline][binding->resource_idx]))
3470 break;
3472 uav_vk = wined3d_unordered_access_view_vk(uav);
3473 if (uav->resource->type == WINED3D_RTYPE_BUFFER)
3475 if (!uav_vk->view_vk.bo_user.valid)
3477 wined3d_unordered_access_view_vk_update(uav_vk, context_vk);
3478 if (pipeline == WINED3D_PIPELINE_GRAPHICS)
3479 context_invalidate_state(&context_vk->c, STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING);
3480 else
3481 context_invalidate_compute_state(&context_vk->c,
3482 STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING);
3484 wined3d_buffer_load(buffer_from_resource(uav->resource), &context_vk->c, state);
3485 wined3d_unordered_access_view_invalidate_location(uav, ~WINED3D_LOCATION_BUFFER);
3487 else
3489 wined3d_texture_load(texture_from_resource(uav->resource), &context_vk->c, FALSE);
3490 wined3d_unordered_access_view_invalidate_location(uav, ~WINED3D_LOCATION_TEXTURE_RGB);
3492 wined3d_unordered_access_view_vk_barrier(uav_vk, context_vk, WINED3D_BIND_UNORDERED_ACCESS);
3493 break;
3495 case WINED3D_SHADER_DESCRIPTOR_TYPE_UAV_COUNTER:
3496 break;
3498 case WINED3D_SHADER_DESCRIPTOR_TYPE_SAMPLER:
3499 if (!(sampler = state->sampler[binding->shader_type][binding->resource_idx]))
3500 sampler = context_vk->c.device->null_sampler;
3501 break;
3503 default:
3504 ERR("Invalid descriptor type %#x.\n", binding->shader_descriptor_type);
3505 break;
3510 VkCommandBuffer wined3d_context_vk_apply_draw_state(struct wined3d_context_vk *context_vk,
3511 const struct wined3d_state *state, struct wined3d_buffer_vk *indirect_vk, bool indexed)
3513 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
3514 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
3515 const struct wined3d_blend_state *b = state->blend_state;
3516 bool dual_source_blend = b && b->dual_source;
3517 struct wined3d_rendertarget_view *dsv;
3518 struct wined3d_rendertarget_view *rtv;
3519 struct wined3d_buffer_vk *buffer_vk;
3520 VkSampleCountFlagBits sample_count;
3521 VkCommandBuffer vk_command_buffer;
3522 unsigned int i, invalidate_rt = 0;
3523 struct wined3d_buffer *buffer;
3524 uint32_t null_buffer_binding;
3525 bool invalidate_ds = false;
3527 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL))
3528 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER)
3529 || dual_source_blend != context_vk->c.last_was_dual_source_blend)
3531 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL);
3532 context_vk->c.last_was_dual_source_blend = dual_source_blend;
3534 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
3535 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_VERTEX);
3536 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY)))
3537 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_GEOMETRY);
3538 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_HULL)))
3539 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_HULL) | (1u << WINED3D_SHADER_TYPE_DOMAIN);
3540 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN)))
3541 context_vk->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_DOMAIN);
3543 context_vk->sample_count = 0;
3544 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
3546 if (!(rtv = state->fb.render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
3547 continue;
3549 if (wined3d_blend_state_get_writemask(state->blend_state, i))
3551 /* We handle clears at the beginning of the render pass, no need for an explicit clear
3552 * first. */
3553 if (wined3d_rendertarget_view_get_locations(rtv) & WINED3D_LOCATION_CLEARED)
3554 wined3d_rendertarget_view_prepare_location(rtv, &context_vk->c, rtv->resource->draw_binding);
3555 else
3556 wined3d_rendertarget_view_load_location(rtv, &context_vk->c, rtv->resource->draw_binding);
3557 invalidate_rt |= (1 << i);
3559 else
3561 wined3d_rendertarget_view_prepare_location(rtv, &context_vk->c, rtv->resource->draw_binding);
3564 sample_count = max(1, wined3d_resource_get_sample_count(rtv->resource));
3565 if (!context_vk->sample_count)
3566 context_vk->sample_count = sample_count;
3567 else if (context_vk->sample_count != sample_count)
3568 FIXME("Inconsistent sample counts (%u != %u).\n", context_vk->sample_count, sample_count);
3571 if ((dsv = state->fb.depth_stencil))
3573 if (wined3d_state_uses_depth_buffer(state))
3575 if (wined3d_rendertarget_view_get_locations(dsv) & WINED3D_LOCATION_CLEARED)
3576 wined3d_rendertarget_view_prepare_location(dsv, &context_vk->c, dsv->resource->draw_binding);
3577 else
3578 wined3d_rendertarget_view_load_location(dsv, &context_vk->c, dsv->resource->draw_binding);
3580 else
3582 wined3d_rendertarget_view_prepare_location(dsv, &context_vk->c, dsv->resource->draw_binding);
3585 if (!state->depth_stencil_state || state->depth_stencil_state->writes_ds)
3586 invalidate_ds = true;
3588 sample_count = max(1, wined3d_resource_get_sample_count(dsv->resource));
3589 if (!context_vk->sample_count)
3590 context_vk->sample_count = sample_count;
3591 else if (context_vk->sample_count != sample_count)
3592 FIXME("Inconsistent sample counts (%u != %u).\n", context_vk->sample_count, sample_count);
3595 if (!context_vk->sample_count)
3596 context_vk->sample_count = VK_SAMPLE_COUNT_1_BIT;
3597 if (context_vk->c.shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
3599 device_vk->d.shader_backend->shader_select(device_vk->d.shader_priv, &context_vk->c, state);
3600 if (!context_vk->graphics.vk_pipeline_layout)
3602 ERR("No pipeline layout set.\n");
3603 return VK_NULL_HANDLE;
3605 context_vk->c.update_shader_resource_bindings = 1;
3606 context_vk->c.update_unordered_access_view_bindings = 1;
3609 wined3d_context_vk_load_shader_resources(context_vk, state, WINED3D_PIPELINE_GRAPHICS);
3611 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
3613 if (!(buffer = state->streams[i].buffer))
3614 continue;
3616 buffer_vk = wined3d_buffer_vk(buffer);
3617 wined3d_buffer_load(&buffer_vk->b, &context_vk->c, state);
3618 wined3d_buffer_vk_barrier(buffer_vk, context_vk, WINED3D_BIND_VERTEX_BUFFER);
3619 if (!buffer_vk->b.bo_user.valid)
3620 context_invalidate_state(&context_vk->c, STATE_STREAMSRC);
3623 if (use_transform_feedback(state) && vk_info->supported[WINED3D_VK_EXT_TRANSFORM_FEEDBACK])
3625 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3627 if (!(buffer = state->stream_output[i].buffer))
3628 continue;
3630 buffer_vk = wined3d_buffer_vk(buffer);
3631 wined3d_buffer_load(&buffer_vk->b, &context_vk->c, state);
3632 wined3d_buffer_vk_barrier(buffer_vk, context_vk, WINED3D_BIND_STREAM_OUTPUT);
3633 wined3d_buffer_invalidate_location(&buffer_vk->b, ~WINED3D_LOCATION_BUFFER);
3634 if (!buffer_vk->b.bo_user.valid)
3635 context_vk->update_stream_output = 1;
3637 context_vk->c.transform_feedback_active = 1;
3640 if (indexed || (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_INDEXBUFFER) && state->index_buffer))
3642 buffer_vk = wined3d_buffer_vk(state->index_buffer);
3643 wined3d_buffer_load(&buffer_vk->b, &context_vk->c, state);
3644 wined3d_buffer_vk_barrier(buffer_vk, context_vk, WINED3D_BIND_INDEX_BUFFER);
3645 if (!buffer_vk->b.bo_user.valid)
3646 context_invalidate_state(&context_vk->c, STATE_INDEXBUFFER);
3649 if (indirect_vk)
3651 wined3d_buffer_load(&indirect_vk->b, &context_vk->c, state);
3652 wined3d_buffer_vk_barrier(indirect_vk, context_vk, WINED3D_BIND_INDIRECT_BUFFER);
3655 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
3657 ERR("Failed to get command buffer.\n");
3658 return VK_NULL_HANDLE;
3661 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_FRAMEBUFFER))
3662 wined3d_context_vk_end_current_render_pass(context_vk);
3663 if (!wined3d_context_vk_begin_render_pass(context_vk, vk_command_buffer, state, vk_info))
3665 ERR("Failed to begin render pass.\n");
3666 return VK_NULL_HANDLE;
3669 while (invalidate_rt)
3671 i = wined3d_bit_scan(&invalidate_rt);
3672 rtv = state->fb.render_targets[i];
3673 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
3674 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
3677 if (invalidate_ds)
3679 wined3d_rendertarget_view_validate_location(dsv, dsv->resource->draw_binding);
3680 wined3d_rendertarget_view_invalidate_location(dsv, ~dsv->resource->draw_binding);
3683 if (wined3d_context_vk_update_graphics_pipeline_key(context_vk, state, context_vk->graphics.vk_pipeline_layout,
3684 &null_buffer_binding) || !context_vk->graphics.vk_pipeline)
3686 if (!(context_vk->graphics.vk_pipeline = wined3d_context_vk_get_graphics_pipeline(context_vk)))
3688 ERR("Failed to get graphics pipeline.\n");
3689 return VK_NULL_HANDLE;
3692 VK_CALL(vkCmdBindPipeline(vk_command_buffer,
3693 VK_PIPELINE_BIND_POINT_GRAPHICS, context_vk->graphics.vk_pipeline));
3694 if (null_buffer_binding != ~0u)
3696 VkDeviceSize offset = 0;
3697 VK_CALL(vkCmdBindVertexBuffers(vk_command_buffer, null_buffer_binding, 1,
3698 &device_vk->null_resources_vk.buffer_info.buffer, &offset));
3702 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STENCIL_REF) && dsv)
3704 VK_CALL(vkCmdSetStencilReference(vk_command_buffer, VK_STENCIL_FACE_FRONT_AND_BACK,
3705 state->stencil_ref & wined3d_mask_from_size(dsv->format->stencil_size)));
3708 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAMSRC))
3709 wined3d_context_vk_bind_vertex_buffers(context_vk, vk_command_buffer, state, vk_info);
3711 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_STREAM_OUTPUT))
3713 context_vk->update_stream_output = 1;
3714 context_vk->c.transform_feedback_paused = 0;
3716 if (context_vk->c.transform_feedback_active && context_vk->update_stream_output)
3718 wined3d_context_vk_bind_stream_output_buffers(context_vk, vk_command_buffer, state, vk_info);
3719 context_vk->update_stream_output = 0;
3722 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_INDEXBUFFER) && state->index_buffer)
3724 const VkDescriptorBufferInfo *buffer_info;
3725 VkIndexType idx_type;
3727 if (state->index_format == WINED3DFMT_R16_UINT)
3728 idx_type = VK_INDEX_TYPE_UINT16;
3729 else
3730 idx_type = VK_INDEX_TYPE_UINT32;
3731 buffer_vk = wined3d_buffer_vk(state->index_buffer);
3732 buffer_info = wined3d_buffer_vk_get_buffer_info(buffer_vk);
3733 wined3d_context_vk_reference_bo(context_vk, wined3d_bo_vk(buffer_vk->b.buffer_object));
3734 VK_CALL(vkCmdBindIndexBuffer(vk_command_buffer, buffer_info->buffer,
3735 buffer_info->offset + state->index_offset, idx_type));
3738 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL))
3739 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3740 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX))
3741 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3742 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY))
3743 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3744 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL))
3745 || wined3d_context_is_graphics_state_dirty(&context_vk->c,
3746 STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN))
3747 || wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING))
3748 context_vk->c.update_shader_resource_bindings = 1;
3749 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING))
3750 context_vk->c.update_unordered_access_view_bindings = 1;
3752 if (context_vk->c.update_shader_resource_bindings || context_vk->c.update_unordered_access_view_bindings)
3754 if (!wined3d_context_vk_update_descriptors(context_vk, vk_command_buffer, state, WINED3D_PIPELINE_GRAPHICS))
3756 ERR("Failed to update shader descriptors.\n");
3757 return VK_NULL_HANDLE;
3760 context_vk->c.update_shader_resource_bindings = 0;
3761 context_vk->c.update_unordered_access_view_bindings = 0;
3764 if (wined3d_context_is_graphics_state_dirty(&context_vk->c, STATE_BLEND_FACTOR))
3765 VK_CALL(vkCmdSetBlendConstants(vk_command_buffer, &state->blend_factor.r));
3767 memset(context_vk->c.dirty_graphics_states, 0, sizeof(context_vk->c.dirty_graphics_states));
3768 context_vk->c.shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3770 return vk_command_buffer;
3773 VkCommandBuffer wined3d_context_vk_apply_compute_state(struct wined3d_context_vk *context_vk,
3774 const struct wined3d_state *state, struct wined3d_buffer_vk *indirect_vk)
3776 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
3777 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
3778 VkCommandBuffer vk_command_buffer;
3780 wined3d_context_vk_end_current_render_pass(context_vk);
3782 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_SHADER))
3783 context_vk->c.shader_update_mask |= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3785 if (context_vk->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
3787 device_vk->d.shader_backend->shader_select_compute(device_vk->d.shader_priv, &context_vk->c, state);
3788 if (!context_vk->compute.vk_pipeline)
3790 ERR("No compute pipeline set.\n");
3791 return VK_NULL_HANDLE;
3793 context_vk->c.update_compute_shader_resource_bindings = 1;
3794 context_vk->c.update_compute_unordered_access_view_bindings = 1;
3795 context_vk->update_compute_pipeline = 1;
3798 wined3d_context_vk_load_shader_resources(context_vk, state, WINED3D_PIPELINE_COMPUTE);
3800 if (indirect_vk)
3802 wined3d_buffer_load_location(&indirect_vk->b, &context_vk->c, WINED3D_LOCATION_BUFFER);
3803 wined3d_buffer_vk_barrier(indirect_vk, context_vk, WINED3D_BIND_INDIRECT_BUFFER);
3806 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
3808 ERR("Failed to get command buffer.\n");
3809 return VK_NULL_HANDLE;
3812 if (context_vk->update_compute_pipeline)
3814 VK_CALL(vkCmdBindPipeline(vk_command_buffer,
3815 VK_PIPELINE_BIND_POINT_COMPUTE, context_vk->compute.vk_pipeline));
3816 context_vk->update_compute_pipeline = 0;
3819 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_CONSTANT_BUFFER)
3820 || wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING))
3821 context_vk->c.update_compute_shader_resource_bindings = 1;
3822 if (wined3d_context_is_compute_state_dirty(&context_vk->c, STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING))
3823 context_vk->c.update_compute_unordered_access_view_bindings = 1;
3825 if (context_vk->c.update_compute_shader_resource_bindings
3826 || context_vk->c.update_compute_unordered_access_view_bindings)
3828 if (!wined3d_context_vk_update_descriptors(context_vk, vk_command_buffer, state, WINED3D_PIPELINE_COMPUTE))
3830 ERR("Failed to update shader descriptors.\n");
3831 return VK_NULL_HANDLE;
3834 context_vk->c.update_compute_shader_resource_bindings = 0;
3835 context_vk->c.update_compute_unordered_access_view_bindings = 0;
3838 memset(context_vk->c.dirty_compute_states, 0, sizeof(context_vk->c.dirty_compute_states));
3839 context_vk->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
3841 return vk_command_buffer;
3844 HRESULT wined3d_context_vk_init(struct wined3d_context_vk *context_vk, struct wined3d_swapchain *swapchain)
3846 VkCommandPoolCreateInfo command_pool_info;
3847 const struct wined3d_vk_info *vk_info;
3848 struct wined3d_adapter_vk *adapter_vk;
3849 struct wined3d_device_vk *device_vk;
3850 VkResult vr;
3852 TRACE("context_vk %p, swapchain %p.\n", context_vk, swapchain);
3854 memset(context_vk, 0, sizeof(*context_vk));
3855 wined3d_context_init(&context_vk->c, swapchain);
3856 device_vk = wined3d_device_vk(swapchain->device);
3857 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
3858 context_vk->vk_info = vk_info = &adapter_vk->vk_info;
3860 command_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
3861 command_pool_info.pNext = NULL;
3862 command_pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
3863 command_pool_info.queueFamilyIndex = device_vk->vk_queue_family_index;
3864 if ((vr = VK_CALL(vkCreateCommandPool(device_vk->vk_device,
3865 &command_pool_info, NULL, &context_vk->vk_command_pool))) < 0)
3867 ERR("Failed to create Vulkan command pool, vr %s.\n", wined3d_debug_vkresult(vr));
3868 wined3d_context_cleanup(&context_vk->c);
3869 return E_FAIL;
3871 context_vk->current_command_buffer.id = 1;
3873 wined3d_context_vk_init_graphics_pipeline_key(context_vk);
3875 list_init(&context_vk->render_pass_queries);
3876 list_init(&context_vk->active_queries);
3877 list_init(&context_vk->completed_query_pools);
3878 list_init(&context_vk->free_occlusion_query_pools);
3879 list_init(&context_vk->free_timestamp_query_pools);
3880 list_init(&context_vk->free_pipeline_statistics_query_pools);
3881 list_init(&context_vk->free_stream_output_statistics_query_pools);
3883 wine_rb_init(&context_vk->render_passes, wined3d_render_pass_vk_compare);
3884 wine_rb_init(&context_vk->pipeline_layouts, wined3d_pipeline_layout_vk_compare);
3885 wine_rb_init(&context_vk->graphics_pipelines, wined3d_graphics_pipeline_vk_compare);
3886 wine_rb_init(&context_vk->bo_slab_available, wined3d_bo_slab_vk_compare);
3888 return WINED3D_OK;