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 VkResult
anv_CreateDescriptorSetLayout(
40 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
41 const VkAllocationCallbacks
* pAllocator
,
42 VkDescriptorSetLayout
* pSetLayout
)
44 ANV_FROM_HANDLE(anv_device
, device
, _device
);
46 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
48 uint32_t max_binding
= 0;
49 uint32_t immutable_sampler_count
= 0;
50 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
51 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
52 if (pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
53 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
56 struct anv_descriptor_set_layout
*set_layout
;
57 struct anv_descriptor_set_binding_layout
*bindings
;
58 struct anv_sampler
**samplers
;
61 anv_multialloc_add(&ma
, &set_layout
, 1);
62 anv_multialloc_add(&ma
, &bindings
, max_binding
+ 1);
63 anv_multialloc_add(&ma
, &samplers
, immutable_sampler_count
);
65 if (!anv_multialloc_alloc2(&ma
, &device
->alloc
, pAllocator
,
66 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
))
67 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
69 memset(set_layout
, 0, sizeof(*set_layout
));
70 set_layout
->binding_count
= max_binding
+ 1;
72 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
73 /* Initialize all binding_layout entries to -1 */
74 memset(&set_layout
->binding
[b
], -1, sizeof(set_layout
->binding
[b
]));
76 set_layout
->binding
[b
].array_size
= 0;
77 set_layout
->binding
[b
].immutable_samplers
= NULL
;
80 /* Initialize all samplers to 0 */
81 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
83 uint32_t sampler_count
[MESA_SHADER_STAGES
] = { 0, };
84 uint32_t surface_count
[MESA_SHADER_STAGES
] = { 0, };
85 uint32_t image_count
[MESA_SHADER_STAGES
] = { 0, };
86 uint32_t buffer_count
= 0;
87 uint32_t dynamic_offset_count
= 0;
89 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
90 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
91 uint32_t b
= binding
->binding
;
92 /* We temporarily store the pointer to the binding in the
93 * immutable_samplers pointer. This provides us with a quick-and-dirty
94 * way to sort the bindings by binding number.
96 set_layout
->binding
[b
].immutable_samplers
= (void *)binding
;
99 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
100 const VkDescriptorSetLayoutBinding
*binding
=
101 (void *)set_layout
->binding
[b
].immutable_samplers
;
106 if (binding
->descriptorCount
== 0)
110 set_layout
->binding
[b
].type
= binding
->descriptorType
;
112 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
113 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
114 set_layout
->size
+= binding
->descriptorCount
;
116 switch (binding
->descriptorType
) {
117 case VK_DESCRIPTOR_TYPE_SAMPLER
:
118 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
119 anv_foreach_stage(s
, binding
->stageFlags
) {
120 set_layout
->binding
[b
].stage
[s
].sampler_index
= sampler_count
[s
];
121 sampler_count
[s
] += binding
->descriptorCount
;
128 switch (binding
->descriptorType
) {
129 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
130 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
131 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
132 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
133 set_layout
->binding
[b
].buffer_index
= buffer_count
;
134 buffer_count
+= binding
->descriptorCount
;
137 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
138 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
139 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
140 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
141 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
142 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
143 anv_foreach_stage(s
, binding
->stageFlags
) {
144 set_layout
->binding
[b
].stage
[s
].surface_index
= surface_count
[s
];
145 surface_count
[s
] += binding
->descriptorCount
;
152 switch (binding
->descriptorType
) {
153 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
154 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
155 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
156 dynamic_offset_count
+= binding
->descriptorCount
;
162 switch (binding
->descriptorType
) {
163 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
164 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
165 anv_foreach_stage(s
, binding
->stageFlags
) {
166 set_layout
->binding
[b
].stage
[s
].image_index
= image_count
[s
];
167 image_count
[s
] += binding
->descriptorCount
;
174 if (binding
->pImmutableSamplers
) {
175 set_layout
->binding
[b
].immutable_samplers
= samplers
;
176 samplers
+= binding
->descriptorCount
;
178 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
179 set_layout
->binding
[b
].immutable_samplers
[i
] =
180 anv_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
182 set_layout
->binding
[b
].immutable_samplers
= NULL
;
185 set_layout
->shader_stages
|= binding
->stageFlags
;
188 set_layout
->buffer_count
= buffer_count
;
189 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
191 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
196 void anv_DestroyDescriptorSetLayout(
198 VkDescriptorSetLayout _set_layout
,
199 const VkAllocationCallbacks
* pAllocator
)
201 ANV_FROM_HANDLE(anv_device
, device
, _device
);
202 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
207 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
211 sha1_update_descriptor_set_layout(struct mesa_sha1
*ctx
,
212 const struct anv_descriptor_set_layout
*layout
)
214 size_t size
= sizeof(*layout
) +
215 sizeof(layout
->binding
[0]) * layout
->binding_count
;
216 _mesa_sha1_update(ctx
, layout
, size
);
220 * Pipeline layouts. These have nothing to do with the pipeline. They are
221 * just multiple descriptor set layouts pasted together
224 VkResult
anv_CreatePipelineLayout(
226 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
227 const VkAllocationCallbacks
* pAllocator
,
228 VkPipelineLayout
* pPipelineLayout
)
230 ANV_FROM_HANDLE(anv_device
, device
, _device
);
231 struct anv_pipeline_layout
*layout
;
233 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
235 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
236 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
238 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
240 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
242 unsigned dynamic_offset_count
= 0;
244 memset(layout
->stage
, 0, sizeof(layout
->stage
));
245 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
246 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
247 pCreateInfo
->pSetLayouts
[set
]);
248 layout
->set
[set
].layout
= set_layout
;
250 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
251 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
252 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
255 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
256 for (gl_shader_stage s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
257 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0)
258 layout
->stage
[s
].has_dynamic_offsets
= true;
263 struct mesa_sha1 ctx
;
264 _mesa_sha1_init(&ctx
);
265 for (unsigned s
= 0; s
< layout
->num_sets
; s
++) {
266 sha1_update_descriptor_set_layout(&ctx
, layout
->set
[s
].layout
);
267 _mesa_sha1_update(&ctx
, &layout
->set
[s
].dynamic_offset_start
,
268 sizeof(layout
->set
[s
].dynamic_offset_start
));
270 _mesa_sha1_update(&ctx
, &layout
->num_sets
, sizeof(layout
->num_sets
));
271 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
272 _mesa_sha1_update(&ctx
, &layout
->stage
[s
].has_dynamic_offsets
,
273 sizeof(layout
->stage
[s
].has_dynamic_offsets
));
275 _mesa_sha1_final(&ctx
, layout
->sha1
);
277 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
282 void anv_DestroyPipelineLayout(
284 VkPipelineLayout _pipelineLayout
,
285 const VkAllocationCallbacks
* pAllocator
)
287 ANV_FROM_HANDLE(anv_device
, device
, _device
);
288 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
290 if (!pipeline_layout
)
293 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
299 * These are implemented using a big pool of memory and a free-list for the
300 * host memory allocations and a state_stream and a free list for the buffer
301 * view surface state. The spec allows us to fail to allocate due to
302 * fragmentation in all cases but two: 1) after pool reset, allocating up
303 * until the pool size with no freeing must succeed and 2) allocating and
304 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
305 * and the free lists lets us recycle blocks for case 2).
310 VkResult
anv_CreateDescriptorPool(
312 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
313 const VkAllocationCallbacks
* pAllocator
,
314 VkDescriptorPool
* pDescriptorPool
)
316 ANV_FROM_HANDLE(anv_device
, device
, _device
);
317 struct anv_descriptor_pool
*pool
;
319 uint32_t descriptor_count
= 0;
320 uint32_t buffer_count
= 0;
321 for (uint32_t i
= 0; i
< pCreateInfo
->poolSizeCount
; i
++) {
322 switch (pCreateInfo
->pPoolSizes
[i
].type
) {
323 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
324 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
325 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
326 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
327 buffer_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
329 descriptor_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
334 const size_t pool_size
=
335 pCreateInfo
->maxSets
* sizeof(struct anv_descriptor_set
) +
336 descriptor_count
* sizeof(struct anv_descriptor
) +
337 buffer_count
* sizeof(struct anv_buffer_view
);
338 const size_t total_size
= sizeof(*pool
) + pool_size
;
340 pool
= vk_alloc2(&device
->alloc
, pAllocator
, total_size
, 8,
341 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
343 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
345 pool
->size
= pool_size
;
347 pool
->free_list
= EMPTY
;
349 anv_state_stream_init(&pool
->surface_state_stream
,
350 &device
->surface_state_pool
, 4096);
351 pool
->surface_state_free_list
= NULL
;
353 *pDescriptorPool
= anv_descriptor_pool_to_handle(pool
);
358 void anv_DestroyDescriptorPool(
360 VkDescriptorPool _pool
,
361 const VkAllocationCallbacks
* pAllocator
)
363 ANV_FROM_HANDLE(anv_device
, device
, _device
);
364 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, _pool
);
369 anv_state_stream_finish(&pool
->surface_state_stream
);
370 vk_free2(&device
->alloc
, pAllocator
, pool
);
373 VkResult
anv_ResetDescriptorPool(
375 VkDescriptorPool descriptorPool
,
376 VkDescriptorPoolResetFlags flags
)
378 ANV_FROM_HANDLE(anv_device
, device
, _device
);
379 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
382 pool
->free_list
= EMPTY
;
383 anv_state_stream_finish(&pool
->surface_state_stream
);
384 anv_state_stream_init(&pool
->surface_state_stream
,
385 &device
->surface_state_pool
, 4096);
386 pool
->surface_state_free_list
= NULL
;
391 struct pool_free_list_entry
{
397 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout
*layout
)
400 sizeof(struct anv_descriptor_set
) +
401 layout
->size
* sizeof(struct anv_descriptor
) +
402 layout
->buffer_count
* sizeof(struct anv_buffer_view
);
406 anv_descriptor_set_binding_layout_get_hw_size(const struct anv_descriptor_set_binding_layout
*binding
)
408 if (!binding
->immutable_samplers
)
409 return binding
->array_size
;
411 uint32_t total_plane_count
= 0;
412 for (uint32_t i
= 0; i
< binding
->array_size
; i
++)
413 total_plane_count
+= binding
->immutable_samplers
[i
]->n_planes
;
415 return total_plane_count
;
418 struct surface_state_free_list_entry
{
420 struct anv_state state
;
424 anv_descriptor_set_create(struct anv_device
*device
,
425 struct anv_descriptor_pool
*pool
,
426 const struct anv_descriptor_set_layout
*layout
,
427 struct anv_descriptor_set
**out_set
)
429 struct anv_descriptor_set
*set
;
430 const size_t size
= anv_descriptor_set_layout_size(layout
);
433 if (size
<= pool
->size
- pool
->next
) {
434 set
= (struct anv_descriptor_set
*) (pool
->data
+ pool
->next
);
437 struct pool_free_list_entry
*entry
;
438 uint32_t *link
= &pool
->free_list
;
439 for (uint32_t f
= pool
->free_list
; f
!= EMPTY
; f
= entry
->next
) {
440 entry
= (struct pool_free_list_entry
*) (pool
->data
+ f
);
441 if (size
<= entry
->size
) {
443 set
= (struct anv_descriptor_set
*) entry
;
451 if (pool
->free_list
!= EMPTY
) {
452 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
454 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
459 set
->layout
= layout
;
461 (struct anv_buffer_view
*) &set
->descriptors
[layout
->size
];
462 set
->buffer_count
= layout
->buffer_count
;
464 /* By defining the descriptors to be zero now, we can later verify that
465 * a descriptor has not been populated with user data.
467 memset(set
->descriptors
, 0, sizeof(struct anv_descriptor
) * layout
->size
);
469 /* Go through and fill out immutable samplers if we have any */
470 struct anv_descriptor
*desc
= set
->descriptors
;
471 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
472 if (layout
->binding
[b
].immutable_samplers
) {
473 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++) {
474 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
475 * UpdateDescriptorSets if needed. However, if the descriptor
476 * set has an immutable sampler, UpdateDescriptorSets may never
477 * touch it, so we need to make sure it's 100% valid now.
479 desc
[i
] = (struct anv_descriptor
) {
480 .type
= VK_DESCRIPTOR_TYPE_SAMPLER
,
481 .sampler
= layout
->binding
[b
].immutable_samplers
[i
],
485 desc
+= layout
->binding
[b
].array_size
;
488 /* Allocate surface state for the buffer views. */
489 for (uint32_t b
= 0; b
< layout
->buffer_count
; b
++) {
490 struct surface_state_free_list_entry
*entry
=
491 pool
->surface_state_free_list
;
492 struct anv_state state
;
495 state
= entry
->state
;
496 pool
->surface_state_free_list
= entry
->next
;
497 assert(state
.alloc_size
== 64);
499 state
= anv_state_stream_alloc(&pool
->surface_state_stream
, 64, 64);
502 set
->buffer_views
[b
].surface_state
= state
;
511 anv_descriptor_set_destroy(struct anv_device
*device
,
512 struct anv_descriptor_pool
*pool
,
513 struct anv_descriptor_set
*set
)
515 /* Put the buffer view surface state back on the free list. */
516 for (uint32_t b
= 0; b
< set
->buffer_count
; b
++) {
517 struct surface_state_free_list_entry
*entry
=
518 set
->buffer_views
[b
].surface_state
.map
;
519 entry
->next
= pool
->surface_state_free_list
;
520 entry
->state
= set
->buffer_views
[b
].surface_state
;
521 pool
->surface_state_free_list
= entry
;
524 /* Put the descriptor set allocation back on the free list. */
525 const uint32_t index
= (char *) set
- pool
->data
;
526 if (index
+ set
->size
== pool
->next
) {
529 struct pool_free_list_entry
*entry
= (struct pool_free_list_entry
*) set
;
530 entry
->next
= pool
->free_list
;
531 entry
->size
= set
->size
;
532 pool
->free_list
= (char *) entry
- pool
->data
;
536 VkResult
anv_AllocateDescriptorSets(
538 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
539 VkDescriptorSet
* pDescriptorSets
)
541 ANV_FROM_HANDLE(anv_device
, device
, _device
);
542 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
544 VkResult result
= VK_SUCCESS
;
545 struct anv_descriptor_set
*set
;
548 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
549 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
550 pAllocateInfo
->pSetLayouts
[i
]);
552 result
= anv_descriptor_set_create(device
, pool
, layout
, &set
);
553 if (result
!= VK_SUCCESS
)
556 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
559 if (result
!= VK_SUCCESS
)
560 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
566 VkResult
anv_FreeDescriptorSets(
568 VkDescriptorPool descriptorPool
,
570 const VkDescriptorSet
* pDescriptorSets
)
572 ANV_FROM_HANDLE(anv_device
, device
, _device
);
573 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
575 for (uint32_t i
= 0; i
< count
; i
++) {
576 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
581 anv_descriptor_set_destroy(device
, pool
, set
);
588 anv_descriptor_set_write_image_view(struct anv_descriptor_set
*set
,
589 const struct gen_device_info
* const devinfo
,
590 const VkDescriptorImageInfo
* const info
,
591 VkDescriptorType type
,
595 const struct anv_descriptor_set_binding_layout
*bind_layout
=
596 &set
->layout
->binding
[binding
];
597 struct anv_descriptor
*desc
=
598 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
599 struct anv_image_view
*image_view
= NULL
;
600 struct anv_sampler
*sampler
= NULL
;
602 assert(type
== bind_layout
->type
);
605 case VK_DESCRIPTOR_TYPE_SAMPLER
:
606 sampler
= anv_sampler_from_handle(info
->sampler
);
609 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
610 image_view
= anv_image_view_from_handle(info
->imageView
);
611 sampler
= anv_sampler_from_handle(info
->sampler
);
614 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
615 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
616 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
617 image_view
= anv_image_view_from_handle(info
->imageView
);
621 unreachable("invalid descriptor type");
624 /* If this descriptor has an immutable sampler, we don't want to stomp on
627 sampler
= bind_layout
->immutable_samplers
?
628 bind_layout
->immutable_samplers
[element
] :
631 *desc
= (struct anv_descriptor
) {
633 .layout
= info
->imageLayout
,
634 .image_view
= image_view
,
640 anv_descriptor_set_write_buffer_view(struct anv_descriptor_set
*set
,
641 VkDescriptorType type
,
642 struct anv_buffer_view
*buffer_view
,
646 const struct anv_descriptor_set_binding_layout
*bind_layout
=
647 &set
->layout
->binding
[binding
];
648 struct anv_descriptor
*desc
=
649 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
651 assert(type
== bind_layout
->type
);
653 *desc
= (struct anv_descriptor
) {
655 .buffer_view
= buffer_view
,
660 anv_descriptor_set_write_buffer(struct anv_descriptor_set
*set
,
661 struct anv_device
*device
,
662 struct anv_state_stream
*alloc_stream
,
663 VkDescriptorType type
,
664 struct anv_buffer
*buffer
,
670 const struct anv_descriptor_set_binding_layout
*bind_layout
=
671 &set
->layout
->binding
[binding
];
672 struct anv_descriptor
*desc
=
673 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
675 assert(type
== bind_layout
->type
);
677 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
678 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
679 *desc
= (struct anv_descriptor
) {
686 struct anv_buffer_view
*bview
=
687 &set
->buffer_views
[bind_layout
->buffer_index
+ element
];
689 bview
->format
= anv_isl_format_for_descriptor_type(type
);
690 bview
->bo
= buffer
->bo
;
691 bview
->offset
= buffer
->offset
+ offset
;
692 bview
->range
= anv_buffer_get_range(buffer
, offset
, range
);
694 /* If we're writing descriptors through a push command, we need to
695 * allocate the surface state from the command buffer. Otherwise it will
696 * be allocated by the descriptor pool when calling
697 * vkAllocateDescriptorSets. */
699 bview
->surface_state
= anv_state_stream_alloc(alloc_stream
, 64, 64);
701 anv_fill_buffer_surface_state(device
, bview
->surface_state
,
703 bview
->offset
, bview
->range
, 1);
705 *desc
= (struct anv_descriptor
) {
707 .buffer_view
= bview
,
712 void anv_UpdateDescriptorSets(
714 uint32_t descriptorWriteCount
,
715 const VkWriteDescriptorSet
* pDescriptorWrites
,
716 uint32_t descriptorCopyCount
,
717 const VkCopyDescriptorSet
* pDescriptorCopies
)
719 ANV_FROM_HANDLE(anv_device
, device
, _device
);
721 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
722 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
723 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
725 switch (write
->descriptorType
) {
726 case VK_DESCRIPTOR_TYPE_SAMPLER
:
727 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
728 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
729 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
730 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
731 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
732 anv_descriptor_set_write_image_view(set
, &device
->info
,
733 write
->pImageInfo
+ j
,
734 write
->descriptorType
,
736 write
->dstArrayElement
+ j
);
740 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
741 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
742 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
743 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
744 write
->pTexelBufferView
[j
]);
746 anv_descriptor_set_write_buffer_view(set
,
747 write
->descriptorType
,
750 write
->dstArrayElement
+ j
);
754 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
755 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
756 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
757 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
758 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
759 assert(write
->pBufferInfo
[j
].buffer
);
760 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
763 anv_descriptor_set_write_buffer(set
,
766 write
->descriptorType
,
769 write
->dstArrayElement
+ j
,
770 write
->pBufferInfo
[j
].offset
,
771 write
->pBufferInfo
[j
].range
);
780 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
781 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
782 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->srcSet
);
783 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
785 const struct anv_descriptor_set_binding_layout
*src_layout
=
786 &src
->layout
->binding
[copy
->srcBinding
];
787 struct anv_descriptor
*src_desc
=
788 &src
->descriptors
[src_layout
->descriptor_index
];
789 src_desc
+= copy
->srcArrayElement
;
791 const struct anv_descriptor_set_binding_layout
*dst_layout
=
792 &dst
->layout
->binding
[copy
->dstBinding
];
793 struct anv_descriptor
*dst_desc
=
794 &dst
->descriptors
[dst_layout
->descriptor_index
];
795 dst_desc
+= copy
->dstArrayElement
;
797 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
798 dst_desc
[j
] = src_desc
[j
];
803 * Descriptor update templates.
807 anv_descriptor_set_write_template(struct anv_descriptor_set
*set
,
808 struct anv_device
*device
,
809 struct anv_state_stream
*alloc_stream
,
810 const struct anv_descriptor_update_template
*template,
813 const struct anv_descriptor_set_layout
*layout
= set
->layout
;
815 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
816 const struct anv_descriptor_template_entry
*entry
=
817 &template->entries
[i
];
818 const struct anv_descriptor_set_binding_layout
*bind_layout
=
819 &layout
->binding
[entry
->binding
];
820 struct anv_descriptor
*desc
= &set
->descriptors
[bind_layout
->descriptor_index
];
821 desc
+= entry
->array_element
;
823 switch (entry
->type
) {
824 case VK_DESCRIPTOR_TYPE_SAMPLER
:
825 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
826 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
827 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
828 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
829 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
830 const VkDescriptorImageInfo
*info
=
831 data
+ entry
->offset
+ j
* entry
->stride
;
832 anv_descriptor_set_write_image_view(set
, &device
->info
,
835 entry
->array_element
+ j
);
839 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
840 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
841 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
842 const VkBufferView
*_bview
=
843 data
+ entry
->offset
+ j
* entry
->stride
;
844 ANV_FROM_HANDLE(anv_buffer_view
, bview
, *_bview
);
846 anv_descriptor_set_write_buffer_view(set
,
850 entry
->array_element
+ j
);
854 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
855 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
856 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
857 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
858 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
859 const VkDescriptorBufferInfo
*info
=
860 data
+ entry
->offset
+ j
* entry
->stride
;
861 ANV_FROM_HANDLE(anv_buffer
, buffer
, info
->buffer
);
863 anv_descriptor_set_write_buffer(set
,
869 entry
->array_element
+ j
,
870 info
->offset
, info
->range
);
880 VkResult
anv_CreateDescriptorUpdateTemplateKHR(
882 const VkDescriptorUpdateTemplateCreateInfoKHR
* pCreateInfo
,
883 const VkAllocationCallbacks
* pAllocator
,
884 VkDescriptorUpdateTemplateKHR
* pDescriptorUpdateTemplate
)
886 ANV_FROM_HANDLE(anv_device
, device
, _device
);
887 struct anv_descriptor_update_template
*template;
889 size_t size
= sizeof(*template) +
890 pCreateInfo
->descriptorUpdateEntryCount
* sizeof(template->entries
[0]);
891 template = vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
892 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
893 if (template == NULL
)
894 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
896 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR
)
897 template->set
= pCreateInfo
->set
;
899 template->entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
900 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
901 const VkDescriptorUpdateTemplateEntryKHR
*pEntry
=
902 &pCreateInfo
->pDescriptorUpdateEntries
[i
];
904 template->entries
[i
] = (struct anv_descriptor_template_entry
) {
905 .type
= pEntry
->descriptorType
,
906 .binding
= pEntry
->dstBinding
,
907 .array_element
= pEntry
->dstArrayElement
,
908 .array_count
= pEntry
->descriptorCount
,
909 .offset
= pEntry
->offset
,
910 .stride
= pEntry
->stride
,
914 *pDescriptorUpdateTemplate
=
915 anv_descriptor_update_template_to_handle(template);
920 void anv_DestroyDescriptorUpdateTemplateKHR(
922 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
923 const VkAllocationCallbacks
* pAllocator
)
925 ANV_FROM_HANDLE(anv_device
, device
, _device
);
926 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
927 descriptorUpdateTemplate
);
929 vk_free2(&device
->alloc
, pAllocator
, template);
932 void anv_UpdateDescriptorSetWithTemplateKHR(
934 VkDescriptorSet descriptorSet
,
935 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
938 ANV_FROM_HANDLE(anv_device
, device
, _device
);
939 ANV_FROM_HANDLE(anv_descriptor_set
, set
, descriptorSet
);
940 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
941 descriptorUpdateTemplate
);
943 anv_descriptor_set_write_template(set
, device
, NULL
, template, pData
);