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
);
857 struct pool_free_list_entry
*entry
;
858 uint32_t *link
= &pool
->free_list
;
859 for (uint32_t f
= pool
->free_list
; f
!= EMPTY
; f
= entry
->next
) {
860 entry
= (struct pool_free_list_entry
*) (pool
->data
+ f
);
861 if (size
<= entry
->size
) {
863 *set
= (struct anv_descriptor_set
*) entry
;
864 (*set
)->size
= entry
->size
;
870 if (pool
->free_list
!= EMPTY
) {
871 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
873 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY
);
879 anv_descriptor_pool_free_set(struct anv_descriptor_pool
*pool
,
880 struct anv_descriptor_set
*set
)
882 /* Put the descriptor set allocation back on the free list. */
883 const uint32_t index
= (char *) set
- pool
->data
;
884 if (index
+ set
->size
== pool
->next
) {
887 struct pool_free_list_entry
*entry
= (struct pool_free_list_entry
*) set
;
888 entry
->next
= pool
->free_list
;
889 entry
->size
= set
->size
;
890 pool
->free_list
= (char *) entry
- pool
->data
;
894 struct surface_state_free_list_entry
{
896 struct anv_state state
;
899 static struct anv_state
900 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool
*pool
)
902 struct surface_state_free_list_entry
*entry
=
903 pool
->surface_state_free_list
;
906 struct anv_state state
= entry
->state
;
907 pool
->surface_state_free_list
= entry
->next
;
908 assert(state
.alloc_size
== 64);
911 return anv_state_stream_alloc(&pool
->surface_state_stream
, 64, 64);
916 anv_descriptor_pool_free_state(struct anv_descriptor_pool
*pool
,
917 struct anv_state state
)
919 /* Put the buffer view surface state back on the free list. */
920 struct surface_state_free_list_entry
*entry
= state
.map
;
921 entry
->next
= pool
->surface_state_free_list
;
922 entry
->state
= state
;
923 pool
->surface_state_free_list
= entry
;
927 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout
*layout
)
930 sizeof(struct anv_descriptor_set
) +
931 layout
->size
* sizeof(struct anv_descriptor
) +
932 layout
->buffer_view_count
* sizeof(struct anv_buffer_view
);
936 anv_descriptor_set_create(struct anv_device
*device
,
937 struct anv_descriptor_pool
*pool
,
938 struct anv_descriptor_set_layout
*layout
,
939 struct anv_descriptor_set
**out_set
)
941 struct anv_descriptor_set
*set
;
942 const size_t size
= anv_descriptor_set_layout_size(layout
);
944 VkResult result
= anv_descriptor_pool_alloc_set(pool
, size
, &set
);
945 if (result
!= VK_SUCCESS
)
948 if (layout
->descriptor_buffer_size
) {
949 /* Align the size to 32 so that alignment gaps don't cause extra holes
950 * in the heap which can lead to bad performance.
952 uint32_t set_buffer_size
= ALIGN(layout
->descriptor_buffer_size
, 32);
953 uint64_t pool_vma_offset
=
954 util_vma_heap_alloc(&pool
->bo_heap
, set_buffer_size
, 32);
955 if (pool_vma_offset
== 0) {
956 anv_descriptor_pool_free_set(pool
, set
);
957 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
959 assert(pool_vma_offset
>= POOL_HEAP_OFFSET
&&
960 pool_vma_offset
- POOL_HEAP_OFFSET
<= INT32_MAX
);
961 set
->desc_mem
.offset
= pool_vma_offset
- POOL_HEAP_OFFSET
;
962 set
->desc_mem
.alloc_size
= set_buffer_size
;
963 set
->desc_mem
.map
= pool
->bo
->map
+ set
->desc_mem
.offset
;
965 set
->desc_surface_state
= anv_descriptor_pool_alloc_state(pool
);
966 anv_fill_buffer_surface_state(device
, set
->desc_surface_state
,
967 ISL_FORMAT_R32G32B32A32_FLOAT
,
968 (struct anv_address
) {
970 .offset
= set
->desc_mem
.offset
,
972 layout
->descriptor_buffer_size
, 1);
974 set
->desc_mem
= ANV_STATE_NULL
;
975 set
->desc_surface_state
= ANV_STATE_NULL
;
978 vk_object_base_init(&device
->vk
, &set
->base
,
979 VK_OBJECT_TYPE_DESCRIPTOR_SET
);
981 set
->layout
= layout
;
982 anv_descriptor_set_layout_ref(layout
);
985 (struct anv_buffer_view
*) &set
->descriptors
[layout
->size
];
986 set
->buffer_view_count
= layout
->buffer_view_count
;
988 /* By defining the descriptors to be zero now, we can later verify that
989 * a descriptor has not been populated with user data.
991 memset(set
->descriptors
, 0, sizeof(struct anv_descriptor
) * layout
->size
);
993 /* Go through and fill out immutable samplers if we have any */
994 struct anv_descriptor
*desc
= set
->descriptors
;
995 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
996 if (layout
->binding
[b
].immutable_samplers
) {
997 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++) {
998 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
999 * UpdateDescriptorSets if needed. However, if the descriptor
1000 * set has an immutable sampler, UpdateDescriptorSets may never
1001 * touch it, so we need to make sure it's 100% valid now.
1003 * We don't need to actually provide a sampler because the helper
1004 * will always write in the immutable sampler regardless of what
1005 * is in the sampler parameter.
1007 VkDescriptorImageInfo info
= { };
1008 anv_descriptor_set_write_image_view(device
, set
, &info
,
1009 VK_DESCRIPTOR_TYPE_SAMPLER
,
1013 desc
+= layout
->binding
[b
].array_size
;
1016 /* Allocate surface state for the buffer views. */
1017 for (uint32_t b
= 0; b
< layout
->buffer_view_count
; b
++) {
1018 set
->buffer_views
[b
].surface_state
=
1019 anv_descriptor_pool_alloc_state(pool
);
1022 list_addtail(&set
->pool_link
, &pool
->desc_sets
);
1030 anv_descriptor_set_destroy(struct anv_device
*device
,
1031 struct anv_descriptor_pool
*pool
,
1032 struct anv_descriptor_set
*set
)
1034 anv_descriptor_set_layout_unref(device
, set
->layout
);
1036 if (set
->desc_mem
.alloc_size
) {
1037 util_vma_heap_free(&pool
->bo_heap
,
1038 (uint64_t)set
->desc_mem
.offset
+ POOL_HEAP_OFFSET
,
1039 set
->desc_mem
.alloc_size
);
1040 anv_descriptor_pool_free_state(pool
, set
->desc_surface_state
);
1043 for (uint32_t b
= 0; b
< set
->buffer_view_count
; b
++)
1044 anv_descriptor_pool_free_state(pool
, set
->buffer_views
[b
].surface_state
);
1046 list_del(&set
->pool_link
);
1048 vk_object_base_finish(&set
->base
);
1049 anv_descriptor_pool_free_set(pool
, set
);
1052 VkResult
anv_AllocateDescriptorSets(
1054 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
1055 VkDescriptorSet
* pDescriptorSets
)
1057 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1058 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
1060 VkResult result
= VK_SUCCESS
;
1061 struct anv_descriptor_set
*set
;
1064 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
1065 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
1066 pAllocateInfo
->pSetLayouts
[i
]);
1068 result
= anv_descriptor_set_create(device
, pool
, layout
, &set
);
1069 if (result
!= VK_SUCCESS
)
1072 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
1075 if (result
!= VK_SUCCESS
)
1076 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
1077 i
, pDescriptorSets
);
1082 VkResult
anv_FreeDescriptorSets(
1084 VkDescriptorPool descriptorPool
,
1086 const VkDescriptorSet
* pDescriptorSets
)
1088 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1089 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
1091 for (uint32_t i
= 0; i
< count
; i
++) {
1092 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
1097 anv_descriptor_set_destroy(device
, pool
, set
);
1104 anv_descriptor_set_write_image_param(uint32_t *param_desc_map
,
1105 const struct brw_image_param
*param
)
1107 #define WRITE_PARAM_FIELD(field, FIELD) \
1108 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
1109 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
1111 WRITE_PARAM_FIELD(offset
, OFFSET
);
1112 WRITE_PARAM_FIELD(size
, SIZE
);
1113 WRITE_PARAM_FIELD(stride
, STRIDE
);
1114 WRITE_PARAM_FIELD(tiling
, TILING
);
1115 WRITE_PARAM_FIELD(swizzling
, SWIZZLING
);
1116 WRITE_PARAM_FIELD(size
, SIZE
);
1118 #undef WRITE_PARAM_FIELD
1122 anv_surface_state_to_handle(struct anv_state state
)
1124 /* Bits 31:12 of the bindless surface offset in the extended message
1125 * descriptor is bits 25:6 of the byte-based address.
1127 assert(state
.offset
>= 0);
1128 uint32_t offset
= state
.offset
;
1129 assert((offset
& 0x3f) == 0 && offset
< (1 << 26));
1134 anv_descriptor_set_write_image_view(struct anv_device
*device
,
1135 struct anv_descriptor_set
*set
,
1136 const VkDescriptorImageInfo
* const info
,
1137 VkDescriptorType type
,
1141 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1142 &set
->layout
->binding
[binding
];
1143 struct anv_descriptor
*desc
=
1144 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1145 struct anv_image_view
*image_view
= NULL
;
1146 struct anv_sampler
*sampler
= NULL
;
1148 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1149 * set initialization to set the bindless samplers.
1151 assert(type
== bind_layout
->type
||
1152 type
== VK_DESCRIPTOR_TYPE_SAMPLER
);
1155 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1156 sampler
= bind_layout
->immutable_samplers
?
1157 bind_layout
->immutable_samplers
[element
] :
1158 anv_sampler_from_handle(info
->sampler
);
1161 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1162 image_view
= anv_image_view_from_handle(info
->imageView
);
1163 sampler
= bind_layout
->immutable_samplers
?
1164 bind_layout
->immutable_samplers
[element
] :
1165 anv_sampler_from_handle(info
->sampler
);
1168 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1169 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1170 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1171 image_view
= anv_image_view_from_handle(info
->imageView
);
1175 unreachable("invalid descriptor type");
1178 *desc
= (struct anv_descriptor
) {
1180 .layout
= info
->imageLayout
,
1181 .image_view
= image_view
,
1185 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1186 element
* anv_descriptor_size(bind_layout
);
1187 memset(desc_map
, 0, anv_descriptor_size(bind_layout
));
1189 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1190 struct anv_sampled_image_descriptor desc_data
[3];
1191 memset(desc_data
, 0, sizeof(desc_data
));
1194 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1195 struct anv_surface_state sstate
=
1196 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
1197 image_view
->planes
[p
].general_sampler_surface_state
:
1198 image_view
->planes
[p
].optimal_sampler_surface_state
;
1199 desc_data
[p
].image
= anv_surface_state_to_handle(sstate
.state
);
1204 for (unsigned p
= 0; p
< sampler
->n_planes
; p
++)
1205 desc_data
[p
].sampler
= sampler
->bindless_state
.offset
+ p
* 32;
1208 /* We may have max_plane_count < 0 if this isn't a sampled image but it
1209 * can be no more than the size of our array of handles.
1211 assert(bind_layout
->max_plane_count
<= ARRAY_SIZE(desc_data
));
1212 memcpy(desc_map
, desc_data
,
1213 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1216 if (image_view
== NULL
)
1219 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1220 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1221 assert(image_view
->n_planes
== 1);
1222 struct anv_storage_image_descriptor desc_data
= {
1223 .read_write
= anv_surface_state_to_handle(
1224 image_view
->planes
[0].storage_surface_state
.state
),
1225 .write_only
= anv_surface_state_to_handle(
1226 image_view
->planes
[0].writeonly_storage_surface_state
.state
),
1228 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1231 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1232 /* Storage images can only ever have one plane */
1233 assert(image_view
->n_planes
== 1);
1234 const struct brw_image_param
*image_param
=
1235 &image_view
->planes
[0].storage_image_param
;
1237 anv_descriptor_set_write_image_param(desc_map
, image_param
);
1240 if (bind_layout
->data
& ANV_DESCRIPTOR_TEXTURE_SWIZZLE
) {
1241 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
));
1243 struct anv_texture_swizzle_descriptor desc_data
[3];
1244 memset(desc_data
, 0, sizeof(desc_data
));
1246 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1247 desc_data
[p
] = (struct anv_texture_swizzle_descriptor
) {
1249 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.r
,
1250 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.g
,
1251 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.b
,
1252 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.a
,
1256 memcpy(desc_map
, desc_data
,
1257 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1262 anv_descriptor_set_write_buffer_view(struct anv_device
*device
,
1263 struct anv_descriptor_set
*set
,
1264 VkDescriptorType type
,
1265 struct anv_buffer_view
*buffer_view
,
1269 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1270 &set
->layout
->binding
[binding
];
1271 struct anv_descriptor
*desc
=
1272 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1274 assert(type
== bind_layout
->type
);
1276 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1277 element
* anv_descriptor_size(bind_layout
);
1279 if (buffer_view
== NULL
) {
1280 *desc
= (struct anv_descriptor
) { .type
= type
, };
1281 memset(desc_map
, 0, anv_descriptor_size(bind_layout
));
1285 *desc
= (struct anv_descriptor
) {
1287 .buffer_view
= buffer_view
,
1290 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1291 struct anv_sampled_image_descriptor desc_data
= {
1292 .image
= anv_surface_state_to_handle(buffer_view
->surface_state
),
1294 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1297 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1298 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1299 struct anv_storage_image_descriptor desc_data
= {
1300 .read_write
= anv_surface_state_to_handle(
1301 buffer_view
->storage_surface_state
),
1302 .write_only
= anv_surface_state_to_handle(
1303 buffer_view
->writeonly_storage_surface_state
),
1305 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1308 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1309 anv_descriptor_set_write_image_param(desc_map
,
1310 &buffer_view
->storage_image_param
);
1315 anv_descriptor_set_write_buffer(struct anv_device
*device
,
1316 struct anv_descriptor_set
*set
,
1317 struct anv_state_stream
*alloc_stream
,
1318 VkDescriptorType type
,
1319 struct anv_buffer
*buffer
,
1322 VkDeviceSize offset
,
1325 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1326 &set
->layout
->binding
[binding
];
1327 struct anv_descriptor
*desc
=
1328 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1330 assert(type
== bind_layout
->type
);
1332 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1333 element
* anv_descriptor_size(bind_layout
);
1335 if (buffer
== NULL
) {
1336 *desc
= (struct anv_descriptor
) { .type
= type
, };
1337 memset(desc_map
, 0, anv_descriptor_size(bind_layout
));
1341 struct anv_address bind_addr
= anv_address_add(buffer
->address
, offset
);
1342 uint64_t bind_range
= anv_buffer_get_range(buffer
, offset
, range
);
1344 /* We report a bounds checking alignment of 32B for the sake of block
1345 * messages which read an entire register worth at a time.
1347 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
||
1348 type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
)
1349 bind_range
= align_u64(bind_range
, ANV_UBO_ALIGNMENT
);
1351 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1352 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1353 *desc
= (struct anv_descriptor
) {
1360 assert(bind_layout
->data
& ANV_DESCRIPTOR_BUFFER_VIEW
);
1361 struct anv_buffer_view
*bview
=
1362 &set
->buffer_views
[bind_layout
->buffer_view_index
+ element
];
1364 bview
->format
= anv_isl_format_for_descriptor_type(type
);
1365 bview
->range
= bind_range
;
1366 bview
->address
= bind_addr
;
1368 /* If we're writing descriptors through a push command, we need to
1369 * allocate the surface state from the command buffer. Otherwise it will
1370 * be allocated by the descriptor pool when calling
1371 * vkAllocateDescriptorSets. */
1373 bview
->surface_state
= anv_state_stream_alloc(alloc_stream
, 64, 64);
1375 anv_fill_buffer_surface_state(device
, bview
->surface_state
,
1376 bview
->format
, bind_addr
, bind_range
, 1);
1378 *desc
= (struct anv_descriptor
) {
1380 .buffer_view
= bview
,
1384 if (bind_layout
->data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
1385 struct anv_address_range_descriptor desc_data
= {
1386 .address
= anv_address_physical(bind_addr
),
1387 .range
= bind_range
,
1389 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1394 anv_descriptor_set_write_inline_uniform_data(struct anv_device
*device
,
1395 struct anv_descriptor_set
*set
,
1401 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1402 &set
->layout
->binding
[binding
];
1404 assert(bind_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
);
1406 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
;
1408 memcpy(desc_map
+ offset
, data
, size
);
1411 void anv_UpdateDescriptorSets(
1413 uint32_t descriptorWriteCount
,
1414 const VkWriteDescriptorSet
* pDescriptorWrites
,
1415 uint32_t descriptorCopyCount
,
1416 const VkCopyDescriptorSet
* pDescriptorCopies
)
1418 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1420 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1421 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1422 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
1424 switch (write
->descriptorType
) {
1425 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1426 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1427 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1428 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1429 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1430 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1431 anv_descriptor_set_write_image_view(device
, set
,
1432 write
->pImageInfo
+ j
,
1433 write
->descriptorType
,
1435 write
->dstArrayElement
+ j
);
1439 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1440 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1441 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1442 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1443 write
->pTexelBufferView
[j
]);
1445 anv_descriptor_set_write_buffer_view(device
, set
,
1446 write
->descriptorType
,
1449 write
->dstArrayElement
+ j
);
1453 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1454 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1455 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1456 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1457 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1458 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1460 anv_descriptor_set_write_buffer(device
, set
,
1462 write
->descriptorType
,
1465 write
->dstArrayElement
+ j
,
1466 write
->pBufferInfo
[j
].offset
,
1467 write
->pBufferInfo
[j
].range
);
1471 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
: {
1472 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_write
=
1473 vk_find_struct_const(write
->pNext
,
1474 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
1475 assert(inline_write
->dataSize
== write
->descriptorCount
);
1476 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1478 inline_write
->pData
,
1479 write
->dstArrayElement
,
1480 inline_write
->dataSize
);
1489 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
1490 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
1491 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->srcSet
);
1492 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
1494 const struct anv_descriptor_set_binding_layout
*src_layout
=
1495 &src
->layout
->binding
[copy
->srcBinding
];
1496 struct anv_descriptor
*src_desc
=
1497 &src
->descriptors
[src_layout
->descriptor_index
];
1498 src_desc
+= copy
->srcArrayElement
;
1500 const struct anv_descriptor_set_binding_layout
*dst_layout
=
1501 &dst
->layout
->binding
[copy
->dstBinding
];
1502 struct anv_descriptor
*dst_desc
=
1503 &dst
->descriptors
[dst_layout
->descriptor_index
];
1504 dst_desc
+= copy
->dstArrayElement
;
1506 if (src_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
1507 assert(src_layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
1508 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1509 copy
->dstArrayElement
,
1510 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1511 copy
->srcArrayElement
,
1512 copy
->descriptorCount
);
1514 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
1515 dst_desc
[j
] = src_desc
[j
];
1517 unsigned desc_size
= anv_descriptor_size(src_layout
);
1518 if (desc_size
> 0) {
1519 assert(desc_size
== anv_descriptor_size(dst_layout
));
1520 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1521 copy
->dstArrayElement
* desc_size
,
1522 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1523 copy
->srcArrayElement
* desc_size
,
1524 copy
->descriptorCount
* desc_size
);
1531 * Descriptor update templates.
1535 anv_descriptor_set_write_template(struct anv_device
*device
,
1536 struct anv_descriptor_set
*set
,
1537 struct anv_state_stream
*alloc_stream
,
1538 const struct anv_descriptor_update_template
*template,
1541 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1542 const struct anv_descriptor_template_entry
*entry
=
1543 &template->entries
[i
];
1545 switch (entry
->type
) {
1546 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1547 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1548 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1549 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1550 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1551 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1552 const VkDescriptorImageInfo
*info
=
1553 data
+ entry
->offset
+ j
* entry
->stride
;
1554 anv_descriptor_set_write_image_view(device
, set
,
1557 entry
->array_element
+ j
);
1561 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1562 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1563 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1564 const VkBufferView
*_bview
=
1565 data
+ entry
->offset
+ j
* entry
->stride
;
1566 ANV_FROM_HANDLE(anv_buffer_view
, bview
, *_bview
);
1568 anv_descriptor_set_write_buffer_view(device
, set
,
1572 entry
->array_element
+ j
);
1576 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1577 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1578 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1579 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1580 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1581 const VkDescriptorBufferInfo
*info
=
1582 data
+ entry
->offset
+ j
* entry
->stride
;
1583 ANV_FROM_HANDLE(anv_buffer
, buffer
, info
->buffer
);
1585 anv_descriptor_set_write_buffer(device
, set
,
1590 entry
->array_element
+ j
,
1591 info
->offset
, info
->range
);
1595 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
1596 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1598 data
+ entry
->offset
,
1599 entry
->array_element
,
1600 entry
->array_count
);
1609 VkResult
anv_CreateDescriptorUpdateTemplate(
1611 const VkDescriptorUpdateTemplateCreateInfo
* pCreateInfo
,
1612 const VkAllocationCallbacks
* pAllocator
,
1613 VkDescriptorUpdateTemplate
* pDescriptorUpdateTemplate
)
1615 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1616 struct anv_descriptor_update_template
*template;
1618 size_t size
= sizeof(*template) +
1619 pCreateInfo
->descriptorUpdateEntryCount
* sizeof(template->entries
[0]);
1620 template = vk_alloc2(&device
->vk
.alloc
, pAllocator
, size
, 8,
1621 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1622 if (template == NULL
)
1623 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1625 vk_object_base_init(&device
->vk
, &template->base
,
1626 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE
);
1627 template->bind_point
= pCreateInfo
->pipelineBindPoint
;
1629 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
)
1630 template->set
= pCreateInfo
->set
;
1632 template->entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1633 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1634 const VkDescriptorUpdateTemplateEntry
*pEntry
=
1635 &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1637 template->entries
[i
] = (struct anv_descriptor_template_entry
) {
1638 .type
= pEntry
->descriptorType
,
1639 .binding
= pEntry
->dstBinding
,
1640 .array_element
= pEntry
->dstArrayElement
,
1641 .array_count
= pEntry
->descriptorCount
,
1642 .offset
= pEntry
->offset
,
1643 .stride
= pEntry
->stride
,
1647 *pDescriptorUpdateTemplate
=
1648 anv_descriptor_update_template_to_handle(template);
1653 void anv_DestroyDescriptorUpdateTemplate(
1655 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1656 const VkAllocationCallbacks
* pAllocator
)
1658 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1659 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1660 descriptorUpdateTemplate
);
1662 vk_object_base_finish(&template->base
);
1663 vk_free2(&device
->vk
.alloc
, pAllocator
, template);
1666 void anv_UpdateDescriptorSetWithTemplate(
1668 VkDescriptorSet descriptorSet
,
1669 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1672 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1673 ANV_FROM_HANDLE(anv_descriptor_set
, set
, descriptorSet
);
1674 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1675 descriptorUpdateTemplate
);
1677 anv_descriptor_set_write_template(device
, set
, NULL
, template, pData
);