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");
85 void anv_GetDescriptorSetLayoutSupport(
87 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
88 VkDescriptorSetLayoutSupport
* pSupport
)
90 uint32_t surface_count
[MESA_SHADER_STAGES
] = { 0, };
92 for (uint32_t b
= 0; b
< pCreateInfo
->bindingCount
; b
++) {
93 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[b
];
95 switch (binding
->descriptorType
) {
96 case VK_DESCRIPTOR_TYPE_SAMPLER
:
97 /* There is no real limit on samplers */
100 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
101 if (binding
->pImmutableSamplers
) {
102 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
103 ANV_FROM_HANDLE(anv_sampler
, sampler
,
104 binding
->pImmutableSamplers
[i
]);
105 anv_foreach_stage(s
, binding
->stageFlags
)
106 surface_count
[s
] += sampler
->n_planes
;
109 anv_foreach_stage(s
, binding
->stageFlags
)
110 surface_count
[s
] += binding
->descriptorCount
;
115 anv_foreach_stage(s
, binding
->stageFlags
)
116 surface_count
[s
] += binding
->descriptorCount
;
121 bool supported
= true;
122 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
123 /* Our maximum binding table size is 250 and we need to reserve 8 for
124 * render targets. 240 is a nice round number.
126 if (surface_count
[s
] >= 240)
130 pSupport
->supported
= supported
;
133 VkResult
anv_CreateDescriptorSetLayout(
135 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
136 const VkAllocationCallbacks
* pAllocator
,
137 VkDescriptorSetLayout
* pSetLayout
)
139 ANV_FROM_HANDLE(anv_device
, device
, _device
);
141 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
143 uint32_t max_binding
= 0;
144 uint32_t immutable_sampler_count
= 0;
145 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
146 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
148 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
150 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
151 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
152 * pImmutableSamplers can be used to initialize a set of immutable
153 * samplers. [...] If descriptorType is not one of these descriptor
154 * types, then pImmutableSamplers is ignored.
156 * We need to be careful here and only parse pImmutableSamplers if we
157 * have one of the right descriptor types.
159 VkDescriptorType desc_type
= pCreateInfo
->pBindings
[j
].descriptorType
;
160 if ((desc_type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
161 desc_type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
) &&
162 pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
163 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
166 struct anv_descriptor_set_layout
*set_layout
;
167 struct anv_descriptor_set_binding_layout
*bindings
;
168 struct anv_sampler
**samplers
;
170 /* We need to allocate decriptor set layouts off the device allocator
171 * with DEVICE scope because they are reference counted and may not be
172 * destroyed when vkDestroyDescriptorSetLayout is called.
175 anv_multialloc_add(&ma
, &set_layout
, 1);
176 anv_multialloc_add(&ma
, &bindings
, max_binding
+ 1);
177 anv_multialloc_add(&ma
, &samplers
, immutable_sampler_count
);
179 if (!anv_multialloc_alloc(&ma
, &device
->alloc
,
180 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
))
181 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
183 memset(set_layout
, 0, sizeof(*set_layout
));
184 set_layout
->ref_cnt
= 1;
185 set_layout
->binding_count
= max_binding
+ 1;
187 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
188 /* Initialize all binding_layout entries to -1 */
189 memset(&set_layout
->binding
[b
], -1, sizeof(set_layout
->binding
[b
]));
191 set_layout
->binding
[b
].data
= 0;
192 set_layout
->binding
[b
].array_size
= 0;
193 set_layout
->binding
[b
].immutable_samplers
= NULL
;
196 /* Initialize all samplers to 0 */
197 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
199 uint32_t buffer_view_count
= 0;
200 uint32_t dynamic_offset_count
= 0;
202 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
203 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
204 uint32_t b
= binding
->binding
;
205 /* We temporarily store the pointer to the binding in the
206 * immutable_samplers pointer. This provides us with a quick-and-dirty
207 * way to sort the bindings by binding number.
209 set_layout
->binding
[b
].immutable_samplers
= (void *)binding
;
212 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
213 const VkDescriptorSetLayoutBinding
*binding
=
214 (void *)set_layout
->binding
[b
].immutable_samplers
;
219 /* We temporarily stashed the pointer to the binding in the
220 * immutable_samplers pointer. Now that we've pulled it back out
221 * again, we reset immutable_samplers to NULL.
223 set_layout
->binding
[b
].immutable_samplers
= NULL
;
225 if (binding
->descriptorCount
== 0)
229 set_layout
->binding
[b
].type
= binding
->descriptorType
;
231 set_layout
->binding
[b
].data
=
232 anv_descriptor_data_for_type(&device
->instance
->physicalDevice
,
233 binding
->descriptorType
);
234 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
235 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
236 set_layout
->size
+= binding
->descriptorCount
;
238 if (set_layout
->binding
[b
].data
& ANV_DESCRIPTOR_BUFFER_VIEW
) {
239 set_layout
->binding
[b
].buffer_view_index
= buffer_view_count
;
240 buffer_view_count
+= binding
->descriptorCount
;
243 switch (binding
->descriptorType
) {
244 case VK_DESCRIPTOR_TYPE_SAMPLER
:
245 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
246 if (binding
->pImmutableSamplers
) {
247 set_layout
->binding
[b
].immutable_samplers
= samplers
;
248 samplers
+= binding
->descriptorCount
;
250 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
251 set_layout
->binding
[b
].immutable_samplers
[i
] =
252 anv_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
259 switch (binding
->descriptorType
) {
260 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
261 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
262 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
263 dynamic_offset_count
+= binding
->descriptorCount
;
270 set_layout
->shader_stages
|= binding
->stageFlags
;
273 set_layout
->buffer_view_count
= buffer_view_count
;
274 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
276 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
281 void anv_DestroyDescriptorSetLayout(
283 VkDescriptorSetLayout _set_layout
,
284 const VkAllocationCallbacks
* pAllocator
)
286 ANV_FROM_HANDLE(anv_device
, device
, _device
);
287 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
292 anv_descriptor_set_layout_unref(device
, set_layout
);
295 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
298 sha1_update_immutable_sampler(struct mesa_sha1
*ctx
,
299 const struct anv_sampler
*sampler
)
301 if (!sampler
->conversion
)
304 /* The only thing that affects the shader is ycbcr conversion */
305 _mesa_sha1_update(ctx
, sampler
->conversion
,
306 sizeof(*sampler
->conversion
));
310 sha1_update_descriptor_set_binding_layout(struct mesa_sha1
*ctx
,
311 const struct anv_descriptor_set_binding_layout
*layout
)
313 SHA1_UPDATE_VALUE(ctx
, layout
->data
);
314 SHA1_UPDATE_VALUE(ctx
, layout
->array_size
);
315 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_index
);
316 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_index
);
317 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_index
);
319 if (layout
->immutable_samplers
) {
320 for (uint16_t i
= 0; i
< layout
->array_size
; i
++)
321 sha1_update_immutable_sampler(ctx
, layout
->immutable_samplers
[i
]);
326 sha1_update_descriptor_set_layout(struct mesa_sha1
*ctx
,
327 const struct anv_descriptor_set_layout
*layout
)
329 SHA1_UPDATE_VALUE(ctx
, layout
->binding_count
);
330 SHA1_UPDATE_VALUE(ctx
, layout
->size
);
331 SHA1_UPDATE_VALUE(ctx
, layout
->shader_stages
);
332 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_count
);
333 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_count
);
335 for (uint16_t i
= 0; i
< layout
->binding_count
; i
++)
336 sha1_update_descriptor_set_binding_layout(ctx
, &layout
->binding
[i
]);
340 * Pipeline layouts. These have nothing to do with the pipeline. They are
341 * just multiple descriptor set layouts pasted together
344 VkResult
anv_CreatePipelineLayout(
346 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
347 const VkAllocationCallbacks
* pAllocator
,
348 VkPipelineLayout
* pPipelineLayout
)
350 ANV_FROM_HANDLE(anv_device
, device
, _device
);
351 struct anv_pipeline_layout
*layout
;
353 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
355 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
356 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
358 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
360 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
362 unsigned dynamic_offset_count
= 0;
364 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
365 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
366 pCreateInfo
->pSetLayouts
[set
]);
367 layout
->set
[set
].layout
= set_layout
;
368 anv_descriptor_set_layout_ref(set_layout
);
370 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
371 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
372 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
375 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
379 struct mesa_sha1 ctx
;
380 _mesa_sha1_init(&ctx
);
381 for (unsigned s
= 0; s
< layout
->num_sets
; s
++) {
382 sha1_update_descriptor_set_layout(&ctx
, layout
->set
[s
].layout
);
383 _mesa_sha1_update(&ctx
, &layout
->set
[s
].dynamic_offset_start
,
384 sizeof(layout
->set
[s
].dynamic_offset_start
));
386 _mesa_sha1_update(&ctx
, &layout
->num_sets
, sizeof(layout
->num_sets
));
387 _mesa_sha1_final(&ctx
, layout
->sha1
);
389 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
394 void anv_DestroyPipelineLayout(
396 VkPipelineLayout _pipelineLayout
,
397 const VkAllocationCallbacks
* pAllocator
)
399 ANV_FROM_HANDLE(anv_device
, device
, _device
);
400 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
402 if (!pipeline_layout
)
405 for (uint32_t i
= 0; i
< pipeline_layout
->num_sets
; i
++)
406 anv_descriptor_set_layout_unref(device
, pipeline_layout
->set
[i
].layout
);
408 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
414 * These are implemented using a big pool of memory and a free-list for the
415 * host memory allocations and a state_stream and a free list for the buffer
416 * view surface state. The spec allows us to fail to allocate due to
417 * fragmentation in all cases but two: 1) after pool reset, allocating up
418 * until the pool size with no freeing must succeed and 2) allocating and
419 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
420 * and the free lists lets us recycle blocks for case 2).
425 VkResult
anv_CreateDescriptorPool(
427 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
428 const VkAllocationCallbacks
* pAllocator
,
429 VkDescriptorPool
* pDescriptorPool
)
431 ANV_FROM_HANDLE(anv_device
, device
, _device
);
432 struct anv_descriptor_pool
*pool
;
434 uint32_t descriptor_count
= 0;
435 uint32_t buffer_view_count
= 0;
436 for (uint32_t i
= 0; i
< pCreateInfo
->poolSizeCount
; i
++) {
437 enum anv_descriptor_data desc_data
=
438 anv_descriptor_data_for_type(&device
->instance
->physicalDevice
,
439 pCreateInfo
->pPoolSizes
[i
].type
);
441 if (desc_data
& ANV_DESCRIPTOR_BUFFER_VIEW
)
442 buffer_view_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
444 descriptor_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
447 const size_t pool_size
=
448 pCreateInfo
->maxSets
* sizeof(struct anv_descriptor_set
) +
449 descriptor_count
* sizeof(struct anv_descriptor
) +
450 buffer_view_count
* sizeof(struct anv_buffer_view
);
451 const size_t total_size
= sizeof(*pool
) + pool_size
;
453 pool
= vk_alloc2(&device
->alloc
, pAllocator
, total_size
, 8,
454 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
456 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
458 pool
->size
= pool_size
;
460 pool
->free_list
= EMPTY
;
462 anv_state_stream_init(&pool
->surface_state_stream
,
463 &device
->surface_state_pool
, 4096);
464 pool
->surface_state_free_list
= NULL
;
466 *pDescriptorPool
= anv_descriptor_pool_to_handle(pool
);
471 void anv_DestroyDescriptorPool(
473 VkDescriptorPool _pool
,
474 const VkAllocationCallbacks
* pAllocator
)
476 ANV_FROM_HANDLE(anv_device
, device
, _device
);
477 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, _pool
);
482 anv_state_stream_finish(&pool
->surface_state_stream
);
483 vk_free2(&device
->alloc
, pAllocator
, pool
);
486 VkResult
anv_ResetDescriptorPool(
488 VkDescriptorPool descriptorPool
,
489 VkDescriptorPoolResetFlags flags
)
491 ANV_FROM_HANDLE(anv_device
, device
, _device
);
492 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
495 pool
->free_list
= EMPTY
;
496 anv_state_stream_finish(&pool
->surface_state_stream
);
497 anv_state_stream_init(&pool
->surface_state_stream
,
498 &device
->surface_state_pool
, 4096);
499 pool
->surface_state_free_list
= NULL
;
504 struct pool_free_list_entry
{
510 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool
*pool
,
512 struct anv_descriptor_set
**set
)
514 if (size
<= pool
->size
- pool
->next
) {
515 *set
= (struct anv_descriptor_set
*) (pool
->data
+ pool
->next
);
519 struct pool_free_list_entry
*entry
;
520 uint32_t *link
= &pool
->free_list
;
521 for (uint32_t f
= pool
->free_list
; f
!= EMPTY
; f
= entry
->next
) {
522 entry
= (struct pool_free_list_entry
*) (pool
->data
+ f
);
523 if (size
<= entry
->size
) {
525 *set
= (struct anv_descriptor_set
*) entry
;
531 if (pool
->free_list
!= EMPTY
) {
532 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
534 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY
);
540 anv_descriptor_pool_free_set(struct anv_descriptor_pool
*pool
,
541 struct anv_descriptor_set
*set
)
543 /* Put the descriptor set allocation back on the free list. */
544 const uint32_t index
= (char *) set
- pool
->data
;
545 if (index
+ set
->size
== pool
->next
) {
548 struct pool_free_list_entry
*entry
= (struct pool_free_list_entry
*) set
;
549 entry
->next
= pool
->free_list
;
550 entry
->size
= set
->size
;
551 pool
->free_list
= (char *) entry
- pool
->data
;
555 struct surface_state_free_list_entry
{
557 struct anv_state state
;
560 static struct anv_state
561 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool
*pool
)
563 struct surface_state_free_list_entry
*entry
=
564 pool
->surface_state_free_list
;
567 struct anv_state state
= entry
->state
;
568 pool
->surface_state_free_list
= entry
->next
;
569 assert(state
.alloc_size
== 64);
572 return anv_state_stream_alloc(&pool
->surface_state_stream
, 64, 64);
577 anv_descriptor_pool_free_state(struct anv_descriptor_pool
*pool
,
578 struct anv_state state
)
580 /* Put the buffer view surface state back on the free list. */
581 struct surface_state_free_list_entry
*entry
= state
.map
;
582 entry
->next
= pool
->surface_state_free_list
;
583 entry
->state
= state
;
584 pool
->surface_state_free_list
= entry
;
588 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout
*layout
)
591 sizeof(struct anv_descriptor_set
) +
592 layout
->size
* sizeof(struct anv_descriptor
) +
593 layout
->buffer_view_count
* sizeof(struct anv_buffer_view
);
597 anv_descriptor_set_create(struct anv_device
*device
,
598 struct anv_descriptor_pool
*pool
,
599 struct anv_descriptor_set_layout
*layout
,
600 struct anv_descriptor_set
**out_set
)
602 struct anv_descriptor_set
*set
;
603 const size_t size
= anv_descriptor_set_layout_size(layout
);
605 VkResult result
= anv_descriptor_pool_alloc_set(pool
, size
, &set
);
606 if (result
!= VK_SUCCESS
)
609 set
->layout
= layout
;
610 anv_descriptor_set_layout_ref(layout
);
614 (struct anv_buffer_view
*) &set
->descriptors
[layout
->size
];
615 set
->buffer_view_count
= layout
->buffer_view_count
;
617 /* By defining the descriptors to be zero now, we can later verify that
618 * a descriptor has not been populated with user data.
620 memset(set
->descriptors
, 0, sizeof(struct anv_descriptor
) * layout
->size
);
622 /* Go through and fill out immutable samplers if we have any */
623 struct anv_descriptor
*desc
= set
->descriptors
;
624 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
625 if (layout
->binding
[b
].immutable_samplers
) {
626 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++) {
627 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
628 * UpdateDescriptorSets if needed. However, if the descriptor
629 * set has an immutable sampler, UpdateDescriptorSets may never
630 * touch it, so we need to make sure it's 100% valid now.
632 desc
[i
] = (struct anv_descriptor
) {
633 .type
= VK_DESCRIPTOR_TYPE_SAMPLER
,
634 .sampler
= layout
->binding
[b
].immutable_samplers
[i
],
638 desc
+= layout
->binding
[b
].array_size
;
641 /* Allocate surface state for the buffer views. */
642 for (uint32_t b
= 0; b
< layout
->buffer_view_count
; b
++) {
643 set
->buffer_views
[b
].surface_state
=
644 anv_descriptor_pool_alloc_state(pool
);
653 anv_descriptor_set_destroy(struct anv_device
*device
,
654 struct anv_descriptor_pool
*pool
,
655 struct anv_descriptor_set
*set
)
657 anv_descriptor_set_layout_unref(device
, set
->layout
);
659 for (uint32_t b
= 0; b
< set
->buffer_view_count
; b
++)
660 anv_descriptor_pool_free_state(pool
, set
->buffer_views
[b
].surface_state
);
662 anv_descriptor_pool_free_set(pool
, set
);
665 VkResult
anv_AllocateDescriptorSets(
667 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
668 VkDescriptorSet
* pDescriptorSets
)
670 ANV_FROM_HANDLE(anv_device
, device
, _device
);
671 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
673 VkResult result
= VK_SUCCESS
;
674 struct anv_descriptor_set
*set
;
677 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
678 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
679 pAllocateInfo
->pSetLayouts
[i
]);
681 result
= anv_descriptor_set_create(device
, pool
, layout
, &set
);
682 if (result
!= VK_SUCCESS
)
685 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
688 if (result
!= VK_SUCCESS
)
689 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
695 VkResult
anv_FreeDescriptorSets(
697 VkDescriptorPool descriptorPool
,
699 const VkDescriptorSet
* pDescriptorSets
)
701 ANV_FROM_HANDLE(anv_device
, device
, _device
);
702 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
704 for (uint32_t i
= 0; i
< count
; i
++) {
705 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
710 anv_descriptor_set_destroy(device
, pool
, set
);
717 anv_descriptor_set_write_image_view(struct anv_device
*device
,
718 struct anv_descriptor_set
*set
,
719 const VkDescriptorImageInfo
* const info
,
720 VkDescriptorType type
,
724 const struct anv_descriptor_set_binding_layout
*bind_layout
=
725 &set
->layout
->binding
[binding
];
726 struct anv_descriptor
*desc
=
727 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
728 struct anv_image_view
*image_view
= NULL
;
729 struct anv_sampler
*sampler
= NULL
;
731 assert(type
== bind_layout
->type
);
734 case VK_DESCRIPTOR_TYPE_SAMPLER
:
735 sampler
= anv_sampler_from_handle(info
->sampler
);
738 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
739 image_view
= anv_image_view_from_handle(info
->imageView
);
740 sampler
= anv_sampler_from_handle(info
->sampler
);
743 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
744 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
745 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
746 image_view
= anv_image_view_from_handle(info
->imageView
);
750 unreachable("invalid descriptor type");
753 /* If this descriptor has an immutable sampler, we don't want to stomp on
756 sampler
= bind_layout
->immutable_samplers
?
757 bind_layout
->immutable_samplers
[element
] :
760 *desc
= (struct anv_descriptor
) {
762 .layout
= info
->imageLayout
,
763 .image_view
= image_view
,
769 anv_descriptor_set_write_buffer_view(struct anv_device
*device
,
770 struct anv_descriptor_set
*set
,
771 VkDescriptorType type
,
772 struct anv_buffer_view
*buffer_view
,
776 const struct anv_descriptor_set_binding_layout
*bind_layout
=
777 &set
->layout
->binding
[binding
];
778 struct anv_descriptor
*desc
=
779 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
781 assert(type
== bind_layout
->type
);
783 *desc
= (struct anv_descriptor
) {
785 .buffer_view
= buffer_view
,
790 anv_descriptor_set_write_buffer(struct anv_device
*device
,
791 struct anv_descriptor_set
*set
,
792 struct anv_state_stream
*alloc_stream
,
793 VkDescriptorType type
,
794 struct anv_buffer
*buffer
,
800 const struct anv_descriptor_set_binding_layout
*bind_layout
=
801 &set
->layout
->binding
[binding
];
802 struct anv_descriptor
*desc
=
803 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
805 assert(type
== bind_layout
->type
);
807 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
808 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
809 *desc
= (struct anv_descriptor
) {
816 assert(bind_layout
->data
& ANV_DESCRIPTOR_BUFFER_VIEW
);
817 struct anv_buffer_view
*bview
=
818 &set
->buffer_views
[bind_layout
->buffer_view_index
+ element
];
820 bview
->format
= anv_isl_format_for_descriptor_type(type
);
821 bview
->range
= anv_buffer_get_range(buffer
, offset
, range
);
822 bview
->address
= anv_address_add(buffer
->address
, offset
);
824 /* If we're writing descriptors through a push command, we need to
825 * allocate the surface state from the command buffer. Otherwise it will
826 * be allocated by the descriptor pool when calling
827 * vkAllocateDescriptorSets. */
829 bview
->surface_state
= anv_state_stream_alloc(alloc_stream
, 64, 64);
831 anv_fill_buffer_surface_state(device
, bview
->surface_state
,
833 bview
->address
, bview
->range
, 1);
835 *desc
= (struct anv_descriptor
) {
837 .buffer_view
= bview
,
842 void anv_UpdateDescriptorSets(
844 uint32_t descriptorWriteCount
,
845 const VkWriteDescriptorSet
* pDescriptorWrites
,
846 uint32_t descriptorCopyCount
,
847 const VkCopyDescriptorSet
* pDescriptorCopies
)
849 ANV_FROM_HANDLE(anv_device
, device
, _device
);
851 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
852 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
853 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
855 switch (write
->descriptorType
) {
856 case VK_DESCRIPTOR_TYPE_SAMPLER
:
857 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
858 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
859 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
860 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
861 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
862 anv_descriptor_set_write_image_view(device
, set
,
863 write
->pImageInfo
+ j
,
864 write
->descriptorType
,
866 write
->dstArrayElement
+ j
);
870 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
871 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
872 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
873 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
874 write
->pTexelBufferView
[j
]);
876 anv_descriptor_set_write_buffer_view(device
, set
,
877 write
->descriptorType
,
880 write
->dstArrayElement
+ j
);
884 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
885 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
886 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
887 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
888 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
889 assert(write
->pBufferInfo
[j
].buffer
);
890 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
893 anv_descriptor_set_write_buffer(device
, set
,
895 write
->descriptorType
,
898 write
->dstArrayElement
+ j
,
899 write
->pBufferInfo
[j
].offset
,
900 write
->pBufferInfo
[j
].range
);
909 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
910 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
911 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->srcSet
);
912 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
914 const struct anv_descriptor_set_binding_layout
*src_layout
=
915 &src
->layout
->binding
[copy
->srcBinding
];
916 struct anv_descriptor
*src_desc
=
917 &src
->descriptors
[src_layout
->descriptor_index
];
918 src_desc
+= copy
->srcArrayElement
;
920 const struct anv_descriptor_set_binding_layout
*dst_layout
=
921 &dst
->layout
->binding
[copy
->dstBinding
];
922 struct anv_descriptor
*dst_desc
=
923 &dst
->descriptors
[dst_layout
->descriptor_index
];
924 dst_desc
+= copy
->dstArrayElement
;
926 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
927 dst_desc
[j
] = src_desc
[j
];
932 * Descriptor update templates.
936 anv_descriptor_set_write_template(struct anv_device
*device
,
937 struct anv_descriptor_set
*set
,
938 struct anv_state_stream
*alloc_stream
,
939 const struct anv_descriptor_update_template
*template,
942 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
943 const struct anv_descriptor_template_entry
*entry
=
944 &template->entries
[i
];
946 switch (entry
->type
) {
947 case VK_DESCRIPTOR_TYPE_SAMPLER
:
948 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
949 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
950 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
951 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
952 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
953 const VkDescriptorImageInfo
*info
=
954 data
+ entry
->offset
+ j
* entry
->stride
;
955 anv_descriptor_set_write_image_view(device
, set
,
958 entry
->array_element
+ j
);
962 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
963 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
964 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
965 const VkBufferView
*_bview
=
966 data
+ entry
->offset
+ j
* entry
->stride
;
967 ANV_FROM_HANDLE(anv_buffer_view
, bview
, *_bview
);
969 anv_descriptor_set_write_buffer_view(device
, set
,
973 entry
->array_element
+ j
);
977 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
978 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
979 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
980 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
981 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
982 const VkDescriptorBufferInfo
*info
=
983 data
+ entry
->offset
+ j
* entry
->stride
;
984 ANV_FROM_HANDLE(anv_buffer
, buffer
, info
->buffer
);
986 anv_descriptor_set_write_buffer(device
, set
,
991 entry
->array_element
+ j
,
992 info
->offset
, info
->range
);
1002 VkResult
anv_CreateDescriptorUpdateTemplate(
1004 const VkDescriptorUpdateTemplateCreateInfo
* pCreateInfo
,
1005 const VkAllocationCallbacks
* pAllocator
,
1006 VkDescriptorUpdateTemplate
* pDescriptorUpdateTemplate
)
1008 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1009 struct anv_descriptor_update_template
*template;
1011 size_t size
= sizeof(*template) +
1012 pCreateInfo
->descriptorUpdateEntryCount
* sizeof(template->entries
[0]);
1013 template = vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1014 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1015 if (template == NULL
)
1016 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1018 template->bind_point
= pCreateInfo
->pipelineBindPoint
;
1020 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
)
1021 template->set
= pCreateInfo
->set
;
1023 template->entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1024 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1025 const VkDescriptorUpdateTemplateEntry
*pEntry
=
1026 &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1028 template->entries
[i
] = (struct anv_descriptor_template_entry
) {
1029 .type
= pEntry
->descriptorType
,
1030 .binding
= pEntry
->dstBinding
,
1031 .array_element
= pEntry
->dstArrayElement
,
1032 .array_count
= pEntry
->descriptorCount
,
1033 .offset
= pEntry
->offset
,
1034 .stride
= pEntry
->stride
,
1038 *pDescriptorUpdateTemplate
=
1039 anv_descriptor_update_template_to_handle(template);
1044 void anv_DestroyDescriptorUpdateTemplate(
1046 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1047 const VkAllocationCallbacks
* pAllocator
)
1049 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1050 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1051 descriptorUpdateTemplate
);
1053 vk_free2(&device
->alloc
, pAllocator
, template);
1056 void anv_UpdateDescriptorSetWithTemplate(
1058 VkDescriptorSet descriptorSet
,
1059 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1062 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1063 ANV_FROM_HANDLE(anv_descriptor_set
, set
, descriptorSet
);
1064 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1065 descriptorUpdateTemplate
);
1067 anv_descriptor_set_write_template(device
, set
, NULL
, template, pData
);