2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "util/mesa-sha1.h"
31 #include "radv_private.h"
33 #include "vk_format.h"
37 static bool has_equal_immutable_samplers(const VkSampler
*samplers
, uint32_t count
)
41 for(uint32_t i
= 1; i
< count
; ++i
) {
42 if (memcmp(radv_sampler_from_handle(samplers
[0])->state
,
43 radv_sampler_from_handle(samplers
[i
])->state
, 16)) {
50 static int binding_compare(const void* av
, const void *bv
)
52 const VkDescriptorSetLayoutBinding
*a
= (const VkDescriptorSetLayoutBinding
*)av
;
53 const VkDescriptorSetLayoutBinding
*b
= (const VkDescriptorSetLayoutBinding
*)bv
;
55 return (a
->binding
< b
->binding
) ? -1 : (a
->binding
> b
->binding
) ? 1 : 0;
58 static VkDescriptorSetLayoutBinding
*
59 create_sorted_bindings(const VkDescriptorSetLayoutBinding
*bindings
, unsigned count
) {
60 VkDescriptorSetLayoutBinding
*sorted_bindings
= malloc(count
* sizeof(VkDescriptorSetLayoutBinding
));
64 memcpy(sorted_bindings
, bindings
, count
* sizeof(VkDescriptorSetLayoutBinding
));
66 qsort(sorted_bindings
, count
, sizeof(VkDescriptorSetLayoutBinding
), binding_compare
);
68 return sorted_bindings
;
71 VkResult
radv_CreateDescriptorSetLayout(
73 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
74 const VkAllocationCallbacks
* pAllocator
,
75 VkDescriptorSetLayout
* pSetLayout
)
77 RADV_FROM_HANDLE(radv_device
, device
, _device
);
78 struct radv_descriptor_set_layout
*set_layout
;
80 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
81 const VkDescriptorSetLayoutBindingFlagsCreateInfo
*variable_flags
=
82 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO
);
84 uint32_t max_binding
= 0;
85 uint32_t immutable_sampler_count
= 0;
86 uint32_t ycbcr_sampler_count
= 0;
87 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
88 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
89 if ((pCreateInfo
->pBindings
[j
].descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
90 pCreateInfo
->pBindings
[j
].descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
91 pCreateInfo
->pBindings
[j
].pImmutableSamplers
) {
92 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
94 bool has_ycbcr_sampler
= false;
95 for (unsigned i
= 0; i
< pCreateInfo
->pBindings
[j
].descriptorCount
; ++i
) {
96 if (radv_sampler_from_handle(pCreateInfo
->pBindings
[j
].pImmutableSamplers
[i
])->ycbcr_sampler
)
97 has_ycbcr_sampler
= true;
100 if (has_ycbcr_sampler
)
101 ycbcr_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
105 uint32_t samplers_offset
= sizeof(struct radv_descriptor_set_layout
) +
106 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]);
107 size_t size
= samplers_offset
+ immutable_sampler_count
* 4 * sizeof(uint32_t);
108 if (ycbcr_sampler_count
> 0) {
109 size
+= ycbcr_sampler_count
* sizeof(struct radv_sampler_ycbcr_conversion
) + (max_binding
+ 1) * sizeof(uint32_t);
112 set_layout
= vk_zalloc2(&device
->vk
.alloc
, pAllocator
, size
, 8,
113 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
115 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
117 vk_object_base_init(&device
->vk
, &set_layout
->base
,
118 VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT
);
120 set_layout
->flags
= pCreateInfo
->flags
;
121 set_layout
->layout_size
= size
;
123 /* We just allocate all the samplers at the end of the struct */
124 uint32_t *samplers
= (uint32_t*)&set_layout
->binding
[max_binding
+ 1];
125 struct radv_sampler_ycbcr_conversion
*ycbcr_samplers
= NULL
;
126 uint32_t *ycbcr_sampler_offsets
= NULL
;
128 if (ycbcr_sampler_count
> 0) {
129 ycbcr_sampler_offsets
= samplers
+ 4 * immutable_sampler_count
;
130 set_layout
->ycbcr_sampler_offsets_offset
= (char*)ycbcr_sampler_offsets
- (char*)set_layout
;
131 ycbcr_samplers
= (struct radv_sampler_ycbcr_conversion
*)(ycbcr_sampler_offsets
+ max_binding
+ 1);
133 set_layout
->ycbcr_sampler_offsets_offset
= 0;
135 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
136 pCreateInfo
->bindingCount
);
138 vk_object_base_finish(&set_layout
->base
);
139 vk_free2(&device
->vk
.alloc
, pAllocator
, set_layout
);
140 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
143 set_layout
->binding_count
= max_binding
+ 1;
144 set_layout
->shader_stages
= 0;
145 set_layout
->dynamic_shader_stages
= 0;
146 set_layout
->has_immutable_samplers
= false;
147 set_layout
->size
= 0;
149 memset(set_layout
->binding
, 0, size
- sizeof(struct radv_descriptor_set_layout
));
151 uint32_t buffer_count
= 0;
152 uint32_t dynamic_offset_count
= 0;
154 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
155 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ j
;
156 uint32_t b
= binding
->binding
;
157 uint32_t alignment
= 0;
158 unsigned binding_buffer_count
= 0;
159 uint32_t descriptor_count
= binding
->descriptorCount
;
160 bool has_ycbcr_sampler
= false;
162 /* main image + fmask */
163 uint32_t max_sampled_image_descriptors
= 2;
165 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
&&
166 binding
->pImmutableSamplers
) {
167 for (unsigned i
= 0; i
< binding
->descriptorCount
; ++i
) {
168 struct radv_sampler_ycbcr_conversion
*conversion
=
169 radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
;
172 has_ycbcr_sampler
= true;
173 max_sampled_image_descriptors
= MAX2(max_sampled_image_descriptors
,
174 vk_format_get_plane_count(conversion
->format
));
179 switch (binding
->descriptorType
) {
180 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
181 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
182 assert(!(pCreateInfo
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
183 set_layout
->binding
[b
].dynamic_offset_count
= 1;
184 set_layout
->dynamic_shader_stages
|= binding
->stageFlags
;
185 set_layout
->binding
[b
].size
= 0;
186 binding_buffer_count
= 1;
189 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
190 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
191 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
192 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
193 set_layout
->binding
[b
].size
= 16;
194 binding_buffer_count
= 1;
197 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
198 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
199 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
200 /* main descriptor + fmask descriptor */
201 set_layout
->binding
[b
].size
= 64;
202 binding_buffer_count
= 1;
205 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
206 /* main descriptor + fmask descriptor + sampler */
207 set_layout
->binding
[b
].size
= 96;
208 binding_buffer_count
= 1;
211 case VK_DESCRIPTOR_TYPE_SAMPLER
:
212 set_layout
->binding
[b
].size
= 16;
215 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
217 set_layout
->binding
[b
].size
= descriptor_count
;
218 descriptor_count
= 1;
224 set_layout
->size
= align(set_layout
->size
, alignment
);
225 set_layout
->binding
[b
].type
= binding
->descriptorType
;
226 set_layout
->binding
[b
].array_size
= descriptor_count
;
227 set_layout
->binding
[b
].offset
= set_layout
->size
;
228 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
229 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
231 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
232 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
233 assert(!binding
->pImmutableSamplers
); /* Terribly ill defined how many samplers are valid */
234 assert(binding
->binding
== max_binding
);
236 set_layout
->has_variable_descriptors
= true;
239 if ((binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
240 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
241 binding
->pImmutableSamplers
) {
242 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
243 set_layout
->binding
[b
].immutable_samplers_equal
=
244 has_equal_immutable_samplers(binding
->pImmutableSamplers
, binding
->descriptorCount
);
245 set_layout
->has_immutable_samplers
= true;
248 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
249 memcpy(samplers
+ 4 * i
, &radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->state
, 16);
251 /* Don't reserve space for the samplers if they're not accessed. */
252 if (set_layout
->binding
[b
].immutable_samplers_equal
) {
253 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
&&
254 max_sampled_image_descriptors
<= 2)
255 set_layout
->binding
[b
].size
-= 32;
256 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
257 set_layout
->binding
[b
].size
-= 16;
259 samplers
+= 4 * binding
->descriptorCount
;
260 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
262 if (has_ycbcr_sampler
) {
263 ycbcr_sampler_offsets
[b
] = (const char*)ycbcr_samplers
- (const char*)set_layout
;
264 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
265 if (radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
)
266 ycbcr_samplers
[i
] = *radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
;
268 ycbcr_samplers
[i
].format
= VK_FORMAT_UNDEFINED
;
270 ycbcr_samplers
+= binding
->descriptorCount
;
274 set_layout
->size
+= descriptor_count
* set_layout
->binding
[b
].size
;
275 buffer_count
+= descriptor_count
* binding_buffer_count
;
276 dynamic_offset_count
+= descriptor_count
*
277 set_layout
->binding
[b
].dynamic_offset_count
;
278 set_layout
->shader_stages
|= binding
->stageFlags
;
283 set_layout
->buffer_count
= buffer_count
;
284 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
286 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
291 void radv_DestroyDescriptorSetLayout(
293 VkDescriptorSetLayout _set_layout
,
294 const VkAllocationCallbacks
* pAllocator
)
296 RADV_FROM_HANDLE(radv_device
, device
, _device
);
297 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
302 vk_object_base_finish(&set_layout
->base
);
303 vk_free2(&device
->vk
.alloc
, pAllocator
, set_layout
);
306 void radv_GetDescriptorSetLayoutSupport(VkDevice device
,
307 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
308 VkDescriptorSetLayoutSupport
* pSupport
)
310 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
311 pCreateInfo
->bindingCount
);
313 pSupport
->supported
= false;
317 const VkDescriptorSetLayoutBindingFlagsCreateInfo
*variable_flags
=
318 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO
);
319 VkDescriptorSetVariableDescriptorCountLayoutSupport
*variable_count
=
320 vk_find_struct((void*)pCreateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT
);
321 if (variable_count
) {
322 variable_count
->maxVariableDescriptorCount
= 0;
325 bool supported
= true;
327 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
328 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ i
;
330 uint64_t descriptor_size
= 0;
331 uint64_t descriptor_alignment
= 1;
332 uint32_t descriptor_count
= binding
->descriptorCount
;
333 switch (binding
->descriptorType
) {
334 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
335 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
337 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
338 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
339 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
340 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
341 descriptor_size
= 16;
342 descriptor_alignment
= 16;
344 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
345 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
346 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
347 descriptor_size
= 64;
348 descriptor_alignment
= 32;
350 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
351 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
352 descriptor_size
= 64;
354 descriptor_size
= 96;
356 descriptor_alignment
= 32;
358 case VK_DESCRIPTOR_TYPE_SAMPLER
:
359 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
360 descriptor_size
= 16;
361 descriptor_alignment
= 16;
364 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
365 descriptor_alignment
= 16;
366 descriptor_size
= descriptor_count
;
367 descriptor_count
= 1;
373 if (size
&& !align_u64(size
, descriptor_alignment
)) {
376 size
= align_u64(size
, descriptor_alignment
);
378 uint64_t max_count
= INT32_MAX
;
379 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
380 max_count
= INT32_MAX
- size
;
381 else if (descriptor_size
)
382 max_count
= (INT32_MAX
- size
) / descriptor_size
;
384 if (max_count
< descriptor_count
) {
387 if (variable_flags
&& binding
->binding
<variable_flags
->bindingCount
&& variable_count
&&
388 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
389 variable_count
->maxVariableDescriptorCount
= MIN2(UINT32_MAX
, max_count
);
391 size
+= descriptor_count
* descriptor_size
;
396 pSupport
->supported
= supported
;
400 * Pipeline layouts. These have nothing to do with the pipeline. They are
401 * just multiple descriptor set layouts pasted together.
404 VkResult
radv_CreatePipelineLayout(
406 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
407 const VkAllocationCallbacks
* pAllocator
,
408 VkPipelineLayout
* pPipelineLayout
)
410 RADV_FROM_HANDLE(radv_device
, device
, _device
);
411 struct radv_pipeline_layout
*layout
;
412 struct mesa_sha1 ctx
;
414 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
416 layout
= vk_alloc2(&device
->vk
.alloc
, pAllocator
, sizeof(*layout
), 8,
417 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
419 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
421 vk_object_base_init(&device
->vk
, &layout
->base
,
422 VK_OBJECT_TYPE_PIPELINE_LAYOUT
);
424 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
426 unsigned dynamic_offset_count
= 0;
427 uint16_t dynamic_shader_stages
= 0;
430 _mesa_sha1_init(&ctx
);
431 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
432 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
433 pCreateInfo
->pSetLayouts
[set
]);
434 layout
->set
[set
].layout
= set_layout
;
436 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
437 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
438 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
439 dynamic_shader_stages
|= set_layout
->dynamic_shader_stages
;
441 _mesa_sha1_update(&ctx
, set_layout
, set_layout
->layout_size
);
444 layout
->dynamic_offset_count
= dynamic_offset_count
;
445 layout
->dynamic_shader_stages
= dynamic_shader_stages
;
446 layout
->push_constant_size
= 0;
448 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
449 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
450 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
451 range
->offset
+ range
->size
);
454 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
455 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
456 sizeof(layout
->push_constant_size
));
457 _mesa_sha1_final(&ctx
, layout
->sha1
);
458 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
463 void radv_DestroyPipelineLayout(
465 VkPipelineLayout _pipelineLayout
,
466 const VkAllocationCallbacks
* pAllocator
)
468 RADV_FROM_HANDLE(radv_device
, device
, _device
);
469 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
471 if (!pipeline_layout
)
474 vk_object_base_finish(&pipeline_layout
->base
);
475 vk_free2(&device
->vk
.alloc
, pAllocator
, pipeline_layout
);
481 radv_descriptor_set_create(struct radv_device
*device
,
482 struct radv_descriptor_pool
*pool
,
483 const struct radv_descriptor_set_layout
*layout
,
484 const uint32_t *variable_count
,
485 struct radv_descriptor_set
**out_set
)
487 struct radv_descriptor_set
*set
;
488 uint32_t buffer_count
= layout
->buffer_count
;
489 if (variable_count
) {
491 if (layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
492 layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
494 buffer_count
= layout
->binding
[layout
->binding_count
- 1].buffer_offset
+
495 *variable_count
* stride
;
497 unsigned range_offset
= sizeof(struct radv_descriptor_set
) +
498 sizeof(struct radeon_winsys_bo
*) * buffer_count
;
499 unsigned mem_size
= range_offset
+
500 sizeof(struct radv_descriptor_range
) * layout
->dynamic_offset_count
;
502 if (pool
->host_memory_base
) {
503 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
504 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
506 set
= (struct radv_descriptor_set
*)pool
->host_memory_ptr
;
507 pool
->host_memory_ptr
+= mem_size
;
509 set
= vk_alloc2(&device
->vk
.alloc
, NULL
, mem_size
, 8,
510 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
513 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
516 memset(set
, 0, mem_size
);
518 vk_object_base_init(&device
->vk
, &set
->base
,
519 VK_OBJECT_TYPE_DESCRIPTOR_SET
);
521 if (layout
->dynamic_offset_count
) {
522 set
->dynamic_descriptors
= (struct radv_descriptor_range
*)((uint8_t*)set
+ range_offset
);
525 set
->layout
= layout
;
526 set
->buffer_count
= buffer_count
;
527 uint32_t layout_size
= layout
->size
;
528 if (variable_count
) {
529 assert(layout
->has_variable_descriptors
);
530 uint32_t stride
= layout
->binding
[layout
->binding_count
- 1].size
;
531 if (layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
534 layout_size
= layout
->binding
[layout
->binding_count
- 1].offset
+
535 *variable_count
* stride
;
537 layout_size
= align_u32(layout_size
, 32);
539 set
->size
= layout_size
;
541 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
542 vk_free2(&device
->vk
.alloc
, NULL
, set
);
543 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
546 /* try to allocate linearly first, so that we don't spend
547 * time looking for gaps if the app only allocates &
548 * resets via the pool. */
549 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
551 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
552 set
->va
= radv_buffer_get_va(set
->bo
) + pool
->current_offset
;
553 if (!pool
->host_memory_base
) {
554 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
555 pool
->entries
[pool
->entry_count
].size
= layout_size
;
556 pool
->entries
[pool
->entry_count
].set
= set
;
559 pool
->current_offset
+= layout_size
;
560 } else if (!pool
->host_memory_base
) {
564 for (index
= 0; index
< pool
->entry_count
; ++index
) {
565 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
567 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
570 if (pool
->size
- offset
< layout_size
) {
571 vk_free2(&device
->vk
.alloc
, NULL
, set
);
572 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
575 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
576 set
->va
= radv_buffer_get_va(set
->bo
) + offset
;
577 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
578 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
579 pool
->entries
[index
].offset
= offset
;
580 pool
->entries
[index
].size
= layout_size
;
581 pool
->entries
[index
].set
= set
;
584 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
587 if (layout
->has_immutable_samplers
) {
588 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
589 if (!layout
->binding
[i
].immutable_samplers_offset
||
590 layout
->binding
[i
].immutable_samplers_equal
)
593 unsigned offset
= layout
->binding
[i
].offset
/ 4;
594 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
595 offset
+= radv_combined_image_descriptor_sampler_offset(layout
->binding
+ i
) / 4;
597 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
598 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
599 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
600 offset
+= layout
->binding
[i
].size
/ 4;
610 radv_descriptor_set_destroy(struct radv_device
*device
,
611 struct radv_descriptor_pool
*pool
,
612 struct radv_descriptor_set
*set
,
615 assert(!pool
->host_memory_base
);
617 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
618 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- pool
->mapped_ptr
;
619 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
620 if (pool
->entries
[i
].offset
== offset
) {
621 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
622 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
628 vk_object_base_finish(&set
->base
);
629 vk_free2(&device
->vk
.alloc
, NULL
, set
);
632 VkResult
radv_CreateDescriptorPool(
634 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
635 const VkAllocationCallbacks
* pAllocator
,
636 VkDescriptorPool
* pDescriptorPool
)
638 RADV_FROM_HANDLE(radv_device
, device
, _device
);
639 struct radv_descriptor_pool
*pool
;
640 uint64_t size
= sizeof(struct radv_descriptor_pool
);
641 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
643 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
644 switch (ext
->sType
) {
645 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
: {
646 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*info
=
647 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*)ext
;
648 /* the sizes are 4 aligned, and we need to align to at
649 * most 32, which needs at most 28 bytes extra per
651 bo_size
+= 28llu * info
->maxInlineUniformBlockBindings
;
659 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
660 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
661 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
663 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
664 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
665 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
666 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
668 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
669 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
670 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
671 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
672 case VK_DESCRIPTOR_TYPE_SAMPLER
:
673 /* 32 as we may need to align for images */
674 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
676 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
677 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
678 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
679 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
681 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
682 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
684 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
685 bo_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
692 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
693 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
694 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
695 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
698 size
+= sizeof(struct radv_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
701 pool
= vk_alloc2(&device
->vk
.alloc
, pAllocator
, size
, 8,
702 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
704 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
706 memset(pool
, 0, sizeof(*pool
));
708 vk_object_base_init(&device
->vk
, &pool
->base
,
709 VK_OBJECT_TYPE_DESCRIPTOR_POOL
);
711 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
712 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
713 pool
->host_memory_ptr
= pool
->host_memory_base
;
714 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
718 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
, 32,
720 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
721 RADEON_FLAG_READ_ONLY
|
723 RADV_BO_PRIORITY_DESCRIPTOR
);
724 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
726 pool
->size
= bo_size
;
727 pool
->max_entry_count
= pCreateInfo
->maxSets
;
729 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
733 void radv_DestroyDescriptorPool(
735 VkDescriptorPool _pool
,
736 const VkAllocationCallbacks
* pAllocator
)
738 RADV_FROM_HANDLE(radv_device
, device
, _device
);
739 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
744 if (!pool
->host_memory_base
) {
745 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
746 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
751 device
->ws
->buffer_destroy(pool
->bo
);
753 vk_object_base_finish(&pool
->base
);
754 vk_free2(&device
->vk
.alloc
, pAllocator
, pool
);
757 VkResult
radv_ResetDescriptorPool(
759 VkDescriptorPool descriptorPool
,
760 VkDescriptorPoolResetFlags flags
)
762 RADV_FROM_HANDLE(radv_device
, device
, _device
);
763 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
765 if (!pool
->host_memory_base
) {
766 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
767 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
769 pool
->entry_count
= 0;
772 pool
->current_offset
= 0;
773 pool
->host_memory_ptr
= pool
->host_memory_base
;
778 VkResult
radv_AllocateDescriptorSets(
780 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
781 VkDescriptorSet
* pDescriptorSets
)
783 RADV_FROM_HANDLE(radv_device
, device
, _device
);
784 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
786 VkResult result
= VK_SUCCESS
;
788 struct radv_descriptor_set
*set
= NULL
;
790 const VkDescriptorSetVariableDescriptorCountAllocateInfo
*variable_counts
=
791 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO
);
792 const uint32_t zero
= 0;
794 /* allocate a set of buffers for each shader to contain descriptors */
795 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
796 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
797 pAllocateInfo
->pSetLayouts
[i
]);
799 const uint32_t *variable_count
= NULL
;
800 if (variable_counts
) {
801 if (i
< variable_counts
->descriptorSetCount
)
802 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
804 variable_count
= &zero
;
807 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
809 result
= radv_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
810 if (result
!= VK_SUCCESS
)
813 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
816 if (result
!= VK_SUCCESS
) {
817 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
819 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
820 pDescriptorSets
[i
] = VK_NULL_HANDLE
;
826 VkResult
radv_FreeDescriptorSets(
828 VkDescriptorPool descriptorPool
,
830 const VkDescriptorSet
* pDescriptorSets
)
832 RADV_FROM_HANDLE(radv_device
, device
, _device
);
833 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
835 for (uint32_t i
= 0; i
< count
; i
++) {
836 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
838 if (set
&& !pool
->host_memory_base
)
839 radv_descriptor_set_destroy(device
, pool
, set
, true);
844 static void write_texel_buffer_descriptor(struct radv_device
*device
,
845 struct radv_cmd_buffer
*cmd_buffer
,
847 struct radeon_winsys_bo
**buffer_list
,
848 const VkBufferView _buffer_view
)
850 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
853 memset(dst
, 0, 4 * 4);
857 memcpy(dst
, buffer_view
->state
, 4 * 4);
860 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer_view
->bo
);
862 *buffer_list
= buffer_view
->bo
;
865 static void write_buffer_descriptor(struct radv_device
*device
,
866 struct radv_cmd_buffer
*cmd_buffer
,
868 struct radeon_winsys_bo
**buffer_list
,
869 const VkDescriptorBufferInfo
*buffer_info
)
871 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
874 memset(dst
, 0, 4 * 4);
878 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
879 uint32_t range
= buffer_info
->range
;
881 if (buffer_info
->range
== VK_WHOLE_SIZE
)
882 range
= buffer
->size
- buffer_info
->offset
;
884 /* robustBufferAccess is relaxed enough to allow this (in combination
885 * with the alignment/size we return from vkGetBufferMemoryRequirements)
886 * and this allows the shader compiler to create more efficient 8/16-bit
887 * buffer accesses. */
888 range
= align(range
, 4);
890 va
+= buffer_info
->offset
+ buffer
->offset
;
892 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
894 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
895 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
896 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
897 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
899 if (device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
900 dst
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
901 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
902 S_008F0C_RESOURCE_LEVEL(1);
904 dst
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
905 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
909 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer
->bo
);
911 *buffer_list
= buffer
->bo
;
914 static void write_block_descriptor(struct radv_device
*device
,
915 struct radv_cmd_buffer
*cmd_buffer
,
917 const VkWriteDescriptorSet
*writeset
)
919 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_ub
=
920 vk_find_struct_const(writeset
->pNext
, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
922 memcpy(dst
, inline_ub
->pData
, inline_ub
->dataSize
);
925 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
926 struct radv_descriptor_range
*range
,
927 struct radeon_winsys_bo
**buffer_list
,
928 const VkDescriptorBufferInfo
*buffer_info
)
930 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
937 va
= radv_buffer_get_va(buffer
->bo
);
938 size
= buffer_info
->range
;
940 if (buffer_info
->range
== VK_WHOLE_SIZE
)
941 size
= buffer
->size
- buffer_info
->offset
;
943 /* robustBufferAccess is relaxed enough to allow this (in combination
944 * with the alignment/size we return from vkGetBufferMemoryRequirements)
945 * and this allows the shader compiler to create more efficient 8/16-bit
946 * buffer accesses. */
947 size
= align(size
, 4);
949 va
+= buffer_info
->offset
+ buffer
->offset
;
953 *buffer_list
= buffer
->bo
;
957 write_image_descriptor(struct radv_device
*device
,
958 struct radv_cmd_buffer
*cmd_buffer
,
959 unsigned size
, unsigned *dst
,
960 struct radeon_winsys_bo
**buffer_list
,
961 VkDescriptorType descriptor_type
,
962 const VkDescriptorImageInfo
*image_info
)
964 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
965 union radv_descriptor
*descriptor
;
968 memset(dst
, 0, size
);
972 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
973 descriptor
= &iview
->storage_descriptor
;
975 descriptor
= &iview
->descriptor
;
978 memcpy(dst
, descriptor
, size
);
981 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, iview
->bo
);
983 *buffer_list
= iview
->bo
;
987 write_combined_image_sampler_descriptor(struct radv_device
*device
,
988 struct radv_cmd_buffer
*cmd_buffer
,
989 unsigned sampler_offset
,
991 struct radeon_winsys_bo
**buffer_list
,
992 VkDescriptorType descriptor_type
,
993 const VkDescriptorImageInfo
*image_info
,
996 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
998 write_image_descriptor(device
, cmd_buffer
, sampler_offset
, dst
, buffer_list
,
999 descriptor_type
, image_info
);
1000 /* copy over sampler state */
1002 memcpy(dst
+ sampler_offset
/ sizeof(*dst
), sampler
->state
, 16);
1007 write_sampler_descriptor(struct radv_device
*device
,
1009 const VkDescriptorImageInfo
*image_info
)
1011 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
1013 memcpy(dst
, sampler
->state
, 16);
1016 void radv_update_descriptor_sets(
1017 struct radv_device
* device
,
1018 struct radv_cmd_buffer
* cmd_buffer
,
1019 VkDescriptorSet dstSetOverride
,
1020 uint32_t descriptorWriteCount
,
1021 const VkWriteDescriptorSet
* pDescriptorWrites
,
1022 uint32_t descriptorCopyCount
,
1023 const VkCopyDescriptorSet
* pDescriptorCopies
)
1026 for (i
= 0; i
< descriptorWriteCount
; i
++) {
1027 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
1028 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
1029 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
1030 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1031 set
->layout
->binding
+ writeset
->dstBinding
;
1032 uint32_t *ptr
= set
->mapped_ptr
;
1033 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
1034 /* Immutable samplers are not copied into push descriptors when they are
1035 * allocated, so if we are writing push descriptors we have to copy the
1036 * immutable samplers into them now.
1038 const bool copy_immutable_samplers
= cmd_buffer
&&
1039 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
1040 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
1042 ptr
+= binding_layout
->offset
/ 4;
1044 if (writeset
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1045 write_block_descriptor(device
, cmd_buffer
, (uint8_t*)ptr
+ writeset
->dstArrayElement
, writeset
);
1049 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
1050 buffer_list
+= binding_layout
->buffer_offset
;
1051 buffer_list
+= writeset
->dstArrayElement
;
1052 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
1053 switch(writeset
->descriptorType
) {
1054 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1055 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1056 unsigned idx
= writeset
->dstArrayElement
+ j
;
1057 idx
+= binding_layout
->dynamic_offset_offset
;
1058 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1059 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1060 buffer_list
, writeset
->pBufferInfo
+ j
);
1063 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1064 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1065 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1066 writeset
->pBufferInfo
+ j
);
1068 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1069 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1070 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1071 writeset
->pTexelBufferView
[j
]);
1073 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1074 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1075 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1076 write_image_descriptor(device
, cmd_buffer
, 64, ptr
, buffer_list
,
1077 writeset
->descriptorType
,
1078 writeset
->pImageInfo
+ j
);
1080 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
: {
1081 unsigned sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
);
1082 write_combined_image_sampler_descriptor(device
, cmd_buffer
, sampler_offset
,
1084 writeset
->descriptorType
,
1085 writeset
->pImageInfo
+ j
,
1086 !binding_layout
->immutable_samplers_offset
);
1087 if (copy_immutable_samplers
) {
1088 const unsigned idx
= writeset
->dstArrayElement
+ j
;
1089 memcpy((char*)ptr
+ sampler_offset
, samplers
+ 4 * idx
, 16);
1093 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1094 if (!binding_layout
->immutable_samplers_offset
) {
1095 write_sampler_descriptor(device
, ptr
,
1096 writeset
->pImageInfo
+ j
);
1097 } else if (copy_immutable_samplers
) {
1098 unsigned idx
= writeset
->dstArrayElement
+ j
;
1099 memcpy(ptr
, samplers
+ 4 * idx
, 16);
1105 ptr
+= binding_layout
->size
/ 4;
1111 for (i
= 0; i
< descriptorCopyCount
; i
++) {
1112 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
1113 RADV_FROM_HANDLE(radv_descriptor_set
, src_set
,
1115 RADV_FROM_HANDLE(radv_descriptor_set
, dst_set
,
1117 const struct radv_descriptor_set_binding_layout
*src_binding_layout
=
1118 src_set
->layout
->binding
+ copyset
->srcBinding
;
1119 const struct radv_descriptor_set_binding_layout
*dst_binding_layout
=
1120 dst_set
->layout
->binding
+ copyset
->dstBinding
;
1121 uint32_t *src_ptr
= src_set
->mapped_ptr
;
1122 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
1123 struct radeon_winsys_bo
**src_buffer_list
= src_set
->descriptors
;
1124 struct radeon_winsys_bo
**dst_buffer_list
= dst_set
->descriptors
;
1126 src_ptr
+= src_binding_layout
->offset
/ 4;
1127 dst_ptr
+= dst_binding_layout
->offset
/ 4;
1129 if (src_binding_layout
->type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1130 src_ptr
+= copyset
->srcArrayElement
/ 4;
1131 dst_ptr
+= copyset
->dstArrayElement
/ 4;
1133 memcpy(dst_ptr
, src_ptr
, copyset
->descriptorCount
);
1137 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
1138 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
1140 src_buffer_list
+= src_binding_layout
->buffer_offset
;
1141 src_buffer_list
+= copyset
->srcArrayElement
;
1143 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
1144 dst_buffer_list
+= copyset
->dstArrayElement
;
1146 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
1147 switch (src_binding_layout
->type
) {
1148 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1149 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1150 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1151 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1152 struct radv_descriptor_range
*src_range
, *dst_range
;
1153 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
1154 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
1156 src_range
= src_set
->dynamic_descriptors
+ src_idx
;
1157 dst_range
= dst_set
->dynamic_descriptors
+ dst_idx
;
1158 *dst_range
= *src_range
;
1162 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
1164 src_ptr
+= src_binding_layout
->size
/ 4;
1165 dst_ptr
+= dst_binding_layout
->size
/ 4;
1167 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
1168 /* Sampler descriptors don't have a buffer list. */
1169 dst_buffer_list
[j
] = src_buffer_list
[j
];
1175 void radv_UpdateDescriptorSets(
1177 uint32_t descriptorWriteCount
,
1178 const VkWriteDescriptorSet
* pDescriptorWrites
,
1179 uint32_t descriptorCopyCount
,
1180 const VkCopyDescriptorSet
* pDescriptorCopies
)
1182 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1184 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
1185 descriptorCopyCount
, pDescriptorCopies
);
1188 VkResult
radv_CreateDescriptorUpdateTemplate(VkDevice _device
,
1189 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1190 const VkAllocationCallbacks
*pAllocator
,
1191 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1193 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1194 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
1195 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1196 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
1197 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
1198 struct radv_descriptor_update_template
*templ
;
1201 templ
= vk_alloc2(&device
->vk
.alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1203 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1205 vk_object_base_init(&device
->vk
, &templ
->base
,
1206 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE
);
1208 templ
->entry_count
= entry_count
;
1210 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
) {
1211 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, pCreateInfo
->pipelineLayout
);
1213 /* descriptorSetLayout should be ignored for push descriptors
1214 * and instead it refers to pipelineLayout and set.
1216 assert(pCreateInfo
->set
< MAX_SETS
);
1217 set_layout
= pipeline_layout
->set
[pCreateInfo
->set
].layout
;
1219 templ
->bind_point
= pCreateInfo
->pipelineBindPoint
;
1222 for (i
= 0; i
< entry_count
; i
++) {
1223 const VkDescriptorUpdateTemplateEntry
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1224 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1225 set_layout
->binding
+ entry
->dstBinding
;
1226 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
1227 const uint32_t *immutable_samplers
= NULL
;
1228 uint32_t dst_offset
;
1229 uint32_t dst_stride
;
1231 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1232 is dynamic, and an offset into mapped_ptr otherwise */
1233 switch (entry
->descriptorType
) {
1234 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1235 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1236 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
);
1237 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
1238 dst_stride
= 0; /* Not used */
1241 switch (entry
->descriptorType
) {
1242 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1243 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1244 /* Immutable samplers are copied into push descriptors when they are pushed */
1245 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
1246 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
1247 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
1253 dst_offset
= binding_layout
->offset
/ 4;
1254 if (entry
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
1255 dst_offset
+= entry
->dstArrayElement
/ 4;
1257 dst_offset
+= binding_layout
->size
* entry
->dstArrayElement
/ 4;
1259 dst_stride
= binding_layout
->size
/ 4;
1263 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
1264 .descriptor_type
= entry
->descriptorType
,
1265 .descriptor_count
= entry
->descriptorCount
,
1266 .src_offset
= entry
->offset
,
1267 .src_stride
= entry
->stride
,
1268 .dst_offset
= dst_offset
,
1269 .dst_stride
= dst_stride
,
1270 .buffer_offset
= buffer_offset
,
1271 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
1272 .sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
),
1273 .immutable_samplers
= immutable_samplers
1277 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
1281 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device
,
1282 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1283 const VkAllocationCallbacks
*pAllocator
)
1285 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1286 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1291 vk_object_base_finish(&templ
->base
);
1292 vk_free2(&device
->vk
.alloc
, pAllocator
, templ
);
1295 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
1296 struct radv_cmd_buffer
*cmd_buffer
,
1297 struct radv_descriptor_set
*set
,
1298 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1301 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1304 for (i
= 0; i
< templ
->entry_count
; ++i
) {
1305 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
1306 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
1307 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
1310 if (templ
->entry
[i
].descriptor_type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1311 memcpy((uint8_t*)pDst
, pSrc
, templ
->entry
[i
].descriptor_count
);
1315 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1316 switch (templ
->entry
[i
].descriptor_type
) {
1317 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1318 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1319 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
1320 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1321 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1322 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
1325 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1326 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1327 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1328 (struct VkDescriptorBufferInfo
*) pSrc
);
1330 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1331 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1332 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1333 *(VkBufferView
*) pSrc
);
1335 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1336 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1337 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1338 write_image_descriptor(device
, cmd_buffer
, 64, pDst
, buffer_list
,
1339 templ
->entry
[i
].descriptor_type
,
1340 (struct VkDescriptorImageInfo
*) pSrc
);
1342 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1343 write_combined_image_sampler_descriptor(device
, cmd_buffer
, templ
->entry
[i
].sampler_offset
,
1344 pDst
, buffer_list
, templ
->entry
[i
].descriptor_type
,
1345 (struct VkDescriptorImageInfo
*) pSrc
,
1346 templ
->entry
[i
].has_sampler
);
1347 if (templ
->entry
[i
].immutable_samplers
) {
1348 memcpy((char*)pDst
+ templ
->entry
[i
].sampler_offset
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1351 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1352 if (templ
->entry
[i
].has_sampler
)
1353 write_sampler_descriptor(device
, pDst
,
1354 (struct VkDescriptorImageInfo
*) pSrc
);
1355 else if (templ
->entry
[i
].immutable_samplers
)
1356 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1361 pSrc
+= templ
->entry
[i
].src_stride
;
1362 pDst
+= templ
->entry
[i
].dst_stride
;
1368 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device
,
1369 VkDescriptorSet descriptorSet
,
1370 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1373 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1374 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
1376 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);
1380 VkResult
radv_CreateSamplerYcbcrConversion(VkDevice _device
,
1381 const VkSamplerYcbcrConversionCreateInfo
* pCreateInfo
,
1382 const VkAllocationCallbacks
* pAllocator
,
1383 VkSamplerYcbcrConversion
* pYcbcrConversion
)
1385 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1386 struct radv_sampler_ycbcr_conversion
*conversion
= NULL
;
1388 conversion
= vk_zalloc2(&device
->vk
.alloc
, pAllocator
, sizeof(*conversion
), 8,
1389 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1391 if (conversion
== NULL
)
1392 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1394 vk_object_base_init(&device
->vk
, &conversion
->base
,
1395 VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION
);
1397 conversion
->format
= pCreateInfo
->format
;
1398 conversion
->ycbcr_model
= pCreateInfo
->ycbcrModel
;
1399 conversion
->ycbcr_range
= pCreateInfo
->ycbcrRange
;
1400 conversion
->components
= pCreateInfo
->components
;
1401 conversion
->chroma_offsets
[0] = pCreateInfo
->xChromaOffset
;
1402 conversion
->chroma_offsets
[1] = pCreateInfo
->yChromaOffset
;
1403 conversion
->chroma_filter
= pCreateInfo
->chromaFilter
;
1405 *pYcbcrConversion
= radv_sampler_ycbcr_conversion_to_handle(conversion
);
1410 void radv_DestroySamplerYcbcrConversion(VkDevice _device
,
1411 VkSamplerYcbcrConversion ycbcrConversion
,
1412 const VkAllocationCallbacks
* pAllocator
)
1414 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1415 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion
, ycbcr_conversion
, ycbcrConversion
);
1417 if (!ycbcr_conversion
)
1420 vk_object_base_finish(&ycbcr_conversion
->base
);
1421 vk_free2(&device
->vk
.alloc
, pAllocator
, ycbcr_conversion
);