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 static void radv_destroy_descriptor_pool(struct radv_device
*device
,
633 const VkAllocationCallbacks
*pAllocator
,
634 struct radv_descriptor_pool
*pool
)
636 if (!pool
->host_memory_base
) {
637 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
638 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
643 device
->ws
->buffer_destroy(pool
->bo
);
645 vk_object_base_finish(&pool
->base
);
646 vk_free2(&device
->vk
.alloc
, pAllocator
, pool
);
649 VkResult
radv_CreateDescriptorPool(
651 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
652 const VkAllocationCallbacks
* pAllocator
,
653 VkDescriptorPool
* pDescriptorPool
)
655 RADV_FROM_HANDLE(radv_device
, device
, _device
);
656 struct radv_descriptor_pool
*pool
;
657 uint64_t size
= sizeof(struct radv_descriptor_pool
);
658 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
660 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
661 switch (ext
->sType
) {
662 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
: {
663 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*info
=
664 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*)ext
;
665 /* the sizes are 4 aligned, and we need to align to at
666 * most 32, which needs at most 28 bytes extra per
668 bo_size
+= 28llu * info
->maxInlineUniformBlockBindings
;
676 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
677 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
678 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
680 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
681 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
682 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
683 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
685 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
686 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
687 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
688 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
689 case VK_DESCRIPTOR_TYPE_SAMPLER
:
690 /* 32 as we may need to align for images */
691 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
693 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
694 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
695 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
696 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
698 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
699 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
701 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
702 bo_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
709 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
710 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
711 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
712 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
715 size
+= sizeof(struct radv_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
718 pool
= vk_alloc2(&device
->vk
.alloc
, pAllocator
, size
, 8,
719 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
721 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
723 memset(pool
, 0, sizeof(*pool
));
725 vk_object_base_init(&device
->vk
, &pool
->base
,
726 VK_OBJECT_TYPE_DESCRIPTOR_POOL
);
728 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
729 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
730 pool
->host_memory_ptr
= pool
->host_memory_base
;
731 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
735 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
, 32,
737 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
738 RADEON_FLAG_READ_ONLY
|
740 RADV_BO_PRIORITY_DESCRIPTOR
);
742 radv_destroy_descriptor_pool(device
, pAllocator
, pool
);
743 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
745 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
746 if (!pool
->mapped_ptr
) {
747 radv_destroy_descriptor_pool(device
, pAllocator
, pool
);
748 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
751 pool
->size
= bo_size
;
752 pool
->max_entry_count
= pCreateInfo
->maxSets
;
754 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
758 void radv_DestroyDescriptorPool(
760 VkDescriptorPool _pool
,
761 const VkAllocationCallbacks
* pAllocator
)
763 RADV_FROM_HANDLE(radv_device
, device
, _device
);
764 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
769 radv_destroy_descriptor_pool(device
, pAllocator
, pool
);
772 VkResult
radv_ResetDescriptorPool(
774 VkDescriptorPool descriptorPool
,
775 VkDescriptorPoolResetFlags flags
)
777 RADV_FROM_HANDLE(radv_device
, device
, _device
);
778 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
780 if (!pool
->host_memory_base
) {
781 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
782 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
784 pool
->entry_count
= 0;
787 pool
->current_offset
= 0;
788 pool
->host_memory_ptr
= pool
->host_memory_base
;
793 VkResult
radv_AllocateDescriptorSets(
795 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
796 VkDescriptorSet
* pDescriptorSets
)
798 RADV_FROM_HANDLE(radv_device
, device
, _device
);
799 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
801 VkResult result
= VK_SUCCESS
;
803 struct radv_descriptor_set
*set
= NULL
;
805 const VkDescriptorSetVariableDescriptorCountAllocateInfo
*variable_counts
=
806 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO
);
807 const uint32_t zero
= 0;
809 /* allocate a set of buffers for each shader to contain descriptors */
810 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
811 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
812 pAllocateInfo
->pSetLayouts
[i
]);
814 const uint32_t *variable_count
= NULL
;
815 if (variable_counts
) {
816 if (i
< variable_counts
->descriptorSetCount
)
817 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
819 variable_count
= &zero
;
822 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
824 result
= radv_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
825 if (result
!= VK_SUCCESS
)
828 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
831 if (result
!= VK_SUCCESS
) {
832 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
834 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
835 pDescriptorSets
[i
] = VK_NULL_HANDLE
;
841 VkResult
radv_FreeDescriptorSets(
843 VkDescriptorPool descriptorPool
,
845 const VkDescriptorSet
* pDescriptorSets
)
847 RADV_FROM_HANDLE(radv_device
, device
, _device
);
848 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
850 for (uint32_t i
= 0; i
< count
; i
++) {
851 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
853 if (set
&& !pool
->host_memory_base
)
854 radv_descriptor_set_destroy(device
, pool
, set
, true);
859 static void write_texel_buffer_descriptor(struct radv_device
*device
,
860 struct radv_cmd_buffer
*cmd_buffer
,
862 struct radeon_winsys_bo
**buffer_list
,
863 const VkBufferView _buffer_view
)
865 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
868 memset(dst
, 0, 4 * 4);
872 memcpy(dst
, buffer_view
->state
, 4 * 4);
875 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer_view
->bo
);
877 *buffer_list
= buffer_view
->bo
;
880 static void write_buffer_descriptor(struct radv_device
*device
,
881 struct radv_cmd_buffer
*cmd_buffer
,
883 struct radeon_winsys_bo
**buffer_list
,
884 const VkDescriptorBufferInfo
*buffer_info
)
886 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
889 memset(dst
, 0, 4 * 4);
893 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
894 uint32_t range
= buffer_info
->range
;
896 if (buffer_info
->range
== VK_WHOLE_SIZE
)
897 range
= buffer
->size
- buffer_info
->offset
;
899 /* robustBufferAccess is relaxed enough to allow this (in combination
900 * with the alignment/size we return from vkGetBufferMemoryRequirements)
901 * and this allows the shader compiler to create more efficient 8/16-bit
902 * buffer accesses. */
903 range
= align(range
, 4);
905 va
+= buffer_info
->offset
+ buffer
->offset
;
907 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
909 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
910 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
911 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
912 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
914 if (device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
915 dst
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
916 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
917 S_008F0C_RESOURCE_LEVEL(1);
919 dst
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
920 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
924 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer
->bo
);
926 *buffer_list
= buffer
->bo
;
929 static void write_block_descriptor(struct radv_device
*device
,
930 struct radv_cmd_buffer
*cmd_buffer
,
932 const VkWriteDescriptorSet
*writeset
)
934 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_ub
=
935 vk_find_struct_const(writeset
->pNext
, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
937 memcpy(dst
, inline_ub
->pData
, inline_ub
->dataSize
);
940 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
941 struct radv_descriptor_range
*range
,
942 struct radeon_winsys_bo
**buffer_list
,
943 const VkDescriptorBufferInfo
*buffer_info
)
945 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
952 va
= radv_buffer_get_va(buffer
->bo
);
953 size
= buffer_info
->range
;
955 if (buffer_info
->range
== VK_WHOLE_SIZE
)
956 size
= buffer
->size
- buffer_info
->offset
;
958 /* robustBufferAccess is relaxed enough to allow this (in combination
959 * with the alignment/size we return from vkGetBufferMemoryRequirements)
960 * and this allows the shader compiler to create more efficient 8/16-bit
961 * buffer accesses. */
962 size
= align(size
, 4);
964 va
+= buffer_info
->offset
+ buffer
->offset
;
968 *buffer_list
= buffer
->bo
;
972 write_image_descriptor(struct radv_device
*device
,
973 struct radv_cmd_buffer
*cmd_buffer
,
974 unsigned size
, unsigned *dst
,
975 struct radeon_winsys_bo
**buffer_list
,
976 VkDescriptorType descriptor_type
,
977 const VkDescriptorImageInfo
*image_info
)
979 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
980 union radv_descriptor
*descriptor
;
983 memset(dst
, 0, size
);
987 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
988 descriptor
= &iview
->storage_descriptor
;
990 descriptor
= &iview
->descriptor
;
993 memcpy(dst
, descriptor
, size
);
996 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, iview
->bo
);
998 *buffer_list
= iview
->bo
;
1002 write_combined_image_sampler_descriptor(struct radv_device
*device
,
1003 struct radv_cmd_buffer
*cmd_buffer
,
1004 unsigned sampler_offset
,
1006 struct radeon_winsys_bo
**buffer_list
,
1007 VkDescriptorType descriptor_type
,
1008 const VkDescriptorImageInfo
*image_info
,
1011 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
1013 write_image_descriptor(device
, cmd_buffer
, sampler_offset
, dst
, buffer_list
,
1014 descriptor_type
, image_info
);
1015 /* copy over sampler state */
1017 memcpy(dst
+ sampler_offset
/ sizeof(*dst
), sampler
->state
, 16);
1022 write_sampler_descriptor(struct radv_device
*device
,
1024 const VkDescriptorImageInfo
*image_info
)
1026 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
1028 memcpy(dst
, sampler
->state
, 16);
1031 void radv_update_descriptor_sets(
1032 struct radv_device
* device
,
1033 struct radv_cmd_buffer
* cmd_buffer
,
1034 VkDescriptorSet dstSetOverride
,
1035 uint32_t descriptorWriteCount
,
1036 const VkWriteDescriptorSet
* pDescriptorWrites
,
1037 uint32_t descriptorCopyCount
,
1038 const VkCopyDescriptorSet
* pDescriptorCopies
)
1041 for (i
= 0; i
< descriptorWriteCount
; i
++) {
1042 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
1043 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
1044 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
1045 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1046 set
->layout
->binding
+ writeset
->dstBinding
;
1047 uint32_t *ptr
= set
->mapped_ptr
;
1048 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
1049 /* Immutable samplers are not copied into push descriptors when they are
1050 * allocated, so if we are writing push descriptors we have to copy the
1051 * immutable samplers into them now.
1053 const bool copy_immutable_samplers
= cmd_buffer
&&
1054 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
1055 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
1057 ptr
+= binding_layout
->offset
/ 4;
1059 if (writeset
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1060 write_block_descriptor(device
, cmd_buffer
, (uint8_t*)ptr
+ writeset
->dstArrayElement
, writeset
);
1064 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
1065 buffer_list
+= binding_layout
->buffer_offset
;
1066 buffer_list
+= writeset
->dstArrayElement
;
1067 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
1068 switch(writeset
->descriptorType
) {
1069 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1070 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1071 unsigned idx
= writeset
->dstArrayElement
+ j
;
1072 idx
+= binding_layout
->dynamic_offset_offset
;
1073 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1074 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1075 buffer_list
, writeset
->pBufferInfo
+ j
);
1078 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1079 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1080 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1081 writeset
->pBufferInfo
+ j
);
1083 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1084 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1085 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1086 writeset
->pTexelBufferView
[j
]);
1088 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1089 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1090 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1091 write_image_descriptor(device
, cmd_buffer
, 64, ptr
, buffer_list
,
1092 writeset
->descriptorType
,
1093 writeset
->pImageInfo
+ j
);
1095 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
: {
1096 unsigned sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
);
1097 write_combined_image_sampler_descriptor(device
, cmd_buffer
, sampler_offset
,
1099 writeset
->descriptorType
,
1100 writeset
->pImageInfo
+ j
,
1101 !binding_layout
->immutable_samplers_offset
);
1102 if (copy_immutable_samplers
) {
1103 const unsigned idx
= writeset
->dstArrayElement
+ j
;
1104 memcpy((char*)ptr
+ sampler_offset
, samplers
+ 4 * idx
, 16);
1108 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1109 if (!binding_layout
->immutable_samplers_offset
) {
1110 write_sampler_descriptor(device
, ptr
,
1111 writeset
->pImageInfo
+ j
);
1112 } else if (copy_immutable_samplers
) {
1113 unsigned idx
= writeset
->dstArrayElement
+ j
;
1114 memcpy(ptr
, samplers
+ 4 * idx
, 16);
1120 ptr
+= binding_layout
->size
/ 4;
1126 for (i
= 0; i
< descriptorCopyCount
; i
++) {
1127 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
1128 RADV_FROM_HANDLE(radv_descriptor_set
, src_set
,
1130 RADV_FROM_HANDLE(radv_descriptor_set
, dst_set
,
1132 const struct radv_descriptor_set_binding_layout
*src_binding_layout
=
1133 src_set
->layout
->binding
+ copyset
->srcBinding
;
1134 const struct radv_descriptor_set_binding_layout
*dst_binding_layout
=
1135 dst_set
->layout
->binding
+ copyset
->dstBinding
;
1136 uint32_t *src_ptr
= src_set
->mapped_ptr
;
1137 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
1138 struct radeon_winsys_bo
**src_buffer_list
= src_set
->descriptors
;
1139 struct radeon_winsys_bo
**dst_buffer_list
= dst_set
->descriptors
;
1141 src_ptr
+= src_binding_layout
->offset
/ 4;
1142 dst_ptr
+= dst_binding_layout
->offset
/ 4;
1144 if (src_binding_layout
->type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1145 src_ptr
+= copyset
->srcArrayElement
/ 4;
1146 dst_ptr
+= copyset
->dstArrayElement
/ 4;
1148 memcpy(dst_ptr
, src_ptr
, copyset
->descriptorCount
);
1152 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
1153 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
1155 src_buffer_list
+= src_binding_layout
->buffer_offset
;
1156 src_buffer_list
+= copyset
->srcArrayElement
;
1158 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
1159 dst_buffer_list
+= copyset
->dstArrayElement
;
1161 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
1162 switch (src_binding_layout
->type
) {
1163 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1164 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1165 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1166 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1167 struct radv_descriptor_range
*src_range
, *dst_range
;
1168 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
1169 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
1171 src_range
= src_set
->dynamic_descriptors
+ src_idx
;
1172 dst_range
= dst_set
->dynamic_descriptors
+ dst_idx
;
1173 *dst_range
= *src_range
;
1177 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
1179 src_ptr
+= src_binding_layout
->size
/ 4;
1180 dst_ptr
+= dst_binding_layout
->size
/ 4;
1182 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
1183 /* Sampler descriptors don't have a buffer list. */
1184 dst_buffer_list
[j
] = src_buffer_list
[j
];
1190 void radv_UpdateDescriptorSets(
1192 uint32_t descriptorWriteCount
,
1193 const VkWriteDescriptorSet
* pDescriptorWrites
,
1194 uint32_t descriptorCopyCount
,
1195 const VkCopyDescriptorSet
* pDescriptorCopies
)
1197 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1199 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
1200 descriptorCopyCount
, pDescriptorCopies
);
1203 VkResult
radv_CreateDescriptorUpdateTemplate(VkDevice _device
,
1204 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1205 const VkAllocationCallbacks
*pAllocator
,
1206 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1208 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1209 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
1210 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1211 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
1212 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
1213 struct radv_descriptor_update_template
*templ
;
1216 templ
= vk_alloc2(&device
->vk
.alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1218 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1220 vk_object_base_init(&device
->vk
, &templ
->base
,
1221 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE
);
1223 templ
->entry_count
= entry_count
;
1225 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
) {
1226 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, pCreateInfo
->pipelineLayout
);
1228 /* descriptorSetLayout should be ignored for push descriptors
1229 * and instead it refers to pipelineLayout and set.
1231 assert(pCreateInfo
->set
< MAX_SETS
);
1232 set_layout
= pipeline_layout
->set
[pCreateInfo
->set
].layout
;
1234 templ
->bind_point
= pCreateInfo
->pipelineBindPoint
;
1237 for (i
= 0; i
< entry_count
; i
++) {
1238 const VkDescriptorUpdateTemplateEntry
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1239 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1240 set_layout
->binding
+ entry
->dstBinding
;
1241 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
1242 const uint32_t *immutable_samplers
= NULL
;
1243 uint32_t dst_offset
;
1244 uint32_t dst_stride
;
1246 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1247 is dynamic, and an offset into mapped_ptr otherwise */
1248 switch (entry
->descriptorType
) {
1249 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1250 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1251 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
);
1252 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
1253 dst_stride
= 0; /* Not used */
1256 switch (entry
->descriptorType
) {
1257 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1258 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1259 /* Immutable samplers are copied into push descriptors when they are pushed */
1260 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
1261 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
1262 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
1268 dst_offset
= binding_layout
->offset
/ 4;
1269 if (entry
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
1270 dst_offset
+= entry
->dstArrayElement
/ 4;
1272 dst_offset
+= binding_layout
->size
* entry
->dstArrayElement
/ 4;
1274 dst_stride
= binding_layout
->size
/ 4;
1278 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
1279 .descriptor_type
= entry
->descriptorType
,
1280 .descriptor_count
= entry
->descriptorCount
,
1281 .src_offset
= entry
->offset
,
1282 .src_stride
= entry
->stride
,
1283 .dst_offset
= dst_offset
,
1284 .dst_stride
= dst_stride
,
1285 .buffer_offset
= buffer_offset
,
1286 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
1287 .sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
),
1288 .immutable_samplers
= immutable_samplers
1292 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
1296 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device
,
1297 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1298 const VkAllocationCallbacks
*pAllocator
)
1300 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1301 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1306 vk_object_base_finish(&templ
->base
);
1307 vk_free2(&device
->vk
.alloc
, pAllocator
, templ
);
1310 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
1311 struct radv_cmd_buffer
*cmd_buffer
,
1312 struct radv_descriptor_set
*set
,
1313 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1316 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1319 for (i
= 0; i
< templ
->entry_count
; ++i
) {
1320 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
1321 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
1322 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
1325 if (templ
->entry
[i
].descriptor_type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1326 memcpy((uint8_t*)pDst
, pSrc
, templ
->entry
[i
].descriptor_count
);
1330 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1331 switch (templ
->entry
[i
].descriptor_type
) {
1332 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1333 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1334 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
1335 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1336 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1337 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
1340 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1341 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1342 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1343 (struct VkDescriptorBufferInfo
*) pSrc
);
1345 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1346 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1347 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1348 *(VkBufferView
*) pSrc
);
1350 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1351 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1352 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1353 write_image_descriptor(device
, cmd_buffer
, 64, pDst
, buffer_list
,
1354 templ
->entry
[i
].descriptor_type
,
1355 (struct VkDescriptorImageInfo
*) pSrc
);
1357 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1358 write_combined_image_sampler_descriptor(device
, cmd_buffer
, templ
->entry
[i
].sampler_offset
,
1359 pDst
, buffer_list
, templ
->entry
[i
].descriptor_type
,
1360 (struct VkDescriptorImageInfo
*) pSrc
,
1361 templ
->entry
[i
].has_sampler
);
1362 if (templ
->entry
[i
].immutable_samplers
) {
1363 memcpy((char*)pDst
+ templ
->entry
[i
].sampler_offset
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1366 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1367 if (templ
->entry
[i
].has_sampler
)
1368 write_sampler_descriptor(device
, pDst
,
1369 (struct VkDescriptorImageInfo
*) pSrc
);
1370 else if (templ
->entry
[i
].immutable_samplers
)
1371 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1376 pSrc
+= templ
->entry
[i
].src_stride
;
1377 pDst
+= templ
->entry
[i
].dst_stride
;
1383 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device
,
1384 VkDescriptorSet descriptorSet
,
1385 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1388 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1389 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
1391 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);
1395 VkResult
radv_CreateSamplerYcbcrConversion(VkDevice _device
,
1396 const VkSamplerYcbcrConversionCreateInfo
* pCreateInfo
,
1397 const VkAllocationCallbacks
* pAllocator
,
1398 VkSamplerYcbcrConversion
* pYcbcrConversion
)
1400 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1401 struct radv_sampler_ycbcr_conversion
*conversion
= NULL
;
1403 conversion
= vk_zalloc2(&device
->vk
.alloc
, pAllocator
, sizeof(*conversion
), 8,
1404 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1406 if (conversion
== NULL
)
1407 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1409 vk_object_base_init(&device
->vk
, &conversion
->base
,
1410 VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION
);
1412 conversion
->format
= pCreateInfo
->format
;
1413 conversion
->ycbcr_model
= pCreateInfo
->ycbcrModel
;
1414 conversion
->ycbcr_range
= pCreateInfo
->ycbcrRange
;
1415 conversion
->components
= pCreateInfo
->components
;
1416 conversion
->chroma_offsets
[0] = pCreateInfo
->xChromaOffset
;
1417 conversion
->chroma_offsets
[1] = pCreateInfo
->yChromaOffset
;
1418 conversion
->chroma_filter
= pCreateInfo
->chromaFilter
;
1420 *pYcbcrConversion
= radv_sampler_ycbcr_conversion_to_handle(conversion
);
1425 void radv_DestroySamplerYcbcrConversion(VkDevice _device
,
1426 VkSamplerYcbcrConversion ycbcrConversion
,
1427 const VkAllocationCallbacks
* pAllocator
)
1429 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1430 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion
, ycbcr_conversion
, ycbcrConversion
);
1432 if (!ycbcr_conversion
)
1435 vk_object_base_finish(&ycbcr_conversion
->base
);
1436 vk_free2(&device
->vk
.alloc
, pAllocator
, ycbcr_conversion
);