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