f34a44aefd7e8cf00607969b35813b9277b970c9
[mesa.git] / src / intel / vulkan / anv_descriptor_set.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "util/mesa-sha1.h"
31 #include "vk_util.h"
32
33 #include "anv_private.h"
34
35 /*
36 * Descriptor set layouts.
37 */
38
39 static enum anv_descriptor_data
40 anv_descriptor_data_for_type(const struct anv_physical_device *device,
41 VkDescriptorType type)
42 {
43 enum anv_descriptor_data data = 0;
44
45 switch (type) {
46 case VK_DESCRIPTOR_TYPE_SAMPLER:
47 data = ANV_DESCRIPTOR_SAMPLER_STATE;
48 break;
49
50 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
51 data = ANV_DESCRIPTOR_SURFACE_STATE |
52 ANV_DESCRIPTOR_SAMPLER_STATE;
53 break;
54
55 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
56 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
57 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
58 data = ANV_DESCRIPTOR_SURFACE_STATE;
59 break;
60
61 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
62 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
63 data = ANV_DESCRIPTOR_SURFACE_STATE;
64 if (device->info.gen < 9)
65 data |= ANV_DESCRIPTOR_IMAGE_PARAM;
66 break;
67
68 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
69 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
70 data = ANV_DESCRIPTOR_SURFACE_STATE |
71 ANV_DESCRIPTOR_BUFFER_VIEW;
72 break;
73
74 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
75 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
76 data = ANV_DESCRIPTOR_SURFACE_STATE;
77 break;
78
79 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
80 data = ANV_DESCRIPTOR_INLINE_UNIFORM;
81 break;
82
83 default:
84 unreachable("Unsupported descriptor type");
85 }
86
87 return data;
88 }
89
90 static unsigned
91 anv_descriptor_data_size(enum anv_descriptor_data data)
92 {
93 return 0;
94 }
95
96 /** Returns the size in bytes of each descriptor with the given layout */
97 unsigned
98 anv_descriptor_size(const struct anv_descriptor_set_binding_layout *layout)
99 {
100 if (layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM) {
101 assert(layout->data == ANV_DESCRIPTOR_INLINE_UNIFORM);
102 return layout->array_size;
103 }
104
105 return anv_descriptor_data_size(layout->data);
106 }
107
108 /** Returns the size in bytes of each descriptor of the given type
109 *
110 * This version of the function does not have access to the entire layout so
111 * it may only work on certain descriptor types where the descriptor size is
112 * entirely determined by the descriptor type. Whenever possible, code should
113 * use anv_descriptor_size() instead.
114 */
115 unsigned
116 anv_descriptor_type_size(const struct anv_physical_device *pdevice,
117 VkDescriptorType type)
118 {
119 assert(type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
120 return anv_descriptor_data_size(anv_descriptor_data_for_type(pdevice, type));
121 }
122
123 void anv_GetDescriptorSetLayoutSupport(
124 VkDevice device,
125 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
126 VkDescriptorSetLayoutSupport* pSupport)
127 {
128 uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
129
130 for (uint32_t b = 0; b < pCreateInfo->bindingCount; b++) {
131 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[b];
132
133 switch (binding->descriptorType) {
134 case VK_DESCRIPTOR_TYPE_SAMPLER:
135 /* There is no real limit on samplers */
136 break;
137
138 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
139 if (binding->pImmutableSamplers) {
140 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
141 ANV_FROM_HANDLE(anv_sampler, sampler,
142 binding->pImmutableSamplers[i]);
143 anv_foreach_stage(s, binding->stageFlags)
144 surface_count[s] += sampler->n_planes;
145 }
146 } else {
147 anv_foreach_stage(s, binding->stageFlags)
148 surface_count[s] += binding->descriptorCount;
149 }
150 break;
151
152 default:
153 anv_foreach_stage(s, binding->stageFlags)
154 surface_count[s] += binding->descriptorCount;
155 break;
156 }
157 }
158
159 bool supported = true;
160 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
161 /* Our maximum binding table size is 250 and we need to reserve 8 for
162 * render targets. 240 is a nice round number.
163 */
164 if (surface_count[s] >= 240)
165 supported = false;
166 }
167
168 pSupport->supported = supported;
169 }
170
171 VkResult anv_CreateDescriptorSetLayout(
172 VkDevice _device,
173 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
174 const VkAllocationCallbacks* pAllocator,
175 VkDescriptorSetLayout* pSetLayout)
176 {
177 ANV_FROM_HANDLE(anv_device, device, _device);
178
179 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
180
181 uint32_t max_binding = 0;
182 uint32_t immutable_sampler_count = 0;
183 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
184 max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
185
186 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
187 *
188 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
189 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
190 * pImmutableSamplers can be used to initialize a set of immutable
191 * samplers. [...] If descriptorType is not one of these descriptor
192 * types, then pImmutableSamplers is ignored.
193 *
194 * We need to be careful here and only parse pImmutableSamplers if we
195 * have one of the right descriptor types.
196 */
197 VkDescriptorType desc_type = pCreateInfo->pBindings[j].descriptorType;
198 if ((desc_type == VK_DESCRIPTOR_TYPE_SAMPLER ||
199 desc_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
200 pCreateInfo->pBindings[j].pImmutableSamplers)
201 immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
202 }
203
204 struct anv_descriptor_set_layout *set_layout;
205 struct anv_descriptor_set_binding_layout *bindings;
206 struct anv_sampler **samplers;
207
208 /* We need to allocate decriptor set layouts off the device allocator
209 * with DEVICE scope because they are reference counted and may not be
210 * destroyed when vkDestroyDescriptorSetLayout is called.
211 */
212 ANV_MULTIALLOC(ma);
213 anv_multialloc_add(&ma, &set_layout, 1);
214 anv_multialloc_add(&ma, &bindings, max_binding + 1);
215 anv_multialloc_add(&ma, &samplers, immutable_sampler_count);
216
217 if (!anv_multialloc_alloc(&ma, &device->alloc,
218 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
219 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
220
221 memset(set_layout, 0, sizeof(*set_layout));
222 set_layout->ref_cnt = 1;
223 set_layout->binding_count = max_binding + 1;
224
225 for (uint32_t b = 0; b <= max_binding; b++) {
226 /* Initialize all binding_layout entries to -1 */
227 memset(&set_layout->binding[b], -1, sizeof(set_layout->binding[b]));
228
229 set_layout->binding[b].data = 0;
230 set_layout->binding[b].array_size = 0;
231 set_layout->binding[b].immutable_samplers = NULL;
232 }
233
234 /* Initialize all samplers to 0 */
235 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
236
237 uint32_t buffer_view_count = 0;
238 uint32_t dynamic_offset_count = 0;
239 uint32_t descriptor_buffer_size = 0;
240
241 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
242 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
243 uint32_t b = binding->binding;
244 /* We temporarily store the pointer to the binding in the
245 * immutable_samplers pointer. This provides us with a quick-and-dirty
246 * way to sort the bindings by binding number.
247 */
248 set_layout->binding[b].immutable_samplers = (void *)binding;
249 }
250
251 for (uint32_t b = 0; b <= max_binding; b++) {
252 const VkDescriptorSetLayoutBinding *binding =
253 (void *)set_layout->binding[b].immutable_samplers;
254
255 if (binding == NULL)
256 continue;
257
258 /* We temporarily stashed the pointer to the binding in the
259 * immutable_samplers pointer. Now that we've pulled it back out
260 * again, we reset immutable_samplers to NULL.
261 */
262 set_layout->binding[b].immutable_samplers = NULL;
263
264 if (binding->descriptorCount == 0)
265 continue;
266
267 #ifndef NDEBUG
268 set_layout->binding[b].type = binding->descriptorType;
269 #endif
270 set_layout->binding[b].data =
271 anv_descriptor_data_for_type(&device->instance->physicalDevice,
272 binding->descriptorType);
273 set_layout->binding[b].array_size = binding->descriptorCount;
274 set_layout->binding[b].descriptor_index = set_layout->size;
275 set_layout->size += binding->descriptorCount;
276
277 if (set_layout->binding[b].data & ANV_DESCRIPTOR_BUFFER_VIEW) {
278 set_layout->binding[b].buffer_view_index = buffer_view_count;
279 buffer_view_count += binding->descriptorCount;
280 }
281
282 switch (binding->descriptorType) {
283 case VK_DESCRIPTOR_TYPE_SAMPLER:
284 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
285 if (binding->pImmutableSamplers) {
286 set_layout->binding[b].immutable_samplers = samplers;
287 samplers += binding->descriptorCount;
288
289 for (uint32_t i = 0; i < binding->descriptorCount; i++)
290 set_layout->binding[b].immutable_samplers[i] =
291 anv_sampler_from_handle(binding->pImmutableSamplers[i]);
292 }
293 break;
294 default:
295 break;
296 }
297
298 switch (binding->descriptorType) {
299 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
300 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
301 set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
302 dynamic_offset_count += binding->descriptorCount;
303 break;
304
305 default:
306 break;
307 }
308
309 if (binding->descriptorType ==
310 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
311 /* Inline uniform blocks are specified to use the descriptor array
312 * size as the size in bytes of the block.
313 */
314 descriptor_buffer_size = align_u32(descriptor_buffer_size, 32);
315 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
316 descriptor_buffer_size += binding->descriptorCount;
317 } else {
318 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
319 descriptor_buffer_size += anv_descriptor_size(&set_layout->binding[b]) *
320 binding->descriptorCount;
321 }
322
323 set_layout->shader_stages |= binding->stageFlags;
324 }
325
326 set_layout->buffer_view_count = buffer_view_count;
327 set_layout->dynamic_offset_count = dynamic_offset_count;
328 set_layout->descriptor_buffer_size = descriptor_buffer_size;
329
330 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
331
332 return VK_SUCCESS;
333 }
334
335 void anv_DestroyDescriptorSetLayout(
336 VkDevice _device,
337 VkDescriptorSetLayout _set_layout,
338 const VkAllocationCallbacks* pAllocator)
339 {
340 ANV_FROM_HANDLE(anv_device, device, _device);
341 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
342
343 if (!set_layout)
344 return;
345
346 anv_descriptor_set_layout_unref(device, set_layout);
347 }
348
349 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
350
351 static void
352 sha1_update_immutable_sampler(struct mesa_sha1 *ctx,
353 const struct anv_sampler *sampler)
354 {
355 if (!sampler->conversion)
356 return;
357
358 /* The only thing that affects the shader is ycbcr conversion */
359 _mesa_sha1_update(ctx, sampler->conversion,
360 sizeof(*sampler->conversion));
361 }
362
363 static void
364 sha1_update_descriptor_set_binding_layout(struct mesa_sha1 *ctx,
365 const struct anv_descriptor_set_binding_layout *layout)
366 {
367 SHA1_UPDATE_VALUE(ctx, layout->data);
368 SHA1_UPDATE_VALUE(ctx, layout->array_size);
369 SHA1_UPDATE_VALUE(ctx, layout->descriptor_index);
370 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_index);
371 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_index);
372 SHA1_UPDATE_VALUE(ctx, layout->descriptor_offset);
373
374 if (layout->immutable_samplers) {
375 for (uint16_t i = 0; i < layout->array_size; i++)
376 sha1_update_immutable_sampler(ctx, layout->immutable_samplers[i]);
377 }
378 }
379
380 static void
381 sha1_update_descriptor_set_layout(struct mesa_sha1 *ctx,
382 const struct anv_descriptor_set_layout *layout)
383 {
384 SHA1_UPDATE_VALUE(ctx, layout->binding_count);
385 SHA1_UPDATE_VALUE(ctx, layout->size);
386 SHA1_UPDATE_VALUE(ctx, layout->shader_stages);
387 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_count);
388 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_count);
389 SHA1_UPDATE_VALUE(ctx, layout->descriptor_buffer_size);
390
391 for (uint16_t i = 0; i < layout->binding_count; i++)
392 sha1_update_descriptor_set_binding_layout(ctx, &layout->binding[i]);
393 }
394
395 /*
396 * Pipeline layouts. These have nothing to do with the pipeline. They are
397 * just multiple descriptor set layouts pasted together
398 */
399
400 VkResult anv_CreatePipelineLayout(
401 VkDevice _device,
402 const VkPipelineLayoutCreateInfo* pCreateInfo,
403 const VkAllocationCallbacks* pAllocator,
404 VkPipelineLayout* pPipelineLayout)
405 {
406 ANV_FROM_HANDLE(anv_device, device, _device);
407 struct anv_pipeline_layout *layout;
408
409 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
410
411 layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
412 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
413 if (layout == NULL)
414 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
415
416 layout->num_sets = pCreateInfo->setLayoutCount;
417
418 unsigned dynamic_offset_count = 0;
419
420 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
421 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
422 pCreateInfo->pSetLayouts[set]);
423 layout->set[set].layout = set_layout;
424 anv_descriptor_set_layout_ref(set_layout);
425
426 layout->set[set].dynamic_offset_start = dynamic_offset_count;
427 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
428 if (set_layout->binding[b].dynamic_offset_index < 0)
429 continue;
430
431 dynamic_offset_count += set_layout->binding[b].array_size;
432 }
433 }
434
435 struct mesa_sha1 ctx;
436 _mesa_sha1_init(&ctx);
437 for (unsigned s = 0; s < layout->num_sets; s++) {
438 sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
439 _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
440 sizeof(layout->set[s].dynamic_offset_start));
441 }
442 _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
443 _mesa_sha1_final(&ctx, layout->sha1);
444
445 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
446
447 return VK_SUCCESS;
448 }
449
450 void anv_DestroyPipelineLayout(
451 VkDevice _device,
452 VkPipelineLayout _pipelineLayout,
453 const VkAllocationCallbacks* pAllocator)
454 {
455 ANV_FROM_HANDLE(anv_device, device, _device);
456 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
457
458 if (!pipeline_layout)
459 return;
460
461 for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
462 anv_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
463
464 vk_free2(&device->alloc, pAllocator, pipeline_layout);
465 }
466
467 /*
468 * Descriptor pools.
469 *
470 * These are implemented using a big pool of memory and a free-list for the
471 * host memory allocations and a state_stream and a free list for the buffer
472 * view surface state. The spec allows us to fail to allocate due to
473 * fragmentation in all cases but two: 1) after pool reset, allocating up
474 * until the pool size with no freeing must succeed and 2) allocating and
475 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
476 * and the free lists lets us recycle blocks for case 2).
477 */
478
479 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
480 * ensure we can allocate the entire BO without hitting zero. The actual
481 * amount doesn't matter.
482 */
483 #define POOL_HEAP_OFFSET 64
484
485 #define EMPTY 1
486
487 VkResult anv_CreateDescriptorPool(
488 VkDevice _device,
489 const VkDescriptorPoolCreateInfo* pCreateInfo,
490 const VkAllocationCallbacks* pAllocator,
491 VkDescriptorPool* pDescriptorPool)
492 {
493 ANV_FROM_HANDLE(anv_device, device, _device);
494 struct anv_descriptor_pool *pool;
495
496 const VkDescriptorPoolInlineUniformBlockCreateInfoEXT *inline_info =
497 vk_find_struct_const(pCreateInfo->pNext,
498 DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT);
499
500 uint32_t descriptor_count = 0;
501 uint32_t buffer_view_count = 0;
502 uint32_t descriptor_bo_size = 0;
503 for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
504 enum anv_descriptor_data desc_data =
505 anv_descriptor_data_for_type(&device->instance->physicalDevice,
506 pCreateInfo->pPoolSizes[i].type);
507
508 if (desc_data & ANV_DESCRIPTOR_BUFFER_VIEW)
509 buffer_view_count += pCreateInfo->pPoolSizes[i].descriptorCount;
510
511 unsigned desc_data_size = anv_descriptor_data_size(desc_data) *
512 pCreateInfo->pPoolSizes[i].descriptorCount;
513
514 if (pCreateInfo->pPoolSizes[i].type ==
515 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
516 /* Inline uniform blocks are specified to use the descriptor array
517 * size as the size in bytes of the block.
518 */
519 assert(inline_info);
520 desc_data_size += pCreateInfo->pPoolSizes[i].descriptorCount;
521 }
522
523 descriptor_bo_size += desc_data_size;
524
525 descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
526 }
527 /* We have to align descriptor buffer allocations to 32B so that we can
528 * push descriptor buffers. This means that each descriptor buffer
529 * allocated may burn up to 32B of extra space to get the right alignment.
530 * (Technically, it's at most 28B because we're always going to start at
531 * least 4B aligned but we're being conservative here.) Allocate enough
532 * extra space that we can chop it into maxSets pieces and align each one
533 * of them to 32B.
534 */
535 descriptor_bo_size += 32 * pCreateInfo->maxSets;
536 descriptor_bo_size = ALIGN(descriptor_bo_size, 4096);
537 /* We align inline uniform blocks to 32B */
538 if (inline_info)
539 descriptor_bo_size += 32 * inline_info->maxInlineUniformBlockBindings;
540
541 const size_t pool_size =
542 pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
543 descriptor_count * sizeof(struct anv_descriptor) +
544 buffer_view_count * sizeof(struct anv_buffer_view);
545 const size_t total_size = sizeof(*pool) + pool_size;
546
547 pool = vk_alloc2(&device->alloc, pAllocator, total_size, 8,
548 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
549 if (!pool)
550 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
551
552 pool->size = pool_size;
553 pool->next = 0;
554 pool->free_list = EMPTY;
555
556 if (descriptor_bo_size > 0) {
557 VkResult result = anv_bo_init_new(&pool->bo, device, descriptor_bo_size);
558 if (result != VK_SUCCESS) {
559 vk_free2(&device->alloc, pAllocator, pool);
560 return result;
561 }
562
563 anv_gem_set_caching(device, pool->bo.gem_handle, I915_CACHING_CACHED);
564
565 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0,
566 descriptor_bo_size, 0);
567 if (pool->bo.map == NULL) {
568 anv_gem_close(device, pool->bo.gem_handle);
569 vk_free2(&device->alloc, pAllocator, pool);
570 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
571 }
572
573 if (device->instance->physicalDevice.use_softpin) {
574 pool->bo.flags |= EXEC_OBJECT_PINNED;
575 anv_vma_alloc(device, &pool->bo);
576 }
577
578 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, descriptor_bo_size);
579 } else {
580 pool->bo.size = 0;
581 }
582
583 anv_state_stream_init(&pool->surface_state_stream,
584 &device->surface_state_pool, 4096);
585 pool->surface_state_free_list = NULL;
586
587 list_inithead(&pool->desc_sets);
588
589 *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
590
591 return VK_SUCCESS;
592 }
593
594 void anv_DestroyDescriptorPool(
595 VkDevice _device,
596 VkDescriptorPool _pool,
597 const VkAllocationCallbacks* pAllocator)
598 {
599 ANV_FROM_HANDLE(anv_device, device, _device);
600 ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
601
602 if (!pool)
603 return;
604
605 if (pool->bo.size) {
606 anv_gem_munmap(pool->bo.map, pool->bo.size);
607 anv_vma_free(device, &pool->bo);
608 anv_gem_close(device, pool->bo.gem_handle);
609 }
610 anv_state_stream_finish(&pool->surface_state_stream);
611
612 list_for_each_entry_safe(struct anv_descriptor_set, set,
613 &pool->desc_sets, pool_link) {
614 anv_descriptor_set_destroy(device, pool, set);
615 }
616
617 util_vma_heap_finish(&pool->bo_heap);
618
619 vk_free2(&device->alloc, pAllocator, pool);
620 }
621
622 VkResult anv_ResetDescriptorPool(
623 VkDevice _device,
624 VkDescriptorPool descriptorPool,
625 VkDescriptorPoolResetFlags flags)
626 {
627 ANV_FROM_HANDLE(anv_device, device, _device);
628 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
629
630 pool->next = 0;
631 pool->free_list = EMPTY;
632
633 if (pool->bo.size) {
634 util_vma_heap_finish(&pool->bo_heap);
635 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, pool->bo.size);
636 }
637
638 anv_state_stream_finish(&pool->surface_state_stream);
639
640 list_for_each_entry_safe(struct anv_descriptor_set, set,
641 &pool->desc_sets, pool_link) {
642 anv_descriptor_set_destroy(device, pool, set);
643 }
644
645 anv_state_stream_init(&pool->surface_state_stream,
646 &device->surface_state_pool, 4096);
647 pool->surface_state_free_list = NULL;
648
649 return VK_SUCCESS;
650 }
651
652 struct pool_free_list_entry {
653 uint32_t next;
654 uint32_t size;
655 };
656
657 static VkResult
658 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool *pool,
659 uint32_t size,
660 struct anv_descriptor_set **set)
661 {
662 if (size <= pool->size - pool->next) {
663 *set = (struct anv_descriptor_set *) (pool->data + pool->next);
664 pool->next += size;
665 return VK_SUCCESS;
666 } else {
667 struct pool_free_list_entry *entry;
668 uint32_t *link = &pool->free_list;
669 for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
670 entry = (struct pool_free_list_entry *) (pool->data + f);
671 if (size <= entry->size) {
672 *link = entry->next;
673 *set = (struct anv_descriptor_set *) entry;
674 return VK_SUCCESS;
675 }
676 link = &entry->next;
677 }
678
679 if (pool->free_list != EMPTY) {
680 return vk_error(VK_ERROR_FRAGMENTED_POOL);
681 } else {
682 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
683 }
684 }
685 }
686
687 static void
688 anv_descriptor_pool_free_set(struct anv_descriptor_pool *pool,
689 struct anv_descriptor_set *set)
690 {
691 /* Put the descriptor set allocation back on the free list. */
692 const uint32_t index = (char *) set - pool->data;
693 if (index + set->size == pool->next) {
694 pool->next = index;
695 } else {
696 struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
697 entry->next = pool->free_list;
698 entry->size = set->size;
699 pool->free_list = (char *) entry - pool->data;
700 }
701
702 list_del(&set->pool_link);
703 }
704
705 struct surface_state_free_list_entry {
706 void *next;
707 struct anv_state state;
708 };
709
710 static struct anv_state
711 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool *pool)
712 {
713 struct surface_state_free_list_entry *entry =
714 pool->surface_state_free_list;
715
716 if (entry) {
717 struct anv_state state = entry->state;
718 pool->surface_state_free_list = entry->next;
719 assert(state.alloc_size == 64);
720 return state;
721 } else {
722 return anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
723 }
724 }
725
726 static void
727 anv_descriptor_pool_free_state(struct anv_descriptor_pool *pool,
728 struct anv_state state)
729 {
730 /* Put the buffer view surface state back on the free list. */
731 struct surface_state_free_list_entry *entry = state.map;
732 entry->next = pool->surface_state_free_list;
733 entry->state = state;
734 pool->surface_state_free_list = entry;
735 }
736
737 size_t
738 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout)
739 {
740 return
741 sizeof(struct anv_descriptor_set) +
742 layout->size * sizeof(struct anv_descriptor) +
743 layout->buffer_view_count * sizeof(struct anv_buffer_view);
744 }
745
746 VkResult
747 anv_descriptor_set_create(struct anv_device *device,
748 struct anv_descriptor_pool *pool,
749 struct anv_descriptor_set_layout *layout,
750 struct anv_descriptor_set **out_set)
751 {
752 struct anv_descriptor_set *set;
753 const size_t size = anv_descriptor_set_layout_size(layout);
754
755 VkResult result = anv_descriptor_pool_alloc_set(pool, size, &set);
756 if (result != VK_SUCCESS)
757 return result;
758
759 if (layout->descriptor_buffer_size) {
760 /* Align the size to 32 so that alignment gaps don't cause extra holes
761 * in the heap which can lead to bad performance.
762 */
763 uint64_t pool_vma_offset =
764 util_vma_heap_alloc(&pool->bo_heap,
765 ALIGN(layout->descriptor_buffer_size, 32), 32);
766 if (pool_vma_offset == 0) {
767 anv_descriptor_pool_free_set(pool, set);
768 return vk_error(VK_ERROR_FRAGMENTED_POOL);
769 }
770 assert(pool_vma_offset >= POOL_HEAP_OFFSET &&
771 pool_vma_offset - POOL_HEAP_OFFSET <= INT32_MAX);
772 set->desc_mem.offset = pool_vma_offset - POOL_HEAP_OFFSET;
773 set->desc_mem.alloc_size = layout->descriptor_buffer_size;
774 set->desc_mem.map = pool->bo.map + set->desc_mem.offset;
775
776 set->desc_surface_state = anv_descriptor_pool_alloc_state(pool);
777 anv_fill_buffer_surface_state(device, set->desc_surface_state,
778 ISL_FORMAT_R32G32B32A32_FLOAT,
779 (struct anv_address) {
780 .bo = &pool->bo,
781 .offset = set->desc_mem.offset,
782 },
783 layout->descriptor_buffer_size, 1);
784 } else {
785 set->desc_mem = ANV_STATE_NULL;
786 set->desc_surface_state = ANV_STATE_NULL;
787 }
788
789 set->pool = pool;
790 set->layout = layout;
791 anv_descriptor_set_layout_ref(layout);
792
793 set->size = size;
794 set->buffer_views =
795 (struct anv_buffer_view *) &set->descriptors[layout->size];
796 set->buffer_view_count = layout->buffer_view_count;
797
798 /* By defining the descriptors to be zero now, we can later verify that
799 * a descriptor has not been populated with user data.
800 */
801 memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
802
803 /* Go through and fill out immutable samplers if we have any */
804 struct anv_descriptor *desc = set->descriptors;
805 for (uint32_t b = 0; b < layout->binding_count; b++) {
806 if (layout->binding[b].immutable_samplers) {
807 for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
808 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
809 * UpdateDescriptorSets if needed. However, if the descriptor
810 * set has an immutable sampler, UpdateDescriptorSets may never
811 * touch it, so we need to make sure it's 100% valid now.
812 */
813 desc[i] = (struct anv_descriptor) {
814 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
815 .sampler = layout->binding[b].immutable_samplers[i],
816 };
817 }
818 }
819 desc += layout->binding[b].array_size;
820 }
821
822 /* Allocate surface state for the buffer views. */
823 for (uint32_t b = 0; b < layout->buffer_view_count; b++) {
824 set->buffer_views[b].surface_state =
825 anv_descriptor_pool_alloc_state(pool);
826 }
827
828 *out_set = set;
829
830 return VK_SUCCESS;
831 }
832
833 void
834 anv_descriptor_set_destroy(struct anv_device *device,
835 struct anv_descriptor_pool *pool,
836 struct anv_descriptor_set *set)
837 {
838 anv_descriptor_set_layout_unref(device, set->layout);
839
840 if (set->desc_mem.alloc_size) {
841 util_vma_heap_free(&pool->bo_heap,
842 (uint64_t)set->desc_mem.offset + POOL_HEAP_OFFSET,
843 set->desc_mem.alloc_size);
844 anv_descriptor_pool_free_state(pool, set->desc_surface_state);
845 }
846
847 for (uint32_t b = 0; b < set->buffer_view_count; b++)
848 anv_descriptor_pool_free_state(pool, set->buffer_views[b].surface_state);
849
850 anv_descriptor_pool_free_set(pool, set);
851 }
852
853 VkResult anv_AllocateDescriptorSets(
854 VkDevice _device,
855 const VkDescriptorSetAllocateInfo* pAllocateInfo,
856 VkDescriptorSet* pDescriptorSets)
857 {
858 ANV_FROM_HANDLE(anv_device, device, _device);
859 ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
860
861 VkResult result = VK_SUCCESS;
862 struct anv_descriptor_set *set;
863 uint32_t i;
864
865 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
866 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
867 pAllocateInfo->pSetLayouts[i]);
868
869 result = anv_descriptor_set_create(device, pool, layout, &set);
870 if (result != VK_SUCCESS)
871 break;
872
873 list_addtail(&set->pool_link, &pool->desc_sets);
874
875 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
876 }
877
878 if (result != VK_SUCCESS)
879 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
880 i, pDescriptorSets);
881
882 return result;
883 }
884
885 VkResult anv_FreeDescriptorSets(
886 VkDevice _device,
887 VkDescriptorPool descriptorPool,
888 uint32_t count,
889 const VkDescriptorSet* pDescriptorSets)
890 {
891 ANV_FROM_HANDLE(anv_device, device, _device);
892 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
893
894 for (uint32_t i = 0; i < count; i++) {
895 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
896
897 if (!set)
898 continue;
899
900 anv_descriptor_set_destroy(device, pool, set);
901 }
902
903 return VK_SUCCESS;
904 }
905
906 void
907 anv_descriptor_set_write_image_view(struct anv_device *device,
908 struct anv_descriptor_set *set,
909 const VkDescriptorImageInfo * const info,
910 VkDescriptorType type,
911 uint32_t binding,
912 uint32_t element)
913 {
914 const struct anv_descriptor_set_binding_layout *bind_layout =
915 &set->layout->binding[binding];
916 struct anv_descriptor *desc =
917 &set->descriptors[bind_layout->descriptor_index + element];
918 struct anv_image_view *image_view = NULL;
919 struct anv_sampler *sampler = NULL;
920
921 assert(type == bind_layout->type);
922
923 switch (type) {
924 case VK_DESCRIPTOR_TYPE_SAMPLER:
925 sampler = anv_sampler_from_handle(info->sampler);
926 break;
927
928 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
929 image_view = anv_image_view_from_handle(info->imageView);
930 sampler = anv_sampler_from_handle(info->sampler);
931 break;
932
933 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
934 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
935 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
936 image_view = anv_image_view_from_handle(info->imageView);
937 break;
938
939 default:
940 unreachable("invalid descriptor type");
941 }
942
943 /* If this descriptor has an immutable sampler, we don't want to stomp on
944 * it.
945 */
946 sampler = bind_layout->immutable_samplers ?
947 bind_layout->immutable_samplers[element] :
948 sampler;
949
950 *desc = (struct anv_descriptor) {
951 .type = type,
952 .layout = info->imageLayout,
953 .image_view = image_view,
954 .sampler = sampler,
955 };
956 }
957
958 void
959 anv_descriptor_set_write_buffer_view(struct anv_device *device,
960 struct anv_descriptor_set *set,
961 VkDescriptorType type,
962 struct anv_buffer_view *buffer_view,
963 uint32_t binding,
964 uint32_t element)
965 {
966 const struct anv_descriptor_set_binding_layout *bind_layout =
967 &set->layout->binding[binding];
968 struct anv_descriptor *desc =
969 &set->descriptors[bind_layout->descriptor_index + element];
970
971 assert(type == bind_layout->type);
972
973 *desc = (struct anv_descriptor) {
974 .type = type,
975 .buffer_view = buffer_view,
976 };
977 }
978
979 void
980 anv_descriptor_set_write_buffer(struct anv_device *device,
981 struct anv_descriptor_set *set,
982 struct anv_state_stream *alloc_stream,
983 VkDescriptorType type,
984 struct anv_buffer *buffer,
985 uint32_t binding,
986 uint32_t element,
987 VkDeviceSize offset,
988 VkDeviceSize range)
989 {
990 const struct anv_descriptor_set_binding_layout *bind_layout =
991 &set->layout->binding[binding];
992 struct anv_descriptor *desc =
993 &set->descriptors[bind_layout->descriptor_index + element];
994
995 assert(type == bind_layout->type);
996
997 if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
998 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
999 *desc = (struct anv_descriptor) {
1000 .type = type,
1001 .buffer = buffer,
1002 .offset = offset,
1003 .range = range,
1004 };
1005 } else {
1006 assert(bind_layout->data & ANV_DESCRIPTOR_BUFFER_VIEW);
1007 struct anv_buffer_view *bview =
1008 &set->buffer_views[bind_layout->buffer_view_index + element];
1009
1010 bview->format = anv_isl_format_for_descriptor_type(type);
1011 bview->range = anv_buffer_get_range(buffer, offset, range);
1012 bview->address = anv_address_add(buffer->address, offset);
1013
1014 /* If we're writing descriptors through a push command, we need to
1015 * allocate the surface state from the command buffer. Otherwise it will
1016 * be allocated by the descriptor pool when calling
1017 * vkAllocateDescriptorSets. */
1018 if (alloc_stream)
1019 bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
1020
1021 anv_fill_buffer_surface_state(device, bview->surface_state,
1022 bview->format,
1023 bview->address, bview->range, 1);
1024
1025 *desc = (struct anv_descriptor) {
1026 .type = type,
1027 .buffer_view = bview,
1028 };
1029 }
1030 }
1031
1032 void
1033 anv_descriptor_set_write_inline_uniform_data(struct anv_device *device,
1034 struct anv_descriptor_set *set,
1035 uint32_t binding,
1036 const void *data,
1037 size_t offset,
1038 size_t size)
1039 {
1040 const struct anv_descriptor_set_binding_layout *bind_layout =
1041 &set->layout->binding[binding];
1042
1043 assert(bind_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM);
1044
1045 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset;
1046
1047 memcpy(desc_map + offset, data, size);
1048 }
1049
1050 void anv_UpdateDescriptorSets(
1051 VkDevice _device,
1052 uint32_t descriptorWriteCount,
1053 const VkWriteDescriptorSet* pDescriptorWrites,
1054 uint32_t descriptorCopyCount,
1055 const VkCopyDescriptorSet* pDescriptorCopies)
1056 {
1057 ANV_FROM_HANDLE(anv_device, device, _device);
1058
1059 for (uint32_t i = 0; i < descriptorWriteCount; i++) {
1060 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
1061 ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
1062
1063 switch (write->descriptorType) {
1064 case VK_DESCRIPTOR_TYPE_SAMPLER:
1065 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1066 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1067 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1068 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1069 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1070 anv_descriptor_set_write_image_view(device, set,
1071 write->pImageInfo + j,
1072 write->descriptorType,
1073 write->dstBinding,
1074 write->dstArrayElement + j);
1075 }
1076 break;
1077
1078 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1079 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1080 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1081 ANV_FROM_HANDLE(anv_buffer_view, bview,
1082 write->pTexelBufferView[j]);
1083
1084 anv_descriptor_set_write_buffer_view(device, set,
1085 write->descriptorType,
1086 bview,
1087 write->dstBinding,
1088 write->dstArrayElement + j);
1089 }
1090 break;
1091
1092 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1093 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1094 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1095 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1096 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1097 assert(write->pBufferInfo[j].buffer);
1098 ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
1099 assert(buffer);
1100
1101 anv_descriptor_set_write_buffer(device, set,
1102 NULL,
1103 write->descriptorType,
1104 buffer,
1105 write->dstBinding,
1106 write->dstArrayElement + j,
1107 write->pBufferInfo[j].offset,
1108 write->pBufferInfo[j].range);
1109 }
1110 break;
1111
1112 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1113 const VkWriteDescriptorSetInlineUniformBlockEXT *inline_write =
1114 vk_find_struct_const(write->pNext,
1115 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT);
1116 assert(inline_write->dataSize == write->descriptorCount);
1117 anv_descriptor_set_write_inline_uniform_data(device, set,
1118 write->dstBinding,
1119 inline_write->pData,
1120 write->dstArrayElement,
1121 inline_write->dataSize);
1122 break;
1123 }
1124
1125 default:
1126 break;
1127 }
1128 }
1129
1130 for (uint32_t i = 0; i < descriptorCopyCount; i++) {
1131 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
1132 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
1133 ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
1134
1135 const struct anv_descriptor_set_binding_layout *src_layout =
1136 &src->layout->binding[copy->srcBinding];
1137 struct anv_descriptor *src_desc =
1138 &src->descriptors[src_layout->descriptor_index];
1139 src_desc += copy->srcArrayElement;
1140
1141 const struct anv_descriptor_set_binding_layout *dst_layout =
1142 &dst->layout->binding[copy->dstBinding];
1143 struct anv_descriptor *dst_desc =
1144 &dst->descriptors[dst_layout->descriptor_index];
1145 dst_desc += copy->dstArrayElement;
1146
1147 for (uint32_t j = 0; j < copy->descriptorCount; j++)
1148 dst_desc[j] = src_desc[j];
1149
1150 if (src_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM) {
1151 assert(src_layout->data == ANV_DESCRIPTOR_INLINE_UNIFORM);
1152 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1153 copy->dstArrayElement,
1154 src->desc_mem.map + src_layout->descriptor_offset +
1155 copy->srcArrayElement,
1156 copy->descriptorCount);
1157 } else {
1158 unsigned desc_size = anv_descriptor_size(src_layout);
1159 if (desc_size > 0) {
1160 assert(desc_size == anv_descriptor_size(dst_layout));
1161 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1162 copy->dstArrayElement * desc_size,
1163 src->desc_mem.map + src_layout->descriptor_offset +
1164 copy->srcArrayElement * desc_size,
1165 copy->descriptorCount * desc_size);
1166 }
1167 }
1168 }
1169 }
1170
1171 /*
1172 * Descriptor update templates.
1173 */
1174
1175 void
1176 anv_descriptor_set_write_template(struct anv_device *device,
1177 struct anv_descriptor_set *set,
1178 struct anv_state_stream *alloc_stream,
1179 const struct anv_descriptor_update_template *template,
1180 const void *data)
1181 {
1182 for (uint32_t i = 0; i < template->entry_count; i++) {
1183 const struct anv_descriptor_template_entry *entry =
1184 &template->entries[i];
1185
1186 switch (entry->type) {
1187 case VK_DESCRIPTOR_TYPE_SAMPLER:
1188 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1189 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1190 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1191 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1192 for (uint32_t j = 0; j < entry->array_count; j++) {
1193 const VkDescriptorImageInfo *info =
1194 data + entry->offset + j * entry->stride;
1195 anv_descriptor_set_write_image_view(device, set,
1196 info, entry->type,
1197 entry->binding,
1198 entry->array_element + j);
1199 }
1200 break;
1201
1202 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1203 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1204 for (uint32_t j = 0; j < entry->array_count; j++) {
1205 const VkBufferView *_bview =
1206 data + entry->offset + j * entry->stride;
1207 ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
1208
1209 anv_descriptor_set_write_buffer_view(device, set,
1210 entry->type,
1211 bview,
1212 entry->binding,
1213 entry->array_element + j);
1214 }
1215 break;
1216
1217 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1218 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1219 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1220 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1221 for (uint32_t j = 0; j < entry->array_count; j++) {
1222 const VkDescriptorBufferInfo *info =
1223 data + entry->offset + j * entry->stride;
1224 ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
1225
1226 anv_descriptor_set_write_buffer(device, set,
1227 alloc_stream,
1228 entry->type,
1229 buffer,
1230 entry->binding,
1231 entry->array_element + j,
1232 info->offset, info->range);
1233 }
1234 break;
1235
1236 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
1237 anv_descriptor_set_write_inline_uniform_data(device, set,
1238 entry->binding,
1239 data + entry->offset,
1240 entry->array_element,
1241 entry->array_count);
1242 break;
1243
1244 default:
1245 break;
1246 }
1247 }
1248 }
1249
1250 VkResult anv_CreateDescriptorUpdateTemplate(
1251 VkDevice _device,
1252 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
1253 const VkAllocationCallbacks* pAllocator,
1254 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate)
1255 {
1256 ANV_FROM_HANDLE(anv_device, device, _device);
1257 struct anv_descriptor_update_template *template;
1258
1259 size_t size = sizeof(*template) +
1260 pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
1261 template = vk_alloc2(&device->alloc, pAllocator, size, 8,
1262 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1263 if (template == NULL)
1264 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1265
1266 template->bind_point = pCreateInfo->pipelineBindPoint;
1267
1268 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
1269 template->set = pCreateInfo->set;
1270
1271 template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
1272 for (uint32_t i = 0; i < template->entry_count; i++) {
1273 const VkDescriptorUpdateTemplateEntry *pEntry =
1274 &pCreateInfo->pDescriptorUpdateEntries[i];
1275
1276 template->entries[i] = (struct anv_descriptor_template_entry) {
1277 .type = pEntry->descriptorType,
1278 .binding = pEntry->dstBinding,
1279 .array_element = pEntry->dstArrayElement,
1280 .array_count = pEntry->descriptorCount,
1281 .offset = pEntry->offset,
1282 .stride = pEntry->stride,
1283 };
1284 }
1285
1286 *pDescriptorUpdateTemplate =
1287 anv_descriptor_update_template_to_handle(template);
1288
1289 return VK_SUCCESS;
1290 }
1291
1292 void anv_DestroyDescriptorUpdateTemplate(
1293 VkDevice _device,
1294 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1295 const VkAllocationCallbacks* pAllocator)
1296 {
1297 ANV_FROM_HANDLE(anv_device, device, _device);
1298 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1299 descriptorUpdateTemplate);
1300
1301 vk_free2(&device->alloc, pAllocator, template);
1302 }
1303
1304 void anv_UpdateDescriptorSetWithTemplate(
1305 VkDevice _device,
1306 VkDescriptorSet descriptorSet,
1307 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1308 const void* pData)
1309 {
1310 ANV_FROM_HANDLE(anv_device, device, _device);
1311 ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1312 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1313 descriptorUpdateTemplate);
1314
1315 anv_descriptor_set_write_template(device, set, NULL, template, pData);
1316 }