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 void anv_GetDescriptorSetLayoutSupport(
40 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
41 VkDescriptorSetLayoutSupport* pSupport)
43 uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
45 for (uint32_t b = 0; b < pCreateInfo->bindingCount; b++) {
46 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[b];
48 switch (binding->descriptorType) {
49 case VK_DESCRIPTOR_TYPE_SAMPLER:
50 /* There is no real limit on samplers */
53 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
54 if (binding->pImmutableSamplers) {
55 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
56 ANV_FROM_HANDLE(anv_sampler, sampler,
57 binding->pImmutableSamplers[i]);
58 anv_foreach_stage(s, binding->stageFlags)
59 surface_count[s] += sampler->n_planes;
62 anv_foreach_stage(s, binding->stageFlags)
63 surface_count[s] += binding->descriptorCount;
68 anv_foreach_stage(s, binding->stageFlags)
69 surface_count[s] += binding->descriptorCount;
74 bool supported = true;
75 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
76 /* Our maximum binding table size is 250 and we need to reserve 8 for
77 * render targets. 240 is a nice round number.
79 if (surface_count[s] >= 240)
83 pSupport->supported = supported;
86 VkResult anv_CreateDescriptorSetLayout(
88 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
89 const VkAllocationCallbacks* pAllocator,
90 VkDescriptorSetLayout* pSetLayout)
92 ANV_FROM_HANDLE(anv_device, device, _device);
94 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
96 uint32_t max_binding = 0;
97 uint32_t immutable_sampler_count = 0;
98 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
99 max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
101 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
103 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
104 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
105 * pImmutableSamplers can be used to initialize a set of immutable
106 * samplers. [...] If descriptorType is not one of these descriptor
107 * types, then pImmutableSamplers is ignored.
109 * We need to be careful here and only parse pImmutableSamplers if we
110 * have one of the right descriptor types.
112 VkDescriptorType desc_type = pCreateInfo->pBindings[j].descriptorType;
113 if ((desc_type == VK_DESCRIPTOR_TYPE_SAMPLER ||
114 desc_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
115 pCreateInfo->pBindings[j].pImmutableSamplers)
116 immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
119 struct anv_descriptor_set_layout *set_layout;
120 struct anv_descriptor_set_binding_layout *bindings;
121 struct anv_sampler **samplers;
123 /* We need to allocate decriptor set layouts off the device allocator
124 * with DEVICE scope because they are reference counted and may not be
125 * destroyed when vkDestroyDescriptorSetLayout is called.
128 anv_multialloc_add(&ma, &set_layout, 1);
129 anv_multialloc_add(&ma, &bindings, max_binding + 1);
130 anv_multialloc_add(&ma, &samplers, immutable_sampler_count);
132 if (!anv_multialloc_alloc(&ma, &device->alloc,
133 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
134 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
136 memset(set_layout, 0, sizeof(*set_layout));
137 set_layout->ref_cnt = 1;
138 set_layout->binding_count = max_binding + 1;
140 for (uint32_t b = 0; b <= max_binding; b++) {
141 /* Initialize all binding_layout entries to -1 */
142 memset(&set_layout->binding[b], -1, sizeof(set_layout->binding[b]));
144 set_layout->binding[b].array_size = 0;
145 set_layout->binding[b].immutable_samplers = NULL;
148 /* Initialize all samplers to 0 */
149 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
151 uint32_t sampler_count[MESA_SHADER_STAGES] = { 0, };
152 uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
153 uint32_t image_count[MESA_SHADER_STAGES] = { 0, };
154 uint32_t buffer_count = 0;
155 uint32_t dynamic_offset_count = 0;
157 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
158 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
159 uint32_t b = binding->binding;
160 /* We temporarily store the pointer to the binding in the
161 * immutable_samplers pointer. This provides us with a quick-and-dirty
162 * way to sort the bindings by binding number.
164 set_layout->binding[b].immutable_samplers = (void *)binding;
167 for (uint32_t b = 0; b <= max_binding; b++) {
168 const VkDescriptorSetLayoutBinding *binding =
169 (void *)set_layout->binding[b].immutable_samplers;
174 /* We temporarily stashed the pointer to the binding in the
175 * immutable_samplers pointer. Now that we've pulled it back out
176 * again, we reset immutable_samplers to NULL.
178 set_layout->binding[b].immutable_samplers = NULL;
180 if (binding->descriptorCount == 0)
184 set_layout->binding[b].type = binding->descriptorType;
186 set_layout->binding[b].array_size = binding->descriptorCount;
187 set_layout->binding[b].descriptor_index = set_layout->size;
188 set_layout->size += binding->descriptorCount;
190 switch (binding->descriptorType) {
191 case VK_DESCRIPTOR_TYPE_SAMPLER:
192 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
193 anv_foreach_stage(s, binding->stageFlags) {
194 set_layout->binding[b].stage[s].sampler_index = sampler_count[s];
195 sampler_count[s] += binding->descriptorCount;
198 if (binding->pImmutableSamplers) {
199 set_layout->binding[b].immutable_samplers = samplers;
200 samplers += binding->descriptorCount;
202 for (uint32_t i = 0; i < binding->descriptorCount; i++)
203 set_layout->binding[b].immutable_samplers[i] =
204 anv_sampler_from_handle(binding->pImmutableSamplers[i]);
211 switch (binding->descriptorType) {
212 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
213 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
214 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
215 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
216 set_layout->binding[b].buffer_index = buffer_count;
217 buffer_count += binding->descriptorCount;
220 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
221 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
222 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
223 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
224 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
225 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
226 anv_foreach_stage(s, binding->stageFlags) {
227 set_layout->binding[b].stage[s].surface_index = surface_count[s];
228 surface_count[s] += binding->descriptorCount;
235 switch (binding->descriptorType) {
236 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
237 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
238 set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
239 dynamic_offset_count += binding->descriptorCount;
245 switch (binding->descriptorType) {
246 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
247 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
248 anv_foreach_stage(s, binding->stageFlags) {
249 set_layout->binding[b].stage[s].image_index = image_count[s];
250 image_count[s] += binding->descriptorCount;
257 set_layout->shader_stages |= binding->stageFlags;
260 set_layout->buffer_count = buffer_count;
261 set_layout->dynamic_offset_count = dynamic_offset_count;
263 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
268 void anv_DestroyDescriptorSetLayout(
270 VkDescriptorSetLayout _set_layout,
271 const VkAllocationCallbacks* pAllocator)
273 ANV_FROM_HANDLE(anv_device, device, _device);
274 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
279 anv_descriptor_set_layout_unref(device, set_layout);
282 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
285 sha1_update_immutable_sampler(struct mesa_sha1 *ctx,
286 const struct anv_sampler *sampler)
288 if (!sampler->conversion)
291 /* The only thing that affects the shader is ycbcr conversion */
292 _mesa_sha1_update(ctx, sampler->conversion,
293 sizeof(*sampler->conversion));
297 sha1_update_descriptor_set_binding_layout(struct mesa_sha1 *ctx,
298 const struct anv_descriptor_set_binding_layout *layout)
300 SHA1_UPDATE_VALUE(ctx, layout->array_size);
301 SHA1_UPDATE_VALUE(ctx, layout->descriptor_index);
302 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_index);
303 SHA1_UPDATE_VALUE(ctx, layout->buffer_index);
304 _mesa_sha1_update(ctx, layout->stage, sizeof(layout->stage));
306 if (layout->immutable_samplers) {
307 for (uint16_t i = 0; i < layout->array_size; i++)
308 sha1_update_immutable_sampler(ctx, layout->immutable_samplers[i]);
313 sha1_update_descriptor_set_layout(struct mesa_sha1 *ctx,
314 const struct anv_descriptor_set_layout *layout)
316 SHA1_UPDATE_VALUE(ctx, layout->binding_count);
317 SHA1_UPDATE_VALUE(ctx, layout->size);
318 SHA1_UPDATE_VALUE(ctx, layout->shader_stages);
319 SHA1_UPDATE_VALUE(ctx, layout->buffer_count);
320 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_count);
322 for (uint16_t i = 0; i < layout->binding_count; i++)
323 sha1_update_descriptor_set_binding_layout(ctx, &layout->binding[i]);
327 * Pipeline layouts. These have nothing to do with the pipeline. They are
328 * just multiple descriptor set layouts pasted together
331 VkResult anv_CreatePipelineLayout(
333 const VkPipelineLayoutCreateInfo* pCreateInfo,
334 const VkAllocationCallbacks* pAllocator,
335 VkPipelineLayout* pPipelineLayout)
337 ANV_FROM_HANDLE(anv_device, device, _device);
338 struct anv_pipeline_layout *layout;
340 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
342 layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
343 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
345 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
347 layout->num_sets = pCreateInfo->setLayoutCount;
349 unsigned dynamic_offset_count = 0;
351 memset(layout->stage, 0, sizeof(layout->stage));
352 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
353 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
354 pCreateInfo->pSetLayouts[set]);
355 layout->set[set].layout = set_layout;
356 anv_descriptor_set_layout_ref(set_layout);
358 layout->set[set].dynamic_offset_start = dynamic_offset_count;
359 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
360 if (set_layout->binding[b].dynamic_offset_index < 0)
363 dynamic_offset_count += set_layout->binding[b].array_size;
364 for (gl_shader_stage s = 0; s < MESA_SHADER_STAGES; s++) {
365 if (set_layout->binding[b].stage[s].surface_index >= 0)
366 layout->stage[s].has_dynamic_offsets = true;
371 struct mesa_sha1 ctx;
372 _mesa_sha1_init(&ctx);
373 for (unsigned s = 0; s < layout->num_sets; s++) {
374 sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
375 _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
376 sizeof(layout->set[s].dynamic_offset_start));
378 _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
379 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
380 _mesa_sha1_update(&ctx, &layout->stage[s].has_dynamic_offsets,
381 sizeof(layout->stage[s].has_dynamic_offsets));
383 _mesa_sha1_final(&ctx, layout->sha1);
385 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
390 void anv_DestroyPipelineLayout(
392 VkPipelineLayout _pipelineLayout,
393 const VkAllocationCallbacks* pAllocator)
395 ANV_FROM_HANDLE(anv_device, device, _device);
396 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
398 if (!pipeline_layout)
401 for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
402 anv_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
404 vk_free2(&device->alloc, pAllocator, pipeline_layout);
410 * These are implemented using a big pool of memory and a free-list for the
411 * host memory allocations and a state_stream and a free list for the buffer
412 * view surface state. The spec allows us to fail to allocate due to
413 * fragmentation in all cases but two: 1) after pool reset, allocating up
414 * until the pool size with no freeing must succeed and 2) allocating and
415 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
416 * and the free lists lets us recycle blocks for case 2).
421 VkResult anv_CreateDescriptorPool(
423 const VkDescriptorPoolCreateInfo* pCreateInfo,
424 const VkAllocationCallbacks* pAllocator,
425 VkDescriptorPool* pDescriptorPool)
427 ANV_FROM_HANDLE(anv_device, device, _device);
428 struct anv_descriptor_pool *pool;
430 uint32_t descriptor_count = 0;
431 uint32_t buffer_count = 0;
432 for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
433 switch (pCreateInfo->pPoolSizes[i].type) {
434 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
435 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
436 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
437 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
438 buffer_count += pCreateInfo->pPoolSizes[i].descriptorCount;
440 descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
445 const size_t pool_size =
446 pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
447 descriptor_count * sizeof(struct anv_descriptor) +
448 buffer_count * sizeof(struct anv_buffer_view);
449 const size_t total_size = sizeof(*pool) + pool_size;
451 pool = vk_alloc2(&device->alloc, pAllocator, total_size, 8,
452 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
454 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
456 pool->size = pool_size;
458 pool->free_list = EMPTY;
460 anv_state_stream_init(&pool->surface_state_stream,
461 &device->surface_state_pool, 4096);
462 pool->surface_state_free_list = NULL;
464 *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
469 void anv_DestroyDescriptorPool(
471 VkDescriptorPool _pool,
472 const VkAllocationCallbacks* pAllocator)
474 ANV_FROM_HANDLE(anv_device, device, _device);
475 ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
480 anv_state_stream_finish(&pool->surface_state_stream);
481 vk_free2(&device->alloc, pAllocator, pool);
484 VkResult anv_ResetDescriptorPool(
486 VkDescriptorPool descriptorPool,
487 VkDescriptorPoolResetFlags flags)
489 ANV_FROM_HANDLE(anv_device, device, _device);
490 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
493 pool->free_list = EMPTY;
494 anv_state_stream_finish(&pool->surface_state_stream);
495 anv_state_stream_init(&pool->surface_state_stream,
496 &device->surface_state_pool, 4096);
497 pool->surface_state_free_list = NULL;
502 struct pool_free_list_entry {
508 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout)
511 sizeof(struct anv_descriptor_set) +
512 layout->size * sizeof(struct anv_descriptor) +
513 layout->buffer_count * sizeof(struct anv_buffer_view);
516 struct surface_state_free_list_entry {
518 struct anv_state state;
522 anv_descriptor_set_create(struct anv_device *device,
523 struct anv_descriptor_pool *pool,
524 struct anv_descriptor_set_layout *layout,
525 struct anv_descriptor_set **out_set)
527 struct anv_descriptor_set *set;
528 const size_t size = anv_descriptor_set_layout_size(layout);
531 if (size <= pool->size - pool->next) {
532 set = (struct anv_descriptor_set *) (pool->data + pool->next);
535 struct pool_free_list_entry *entry;
536 uint32_t *link = &pool->free_list;
537 for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
538 entry = (struct pool_free_list_entry *) (pool->data + f);
539 if (size <= entry->size) {
541 set = (struct anv_descriptor_set *) entry;
549 if (pool->free_list != EMPTY) {
550 return vk_error(VK_ERROR_FRAGMENTED_POOL);
552 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
556 set->layout = layout;
557 anv_descriptor_set_layout_ref(layout);
561 (struct anv_buffer_view *) &set->descriptors[layout->size];
562 set->buffer_count = layout->buffer_count;
564 /* By defining the descriptors to be zero now, we can later verify that
565 * a descriptor has not been populated with user data.
567 memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
569 /* Go through and fill out immutable samplers if we have any */
570 struct anv_descriptor *desc = set->descriptors;
571 for (uint32_t b = 0; b < layout->binding_count; b++) {
572 if (layout->binding[b].immutable_samplers) {
573 for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
574 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
575 * UpdateDescriptorSets if needed. However, if the descriptor
576 * set has an immutable sampler, UpdateDescriptorSets may never
577 * touch it, so we need to make sure it's 100% valid now.
579 desc[i] = (struct anv_descriptor) {
580 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
581 .sampler = layout->binding[b].immutable_samplers[i],
585 desc += layout->binding[b].array_size;
588 /* Allocate surface state for the buffer views. */
589 for (uint32_t b = 0; b < layout->buffer_count; b++) {
590 struct surface_state_free_list_entry *entry =
591 pool->surface_state_free_list;
592 struct anv_state state;
595 state = entry->state;
596 pool->surface_state_free_list = entry->next;
597 assert(state.alloc_size == 64);
599 state = anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
602 set->buffer_views[b].surface_state = state;
611 anv_descriptor_set_destroy(struct anv_device *device,
612 struct anv_descriptor_pool *pool,
613 struct anv_descriptor_set *set)
615 anv_descriptor_set_layout_unref(device, set->layout);
617 /* Put the buffer view surface state back on the free list. */
618 for (uint32_t b = 0; b < set->buffer_count; b++) {
619 struct surface_state_free_list_entry *entry =
620 set->buffer_views[b].surface_state.map;
621 entry->next = pool->surface_state_free_list;
622 entry->state = set->buffer_views[b].surface_state;
623 pool->surface_state_free_list = entry;
626 /* Put the descriptor set allocation back on the free list. */
627 const uint32_t index = (char *) set - pool->data;
628 if (index + set->size == pool->next) {
631 struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
632 entry->next = pool->free_list;
633 entry->size = set->size;
634 pool->free_list = (char *) entry - pool->data;
638 VkResult anv_AllocateDescriptorSets(
640 const VkDescriptorSetAllocateInfo* pAllocateInfo,
641 VkDescriptorSet* pDescriptorSets)
643 ANV_FROM_HANDLE(anv_device, device, _device);
644 ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
646 VkResult result = VK_SUCCESS;
647 struct anv_descriptor_set *set;
650 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
651 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
652 pAllocateInfo->pSetLayouts[i]);
654 result = anv_descriptor_set_create(device, pool, layout, &set);
655 if (result != VK_SUCCESS)
658 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
661 if (result != VK_SUCCESS)
662 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
668 VkResult anv_FreeDescriptorSets(
670 VkDescriptorPool descriptorPool,
672 const VkDescriptorSet* pDescriptorSets)
674 ANV_FROM_HANDLE(anv_device, device, _device);
675 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
677 for (uint32_t i = 0; i < count; i++) {
678 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
683 anv_descriptor_set_destroy(device, pool, set);
690 anv_descriptor_set_write_image_view(struct anv_descriptor_set *set,
691 const struct gen_device_info * const devinfo,
692 const VkDescriptorImageInfo * const info,
693 VkDescriptorType type,
697 const struct anv_descriptor_set_binding_layout *bind_layout =
698 &set->layout->binding[binding];
699 struct anv_descriptor *desc =
700 &set->descriptors[bind_layout->descriptor_index + element];
701 struct anv_image_view *image_view = NULL;
702 struct anv_sampler *sampler = NULL;
704 assert(type == bind_layout->type);
707 case VK_DESCRIPTOR_TYPE_SAMPLER:
708 sampler = anv_sampler_from_handle(info->sampler);
711 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
712 image_view = anv_image_view_from_handle(info->imageView);
713 sampler = anv_sampler_from_handle(info->sampler);
716 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
717 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
718 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
719 image_view = anv_image_view_from_handle(info->imageView);
723 unreachable("invalid descriptor type");
726 /* If this descriptor has an immutable sampler, we don't want to stomp on
729 sampler = bind_layout->immutable_samplers ?
730 bind_layout->immutable_samplers[element] :
733 *desc = (struct anv_descriptor) {
735 .layout = info->imageLayout,
736 .image_view = image_view,
742 anv_descriptor_set_write_buffer_view(struct anv_descriptor_set *set,
743 VkDescriptorType type,
744 struct anv_buffer_view *buffer_view,
748 const struct anv_descriptor_set_binding_layout *bind_layout =
749 &set->layout->binding[binding];
750 struct anv_descriptor *desc =
751 &set->descriptors[bind_layout->descriptor_index + element];
753 assert(type == bind_layout->type);
755 *desc = (struct anv_descriptor) {
757 .buffer_view = buffer_view,
762 anv_descriptor_set_write_buffer(struct anv_descriptor_set *set,
763 struct anv_device *device,
764 struct anv_state_stream *alloc_stream,
765 VkDescriptorType type,
766 struct anv_buffer *buffer,
772 const struct anv_descriptor_set_binding_layout *bind_layout =
773 &set->layout->binding[binding];
774 struct anv_descriptor *desc =
775 &set->descriptors[bind_layout->descriptor_index + element];
777 assert(type == bind_layout->type);
779 if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
780 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
781 *desc = (struct anv_descriptor) {
788 struct anv_buffer_view *bview =
789 &set->buffer_views[bind_layout->buffer_index + element];
791 bview->format = anv_isl_format_for_descriptor_type(type);
792 bview->range = anv_buffer_get_range(buffer, offset, range);
793 bview->address = anv_address_add(buffer->address, offset);
795 /* If we're writing descriptors through a push command, we need to
796 * allocate the surface state from the command buffer. Otherwise it will
797 * be allocated by the descriptor pool when calling
798 * vkAllocateDescriptorSets. */
800 bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
802 anv_fill_buffer_surface_state(device, bview->surface_state,
804 bview->address, bview->range, 1);
806 *desc = (struct anv_descriptor) {
808 .buffer_view = bview,
813 void anv_UpdateDescriptorSets(
815 uint32_t descriptorWriteCount,
816 const VkWriteDescriptorSet* pDescriptorWrites,
817 uint32_t descriptorCopyCount,
818 const VkCopyDescriptorSet* pDescriptorCopies)
820 ANV_FROM_HANDLE(anv_device, device, _device);
822 for (uint32_t i = 0; i < descriptorWriteCount; i++) {
823 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
824 ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
826 switch (write->descriptorType) {
827 case VK_DESCRIPTOR_TYPE_SAMPLER:
828 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
829 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
830 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
831 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
832 for (uint32_t j = 0; j < write->descriptorCount; j++) {
833 anv_descriptor_set_write_image_view(set, &device->info,
834 write->pImageInfo + j,
835 write->descriptorType,
837 write->dstArrayElement + j);
841 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
842 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
843 for (uint32_t j = 0; j < write->descriptorCount; j++) {
844 ANV_FROM_HANDLE(anv_buffer_view, bview,
845 write->pTexelBufferView[j]);
847 anv_descriptor_set_write_buffer_view(set,
848 write->descriptorType,
851 write->dstArrayElement + j);
855 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
856 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
857 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
858 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
859 for (uint32_t j = 0; j < write->descriptorCount; j++) {
860 assert(write->pBufferInfo[j].buffer);
861 ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
864 anv_descriptor_set_write_buffer(set,
867 write->descriptorType,
870 write->dstArrayElement + j,
871 write->pBufferInfo[j].offset,
872 write->pBufferInfo[j].range);
881 for (uint32_t i = 0; i < descriptorCopyCount; i++) {
882 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
883 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
884 ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
886 const struct anv_descriptor_set_binding_layout *src_layout =
887 &src->layout->binding[copy->srcBinding];
888 struct anv_descriptor *src_desc =
889 &src->descriptors[src_layout->descriptor_index];
890 src_desc += copy->srcArrayElement;
892 const struct anv_descriptor_set_binding_layout *dst_layout =
893 &dst->layout->binding[copy->dstBinding];
894 struct anv_descriptor *dst_desc =
895 &dst->descriptors[dst_layout->descriptor_index];
896 dst_desc += copy->dstArrayElement;
898 for (uint32_t j = 0; j < copy->descriptorCount; j++)
899 dst_desc[j] = src_desc[j];
904 * Descriptor update templates.
908 anv_descriptor_set_write_template(struct anv_descriptor_set *set,
909 struct anv_device *device,
910 struct anv_state_stream *alloc_stream,
911 const struct anv_descriptor_update_template *template,
914 for (uint32_t i = 0; i < template->entry_count; i++) {
915 const struct anv_descriptor_template_entry *entry =
916 &template->entries[i];
918 switch (entry->type) {
919 case VK_DESCRIPTOR_TYPE_SAMPLER:
920 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
921 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
922 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
923 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
924 for (uint32_t j = 0; j < entry->array_count; j++) {
925 const VkDescriptorImageInfo *info =
926 data + entry->offset + j * entry->stride;
927 anv_descriptor_set_write_image_view(set, &device->info,
930 entry->array_element + j);
934 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
935 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
936 for (uint32_t j = 0; j < entry->array_count; j++) {
937 const VkBufferView *_bview =
938 data + entry->offset + j * entry->stride;
939 ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
941 anv_descriptor_set_write_buffer_view(set,
945 entry->array_element + j);
949 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
950 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
951 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
952 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
953 for (uint32_t j = 0; j < entry->array_count; j++) {
954 const VkDescriptorBufferInfo *info =
955 data + entry->offset + j * entry->stride;
956 ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
958 anv_descriptor_set_write_buffer(set,
964 entry->array_element + j,
965 info->offset, info->range);
975 VkResult anv_CreateDescriptorUpdateTemplate(
977 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
978 const VkAllocationCallbacks* pAllocator,
979 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate)
981 ANV_FROM_HANDLE(anv_device, device, _device);
982 struct anv_descriptor_update_template *template;
984 size_t size = sizeof(*template) +
985 pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
986 template = vk_alloc2(&device->alloc, pAllocator, size, 8,
987 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
988 if (template == NULL)
989 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
991 template->bind_point = pCreateInfo->pipelineBindPoint;
993 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
994 template->set = pCreateInfo->set;
996 template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
997 for (uint32_t i = 0; i < template->entry_count; i++) {
998 const VkDescriptorUpdateTemplateEntryKHR *pEntry =
999 &pCreateInfo->pDescriptorUpdateEntries[i];
1001 template->entries[i] = (struct anv_descriptor_template_entry) {
1002 .type = pEntry->descriptorType,
1003 .binding = pEntry->dstBinding,
1004 .array_element = pEntry->dstArrayElement,
1005 .array_count = pEntry->descriptorCount,
1006 .offset = pEntry->offset,
1007 .stride = pEntry->stride,
1011 *pDescriptorUpdateTemplate =
1012 anv_descriptor_update_template_to_handle(template);
1017 void anv_DestroyDescriptorUpdateTemplate(
1019 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1020 const VkAllocationCallbacks* pAllocator)
1022 ANV_FROM_HANDLE(anv_device, device, _device);
1023 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1024 descriptorUpdateTemplate);
1026 vk_free2(&device->alloc, pAllocator, template);
1029 void anv_UpdateDescriptorSetWithTemplate(
1031 VkDescriptorSet descriptorSet,
1032 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1035 ANV_FROM_HANDLE(anv_device, device, _device);
1036 ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1037 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1038 descriptorUpdateTemplate);
1040 anv_descriptor_set_write_template(set, device, NULL, template, pData);