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 assert(binding
->descriptorCount
> 0);
108 set_layout
->binding
[b
].type
= binding
->descriptorType
;
110 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
111 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
112 set_layout
->size
+= binding
->descriptorCount
;
114 switch (binding
->descriptorType
) {
115 case VK_DESCRIPTOR_TYPE_SAMPLER
:
116 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
117 anv_foreach_stage(s
, binding
->stageFlags
) {
118 set_layout
->binding
[b
].stage
[s
].sampler_index
= sampler_count
[s
];
119 sampler_count
[s
] += binding
->descriptorCount
;
126 switch (binding
->descriptorType
) {
127 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
128 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
129 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
130 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
131 set_layout
->binding
[b
].buffer_index
= buffer_count
;
132 buffer_count
+= binding
->descriptorCount
;
135 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
136 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
137 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
138 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
139 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
140 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
141 anv_foreach_stage(s
, binding
->stageFlags
) {
142 set_layout
->binding
[b
].stage
[s
].surface_index
= surface_count
[s
];
143 surface_count
[s
] += binding
->descriptorCount
;
150 switch (binding
->descriptorType
) {
151 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
152 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
153 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
154 dynamic_offset_count
+= binding
->descriptorCount
;
160 switch (binding
->descriptorType
) {
161 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
162 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
163 anv_foreach_stage(s
, binding
->stageFlags
) {
164 set_layout
->binding
[b
].stage
[s
].image_index
= image_count
[s
];
165 image_count
[s
] += binding
->descriptorCount
;
172 if (binding
->pImmutableSamplers
) {
173 set_layout
->binding
[b
].immutable_samplers
= samplers
;
174 samplers
+= binding
->descriptorCount
;
176 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
177 set_layout
->binding
[b
].immutable_samplers
[i
] =
178 anv_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
180 set_layout
->binding
[b
].immutable_samplers
= NULL
;
183 set_layout
->shader_stages
|= binding
->stageFlags
;
186 set_layout
->buffer_count
= buffer_count
;
187 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
189 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
194 void anv_DestroyDescriptorSetLayout(
196 VkDescriptorSetLayout _set_layout
,
197 const VkAllocationCallbacks
* pAllocator
)
199 ANV_FROM_HANDLE(anv_device
, device
, _device
);
200 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
205 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
209 sha1_update_descriptor_set_layout(struct mesa_sha1
*ctx
,
210 const struct anv_descriptor_set_layout
*layout
)
212 size_t size
= sizeof(*layout
) +
213 sizeof(layout
->binding
[0]) * layout
->binding_count
;
214 _mesa_sha1_update(ctx
, layout
, size
);
218 * Pipeline layouts. These have nothing to do with the pipeline. They are
219 * just multiple descriptor set layouts pasted together
222 VkResult
anv_CreatePipelineLayout(
224 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
225 const VkAllocationCallbacks
* pAllocator
,
226 VkPipelineLayout
* pPipelineLayout
)
228 ANV_FROM_HANDLE(anv_device
, device
, _device
);
229 struct anv_pipeline_layout
*layout
;
231 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
233 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
234 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
236 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
238 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
240 unsigned dynamic_offset_count
= 0;
242 memset(layout
->stage
, 0, sizeof(layout
->stage
));
243 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
244 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
245 pCreateInfo
->pSetLayouts
[set
]);
246 layout
->set
[set
].layout
= set_layout
;
248 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
249 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
250 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
253 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
254 for (gl_shader_stage s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
255 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0)
256 layout
->stage
[s
].has_dynamic_offsets
= true;
261 struct mesa_sha1 ctx
;
262 _mesa_sha1_init(&ctx
);
263 for (unsigned s
= 0; s
< layout
->num_sets
; s
++) {
264 sha1_update_descriptor_set_layout(&ctx
, layout
->set
[s
].layout
);
265 _mesa_sha1_update(&ctx
, &layout
->set
[s
].dynamic_offset_start
,
266 sizeof(layout
->set
[s
].dynamic_offset_start
));
268 _mesa_sha1_update(&ctx
, &layout
->num_sets
, sizeof(layout
->num_sets
));
269 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
270 _mesa_sha1_update(&ctx
, &layout
->stage
[s
].has_dynamic_offsets
,
271 sizeof(layout
->stage
[s
].has_dynamic_offsets
));
273 _mesa_sha1_final(&ctx
, layout
->sha1
);
275 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
280 void anv_DestroyPipelineLayout(
282 VkPipelineLayout _pipelineLayout
,
283 const VkAllocationCallbacks
* pAllocator
)
285 ANV_FROM_HANDLE(anv_device
, device
, _device
);
286 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
288 if (!pipeline_layout
)
291 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
297 * These are implemented using a big pool of memory and a free-list for the
298 * host memory allocations and a state_stream and a free list for the buffer
299 * view surface state. The spec allows us to fail to allocate due to
300 * fragmentation in all cases but two: 1) after pool reset, allocating up
301 * until the pool size with no freeing must succeed and 2) allocating and
302 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
303 * and the free lists lets us recycle blocks for case 2).
308 VkResult
anv_CreateDescriptorPool(
310 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
311 const VkAllocationCallbacks
* pAllocator
,
312 VkDescriptorPool
* pDescriptorPool
)
314 ANV_FROM_HANDLE(anv_device
, device
, _device
);
315 struct anv_descriptor_pool
*pool
;
317 uint32_t descriptor_count
= 0;
318 uint32_t buffer_count
= 0;
319 for (uint32_t i
= 0; i
< pCreateInfo
->poolSizeCount
; i
++) {
320 switch (pCreateInfo
->pPoolSizes
[i
].type
) {
321 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
322 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
323 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
324 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
325 buffer_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
327 descriptor_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
332 const size_t pool_size
=
333 pCreateInfo
->maxSets
* sizeof(struct anv_descriptor_set
) +
334 descriptor_count
* sizeof(struct anv_descriptor
) +
335 buffer_count
* sizeof(struct anv_buffer_view
);
336 const size_t total_size
= sizeof(*pool
) + pool_size
;
338 pool
= vk_alloc2(&device
->alloc
, pAllocator
, total_size
, 8,
339 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
341 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
343 pool
->size
= pool_size
;
345 pool
->free_list
= EMPTY
;
347 anv_state_stream_init(&pool
->surface_state_stream
,
348 &device
->surface_state_pool
, 4096);
349 pool
->surface_state_free_list
= NULL
;
351 *pDescriptorPool
= anv_descriptor_pool_to_handle(pool
);
356 void anv_DestroyDescriptorPool(
358 VkDescriptorPool _pool
,
359 const VkAllocationCallbacks
* pAllocator
)
361 ANV_FROM_HANDLE(anv_device
, device
, _device
);
362 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, _pool
);
367 anv_state_stream_finish(&pool
->surface_state_stream
);
368 vk_free2(&device
->alloc
, pAllocator
, pool
);
371 VkResult
anv_ResetDescriptorPool(
373 VkDescriptorPool descriptorPool
,
374 VkDescriptorPoolResetFlags flags
)
376 ANV_FROM_HANDLE(anv_device
, device
, _device
);
377 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
380 pool
->free_list
= EMPTY
;
381 anv_state_stream_finish(&pool
->surface_state_stream
);
382 anv_state_stream_init(&pool
->surface_state_stream
,
383 &device
->surface_state_pool
, 4096);
384 pool
->surface_state_free_list
= NULL
;
389 struct pool_free_list_entry
{
395 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout
*layout
)
398 sizeof(struct anv_descriptor_set
) +
399 layout
->size
* sizeof(struct anv_descriptor
) +
400 layout
->buffer_count
* sizeof(struct anv_buffer_view
);
404 anv_descriptor_set_binding_layout_get_hw_size(const struct anv_descriptor_set_binding_layout
*binding
)
406 if (!binding
->immutable_samplers
)
407 return binding
->array_size
;
409 uint32_t total_plane_count
= 0;
410 for (uint32_t i
= 0; i
< binding
->array_size
; i
++)
411 total_plane_count
+= binding
->immutable_samplers
[i
]->n_planes
;
413 return total_plane_count
;
416 struct surface_state_free_list_entry
{
418 struct anv_state state
;
422 anv_descriptor_set_create(struct anv_device
*device
,
423 struct anv_descriptor_pool
*pool
,
424 const struct anv_descriptor_set_layout
*layout
,
425 struct anv_descriptor_set
**out_set
)
427 struct anv_descriptor_set
*set
;
428 const size_t size
= anv_descriptor_set_layout_size(layout
);
431 if (size
<= pool
->size
- pool
->next
) {
432 set
= (struct anv_descriptor_set
*) (pool
->data
+ pool
->next
);
435 struct pool_free_list_entry
*entry
;
436 uint32_t *link
= &pool
->free_list
;
437 for (uint32_t f
= pool
->free_list
; f
!= EMPTY
; f
= entry
->next
) {
438 entry
= (struct pool_free_list_entry
*) (pool
->data
+ f
);
439 if (size
<= entry
->size
) {
441 set
= (struct anv_descriptor_set
*) entry
;
449 if (pool
->free_list
!= EMPTY
) {
450 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
452 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
457 set
->layout
= layout
;
459 (struct anv_buffer_view
*) &set
->descriptors
[layout
->size
];
460 set
->buffer_count
= layout
->buffer_count
;
462 /* By defining the descriptors to be zero now, we can later verify that
463 * a descriptor has not been populated with user data.
465 memset(set
->descriptors
, 0, sizeof(struct anv_descriptor
) * layout
->size
);
467 /* Go through and fill out immutable samplers if we have any */
468 struct anv_descriptor
*desc
= set
->descriptors
;
469 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
470 if (layout
->binding
[b
].immutable_samplers
) {
471 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++) {
472 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
473 * UpdateDescriptorSets if needed. However, if the descriptor
474 * set has an immutable sampler, UpdateDescriptorSets may never
475 * touch it, so we need to make sure it's 100% valid now.
477 desc
[i
] = (struct anv_descriptor
) {
478 .type
= VK_DESCRIPTOR_TYPE_SAMPLER
,
479 .sampler
= layout
->binding
[b
].immutable_samplers
[i
],
483 desc
+= layout
->binding
[b
].array_size
;
486 /* Allocate surface state for the buffer views. */
487 for (uint32_t b
= 0; b
< layout
->buffer_count
; b
++) {
488 struct surface_state_free_list_entry
*entry
=
489 pool
->surface_state_free_list
;
490 struct anv_state state
;
493 state
= entry
->state
;
494 pool
->surface_state_free_list
= entry
->next
;
495 assert(state
.alloc_size
== 64);
497 state
= anv_state_stream_alloc(&pool
->surface_state_stream
, 64, 64);
500 set
->buffer_views
[b
].surface_state
= state
;
509 anv_descriptor_set_destroy(struct anv_device
*device
,
510 struct anv_descriptor_pool
*pool
,
511 struct anv_descriptor_set
*set
)
513 /* Put the buffer view surface state back on the free list. */
514 for (uint32_t b
= 0; b
< set
->buffer_count
; b
++) {
515 struct surface_state_free_list_entry
*entry
=
516 set
->buffer_views
[b
].surface_state
.map
;
517 entry
->next
= pool
->surface_state_free_list
;
518 entry
->state
= set
->buffer_views
[b
].surface_state
;
519 pool
->surface_state_free_list
= entry
;
522 /* Put the descriptor set allocation back on the free list. */
523 const uint32_t index
= (char *) set
- pool
->data
;
524 if (index
+ set
->size
== pool
->next
) {
527 struct pool_free_list_entry
*entry
= (struct pool_free_list_entry
*) set
;
528 entry
->next
= pool
->free_list
;
529 entry
->size
= set
->size
;
530 pool
->free_list
= (char *) entry
- pool
->data
;
534 VkResult
anv_AllocateDescriptorSets(
536 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
537 VkDescriptorSet
* pDescriptorSets
)
539 ANV_FROM_HANDLE(anv_device
, device
, _device
);
540 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
542 VkResult result
= VK_SUCCESS
;
543 struct anv_descriptor_set
*set
;
546 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
547 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
548 pAllocateInfo
->pSetLayouts
[i
]);
550 result
= anv_descriptor_set_create(device
, pool
, layout
, &set
);
551 if (result
!= VK_SUCCESS
)
554 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
557 if (result
!= VK_SUCCESS
)
558 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
564 VkResult
anv_FreeDescriptorSets(
566 VkDescriptorPool descriptorPool
,
568 const VkDescriptorSet
* pDescriptorSets
)
570 ANV_FROM_HANDLE(anv_device
, device
, _device
);
571 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
573 for (uint32_t i
= 0; i
< count
; i
++) {
574 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
579 anv_descriptor_set_destroy(device
, pool
, set
);
586 anv_descriptor_set_write_image_view(struct anv_descriptor_set
*set
,
587 const struct gen_device_info
* const devinfo
,
588 const VkDescriptorImageInfo
* const info
,
589 VkDescriptorType type
,
593 const struct anv_descriptor_set_binding_layout
*bind_layout
=
594 &set
->layout
->binding
[binding
];
595 struct anv_descriptor
*desc
=
596 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
597 struct anv_image_view
*image_view
= NULL
;
598 struct anv_sampler
*sampler
= NULL
;
600 assert(type
== bind_layout
->type
);
603 case VK_DESCRIPTOR_TYPE_SAMPLER
:
604 sampler
= anv_sampler_from_handle(info
->sampler
);
607 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
608 image_view
= anv_image_view_from_handle(info
->imageView
);
609 sampler
= anv_sampler_from_handle(info
->sampler
);
612 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
613 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
614 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
615 image_view
= anv_image_view_from_handle(info
->imageView
);
619 unreachable("invalid descriptor type");
622 /* If this descriptor has an immutable sampler, we don't want to stomp on
625 sampler
= bind_layout
->immutable_samplers
?
626 bind_layout
->immutable_samplers
[element
] :
629 *desc
= (struct anv_descriptor
) {
631 .layout
= info
->imageLayout
,
632 .image_view
= image_view
,
638 anv_descriptor_set_write_buffer_view(struct anv_descriptor_set
*set
,
639 VkDescriptorType type
,
640 struct anv_buffer_view
*buffer_view
,
644 const struct anv_descriptor_set_binding_layout
*bind_layout
=
645 &set
->layout
->binding
[binding
];
646 struct anv_descriptor
*desc
=
647 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
649 assert(type
== bind_layout
->type
);
651 *desc
= (struct anv_descriptor
) {
653 .buffer_view
= buffer_view
,
658 anv_descriptor_set_write_buffer(struct anv_descriptor_set
*set
,
659 struct anv_device
*device
,
660 struct anv_state_stream
*alloc_stream
,
661 VkDescriptorType type
,
662 struct anv_buffer
*buffer
,
668 const struct anv_descriptor_set_binding_layout
*bind_layout
=
669 &set
->layout
->binding
[binding
];
670 struct anv_descriptor
*desc
=
671 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
673 assert(type
== bind_layout
->type
);
675 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
676 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
677 *desc
= (struct anv_descriptor
) {
684 struct anv_buffer_view
*bview
=
685 &set
->buffer_views
[bind_layout
->buffer_index
+ element
];
687 bview
->format
= anv_isl_format_for_descriptor_type(type
);
688 bview
->bo
= buffer
->bo
;
689 bview
->offset
= buffer
->offset
+ offset
;
690 bview
->range
= anv_buffer_get_range(buffer
, offset
, range
);
692 /* If we're writing descriptors through a push command, we need to
693 * allocate the surface state from the command buffer. Otherwise it will
694 * be allocated by the descriptor pool when calling
695 * vkAllocateDescriptorSets. */
697 bview
->surface_state
= anv_state_stream_alloc(alloc_stream
, 64, 64);
699 anv_fill_buffer_surface_state(device
, bview
->surface_state
,
701 bview
->offset
, bview
->range
, 1);
703 *desc
= (struct anv_descriptor
) {
705 .buffer_view
= bview
,
710 void anv_UpdateDescriptorSets(
712 uint32_t descriptorWriteCount
,
713 const VkWriteDescriptorSet
* pDescriptorWrites
,
714 uint32_t descriptorCopyCount
,
715 const VkCopyDescriptorSet
* pDescriptorCopies
)
717 ANV_FROM_HANDLE(anv_device
, device
, _device
);
719 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
720 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
721 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
723 switch (write
->descriptorType
) {
724 case VK_DESCRIPTOR_TYPE_SAMPLER
:
725 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
726 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
727 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
728 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
729 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
730 anv_descriptor_set_write_image_view(set
, &device
->info
,
731 write
->pImageInfo
+ j
,
732 write
->descriptorType
,
734 write
->dstArrayElement
+ j
);
738 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
739 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
740 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
741 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
742 write
->pTexelBufferView
[j
]);
744 anv_descriptor_set_write_buffer_view(set
,
745 write
->descriptorType
,
748 write
->dstArrayElement
+ j
);
752 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
753 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
754 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
755 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
756 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
757 assert(write
->pBufferInfo
[j
].buffer
);
758 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
761 anv_descriptor_set_write_buffer(set
,
764 write
->descriptorType
,
767 write
->dstArrayElement
+ j
,
768 write
->pBufferInfo
[j
].offset
,
769 write
->pBufferInfo
[j
].range
);
778 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
779 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
780 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->srcSet
);
781 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
783 const struct anv_descriptor_set_binding_layout
*src_layout
=
784 &src
->layout
->binding
[copy
->srcBinding
];
785 struct anv_descriptor
*src_desc
=
786 &src
->descriptors
[src_layout
->descriptor_index
];
787 src_desc
+= copy
->srcArrayElement
;
789 const struct anv_descriptor_set_binding_layout
*dst_layout
=
790 &dst
->layout
->binding
[copy
->dstBinding
];
791 struct anv_descriptor
*dst_desc
=
792 &dst
->descriptors
[dst_layout
->descriptor_index
];
793 dst_desc
+= copy
->dstArrayElement
;
795 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
796 dst_desc
[j
] = src_desc
[j
];
801 * Descriptor update templates.
805 anv_descriptor_set_write_template(struct anv_descriptor_set
*set
,
806 struct anv_device
*device
,
807 struct anv_state_stream
*alloc_stream
,
808 const struct anv_descriptor_update_template
*template,
811 const struct anv_descriptor_set_layout
*layout
= set
->layout
;
813 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
814 const struct anv_descriptor_template_entry
*entry
=
815 &template->entries
[i
];
816 const struct anv_descriptor_set_binding_layout
*bind_layout
=
817 &layout
->binding
[entry
->binding
];
818 struct anv_descriptor
*desc
= &set
->descriptors
[bind_layout
->descriptor_index
];
819 desc
+= entry
->array_element
;
821 switch (entry
->type
) {
822 case VK_DESCRIPTOR_TYPE_SAMPLER
:
823 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
824 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
825 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
826 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
827 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
828 const VkDescriptorImageInfo
*info
=
829 data
+ entry
->offset
+ j
* entry
->stride
;
830 anv_descriptor_set_write_image_view(set
, &device
->info
,
833 entry
->array_element
+ j
);
837 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
838 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
839 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
840 const VkBufferView
*_bview
=
841 data
+ entry
->offset
+ j
* entry
->stride
;
842 ANV_FROM_HANDLE(anv_buffer_view
, bview
, *_bview
);
844 anv_descriptor_set_write_buffer_view(set
,
848 entry
->array_element
+ j
);
852 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
853 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
854 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
855 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
856 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
857 const VkDescriptorBufferInfo
*info
=
858 data
+ entry
->offset
+ j
* entry
->stride
;
859 ANV_FROM_HANDLE(anv_buffer
, buffer
, info
->buffer
);
861 anv_descriptor_set_write_buffer(set
,
867 entry
->array_element
+ j
,
868 info
->offset
, info
->range
);
878 VkResult
anv_CreateDescriptorUpdateTemplateKHR(
880 const VkDescriptorUpdateTemplateCreateInfoKHR
* pCreateInfo
,
881 const VkAllocationCallbacks
* pAllocator
,
882 VkDescriptorUpdateTemplateKHR
* pDescriptorUpdateTemplate
)
884 ANV_FROM_HANDLE(anv_device
, device
, _device
);
885 struct anv_descriptor_update_template
*template;
887 size_t size
= sizeof(*template) +
888 pCreateInfo
->descriptorUpdateEntryCount
* sizeof(template->entries
[0]);
889 template = vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
890 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
891 if (template == NULL
)
892 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
894 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR
)
895 template->set
= pCreateInfo
->set
;
897 template->entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
898 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
899 const VkDescriptorUpdateTemplateEntryKHR
*pEntry
=
900 &pCreateInfo
->pDescriptorUpdateEntries
[i
];
902 template->entries
[i
] = (struct anv_descriptor_template_entry
) {
903 .type
= pEntry
->descriptorType
,
904 .binding
= pEntry
->dstBinding
,
905 .array_element
= pEntry
->dstArrayElement
,
906 .array_count
= pEntry
->descriptorCount
,
907 .offset
= pEntry
->offset
,
908 .stride
= pEntry
->stride
,
912 *pDescriptorUpdateTemplate
=
913 anv_descriptor_update_template_to_handle(template);
918 void anv_DestroyDescriptorUpdateTemplateKHR(
920 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
921 const VkAllocationCallbacks
* pAllocator
)
923 ANV_FROM_HANDLE(anv_device
, device
, _device
);
924 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
925 descriptorUpdateTemplate
);
927 vk_free2(&device
->alloc
, pAllocator
, template);
930 void anv_UpdateDescriptorSetWithTemplateKHR(
932 VkDescriptorSet descriptorSet
,
933 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
936 ANV_FROM_HANDLE(anv_device
, device
, _device
);
937 ANV_FROM_HANDLE(anv_descriptor_set
, set
, descriptorSet
);
938 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
939 descriptorUpdateTemplate
);
941 anv_descriptor_set_write_template(set
, device
, NULL
, template, pData
);