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
);
142 /** Returns the size in bytes of each descriptor with the given layout */
144 anv_descriptor_size(const struct anv_descriptor_set_binding_layout
*layout
)
146 if (layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
147 assert(layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
148 return layout
->array_size
;
151 unsigned size
= anv_descriptor_data_size(layout
->data
);
153 /* For multi-planar bindings, we make every descriptor consume the maximum
154 * number of planes so we don't have to bother with walking arrays and
155 * adding things up every time. Fortunately, YCbCr samplers aren't all
156 * that common and likely won't be in the middle of big arrays.
158 if (layout
->max_plane_count
> 1)
159 size
*= layout
->max_plane_count
;
164 /** Returns the size in bytes of each descriptor of the given type
166 * This version of the function does not have access to the entire layout so
167 * it may only work on certain descriptor types where the descriptor size is
168 * entirely determined by the descriptor type. Whenever possible, code should
169 * use anv_descriptor_size() instead.
172 anv_descriptor_type_size(const struct anv_physical_device
*pdevice
,
173 VkDescriptorType type
)
175 assert(type
!= VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
&&
176 type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
177 type
!= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
&&
178 type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
);
180 return anv_descriptor_data_size(anv_descriptor_data_for_type(pdevice
, type
));
184 anv_descriptor_data_supports_bindless(const struct anv_physical_device
*pdevice
,
185 enum anv_descriptor_data data
,
188 if (data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
189 assert(pdevice
->has_a64_buffer_access
);
193 if (data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
194 assert(pdevice
->has_bindless_images
|| pdevice
->has_bindless_samplers
);
195 return sampler
? pdevice
->has_bindless_samplers
:
196 pdevice
->has_bindless_images
;
199 if (data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
200 assert(pdevice
->has_bindless_images
);
208 anv_descriptor_supports_bindless(const struct anv_physical_device
*pdevice
,
209 const struct anv_descriptor_set_binding_layout
*binding
,
212 return anv_descriptor_data_supports_bindless(pdevice
, binding
->data
,
217 anv_descriptor_requires_bindless(const struct anv_physical_device
*pdevice
,
218 const struct anv_descriptor_set_binding_layout
*binding
,
221 if (pdevice
->always_use_bindless
)
222 return anv_descriptor_supports_bindless(pdevice
, binding
, sampler
);
224 static const VkDescriptorBindingFlagBitsEXT flags_requiring_bindless
=
225 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT
|
226 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT
|
227 VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT
;
229 return (binding
->flags
& flags_requiring_bindless
) != 0;
232 void anv_GetDescriptorSetLayoutSupport(
234 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
235 VkDescriptorSetLayoutSupport
* pSupport
)
237 ANV_FROM_HANDLE(anv_device
, device
, _device
);
238 const struct anv_physical_device
*pdevice
=
239 &device
->instance
->physicalDevice
;
241 uint32_t surface_count
[MESA_SHADER_STAGES
] = { 0, };
243 for (uint32_t b
= 0; b
< pCreateInfo
->bindingCount
; b
++) {
244 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[b
];
246 enum anv_descriptor_data desc_data
=
247 anv_descriptor_data_for_type(pdevice
, binding
->descriptorType
);
249 switch (binding
->descriptorType
) {
250 case VK_DESCRIPTOR_TYPE_SAMPLER
:
251 /* There is no real limit on samplers */
254 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
255 if (anv_descriptor_data_supports_bindless(pdevice
, desc_data
, false))
258 if (binding
->pImmutableSamplers
) {
259 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
260 ANV_FROM_HANDLE(anv_sampler
, sampler
,
261 binding
->pImmutableSamplers
[i
]);
262 anv_foreach_stage(s
, binding
->stageFlags
)
263 surface_count
[s
] += sampler
->n_planes
;
266 anv_foreach_stage(s
, binding
->stageFlags
)
267 surface_count
[s
] += binding
->descriptorCount
;
272 if (anv_descriptor_data_supports_bindless(pdevice
, desc_data
, false))
275 anv_foreach_stage(s
, binding
->stageFlags
)
276 surface_count
[s
] += binding
->descriptorCount
;
281 bool supported
= true;
282 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
283 /* Our maximum binding table size is 240 and we need to reserve 8 for
286 if (surface_count
[s
] >= MAX_BINDING_TABLE_SIZE
- MAX_RTS
)
290 pSupport
->supported
= supported
;
293 VkResult
anv_CreateDescriptorSetLayout(
295 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
296 const VkAllocationCallbacks
* pAllocator
,
297 VkDescriptorSetLayout
* pSetLayout
)
299 ANV_FROM_HANDLE(anv_device
, device
, _device
);
301 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
303 uint32_t max_binding
= 0;
304 uint32_t immutable_sampler_count
= 0;
305 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
306 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
308 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
310 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
311 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
312 * pImmutableSamplers can be used to initialize a set of immutable
313 * samplers. [...] If descriptorType is not one of these descriptor
314 * types, then pImmutableSamplers is ignored.
316 * We need to be careful here and only parse pImmutableSamplers if we
317 * have one of the right descriptor types.
319 VkDescriptorType desc_type
= pCreateInfo
->pBindings
[j
].descriptorType
;
320 if ((desc_type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
321 desc_type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
) &&
322 pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
323 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
326 struct anv_descriptor_set_layout
*set_layout
;
327 struct anv_descriptor_set_binding_layout
*bindings
;
328 struct anv_sampler
**samplers
;
330 /* We need to allocate decriptor set layouts off the device allocator
331 * with DEVICE scope because they are reference counted and may not be
332 * destroyed when vkDestroyDescriptorSetLayout is called.
335 anv_multialloc_add(&ma
, &set_layout
, 1);
336 anv_multialloc_add(&ma
, &bindings
, max_binding
+ 1);
337 anv_multialloc_add(&ma
, &samplers
, immutable_sampler_count
);
339 if (!anv_multialloc_alloc(&ma
, &device
->alloc
,
340 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
))
341 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
343 memset(set_layout
, 0, sizeof(*set_layout
));
344 set_layout
->ref_cnt
= 1;
345 set_layout
->binding_count
= max_binding
+ 1;
347 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
348 /* Initialize all binding_layout entries to -1 */
349 memset(&set_layout
->binding
[b
], -1, sizeof(set_layout
->binding
[b
]));
351 set_layout
->binding
[b
].flags
= 0;
352 set_layout
->binding
[b
].data
= 0;
353 set_layout
->binding
[b
].max_plane_count
= 0;
354 set_layout
->binding
[b
].array_size
= 0;
355 set_layout
->binding
[b
].immutable_samplers
= NULL
;
358 /* Initialize all samplers to 0 */
359 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
361 uint32_t buffer_view_count
= 0;
362 uint32_t dynamic_offset_count
= 0;
363 uint32_t descriptor_buffer_size
= 0;
365 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
366 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
367 uint32_t b
= binding
->binding
;
368 /* We temporarily store pCreateInfo->pBindings[] index (plus one) in the
369 * immutable_samplers pointer. This provides us with a quick-and-dirty
370 * way to sort the bindings by binding number.
372 set_layout
->binding
[b
].immutable_samplers
= (void *)(uintptr_t)(j
+ 1);
375 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*binding_flags_info
=
376 vk_find_struct_const(pCreateInfo
->pNext
,
377 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
379 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
380 /* We stashed the pCreateInfo->pBindings[] index (plus one) in the
381 * immutable_samplers pointer. Check for NULL (empty binding) and then
382 * reset it and compute the index.
384 if (set_layout
->binding
[b
].immutable_samplers
== NULL
)
386 const uint32_t info_idx
=
387 (uintptr_t)(void *)set_layout
->binding
[b
].immutable_samplers
- 1;
388 set_layout
->binding
[b
].immutable_samplers
= NULL
;
390 const VkDescriptorSetLayoutBinding
*binding
=
391 &pCreateInfo
->pBindings
[info_idx
];
393 if (binding
->descriptorCount
== 0)
397 set_layout
->binding
[b
].type
= binding
->descriptorType
;
400 if (binding_flags_info
&& binding_flags_info
->bindingCount
> 0) {
401 assert(binding_flags_info
->bindingCount
== pCreateInfo
->bindingCount
);
402 set_layout
->binding
[b
].flags
=
403 binding_flags_info
->pBindingFlags
[info_idx
];
406 set_layout
->binding
[b
].data
=
407 anv_descriptor_data_for_type(&device
->instance
->physicalDevice
,
408 binding
->descriptorType
);
409 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
410 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
411 set_layout
->size
+= binding
->descriptorCount
;
413 if (set_layout
->binding
[b
].data
& ANV_DESCRIPTOR_BUFFER_VIEW
) {
414 set_layout
->binding
[b
].buffer_view_index
= buffer_view_count
;
415 buffer_view_count
+= binding
->descriptorCount
;
418 switch (binding
->descriptorType
) {
419 case VK_DESCRIPTOR_TYPE_SAMPLER
:
420 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
421 set_layout
->binding
[b
].max_plane_count
= 1;
422 if (binding
->pImmutableSamplers
) {
423 set_layout
->binding
[b
].immutable_samplers
= samplers
;
424 samplers
+= binding
->descriptorCount
;
426 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
427 ANV_FROM_HANDLE(anv_sampler
, sampler
,
428 binding
->pImmutableSamplers
[i
]);
430 set_layout
->binding
[b
].immutable_samplers
[i
] = sampler
;
431 if (set_layout
->binding
[b
].max_plane_count
< sampler
->n_planes
)
432 set_layout
->binding
[b
].max_plane_count
= sampler
->n_planes
;
437 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
438 set_layout
->binding
[b
].max_plane_count
= 1;
445 switch (binding
->descriptorType
) {
446 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
447 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
448 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
449 dynamic_offset_count
+= binding
->descriptorCount
;
456 if (binding
->descriptorType
==
457 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
458 /* Inline uniform blocks are specified to use the descriptor array
459 * size as the size in bytes of the block.
461 descriptor_buffer_size
= align_u32(descriptor_buffer_size
, 32);
462 set_layout
->binding
[b
].descriptor_offset
= descriptor_buffer_size
;
463 descriptor_buffer_size
+= binding
->descriptorCount
;
465 set_layout
->binding
[b
].descriptor_offset
= descriptor_buffer_size
;
466 descriptor_buffer_size
+= anv_descriptor_size(&set_layout
->binding
[b
]) *
467 binding
->descriptorCount
;
470 set_layout
->shader_stages
|= binding
->stageFlags
;
473 set_layout
->buffer_view_count
= buffer_view_count
;
474 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
475 set_layout
->descriptor_buffer_size
= descriptor_buffer_size
;
477 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
482 void anv_DestroyDescriptorSetLayout(
484 VkDescriptorSetLayout _set_layout
,
485 const VkAllocationCallbacks
* pAllocator
)
487 ANV_FROM_HANDLE(anv_device
, device
, _device
);
488 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
493 anv_descriptor_set_layout_unref(device
, set_layout
);
496 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
499 sha1_update_immutable_sampler(struct mesa_sha1
*ctx
,
500 const struct anv_sampler
*sampler
)
502 if (!sampler
->conversion
)
505 /* The only thing that affects the shader is ycbcr conversion */
506 _mesa_sha1_update(ctx
, sampler
->conversion
,
507 sizeof(*sampler
->conversion
));
511 sha1_update_descriptor_set_binding_layout(struct mesa_sha1
*ctx
,
512 const struct anv_descriptor_set_binding_layout
*layout
)
514 SHA1_UPDATE_VALUE(ctx
, layout
->flags
);
515 SHA1_UPDATE_VALUE(ctx
, layout
->data
);
516 SHA1_UPDATE_VALUE(ctx
, layout
->max_plane_count
);
517 SHA1_UPDATE_VALUE(ctx
, layout
->array_size
);
518 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_index
);
519 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_index
);
520 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_index
);
521 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_offset
);
523 if (layout
->immutable_samplers
) {
524 for (uint16_t i
= 0; i
< layout
->array_size
; i
++)
525 sha1_update_immutable_sampler(ctx
, layout
->immutable_samplers
[i
]);
530 sha1_update_descriptor_set_layout(struct mesa_sha1
*ctx
,
531 const struct anv_descriptor_set_layout
*layout
)
533 SHA1_UPDATE_VALUE(ctx
, layout
->binding_count
);
534 SHA1_UPDATE_VALUE(ctx
, layout
->size
);
535 SHA1_UPDATE_VALUE(ctx
, layout
->shader_stages
);
536 SHA1_UPDATE_VALUE(ctx
, layout
->buffer_view_count
);
537 SHA1_UPDATE_VALUE(ctx
, layout
->dynamic_offset_count
);
538 SHA1_UPDATE_VALUE(ctx
, layout
->descriptor_buffer_size
);
540 for (uint16_t i
= 0; i
< layout
->binding_count
; i
++)
541 sha1_update_descriptor_set_binding_layout(ctx
, &layout
->binding
[i
]);
545 * Pipeline layouts. These have nothing to do with the pipeline. They are
546 * just multiple descriptor set layouts pasted together
549 VkResult
anv_CreatePipelineLayout(
551 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
552 const VkAllocationCallbacks
* pAllocator
,
553 VkPipelineLayout
* pPipelineLayout
)
555 ANV_FROM_HANDLE(anv_device
, device
, _device
);
556 struct anv_pipeline_layout
*layout
;
558 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
560 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
561 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
563 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
565 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
567 unsigned dynamic_offset_count
= 0;
569 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
570 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
571 pCreateInfo
->pSetLayouts
[set
]);
572 layout
->set
[set
].layout
= set_layout
;
573 anv_descriptor_set_layout_ref(set_layout
);
575 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
576 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
577 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
580 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
584 struct mesa_sha1 ctx
;
585 _mesa_sha1_init(&ctx
);
586 for (unsigned s
= 0; s
< layout
->num_sets
; s
++) {
587 sha1_update_descriptor_set_layout(&ctx
, layout
->set
[s
].layout
);
588 _mesa_sha1_update(&ctx
, &layout
->set
[s
].dynamic_offset_start
,
589 sizeof(layout
->set
[s
].dynamic_offset_start
));
591 _mesa_sha1_update(&ctx
, &layout
->num_sets
, sizeof(layout
->num_sets
));
592 _mesa_sha1_final(&ctx
, layout
->sha1
);
594 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
599 void anv_DestroyPipelineLayout(
601 VkPipelineLayout _pipelineLayout
,
602 const VkAllocationCallbacks
* pAllocator
)
604 ANV_FROM_HANDLE(anv_device
, device
, _device
);
605 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
607 if (!pipeline_layout
)
610 for (uint32_t i
= 0; i
< pipeline_layout
->num_sets
; i
++)
611 anv_descriptor_set_layout_unref(device
, pipeline_layout
->set
[i
].layout
);
613 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
619 * These are implemented using a big pool of memory and a free-list for the
620 * host memory allocations and a state_stream and a free list for the buffer
621 * view surface state. The spec allows us to fail to allocate due to
622 * fragmentation in all cases but two: 1) after pool reset, allocating up
623 * until the pool size with no freeing must succeed and 2) allocating and
624 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
625 * and the free lists lets us recycle blocks for case 2).
628 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
629 * ensure we can allocate the entire BO without hitting zero. The actual
630 * amount doesn't matter.
632 #define POOL_HEAP_OFFSET 64
636 VkResult
anv_CreateDescriptorPool(
638 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
639 const VkAllocationCallbacks
* pAllocator
,
640 VkDescriptorPool
* pDescriptorPool
)
642 ANV_FROM_HANDLE(anv_device
, device
, _device
);
643 struct anv_descriptor_pool
*pool
;
645 const VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*inline_info
=
646 vk_find_struct_const(pCreateInfo
->pNext
,
647 DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
);
649 uint32_t descriptor_count
= 0;
650 uint32_t buffer_view_count
= 0;
651 uint32_t descriptor_bo_size
= 0;
652 for (uint32_t i
= 0; i
< pCreateInfo
->poolSizeCount
; i
++) {
653 enum anv_descriptor_data desc_data
=
654 anv_descriptor_data_for_type(&device
->instance
->physicalDevice
,
655 pCreateInfo
->pPoolSizes
[i
].type
);
657 if (desc_data
& ANV_DESCRIPTOR_BUFFER_VIEW
)
658 buffer_view_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
660 unsigned desc_data_size
= anv_descriptor_data_size(desc_data
) *
661 pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
663 /* Combined image sampler descriptors can take up to 3 slots if they
664 * hold a YCbCr image.
666 if (pCreateInfo
->pPoolSizes
[i
].type
==
667 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
670 if (pCreateInfo
->pPoolSizes
[i
].type
==
671 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
672 /* Inline uniform blocks are specified to use the descriptor array
673 * size as the size in bytes of the block.
676 desc_data_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
679 descriptor_bo_size
+= desc_data_size
;
681 descriptor_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
683 /* We have to align descriptor buffer allocations to 32B so that we can
684 * push descriptor buffers. This means that each descriptor buffer
685 * allocated may burn up to 32B of extra space to get the right alignment.
686 * (Technically, it's at most 28B because we're always going to start at
687 * least 4B aligned but we're being conservative here.) Allocate enough
688 * extra space that we can chop it into maxSets pieces and align each one
691 descriptor_bo_size
+= 32 * pCreateInfo
->maxSets
;
692 /* We align inline uniform blocks to 32B */
694 descriptor_bo_size
+= 32 * inline_info
->maxInlineUniformBlockBindings
;
695 descriptor_bo_size
= ALIGN(descriptor_bo_size
, 4096);
697 const size_t pool_size
=
698 pCreateInfo
->maxSets
* sizeof(struct anv_descriptor_set
) +
699 descriptor_count
* sizeof(struct anv_descriptor
) +
700 buffer_view_count
* sizeof(struct anv_buffer_view
);
701 const size_t total_size
= sizeof(*pool
) + pool_size
;
703 pool
= vk_alloc2(&device
->alloc
, pAllocator
, total_size
, 8,
704 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
706 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
708 pool
->size
= pool_size
;
710 pool
->free_list
= EMPTY
;
712 if (descriptor_bo_size
> 0) {
713 VkResult result
= anv_bo_init_new(&pool
->bo
, device
, descriptor_bo_size
);
714 if (result
!= VK_SUCCESS
) {
715 vk_free2(&device
->alloc
, pAllocator
, pool
);
719 anv_gem_set_caching(device
, pool
->bo
.gem_handle
, I915_CACHING_CACHED
);
721 pool
->bo
.map
= anv_gem_mmap(device
, pool
->bo
.gem_handle
, 0,
722 descriptor_bo_size
, 0);
723 if (pool
->bo
.map
== NULL
) {
724 anv_gem_close(device
, pool
->bo
.gem_handle
);
725 vk_free2(&device
->alloc
, pAllocator
, pool
);
726 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
729 if (device
->instance
->physicalDevice
.use_softpin
) {
730 pool
->bo
.flags
|= EXEC_OBJECT_PINNED
;
731 anv_vma_alloc(device
, &pool
->bo
);
734 util_vma_heap_init(&pool
->bo_heap
, POOL_HEAP_OFFSET
, descriptor_bo_size
);
739 anv_state_stream_init(&pool
->surface_state_stream
,
740 &device
->surface_state_pool
, 4096);
741 pool
->surface_state_free_list
= NULL
;
743 list_inithead(&pool
->desc_sets
);
745 *pDescriptorPool
= anv_descriptor_pool_to_handle(pool
);
750 void anv_DestroyDescriptorPool(
752 VkDescriptorPool _pool
,
753 const VkAllocationCallbacks
* pAllocator
)
755 ANV_FROM_HANDLE(anv_device
, device
, _device
);
756 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, _pool
);
761 list_for_each_entry_safe(struct anv_descriptor_set
, set
,
762 &pool
->desc_sets
, pool_link
) {
763 anv_descriptor_set_layout_unref(device
, set
->layout
);
767 anv_gem_munmap(pool
->bo
.map
, pool
->bo
.size
);
768 anv_vma_free(device
, &pool
->bo
);
769 anv_gem_close(device
, pool
->bo
.gem_handle
);
770 util_vma_heap_finish(&pool
->bo_heap
);
772 anv_state_stream_finish(&pool
->surface_state_stream
);
774 vk_free2(&device
->alloc
, pAllocator
, pool
);
777 VkResult
anv_ResetDescriptorPool(
779 VkDescriptorPool descriptorPool
,
780 VkDescriptorPoolResetFlags flags
)
782 ANV_FROM_HANDLE(anv_device
, device
, _device
);
783 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
785 list_for_each_entry_safe(struct anv_descriptor_set
, set
,
786 &pool
->desc_sets
, pool_link
) {
787 anv_descriptor_set_layout_unref(device
, set
->layout
);
789 list_inithead(&pool
->desc_sets
);
792 pool
->free_list
= EMPTY
;
795 util_vma_heap_finish(&pool
->bo_heap
);
796 util_vma_heap_init(&pool
->bo_heap
, POOL_HEAP_OFFSET
, pool
->bo
.size
);
799 anv_state_stream_finish(&pool
->surface_state_stream
);
800 anv_state_stream_init(&pool
->surface_state_stream
,
801 &device
->surface_state_pool
, 4096);
802 pool
->surface_state_free_list
= NULL
;
807 struct pool_free_list_entry
{
813 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool
*pool
,
815 struct anv_descriptor_set
**set
)
817 if (size
<= pool
->size
- pool
->next
) {
818 *set
= (struct anv_descriptor_set
*) (pool
->data
+ pool
->next
);
822 struct pool_free_list_entry
*entry
;
823 uint32_t *link
= &pool
->free_list
;
824 for (uint32_t f
= pool
->free_list
; f
!= EMPTY
; f
= entry
->next
) {
825 entry
= (struct pool_free_list_entry
*) (pool
->data
+ f
);
826 if (size
<= entry
->size
) {
828 *set
= (struct anv_descriptor_set
*) entry
;
834 if (pool
->free_list
!= EMPTY
) {
835 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
837 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY
);
843 anv_descriptor_pool_free_set(struct anv_descriptor_pool
*pool
,
844 struct anv_descriptor_set
*set
)
846 /* Put the descriptor set allocation back on the free list. */
847 const uint32_t index
= (char *) set
- pool
->data
;
848 if (index
+ set
->size
== pool
->next
) {
851 struct pool_free_list_entry
*entry
= (struct pool_free_list_entry
*) set
;
852 entry
->next
= pool
->free_list
;
853 entry
->size
= set
->size
;
854 pool
->free_list
= (char *) entry
- pool
->data
;
858 struct surface_state_free_list_entry
{
860 struct anv_state state
;
863 static struct anv_state
864 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool
*pool
)
866 struct surface_state_free_list_entry
*entry
=
867 pool
->surface_state_free_list
;
870 struct anv_state state
= entry
->state
;
871 pool
->surface_state_free_list
= entry
->next
;
872 assert(state
.alloc_size
== 64);
875 return anv_state_stream_alloc(&pool
->surface_state_stream
, 64, 64);
880 anv_descriptor_pool_free_state(struct anv_descriptor_pool
*pool
,
881 struct anv_state state
)
883 /* Put the buffer view surface state back on the free list. */
884 struct surface_state_free_list_entry
*entry
= state
.map
;
885 entry
->next
= pool
->surface_state_free_list
;
886 entry
->state
= state
;
887 pool
->surface_state_free_list
= entry
;
891 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout
*layout
)
894 sizeof(struct anv_descriptor_set
) +
895 layout
->size
* sizeof(struct anv_descriptor
) +
896 layout
->buffer_view_count
* sizeof(struct anv_buffer_view
);
900 anv_descriptor_set_create(struct anv_device
*device
,
901 struct anv_descriptor_pool
*pool
,
902 struct anv_descriptor_set_layout
*layout
,
903 struct anv_descriptor_set
**out_set
)
905 struct anv_descriptor_set
*set
;
906 const size_t size
= anv_descriptor_set_layout_size(layout
);
908 VkResult result
= anv_descriptor_pool_alloc_set(pool
, size
, &set
);
909 if (result
!= VK_SUCCESS
)
912 if (layout
->descriptor_buffer_size
) {
913 /* Align the size to 32 so that alignment gaps don't cause extra holes
914 * in the heap which can lead to bad performance.
916 uint32_t set_buffer_size
= ALIGN(layout
->descriptor_buffer_size
, 32);
917 uint64_t pool_vma_offset
=
918 util_vma_heap_alloc(&pool
->bo_heap
, set_buffer_size
, 32);
919 if (pool_vma_offset
== 0) {
920 anv_descriptor_pool_free_set(pool
, set
);
921 return vk_error(VK_ERROR_FRAGMENTED_POOL
);
923 assert(pool_vma_offset
>= POOL_HEAP_OFFSET
&&
924 pool_vma_offset
- POOL_HEAP_OFFSET
<= INT32_MAX
);
925 set
->desc_mem
.offset
= pool_vma_offset
- POOL_HEAP_OFFSET
;
926 set
->desc_mem
.alloc_size
= set_buffer_size
;
927 set
->desc_mem
.map
= pool
->bo
.map
+ set
->desc_mem
.offset
;
929 set
->desc_surface_state
= anv_descriptor_pool_alloc_state(pool
);
930 anv_fill_buffer_surface_state(device
, set
->desc_surface_state
,
931 ISL_FORMAT_R32G32B32A32_FLOAT
,
932 (struct anv_address
) {
934 .offset
= set
->desc_mem
.offset
,
936 layout
->descriptor_buffer_size
, 1);
938 set
->desc_mem
= ANV_STATE_NULL
;
939 set
->desc_surface_state
= ANV_STATE_NULL
;
943 set
->layout
= layout
;
944 anv_descriptor_set_layout_ref(layout
);
948 (struct anv_buffer_view
*) &set
->descriptors
[layout
->size
];
949 set
->buffer_view_count
= layout
->buffer_view_count
;
951 /* By defining the descriptors to be zero now, we can later verify that
952 * a descriptor has not been populated with user data.
954 memset(set
->descriptors
, 0, sizeof(struct anv_descriptor
) * layout
->size
);
956 /* Go through and fill out immutable samplers if we have any */
957 struct anv_descriptor
*desc
= set
->descriptors
;
958 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
959 if (layout
->binding
[b
].immutable_samplers
) {
960 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++) {
961 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
962 * UpdateDescriptorSets if needed. However, if the descriptor
963 * set has an immutable sampler, UpdateDescriptorSets may never
964 * touch it, so we need to make sure it's 100% valid now.
966 * We don't need to actually provide a sampler because the helper
967 * will always write in the immutable sampler regardless of what
968 * is in the sampler parameter.
970 struct VkDescriptorImageInfo info
= { };
971 anv_descriptor_set_write_image_view(device
, set
, &info
,
972 VK_DESCRIPTOR_TYPE_SAMPLER
,
976 desc
+= layout
->binding
[b
].array_size
;
979 /* Allocate surface state for the buffer views. */
980 for (uint32_t b
= 0; b
< layout
->buffer_view_count
; b
++) {
981 set
->buffer_views
[b
].surface_state
=
982 anv_descriptor_pool_alloc_state(pool
);
985 list_addtail(&set
->pool_link
, &pool
->desc_sets
);
993 anv_descriptor_set_destroy(struct anv_device
*device
,
994 struct anv_descriptor_pool
*pool
,
995 struct anv_descriptor_set
*set
)
997 anv_descriptor_set_layout_unref(device
, set
->layout
);
999 if (set
->desc_mem
.alloc_size
) {
1000 util_vma_heap_free(&pool
->bo_heap
,
1001 (uint64_t)set
->desc_mem
.offset
+ POOL_HEAP_OFFSET
,
1002 set
->desc_mem
.alloc_size
);
1003 anv_descriptor_pool_free_state(pool
, set
->desc_surface_state
);
1006 for (uint32_t b
= 0; b
< set
->buffer_view_count
; b
++)
1007 anv_descriptor_pool_free_state(pool
, set
->buffer_views
[b
].surface_state
);
1009 list_del(&set
->pool_link
);
1011 anv_descriptor_pool_free_set(pool
, set
);
1014 VkResult
anv_AllocateDescriptorSets(
1016 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
1017 VkDescriptorSet
* pDescriptorSets
)
1019 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1020 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
1022 VkResult result
= VK_SUCCESS
;
1023 struct anv_descriptor_set
*set
;
1026 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
1027 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
1028 pAllocateInfo
->pSetLayouts
[i
]);
1030 result
= anv_descriptor_set_create(device
, pool
, layout
, &set
);
1031 if (result
!= VK_SUCCESS
)
1034 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
1037 if (result
!= VK_SUCCESS
)
1038 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
1039 i
, pDescriptorSets
);
1044 VkResult
anv_FreeDescriptorSets(
1046 VkDescriptorPool descriptorPool
,
1048 const VkDescriptorSet
* pDescriptorSets
)
1050 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1051 ANV_FROM_HANDLE(anv_descriptor_pool
, pool
, descriptorPool
);
1053 for (uint32_t i
= 0; i
< count
; i
++) {
1054 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
1059 anv_descriptor_set_destroy(device
, pool
, set
);
1066 anv_descriptor_set_write_image_param(uint32_t *param_desc_map
,
1067 const struct brw_image_param
*param
)
1069 #define WRITE_PARAM_FIELD(field, FIELD) \
1070 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
1071 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
1073 WRITE_PARAM_FIELD(offset
, OFFSET
);
1074 WRITE_PARAM_FIELD(size
, SIZE
);
1075 WRITE_PARAM_FIELD(stride
, STRIDE
);
1076 WRITE_PARAM_FIELD(tiling
, TILING
);
1077 WRITE_PARAM_FIELD(swizzling
, SWIZZLING
);
1078 WRITE_PARAM_FIELD(size
, SIZE
);
1080 #undef WRITE_PARAM_FIELD
1084 anv_surface_state_to_handle(struct anv_state state
)
1086 /* Bits 31:12 of the bindless surface offset in the extended message
1087 * descriptor is bits 25:6 of the byte-based address.
1089 assert(state
.offset
>= 0);
1090 uint32_t offset
= state
.offset
;
1091 assert((offset
& 0x3f) == 0 && offset
< (1 << 26));
1096 anv_descriptor_set_write_image_view(struct anv_device
*device
,
1097 struct anv_descriptor_set
*set
,
1098 const VkDescriptorImageInfo
* const info
,
1099 VkDescriptorType type
,
1103 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1104 &set
->layout
->binding
[binding
];
1105 struct anv_descriptor
*desc
=
1106 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1107 struct anv_image_view
*image_view
= NULL
;
1108 struct anv_sampler
*sampler
= NULL
;
1110 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1111 * set initialization to set the bindless samplers.
1113 assert(type
== bind_layout
->type
||
1114 type
== VK_DESCRIPTOR_TYPE_SAMPLER
);
1117 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1118 sampler
= anv_sampler_from_handle(info
->sampler
);
1121 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1122 image_view
= anv_image_view_from_handle(info
->imageView
);
1123 sampler
= anv_sampler_from_handle(info
->sampler
);
1126 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1127 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1128 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1129 image_view
= anv_image_view_from_handle(info
->imageView
);
1133 unreachable("invalid descriptor type");
1136 /* If this descriptor has an immutable sampler, we don't want to stomp on
1139 sampler
= bind_layout
->immutable_samplers
?
1140 bind_layout
->immutable_samplers
[element
] :
1143 *desc
= (struct anv_descriptor
) {
1145 .layout
= info
->imageLayout
,
1146 .image_view
= image_view
,
1150 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1151 element
* anv_descriptor_size(bind_layout
);
1153 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1154 struct anv_sampled_image_descriptor desc_data
[3];
1155 memset(desc_data
, 0, sizeof(desc_data
));
1158 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1159 struct anv_surface_state sstate
=
1160 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
1161 image_view
->planes
[p
].general_sampler_surface_state
:
1162 image_view
->planes
[p
].optimal_sampler_surface_state
;
1163 desc_data
[p
].image
= anv_surface_state_to_handle(sstate
.state
);
1168 for (unsigned p
= 0; p
< sampler
->n_planes
; p
++)
1169 desc_data
[p
].sampler
= sampler
->bindless_state
.offset
+ p
* 32;
1172 /* We may have max_plane_count < 0 if this isn't a sampled image but it
1173 * can be no more than the size of our array of handles.
1175 assert(bind_layout
->max_plane_count
<= ARRAY_SIZE(desc_data
));
1176 memcpy(desc_map
, desc_data
,
1177 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1180 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1181 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1182 assert(image_view
->n_planes
== 1);
1183 struct anv_storage_image_descriptor desc_data
= {
1184 .read_write
= anv_surface_state_to_handle(
1185 image_view
->planes
[0].storage_surface_state
.state
),
1186 .write_only
= anv_surface_state_to_handle(
1187 image_view
->planes
[0].writeonly_storage_surface_state
.state
),
1189 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1192 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1193 /* Storage images can only ever have one plane */
1194 assert(image_view
->n_planes
== 1);
1195 const struct brw_image_param
*image_param
=
1196 &image_view
->planes
[0].storage_image_param
;
1198 anv_descriptor_set_write_image_param(desc_map
, image_param
);
1201 if (image_view
&& (bind_layout
->data
& ANV_DESCRIPTOR_TEXTURE_SWIZZLE
)) {
1202 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
));
1204 struct anv_texture_swizzle_descriptor desc_data
[3];
1205 memset(desc_data
, 0, sizeof(desc_data
));
1207 for (unsigned p
= 0; p
< image_view
->n_planes
; p
++) {
1208 desc_data
[p
] = (struct anv_texture_swizzle_descriptor
) {
1210 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.r
,
1211 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.g
,
1212 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.b
,
1213 (uint8_t)image_view
->planes
[p
].isl
.swizzle
.a
,
1217 memcpy(desc_map
, desc_data
,
1218 MAX2(1, bind_layout
->max_plane_count
) * sizeof(desc_data
[0]));
1223 anv_descriptor_set_write_buffer_view(struct anv_device
*device
,
1224 struct anv_descriptor_set
*set
,
1225 VkDescriptorType type
,
1226 struct anv_buffer_view
*buffer_view
,
1230 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1231 &set
->layout
->binding
[binding
];
1232 struct anv_descriptor
*desc
=
1233 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1235 assert(type
== bind_layout
->type
);
1237 *desc
= (struct anv_descriptor
) {
1239 .buffer_view
= buffer_view
,
1242 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1243 element
* anv_descriptor_size(bind_layout
);
1245 if (bind_layout
->data
& ANV_DESCRIPTOR_SAMPLED_IMAGE
) {
1246 struct anv_sampled_image_descriptor desc_data
= {
1247 .image
= anv_surface_state_to_handle(buffer_view
->surface_state
),
1249 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1252 if (bind_layout
->data
& ANV_DESCRIPTOR_STORAGE_IMAGE
) {
1253 assert(!(bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
));
1254 struct anv_storage_image_descriptor desc_data
= {
1255 .read_write
= anv_surface_state_to_handle(
1256 buffer_view
->storage_surface_state
),
1257 .write_only
= anv_surface_state_to_handle(
1258 buffer_view
->writeonly_storage_surface_state
),
1260 memcpy(desc_map
, &desc_data
, sizeof(desc_data
));
1263 if (bind_layout
->data
& ANV_DESCRIPTOR_IMAGE_PARAM
) {
1264 anv_descriptor_set_write_image_param(desc_map
,
1265 &buffer_view
->storage_image_param
);
1270 anv_descriptor_set_write_buffer(struct anv_device
*device
,
1271 struct anv_descriptor_set
*set
,
1272 struct anv_state_stream
*alloc_stream
,
1273 VkDescriptorType type
,
1274 struct anv_buffer
*buffer
,
1277 VkDeviceSize offset
,
1280 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1281 &set
->layout
->binding
[binding
];
1282 struct anv_descriptor
*desc
=
1283 &set
->descriptors
[bind_layout
->descriptor_index
+ element
];
1285 assert(type
== bind_layout
->type
);
1287 struct anv_address bind_addr
= anv_address_add(buffer
->address
, offset
);
1288 uint64_t bind_range
= anv_buffer_get_range(buffer
, offset
, range
);
1290 if (type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1291 type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1292 *desc
= (struct anv_descriptor
) {
1299 assert(bind_layout
->data
& ANV_DESCRIPTOR_BUFFER_VIEW
);
1300 struct anv_buffer_view
*bview
=
1301 &set
->buffer_views
[bind_layout
->buffer_view_index
+ element
];
1303 bview
->format
= anv_isl_format_for_descriptor_type(type
);
1304 bview
->range
= bind_range
;
1305 bview
->address
= bind_addr
;
1307 /* If we're writing descriptors through a push command, we need to
1308 * allocate the surface state from the command buffer. Otherwise it will
1309 * be allocated by the descriptor pool when calling
1310 * vkAllocateDescriptorSets. */
1312 bview
->surface_state
= anv_state_stream_alloc(alloc_stream
, 64, 64);
1314 anv_fill_buffer_surface_state(device
, bview
->surface_state
,
1315 bview
->format
, bind_addr
, bind_range
, 1);
1317 *desc
= (struct anv_descriptor
) {
1319 .buffer_view
= bview
,
1323 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
+
1324 element
* anv_descriptor_size(bind_layout
);
1326 if (bind_layout
->data
& ANV_DESCRIPTOR_ADDRESS_RANGE
) {
1327 struct anv_address_range_descriptor desc
= {
1328 .address
= anv_address_physical(bind_addr
),
1329 .range
= bind_range
,
1331 memcpy(desc_map
, &desc
, sizeof(desc
));
1336 anv_descriptor_set_write_inline_uniform_data(struct anv_device
*device
,
1337 struct anv_descriptor_set
*set
,
1343 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1344 &set
->layout
->binding
[binding
];
1346 assert(bind_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
);
1348 void *desc_map
= set
->desc_mem
.map
+ bind_layout
->descriptor_offset
;
1350 memcpy(desc_map
+ offset
, data
, size
);
1353 void anv_UpdateDescriptorSets(
1355 uint32_t descriptorWriteCount
,
1356 const VkWriteDescriptorSet
* pDescriptorWrites
,
1357 uint32_t descriptorCopyCount
,
1358 const VkCopyDescriptorSet
* pDescriptorCopies
)
1360 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1362 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1363 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1364 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
1366 switch (write
->descriptorType
) {
1367 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1368 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1369 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1370 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1371 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1372 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1373 anv_descriptor_set_write_image_view(device
, set
,
1374 write
->pImageInfo
+ j
,
1375 write
->descriptorType
,
1377 write
->dstArrayElement
+ j
);
1381 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1382 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1383 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1384 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1385 write
->pTexelBufferView
[j
]);
1387 anv_descriptor_set_write_buffer_view(device
, set
,
1388 write
->descriptorType
,
1391 write
->dstArrayElement
+ j
);
1395 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1396 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1397 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1398 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1399 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1400 assert(write
->pBufferInfo
[j
].buffer
);
1401 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1404 anv_descriptor_set_write_buffer(device
, set
,
1406 write
->descriptorType
,
1409 write
->dstArrayElement
+ j
,
1410 write
->pBufferInfo
[j
].offset
,
1411 write
->pBufferInfo
[j
].range
);
1415 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
: {
1416 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_write
=
1417 vk_find_struct_const(write
->pNext
,
1418 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
1419 assert(inline_write
->dataSize
== write
->descriptorCount
);
1420 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1422 inline_write
->pData
,
1423 write
->dstArrayElement
,
1424 inline_write
->dataSize
);
1433 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
1434 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
1435 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->srcSet
);
1436 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
1438 const struct anv_descriptor_set_binding_layout
*src_layout
=
1439 &src
->layout
->binding
[copy
->srcBinding
];
1440 struct anv_descriptor
*src_desc
=
1441 &src
->descriptors
[src_layout
->descriptor_index
];
1442 src_desc
+= copy
->srcArrayElement
;
1444 const struct anv_descriptor_set_binding_layout
*dst_layout
=
1445 &dst
->layout
->binding
[copy
->dstBinding
];
1446 struct anv_descriptor
*dst_desc
=
1447 &dst
->descriptors
[dst_layout
->descriptor_index
];
1448 dst_desc
+= copy
->dstArrayElement
;
1450 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
1451 dst_desc
[j
] = src_desc
[j
];
1453 if (src_layout
->data
& ANV_DESCRIPTOR_INLINE_UNIFORM
) {
1454 assert(src_layout
->data
== ANV_DESCRIPTOR_INLINE_UNIFORM
);
1455 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1456 copy
->dstArrayElement
,
1457 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1458 copy
->srcArrayElement
,
1459 copy
->descriptorCount
);
1461 unsigned desc_size
= anv_descriptor_size(src_layout
);
1462 if (desc_size
> 0) {
1463 assert(desc_size
== anv_descriptor_size(dst_layout
));
1464 memcpy(dst
->desc_mem
.map
+ dst_layout
->descriptor_offset
+
1465 copy
->dstArrayElement
* desc_size
,
1466 src
->desc_mem
.map
+ src_layout
->descriptor_offset
+
1467 copy
->srcArrayElement
* desc_size
,
1468 copy
->descriptorCount
* desc_size
);
1475 * Descriptor update templates.
1479 anv_descriptor_set_write_template(struct anv_device
*device
,
1480 struct anv_descriptor_set
*set
,
1481 struct anv_state_stream
*alloc_stream
,
1482 const struct anv_descriptor_update_template
*template,
1485 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1486 const struct anv_descriptor_template_entry
*entry
=
1487 &template->entries
[i
];
1489 switch (entry
->type
) {
1490 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1491 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1492 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1493 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1494 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1495 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1496 const VkDescriptorImageInfo
*info
=
1497 data
+ entry
->offset
+ j
* entry
->stride
;
1498 anv_descriptor_set_write_image_view(device
, set
,
1501 entry
->array_element
+ j
);
1505 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1506 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1507 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1508 const VkBufferView
*_bview
=
1509 data
+ entry
->offset
+ j
* entry
->stride
;
1510 ANV_FROM_HANDLE(anv_buffer_view
, bview
, *_bview
);
1512 anv_descriptor_set_write_buffer_view(device
, set
,
1516 entry
->array_element
+ j
);
1520 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1521 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1522 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1523 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1524 for (uint32_t j
= 0; j
< entry
->array_count
; j
++) {
1525 const VkDescriptorBufferInfo
*info
=
1526 data
+ entry
->offset
+ j
* entry
->stride
;
1527 ANV_FROM_HANDLE(anv_buffer
, buffer
, info
->buffer
);
1529 anv_descriptor_set_write_buffer(device
, set
,
1534 entry
->array_element
+ j
,
1535 info
->offset
, info
->range
);
1539 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
1540 anv_descriptor_set_write_inline_uniform_data(device
, set
,
1542 data
+ entry
->offset
,
1543 entry
->array_element
,
1544 entry
->array_count
);
1553 VkResult
anv_CreateDescriptorUpdateTemplate(
1555 const VkDescriptorUpdateTemplateCreateInfo
* pCreateInfo
,
1556 const VkAllocationCallbacks
* pAllocator
,
1557 VkDescriptorUpdateTemplate
* pDescriptorUpdateTemplate
)
1559 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1560 struct anv_descriptor_update_template
*template;
1562 size_t size
= sizeof(*template) +
1563 pCreateInfo
->descriptorUpdateEntryCount
* sizeof(template->entries
[0]);
1564 template = vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1565 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1566 if (template == NULL
)
1567 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1569 template->bind_point
= pCreateInfo
->pipelineBindPoint
;
1571 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
)
1572 template->set
= pCreateInfo
->set
;
1574 template->entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1575 for (uint32_t i
= 0; i
< template->entry_count
; i
++) {
1576 const VkDescriptorUpdateTemplateEntry
*pEntry
=
1577 &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1579 template->entries
[i
] = (struct anv_descriptor_template_entry
) {
1580 .type
= pEntry
->descriptorType
,
1581 .binding
= pEntry
->dstBinding
,
1582 .array_element
= pEntry
->dstArrayElement
,
1583 .array_count
= pEntry
->descriptorCount
,
1584 .offset
= pEntry
->offset
,
1585 .stride
= pEntry
->stride
,
1589 *pDescriptorUpdateTemplate
=
1590 anv_descriptor_update_template_to_handle(template);
1595 void anv_DestroyDescriptorUpdateTemplate(
1597 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1598 const VkAllocationCallbacks
* pAllocator
)
1600 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1601 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1602 descriptorUpdateTemplate
);
1604 vk_free2(&device
->alloc
, pAllocator
, template);
1607 void anv_UpdateDescriptorSetWithTemplate(
1609 VkDescriptorSet descriptorSet
,
1610 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1613 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1614 ANV_FROM_HANDLE(anv_descriptor_set
, set
, descriptorSet
);
1615 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1616 descriptorUpdateTemplate
);
1618 anv_descriptor_set_write_template(device
, set
, NULL
, template, pData
);