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
;
106 /* On Ivy Bridge and Bay Trail, we need swizzles textures in the shader
107 * Do not handle VK_DESCRIPTOR_TYPE_STORAGE_IMAGE and
108 * VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT because they already must
109 * have identity swizzle.
111 if (device
->info
.gen
== 7 && !device
->info
.is_haswell
&&
112 (type
== VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
||
113 type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
))
114 data
|= ANV_DESCRIPTOR_TEXTURE_SWIZZLE
;
120 anv_descriptor_data_size(enum anv_descriptor_data data
)
124 if (data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
)
125 size
+= sizeof(struct anv_sampled_image_descriptor
);
127 if (data
& ANV_DESCRIPTOR_STORAGE_IMAGE
)
128 size
+= sizeof(struct anv_storage_image_descriptor
);
130 if (data
& ANV_DESCRIPTOR_IMAGE_PARAM
)
131 size
+= BRW_IMAGE_PARAM_SIZE
* 4;
133 if (data
& ANV_DESCRIPTOR_ADDRESS_RANGE
)
134 size
+= sizeof(struct anv_address_range_descriptor
);
136 if (data
& ANV_DESCRIPTOR_TEXTURE_SWIZZLE
)
137 size
+= sizeof(struct anv_texture_swizzle_descriptor
);
143 anv_needs_descriptor_buffer(VkDescriptorType desc_type
,
144 enum anv_descriptor_data desc_data
)
146 if (desc_type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
||
147 anv_descriptor_data_size(desc_data
) > 0)
152 /** Returns the size in bytes of each descriptor with the given layout */
154 anv_descriptor_size(const struct anv_descriptor_set_binding_layout
*layout
)
156 if (layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
157 assert(layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
158 return layout
->array_size
;
161 unsigned size
= anv_descriptor_data_size(layout
->data
);
163 /* For multi-planar bindings, we make every descriptor consume the maximum
164 * number of planes so we don't have to bother with walking arrays and
165 * adding things up every time. Fortunately, YCbCr samplers aren't all
166 * that common and likely won't be in the middle of big arrays.
168 if (layout
->max_plane_count
> 1)
169 size
*= layout
->max_plane_count
;
174 /** Returns the size in bytes of each descriptor of the given type
176 * This version of the function does not have access to the entire layout so
177 * it may only work on certain descriptor types where the descriptor size is
178 * entirely determined by the descriptor type. Whenever possible, code should
179 * use anv_descriptor_size() instead.
182 anv_descriptor_type_size(const struct anv_physical_device
*pdevice
,
183 VkDescriptorType type
)
185 assert(type
!= VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
&&
186 type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
187 type
!= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
&&
188 type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
);
190 return anv_descriptor_data_size(anv_descriptor_data_for_type(pdevice
, type
));
194 anv_descriptor_data_supports_bindless(const struct anv_physical_device
*pdevice
,
195 enum anv_descriptor_data data
,
198 if (data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
199 assert(pdevice
->has_a64_buffer_access
);
203 if (data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
204 assert(pdevice
->has_bindless_images
|| pdevice
->has_bindless_samplers
);
205 return sampler
? pdevice
->has_bindless_samplers
:
206 pdevice
->has_bindless_images
;
209 if (data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
210 assert(pdevice
->has_bindless_images
);
218 anv_descriptor_supports_bindless(const struct anv_physical_device
*pdevice
,
219 const struct anv_descriptor_set_binding_layout
*binding
,
222 return anv_descriptor_data_supports_bindless(pdevice
, binding
->data
,
227 anv_descriptor_requires_bindless(const struct anv_physical_device
*pdevice
,
228 const struct anv_descriptor_set_binding_layout
*binding
,
231 if (pdevice
->always_use_bindless
)
232 return anv_descriptor_supports_bindless(pdevice
, binding
, sampler
);
234 static const VkDescriptorBindingFlagBitsEXT flags_requiring_bindless
=
235 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT
|
236 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT
|
237 VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT
;
239 return (binding
->flags
& flags_requiring_bindless
) != 0;
242 void anv_GetDescriptorSetLayoutSupport(
244 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
245 VkDescriptorSetLayoutSupport
* pSupport
)
247 ANV_FROM_HANDLE(anv_device
, device
, _device
);
248 const struct anv_physical_device
*pdevice
= device
->physical
;
250 uint32_t surface_count
[MESA_SHADER_STAGES
] = { 0, };
251 bool needs_descriptor_buffer
= false;
253 for (uint32_t b
= 0; b
< pCreateInfo
->bindingCount
; b
++) {
254 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[b
];
256 enum anv_descriptor_data desc_data
=
257 anv_descriptor_data_for_type(pdevice
, binding
->descriptorType
);
259 if (anv_needs_descriptor_buffer(binding
->descriptorType
, desc_data
))
260 needs_descriptor_buffer
= true;
262 switch (binding
->descriptorType
) {
263 case VK_DESCRIPTOR_TYPE_SAMPLER
:
264 /* There is no real limit on samplers */
267 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
268 /* Inline uniforms don't use a binding */
271 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
272 if (anv_descriptor_data_supports_bindless(pdevice
, desc_data
, false))
275 if (binding
->pImmutableSamplers
) {
276 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
277 ANV_FROM_HANDLE(anv_sampler
, sampler
,
278 binding
->pImmutableSamplers
[i
]);
279 anv_foreach_stage(s
, binding
->stageFlags
)
280 surface_count
[s
] += sampler
->n_planes
;
283 anv_foreach_stage(s
, binding
->stageFlags
)
284 surface_count
[s
] += binding
->descriptorCount
;
289 if (anv_descriptor_data_supports_bindless(pdevice
, desc_data
, false))
292 anv_foreach_stage(s
, binding
->stageFlags
)
293 surface_count
[s
] += binding
->descriptorCount
;
298 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
299 if (needs_descriptor_buffer
)
300 surface_count
[s
] += 1;
303 bool supported
= true;
304 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
305 /* Our maximum binding table size is 240 and we need to reserve 8 for
308 if (surface_count
[s
] > MAX_BINDING_TABLE_SIZE
- MAX_RTS
)
312 pSupport
->supported
= supported
;
315 VkResult
anv_CreateDescriptorSetLayout(
317 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
318 const VkAllocationCallbacks
* pAllocator
,
319 VkDescriptorSetLayout
* pSetLayout
)
321 ANV_FROM_HANDLE(anv_device
, device
, _device
);
323 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
325 uint32_t max_binding
= 0;
326 uint32_t immutable_sampler_count
= 0;
327 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
328 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
330 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
332 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
333 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
334 * pImmutableSamplers can be used to initialize a set of immutable
335 * samplers. [...] If descriptorType is not one of these descriptor
336 * types, then pImmutableSamplers is ignored.
338 * We need to be careful here and only parse pImmutableSamplers if we
339 * have one of the right descriptor types.
341 VkDescriptorType desc_type
= pCreateInfo
->pBindings
[j
].descriptorType
;
342 if ((desc_type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
343 desc_type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
) &&
344 pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
345 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
348 struct anv_descriptor_set_layout
*set_layout
;
349 struct anv_descriptor_set_binding_layout
*bindings
;
350 struct anv_sampler
**samplers
;
352 /* We need to allocate decriptor set layouts off the device allocator
353 * with DEVICE scope because they are reference counted and may not be
354 * destroyed when vkDestroyDescriptorSetLayout is called.
357 anv_multialloc_add(&ma
, &set_layout
, 1);
358 anv_multialloc_add(&ma
, &bindings
, max_binding
+ 1);
359 anv_multialloc_add(&ma
, &samplers
, immutable_sampler_count
);
361 if (!anv_multialloc_alloc(&ma
, &device
->vk
.alloc
,
362 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
))
363 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
365 memset(set_layout
, 0, sizeof(*set_layout
));
366 vk_object_base_init(&device
->vk
, &set_layout
->base
,
367 VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT
);
368 set_layout
->ref_cnt
= 1;
369 set_layout
->binding_count
= max_binding
+ 1;
371 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
372 /* Initialize all binding_layout entries to -1 */
373 memset(&set_layout
->binding
[b
], -1, sizeof(set_layout
->binding
[b
]));
375 set_layout
->binding
[b
].flags
= 0;
376 set_layout
->binding
[b
].data
= 0;
377 set_layout
->binding
[b
].max_plane_count
= 0;
378 set_layout
->binding
[b
].array_size
= 0;
379 set_layout
->binding
[b
].immutable_samplers
= NULL
;
382 /* Initialize all samplers to 0 */
383 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
385 uint32_t buffer_view_count
= 0;
386 uint32_t dynamic_offset_count
= 0;
387 uint32_t descriptor_buffer_size
= 0;
389 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
390 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
391 uint32_t b
= binding
->binding
;
392 /* We temporarily store pCreateInfo->pBindings[] index (plus one) in the
393 * immutable_samplers pointer. This provides us with a quick-and-dirty
394 * way to sort the bindings by binding number.
396 set_layout
->binding
[b
].immutable_samplers
= (void *)(uintptr_t)(j
+ 1);
399 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*binding_flags_info
=
400 vk_find_struct_const(pCreateInfo
->pNext
,
401 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
403 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
404 /* We stashed the pCreateInfo->pBindings[] index (plus one) in the
405 * immutable_samplers pointer. Check for NULL (empty binding) and then
406 * reset it and compute the index.
408 if (set_layout
->binding
[b
].immutable_samplers
== NULL
)
410 const uint32_t info_idx
=
411 (uintptr_t)(void *)set_layout
->binding
[b
].immutable_samplers
- 1;
412 set_layout
->binding
[b
].immutable_samplers
= NULL
;
414 const VkDescriptorSetLayoutBinding
*binding
=
415 &pCreateInfo
->pBindings
[info_idx
];
417 if (binding
->descriptorCount
== 0)
421 set_layout
->binding
[b
].type
= binding
->descriptorType
;
424 if (binding_flags_info
&& binding_flags_info
->bindingCount
> 0) {
425 assert(binding_flags_info
->bindingCount
== pCreateInfo
->bindingCount
);
426 set_layout
->binding
[b
].flags
=
427 binding_flags_info
->pBindingFlags
[info_idx
];
430 set_layout
->binding
[b
].data
=
431 anv_descriptor_data_for_type(device
->physical
,
432 binding
->descriptorType
);
433 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
434 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
435 set_layout
->size
+= binding
->descriptorCount
;
437 if (set_layout
->binding
[b
].data
& ANV_DESCRIPTOR_BUFFER_VIEW
) {
438 set_layout
->binding
[b
].buffer_view_index
= buffer_view_count
;
439 buffer_view_count
+= binding
->descriptorCount
;
442 switch (binding
->descriptorType
) {
443 case VK_DESCRIPTOR_TYPE_SAMPLER
:
444 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
445 set_layout
->binding
[b
].max_plane_count
= 1;
446 if (binding
->pImmutableSamplers
) {
447 set_layout
->binding
[b
].immutable_samplers
= samplers
;
448 samplers
+= binding
->descriptorCount
;
450 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
451 ANV_FROM_HANDLE(anv_sampler
, sampler
,
452 binding
->pImmutableSamplers
[i
]);
454 set_layout
->binding
[b
].immutable_samplers
[i
] = sampler
;
455 if (set_layout
->binding
[b
].max_plane_count
< sampler
->n_planes
)
456 set_layout
->binding
[b
].max_plane_count
= sampler
->n_planes
;
461 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
462 set_layout
->binding
[b
].max_plane_count
= 1;
469 switch (binding
->descriptorType
) {
470 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
471 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
472 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
473 anv_foreach_stage(s
, binding
->stageFlags
) {
474 STATIC_ASSERT(MAX_DYNAMIC_BUFFERS
<=
475 sizeof(set_layout
->stage_dynamic_offsets
[s
]) * 8);
476 set_layout
->stage_dynamic_offsets
[s
] |=
477 BITFIELD_RANGE(set_layout
->binding
[b
].dynamic_offset_index
,
478 binding
->descriptorCount
);
480 dynamic_offset_count
+= binding
->descriptorCount
;
481 assert(dynamic_offset_count
< MAX_DYNAMIC_BUFFERS
);
488 if (binding
->descriptorType
==
489 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
490 /* Inline uniform blocks are specified to use the descriptor array
491 * size as the size in bytes of the block.
493 descriptor_buffer_size
= align_u32(descriptor_buffer_size
, 32);
494 set_layout
->binding
[b
].descriptor_offset
= descriptor_buffer_size
;
495 descriptor_buffer_size
+= binding
->descriptorCount
;
497 set_layout
->binding
[b
].descriptor_offset
= descriptor_buffer_size
;
498 descriptor_buffer_size
+= anv_descriptor_size(&set_layout
->binding
[b
]) *
499 binding
->descriptorCount
;
502 set_layout
->shader_stages
|= binding
->stageFlags
;
505 set_layout
->buffer_view_count
= buffer_view_count
;
506 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
507 set_layout
->descriptor_buffer_size
= descriptor_buffer_size
;
509 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
515 anv_descriptor_set_layout_destroy(struct anv_device
*device
,
516 struct anv_descriptor_set_layout
*layout
)
518 assert(layout
->ref_cnt
== 0);
519 vk_object_base_finish(&layout
->base
);
520 vk_free(&device
->vk
.alloc
, layout
);
523 void anv_DestroyDescriptorSetLayout(
525 VkDescriptorSetLayout _set_layout
,
526 const VkAllocationCallbacks
* pAllocator
)
528 ANV_FROM_HANDLE(anv_device
, device
, _device
);
529 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
534 anv_descriptor_set_layout_unref(device
, set_layout
);
537 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
540 sha1_update_immutable_sampler(struct mesa_sha1
*ctx
,
541 const struct anv_sampler
*sampler
)
543 if (!sampler
->conversion
)
546 /* The only thing that affects the shader is ycbcr conversion */
547 _mesa_sha1_update(ctx
, sampler
->conversion
,
548 sizeof(*sampler
->conversion
));
552 sha1_update_descriptor_set_binding_layout(struct mesa_sha1
*ctx
,
553 const struct anv_descriptor_set_binding_layout
*layout
)
555 SHA1_UPDATE_VALUE(ctx
, layout
->flags
);
556 SHA1_UPDATE_VALUE(ctx
, layout
->data
);
557 SHA1_UPDATE_VALUE(ctx
, layout
->max_plane_count
);
558 SHA1_UPDATE_VALUE(ctx
, layout
->array_size
);
559 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_index
);
560 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_index
);
561 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_index
);
562 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_offset
);
564 if (layout
->immutable_samplers
) {
565 for (uint16_t i
= 0; i
< layout
->array_size
; i
++)
566 sha1_update_immutable_sampler(ctx
, layout
->immutable_samplers
[i
]);
571 sha1_update_descriptor_set_layout(struct mesa_sha1
*ctx
,
572 const struct anv_descriptor_set_layout
*layout
)
574 SHA1_UPDATE_VALUE(ctx
, layout
->binding_count
);
575 SHA1_UPDATE_VALUE(ctx
, layout
->size
);
576 SHA1_UPDATE_VALUE(ctx
, layout
->shader_stages
);
577 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_count
);
578 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_count
);
579 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_buffer_size
);
581 for (uint16_t i
= 0; i
< layout
->binding_count
; i
++)
582 sha1_update_descriptor_set_binding_layout(ctx
, &layout
->binding
[i
]);
586 * Pipeline layouts. These have nothing to do with the pipeline. They are
587 * just multiple descriptor set layouts pasted together
590 VkResult
anv_CreatePipelineLayout(
592 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
593 const VkAllocationCallbacks
* pAllocator
,
594 VkPipelineLayout
* pPipelineLayout
)
596 ANV_FROM_HANDLE(anv_device
, device
, _device
);
597 struct anv_pipeline_layout
*layout
;
599 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
601 layout
= vk_alloc2(&device
->vk
.alloc
, pAllocator
, sizeof(*layout
), 8,
602 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
604 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
606 vk_object_base_init(&device
->vk
, &layout
->base
,
607 VK_OBJECT_TYPE_PIPELINE_LAYOUT
);
608 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
610 unsigned dynamic_offset_count
= 0;
612 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
613 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
614 pCreateInfo
->pSetLayouts
[set
]);
615 layout
->set
[set
].layout
= set_layout
;
616 anv_descriptor_set_layout_ref(set_layout
);
618 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
619 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
620 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
623 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
626 assert(dynamic_offset_count
< MAX_DYNAMIC_BUFFERS
);
628 struct mesa_sha1 ctx
;
629 _mesa_sha1_init(&ctx
);
630 for (unsigned s
= 0; s
< layout
->num_sets
; s
++) {
631 sha1_update_descriptor_set_layout(&ctx
, layout
->set
[s
].layout
);
632 _mesa_sha1_update(&ctx
, &layout
->set
[s
].dynamic_offset_start
,
633 sizeof(layout
->set
[s
].dynamic_offset_start
));
635 _mesa_sha1_update(&ctx
, &layout
->num_sets
, sizeof(layout
->num_sets
));
636 _mesa_sha1_final(&ctx
, layout
->sha1
);
638 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
643 void anv_DestroyPipelineLayout(
645 VkPipelineLayout _pipelineLayout
,
646 const VkAllocationCallbacks
* pAllocator
)
648 ANV_FROM_HANDLE(anv_device
, device
, _device
);
649 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
651 if (!pipeline_layout
)
654 for (uint32_t i
= 0; i
< pipeline_layout
->num_sets
; i
++)
655 anv_descriptor_set_layout_unref(device
, pipeline_layout
->set
[i
].layout
);
657 vk_object_base_finish(&pipeline_layout
->base
);
658 vk_free2(&device
->vk
.alloc
, pAllocator
, pipeline_layout
);
664 * These are implemented using a big pool of memory and a free-list for the
665 * host memory allocations and a state_stream and a free list for the buffer
666 * view surface state. The spec allows us to fail to allocate due to
667 * fragmentation in all cases but two: 1) after pool reset, allocating up
668 * until the pool size with no freeing must succeed and 2) allocating and
669 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
670 * and the free lists lets us recycle blocks for case 2).
673 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
674 * ensure we can allocate the entire BO without hitting zero. The actual
675 * amount doesn't matter.
677 #define POOL_HEAP_OFFSET 64
681 VkResult
anv_CreateDescriptorPool(
683 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
684 const VkAllocationCallbacks
* pAllocator
,
685 VkDescriptorPool
* pDescriptorPool
)
687 ANV_FROM_HANDLE(anv_device
, device
, _device
);
688 struct anv_descriptor_pool
*pool
;
690 const VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*inline_info
=
691 vk_find_struct_const(pCreateInfo
->pNext
,
692 DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
);
694 uint32_t descriptor_count
= 0;
695 uint32_t buffer_view_count
= 0;
696 uint32_t descriptor_bo_size
= 0;
697 for (uint32_t i
= 0; i
< pCreateInfo
->poolSizeCount
; i
++) {
698 enum anv_descriptor_data desc_data
=
699 anv_descriptor_data_for_type(device
->physical
,
700 pCreateInfo
->pPoolSizes
[i
].type
);
702 if (desc_data
& ANV_DESCRIPTOR_BUFFER_VIEW
)
703 buffer_view_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
705 unsigned desc_data_size
= anv_descriptor_data_size(desc_data
) *
706 pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
708 /* Combined image sampler descriptors can take up to 3 slots if they
709 * hold a YCbCr image.
711 if (pCreateInfo
->pPoolSizes
[i
].type
==
712 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
715 if (pCreateInfo
->pPoolSizes
[i
].type
==
716 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
717 /* Inline uniform blocks are specified to use the descriptor array
718 * size as the size in bytes of the block.
721 desc_data_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
724 descriptor_bo_size
+= desc_data_size
;
726 descriptor_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
728 /* We have to align descriptor buffer allocations to 32B so that we can
729 * push descriptor buffers. This means that each descriptor buffer
730 * allocated may burn up to 32B of extra space to get the right alignment.
731 * (Technically, it's at most 28B because we're always going to start at
732 * least 4B aligned but we're being conservative here.) Allocate enough
733 * extra space that we can chop it into maxSets pieces and align each one
736 descriptor_bo_size
+= 32 * pCreateInfo
->maxSets
;
737 /* We align inline uniform blocks to 32B */
739 descriptor_bo_size
+= 32 * inline_info
->maxInlineUniformBlockBindings
;
740 descriptor_bo_size
= ALIGN(descriptor_bo_size
, 4096);
742 const size_t pool_size
=
743 pCreateInfo
->maxSets
* sizeof(struct anv_descriptor_set
) +
744 descriptor_count
* sizeof(struct anv_descriptor
) +
745 buffer_view_count
* sizeof(struct anv_buffer_view
);
746 const size_t total_size
= sizeof(*pool
) + pool_size
;
748 pool
= vk_alloc2(&device
->vk
.alloc
, pAllocator
, total_size
, 8,
749 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
751 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
753 vk_object_base_init(&device
->vk
, &pool
->base
,
754 VK_OBJECT_TYPE_DESCRIPTOR_POOL
);
755 pool
->size
= pool_size
;
757 pool
->free_list
= EMPTY
;
759 if (descriptor_bo_size
> 0) {
760 VkResult result
= anv_device_alloc_bo(device
,
762 ANV_BO_ALLOC_MAPPED
|
763 ANV_BO_ALLOC_SNOOPED
,
764 0 /* explicit_address */,
766 if (result
!= VK_SUCCESS
) {
767 vk_free2(&device
->vk
.alloc
, pAllocator
, pool
);
771 util_vma_heap_init(&pool
->bo_heap
, POOL_HEAP_OFFSET
, descriptor_bo_size
);
776 anv_state_stream_init(&pool
->surface_state_stream
,
777 &device
->surface_state_pool
, 4096);
778 pool
->surface_state_free_list
= NULL
;
780 list_inithead(&pool
->desc_sets
);
782 *pDescriptorPool
= anv_descriptor_pool_to_handle(pool
);
787 void anv_DestroyDescriptorPool(
789 VkDescriptorPool _pool
,
790 const VkAllocationCallbacks
* pAllocator
)
792 ANV_FROM_HANDLE(anv_device
, device
, _device
);
793 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, _pool
);
798 list_for_each_entry_safe(struct anv_descriptor_set
, set
,
799 &pool
->desc_sets
, pool_link
) {
800 anv_descriptor_set_layout_unref(device
, set
->layout
);
804 anv_device_release_bo(device
, pool
->bo
);
805 anv_state_stream_finish(&pool
->surface_state_stream
);
807 vk_object_base_finish(&pool
->base
);
808 vk_free2(&device
->vk
.alloc
, pAllocator
, pool
);
811 VkResult
anv_ResetDescriptorPool(
813 VkDescriptorPool descriptorPool
,
814 VkDescriptorPoolResetFlags flags
)
816 ANV_FROM_HANDLE(anv_device
, device
, _device
);
817 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
819 list_for_each_entry_safe(struct anv_descriptor_set
, set
,
820 &pool
->desc_sets
, pool_link
) {
821 anv_descriptor_set_layout_unref(device
, set
->layout
);
823 list_inithead(&pool
->desc_sets
);
826 pool
->free_list
= EMPTY
;
829 util_vma_heap_finish(&pool
->bo_heap
);
830 util_vma_heap_init(&pool
->bo_heap
, POOL_HEAP_OFFSET
, pool
->bo
->size
);
833 anv_state_stream_finish(&pool
->surface_state_stream
);
834 anv_state_stream_init(&pool
->surface_state_stream
,
835 &device
->surface_state_pool
, 4096);
836 pool
->surface_state_free_list
= NULL
;
841 struct pool_free_list_entry
{
847 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool
*pool
,
849 struct anv_descriptor_set
**set
)
851 if (size
<= pool
->size
- pool
->next
) {
852 *set
= (struct anv_descriptor_set
*) (pool
->data
+ pool
->next
);
856 struct pool_free_list_entry
*entry
;
857 uint32_t *link
= &pool
->free_list
;
858 for (uint32_t f
= pool
->free_list
; f
!= EMPTY
; f
= entry
->next
) {
859 entry
= (struct pool_free_list_entry
*) (pool
->data
+ f
);
860 if (size
<= entry
->size
) {
862 *set
= (struct anv_descriptor_set
*) entry
;
868 if (pool
->free_list
!= EMPTY
) {
869 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
871 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY
);
877 anv_descriptor_pool_free_set(struct anv_descriptor_pool
*pool
,
878 struct anv_descriptor_set
*set
)
880 /* Put the descriptor set allocation back on the free list. */
881 const uint32_t index
= (char *) set
- pool
->data
;
882 if (index
+ set
->size
== pool
->next
) {
885 struct pool_free_list_entry
*entry
= (struct pool_free_list_entry
*) set
;
886 entry
->next
= pool
->free_list
;
887 entry
->size
= set
->size
;
888 pool
->free_list
= (char *) entry
- pool
->data
;
892 struct surface_state_free_list_entry
{
894 struct anv_state state
;
897 static struct anv_state
898 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool
*pool
)
900 struct surface_state_free_list_entry
*entry
=
901 pool
->surface_state_free_list
;
904 struct anv_state state
= entry
->state
;
905 pool
->surface_state_free_list
= entry
->next
;
906 assert(state
.alloc_size
== 64);
909 return anv_state_stream_alloc(&pool
->surface_state_stream
, 64, 64);
914 anv_descriptor_pool_free_state(struct anv_descriptor_pool
*pool
,
915 struct anv_state state
)
917 /* Put the buffer view surface state back on the free list. */
918 struct surface_state_free_list_entry
*entry
= state
.map
;
919 entry
->next
= pool
->surface_state_free_list
;
920 entry
->state
= state
;
921 pool
->surface_state_free_list
= entry
;
925 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout
*layout
)
928 sizeof(struct anv_descriptor_set
) +
929 layout
->size
* sizeof(struct anv_descriptor
) +
930 layout
->buffer_view_count
* sizeof(struct anv_buffer_view
);
934 anv_descriptor_set_create(struct anv_device
*device
,
935 struct anv_descriptor_pool
*pool
,
936 struct anv_descriptor_set_layout
*layout
,
937 struct anv_descriptor_set
**out_set
)
939 struct anv_descriptor_set
*set
;
940 const size_t size
= anv_descriptor_set_layout_size(layout
);
942 VkResult result
= anv_descriptor_pool_alloc_set(pool
, size
, &set
);
943 if (result
!= VK_SUCCESS
)
946 if (layout
->descriptor_buffer_size
) {
947 /* Align the size to 32 so that alignment gaps don't cause extra holes
948 * in the heap which can lead to bad performance.
950 uint32_t set_buffer_size
= ALIGN(layout
->descriptor_buffer_size
, 32);
951 uint64_t pool_vma_offset
=
952 util_vma_heap_alloc(&pool
->bo_heap
, set_buffer_size
, 32);
953 if (pool_vma_offset
== 0) {
954 vk_object_base_finish(&set
->base
);
955 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
957 assert(pool_vma_offset
>= POOL_HEAP_OFFSET
&&
958 pool_vma_offset
- POOL_HEAP_OFFSET
<= INT32_MAX
);
959 set
->desc_mem
.offset
= pool_vma_offset
- POOL_HEAP_OFFSET
;
960 set
->desc_mem
.alloc_size
= set_buffer_size
;
961 set
->desc_mem
.map
= pool
->bo
->map
+ set
->desc_mem
.offset
;
963 set
->desc_surface_state
= anv_descriptor_pool_alloc_state(pool
);
964 anv_fill_buffer_surface_state(device
, set
->desc_surface_state
,
965 ISL_FORMAT_R32G32B32A32_FLOAT
,
966 (struct anv_address
) {
968 .offset
= set
->desc_mem
.offset
,
970 layout
->descriptor_buffer_size
, 1);
972 set
->desc_mem
= ANV_STATE_NULL
;
973 set
->desc_surface_state
= ANV_STATE_NULL
;
976 vk_object_base_init(&device
->vk
, &set
->base
,
977 VK_OBJECT_TYPE_DESCRIPTOR_SET
);
979 set
->layout
= layout
;
980 anv_descriptor_set_layout_ref(layout
);
984 (struct anv_buffer_view
*) &set
->descriptors
[layout
->size
];
985 set
->buffer_view_count
= layout
->buffer_view_count
;
987 /* By defining the descriptors to be zero now, we can later verify that
988 * a descriptor has not been populated with user data.
990 memset(set
->descriptors
, 0, sizeof(struct anv_descriptor
) * layout
->size
);
992 /* Go through and fill out immutable samplers if we have any */
993 struct anv_descriptor
*desc
= set
->descriptors
;
994 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
995 if (layout
->binding
[b
].immutable_samplers
) {
996 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++) {
997 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
998 * UpdateDescriptorSets if needed. However, if the descriptor
999 * set has an immutable sampler, UpdateDescriptorSets may never
1000 * touch it, so we need to make sure it's 100% valid now.
1002 * We don't need to actually provide a sampler because the helper
1003 * will always write in the immutable sampler regardless of what
1004 * is in the sampler parameter.
1006 VkDescriptorImageInfo info
= { };
1007 anv_descriptor_set_write_image_view(device
, set
, &info
,
1008 VK_DESCRIPTOR_TYPE_SAMPLER
,
1012 desc
+= layout
->binding
[b
].array_size
;
1015 /* Allocate surface state for the buffer views. */
1016 for (uint32_t b
= 0; b
< layout
->buffer_view_count
; b
++) {
1017 set
->buffer_views
[b
].surface_state
=
1018 anv_descriptor_pool_alloc_state(pool
);
1021 list_addtail(&set
->pool_link
, &pool
->desc_sets
);
1029 anv_descriptor_set_destroy(struct anv_device
*device
,
1030 struct anv_descriptor_pool
*pool
,
1031 struct anv_descriptor_set
*set
)
1033 anv_descriptor_set_layout_unref(device
, set
->layout
);
1035 if (set
->desc_mem
.alloc_size
) {
1036 util_vma_heap_free(&pool
->bo_heap
,
1037 (uint64_t)set
->desc_mem
.offset
+ POOL_HEAP_OFFSET
,
1038 set
->desc_mem
.alloc_size
);
1039 anv_descriptor_pool_free_state(pool
, set
->desc_surface_state
);
1042 for (uint32_t b
= 0; b
< set
->buffer_view_count
; b
++)
1043 anv_descriptor_pool_free_state(pool
, set
->buffer_views
[b
].surface_state
);
1045 list_del(&set
->pool_link
);
1047 vk_object_base_finish(&set
->base
);
1048 anv_descriptor_pool_free_set(pool
, set
);
1051 VkResult
anv_AllocateDescriptorSets(
1053 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
1054 VkDescriptorSet
* pDescriptorSets
)
1056 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1057 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
1059 VkResult result
= VK_SUCCESS
;
1060 struct anv_descriptor_set
*set
;
1063 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
1064 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
1065 pAllocateInfo
->pSetLayouts
[i
]);
1067 result
= anv_descriptor_set_create(device
, pool
, layout
, &set
);
1068 if (result
!= VK_SUCCESS
)
1071 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
1074 if (result
!= VK_SUCCESS
)
1075 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
1076 i
, pDescriptorSets
);
1081 VkResult
anv_FreeDescriptorSets(
1083 VkDescriptorPool descriptorPool
,
1085 const VkDescriptorSet
* pDescriptorSets
)
1087 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1088 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
1090 for (uint32_t i
= 0; i
< count
; i
++) {
1091 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
1096 anv_descriptor_set_destroy(device
, pool
, set
);
1103 anv_descriptor_set_write_image_param(uint32_t *param_desc_map
,
1104 const struct brw_image_param
*param
)
1106 #define WRITE_PARAM_FIELD(field, FIELD) \
1107 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
1108 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
1110 WRITE_PARAM_FIELD(offset
, OFFSET
);
1111 WRITE_PARAM_FIELD(size
, SIZE
);
1112 WRITE_PARAM_FIELD(stride
, STRIDE
);
1113 WRITE_PARAM_FIELD(tiling
, TILING
);
1114 WRITE_PARAM_FIELD(swizzling
, SWIZZLING
);
1115 WRITE_PARAM_FIELD(size
, SIZE
);
1117 #undef WRITE_PARAM_FIELD
1121 anv_surface_state_to_handle(struct anv_state state
)
1123 /* Bits 31:12 of the bindless surface offset in the extended message
1124 * descriptor is bits 25:6 of the byte-based address.
1126 assert(state
.offset
>= 0);
1127 uint32_t offset
= state
.offset
;
1128 assert((offset
& 0x3f) == 0 && offset
< (1 << 26));
1133 anv_descriptor_set_write_image_view(struct anv_device
*device
,
1134 struct anv_descriptor_set
*set
,
1135 const VkDescriptorImageInfo
* const info
,
1136 VkDescriptorType type
,
1140 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1141 &set
->layout
->binding
[binding
];
1142 struct anv_descriptor
*desc
=
1143 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1144 struct anv_image_view
*image_view
= NULL
;
1145 struct anv_sampler
*sampler
= NULL
;
1147 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1148 * set initialization to set the bindless samplers.
1150 assert(type
== bind_layout
->type
||
1151 type
== VK_DESCRIPTOR_TYPE_SAMPLER
);
1154 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1155 sampler
= bind_layout
->immutable_samplers
?
1156 bind_layout
->immutable_samplers
[element
] :
1157 anv_sampler_from_handle(info
->sampler
);
1160 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1161 image_view
= anv_image_view_from_handle(info
->imageView
);
1162 sampler
= bind_layout
->immutable_samplers
?
1163 bind_layout
->immutable_samplers
[element
] :
1164 anv_sampler_from_handle(info
->sampler
);
1167 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1168 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1169 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1170 image_view
= anv_image_view_from_handle(info
->imageView
);
1174 unreachable("invalid descriptor type");
1177 *desc
= (struct anv_descriptor
) {
1179 .layout
= info
->imageLayout
,
1180 .image_view
= image_view
,
1184 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1185 element
* anv_descriptor_size(bind_layout
);
1186 memset(desc_map
, 0, anv_descriptor_size(bind_layout
));
1188 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1189 struct anv_sampled_image_descriptor desc_data
[3];
1190 memset(desc_data
, 0, sizeof(desc_data
));
1193 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1194 struct anv_surface_state sstate
=
1195 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
1196 image_view
->planes
[p
].general_sampler_surface_state
:
1197 image_view
->planes
[p
].optimal_sampler_surface_state
;
1198 desc_data
[p
].image
= anv_surface_state_to_handle(sstate
.state
);
1203 for (unsigned p
= 0; p
< sampler
->n_planes
; p
++)
1204 desc_data
[p
].sampler
= sampler
->bindless_state
.offset
+ p
* 32;
1207 /* We may have max_plane_count < 0 if this isn't a sampled image but it
1208 * can be no more than the size of our array of handles.
1210 assert(bind_layout
->max_plane_count
<= ARRAY_SIZE(desc_data
));
1211 memcpy(desc_map
, desc_data
,
1212 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1215 if (image_view
== NULL
)
1218 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1219 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1220 assert(image_view
->n_planes
== 1);
1221 struct anv_storage_image_descriptor desc_data
= {
1222 .read_write
= anv_surface_state_to_handle(
1223 image_view
->planes
[0].storage_surface_state
.state
),
1224 .write_only
= anv_surface_state_to_handle(
1225 image_view
->planes
[0].writeonly_storage_surface_state
.state
),
1227 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1230 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1231 /* Storage images can only ever have one plane */
1232 assert(image_view
->n_planes
== 1);
1233 const struct brw_image_param
*image_param
=
1234 &image_view
->planes
[0].storage_image_param
;
1236 anv_descriptor_set_write_image_param(desc_map
, image_param
);
1239 if (bind_layout
->data
& ANV_DESCRIPTOR_TEXTURE_SWIZZLE
) {
1240 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
));
1242 struct anv_texture_swizzle_descriptor desc_data
[3];
1243 memset(desc_data
, 0, sizeof(desc_data
));
1245 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1246 desc_data
[p
] = (struct anv_texture_swizzle_descriptor
) {
1248 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.r
,
1249 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.g
,
1250 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.b
,
1251 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.a
,
1255 memcpy(desc_map
, desc_data
,
1256 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1261 anv_descriptor_set_write_buffer_view(struct anv_device
*device
,
1262 struct anv_descriptor_set
*set
,
1263 VkDescriptorType type
,
1264 struct anv_buffer_view
*buffer_view
,
1268 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1269 &set
->layout
->binding
[binding
];
1270 struct anv_descriptor
*desc
=
1271 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1273 assert(type
== bind_layout
->type
);
1275 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1276 element
* anv_descriptor_size(bind_layout
);
1278 if (buffer_view
== NULL
) {
1279 *desc
= (struct anv_descriptor
) { .type
= type
, };
1280 memset(desc_map
, 0, anv_descriptor_size(bind_layout
));
1284 *desc
= (struct anv_descriptor
) {
1286 .buffer_view
= buffer_view
,
1289 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1290 struct anv_sampled_image_descriptor desc_data
= {
1291 .image
= anv_surface_state_to_handle(buffer_view
->surface_state
),
1293 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1296 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1297 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1298 struct anv_storage_image_descriptor desc_data
= {
1299 .read_write
= anv_surface_state_to_handle(
1300 buffer_view
->storage_surface_state
),
1301 .write_only
= anv_surface_state_to_handle(
1302 buffer_view
->writeonly_storage_surface_state
),
1304 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1307 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1308 anv_descriptor_set_write_image_param(desc_map
,
1309 &buffer_view
->storage_image_param
);
1314 anv_descriptor_set_write_buffer(struct anv_device
*device
,
1315 struct anv_descriptor_set
*set
,
1316 struct anv_state_stream
*alloc_stream
,
1317 VkDescriptorType type
,
1318 struct anv_buffer
*buffer
,
1321 VkDeviceSize offset
,
1324 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1325 &set
->layout
->binding
[binding
];
1326 struct anv_descriptor
*desc
=
1327 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1329 assert(type
== bind_layout
->type
);
1331 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1332 element
* anv_descriptor_size(bind_layout
);
1334 if (buffer
== NULL
) {
1335 *desc
= (struct anv_descriptor
) { .type
= type
, };
1336 memset(desc_map
, 0, anv_descriptor_size(bind_layout
));
1340 struct anv_address bind_addr
= anv_address_add(buffer
->address
, offset
);
1341 uint64_t bind_range
= anv_buffer_get_range(buffer
, offset
, range
);
1343 /* We report a bounds checking alignment of 32B for the sake of block
1344 * messages which read an entire register worth at a time.
1346 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
||
1347 type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
)
1348 bind_range
= align_u64(bind_range
, ANV_UBO_ALIGNMENT
);
1350 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1351 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1352 *desc
= (struct anv_descriptor
) {
1359 assert(bind_layout
->data
& ANV_DESCRIPTOR_BUFFER_VIEW
);
1360 struct anv_buffer_view
*bview
=
1361 &set
->buffer_views
[bind_layout
->buffer_view_index
+ element
];
1363 bview
->format
= anv_isl_format_for_descriptor_type(type
);
1364 bview
->range
= bind_range
;
1365 bview
->address
= bind_addr
;
1367 /* If we're writing descriptors through a push command, we need to
1368 * allocate the surface state from the command buffer. Otherwise it will
1369 * be allocated by the descriptor pool when calling
1370 * vkAllocateDescriptorSets. */
1372 bview
->surface_state
= anv_state_stream_alloc(alloc_stream
, 64, 64);
1374 anv_fill_buffer_surface_state(device
, bview
->surface_state
,
1375 bview
->format
, bind_addr
, bind_range
, 1);
1377 *desc
= (struct anv_descriptor
) {
1379 .buffer_view
= bview
,
1383 if (bind_layout
->data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
1384 struct anv_address_range_descriptor desc_data
= {
1385 .address
= anv_address_physical(bind_addr
),
1386 .range
= bind_range
,
1388 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1393 anv_descriptor_set_write_inline_uniform_data(struct anv_device
*device
,
1394 struct anv_descriptor_set
*set
,
1400 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1401 &set
->layout
->binding
[binding
];
1403 assert(bind_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
);
1405 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
;
1407 memcpy(desc_map
+ offset
, data
, size
);
1410 void anv_UpdateDescriptorSets(
1412 uint32_t descriptorWriteCount
,
1413 const VkWriteDescriptorSet
* pDescriptorWrites
,
1414 uint32_t descriptorCopyCount
,
1415 const VkCopyDescriptorSet
* pDescriptorCopies
)
1417 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1419 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1420 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1421 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
1423 switch (write
->descriptorType
) {
1424 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1425 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1426 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1427 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1428 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1429 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1430 anv_descriptor_set_write_image_view(device
, set
,
1431 write
->pImageInfo
+ j
,
1432 write
->descriptorType
,
1434 write
->dstArrayElement
+ j
);
1438 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1439 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1440 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1441 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1442 write
->pTexelBufferView
[j
]);
1444 anv_descriptor_set_write_buffer_view(device
, set
,
1445 write
->descriptorType
,
1448 write
->dstArrayElement
+ j
);
1452 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1453 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1454 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1455 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1456 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1457 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1459 anv_descriptor_set_write_buffer(device
, set
,
1461 write
->descriptorType
,
1464 write
->dstArrayElement
+ j
,
1465 write
->pBufferInfo
[j
].offset
,
1466 write
->pBufferInfo
[j
].range
);
1470 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
: {
1471 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_write
=
1472 vk_find_struct_const(write
->pNext
,
1473 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
1474 assert(inline_write
->dataSize
== write
->descriptorCount
);
1475 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1477 inline_write
->pData
,
1478 write
->dstArrayElement
,
1479 inline_write
->dataSize
);
1488 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
1489 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
1490 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->srcSet
);
1491 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
1493 const struct anv_descriptor_set_binding_layout
*src_layout
=
1494 &src
->layout
->binding
[copy
->srcBinding
];
1495 struct anv_descriptor
*src_desc
=
1496 &src
->descriptors
[src_layout
->descriptor_index
];
1497 src_desc
+= copy
->srcArrayElement
;
1499 const struct anv_descriptor_set_binding_layout
*dst_layout
=
1500 &dst
->layout
->binding
[copy
->dstBinding
];
1501 struct anv_descriptor
*dst_desc
=
1502 &dst
->descriptors
[dst_layout
->descriptor_index
];
1503 dst_desc
+= copy
->dstArrayElement
;
1505 if (src_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
1506 assert(src_layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
1507 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1508 copy
->dstArrayElement
,
1509 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1510 copy
->srcArrayElement
,
1511 copy
->descriptorCount
);
1513 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
1514 dst_desc
[j
] = src_desc
[j
];
1516 unsigned desc_size
= anv_descriptor_size(src_layout
);
1517 if (desc_size
> 0) {
1518 assert(desc_size
== anv_descriptor_size(dst_layout
));
1519 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1520 copy
->dstArrayElement
* desc_size
,
1521 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1522 copy
->srcArrayElement
* desc_size
,
1523 copy
->descriptorCount
* desc_size
);
1530 * Descriptor update templates.
1534 anv_descriptor_set_write_template(struct anv_device
*device
,
1535 struct anv_descriptor_set
*set
,
1536 struct anv_state_stream
*alloc_stream
,
1537 const struct anv_descriptor_update_template
*template,
1540 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1541 const struct anv_descriptor_template_entry
*entry
=
1542 &template->entries
[i
];
1544 switch (entry
->type
) {
1545 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1546 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1547 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1548 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1549 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1550 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1551 const VkDescriptorImageInfo
*info
=
1552 data
+ entry
->offset
+ j
* entry
->stride
;
1553 anv_descriptor_set_write_image_view(device
, set
,
1556 entry
->array_element
+ j
);
1560 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1561 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1562 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1563 const VkBufferView
*_bview
=
1564 data
+ entry
->offset
+ j
* entry
->stride
;
1565 ANV_FROM_HANDLE(anv_buffer_view
, bview
, *_bview
);
1567 anv_descriptor_set_write_buffer_view(device
, set
,
1571 entry
->array_element
+ j
);
1575 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1576 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1577 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1578 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1579 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1580 const VkDescriptorBufferInfo
*info
=
1581 data
+ entry
->offset
+ j
* entry
->stride
;
1582 ANV_FROM_HANDLE(anv_buffer
, buffer
, info
->buffer
);
1584 anv_descriptor_set_write_buffer(device
, set
,
1589 entry
->array_element
+ j
,
1590 info
->offset
, info
->range
);
1594 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
1595 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1597 data
+ entry
->offset
,
1598 entry
->array_element
,
1599 entry
->array_count
);
1608 VkResult
anv_CreateDescriptorUpdateTemplate(
1610 const VkDescriptorUpdateTemplateCreateInfo
* pCreateInfo
,
1611 const VkAllocationCallbacks
* pAllocator
,
1612 VkDescriptorUpdateTemplate
* pDescriptorUpdateTemplate
)
1614 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1615 struct anv_descriptor_update_template
*template;
1617 size_t size
= sizeof(*template) +
1618 pCreateInfo
->descriptorUpdateEntryCount
* sizeof(template->entries
[0]);
1619 template = vk_alloc2(&device
->vk
.alloc
, pAllocator
, size
, 8,
1620 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1621 if (template == NULL
)
1622 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1624 vk_object_base_init(&device
->vk
, &template->base
,
1625 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE
);
1626 template->bind_point
= pCreateInfo
->pipelineBindPoint
;
1628 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
)
1629 template->set
= pCreateInfo
->set
;
1631 template->entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1632 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1633 const VkDescriptorUpdateTemplateEntry
*pEntry
=
1634 &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1636 template->entries
[i
] = (struct anv_descriptor_template_entry
) {
1637 .type
= pEntry
->descriptorType
,
1638 .binding
= pEntry
->dstBinding
,
1639 .array_element
= pEntry
->dstArrayElement
,
1640 .array_count
= pEntry
->descriptorCount
,
1641 .offset
= pEntry
->offset
,
1642 .stride
= pEntry
->stride
,
1646 *pDescriptorUpdateTemplate
=
1647 anv_descriptor_update_template_to_handle(template);
1652 void anv_DestroyDescriptorUpdateTemplate(
1654 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1655 const VkAllocationCallbacks
* pAllocator
)
1657 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1658 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1659 descriptorUpdateTemplate
);
1661 vk_object_base_finish(&template->base
);
1662 vk_free2(&device
->vk
.alloc
, pAllocator
, template);
1665 void anv_UpdateDescriptorSetWithTemplate(
1667 VkDescriptorSet descriptorSet
,
1668 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1671 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1672 ANV_FROM_HANDLE(anv_descriptor_set
, set
, descriptorSet
);
1673 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1674 descriptorUpdateTemplate
);
1676 anv_descriptor_set_write_template(device
, set
, NULL
, template, pData
);