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"
33 #include "anv_private.h"
36 * Descriptor set layouts.
39 static enum anv_descriptor_data
40 anv_descriptor_data_for_type(const struct anv_physical_device
*device
,
41 VkDescriptorType type
)
43 enum anv_descriptor_data data
= 0;
46 case VK_DESCRIPTOR_TYPE_SAMPLER
:
47 data
= ANV_DESCRIPTOR_SAMPLER_STATE
;
48 if (device
->has_bindless_samplers
)
49 data
|= ANV_DESCRIPTOR_SAMPLED_IMAGE
;
52 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
53 data
= ANV_DESCRIPTOR_SURFACE_STATE
|
54 ANV_DESCRIPTOR_SAMPLER_STATE
;
55 if (device
->has_bindless_images
|| device
->has_bindless_samplers
)
56 data
|= ANV_DESCRIPTOR_SAMPLED_IMAGE
;
59 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
60 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
61 data
= ANV_DESCRIPTOR_SURFACE_STATE
;
62 if (device
->has_bindless_images
)
63 data
|= ANV_DESCRIPTOR_SAMPLED_IMAGE
;
66 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
67 data
= ANV_DESCRIPTOR_SURFACE_STATE
;
70 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
71 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
72 data
= ANV_DESCRIPTOR_SURFACE_STATE
;
73 if (device
->info
.gen
< 9)
74 data
|= ANV_DESCRIPTOR_IMAGE_PARAM
;
75 if (device
->has_bindless_images
)
76 data
|= ANV_DESCRIPTOR_STORAGE_IMAGE
;
79 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
80 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
81 data
= ANV_DESCRIPTOR_SURFACE_STATE
|
82 ANV_DESCRIPTOR_BUFFER_VIEW
;
85 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
86 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
87 data
= ANV_DESCRIPTOR_SURFACE_STATE
;
90 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
91 data
= ANV_DESCRIPTOR_INLINE_UNIFORM
;
95 unreachable("Unsupported descriptor type");
98 /* On gen8 and above when we have softpin enabled, we also need to push
99 * SSBO address ranges so that we can use A64 messages in the shader.
101 if (device
->has_a64_buffer_access
&&
102 (type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
||
103 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
))
104 data
|= ANV_DESCRIPTOR_ADDRESS_RANGE
;
110 anv_descriptor_data_size(enum anv_descriptor_data data
)
114 if (data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
)
115 size
+= sizeof(struct anv_sampled_image_descriptor
);
117 if (data
& ANV_DESCRIPTOR_STORAGE_IMAGE
)
118 size
+= sizeof(struct anv_storage_image_descriptor
);
120 if (data
& ANV_DESCRIPTOR_IMAGE_PARAM
)
121 size
+= BRW_IMAGE_PARAM_SIZE
* 4;
123 if (data
& ANV_DESCRIPTOR_ADDRESS_RANGE
)
124 size
+= sizeof(struct anv_address_range_descriptor
);
129 /** Returns the size in bytes of each descriptor with the given layout */
131 anv_descriptor_size(const struct anv_descriptor_set_binding_layout
*layout
)
133 if (layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
134 assert(layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
135 return layout
->array_size
;
138 unsigned size
= anv_descriptor_data_size(layout
->data
);
140 /* For multi-planar bindings, we make every descriptor consume the maximum
141 * number of planes so we don't have to bother with walking arrays and
142 * adding things up every time. Fortunately, YCbCr samplers aren't all
143 * that common and likely won't be in the middle of big arrays.
145 if (layout
->max_plane_count
> 1)
146 size
*= layout
->max_plane_count
;
151 /** Returns the size in bytes of each descriptor of the given type
153 * This version of the function does not have access to the entire layout so
154 * it may only work on certain descriptor types where the descriptor size is
155 * entirely determined by the descriptor type. Whenever possible, code should
156 * use anv_descriptor_size() instead.
159 anv_descriptor_type_size(const struct anv_physical_device
*pdevice
,
160 VkDescriptorType type
)
162 assert(type
!= VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
&&
163 type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
164 type
!= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
&&
165 type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
);
167 return anv_descriptor_data_size(anv_descriptor_data_for_type(pdevice
, type
));
171 anv_descriptor_data_supports_bindless(const struct anv_physical_device
*pdevice
,
172 enum anv_descriptor_data data
,
175 if (data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
176 assert(pdevice
->has_a64_buffer_access
);
180 if (data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
181 assert(pdevice
->has_bindless_images
|| pdevice
->has_bindless_samplers
);
182 return sampler
? pdevice
->has_bindless_samplers
:
183 pdevice
->has_bindless_images
;
186 if (data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
187 assert(pdevice
->has_bindless_images
);
195 anv_descriptor_supports_bindless(const struct anv_physical_device
*pdevice
,
196 const struct anv_descriptor_set_binding_layout
*binding
,
199 return anv_descriptor_data_supports_bindless(pdevice
, binding
->data
,
204 anv_descriptor_requires_bindless(const struct anv_physical_device
*pdevice
,
205 const struct anv_descriptor_set_binding_layout
*binding
,
208 if (pdevice
->always_use_bindless
)
209 return anv_descriptor_supports_bindless(pdevice
, binding
, sampler
);
214 void anv_GetDescriptorSetLayoutSupport(
216 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
217 VkDescriptorSetLayoutSupport
* pSupport
)
219 ANV_FROM_HANDLE(anv_device
, device
, _device
);
220 const struct anv_physical_device
*pdevice
=
221 &device
->instance
->physicalDevice
;
223 uint32_t surface_count
[MESA_SHADER_STAGES
] = { 0, };
225 for (uint32_t b
= 0; b
< pCreateInfo
->bindingCount
; b
++) {
226 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[b
];
228 enum anv_descriptor_data desc_data
=
229 anv_descriptor_data_for_type(pdevice
, binding
->descriptorType
);
231 switch (binding
->descriptorType
) {
232 case VK_DESCRIPTOR_TYPE_SAMPLER
:
233 /* There is no real limit on samplers */
236 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
237 if (anv_descriptor_data_supports_bindless(pdevice
, desc_data
, false))
240 if (binding
->pImmutableSamplers
) {
241 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
242 ANV_FROM_HANDLE(anv_sampler
, sampler
,
243 binding
->pImmutableSamplers
[i
]);
244 anv_foreach_stage(s
, binding
->stageFlags
)
245 surface_count
[s
] += sampler
->n_planes
;
248 anv_foreach_stage(s
, binding
->stageFlags
)
249 surface_count
[s
] += binding
->descriptorCount
;
254 if (anv_descriptor_data_supports_bindless(pdevice
, desc_data
, false))
257 anv_foreach_stage(s
, binding
->stageFlags
)
258 surface_count
[s
] += binding
->descriptorCount
;
263 bool supported
= true;
264 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
265 /* Our maximum binding table size is 240 and we need to reserve 8 for
268 if (surface_count
[s
] >= MAX_BINDING_TABLE_SIZE
- MAX_RTS
)
272 pSupport
->supported
= supported
;
275 VkResult
anv_CreateDescriptorSetLayout(
277 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
278 const VkAllocationCallbacks
* pAllocator
,
279 VkDescriptorSetLayout
* pSetLayout
)
281 ANV_FROM_HANDLE(anv_device
, device
, _device
);
283 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
285 uint32_t max_binding
= 0;
286 uint32_t immutable_sampler_count
= 0;
287 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
288 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
290 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
292 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
293 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
294 * pImmutableSamplers can be used to initialize a set of immutable
295 * samplers. [...] If descriptorType is not one of these descriptor
296 * types, then pImmutableSamplers is ignored.
298 * We need to be careful here and only parse pImmutableSamplers if we
299 * have one of the right descriptor types.
301 VkDescriptorType desc_type
= pCreateInfo
->pBindings
[j
].descriptorType
;
302 if ((desc_type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
303 desc_type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
) &&
304 pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
305 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
308 struct anv_descriptor_set_layout
*set_layout
;
309 struct anv_descriptor_set_binding_layout
*bindings
;
310 struct anv_sampler
**samplers
;
312 /* We need to allocate decriptor set layouts off the device allocator
313 * with DEVICE scope because they are reference counted and may not be
314 * destroyed when vkDestroyDescriptorSetLayout is called.
317 anv_multialloc_add(&ma
, &set_layout
, 1);
318 anv_multialloc_add(&ma
, &bindings
, max_binding
+ 1);
319 anv_multialloc_add(&ma
, &samplers
, immutable_sampler_count
);
321 if (!anv_multialloc_alloc(&ma
, &device
->alloc
,
322 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
))
323 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
325 memset(set_layout
, 0, sizeof(*set_layout
));
326 set_layout
->ref_cnt
= 1;
327 set_layout
->binding_count
= max_binding
+ 1;
329 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
330 /* Initialize all binding_layout entries to -1 */
331 memset(&set_layout
->binding
[b
], -1, sizeof(set_layout
->binding
[b
]));
333 set_layout
->binding
[b
].flags
= 0;
334 set_layout
->binding
[b
].data
= 0;
335 set_layout
->binding
[b
].max_plane_count
= 0;
336 set_layout
->binding
[b
].array_size
= 0;
337 set_layout
->binding
[b
].immutable_samplers
= NULL
;
340 /* Initialize all samplers to 0 */
341 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
343 uint32_t buffer_view_count
= 0;
344 uint32_t dynamic_offset_count
= 0;
345 uint32_t descriptor_buffer_size
= 0;
347 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
348 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
349 uint32_t b
= binding
->binding
;
350 /* We temporarily store the pointer to the binding in the
351 * immutable_samplers pointer. This provides us with a quick-and-dirty
352 * way to sort the bindings by binding number.
354 set_layout
->binding
[b
].immutable_samplers
= (void *)binding
;
357 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*binding_flags_info
=
358 vk_find_struct_const(pCreateInfo
->pNext
,
359 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
360 if (binding_flags_info
)
361 assert(binding_flags_info
->bindingCount
== pCreateInfo
->bindingCount
);
363 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
364 const VkDescriptorSetLayoutBinding
*binding
=
365 (void *)set_layout
->binding
[b
].immutable_samplers
;
370 /* We temporarily stashed the pointer to the binding in the
371 * immutable_samplers pointer. Now that we've pulled it back out
372 * again, we reset immutable_samplers to NULL.
374 set_layout
->binding
[b
].immutable_samplers
= NULL
;
376 if (binding
->descriptorCount
== 0)
380 set_layout
->binding
[b
].type
= binding
->descriptorType
;
383 if (binding_flags_info
) {
384 uint32_t binding_strct_idx
= binding
- pCreateInfo
->pBindings
;
385 assert(binding_strct_idx
< binding_flags_info
->bindingCount
);
386 set_layout
->binding
[b
].flags
=
387 binding_flags_info
->pBindingFlags
[binding_strct_idx
];
390 set_layout
->binding
[b
].data
=
391 anv_descriptor_data_for_type(&device
->instance
->physicalDevice
,
392 binding
->descriptorType
);
393 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
394 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
395 set_layout
->size
+= binding
->descriptorCount
;
397 if (set_layout
->binding
[b
].data
& ANV_DESCRIPTOR_BUFFER_VIEW
) {
398 set_layout
->binding
[b
].buffer_view_index
= buffer_view_count
;
399 buffer_view_count
+= binding
->descriptorCount
;
402 switch (binding
->descriptorType
) {
403 case VK_DESCRIPTOR_TYPE_SAMPLER
:
404 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
405 set_layout
->binding
[b
].max_plane_count
= 1;
406 if (binding
->pImmutableSamplers
) {
407 set_layout
->binding
[b
].immutable_samplers
= samplers
;
408 samplers
+= binding
->descriptorCount
;
410 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
411 ANV_FROM_HANDLE(anv_sampler
, sampler
,
412 binding
->pImmutableSamplers
[i
]);
414 set_layout
->binding
[b
].immutable_samplers
[i
] = sampler
;
415 if (set_layout
->binding
[b
].max_plane_count
< sampler
->n_planes
)
416 set_layout
->binding
[b
].max_plane_count
= sampler
->n_planes
;
421 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
422 set_layout
->binding
[b
].max_plane_count
= 1;
429 switch (binding
->descriptorType
) {
430 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
431 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
432 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
433 dynamic_offset_count
+= binding
->descriptorCount
;
440 if (binding
->descriptorType
==
441 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
442 /* Inline uniform blocks are specified to use the descriptor array
443 * size as the size in bytes of the block.
445 descriptor_buffer_size
= align_u32(descriptor_buffer_size
, 32);
446 set_layout
->binding
[b
].descriptor_offset
= descriptor_buffer_size
;
447 descriptor_buffer_size
+= binding
->descriptorCount
;
449 set_layout
->binding
[b
].descriptor_offset
= descriptor_buffer_size
;
450 descriptor_buffer_size
+= anv_descriptor_size(&set_layout
->binding
[b
]) *
451 binding
->descriptorCount
;
454 set_layout
->shader_stages
|= binding
->stageFlags
;
457 set_layout
->buffer_view_count
= buffer_view_count
;
458 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
459 set_layout
->descriptor_buffer_size
= descriptor_buffer_size
;
461 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
466 void anv_DestroyDescriptorSetLayout(
468 VkDescriptorSetLayout _set_layout
,
469 const VkAllocationCallbacks
* pAllocator
)
471 ANV_FROM_HANDLE(anv_device
, device
, _device
);
472 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
477 anv_descriptor_set_layout_unref(device
, set_layout
);
480 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
483 sha1_update_immutable_sampler(struct mesa_sha1
*ctx
,
484 const struct anv_sampler
*sampler
)
486 if (!sampler
->conversion
)
489 /* The only thing that affects the shader is ycbcr conversion */
490 _mesa_sha1_update(ctx
, sampler
->conversion
,
491 sizeof(*sampler
->conversion
));
495 sha1_update_descriptor_set_binding_layout(struct mesa_sha1
*ctx
,
496 const struct anv_descriptor_set_binding_layout
*layout
)
498 SHA1_UPDATE_VALUE(ctx
, layout
->flags
);
499 SHA1_UPDATE_VALUE(ctx
, layout
->data
);
500 SHA1_UPDATE_VALUE(ctx
, layout
->max_plane_count
);
501 SHA1_UPDATE_VALUE(ctx
, layout
->array_size
);
502 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_index
);
503 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_index
);
504 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_index
);
505 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_offset
);
507 if (layout
->immutable_samplers
) {
508 for (uint16_t i
= 0; i
< layout
->array_size
; i
++)
509 sha1_update_immutable_sampler(ctx
, layout
->immutable_samplers
[i
]);
514 sha1_update_descriptor_set_layout(struct mesa_sha1
*ctx
,
515 const struct anv_descriptor_set_layout
*layout
)
517 SHA1_UPDATE_VALUE(ctx
, layout
->binding_count
);
518 SHA1_UPDATE_VALUE(ctx
, layout
->size
);
519 SHA1_UPDATE_VALUE(ctx
, layout
->shader_stages
);
520 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_count
);
521 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_count
);
522 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_buffer_size
);
524 for (uint16_t i
= 0; i
< layout
->binding_count
; i
++)
525 sha1_update_descriptor_set_binding_layout(ctx
, &layout
->binding
[i
]);
529 * Pipeline layouts. These have nothing to do with the pipeline. They are
530 * just multiple descriptor set layouts pasted together
533 VkResult
anv_CreatePipelineLayout(
535 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
536 const VkAllocationCallbacks
* pAllocator
,
537 VkPipelineLayout
* pPipelineLayout
)
539 ANV_FROM_HANDLE(anv_device
, device
, _device
);
540 struct anv_pipeline_layout
*layout
;
542 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
544 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
545 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
547 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
549 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
551 unsigned dynamic_offset_count
= 0;
553 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
554 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
555 pCreateInfo
->pSetLayouts
[set
]);
556 layout
->set
[set
].layout
= set_layout
;
557 anv_descriptor_set_layout_ref(set_layout
);
559 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
560 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
561 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
564 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
568 struct mesa_sha1 ctx
;
569 _mesa_sha1_init(&ctx
);
570 for (unsigned s
= 0; s
< layout
->num_sets
; s
++) {
571 sha1_update_descriptor_set_layout(&ctx
, layout
->set
[s
].layout
);
572 _mesa_sha1_update(&ctx
, &layout
->set
[s
].dynamic_offset_start
,
573 sizeof(layout
->set
[s
].dynamic_offset_start
));
575 _mesa_sha1_update(&ctx
, &layout
->num_sets
, sizeof(layout
->num_sets
));
576 _mesa_sha1_final(&ctx
, layout
->sha1
);
578 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
583 void anv_DestroyPipelineLayout(
585 VkPipelineLayout _pipelineLayout
,
586 const VkAllocationCallbacks
* pAllocator
)
588 ANV_FROM_HANDLE(anv_device
, device
, _device
);
589 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
591 if (!pipeline_layout
)
594 for (uint32_t i
= 0; i
< pipeline_layout
->num_sets
; i
++)
595 anv_descriptor_set_layout_unref(device
, pipeline_layout
->set
[i
].layout
);
597 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
603 * These are implemented using a big pool of memory and a free-list for the
604 * host memory allocations and a state_stream and a free list for the buffer
605 * view surface state. The spec allows us to fail to allocate due to
606 * fragmentation in all cases but two: 1) after pool reset, allocating up
607 * until the pool size with no freeing must succeed and 2) allocating and
608 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
609 * and the free lists lets us recycle blocks for case 2).
612 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
613 * ensure we can allocate the entire BO without hitting zero. The actual
614 * amount doesn't matter.
616 #define POOL_HEAP_OFFSET 64
620 VkResult
anv_CreateDescriptorPool(
622 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
623 const VkAllocationCallbacks
* pAllocator
,
624 VkDescriptorPool
* pDescriptorPool
)
626 ANV_FROM_HANDLE(anv_device
, device
, _device
);
627 struct anv_descriptor_pool
*pool
;
629 const VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*inline_info
=
630 vk_find_struct_const(pCreateInfo
->pNext
,
631 DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
);
633 uint32_t descriptor_count
= 0;
634 uint32_t buffer_view_count
= 0;
635 uint32_t descriptor_bo_size
= 0;
636 for (uint32_t i
= 0; i
< pCreateInfo
->poolSizeCount
; i
++) {
637 enum anv_descriptor_data desc_data
=
638 anv_descriptor_data_for_type(&device
->instance
->physicalDevice
,
639 pCreateInfo
->pPoolSizes
[i
].type
);
641 if (desc_data
& ANV_DESCRIPTOR_BUFFER_VIEW
)
642 buffer_view_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
644 unsigned desc_data_size
= anv_descriptor_data_size(desc_data
) *
645 pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
647 /* Combined image sampler descriptors can take up to 3 slots if they
648 * hold a YCbCr image.
650 if (pCreateInfo
->pPoolSizes
[i
].type
==
651 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
654 if (pCreateInfo
->pPoolSizes
[i
].type
==
655 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
656 /* Inline uniform blocks are specified to use the descriptor array
657 * size as the size in bytes of the block.
660 desc_data_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
663 descriptor_bo_size
+= desc_data_size
;
665 descriptor_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
667 /* We have to align descriptor buffer allocations to 32B so that we can
668 * push descriptor buffers. This means that each descriptor buffer
669 * allocated may burn up to 32B of extra space to get the right alignment.
670 * (Technically, it's at most 28B because we're always going to start at
671 * least 4B aligned but we're being conservative here.) Allocate enough
672 * extra space that we can chop it into maxSets pieces and align each one
675 descriptor_bo_size
+= 32 * pCreateInfo
->maxSets
;
676 descriptor_bo_size
= ALIGN(descriptor_bo_size
, 4096);
677 /* We align inline uniform blocks to 32B */
679 descriptor_bo_size
+= 32 * inline_info
->maxInlineUniformBlockBindings
;
681 const size_t pool_size
=
682 pCreateInfo
->maxSets
* sizeof(struct anv_descriptor_set
) +
683 descriptor_count
* sizeof(struct anv_descriptor
) +
684 buffer_view_count
* sizeof(struct anv_buffer_view
);
685 const size_t total_size
= sizeof(*pool
) + pool_size
;
687 pool
= vk_alloc2(&device
->alloc
, pAllocator
, total_size
, 8,
688 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
690 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
692 pool
->size
= pool_size
;
694 pool
->free_list
= EMPTY
;
696 if (descriptor_bo_size
> 0) {
697 VkResult result
= anv_bo_init_new(&pool
->bo
, device
, descriptor_bo_size
);
698 if (result
!= VK_SUCCESS
) {
699 vk_free2(&device
->alloc
, pAllocator
, pool
);
703 anv_gem_set_caching(device
, pool
->bo
.gem_handle
, I915_CACHING_CACHED
);
705 pool
->bo
.map
= anv_gem_mmap(device
, pool
->bo
.gem_handle
, 0,
706 descriptor_bo_size
, 0);
707 if (pool
->bo
.map
== NULL
) {
708 anv_gem_close(device
, pool
->bo
.gem_handle
);
709 vk_free2(&device
->alloc
, pAllocator
, pool
);
710 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
713 if (device
->instance
->physicalDevice
.use_softpin
) {
714 pool
->bo
.flags
|= EXEC_OBJECT_PINNED
;
715 anv_vma_alloc(device
, &pool
->bo
);
718 util_vma_heap_init(&pool
->bo_heap
, POOL_HEAP_OFFSET
, descriptor_bo_size
);
723 anv_state_stream_init(&pool
->surface_state_stream
,
724 &device
->surface_state_pool
, 4096);
725 pool
->surface_state_free_list
= NULL
;
727 list_inithead(&pool
->desc_sets
);
729 *pDescriptorPool
= anv_descriptor_pool_to_handle(pool
);
734 void anv_DestroyDescriptorPool(
736 VkDescriptorPool _pool
,
737 const VkAllocationCallbacks
* pAllocator
)
739 ANV_FROM_HANDLE(anv_device
, device
, _device
);
740 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, _pool
);
746 anv_gem_munmap(pool
->bo
.map
, pool
->bo
.size
);
747 anv_vma_free(device
, &pool
->bo
);
748 anv_gem_close(device
, pool
->bo
.gem_handle
);
750 anv_state_stream_finish(&pool
->surface_state_stream
);
752 list_for_each_entry_safe(struct anv_descriptor_set
, set
,
753 &pool
->desc_sets
, pool_link
) {
754 anv_descriptor_set_destroy(device
, pool
, set
);
757 util_vma_heap_finish(&pool
->bo_heap
);
759 vk_free2(&device
->alloc
, pAllocator
, pool
);
762 VkResult
anv_ResetDescriptorPool(
764 VkDescriptorPool descriptorPool
,
765 VkDescriptorPoolResetFlags flags
)
767 ANV_FROM_HANDLE(anv_device
, device
, _device
);
768 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
770 list_for_each_entry_safe(struct anv_descriptor_set
, set
,
771 &pool
->desc_sets
, pool_link
) {
772 anv_descriptor_set_destroy(device
, pool
, set
);
776 pool
->free_list
= EMPTY
;
779 util_vma_heap_finish(&pool
->bo_heap
);
780 util_vma_heap_init(&pool
->bo_heap
, POOL_HEAP_OFFSET
, pool
->bo
.size
);
783 anv_state_stream_finish(&pool
->surface_state_stream
);
784 anv_state_stream_init(&pool
->surface_state_stream
,
785 &device
->surface_state_pool
, 4096);
786 pool
->surface_state_free_list
= NULL
;
791 struct pool_free_list_entry
{
797 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool
*pool
,
799 struct anv_descriptor_set
**set
)
801 if (size
<= pool
->size
- pool
->next
) {
802 *set
= (struct anv_descriptor_set
*) (pool
->data
+ pool
->next
);
806 struct pool_free_list_entry
*entry
;
807 uint32_t *link
= &pool
->free_list
;
808 for (uint32_t f
= pool
->free_list
; f
!= EMPTY
; f
= entry
->next
) {
809 entry
= (struct pool_free_list_entry
*) (pool
->data
+ f
);
810 if (size
<= entry
->size
) {
812 *set
= (struct anv_descriptor_set
*) entry
;
818 if (pool
->free_list
!= EMPTY
) {
819 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
821 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY
);
827 anv_descriptor_pool_free_set(struct anv_descriptor_pool
*pool
,
828 struct anv_descriptor_set
*set
)
830 /* Put the descriptor set allocation back on the free list. */
831 const uint32_t index
= (char *) set
- pool
->data
;
832 if (index
+ set
->size
== pool
->next
) {
835 struct pool_free_list_entry
*entry
= (struct pool_free_list_entry
*) set
;
836 entry
->next
= pool
->free_list
;
837 entry
->size
= set
->size
;
838 pool
->free_list
= (char *) entry
- pool
->data
;
841 list_del(&set
->pool_link
);
844 struct surface_state_free_list_entry
{
846 struct anv_state state
;
849 static struct anv_state
850 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool
*pool
)
852 struct surface_state_free_list_entry
*entry
=
853 pool
->surface_state_free_list
;
856 struct anv_state state
= entry
->state
;
857 pool
->surface_state_free_list
= entry
->next
;
858 assert(state
.alloc_size
== 64);
861 return anv_state_stream_alloc(&pool
->surface_state_stream
, 64, 64);
866 anv_descriptor_pool_free_state(struct anv_descriptor_pool
*pool
,
867 struct anv_state state
)
869 /* Put the buffer view surface state back on the free list. */
870 struct surface_state_free_list_entry
*entry
= state
.map
;
871 entry
->next
= pool
->surface_state_free_list
;
872 entry
->state
= state
;
873 pool
->surface_state_free_list
= entry
;
877 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout
*layout
)
880 sizeof(struct anv_descriptor_set
) +
881 layout
->size
* sizeof(struct anv_descriptor
) +
882 layout
->buffer_view_count
* sizeof(struct anv_buffer_view
);
886 anv_descriptor_set_create(struct anv_device
*device
,
887 struct anv_descriptor_pool
*pool
,
888 struct anv_descriptor_set_layout
*layout
,
889 struct anv_descriptor_set
**out_set
)
891 struct anv_descriptor_set
*set
;
892 const size_t size
= anv_descriptor_set_layout_size(layout
);
894 VkResult result
= anv_descriptor_pool_alloc_set(pool
, size
, &set
);
895 if (result
!= VK_SUCCESS
)
898 if (layout
->descriptor_buffer_size
) {
899 /* Align the size to 32 so that alignment gaps don't cause extra holes
900 * in the heap which can lead to bad performance.
902 uint64_t pool_vma_offset
=
903 util_vma_heap_alloc(&pool
->bo_heap
,
904 ALIGN(layout
->descriptor_buffer_size
, 32), 32);
905 if (pool_vma_offset
== 0) {
906 anv_descriptor_pool_free_set(pool
, set
);
907 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
909 assert(pool_vma_offset
>= POOL_HEAP_OFFSET
&&
910 pool_vma_offset
- POOL_HEAP_OFFSET
<= INT32_MAX
);
911 set
->desc_mem
.offset
= pool_vma_offset
- POOL_HEAP_OFFSET
;
912 set
->desc_mem
.alloc_size
= layout
->descriptor_buffer_size
;
913 set
->desc_mem
.map
= pool
->bo
.map
+ set
->desc_mem
.offset
;
915 set
->desc_surface_state
= anv_descriptor_pool_alloc_state(pool
);
916 anv_fill_buffer_surface_state(device
, set
->desc_surface_state
,
917 ISL_FORMAT_R32G32B32A32_FLOAT
,
918 (struct anv_address
) {
920 .offset
= set
->desc_mem
.offset
,
922 layout
->descriptor_buffer_size
, 1);
924 set
->desc_mem
= ANV_STATE_NULL
;
925 set
->desc_surface_state
= ANV_STATE_NULL
;
929 set
->layout
= layout
;
930 anv_descriptor_set_layout_ref(layout
);
934 (struct anv_buffer_view
*) &set
->descriptors
[layout
->size
];
935 set
->buffer_view_count
= layout
->buffer_view_count
;
937 /* By defining the descriptors to be zero now, we can later verify that
938 * a descriptor has not been populated with user data.
940 memset(set
->descriptors
, 0, sizeof(struct anv_descriptor
) * layout
->size
);
942 /* Go through and fill out immutable samplers if we have any */
943 struct anv_descriptor
*desc
= set
->descriptors
;
944 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
945 if (layout
->binding
[b
].immutable_samplers
) {
946 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++) {
947 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
948 * UpdateDescriptorSets if needed. However, if the descriptor
949 * set has an immutable sampler, UpdateDescriptorSets may never
950 * touch it, so we need to make sure it's 100% valid now.
952 * We don't need to actually provide a sampler because the helper
953 * will always write in the immutable sampler regardless of what
954 * is in the sampler parameter.
956 struct VkDescriptorImageInfo info
= { };
957 anv_descriptor_set_write_image_view(device
, set
, &info
,
958 VK_DESCRIPTOR_TYPE_SAMPLER
,
962 desc
+= layout
->binding
[b
].array_size
;
965 /* Allocate surface state for the buffer views. */
966 for (uint32_t b
= 0; b
< layout
->buffer_view_count
; b
++) {
967 set
->buffer_views
[b
].surface_state
=
968 anv_descriptor_pool_alloc_state(pool
);
977 anv_descriptor_set_destroy(struct anv_device
*device
,
978 struct anv_descriptor_pool
*pool
,
979 struct anv_descriptor_set
*set
)
981 anv_descriptor_set_layout_unref(device
, set
->layout
);
983 if (set
->desc_mem
.alloc_size
) {
984 util_vma_heap_free(&pool
->bo_heap
,
985 (uint64_t)set
->desc_mem
.offset
+ POOL_HEAP_OFFSET
,
986 set
->desc_mem
.alloc_size
);
987 anv_descriptor_pool_free_state(pool
, set
->desc_surface_state
);
990 for (uint32_t b
= 0; b
< set
->buffer_view_count
; b
++)
991 anv_descriptor_pool_free_state(pool
, set
->buffer_views
[b
].surface_state
);
993 anv_descriptor_pool_free_set(pool
, set
);
996 VkResult
anv_AllocateDescriptorSets(
998 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
999 VkDescriptorSet
* pDescriptorSets
)
1001 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1002 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
1004 VkResult result
= VK_SUCCESS
;
1005 struct anv_descriptor_set
*set
;
1008 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
1009 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
1010 pAllocateInfo
->pSetLayouts
[i
]);
1012 result
= anv_descriptor_set_create(device
, pool
, layout
, &set
);
1013 if (result
!= VK_SUCCESS
)
1016 list_addtail(&set
->pool_link
, &pool
->desc_sets
);
1018 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
1021 if (result
!= VK_SUCCESS
)
1022 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
1023 i
, pDescriptorSets
);
1028 VkResult
anv_FreeDescriptorSets(
1030 VkDescriptorPool descriptorPool
,
1032 const VkDescriptorSet
* pDescriptorSets
)
1034 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1035 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
1037 for (uint32_t i
= 0; i
< count
; i
++) {
1038 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
1043 anv_descriptor_set_destroy(device
, pool
, set
);
1050 anv_descriptor_set_write_image_param(uint32_t *param_desc_map
,
1051 const struct brw_image_param
*param
)
1053 #define WRITE_PARAM_FIELD(field, FIELD) \
1054 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
1055 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
1057 WRITE_PARAM_FIELD(offset
, OFFSET
);
1058 WRITE_PARAM_FIELD(size
, SIZE
);
1059 WRITE_PARAM_FIELD(stride
, STRIDE
);
1060 WRITE_PARAM_FIELD(tiling
, TILING
);
1061 WRITE_PARAM_FIELD(swizzling
, SWIZZLING
);
1062 WRITE_PARAM_FIELD(size
, SIZE
);
1064 #undef WRITE_PARAM_FIELD
1068 anv_surface_state_to_handle(struct anv_state state
)
1070 /* Bits 31:12 of the bindless surface offset in the extended message
1071 * descriptor is bits 25:6 of the byte-based address.
1073 assert(state
.offset
>= 0);
1074 uint32_t offset
= state
.offset
;
1075 assert((offset
& 0x3f) == 0 && offset
< (1 << 26));
1080 anv_descriptor_set_write_image_view(struct anv_device
*device
,
1081 struct anv_descriptor_set
*set
,
1082 const VkDescriptorImageInfo
* const info
,
1083 VkDescriptorType type
,
1087 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1088 &set
->layout
->binding
[binding
];
1089 struct anv_descriptor
*desc
=
1090 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1091 struct anv_image_view
*image_view
= NULL
;
1092 struct anv_sampler
*sampler
= NULL
;
1094 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1095 * set initialization to set the bindless samplers.
1097 assert(type
== bind_layout
->type
||
1098 type
== VK_DESCRIPTOR_TYPE_SAMPLER
);
1101 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1102 sampler
= anv_sampler_from_handle(info
->sampler
);
1105 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1106 image_view
= anv_image_view_from_handle(info
->imageView
);
1107 sampler
= anv_sampler_from_handle(info
->sampler
);
1110 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1111 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1112 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1113 image_view
= anv_image_view_from_handle(info
->imageView
);
1117 unreachable("invalid descriptor type");
1120 /* If this descriptor has an immutable sampler, we don't want to stomp on
1123 sampler
= bind_layout
->immutable_samplers
?
1124 bind_layout
->immutable_samplers
[element
] :
1127 *desc
= (struct anv_descriptor
) {
1129 .layout
= info
->imageLayout
,
1130 .image_view
= image_view
,
1134 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1135 element
* anv_descriptor_size(bind_layout
);
1137 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1138 struct anv_sampled_image_descriptor desc_data
[3];
1139 memset(desc_data
, 0, sizeof(desc_data
));
1142 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1143 struct anv_surface_state sstate
=
1144 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
1145 image_view
->planes
[p
].general_sampler_surface_state
:
1146 image_view
->planes
[p
].optimal_sampler_surface_state
;
1147 desc_data
[p
].image
= anv_surface_state_to_handle(sstate
.state
);
1152 for (unsigned p
= 0; p
< sampler
->n_planes
; p
++)
1153 desc_data
[p
].sampler
= sampler
->bindless_state
.offset
+ p
* 32;
1156 /* We may have max_plane_count < 0 if this isn't a sampled image but it
1157 * can be no more than the size of our array of handles.
1159 assert(bind_layout
->max_plane_count
<= ARRAY_SIZE(desc_data
));
1160 memcpy(desc_map
, desc_data
,
1161 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1164 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1165 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1166 assert(image_view
->n_planes
== 1);
1167 struct anv_storage_image_descriptor desc_data
= {
1168 .read_write
= anv_surface_state_to_handle(
1169 image_view
->planes
[0].storage_surface_state
.state
),
1170 .write_only
= anv_surface_state_to_handle(
1171 image_view
->planes
[0].writeonly_storage_surface_state
.state
),
1173 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1176 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1177 /* Storage images can only ever have one plane */
1178 assert(image_view
->n_planes
== 1);
1179 const struct brw_image_param
*image_param
=
1180 &image_view
->planes
[0].storage_image_param
;
1182 anv_descriptor_set_write_image_param(desc_map
, image_param
);
1187 anv_descriptor_set_write_buffer_view(struct anv_device
*device
,
1188 struct anv_descriptor_set
*set
,
1189 VkDescriptorType type
,
1190 struct anv_buffer_view
*buffer_view
,
1194 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1195 &set
->layout
->binding
[binding
];
1196 struct anv_descriptor
*desc
=
1197 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1199 assert(type
== bind_layout
->type
);
1201 *desc
= (struct anv_descriptor
) {
1203 .buffer_view
= buffer_view
,
1206 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1207 element
* anv_descriptor_size(bind_layout
);
1209 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1210 struct anv_sampled_image_descriptor desc_data
= {
1211 .image
= anv_surface_state_to_handle(buffer_view
->surface_state
),
1213 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1216 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1217 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1218 struct anv_storage_image_descriptor desc_data
= {
1219 .read_write
= anv_surface_state_to_handle(
1220 buffer_view
->storage_surface_state
),
1221 .write_only
= anv_surface_state_to_handle(
1222 buffer_view
->writeonly_storage_surface_state
),
1224 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1227 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1228 anv_descriptor_set_write_image_param(desc_map
,
1229 &buffer_view
->storage_image_param
);
1234 anv_descriptor_set_write_buffer(struct anv_device
*device
,
1235 struct anv_descriptor_set
*set
,
1236 struct anv_state_stream
*alloc_stream
,
1237 VkDescriptorType type
,
1238 struct anv_buffer
*buffer
,
1241 VkDeviceSize offset
,
1244 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1245 &set
->layout
->binding
[binding
];
1246 struct anv_descriptor
*desc
=
1247 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1249 assert(type
== bind_layout
->type
);
1251 struct anv_address bind_addr
= anv_address_add(buffer
->address
, offset
);
1252 uint64_t bind_range
= anv_buffer_get_range(buffer
, offset
, range
);
1254 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1255 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1256 *desc
= (struct anv_descriptor
) {
1263 assert(bind_layout
->data
& ANV_DESCRIPTOR_BUFFER_VIEW
);
1264 struct anv_buffer_view
*bview
=
1265 &set
->buffer_views
[bind_layout
->buffer_view_index
+ element
];
1267 bview
->format
= anv_isl_format_for_descriptor_type(type
);
1268 bview
->range
= bind_range
;
1269 bview
->address
= bind_addr
;
1271 /* If we're writing descriptors through a push command, we need to
1272 * allocate the surface state from the command buffer. Otherwise it will
1273 * be allocated by the descriptor pool when calling
1274 * vkAllocateDescriptorSets. */
1276 bview
->surface_state
= anv_state_stream_alloc(alloc_stream
, 64, 64);
1278 anv_fill_buffer_surface_state(device
, bview
->surface_state
,
1279 bview
->format
, bind_addr
, bind_range
, 1);
1281 *desc
= (struct anv_descriptor
) {
1283 .buffer_view
= bview
,
1287 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1288 element
* anv_descriptor_size(bind_layout
);
1290 if (bind_layout
->data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
1291 struct anv_address_range_descriptor desc
= {
1292 .address
= anv_address_physical(bind_addr
),
1293 .range
= bind_range
,
1295 memcpy(desc_map
, &desc
, sizeof(desc
));
1300 anv_descriptor_set_write_inline_uniform_data(struct anv_device
*device
,
1301 struct anv_descriptor_set
*set
,
1307 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1308 &set
->layout
->binding
[binding
];
1310 assert(bind_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
);
1312 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
;
1314 memcpy(desc_map
+ offset
, data
, size
);
1317 void anv_UpdateDescriptorSets(
1319 uint32_t descriptorWriteCount
,
1320 const VkWriteDescriptorSet
* pDescriptorWrites
,
1321 uint32_t descriptorCopyCount
,
1322 const VkCopyDescriptorSet
* pDescriptorCopies
)
1324 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1326 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1327 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1328 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
1330 switch (write
->descriptorType
) {
1331 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1332 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1333 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1334 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1335 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1336 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1337 anv_descriptor_set_write_image_view(device
, set
,
1338 write
->pImageInfo
+ j
,
1339 write
->descriptorType
,
1341 write
->dstArrayElement
+ j
);
1345 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1346 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1347 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1348 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1349 write
->pTexelBufferView
[j
]);
1351 anv_descriptor_set_write_buffer_view(device
, set
,
1352 write
->descriptorType
,
1355 write
->dstArrayElement
+ j
);
1359 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1360 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1361 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1362 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1363 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1364 assert(write
->pBufferInfo
[j
].buffer
);
1365 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1368 anv_descriptor_set_write_buffer(device
, set
,
1370 write
->descriptorType
,
1373 write
->dstArrayElement
+ j
,
1374 write
->pBufferInfo
[j
].offset
,
1375 write
->pBufferInfo
[j
].range
);
1379 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
: {
1380 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_write
=
1381 vk_find_struct_const(write
->pNext
,
1382 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
1383 assert(inline_write
->dataSize
== write
->descriptorCount
);
1384 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1386 inline_write
->pData
,
1387 write
->dstArrayElement
,
1388 inline_write
->dataSize
);
1397 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
1398 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
1399 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->srcSet
);
1400 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
1402 const struct anv_descriptor_set_binding_layout
*src_layout
=
1403 &src
->layout
->binding
[copy
->srcBinding
];
1404 struct anv_descriptor
*src_desc
=
1405 &src
->descriptors
[src_layout
->descriptor_index
];
1406 src_desc
+= copy
->srcArrayElement
;
1408 const struct anv_descriptor_set_binding_layout
*dst_layout
=
1409 &dst
->layout
->binding
[copy
->dstBinding
];
1410 struct anv_descriptor
*dst_desc
=
1411 &dst
->descriptors
[dst_layout
->descriptor_index
];
1412 dst_desc
+= copy
->dstArrayElement
;
1414 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
1415 dst_desc
[j
] = src_desc
[j
];
1417 if (src_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
1418 assert(src_layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
1419 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1420 copy
->dstArrayElement
,
1421 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1422 copy
->srcArrayElement
,
1423 copy
->descriptorCount
);
1425 unsigned desc_size
= anv_descriptor_size(src_layout
);
1426 if (desc_size
> 0) {
1427 assert(desc_size
== anv_descriptor_size(dst_layout
));
1428 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1429 copy
->dstArrayElement
* desc_size
,
1430 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1431 copy
->srcArrayElement
* desc_size
,
1432 copy
->descriptorCount
* desc_size
);
1439 * Descriptor update templates.
1443 anv_descriptor_set_write_template(struct anv_device
*device
,
1444 struct anv_descriptor_set
*set
,
1445 struct anv_state_stream
*alloc_stream
,
1446 const struct anv_descriptor_update_template
*template,
1449 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1450 const struct anv_descriptor_template_entry
*entry
=
1451 &template->entries
[i
];
1453 switch (entry
->type
) {
1454 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1455 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1456 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1457 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1458 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1459 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1460 const VkDescriptorImageInfo
*info
=
1461 data
+ entry
->offset
+ j
* entry
->stride
;
1462 anv_descriptor_set_write_image_view(device
, set
,
1465 entry
->array_element
+ j
);
1469 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1470 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1471 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1472 const VkBufferView
*_bview
=
1473 data
+ entry
->offset
+ j
* entry
->stride
;
1474 ANV_FROM_HANDLE(anv_buffer_view
, bview
, *_bview
);
1476 anv_descriptor_set_write_buffer_view(device
, set
,
1480 entry
->array_element
+ j
);
1484 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1485 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1486 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1487 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1488 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1489 const VkDescriptorBufferInfo
*info
=
1490 data
+ entry
->offset
+ j
* entry
->stride
;
1491 ANV_FROM_HANDLE(anv_buffer
, buffer
, info
->buffer
);
1493 anv_descriptor_set_write_buffer(device
, set
,
1498 entry
->array_element
+ j
,
1499 info
->offset
, info
->range
);
1503 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
1504 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1506 data
+ entry
->offset
,
1507 entry
->array_element
,
1508 entry
->array_count
);
1517 VkResult
anv_CreateDescriptorUpdateTemplate(
1519 const VkDescriptorUpdateTemplateCreateInfo
* pCreateInfo
,
1520 const VkAllocationCallbacks
* pAllocator
,
1521 VkDescriptorUpdateTemplate
* pDescriptorUpdateTemplate
)
1523 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1524 struct anv_descriptor_update_template
*template;
1526 size_t size
= sizeof(*template) +
1527 pCreateInfo
->descriptorUpdateEntryCount
* sizeof(template->entries
[0]);
1528 template = vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1529 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1530 if (template == NULL
)
1531 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1533 template->bind_point
= pCreateInfo
->pipelineBindPoint
;
1535 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
)
1536 template->set
= pCreateInfo
->set
;
1538 template->entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1539 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1540 const VkDescriptorUpdateTemplateEntry
*pEntry
=
1541 &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1543 template->entries
[i
] = (struct anv_descriptor_template_entry
) {
1544 .type
= pEntry
->descriptorType
,
1545 .binding
= pEntry
->dstBinding
,
1546 .array_element
= pEntry
->dstArrayElement
,
1547 .array_count
= pEntry
->descriptorCount
,
1548 .offset
= pEntry
->offset
,
1549 .stride
= pEntry
->stride
,
1553 *pDescriptorUpdateTemplate
=
1554 anv_descriptor_update_template_to_handle(template);
1559 void anv_DestroyDescriptorUpdateTemplate(
1561 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1562 const VkAllocationCallbacks
* pAllocator
)
1564 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1565 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1566 descriptorUpdateTemplate
);
1568 vk_free2(&device
->alloc
, pAllocator
, template);
1571 void anv_UpdateDescriptorSetWithTemplate(
1573 VkDescriptorSet descriptorSet
,
1574 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1577 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1578 ANV_FROM_HANDLE(anv_descriptor_set
, set
, descriptorSet
);
1579 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1580 descriptorUpdateTemplate
);
1582 anv_descriptor_set_write_template(device
, set
, NULL
, template, pData
);