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 if (device
->info
.gen
== 7 && !device
->info
.is_haswell
&&
108 (type
== VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
||
109 type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
))
110 data
|= ANV_DESCRIPTOR_TEXTURE_SWIZZLE
;
116 anv_descriptor_data_size(enum anv_descriptor_data data
)
120 if (data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
)
121 size
+= sizeof(struct anv_sampled_image_descriptor
);
123 if (data
& ANV_DESCRIPTOR_STORAGE_IMAGE
)
124 size
+= sizeof(struct anv_storage_image_descriptor
);
126 if (data
& ANV_DESCRIPTOR_IMAGE_PARAM
)
127 size
+= BRW_IMAGE_PARAM_SIZE
* 4;
129 if (data
& ANV_DESCRIPTOR_ADDRESS_RANGE
)
130 size
+= sizeof(struct anv_address_range_descriptor
);
132 if (data
& ANV_DESCRIPTOR_TEXTURE_SWIZZLE
)
133 size
+= sizeof(struct anv_texture_swizzle_descriptor
);
138 /** Returns the size in bytes of each descriptor with the given layout */
140 anv_descriptor_size(const struct anv_descriptor_set_binding_layout
*layout
)
142 if (layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
143 assert(layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
144 return layout
->array_size
;
147 unsigned size
= anv_descriptor_data_size(layout
->data
);
149 /* For multi-planar bindings, we make every descriptor consume the maximum
150 * number of planes so we don't have to bother with walking arrays and
151 * adding things up every time. Fortunately, YCbCr samplers aren't all
152 * that common and likely won't be in the middle of big arrays.
154 if (layout
->max_plane_count
> 1)
155 size
*= layout
->max_plane_count
;
160 /** Returns the size in bytes of each descriptor of the given type
162 * This version of the function does not have access to the entire layout so
163 * it may only work on certain descriptor types where the descriptor size is
164 * entirely determined by the descriptor type. Whenever possible, code should
165 * use anv_descriptor_size() instead.
168 anv_descriptor_type_size(const struct anv_physical_device
*pdevice
,
169 VkDescriptorType type
)
171 assert(type
!= VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
&&
172 type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
173 type
!= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
&&
174 type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
);
176 return anv_descriptor_data_size(anv_descriptor_data_for_type(pdevice
, type
));
180 anv_descriptor_data_supports_bindless(const struct anv_physical_device
*pdevice
,
181 enum anv_descriptor_data data
,
184 if (data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
185 assert(pdevice
->has_a64_buffer_access
);
189 if (data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
190 assert(pdevice
->has_bindless_images
|| pdevice
->has_bindless_samplers
);
191 return sampler
? pdevice
->has_bindless_samplers
:
192 pdevice
->has_bindless_images
;
195 if (data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
196 assert(pdevice
->has_bindless_images
);
204 anv_descriptor_supports_bindless(const struct anv_physical_device
*pdevice
,
205 const struct anv_descriptor_set_binding_layout
*binding
,
208 return anv_descriptor_data_supports_bindless(pdevice
, binding
->data
,
213 anv_descriptor_requires_bindless(const struct anv_physical_device
*pdevice
,
214 const struct anv_descriptor_set_binding_layout
*binding
,
217 if (pdevice
->always_use_bindless
)
218 return anv_descriptor_supports_bindless(pdevice
, binding
, sampler
);
220 static const VkDescriptorBindingFlagBitsEXT flags_requiring_bindless
=
221 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT
|
222 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT
|
223 VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT
;
225 return (binding
->flags
& flags_requiring_bindless
) != 0;
228 void anv_GetDescriptorSetLayoutSupport(
230 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
231 VkDescriptorSetLayoutSupport
* pSupport
)
233 ANV_FROM_HANDLE(anv_device
, device
, _device
);
234 const struct anv_physical_device
*pdevice
=
235 &device
->instance
->physicalDevice
;
237 uint32_t surface_count
[MESA_SHADER_STAGES
] = { 0, };
239 for (uint32_t b
= 0; b
< pCreateInfo
->bindingCount
; b
++) {
240 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[b
];
242 enum anv_descriptor_data desc_data
=
243 anv_descriptor_data_for_type(pdevice
, binding
->descriptorType
);
245 switch (binding
->descriptorType
) {
246 case VK_DESCRIPTOR_TYPE_SAMPLER
:
247 /* There is no real limit on samplers */
250 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
251 if (anv_descriptor_data_supports_bindless(pdevice
, desc_data
, false))
254 if (binding
->pImmutableSamplers
) {
255 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
256 ANV_FROM_HANDLE(anv_sampler
, sampler
,
257 binding
->pImmutableSamplers
[i
]);
258 anv_foreach_stage(s
, binding
->stageFlags
)
259 surface_count
[s
] += sampler
->n_planes
;
262 anv_foreach_stage(s
, binding
->stageFlags
)
263 surface_count
[s
] += binding
->descriptorCount
;
268 if (anv_descriptor_data_supports_bindless(pdevice
, desc_data
, false))
271 anv_foreach_stage(s
, binding
->stageFlags
)
272 surface_count
[s
] += binding
->descriptorCount
;
277 bool supported
= true;
278 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
279 /* Our maximum binding table size is 240 and we need to reserve 8 for
282 if (surface_count
[s
] >= MAX_BINDING_TABLE_SIZE
- MAX_RTS
)
286 pSupport
->supported
= supported
;
289 VkResult
anv_CreateDescriptorSetLayout(
291 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
292 const VkAllocationCallbacks
* pAllocator
,
293 VkDescriptorSetLayout
* pSetLayout
)
295 ANV_FROM_HANDLE(anv_device
, device
, _device
);
297 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
299 uint32_t max_binding
= 0;
300 uint32_t immutable_sampler_count
= 0;
301 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
302 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
304 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
306 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
307 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
308 * pImmutableSamplers can be used to initialize a set of immutable
309 * samplers. [...] If descriptorType is not one of these descriptor
310 * types, then pImmutableSamplers is ignored.
312 * We need to be careful here and only parse pImmutableSamplers if we
313 * have one of the right descriptor types.
315 VkDescriptorType desc_type
= pCreateInfo
->pBindings
[j
].descriptorType
;
316 if ((desc_type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
317 desc_type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
) &&
318 pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
319 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
322 struct anv_descriptor_set_layout
*set_layout
;
323 struct anv_descriptor_set_binding_layout
*bindings
;
324 struct anv_sampler
**samplers
;
326 /* We need to allocate decriptor set layouts off the device allocator
327 * with DEVICE scope because they are reference counted and may not be
328 * destroyed when vkDestroyDescriptorSetLayout is called.
331 anv_multialloc_add(&ma
, &set_layout
, 1);
332 anv_multialloc_add(&ma
, &bindings
, max_binding
+ 1);
333 anv_multialloc_add(&ma
, &samplers
, immutable_sampler_count
);
335 if (!anv_multialloc_alloc(&ma
, &device
->alloc
,
336 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
))
337 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
339 memset(set_layout
, 0, sizeof(*set_layout
));
340 set_layout
->ref_cnt
= 1;
341 set_layout
->binding_count
= max_binding
+ 1;
343 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
344 /* Initialize all binding_layout entries to -1 */
345 memset(&set_layout
->binding
[b
], -1, sizeof(set_layout
->binding
[b
]));
347 set_layout
->binding
[b
].flags
= 0;
348 set_layout
->binding
[b
].data
= 0;
349 set_layout
->binding
[b
].max_plane_count
= 0;
350 set_layout
->binding
[b
].array_size
= 0;
351 set_layout
->binding
[b
].immutable_samplers
= NULL
;
354 /* Initialize all samplers to 0 */
355 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
357 uint32_t buffer_view_count
= 0;
358 uint32_t dynamic_offset_count
= 0;
359 uint32_t descriptor_buffer_size
= 0;
361 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
362 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
363 uint32_t b
= binding
->binding
;
364 /* We temporarily store pCreateInfo->pBindings[] index (plus one) in the
365 * immutable_samplers pointer. This provides us with a quick-and-dirty
366 * way to sort the bindings by binding number.
368 set_layout
->binding
[b
].immutable_samplers
= (void *)(uintptr_t)(j
+ 1);
371 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*binding_flags_info
=
372 vk_find_struct_const(pCreateInfo
->pNext
,
373 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
375 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
376 /* We stashed the pCreateInfo->pBindings[] index (plus one) in the
377 * immutable_samplers pointer. Check for NULL (empty binding) and then
378 * reset it and compute the index.
380 if (set_layout
->binding
[b
].immutable_samplers
== NULL
)
382 const uint32_t info_idx
=
383 (uintptr_t)(void *)set_layout
->binding
[b
].immutable_samplers
- 1;
384 set_layout
->binding
[b
].immutable_samplers
= NULL
;
386 const VkDescriptorSetLayoutBinding
*binding
=
387 &pCreateInfo
->pBindings
[info_idx
];
389 if (binding
->descriptorCount
== 0)
393 set_layout
->binding
[b
].type
= binding
->descriptorType
;
396 if (binding_flags_info
&& binding_flags_info
->bindingCount
> 0) {
397 assert(binding_flags_info
->bindingCount
== pCreateInfo
->bindingCount
);
398 set_layout
->binding
[b
].flags
=
399 binding_flags_info
->pBindingFlags
[info_idx
];
402 set_layout
->binding
[b
].data
=
403 anv_descriptor_data_for_type(&device
->instance
->physicalDevice
,
404 binding
->descriptorType
);
405 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
406 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
407 set_layout
->size
+= binding
->descriptorCount
;
409 if (set_layout
->binding
[b
].data
& ANV_DESCRIPTOR_BUFFER_VIEW
) {
410 set_layout
->binding
[b
].buffer_view_index
= buffer_view_count
;
411 buffer_view_count
+= binding
->descriptorCount
;
414 switch (binding
->descriptorType
) {
415 case VK_DESCRIPTOR_TYPE_SAMPLER
:
416 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
417 set_layout
->binding
[b
].max_plane_count
= 1;
418 if (binding
->pImmutableSamplers
) {
419 set_layout
->binding
[b
].immutable_samplers
= samplers
;
420 samplers
+= binding
->descriptorCount
;
422 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
423 ANV_FROM_HANDLE(anv_sampler
, sampler
,
424 binding
->pImmutableSamplers
[i
]);
426 set_layout
->binding
[b
].immutable_samplers
[i
] = sampler
;
427 if (set_layout
->binding
[b
].max_plane_count
< sampler
->n_planes
)
428 set_layout
->binding
[b
].max_plane_count
= sampler
->n_planes
;
433 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
434 set_layout
->binding
[b
].max_plane_count
= 1;
441 switch (binding
->descriptorType
) {
442 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
443 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
444 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
445 dynamic_offset_count
+= binding
->descriptorCount
;
452 if (binding
->descriptorType
==
453 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
454 /* Inline uniform blocks are specified to use the descriptor array
455 * size as the size in bytes of the block.
457 descriptor_buffer_size
= align_u32(descriptor_buffer_size
, 32);
458 set_layout
->binding
[b
].descriptor_offset
= descriptor_buffer_size
;
459 descriptor_buffer_size
+= binding
->descriptorCount
;
461 set_layout
->binding
[b
].descriptor_offset
= descriptor_buffer_size
;
462 descriptor_buffer_size
+= anv_descriptor_size(&set_layout
->binding
[b
]) *
463 binding
->descriptorCount
;
466 set_layout
->shader_stages
|= binding
->stageFlags
;
469 set_layout
->buffer_view_count
= buffer_view_count
;
470 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
471 set_layout
->descriptor_buffer_size
= descriptor_buffer_size
;
473 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
478 void anv_DestroyDescriptorSetLayout(
480 VkDescriptorSetLayout _set_layout
,
481 const VkAllocationCallbacks
* pAllocator
)
483 ANV_FROM_HANDLE(anv_device
, device
, _device
);
484 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
489 anv_descriptor_set_layout_unref(device
, set_layout
);
492 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
495 sha1_update_immutable_sampler(struct mesa_sha1
*ctx
,
496 const struct anv_sampler
*sampler
)
498 if (!sampler
->conversion
)
501 /* The only thing that affects the shader is ycbcr conversion */
502 _mesa_sha1_update(ctx
, sampler
->conversion
,
503 sizeof(*sampler
->conversion
));
507 sha1_update_descriptor_set_binding_layout(struct mesa_sha1
*ctx
,
508 const struct anv_descriptor_set_binding_layout
*layout
)
510 SHA1_UPDATE_VALUE(ctx
, layout
->flags
);
511 SHA1_UPDATE_VALUE(ctx
, layout
->data
);
512 SHA1_UPDATE_VALUE(ctx
, layout
->max_plane_count
);
513 SHA1_UPDATE_VALUE(ctx
, layout
->array_size
);
514 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_index
);
515 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_index
);
516 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_index
);
517 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_offset
);
519 if (layout
->immutable_samplers
) {
520 for (uint16_t i
= 0; i
< layout
->array_size
; i
++)
521 sha1_update_immutable_sampler(ctx
, layout
->immutable_samplers
[i
]);
526 sha1_update_descriptor_set_layout(struct mesa_sha1
*ctx
,
527 const struct anv_descriptor_set_layout
*layout
)
529 SHA1_UPDATE_VALUE(ctx
, layout
->binding_count
);
530 SHA1_UPDATE_VALUE(ctx
, layout
->size
);
531 SHA1_UPDATE_VALUE(ctx
, layout
->shader_stages
);
532 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_count
);
533 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_count
);
534 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_buffer_size
);
536 for (uint16_t i
= 0; i
< layout
->binding_count
; i
++)
537 sha1_update_descriptor_set_binding_layout(ctx
, &layout
->binding
[i
]);
541 * Pipeline layouts. These have nothing to do with the pipeline. They are
542 * just multiple descriptor set layouts pasted together
545 VkResult
anv_CreatePipelineLayout(
547 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
548 const VkAllocationCallbacks
* pAllocator
,
549 VkPipelineLayout
* pPipelineLayout
)
551 ANV_FROM_HANDLE(anv_device
, device
, _device
);
552 struct anv_pipeline_layout
*layout
;
554 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
556 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
557 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
559 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
561 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
563 unsigned dynamic_offset_count
= 0;
565 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
566 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
567 pCreateInfo
->pSetLayouts
[set
]);
568 layout
->set
[set
].layout
= set_layout
;
569 anv_descriptor_set_layout_ref(set_layout
);
571 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
572 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
573 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
576 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
580 struct mesa_sha1 ctx
;
581 _mesa_sha1_init(&ctx
);
582 for (unsigned s
= 0; s
< layout
->num_sets
; s
++) {
583 sha1_update_descriptor_set_layout(&ctx
, layout
->set
[s
].layout
);
584 _mesa_sha1_update(&ctx
, &layout
->set
[s
].dynamic_offset_start
,
585 sizeof(layout
->set
[s
].dynamic_offset_start
));
587 _mesa_sha1_update(&ctx
, &layout
->num_sets
, sizeof(layout
->num_sets
));
588 _mesa_sha1_final(&ctx
, layout
->sha1
);
590 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
595 void anv_DestroyPipelineLayout(
597 VkPipelineLayout _pipelineLayout
,
598 const VkAllocationCallbacks
* pAllocator
)
600 ANV_FROM_HANDLE(anv_device
, device
, _device
);
601 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
603 if (!pipeline_layout
)
606 for (uint32_t i
= 0; i
< pipeline_layout
->num_sets
; i
++)
607 anv_descriptor_set_layout_unref(device
, pipeline_layout
->set
[i
].layout
);
609 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
615 * These are implemented using a big pool of memory and a free-list for the
616 * host memory allocations and a state_stream and a free list for the buffer
617 * view surface state. The spec allows us to fail to allocate due to
618 * fragmentation in all cases but two: 1) after pool reset, allocating up
619 * until the pool size with no freeing must succeed and 2) allocating and
620 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
621 * and the free lists lets us recycle blocks for case 2).
624 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
625 * ensure we can allocate the entire BO without hitting zero. The actual
626 * amount doesn't matter.
628 #define POOL_HEAP_OFFSET 64
632 VkResult
anv_CreateDescriptorPool(
634 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
635 const VkAllocationCallbacks
* pAllocator
,
636 VkDescriptorPool
* pDescriptorPool
)
638 ANV_FROM_HANDLE(anv_device
, device
, _device
);
639 struct anv_descriptor_pool
*pool
;
641 const VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*inline_info
=
642 vk_find_struct_const(pCreateInfo
->pNext
,
643 DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
);
645 uint32_t descriptor_count
= 0;
646 uint32_t buffer_view_count
= 0;
647 uint32_t descriptor_bo_size
= 0;
648 for (uint32_t i
= 0; i
< pCreateInfo
->poolSizeCount
; i
++) {
649 enum anv_descriptor_data desc_data
=
650 anv_descriptor_data_for_type(&device
->instance
->physicalDevice
,
651 pCreateInfo
->pPoolSizes
[i
].type
);
653 if (desc_data
& ANV_DESCRIPTOR_BUFFER_VIEW
)
654 buffer_view_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
656 unsigned desc_data_size
= anv_descriptor_data_size(desc_data
) *
657 pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
659 /* Combined image sampler descriptors can take up to 3 slots if they
660 * hold a YCbCr image.
662 if (pCreateInfo
->pPoolSizes
[i
].type
==
663 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
666 if (pCreateInfo
->pPoolSizes
[i
].type
==
667 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
668 /* Inline uniform blocks are specified to use the descriptor array
669 * size as the size in bytes of the block.
672 desc_data_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
675 descriptor_bo_size
+= desc_data_size
;
677 descriptor_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
679 /* We have to align descriptor buffer allocations to 32B so that we can
680 * push descriptor buffers. This means that each descriptor buffer
681 * allocated may burn up to 32B of extra space to get the right alignment.
682 * (Technically, it's at most 28B because we're always going to start at
683 * least 4B aligned but we're being conservative here.) Allocate enough
684 * extra space that we can chop it into maxSets pieces and align each one
687 descriptor_bo_size
+= 32 * pCreateInfo
->maxSets
;
688 /* We align inline uniform blocks to 32B */
690 descriptor_bo_size
+= 32 * inline_info
->maxInlineUniformBlockBindings
;
691 descriptor_bo_size
= ALIGN(descriptor_bo_size
, 4096);
693 const size_t pool_size
=
694 pCreateInfo
->maxSets
* sizeof(struct anv_descriptor_set
) +
695 descriptor_count
* sizeof(struct anv_descriptor
) +
696 buffer_view_count
* sizeof(struct anv_buffer_view
);
697 const size_t total_size
= sizeof(*pool
) + pool_size
;
699 pool
= vk_alloc2(&device
->alloc
, pAllocator
, total_size
, 8,
700 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
702 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
704 pool
->size
= pool_size
;
706 pool
->free_list
= EMPTY
;
708 if (descriptor_bo_size
> 0) {
709 VkResult result
= anv_bo_init_new(&pool
->bo
, device
, descriptor_bo_size
);
710 if (result
!= VK_SUCCESS
) {
711 vk_free2(&device
->alloc
, pAllocator
, pool
);
715 anv_gem_set_caching(device
, pool
->bo
.gem_handle
, I915_CACHING_CACHED
);
717 pool
->bo
.map
= anv_gem_mmap(device
, pool
->bo
.gem_handle
, 0,
718 descriptor_bo_size
, 0);
719 if (pool
->bo
.map
== NULL
) {
720 anv_gem_close(device
, pool
->bo
.gem_handle
);
721 vk_free2(&device
->alloc
, pAllocator
, pool
);
722 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
725 if (device
->instance
->physicalDevice
.use_softpin
) {
726 pool
->bo
.flags
|= EXEC_OBJECT_PINNED
;
727 anv_vma_alloc(device
, &pool
->bo
);
730 util_vma_heap_init(&pool
->bo_heap
, POOL_HEAP_OFFSET
, descriptor_bo_size
);
735 anv_state_stream_init(&pool
->surface_state_stream
,
736 &device
->surface_state_pool
, 4096);
737 pool
->surface_state_free_list
= NULL
;
739 list_inithead(&pool
->desc_sets
);
741 *pDescriptorPool
= anv_descriptor_pool_to_handle(pool
);
746 void anv_DestroyDescriptorPool(
748 VkDescriptorPool _pool
,
749 const VkAllocationCallbacks
* pAllocator
)
751 ANV_FROM_HANDLE(anv_device
, device
, _device
);
752 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, _pool
);
757 list_for_each_entry_safe(struct anv_descriptor_set
, set
,
758 &pool
->desc_sets
, pool_link
) {
759 anv_descriptor_set_layout_unref(device
, set
->layout
);
763 anv_gem_munmap(pool
->bo
.map
, pool
->bo
.size
);
764 anv_vma_free(device
, &pool
->bo
);
765 anv_gem_close(device
, pool
->bo
.gem_handle
);
766 util_vma_heap_finish(&pool
->bo_heap
);
768 anv_state_stream_finish(&pool
->surface_state_stream
);
770 vk_free2(&device
->alloc
, pAllocator
, pool
);
773 VkResult
anv_ResetDescriptorPool(
775 VkDescriptorPool descriptorPool
,
776 VkDescriptorPoolResetFlags flags
)
778 ANV_FROM_HANDLE(anv_device
, device
, _device
);
779 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
781 list_for_each_entry_safe(struct anv_descriptor_set
, set
,
782 &pool
->desc_sets
, pool_link
) {
783 anv_descriptor_set_layout_unref(device
, set
->layout
);
785 list_inithead(&pool
->desc_sets
);
788 pool
->free_list
= EMPTY
;
791 util_vma_heap_finish(&pool
->bo_heap
);
792 util_vma_heap_init(&pool
->bo_heap
, POOL_HEAP_OFFSET
, pool
->bo
.size
);
795 anv_state_stream_finish(&pool
->surface_state_stream
);
796 anv_state_stream_init(&pool
->surface_state_stream
,
797 &device
->surface_state_pool
, 4096);
798 pool
->surface_state_free_list
= NULL
;
803 struct pool_free_list_entry
{
809 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool
*pool
,
811 struct anv_descriptor_set
**set
)
813 if (size
<= pool
->size
- pool
->next
) {
814 *set
= (struct anv_descriptor_set
*) (pool
->data
+ pool
->next
);
818 struct pool_free_list_entry
*entry
;
819 uint32_t *link
= &pool
->free_list
;
820 for (uint32_t f
= pool
->free_list
; f
!= EMPTY
; f
= entry
->next
) {
821 entry
= (struct pool_free_list_entry
*) (pool
->data
+ f
);
822 if (size
<= entry
->size
) {
824 *set
= (struct anv_descriptor_set
*) entry
;
830 if (pool
->free_list
!= EMPTY
) {
831 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
833 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY
);
839 anv_descriptor_pool_free_set(struct anv_descriptor_pool
*pool
,
840 struct anv_descriptor_set
*set
)
842 /* Put the descriptor set allocation back on the free list. */
843 const uint32_t index
= (char *) set
- pool
->data
;
844 if (index
+ set
->size
== pool
->next
) {
847 struct pool_free_list_entry
*entry
= (struct pool_free_list_entry
*) set
;
848 entry
->next
= pool
->free_list
;
849 entry
->size
= set
->size
;
850 pool
->free_list
= (char *) entry
- pool
->data
;
854 struct surface_state_free_list_entry
{
856 struct anv_state state
;
859 static struct anv_state
860 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool
*pool
)
862 struct surface_state_free_list_entry
*entry
=
863 pool
->surface_state_free_list
;
866 struct anv_state state
= entry
->state
;
867 pool
->surface_state_free_list
= entry
->next
;
868 assert(state
.alloc_size
== 64);
871 return anv_state_stream_alloc(&pool
->surface_state_stream
, 64, 64);
876 anv_descriptor_pool_free_state(struct anv_descriptor_pool
*pool
,
877 struct anv_state state
)
879 /* Put the buffer view surface state back on the free list. */
880 struct surface_state_free_list_entry
*entry
= state
.map
;
881 entry
->next
= pool
->surface_state_free_list
;
882 entry
->state
= state
;
883 pool
->surface_state_free_list
= entry
;
887 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout
*layout
)
890 sizeof(struct anv_descriptor_set
) +
891 layout
->size
* sizeof(struct anv_descriptor
) +
892 layout
->buffer_view_count
* sizeof(struct anv_buffer_view
);
896 anv_descriptor_set_create(struct anv_device
*device
,
897 struct anv_descriptor_pool
*pool
,
898 struct anv_descriptor_set_layout
*layout
,
899 struct anv_descriptor_set
**out_set
)
901 struct anv_descriptor_set
*set
;
902 const size_t size
= anv_descriptor_set_layout_size(layout
);
904 VkResult result
= anv_descriptor_pool_alloc_set(pool
, size
, &set
);
905 if (result
!= VK_SUCCESS
)
908 if (layout
->descriptor_buffer_size
) {
909 /* Align the size to 32 so that alignment gaps don't cause extra holes
910 * in the heap which can lead to bad performance.
912 uint32_t set_buffer_size
= ALIGN(layout
->descriptor_buffer_size
, 32);
913 uint64_t pool_vma_offset
=
914 util_vma_heap_alloc(&pool
->bo_heap
, set_buffer_size
, 32);
915 if (pool_vma_offset
== 0) {
916 anv_descriptor_pool_free_set(pool
, set
);
917 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
919 assert(pool_vma_offset
>= POOL_HEAP_OFFSET
&&
920 pool_vma_offset
- POOL_HEAP_OFFSET
<= INT32_MAX
);
921 set
->desc_mem
.offset
= pool_vma_offset
- POOL_HEAP_OFFSET
;
922 set
->desc_mem
.alloc_size
= set_buffer_size
;
923 set
->desc_mem
.map
= pool
->bo
.map
+ set
->desc_mem
.offset
;
925 set
->desc_surface_state
= anv_descriptor_pool_alloc_state(pool
);
926 anv_fill_buffer_surface_state(device
, set
->desc_surface_state
,
927 ISL_FORMAT_R32G32B32A32_FLOAT
,
928 (struct anv_address
) {
930 .offset
= set
->desc_mem
.offset
,
932 layout
->descriptor_buffer_size
, 1);
934 set
->desc_mem
= ANV_STATE_NULL
;
935 set
->desc_surface_state
= ANV_STATE_NULL
;
939 set
->layout
= layout
;
940 anv_descriptor_set_layout_ref(layout
);
944 (struct anv_buffer_view
*) &set
->descriptors
[layout
->size
];
945 set
->buffer_view_count
= layout
->buffer_view_count
;
947 /* By defining the descriptors to be zero now, we can later verify that
948 * a descriptor has not been populated with user data.
950 memset(set
->descriptors
, 0, sizeof(struct anv_descriptor
) * layout
->size
);
952 /* Go through and fill out immutable samplers if we have any */
953 struct anv_descriptor
*desc
= set
->descriptors
;
954 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
955 if (layout
->binding
[b
].immutable_samplers
) {
956 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++) {
957 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
958 * UpdateDescriptorSets if needed. However, if the descriptor
959 * set has an immutable sampler, UpdateDescriptorSets may never
960 * touch it, so we need to make sure it's 100% valid now.
962 * We don't need to actually provide a sampler because the helper
963 * will always write in the immutable sampler regardless of what
964 * is in the sampler parameter.
966 struct VkDescriptorImageInfo info
= { };
967 anv_descriptor_set_write_image_view(device
, set
, &info
,
968 VK_DESCRIPTOR_TYPE_SAMPLER
,
972 desc
+= layout
->binding
[b
].array_size
;
975 /* Allocate surface state for the buffer views. */
976 for (uint32_t b
= 0; b
< layout
->buffer_view_count
; b
++) {
977 set
->buffer_views
[b
].surface_state
=
978 anv_descriptor_pool_alloc_state(pool
);
981 list_addtail(&set
->pool_link
, &pool
->desc_sets
);
989 anv_descriptor_set_destroy(struct anv_device
*device
,
990 struct anv_descriptor_pool
*pool
,
991 struct anv_descriptor_set
*set
)
993 anv_descriptor_set_layout_unref(device
, set
->layout
);
995 if (set
->desc_mem
.alloc_size
) {
996 util_vma_heap_free(&pool
->bo_heap
,
997 (uint64_t)set
->desc_mem
.offset
+ POOL_HEAP_OFFSET
,
998 set
->desc_mem
.alloc_size
);
999 anv_descriptor_pool_free_state(pool
, set
->desc_surface_state
);
1002 for (uint32_t b
= 0; b
< set
->buffer_view_count
; b
++)
1003 anv_descriptor_pool_free_state(pool
, set
->buffer_views
[b
].surface_state
);
1005 list_del(&set
->pool_link
);
1007 anv_descriptor_pool_free_set(pool
, set
);
1010 VkResult
anv_AllocateDescriptorSets(
1012 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
1013 VkDescriptorSet
* pDescriptorSets
)
1015 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1016 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
1018 VkResult result
= VK_SUCCESS
;
1019 struct anv_descriptor_set
*set
;
1022 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
1023 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
1024 pAllocateInfo
->pSetLayouts
[i
]);
1026 result
= anv_descriptor_set_create(device
, pool
, layout
, &set
);
1027 if (result
!= VK_SUCCESS
)
1030 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
1033 if (result
!= VK_SUCCESS
)
1034 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
1035 i
, pDescriptorSets
);
1040 VkResult
anv_FreeDescriptorSets(
1042 VkDescriptorPool descriptorPool
,
1044 const VkDescriptorSet
* pDescriptorSets
)
1046 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1047 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
1049 for (uint32_t i
= 0; i
< count
; i
++) {
1050 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
1055 anv_descriptor_set_destroy(device
, pool
, set
);
1062 anv_descriptor_set_write_image_param(uint32_t *param_desc_map
,
1063 const struct brw_image_param
*param
)
1065 #define WRITE_PARAM_FIELD(field, FIELD) \
1066 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
1067 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
1069 WRITE_PARAM_FIELD(offset
, OFFSET
);
1070 WRITE_PARAM_FIELD(size
, SIZE
);
1071 WRITE_PARAM_FIELD(stride
, STRIDE
);
1072 WRITE_PARAM_FIELD(tiling
, TILING
);
1073 WRITE_PARAM_FIELD(swizzling
, SWIZZLING
);
1074 WRITE_PARAM_FIELD(size
, SIZE
);
1076 #undef WRITE_PARAM_FIELD
1080 anv_surface_state_to_handle(struct anv_state state
)
1082 /* Bits 31:12 of the bindless surface offset in the extended message
1083 * descriptor is bits 25:6 of the byte-based address.
1085 assert(state
.offset
>= 0);
1086 uint32_t offset
= state
.offset
;
1087 assert((offset
& 0x3f) == 0 && offset
< (1 << 26));
1092 anv_descriptor_set_write_image_view(struct anv_device
*device
,
1093 struct anv_descriptor_set
*set
,
1094 const VkDescriptorImageInfo
* const info
,
1095 VkDescriptorType type
,
1099 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1100 &set
->layout
->binding
[binding
];
1101 struct anv_descriptor
*desc
=
1102 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1103 struct anv_image_view
*image_view
= NULL
;
1104 struct anv_sampler
*sampler
= NULL
;
1106 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1107 * set initialization to set the bindless samplers.
1109 assert(type
== bind_layout
->type
||
1110 type
== VK_DESCRIPTOR_TYPE_SAMPLER
);
1113 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1114 sampler
= anv_sampler_from_handle(info
->sampler
);
1117 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1118 image_view
= anv_image_view_from_handle(info
->imageView
);
1119 sampler
= anv_sampler_from_handle(info
->sampler
);
1122 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1123 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1124 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1125 image_view
= anv_image_view_from_handle(info
->imageView
);
1129 unreachable("invalid descriptor type");
1132 /* If this descriptor has an immutable sampler, we don't want to stomp on
1135 sampler
= bind_layout
->immutable_samplers
?
1136 bind_layout
->immutable_samplers
[element
] :
1139 *desc
= (struct anv_descriptor
) {
1141 .layout
= info
->imageLayout
,
1142 .image_view
= image_view
,
1146 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1147 element
* anv_descriptor_size(bind_layout
);
1149 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1150 struct anv_sampled_image_descriptor desc_data
[3];
1151 memset(desc_data
, 0, sizeof(desc_data
));
1154 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1155 struct anv_surface_state sstate
=
1156 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
1157 image_view
->planes
[p
].general_sampler_surface_state
:
1158 image_view
->planes
[p
].optimal_sampler_surface_state
;
1159 desc_data
[p
].image
= anv_surface_state_to_handle(sstate
.state
);
1164 for (unsigned p
= 0; p
< sampler
->n_planes
; p
++)
1165 desc_data
[p
].sampler
= sampler
->bindless_state
.offset
+ p
* 32;
1168 /* We may have max_plane_count < 0 if this isn't a sampled image but it
1169 * can be no more than the size of our array of handles.
1171 assert(bind_layout
->max_plane_count
<= ARRAY_SIZE(desc_data
));
1172 memcpy(desc_map
, desc_data
,
1173 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1176 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1177 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1178 assert(image_view
->n_planes
== 1);
1179 struct anv_storage_image_descriptor desc_data
= {
1180 .read_write
= anv_surface_state_to_handle(
1181 image_view
->planes
[0].storage_surface_state
.state
),
1182 .write_only
= anv_surface_state_to_handle(
1183 image_view
->planes
[0].writeonly_storage_surface_state
.state
),
1185 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1188 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1189 /* Storage images can only ever have one plane */
1190 assert(image_view
->n_planes
== 1);
1191 const struct brw_image_param
*image_param
=
1192 &image_view
->planes
[0].storage_image_param
;
1194 anv_descriptor_set_write_image_param(desc_map
, image_param
);
1197 if (bind_layout
->data
& ANV_DESCRIPTOR_TEXTURE_SWIZZLE
) {
1198 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
));
1200 struct anv_texture_swizzle_descriptor desc_data
[3];
1201 memset(desc_data
, 0, sizeof(desc_data
));
1203 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1204 desc_data
[p
] = (struct anv_texture_swizzle_descriptor
) {
1206 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.r
,
1207 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.g
,
1208 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.b
,
1209 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.a
,
1213 memcpy(desc_map
, desc_data
,
1214 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1219 anv_descriptor_set_write_buffer_view(struct anv_device
*device
,
1220 struct anv_descriptor_set
*set
,
1221 VkDescriptorType type
,
1222 struct anv_buffer_view
*buffer_view
,
1226 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1227 &set
->layout
->binding
[binding
];
1228 struct anv_descriptor
*desc
=
1229 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1231 assert(type
== bind_layout
->type
);
1233 *desc
= (struct anv_descriptor
) {
1235 .buffer_view
= buffer_view
,
1238 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1239 element
* anv_descriptor_size(bind_layout
);
1241 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1242 struct anv_sampled_image_descriptor desc_data
= {
1243 .image
= anv_surface_state_to_handle(buffer_view
->surface_state
),
1245 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1248 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1249 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1250 struct anv_storage_image_descriptor desc_data
= {
1251 .read_write
= anv_surface_state_to_handle(
1252 buffer_view
->storage_surface_state
),
1253 .write_only
= anv_surface_state_to_handle(
1254 buffer_view
->writeonly_storage_surface_state
),
1256 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1259 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1260 anv_descriptor_set_write_image_param(desc_map
,
1261 &buffer_view
->storage_image_param
);
1266 anv_descriptor_set_write_buffer(struct anv_device
*device
,
1267 struct anv_descriptor_set
*set
,
1268 struct anv_state_stream
*alloc_stream
,
1269 VkDescriptorType type
,
1270 struct anv_buffer
*buffer
,
1273 VkDeviceSize offset
,
1276 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1277 &set
->layout
->binding
[binding
];
1278 struct anv_descriptor
*desc
=
1279 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1281 assert(type
== bind_layout
->type
);
1283 struct anv_address bind_addr
= anv_address_add(buffer
->address
, offset
);
1284 uint64_t bind_range
= anv_buffer_get_range(buffer
, offset
, range
);
1286 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1287 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1288 *desc
= (struct anv_descriptor
) {
1295 assert(bind_layout
->data
& ANV_DESCRIPTOR_BUFFER_VIEW
);
1296 struct anv_buffer_view
*bview
=
1297 &set
->buffer_views
[bind_layout
->buffer_view_index
+ element
];
1299 bview
->format
= anv_isl_format_for_descriptor_type(type
);
1300 bview
->range
= bind_range
;
1301 bview
->address
= bind_addr
;
1303 /* If we're writing descriptors through a push command, we need to
1304 * allocate the surface state from the command buffer. Otherwise it will
1305 * be allocated by the descriptor pool when calling
1306 * vkAllocateDescriptorSets. */
1308 bview
->surface_state
= anv_state_stream_alloc(alloc_stream
, 64, 64);
1310 anv_fill_buffer_surface_state(device
, bview
->surface_state
,
1311 bview
->format
, bind_addr
, bind_range
, 1);
1313 *desc
= (struct anv_descriptor
) {
1315 .buffer_view
= bview
,
1319 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1320 element
* anv_descriptor_size(bind_layout
);
1322 if (bind_layout
->data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
1323 struct anv_address_range_descriptor desc
= {
1324 .address
= anv_address_physical(bind_addr
),
1325 .range
= bind_range
,
1327 memcpy(desc_map
, &desc
, sizeof(desc
));
1332 anv_descriptor_set_write_inline_uniform_data(struct anv_device
*device
,
1333 struct anv_descriptor_set
*set
,
1339 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1340 &set
->layout
->binding
[binding
];
1342 assert(bind_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
);
1344 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
;
1346 memcpy(desc_map
+ offset
, data
, size
);
1349 void anv_UpdateDescriptorSets(
1351 uint32_t descriptorWriteCount
,
1352 const VkWriteDescriptorSet
* pDescriptorWrites
,
1353 uint32_t descriptorCopyCount
,
1354 const VkCopyDescriptorSet
* pDescriptorCopies
)
1356 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1358 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1359 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1360 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
1362 switch (write
->descriptorType
) {
1363 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1364 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1365 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1366 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1367 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1368 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1369 anv_descriptor_set_write_image_view(device
, set
,
1370 write
->pImageInfo
+ j
,
1371 write
->descriptorType
,
1373 write
->dstArrayElement
+ j
);
1377 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1378 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1379 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1380 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1381 write
->pTexelBufferView
[j
]);
1383 anv_descriptor_set_write_buffer_view(device
, set
,
1384 write
->descriptorType
,
1387 write
->dstArrayElement
+ j
);
1391 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1392 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1393 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1394 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1395 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1396 assert(write
->pBufferInfo
[j
].buffer
);
1397 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1400 anv_descriptor_set_write_buffer(device
, set
,
1402 write
->descriptorType
,
1405 write
->dstArrayElement
+ j
,
1406 write
->pBufferInfo
[j
].offset
,
1407 write
->pBufferInfo
[j
].range
);
1411 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
: {
1412 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_write
=
1413 vk_find_struct_const(write
->pNext
,
1414 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
1415 assert(inline_write
->dataSize
== write
->descriptorCount
);
1416 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1418 inline_write
->pData
,
1419 write
->dstArrayElement
,
1420 inline_write
->dataSize
);
1429 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
1430 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
1431 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->srcSet
);
1432 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
1434 const struct anv_descriptor_set_binding_layout
*src_layout
=
1435 &src
->layout
->binding
[copy
->srcBinding
];
1436 struct anv_descriptor
*src_desc
=
1437 &src
->descriptors
[src_layout
->descriptor_index
];
1438 src_desc
+= copy
->srcArrayElement
;
1440 const struct anv_descriptor_set_binding_layout
*dst_layout
=
1441 &dst
->layout
->binding
[copy
->dstBinding
];
1442 struct anv_descriptor
*dst_desc
=
1443 &dst
->descriptors
[dst_layout
->descriptor_index
];
1444 dst_desc
+= copy
->dstArrayElement
;
1446 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
1447 dst_desc
[j
] = src_desc
[j
];
1449 if (src_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
1450 assert(src_layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
1451 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1452 copy
->dstArrayElement
,
1453 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1454 copy
->srcArrayElement
,
1455 copy
->descriptorCount
);
1457 unsigned desc_size
= anv_descriptor_size(src_layout
);
1458 if (desc_size
> 0) {
1459 assert(desc_size
== anv_descriptor_size(dst_layout
));
1460 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1461 copy
->dstArrayElement
* desc_size
,
1462 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1463 copy
->srcArrayElement
* desc_size
,
1464 copy
->descriptorCount
* desc_size
);
1471 * Descriptor update templates.
1475 anv_descriptor_set_write_template(struct anv_device
*device
,
1476 struct anv_descriptor_set
*set
,
1477 struct anv_state_stream
*alloc_stream
,
1478 const struct anv_descriptor_update_template
*template,
1481 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1482 const struct anv_descriptor_template_entry
*entry
=
1483 &template->entries
[i
];
1485 switch (entry
->type
) {
1486 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1487 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1488 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1489 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1490 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1491 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1492 const VkDescriptorImageInfo
*info
=
1493 data
+ entry
->offset
+ j
* entry
->stride
;
1494 anv_descriptor_set_write_image_view(device
, set
,
1497 entry
->array_element
+ j
);
1501 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1502 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1503 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1504 const VkBufferView
*_bview
=
1505 data
+ entry
->offset
+ j
* entry
->stride
;
1506 ANV_FROM_HANDLE(anv_buffer_view
, bview
, *_bview
);
1508 anv_descriptor_set_write_buffer_view(device
, set
,
1512 entry
->array_element
+ j
);
1516 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1517 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1518 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1519 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1520 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1521 const VkDescriptorBufferInfo
*info
=
1522 data
+ entry
->offset
+ j
* entry
->stride
;
1523 ANV_FROM_HANDLE(anv_buffer
, buffer
, info
->buffer
);
1525 anv_descriptor_set_write_buffer(device
, set
,
1530 entry
->array_element
+ j
,
1531 info
->offset
, info
->range
);
1535 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
1536 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1538 data
+ entry
->offset
,
1539 entry
->array_element
,
1540 entry
->array_count
);
1549 VkResult
anv_CreateDescriptorUpdateTemplate(
1551 const VkDescriptorUpdateTemplateCreateInfo
* pCreateInfo
,
1552 const VkAllocationCallbacks
* pAllocator
,
1553 VkDescriptorUpdateTemplate
* pDescriptorUpdateTemplate
)
1555 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1556 struct anv_descriptor_update_template
*template;
1558 size_t size
= sizeof(*template) +
1559 pCreateInfo
->descriptorUpdateEntryCount
* sizeof(template->entries
[0]);
1560 template = vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1561 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1562 if (template == NULL
)
1563 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1565 template->bind_point
= pCreateInfo
->pipelineBindPoint
;
1567 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
)
1568 template->set
= pCreateInfo
->set
;
1570 template->entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1571 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1572 const VkDescriptorUpdateTemplateEntry
*pEntry
=
1573 &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1575 template->entries
[i
] = (struct anv_descriptor_template_entry
) {
1576 .type
= pEntry
->descriptorType
,
1577 .binding
= pEntry
->dstBinding
,
1578 .array_element
= pEntry
->dstArrayElement
,
1579 .array_count
= pEntry
->descriptorCount
,
1580 .offset
= pEntry
->offset
,
1581 .stride
= pEntry
->stride
,
1585 *pDescriptorUpdateTemplate
=
1586 anv_descriptor_update_template_to_handle(template);
1591 void anv_DestroyDescriptorUpdateTemplate(
1593 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1594 const VkAllocationCallbacks
* pAllocator
)
1596 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1597 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1598 descriptorUpdateTemplate
);
1600 vk_free2(&device
->alloc
, pAllocator
, template);
1603 void anv_UpdateDescriptorSetWithTemplate(
1605 VkDescriptorSet descriptorSet
,
1606 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1609 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1610 ANV_FROM_HANDLE(anv_descriptor_set
, set
, descriptorSet
);
1611 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1612 descriptorUpdateTemplate
);
1614 anv_descriptor_set_write_template(device
, set
, NULL
, template, pData
);