2 * Copyright © 2015 Intel Corporation
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:
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
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
30 #include "util/mesa-sha1.h"
32 #include "anv_private.h"
35 * Descriptor set layouts.
38 static enum anv_descriptor_data
39 anv_descriptor_data_for_type(const struct anv_physical_device *device,
40 VkDescriptorType type)
42 enum anv_descriptor_data data = 0;
45 case VK_DESCRIPTOR_TYPE_SAMPLER:
46 data = ANV_DESCRIPTOR_SAMPLER_STATE;
49 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
50 data = ANV_DESCRIPTOR_SURFACE_STATE |
51 ANV_DESCRIPTOR_SAMPLER_STATE;
54 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
55 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
56 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
57 data = ANV_DESCRIPTOR_SURFACE_STATE;
60 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
61 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
62 data = ANV_DESCRIPTOR_SURFACE_STATE;
63 if (device->info.gen < 9)
64 data |= ANV_DESCRIPTOR_IMAGE_PARAM;
67 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
68 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
69 data = ANV_DESCRIPTOR_SURFACE_STATE |
70 ANV_DESCRIPTOR_BUFFER_VIEW;
73 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
74 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
75 data = ANV_DESCRIPTOR_SURFACE_STATE;
79 unreachable("Unsupported descriptor type");
86 anv_descriptor_data_size(enum anv_descriptor_data data)
91 /** Returns the size in bytes of each descriptor with the given layout */
93 anv_descriptor_size(const struct anv_descriptor_set_binding_layout *layout)
95 return anv_descriptor_data_size(layout->data);
98 /** Returns the size in bytes of each descriptor of the given type
100 * This version of the function does not have access to the entire layout so
101 * it may only work on certain descriptor types where the descriptor size is
102 * entirely determined by the descriptor type. Whenever possible, code should
103 * use anv_descriptor_size() instead.
106 anv_descriptor_type_size(const struct anv_physical_device *pdevice,
107 VkDescriptorType type)
109 return anv_descriptor_data_size(anv_descriptor_data_for_type(pdevice, type));
112 void anv_GetDescriptorSetLayoutSupport(
114 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
115 VkDescriptorSetLayoutSupport* pSupport)
117 uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
119 for (uint32_t b = 0; b < pCreateInfo->bindingCount; b++) {
120 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[b];
122 switch (binding->descriptorType) {
123 case VK_DESCRIPTOR_TYPE_SAMPLER:
124 /* There is no real limit on samplers */
127 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
128 if (binding->pImmutableSamplers) {
129 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
130 ANV_FROM_HANDLE(anv_sampler, sampler,
131 binding->pImmutableSamplers[i]);
132 anv_foreach_stage(s, binding->stageFlags)
133 surface_count[s] += sampler->n_planes;
136 anv_foreach_stage(s, binding->stageFlags)
137 surface_count[s] += binding->descriptorCount;
142 anv_foreach_stage(s, binding->stageFlags)
143 surface_count[s] += binding->descriptorCount;
148 bool supported = true;
149 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
150 /* Our maximum binding table size is 250 and we need to reserve 8 for
151 * render targets. 240 is a nice round number.
153 if (surface_count[s] >= 240)
157 pSupport->supported = supported;
160 VkResult anv_CreateDescriptorSetLayout(
162 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
163 const VkAllocationCallbacks* pAllocator,
164 VkDescriptorSetLayout* pSetLayout)
166 ANV_FROM_HANDLE(anv_device, device, _device);
168 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
170 uint32_t max_binding = 0;
171 uint32_t immutable_sampler_count = 0;
172 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
173 max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
175 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
177 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
178 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
179 * pImmutableSamplers can be used to initialize a set of immutable
180 * samplers. [...] If descriptorType is not one of these descriptor
181 * types, then pImmutableSamplers is ignored.
183 * We need to be careful here and only parse pImmutableSamplers if we
184 * have one of the right descriptor types.
186 VkDescriptorType desc_type = pCreateInfo->pBindings[j].descriptorType;
187 if ((desc_type == VK_DESCRIPTOR_TYPE_SAMPLER ||
188 desc_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
189 pCreateInfo->pBindings[j].pImmutableSamplers)
190 immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
193 struct anv_descriptor_set_layout *set_layout;
194 struct anv_descriptor_set_binding_layout *bindings;
195 struct anv_sampler **samplers;
197 /* We need to allocate decriptor set layouts off the device allocator
198 * with DEVICE scope because they are reference counted and may not be
199 * destroyed when vkDestroyDescriptorSetLayout is called.
202 anv_multialloc_add(&ma, &set_layout, 1);
203 anv_multialloc_add(&ma, &bindings, max_binding + 1);
204 anv_multialloc_add(&ma, &samplers, immutable_sampler_count);
206 if (!anv_multialloc_alloc(&ma, &device->alloc,
207 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
208 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
210 memset(set_layout, 0, sizeof(*set_layout));
211 set_layout->ref_cnt = 1;
212 set_layout->binding_count = max_binding + 1;
214 for (uint32_t b = 0; b <= max_binding; b++) {
215 /* Initialize all binding_layout entries to -1 */
216 memset(&set_layout->binding[b], -1, sizeof(set_layout->binding[b]));
218 set_layout->binding[b].data = 0;
219 set_layout->binding[b].array_size = 0;
220 set_layout->binding[b].immutable_samplers = NULL;
223 /* Initialize all samplers to 0 */
224 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
226 uint32_t buffer_view_count = 0;
227 uint32_t dynamic_offset_count = 0;
228 uint32_t descriptor_buffer_size = 0;
230 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
231 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
232 uint32_t b = binding->binding;
233 /* We temporarily store the pointer to the binding in the
234 * immutable_samplers pointer. This provides us with a quick-and-dirty
235 * way to sort the bindings by binding number.
237 set_layout->binding[b].immutable_samplers = (void *)binding;
240 for (uint32_t b = 0; b <= max_binding; b++) {
241 const VkDescriptorSetLayoutBinding *binding =
242 (void *)set_layout->binding[b].immutable_samplers;
247 /* We temporarily stashed the pointer to the binding in the
248 * immutable_samplers pointer. Now that we've pulled it back out
249 * again, we reset immutable_samplers to NULL.
251 set_layout->binding[b].immutable_samplers = NULL;
253 if (binding->descriptorCount == 0)
257 set_layout->binding[b].type = binding->descriptorType;
259 set_layout->binding[b].data =
260 anv_descriptor_data_for_type(&device->instance->physicalDevice,
261 binding->descriptorType);
262 set_layout->binding[b].array_size = binding->descriptorCount;
263 set_layout->binding[b].descriptor_index = set_layout->size;
264 set_layout->size += binding->descriptorCount;
266 if (set_layout->binding[b].data & ANV_DESCRIPTOR_BUFFER_VIEW) {
267 set_layout->binding[b].buffer_view_index = buffer_view_count;
268 buffer_view_count += binding->descriptorCount;
271 switch (binding->descriptorType) {
272 case VK_DESCRIPTOR_TYPE_SAMPLER:
273 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
274 if (binding->pImmutableSamplers) {
275 set_layout->binding[b].immutable_samplers = samplers;
276 samplers += binding->descriptorCount;
278 for (uint32_t i = 0; i < binding->descriptorCount; i++)
279 set_layout->binding[b].immutable_samplers[i] =
280 anv_sampler_from_handle(binding->pImmutableSamplers[i]);
287 switch (binding->descriptorType) {
288 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
289 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
290 set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
291 dynamic_offset_count += binding->descriptorCount;
298 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
299 descriptor_buffer_size += anv_descriptor_size(&set_layout->binding[b]) *
300 binding->descriptorCount;
302 set_layout->shader_stages |= binding->stageFlags;
305 set_layout->buffer_view_count = buffer_view_count;
306 set_layout->dynamic_offset_count = dynamic_offset_count;
307 set_layout->descriptor_buffer_size = descriptor_buffer_size;
309 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
314 void anv_DestroyDescriptorSetLayout(
316 VkDescriptorSetLayout _set_layout,
317 const VkAllocationCallbacks* pAllocator)
319 ANV_FROM_HANDLE(anv_device, device, _device);
320 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
325 anv_descriptor_set_layout_unref(device, set_layout);
328 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
331 sha1_update_immutable_sampler(struct mesa_sha1 *ctx,
332 const struct anv_sampler *sampler)
334 if (!sampler->conversion)
337 /* The only thing that affects the shader is ycbcr conversion */
338 _mesa_sha1_update(ctx, sampler->conversion,
339 sizeof(*sampler->conversion));
343 sha1_update_descriptor_set_binding_layout(struct mesa_sha1 *ctx,
344 const struct anv_descriptor_set_binding_layout *layout)
346 SHA1_UPDATE_VALUE(ctx, layout->data);
347 SHA1_UPDATE_VALUE(ctx, layout->array_size);
348 SHA1_UPDATE_VALUE(ctx, layout->descriptor_index);
349 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_index);
350 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_index);
351 SHA1_UPDATE_VALUE(ctx, layout->descriptor_offset);
353 if (layout->immutable_samplers) {
354 for (uint16_t i = 0; i < layout->array_size; i++)
355 sha1_update_immutable_sampler(ctx, layout->immutable_samplers[i]);
360 sha1_update_descriptor_set_layout(struct mesa_sha1 *ctx,
361 const struct anv_descriptor_set_layout *layout)
363 SHA1_UPDATE_VALUE(ctx, layout->binding_count);
364 SHA1_UPDATE_VALUE(ctx, layout->size);
365 SHA1_UPDATE_VALUE(ctx, layout->shader_stages);
366 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_count);
367 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_count);
368 SHA1_UPDATE_VALUE(ctx, layout->descriptor_buffer_size);
370 for (uint16_t i = 0; i < layout->binding_count; i++)
371 sha1_update_descriptor_set_binding_layout(ctx, &layout->binding[i]);
375 * Pipeline layouts. These have nothing to do with the pipeline. They are
376 * just multiple descriptor set layouts pasted together
379 VkResult anv_CreatePipelineLayout(
381 const VkPipelineLayoutCreateInfo* pCreateInfo,
382 const VkAllocationCallbacks* pAllocator,
383 VkPipelineLayout* pPipelineLayout)
385 ANV_FROM_HANDLE(anv_device, device, _device);
386 struct anv_pipeline_layout *layout;
388 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
390 layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
391 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
393 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
395 layout->num_sets = pCreateInfo->setLayoutCount;
397 unsigned dynamic_offset_count = 0;
399 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
400 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
401 pCreateInfo->pSetLayouts[set]);
402 layout->set[set].layout = set_layout;
403 anv_descriptor_set_layout_ref(set_layout);
405 layout->set[set].dynamic_offset_start = dynamic_offset_count;
406 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
407 if (set_layout->binding[b].dynamic_offset_index < 0)
410 dynamic_offset_count += set_layout->binding[b].array_size;
414 struct mesa_sha1 ctx;
415 _mesa_sha1_init(&ctx);
416 for (unsigned s = 0; s < layout->num_sets; s++) {
417 sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
418 _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
419 sizeof(layout->set[s].dynamic_offset_start));
421 _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
422 _mesa_sha1_final(&ctx, layout->sha1);
424 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
429 void anv_DestroyPipelineLayout(
431 VkPipelineLayout _pipelineLayout,
432 const VkAllocationCallbacks* pAllocator)
434 ANV_FROM_HANDLE(anv_device, device, _device);
435 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
437 if (!pipeline_layout)
440 for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
441 anv_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
443 vk_free2(&device->alloc, pAllocator, pipeline_layout);
449 * These are implemented using a big pool of memory and a free-list for the
450 * host memory allocations and a state_stream and a free list for the buffer
451 * view surface state. The spec allows us to fail to allocate due to
452 * fragmentation in all cases but two: 1) after pool reset, allocating up
453 * until the pool size with no freeing must succeed and 2) allocating and
454 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
455 * and the free lists lets us recycle blocks for case 2).
458 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
459 * ensure we can allocate the entire BO without hitting zero. The actual
460 * amount doesn't matter.
462 #define POOL_HEAP_OFFSET 64
466 VkResult anv_CreateDescriptorPool(
468 const VkDescriptorPoolCreateInfo* pCreateInfo,
469 const VkAllocationCallbacks* pAllocator,
470 VkDescriptorPool* pDescriptorPool)
472 ANV_FROM_HANDLE(anv_device, device, _device);
473 struct anv_descriptor_pool *pool;
475 uint32_t descriptor_count = 0;
476 uint32_t buffer_view_count = 0;
477 uint32_t descriptor_bo_size = 0;
478 for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
479 enum anv_descriptor_data desc_data =
480 anv_descriptor_data_for_type(&device->instance->physicalDevice,
481 pCreateInfo->pPoolSizes[i].type);
483 if (desc_data & ANV_DESCRIPTOR_BUFFER_VIEW)
484 buffer_view_count += pCreateInfo->pPoolSizes[i].descriptorCount;
486 unsigned desc_data_size = anv_descriptor_data_size(desc_data) *
487 pCreateInfo->pPoolSizes[i].descriptorCount;
488 descriptor_bo_size += desc_data_size;
490 descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
492 /* We have to align descriptor buffer allocations to 32B so that we can
493 * push descriptor buffers. This means that each descriptor buffer
494 * allocated may burn up to 32B of extra space to get the right alignment.
495 * (Technically, it's at most 28B because we're always going to start at
496 * least 4B aligned but we're being conservative here.) Allocate enough
497 * extra space that we can chop it into maxSets pieces and align each one
500 descriptor_bo_size += 32 * pCreateInfo->maxSets;
501 descriptor_bo_size = ALIGN(descriptor_bo_size, 4096);
503 const size_t pool_size =
504 pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
505 descriptor_count * sizeof(struct anv_descriptor) +
506 buffer_view_count * sizeof(struct anv_buffer_view);
507 const size_t total_size = sizeof(*pool) + pool_size;
509 pool = vk_alloc2(&device->alloc, pAllocator, total_size, 8,
510 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
512 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
514 pool->size = pool_size;
516 pool->free_list = EMPTY;
518 if (descriptor_bo_size > 0) {
519 VkResult result = anv_bo_init_new(&pool->bo, device, descriptor_bo_size);
520 if (result != VK_SUCCESS) {
521 vk_free2(&device->alloc, pAllocator, pool);
525 anv_gem_set_caching(device, pool->bo.gem_handle, I915_CACHING_CACHED);
527 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0,
528 descriptor_bo_size, 0);
529 if (pool->bo.map == NULL) {
530 anv_gem_close(device, pool->bo.gem_handle);
531 vk_free2(&device->alloc, pAllocator, pool);
532 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
535 if (device->instance->physicalDevice.use_softpin) {
536 pool->bo.flags |= EXEC_OBJECT_PINNED;
537 anv_vma_alloc(device, &pool->bo);
540 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, descriptor_bo_size);
545 anv_state_stream_init(&pool->surface_state_stream,
546 &device->surface_state_pool, 4096);
547 pool->surface_state_free_list = NULL;
549 *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
554 void anv_DestroyDescriptorPool(
556 VkDescriptorPool _pool,
557 const VkAllocationCallbacks* pAllocator)
559 ANV_FROM_HANDLE(anv_device, device, _device);
560 ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
566 anv_gem_munmap(pool->bo.map, pool->bo.size);
567 anv_vma_free(device, &pool->bo);
568 anv_gem_close(device, pool->bo.gem_handle);
570 anv_state_stream_finish(&pool->surface_state_stream);
571 vk_free2(&device->alloc, pAllocator, pool);
574 VkResult anv_ResetDescriptorPool(
576 VkDescriptorPool descriptorPool,
577 VkDescriptorPoolResetFlags flags)
579 ANV_FROM_HANDLE(anv_device, device, _device);
580 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
583 pool->free_list = EMPTY;
586 util_vma_heap_finish(&pool->bo_heap);
587 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, pool->bo.size);
590 anv_state_stream_finish(&pool->surface_state_stream);
591 anv_state_stream_init(&pool->surface_state_stream,
592 &device->surface_state_pool, 4096);
593 pool->surface_state_free_list = NULL;
598 struct pool_free_list_entry {
604 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool *pool,
606 struct anv_descriptor_set **set)
608 if (size <= pool->size - pool->next) {
609 *set = (struct anv_descriptor_set *) (pool->data + pool->next);
613 struct pool_free_list_entry *entry;
614 uint32_t *link = &pool->free_list;
615 for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
616 entry = (struct pool_free_list_entry *) (pool->data + f);
617 if (size <= entry->size) {
619 *set = (struct anv_descriptor_set *) entry;
625 if (pool->free_list != EMPTY) {
626 return vk_error(VK_ERROR_FRAGMENTED_POOL);
628 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
634 anv_descriptor_pool_free_set(struct anv_descriptor_pool *pool,
635 struct anv_descriptor_set *set)
637 /* Put the descriptor set allocation back on the free list. */
638 const uint32_t index = (char *) set - pool->data;
639 if (index + set->size == pool->next) {
642 struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
643 entry->next = pool->free_list;
644 entry->size = set->size;
645 pool->free_list = (char *) entry - pool->data;
649 struct surface_state_free_list_entry {
651 struct anv_state state;
654 static struct anv_state
655 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool *pool)
657 struct surface_state_free_list_entry *entry =
658 pool->surface_state_free_list;
661 struct anv_state state = entry->state;
662 pool->surface_state_free_list = entry->next;
663 assert(state.alloc_size == 64);
666 return anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
671 anv_descriptor_pool_free_state(struct anv_descriptor_pool *pool,
672 struct anv_state state)
674 /* Put the buffer view surface state back on the free list. */
675 struct surface_state_free_list_entry *entry = state.map;
676 entry->next = pool->surface_state_free_list;
677 entry->state = state;
678 pool->surface_state_free_list = entry;
682 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout)
685 sizeof(struct anv_descriptor_set) +
686 layout->size * sizeof(struct anv_descriptor) +
687 layout->buffer_view_count * sizeof(struct anv_buffer_view);
691 anv_descriptor_set_create(struct anv_device *device,
692 struct anv_descriptor_pool *pool,
693 struct anv_descriptor_set_layout *layout,
694 struct anv_descriptor_set **out_set)
696 struct anv_descriptor_set *set;
697 const size_t size = anv_descriptor_set_layout_size(layout);
699 VkResult result = anv_descriptor_pool_alloc_set(pool, size, &set);
700 if (result != VK_SUCCESS)
703 if (layout->descriptor_buffer_size) {
704 /* Align the size to 32 so that alignment gaps don't cause extra holes
705 * in the heap which can lead to bad performance.
707 uint64_t pool_vma_offset =
708 util_vma_heap_alloc(&pool->bo_heap,
709 ALIGN(layout->descriptor_buffer_size, 32), 32);
710 if (pool_vma_offset == 0) {
711 anv_descriptor_pool_free_set(pool, set);
712 return vk_error(VK_ERROR_FRAGMENTED_POOL);
714 assert(pool_vma_offset >= POOL_HEAP_OFFSET &&
715 pool_vma_offset - POOL_HEAP_OFFSET <= INT32_MAX);
716 set->desc_mem.offset = pool_vma_offset - POOL_HEAP_OFFSET;
717 set->desc_mem.alloc_size = layout->descriptor_buffer_size;
718 set->desc_mem.map = pool->bo.map + set->desc_mem.offset;
720 set->desc_surface_state = anv_descriptor_pool_alloc_state(pool);
721 anv_fill_buffer_surface_state(device, set->desc_surface_state,
722 ISL_FORMAT_R32G32B32A32_FLOAT,
723 (struct anv_address) {
725 .offset = set->desc_mem.offset,
727 layout->descriptor_buffer_size, 1);
729 set->desc_mem = ANV_STATE_NULL;
730 set->desc_surface_state = ANV_STATE_NULL;
734 set->layout = layout;
735 anv_descriptor_set_layout_ref(layout);
739 (struct anv_buffer_view *) &set->descriptors[layout->size];
740 set->buffer_view_count = layout->buffer_view_count;
742 /* By defining the descriptors to be zero now, we can later verify that
743 * a descriptor has not been populated with user data.
745 memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
747 /* Go through and fill out immutable samplers if we have any */
748 struct anv_descriptor *desc = set->descriptors;
749 for (uint32_t b = 0; b < layout->binding_count; b++) {
750 if (layout->binding[b].immutable_samplers) {
751 for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
752 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
753 * UpdateDescriptorSets if needed. However, if the descriptor
754 * set has an immutable sampler, UpdateDescriptorSets may never
755 * touch it, so we need to make sure it's 100% valid now.
757 desc[i] = (struct anv_descriptor) {
758 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
759 .sampler = layout->binding[b].immutable_samplers[i],
763 desc += layout->binding[b].array_size;
766 /* Allocate surface state for the buffer views. */
767 for (uint32_t b = 0; b < layout->buffer_view_count; b++) {
768 set->buffer_views[b].surface_state =
769 anv_descriptor_pool_alloc_state(pool);
778 anv_descriptor_set_destroy(struct anv_device *device,
779 struct anv_descriptor_pool *pool,
780 struct anv_descriptor_set *set)
782 anv_descriptor_set_layout_unref(device, set->layout);
784 if (set->desc_mem.alloc_size) {
785 util_vma_heap_free(&pool->bo_heap,
786 (uint64_t)set->desc_mem.offset + POOL_HEAP_OFFSET,
787 set->desc_mem.alloc_size);
788 anv_descriptor_pool_free_state(pool, set->desc_surface_state);
791 for (uint32_t b = 0; b < set->buffer_view_count; b++)
792 anv_descriptor_pool_free_state(pool, set->buffer_views[b].surface_state);
794 anv_descriptor_pool_free_set(pool, set);
797 VkResult anv_AllocateDescriptorSets(
799 const VkDescriptorSetAllocateInfo* pAllocateInfo,
800 VkDescriptorSet* pDescriptorSets)
802 ANV_FROM_HANDLE(anv_device, device, _device);
803 ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
805 VkResult result = VK_SUCCESS;
806 struct anv_descriptor_set *set;
809 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
810 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
811 pAllocateInfo->pSetLayouts[i]);
813 result = anv_descriptor_set_create(device, pool, layout, &set);
814 if (result != VK_SUCCESS)
817 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
820 if (result != VK_SUCCESS)
821 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
827 VkResult anv_FreeDescriptorSets(
829 VkDescriptorPool descriptorPool,
831 const VkDescriptorSet* pDescriptorSets)
833 ANV_FROM_HANDLE(anv_device, device, _device);
834 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
836 for (uint32_t i = 0; i < count; i++) {
837 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
842 anv_descriptor_set_destroy(device, pool, set);
849 anv_descriptor_set_write_image_view(struct anv_device *device,
850 struct anv_descriptor_set *set,
851 const VkDescriptorImageInfo * const info,
852 VkDescriptorType type,
856 const struct anv_descriptor_set_binding_layout *bind_layout =
857 &set->layout->binding[binding];
858 struct anv_descriptor *desc =
859 &set->descriptors[bind_layout->descriptor_index + element];
860 struct anv_image_view *image_view = NULL;
861 struct anv_sampler *sampler = NULL;
863 assert(type == bind_layout->type);
866 case VK_DESCRIPTOR_TYPE_SAMPLER:
867 sampler = anv_sampler_from_handle(info->sampler);
870 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
871 image_view = anv_image_view_from_handle(info->imageView);
872 sampler = anv_sampler_from_handle(info->sampler);
875 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
876 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
877 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
878 image_view = anv_image_view_from_handle(info->imageView);
882 unreachable("invalid descriptor type");
885 /* If this descriptor has an immutable sampler, we don't want to stomp on
888 sampler = bind_layout->immutable_samplers ?
889 bind_layout->immutable_samplers[element] :
892 *desc = (struct anv_descriptor) {
894 .layout = info->imageLayout,
895 .image_view = image_view,
901 anv_descriptor_set_write_buffer_view(struct anv_device *device,
902 struct anv_descriptor_set *set,
903 VkDescriptorType type,
904 struct anv_buffer_view *buffer_view,
908 const struct anv_descriptor_set_binding_layout *bind_layout =
909 &set->layout->binding[binding];
910 struct anv_descriptor *desc =
911 &set->descriptors[bind_layout->descriptor_index + element];
913 assert(type == bind_layout->type);
915 *desc = (struct anv_descriptor) {
917 .buffer_view = buffer_view,
922 anv_descriptor_set_write_buffer(struct anv_device *device,
923 struct anv_descriptor_set *set,
924 struct anv_state_stream *alloc_stream,
925 VkDescriptorType type,
926 struct anv_buffer *buffer,
932 const struct anv_descriptor_set_binding_layout *bind_layout =
933 &set->layout->binding[binding];
934 struct anv_descriptor *desc =
935 &set->descriptors[bind_layout->descriptor_index + element];
937 assert(type == bind_layout->type);
939 if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
940 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
941 *desc = (struct anv_descriptor) {
948 assert(bind_layout->data & ANV_DESCRIPTOR_BUFFER_VIEW);
949 struct anv_buffer_view *bview =
950 &set->buffer_views[bind_layout->buffer_view_index + element];
952 bview->format = anv_isl_format_for_descriptor_type(type);
953 bview->range = anv_buffer_get_range(buffer, offset, range);
954 bview->address = anv_address_add(buffer->address, offset);
956 /* If we're writing descriptors through a push command, we need to
957 * allocate the surface state from the command buffer. Otherwise it will
958 * be allocated by the descriptor pool when calling
959 * vkAllocateDescriptorSets. */
961 bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
963 anv_fill_buffer_surface_state(device, bview->surface_state,
965 bview->address, bview->range, 1);
967 *desc = (struct anv_descriptor) {
969 .buffer_view = bview,
974 void anv_UpdateDescriptorSets(
976 uint32_t descriptorWriteCount,
977 const VkWriteDescriptorSet* pDescriptorWrites,
978 uint32_t descriptorCopyCount,
979 const VkCopyDescriptorSet* pDescriptorCopies)
981 ANV_FROM_HANDLE(anv_device, device, _device);
983 for (uint32_t i = 0; i < descriptorWriteCount; i++) {
984 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
985 ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
987 switch (write->descriptorType) {
988 case VK_DESCRIPTOR_TYPE_SAMPLER:
989 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
990 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
991 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
992 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
993 for (uint32_t j = 0; j < write->descriptorCount; j++) {
994 anv_descriptor_set_write_image_view(device, set,
995 write->pImageInfo + j,
996 write->descriptorType,
998 write->dstArrayElement + j);
1002 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1003 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1004 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1005 ANV_FROM_HANDLE(anv_buffer_view, bview,
1006 write->pTexelBufferView[j]);
1008 anv_descriptor_set_write_buffer_view(device, set,
1009 write->descriptorType,
1012 write->dstArrayElement + j);
1016 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1017 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1018 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1019 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1020 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1021 assert(write->pBufferInfo[j].buffer);
1022 ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
1025 anv_descriptor_set_write_buffer(device, set,
1027 write->descriptorType,
1030 write->dstArrayElement + j,
1031 write->pBufferInfo[j].offset,
1032 write->pBufferInfo[j].range);
1041 for (uint32_t i = 0; i < descriptorCopyCount; i++) {
1042 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
1043 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
1044 ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
1046 const struct anv_descriptor_set_binding_layout *src_layout =
1047 &src->layout->binding[copy->srcBinding];
1048 struct anv_descriptor *src_desc =
1049 &src->descriptors[src_layout->descriptor_index];
1050 src_desc += copy->srcArrayElement;
1052 const struct anv_descriptor_set_binding_layout *dst_layout =
1053 &dst->layout->binding[copy->dstBinding];
1054 struct anv_descriptor *dst_desc =
1055 &dst->descriptors[dst_layout->descriptor_index];
1056 dst_desc += copy->dstArrayElement;
1058 for (uint32_t j = 0; j < copy->descriptorCount; j++)
1059 dst_desc[j] = src_desc[j];
1061 unsigned desc_size = anv_descriptor_size(src_layout);
1062 if (desc_size > 0) {
1063 assert(desc_size == anv_descriptor_size(dst_layout));
1064 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1065 copy->dstArrayElement * desc_size,
1066 src->desc_mem.map + src_layout->descriptor_offset +
1067 copy->srcArrayElement * desc_size,
1068 copy->descriptorCount * desc_size);
1074 * Descriptor update templates.
1078 anv_descriptor_set_write_template(struct anv_device *device,
1079 struct anv_descriptor_set *set,
1080 struct anv_state_stream *alloc_stream,
1081 const struct anv_descriptor_update_template *template,
1084 for (uint32_t i = 0; i < template->entry_count; i++) {
1085 const struct anv_descriptor_template_entry *entry =
1086 &template->entries[i];
1088 switch (entry->type) {
1089 case VK_DESCRIPTOR_TYPE_SAMPLER:
1090 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1091 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1092 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1093 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1094 for (uint32_t j = 0; j < entry->array_count; j++) {
1095 const VkDescriptorImageInfo *info =
1096 data + entry->offset + j * entry->stride;
1097 anv_descriptor_set_write_image_view(device, set,
1100 entry->array_element + j);
1104 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1105 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1106 for (uint32_t j = 0; j < entry->array_count; j++) {
1107 const VkBufferView *_bview =
1108 data + entry->offset + j * entry->stride;
1109 ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
1111 anv_descriptor_set_write_buffer_view(device, set,
1115 entry->array_element + j);
1119 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1120 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1121 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1122 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1123 for (uint32_t j = 0; j < entry->array_count; j++) {
1124 const VkDescriptorBufferInfo *info =
1125 data + entry->offset + j * entry->stride;
1126 ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
1128 anv_descriptor_set_write_buffer(device, set,
1133 entry->array_element + j,
1134 info->offset, info->range);
1144 VkResult anv_CreateDescriptorUpdateTemplate(
1146 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
1147 const VkAllocationCallbacks* pAllocator,
1148 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate)
1150 ANV_FROM_HANDLE(anv_device, device, _device);
1151 struct anv_descriptor_update_template *template;
1153 size_t size = sizeof(*template) +
1154 pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
1155 template = vk_alloc2(&device->alloc, pAllocator, size, 8,
1156 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1157 if (template == NULL)
1158 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1160 template->bind_point = pCreateInfo->pipelineBindPoint;
1162 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
1163 template->set = pCreateInfo->set;
1165 template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
1166 for (uint32_t i = 0; i < template->entry_count; i++) {
1167 const VkDescriptorUpdateTemplateEntry *pEntry =
1168 &pCreateInfo->pDescriptorUpdateEntries[i];
1170 template->entries[i] = (struct anv_descriptor_template_entry) {
1171 .type = pEntry->descriptorType,
1172 .binding = pEntry->dstBinding,
1173 .array_element = pEntry->dstArrayElement,
1174 .array_count = pEntry->descriptorCount,
1175 .offset = pEntry->offset,
1176 .stride = pEntry->stride,
1180 *pDescriptorUpdateTemplate =
1181 anv_descriptor_update_template_to_handle(template);
1186 void anv_DestroyDescriptorUpdateTemplate(
1188 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1189 const VkAllocationCallbacks* pAllocator)
1191 ANV_FROM_HANDLE(anv_device, device, _device);
1192 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1193 descriptorUpdateTemplate);
1195 vk_free2(&device->alloc, pAllocator, template);
1198 void anv_UpdateDescriptorSetWithTemplate(
1200 VkDescriptorSet descriptorSet,
1201 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1204 ANV_FROM_HANDLE(anv_device, device, _device);
1205 ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1206 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1207 descriptorUpdateTemplate);
1209 anv_descriptor_set_write_template(device, set, NULL, template, pData);