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
->alloc
, pAllocator
, size
, 8,
113 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
115 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
117 set_layout
->flags
= pCreateInfo
->flags
;
118 set_layout
->layout_size
= size
;
120 /* We just allocate all the samplers at the end of the struct */
121 uint32_t *samplers
= (uint32_t*)&set_layout
->binding
[max_binding
+ 1];
122 struct radv_sampler_ycbcr_conversion
*ycbcr_samplers
= NULL
;
123 uint32_t *ycbcr_sampler_offsets
= NULL
;
125 if (ycbcr_sampler_count
> 0) {
126 ycbcr_sampler_offsets
= samplers
+ 4 * immutable_sampler_count
;
127 set_layout
->ycbcr_sampler_offsets_offset
= (char*)ycbcr_sampler_offsets
- (char*)set_layout
;
128 ycbcr_samplers
= (struct radv_sampler_ycbcr_conversion
*)(ycbcr_sampler_offsets
+ max_binding
+ 1);
130 set_layout
->ycbcr_sampler_offsets_offset
= 0;
132 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
133 pCreateInfo
->bindingCount
);
135 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
136 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
139 set_layout
->binding_count
= max_binding
+ 1;
140 set_layout
->shader_stages
= 0;
141 set_layout
->dynamic_shader_stages
= 0;
142 set_layout
->has_immutable_samplers
= false;
143 set_layout
->size
= 0;
145 memset(set_layout
->binding
, 0, size
- sizeof(struct radv_descriptor_set_layout
));
147 uint32_t buffer_count
= 0;
148 uint32_t dynamic_offset_count
= 0;
150 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
151 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ j
;
152 uint32_t b
= binding
->binding
;
153 uint32_t alignment
= 0;
154 unsigned binding_buffer_count
= 0;
155 uint32_t descriptor_count
= binding
->descriptorCount
;
156 bool has_ycbcr_sampler
= false;
158 /* main image + fmask */
159 uint32_t max_sampled_image_descriptors
= 2;
161 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
&&
162 binding
->pImmutableSamplers
) {
163 for (unsigned i
= 0; i
< binding
->descriptorCount
; ++i
) {
164 struct radv_sampler_ycbcr_conversion
*conversion
=
165 radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
;
168 has_ycbcr_sampler
= true;
169 max_sampled_image_descriptors
= MAX2(max_sampled_image_descriptors
,
170 vk_format_get_plane_count(conversion
->format
));
175 switch (binding
->descriptorType
) {
176 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
177 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
178 assert(!(pCreateInfo
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
179 set_layout
->binding
[b
].dynamic_offset_count
= 1;
180 set_layout
->dynamic_shader_stages
|= binding
->stageFlags
;
181 set_layout
->binding
[b
].size
= 0;
182 binding_buffer_count
= 1;
185 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
186 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
187 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
188 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
189 set_layout
->binding
[b
].size
= 16;
190 binding_buffer_count
= 1;
193 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
194 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
195 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
196 /* main descriptor + fmask descriptor */
197 set_layout
->binding
[b
].size
= 64;
198 binding_buffer_count
= 1;
201 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
202 /* main descriptor + fmask descriptor + sampler */
203 set_layout
->binding
[b
].size
= 96;
204 binding_buffer_count
= 1;
207 case VK_DESCRIPTOR_TYPE_SAMPLER
:
208 set_layout
->binding
[b
].size
= 16;
211 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
213 set_layout
->binding
[b
].size
= descriptor_count
;
214 descriptor_count
= 1;
220 set_layout
->size
= align(set_layout
->size
, alignment
);
221 set_layout
->binding
[b
].type
= binding
->descriptorType
;
222 set_layout
->binding
[b
].array_size
= descriptor_count
;
223 set_layout
->binding
[b
].offset
= set_layout
->size
;
224 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
225 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
227 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
228 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
229 assert(!binding
->pImmutableSamplers
); /* Terribly ill defined how many samplers are valid */
230 assert(binding
->binding
== max_binding
);
232 set_layout
->has_variable_descriptors
= true;
235 if ((binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
236 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
237 binding
->pImmutableSamplers
) {
238 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
239 set_layout
->binding
[b
].immutable_samplers_equal
=
240 has_equal_immutable_samplers(binding
->pImmutableSamplers
, binding
->descriptorCount
);
241 set_layout
->has_immutable_samplers
= true;
244 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
245 memcpy(samplers
+ 4 * i
, &radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->state
, 16);
247 /* Don't reserve space for the samplers if they're not accessed. */
248 if (set_layout
->binding
[b
].immutable_samplers_equal
) {
249 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
&&
250 max_sampled_image_descriptors
<= 2)
251 set_layout
->binding
[b
].size
-= 32;
252 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
253 set_layout
->binding
[b
].size
-= 16;
255 samplers
+= 4 * binding
->descriptorCount
;
256 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
258 if (has_ycbcr_sampler
) {
259 ycbcr_sampler_offsets
[b
] = (const char*)ycbcr_samplers
- (const char*)set_layout
;
260 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
261 if (radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
)
262 ycbcr_samplers
[i
] = *radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
;
264 ycbcr_samplers
[i
].format
= VK_FORMAT_UNDEFINED
;
266 ycbcr_samplers
+= binding
->descriptorCount
;
270 set_layout
->size
+= descriptor_count
* set_layout
->binding
[b
].size
;
271 buffer_count
+= descriptor_count
* binding_buffer_count
;
272 dynamic_offset_count
+= descriptor_count
*
273 set_layout
->binding
[b
].dynamic_offset_count
;
274 set_layout
->shader_stages
|= binding
->stageFlags
;
279 set_layout
->buffer_count
= buffer_count
;
280 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
282 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
287 void radv_DestroyDescriptorSetLayout(
289 VkDescriptorSetLayout _set_layout
,
290 const VkAllocationCallbacks
* pAllocator
)
292 RADV_FROM_HANDLE(radv_device
, device
, _device
);
293 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
298 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
301 void radv_GetDescriptorSetLayoutSupport(VkDevice device
,
302 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
303 VkDescriptorSetLayoutSupport
* pSupport
)
305 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
306 pCreateInfo
->bindingCount
);
308 pSupport
->supported
= false;
312 const VkDescriptorSetLayoutBindingFlagsCreateInfo
*variable_flags
=
313 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO
);
314 VkDescriptorSetVariableDescriptorCountLayoutSupport
*variable_count
=
315 vk_find_struct((void*)pCreateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT
);
316 if (variable_count
) {
317 variable_count
->maxVariableDescriptorCount
= 0;
320 bool supported
= true;
322 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
323 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ i
;
325 uint64_t descriptor_size
= 0;
326 uint64_t descriptor_alignment
= 1;
327 uint32_t descriptor_count
= binding
->descriptorCount
;
328 switch (binding
->descriptorType
) {
329 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
330 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
332 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
333 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
334 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
335 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
336 descriptor_size
= 16;
337 descriptor_alignment
= 16;
339 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
340 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
341 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
342 descriptor_size
= 64;
343 descriptor_alignment
= 32;
345 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
346 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
347 descriptor_size
= 64;
349 descriptor_size
= 96;
351 descriptor_alignment
= 32;
353 case VK_DESCRIPTOR_TYPE_SAMPLER
:
354 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
355 descriptor_size
= 16;
356 descriptor_alignment
= 16;
359 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
360 descriptor_alignment
= 16;
361 descriptor_size
= descriptor_count
;
362 descriptor_count
= 1;
368 if (size
&& !align_u64(size
, descriptor_alignment
)) {
371 size
= align_u64(size
, descriptor_alignment
);
373 uint64_t max_count
= INT32_MAX
;
374 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
375 max_count
= INT32_MAX
- size
;
376 else if (descriptor_size
)
377 max_count
= (INT32_MAX
- size
) / descriptor_size
;
379 if (max_count
< descriptor_count
) {
382 if (variable_flags
&& binding
->binding
<variable_flags
->bindingCount
&& variable_count
&&
383 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
384 variable_count
->maxVariableDescriptorCount
= MIN2(UINT32_MAX
, max_count
);
386 size
+= descriptor_count
* descriptor_size
;
391 pSupport
->supported
= supported
;
395 * Pipeline layouts. These have nothing to do with the pipeline. They are
396 * just multiple descriptor set layouts pasted together.
399 VkResult
radv_CreatePipelineLayout(
401 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
402 const VkAllocationCallbacks
* pAllocator
,
403 VkPipelineLayout
* pPipelineLayout
)
405 RADV_FROM_HANDLE(radv_device
, device
, _device
);
406 struct radv_pipeline_layout
*layout
;
407 struct mesa_sha1 ctx
;
409 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
411 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
412 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
414 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
416 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
418 unsigned dynamic_offset_count
= 0;
419 uint16_t dynamic_shader_stages
= 0;
422 _mesa_sha1_init(&ctx
);
423 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
424 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
425 pCreateInfo
->pSetLayouts
[set
]);
426 layout
->set
[set
].layout
= set_layout
;
428 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
429 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
430 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
431 dynamic_shader_stages
|= set_layout
->dynamic_shader_stages
;
433 _mesa_sha1_update(&ctx
, set_layout
, set_layout
->layout_size
);
436 layout
->dynamic_offset_count
= dynamic_offset_count
;
437 layout
->dynamic_shader_stages
= dynamic_shader_stages
;
438 layout
->push_constant_size
= 0;
440 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
441 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
442 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
443 range
->offset
+ range
->size
);
446 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
447 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
448 sizeof(layout
->push_constant_size
));
449 _mesa_sha1_final(&ctx
, layout
->sha1
);
450 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
455 void radv_DestroyPipelineLayout(
457 VkPipelineLayout _pipelineLayout
,
458 const VkAllocationCallbacks
* pAllocator
)
460 RADV_FROM_HANDLE(radv_device
, device
, _device
);
461 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
463 if (!pipeline_layout
)
465 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
471 radv_descriptor_set_create(struct radv_device
*device
,
472 struct radv_descriptor_pool
*pool
,
473 const struct radv_descriptor_set_layout
*layout
,
474 const uint32_t *variable_count
,
475 struct radv_descriptor_set
**out_set
)
477 struct radv_descriptor_set
*set
;
478 uint32_t buffer_count
= layout
->buffer_count
;
479 if (variable_count
) {
481 if (layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
482 layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
484 buffer_count
= layout
->binding
[layout
->binding_count
- 1].buffer_offset
+
485 *variable_count
* stride
;
487 unsigned range_offset
= sizeof(struct radv_descriptor_set
) +
488 sizeof(struct radeon_winsys_bo
*) * buffer_count
;
489 unsigned mem_size
= range_offset
+
490 sizeof(struct radv_descriptor_range
) * layout
->dynamic_offset_count
;
492 if (pool
->host_memory_base
) {
493 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
494 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
496 set
= (struct radv_descriptor_set
*)pool
->host_memory_ptr
;
497 pool
->host_memory_ptr
+= mem_size
;
499 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
500 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
503 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
506 memset(set
, 0, mem_size
);
508 if (layout
->dynamic_offset_count
) {
509 set
->dynamic_descriptors
= (struct radv_descriptor_range
*)((uint8_t*)set
+ range_offset
);
512 set
->layout
= layout
;
513 set
->buffer_count
= buffer_count
;
514 uint32_t layout_size
= layout
->size
;
515 if (variable_count
) {
516 assert(layout
->has_variable_descriptors
);
517 uint32_t stride
= layout
->binding
[layout
->binding_count
- 1].size
;
518 if (layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
521 layout_size
= layout
->binding
[layout
->binding_count
- 1].offset
+
522 *variable_count
* stride
;
524 layout_size
= align_u32(layout_size
, 32);
526 set
->size
= layout_size
;
528 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
529 vk_free2(&device
->alloc
, NULL
, set
);
530 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
533 /* try to allocate linearly first, so that we don't spend
534 * time looking for gaps if the app only allocates &
535 * resets via the pool. */
536 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
538 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
539 set
->va
= radv_buffer_get_va(set
->bo
) + pool
->current_offset
;
540 if (!pool
->host_memory_base
) {
541 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
542 pool
->entries
[pool
->entry_count
].size
= layout_size
;
543 pool
->entries
[pool
->entry_count
].set
= set
;
546 pool
->current_offset
+= layout_size
;
547 } else if (!pool
->host_memory_base
) {
551 for (index
= 0; index
< pool
->entry_count
; ++index
) {
552 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
554 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
557 if (pool
->size
- offset
< layout_size
) {
558 vk_free2(&device
->alloc
, NULL
, set
);
559 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
562 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
563 set
->va
= radv_buffer_get_va(set
->bo
) + offset
;
564 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
565 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
566 pool
->entries
[index
].offset
= offset
;
567 pool
->entries
[index
].size
= layout_size
;
568 pool
->entries
[index
].set
= set
;
571 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
574 if (layout
->has_immutable_samplers
) {
575 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
576 if (!layout
->binding
[i
].immutable_samplers_offset
||
577 layout
->binding
[i
].immutable_samplers_equal
)
580 unsigned offset
= layout
->binding
[i
].offset
/ 4;
581 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
582 offset
+= radv_combined_image_descriptor_sampler_offset(layout
->binding
+ i
) / 4;
584 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
585 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
586 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
587 offset
+= layout
->binding
[i
].size
/ 4;
597 radv_descriptor_set_destroy(struct radv_device
*device
,
598 struct radv_descriptor_pool
*pool
,
599 struct radv_descriptor_set
*set
,
602 assert(!pool
->host_memory_base
);
604 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
605 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- pool
->mapped_ptr
;
606 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
607 if (pool
->entries
[i
].offset
== offset
) {
608 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
609 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
615 vk_free2(&device
->alloc
, NULL
, set
);
618 VkResult
radv_CreateDescriptorPool(
620 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
621 const VkAllocationCallbacks
* pAllocator
,
622 VkDescriptorPool
* pDescriptorPool
)
624 RADV_FROM_HANDLE(radv_device
, device
, _device
);
625 struct radv_descriptor_pool
*pool
;
626 uint64_t size
= sizeof(struct radv_descriptor_pool
);
627 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
629 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
630 switch (ext
->sType
) {
631 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
: {
632 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*info
=
633 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*)ext
;
634 /* the sizes are 4 aligned, and we need to align to at
635 * most 32, which needs at most 28 bytes extra per
637 bo_size
+= 28llu * info
->maxInlineUniformBlockBindings
;
645 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
646 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
647 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
649 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
650 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
651 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
652 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
654 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
655 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
656 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
657 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
658 case VK_DESCRIPTOR_TYPE_SAMPLER
:
659 /* 32 as we may need to align for images */
660 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
662 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
663 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
664 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
665 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
667 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
668 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
670 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
671 bo_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
678 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
679 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
680 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
681 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
684 size
+= sizeof(struct radv_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
687 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
688 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
690 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
692 memset(pool
, 0, sizeof(*pool
));
694 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
695 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
696 pool
->host_memory_ptr
= pool
->host_memory_base
;
697 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
701 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
, 32,
703 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
704 RADEON_FLAG_READ_ONLY
|
706 RADV_BO_PRIORITY_DESCRIPTOR
);
707 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
709 pool
->size
= bo_size
;
710 pool
->max_entry_count
= pCreateInfo
->maxSets
;
712 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
716 void radv_DestroyDescriptorPool(
718 VkDescriptorPool _pool
,
719 const VkAllocationCallbacks
* pAllocator
)
721 RADV_FROM_HANDLE(radv_device
, device
, _device
);
722 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
727 if (!pool
->host_memory_base
) {
728 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
729 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
734 device
->ws
->buffer_destroy(pool
->bo
);
735 vk_free2(&device
->alloc
, pAllocator
, pool
);
738 VkResult
radv_ResetDescriptorPool(
740 VkDescriptorPool descriptorPool
,
741 VkDescriptorPoolResetFlags flags
)
743 RADV_FROM_HANDLE(radv_device
, device
, _device
);
744 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
746 if (!pool
->host_memory_base
) {
747 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
748 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
750 pool
->entry_count
= 0;
753 pool
->current_offset
= 0;
754 pool
->host_memory_ptr
= pool
->host_memory_base
;
759 VkResult
radv_AllocateDescriptorSets(
761 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
762 VkDescriptorSet
* pDescriptorSets
)
764 RADV_FROM_HANDLE(radv_device
, device
, _device
);
765 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
767 VkResult result
= VK_SUCCESS
;
769 struct radv_descriptor_set
*set
= NULL
;
771 const VkDescriptorSetVariableDescriptorCountAllocateInfo
*variable_counts
=
772 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO
);
773 const uint32_t zero
= 0;
775 /* allocate a set of buffers for each shader to contain descriptors */
776 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
777 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
778 pAllocateInfo
->pSetLayouts
[i
]);
780 const uint32_t *variable_count
= NULL
;
781 if (variable_counts
) {
782 if (i
< variable_counts
->descriptorSetCount
)
783 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
785 variable_count
= &zero
;
788 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
790 result
= radv_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
791 if (result
!= VK_SUCCESS
)
794 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
797 if (result
!= VK_SUCCESS
) {
798 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
800 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
801 pDescriptorSets
[i
] = VK_NULL_HANDLE
;
807 VkResult
radv_FreeDescriptorSets(
809 VkDescriptorPool descriptorPool
,
811 const VkDescriptorSet
* pDescriptorSets
)
813 RADV_FROM_HANDLE(radv_device
, device
, _device
);
814 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
816 for (uint32_t i
= 0; i
< count
; i
++) {
817 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
819 if (set
&& !pool
->host_memory_base
)
820 radv_descriptor_set_destroy(device
, pool
, set
, true);
825 static void write_texel_buffer_descriptor(struct radv_device
*device
,
826 struct radv_cmd_buffer
*cmd_buffer
,
828 struct radeon_winsys_bo
**buffer_list
,
829 const VkBufferView _buffer_view
)
831 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
833 memcpy(dst
, buffer_view
->state
, 4 * 4);
836 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer_view
->bo
);
838 *buffer_list
= buffer_view
->bo
;
841 static void write_buffer_descriptor(struct radv_device
*device
,
842 struct radv_cmd_buffer
*cmd_buffer
,
844 struct radeon_winsys_bo
**buffer_list
,
845 const VkDescriptorBufferInfo
*buffer_info
)
847 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
848 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
849 uint32_t range
= buffer_info
->range
;
851 if (buffer_info
->range
== VK_WHOLE_SIZE
)
852 range
= buffer
->size
- buffer_info
->offset
;
854 /* robustBufferAccess is relaxed enough to allow this (in combination
855 * with the alignment/size we return from vkGetBufferMemoryRequirements)
856 * and this allows the shader compiler to create more efficient 8/16-bit
857 * buffer accesses. */
858 range
= align(range
, 4);
860 va
+= buffer_info
->offset
+ buffer
->offset
;
862 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
864 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
865 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
866 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
867 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
869 if (device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
870 dst
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
871 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
872 S_008F0C_RESOURCE_LEVEL(1);
874 dst
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
875 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
879 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer
->bo
);
881 *buffer_list
= buffer
->bo
;
884 static void write_block_descriptor(struct radv_device
*device
,
885 struct radv_cmd_buffer
*cmd_buffer
,
887 const VkWriteDescriptorSet
*writeset
)
889 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_ub
=
890 vk_find_struct_const(writeset
->pNext
, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
892 memcpy(dst
, inline_ub
->pData
, inline_ub
->dataSize
);
895 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
896 struct radv_descriptor_range
*range
,
897 struct radeon_winsys_bo
**buffer_list
,
898 const VkDescriptorBufferInfo
*buffer_info
)
900 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
901 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
902 unsigned size
= buffer_info
->range
;
904 if (buffer_info
->range
== VK_WHOLE_SIZE
)
905 size
= buffer
->size
- buffer_info
->offset
;
907 /* robustBufferAccess is relaxed enough to allow this (in combination
908 * with the alignment/size we return from vkGetBufferMemoryRequirements)
909 * and this allows the shader compiler to create more efficient 8/16-bit
910 * buffer accesses. */
911 size
= align(size
, 4);
913 va
+= buffer_info
->offset
+ buffer
->offset
;
917 *buffer_list
= buffer
->bo
;
921 write_image_descriptor(struct radv_device
*device
,
922 struct radv_cmd_buffer
*cmd_buffer
,
923 unsigned size
, unsigned *dst
,
924 struct radeon_winsys_bo
**buffer_list
,
925 VkDescriptorType descriptor_type
,
926 const VkDescriptorImageInfo
*image_info
)
928 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
929 union radv_descriptor
*descriptor
;
931 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
932 descriptor
= &iview
->storage_descriptor
;
934 descriptor
= &iview
->descriptor
;
937 memcpy(dst
, descriptor
, size
);
940 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, iview
->bo
);
942 *buffer_list
= iview
->bo
;
946 write_combined_image_sampler_descriptor(struct radv_device
*device
,
947 struct radv_cmd_buffer
*cmd_buffer
,
948 unsigned sampler_offset
,
950 struct radeon_winsys_bo
**buffer_list
,
951 VkDescriptorType descriptor_type
,
952 const VkDescriptorImageInfo
*image_info
,
955 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
957 write_image_descriptor(device
, cmd_buffer
, sampler_offset
, dst
, buffer_list
,
958 descriptor_type
, image_info
);
959 /* copy over sampler state */
961 memcpy(dst
+ sampler_offset
/ sizeof(*dst
), sampler
->state
, 16);
966 write_sampler_descriptor(struct radv_device
*device
,
968 const VkDescriptorImageInfo
*image_info
)
970 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
972 memcpy(dst
, sampler
->state
, 16);
975 void radv_update_descriptor_sets(
976 struct radv_device
* device
,
977 struct radv_cmd_buffer
* cmd_buffer
,
978 VkDescriptorSet dstSetOverride
,
979 uint32_t descriptorWriteCount
,
980 const VkWriteDescriptorSet
* pDescriptorWrites
,
981 uint32_t descriptorCopyCount
,
982 const VkCopyDescriptorSet
* pDescriptorCopies
)
985 for (i
= 0; i
< descriptorWriteCount
; i
++) {
986 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
987 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
988 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
989 const struct radv_descriptor_set_binding_layout
*binding_layout
=
990 set
->layout
->binding
+ writeset
->dstBinding
;
991 uint32_t *ptr
= set
->mapped_ptr
;
992 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
993 /* Immutable samplers are not copied into push descriptors when they are
994 * allocated, so if we are writing push descriptors we have to copy the
995 * immutable samplers into them now.
997 const bool copy_immutable_samplers
= cmd_buffer
&&
998 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
999 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
1001 ptr
+= binding_layout
->offset
/ 4;
1003 if (writeset
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1004 write_block_descriptor(device
, cmd_buffer
, (uint8_t*)ptr
+ writeset
->dstArrayElement
, writeset
);
1008 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
1009 buffer_list
+= binding_layout
->buffer_offset
;
1010 buffer_list
+= writeset
->dstArrayElement
;
1011 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
1012 switch(writeset
->descriptorType
) {
1013 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1014 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1015 unsigned idx
= writeset
->dstArrayElement
+ j
;
1016 idx
+= binding_layout
->dynamic_offset_offset
;
1017 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1018 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1019 buffer_list
, writeset
->pBufferInfo
+ j
);
1022 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1023 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1024 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1025 writeset
->pBufferInfo
+ j
);
1027 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1028 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1029 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1030 writeset
->pTexelBufferView
[j
]);
1032 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1033 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1034 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1035 write_image_descriptor(device
, cmd_buffer
, 64, ptr
, buffer_list
,
1036 writeset
->descriptorType
,
1037 writeset
->pImageInfo
+ j
);
1039 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
: {
1040 unsigned sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
);
1041 write_combined_image_sampler_descriptor(device
, cmd_buffer
, sampler_offset
,
1043 writeset
->descriptorType
,
1044 writeset
->pImageInfo
+ j
,
1045 !binding_layout
->immutable_samplers_offset
);
1046 if (copy_immutable_samplers
) {
1047 const unsigned idx
= writeset
->dstArrayElement
+ j
;
1048 memcpy((char*)ptr
+ sampler_offset
, samplers
+ 4 * idx
, 16);
1052 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1053 if (!binding_layout
->immutable_samplers_offset
) {
1054 write_sampler_descriptor(device
, ptr
,
1055 writeset
->pImageInfo
+ j
);
1056 } else if (copy_immutable_samplers
) {
1057 unsigned idx
= writeset
->dstArrayElement
+ j
;
1058 memcpy(ptr
, samplers
+ 4 * idx
, 16);
1064 ptr
+= binding_layout
->size
/ 4;
1070 for (i
= 0; i
< descriptorCopyCount
; i
++) {
1071 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
1072 RADV_FROM_HANDLE(radv_descriptor_set
, src_set
,
1074 RADV_FROM_HANDLE(radv_descriptor_set
, dst_set
,
1076 const struct radv_descriptor_set_binding_layout
*src_binding_layout
=
1077 src_set
->layout
->binding
+ copyset
->srcBinding
;
1078 const struct radv_descriptor_set_binding_layout
*dst_binding_layout
=
1079 dst_set
->layout
->binding
+ copyset
->dstBinding
;
1080 uint32_t *src_ptr
= src_set
->mapped_ptr
;
1081 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
1082 struct radeon_winsys_bo
**src_buffer_list
= src_set
->descriptors
;
1083 struct radeon_winsys_bo
**dst_buffer_list
= dst_set
->descriptors
;
1085 src_ptr
+= src_binding_layout
->offset
/ 4;
1086 dst_ptr
+= dst_binding_layout
->offset
/ 4;
1088 if (src_binding_layout
->type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1089 src_ptr
+= copyset
->srcArrayElement
/ 4;
1090 dst_ptr
+= copyset
->dstArrayElement
/ 4;
1092 memcpy(dst_ptr
, src_ptr
, copyset
->descriptorCount
);
1096 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
1097 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
1099 src_buffer_list
+= src_binding_layout
->buffer_offset
;
1100 src_buffer_list
+= copyset
->srcArrayElement
;
1102 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
1103 dst_buffer_list
+= copyset
->dstArrayElement
;
1105 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
1106 switch (src_binding_layout
->type
) {
1107 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1108 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1109 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1110 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1111 struct radv_descriptor_range
*src_range
, *dst_range
;
1112 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
1113 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
1115 src_range
= src_set
->dynamic_descriptors
+ src_idx
;
1116 dst_range
= dst_set
->dynamic_descriptors
+ dst_idx
;
1117 *dst_range
= *src_range
;
1121 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
1123 src_ptr
+= src_binding_layout
->size
/ 4;
1124 dst_ptr
+= dst_binding_layout
->size
/ 4;
1126 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
1127 /* Sampler descriptors don't have a buffer list. */
1128 dst_buffer_list
[j
] = src_buffer_list
[j
];
1134 void radv_UpdateDescriptorSets(
1136 uint32_t descriptorWriteCount
,
1137 const VkWriteDescriptorSet
* pDescriptorWrites
,
1138 uint32_t descriptorCopyCount
,
1139 const VkCopyDescriptorSet
* pDescriptorCopies
)
1141 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1143 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
1144 descriptorCopyCount
, pDescriptorCopies
);
1147 VkResult
radv_CreateDescriptorUpdateTemplate(VkDevice _device
,
1148 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1149 const VkAllocationCallbacks
*pAllocator
,
1150 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1152 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1153 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
1154 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1155 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
1156 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
1157 struct radv_descriptor_update_template
*templ
;
1160 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1162 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1164 templ
->entry_count
= entry_count
;
1166 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
) {
1167 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, pCreateInfo
->pipelineLayout
);
1169 /* descriptorSetLayout should be ignored for push descriptors
1170 * and instead it refers to pipelineLayout and set.
1172 assert(pCreateInfo
->set
< MAX_SETS
);
1173 set_layout
= pipeline_layout
->set
[pCreateInfo
->set
].layout
;
1175 templ
->bind_point
= pCreateInfo
->pipelineBindPoint
;
1178 for (i
= 0; i
< entry_count
; i
++) {
1179 const VkDescriptorUpdateTemplateEntry
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1180 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1181 set_layout
->binding
+ entry
->dstBinding
;
1182 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
1183 const uint32_t *immutable_samplers
= NULL
;
1184 uint32_t dst_offset
;
1185 uint32_t dst_stride
;
1187 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1188 is dynamic, and an offset into mapped_ptr otherwise */
1189 switch (entry
->descriptorType
) {
1190 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1191 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1192 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
);
1193 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
1194 dst_stride
= 0; /* Not used */
1197 switch (entry
->descriptorType
) {
1198 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1199 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1200 /* Immutable samplers are copied into push descriptors when they are pushed */
1201 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
1202 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
1203 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
1209 dst_offset
= binding_layout
->offset
/ 4;
1210 if (entry
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
1211 dst_offset
+= entry
->dstArrayElement
/ 4;
1213 dst_offset
+= binding_layout
->size
* entry
->dstArrayElement
/ 4;
1215 dst_stride
= binding_layout
->size
/ 4;
1219 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
1220 .descriptor_type
= entry
->descriptorType
,
1221 .descriptor_count
= entry
->descriptorCount
,
1222 .src_offset
= entry
->offset
,
1223 .src_stride
= entry
->stride
,
1224 .dst_offset
= dst_offset
,
1225 .dst_stride
= dst_stride
,
1226 .buffer_offset
= buffer_offset
,
1227 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
1228 .sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
),
1229 .immutable_samplers
= immutable_samplers
1233 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
1237 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device
,
1238 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1239 const VkAllocationCallbacks
*pAllocator
)
1241 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1242 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1247 vk_free2(&device
->alloc
, pAllocator
, templ
);
1250 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
1251 struct radv_cmd_buffer
*cmd_buffer
,
1252 struct radv_descriptor_set
*set
,
1253 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1256 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1259 for (i
= 0; i
< templ
->entry_count
; ++i
) {
1260 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
1261 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
1262 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
1265 if (templ
->entry
[i
].descriptor_type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1266 memcpy((uint8_t*)pDst
, pSrc
, templ
->entry
[i
].descriptor_count
);
1270 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1271 switch (templ
->entry
[i
].descriptor_type
) {
1272 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1273 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1274 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
1275 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1276 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1277 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
1280 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1281 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1282 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1283 (struct VkDescriptorBufferInfo
*) pSrc
);
1285 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1286 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1287 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1288 *(VkBufferView
*) pSrc
);
1290 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1291 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1292 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1293 write_image_descriptor(device
, cmd_buffer
, 64, pDst
, buffer_list
,
1294 templ
->entry
[i
].descriptor_type
,
1295 (struct VkDescriptorImageInfo
*) pSrc
);
1297 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1298 write_combined_image_sampler_descriptor(device
, cmd_buffer
, templ
->entry
[i
].sampler_offset
,
1299 pDst
, buffer_list
, templ
->entry
[i
].descriptor_type
,
1300 (struct VkDescriptorImageInfo
*) pSrc
,
1301 templ
->entry
[i
].has_sampler
);
1302 if (templ
->entry
[i
].immutable_samplers
) {
1303 memcpy((char*)pDst
+ templ
->entry
[i
].sampler_offset
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1306 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1307 if (templ
->entry
[i
].has_sampler
)
1308 write_sampler_descriptor(device
, pDst
,
1309 (struct VkDescriptorImageInfo
*) pSrc
);
1310 else if (templ
->entry
[i
].immutable_samplers
)
1311 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1316 pSrc
+= templ
->entry
[i
].src_stride
;
1317 pDst
+= templ
->entry
[i
].dst_stride
;
1323 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device
,
1324 VkDescriptorSet descriptorSet
,
1325 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1328 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1329 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
1331 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);
1335 VkResult
radv_CreateSamplerYcbcrConversion(VkDevice _device
,
1336 const VkSamplerYcbcrConversionCreateInfo
* pCreateInfo
,
1337 const VkAllocationCallbacks
* pAllocator
,
1338 VkSamplerYcbcrConversion
* pYcbcrConversion
)
1340 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1341 struct radv_sampler_ycbcr_conversion
*conversion
= NULL
;
1343 conversion
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*conversion
), 8,
1344 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1346 if (conversion
== NULL
)
1347 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1349 conversion
->format
= pCreateInfo
->format
;
1350 conversion
->ycbcr_model
= pCreateInfo
->ycbcrModel
;
1351 conversion
->ycbcr_range
= pCreateInfo
->ycbcrRange
;
1352 conversion
->components
= pCreateInfo
->components
;
1353 conversion
->chroma_offsets
[0] = pCreateInfo
->xChromaOffset
;
1354 conversion
->chroma_offsets
[1] = pCreateInfo
->yChromaOffset
;
1355 conversion
->chroma_filter
= pCreateInfo
->chromaFilter
;
1357 *pYcbcrConversion
= radv_sampler_ycbcr_conversion_to_handle(conversion
);
1362 void radv_DestroySamplerYcbcrConversion(VkDevice _device
,
1363 VkSamplerYcbcrConversion ycbcrConversion
,
1364 const VkAllocationCallbacks
* pAllocator
)
1366 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1367 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion
, ycbcr_conversion
, ycbcrConversion
);
1369 if (ycbcr_conversion
)
1370 vk_free2(&device
->alloc
, pAllocator
, ycbcr_conversion
);