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
;
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;
217 unreachable("unknown descriptor type\n");
221 set_layout
->size
= align(set_layout
->size
, alignment
);
222 set_layout
->binding
[b
].type
= binding
->descriptorType
;
223 set_layout
->binding
[b
].array_size
= descriptor_count
;
224 set_layout
->binding
[b
].offset
= set_layout
->size
;
225 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
226 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
228 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
229 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
230 assert(!binding
->pImmutableSamplers
); /* Terribly ill defined how many samplers are valid */
231 assert(binding
->binding
== max_binding
);
233 set_layout
->has_variable_descriptors
= true;
236 if ((binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
237 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
238 binding
->pImmutableSamplers
) {
239 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
240 set_layout
->binding
[b
].immutable_samplers_equal
=
241 has_equal_immutable_samplers(binding
->pImmutableSamplers
, binding
->descriptorCount
);
242 set_layout
->has_immutable_samplers
= true;
245 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
246 memcpy(samplers
+ 4 * i
, &radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->state
, 16);
248 /* Don't reserve space for the samplers if they're not accessed. */
249 if (set_layout
->binding
[b
].immutable_samplers_equal
) {
250 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
&&
251 max_sampled_image_descriptors
<= 2)
252 set_layout
->binding
[b
].size
-= 32;
253 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
254 set_layout
->binding
[b
].size
-= 16;
256 samplers
+= 4 * binding
->descriptorCount
;
257 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
259 if (has_ycbcr_sampler
) {
260 ycbcr_sampler_offsets
[b
] = (const char*)ycbcr_samplers
- (const char*)set_layout
;
261 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
262 if (radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
)
263 ycbcr_samplers
[i
] = *radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
;
265 ycbcr_samplers
[i
].format
= VK_FORMAT_UNDEFINED
;
267 ycbcr_samplers
+= binding
->descriptorCount
;
271 set_layout
->size
+= descriptor_count
* set_layout
->binding
[b
].size
;
272 buffer_count
+= descriptor_count
* binding_buffer_count
;
273 dynamic_offset_count
+= descriptor_count
*
274 set_layout
->binding
[b
].dynamic_offset_count
;
275 set_layout
->shader_stages
|= binding
->stageFlags
;
280 set_layout
->buffer_count
= buffer_count
;
281 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
283 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
288 void radv_DestroyDescriptorSetLayout(
290 VkDescriptorSetLayout _set_layout
,
291 const VkAllocationCallbacks
* pAllocator
)
293 RADV_FROM_HANDLE(radv_device
, device
, _device
);
294 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
299 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
302 void radv_GetDescriptorSetLayoutSupport(VkDevice device
,
303 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
304 VkDescriptorSetLayoutSupport
* pSupport
)
306 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
307 pCreateInfo
->bindingCount
);
309 pSupport
->supported
= false;
313 const VkDescriptorSetLayoutBindingFlagsCreateInfo
*variable_flags
=
314 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO
);
315 VkDescriptorSetVariableDescriptorCountLayoutSupport
*variable_count
=
316 vk_find_struct((void*)pCreateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT
);
317 if (variable_count
) {
318 variable_count
->maxVariableDescriptorCount
= 0;
321 bool supported
= true;
323 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
324 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ i
;
326 uint64_t descriptor_size
= 0;
327 uint64_t descriptor_alignment
= 1;
328 uint32_t descriptor_count
= binding
->descriptorCount
;
329 switch (binding
->descriptorType
) {
330 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
331 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
333 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
334 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
335 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
336 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
337 descriptor_size
= 16;
338 descriptor_alignment
= 16;
340 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
341 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
342 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
343 descriptor_size
= 64;
344 descriptor_alignment
= 32;
346 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
347 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
348 descriptor_size
= 64;
350 descriptor_size
= 96;
352 descriptor_alignment
= 32;
354 case VK_DESCRIPTOR_TYPE_SAMPLER
:
355 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
356 descriptor_size
= 16;
357 descriptor_alignment
= 16;
360 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
361 descriptor_alignment
= 16;
362 descriptor_size
= descriptor_count
;
363 descriptor_count
= 1;
366 unreachable("unknown descriptor type\n");
370 if (size
&& !align_u64(size
, descriptor_alignment
)) {
373 size
= align_u64(size
, descriptor_alignment
);
375 uint64_t max_count
= INT32_MAX
;
376 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
377 max_count
= INT32_MAX
- size
;
378 else if (descriptor_size
)
379 max_count
= (INT32_MAX
- size
) / descriptor_size
;
381 if (max_count
< descriptor_count
) {
384 if (variable_flags
&& binding
->binding
<variable_flags
->bindingCount
&& variable_count
&&
385 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
386 variable_count
->maxVariableDescriptorCount
= MIN2(UINT32_MAX
, max_count
);
388 size
+= descriptor_count
* descriptor_size
;
393 pSupport
->supported
= supported
;
397 * Pipeline layouts. These have nothing to do with the pipeline. They are
398 * just multiple descriptor set layouts pasted together.
401 VkResult
radv_CreatePipelineLayout(
403 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
404 const VkAllocationCallbacks
* pAllocator
,
405 VkPipelineLayout
* pPipelineLayout
)
407 RADV_FROM_HANDLE(radv_device
, device
, _device
);
408 struct radv_pipeline_layout
*layout
;
409 struct mesa_sha1 ctx
;
411 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
413 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
414 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
416 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
418 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
420 unsigned dynamic_offset_count
= 0;
421 uint16_t dynamic_shader_stages
= 0;
424 _mesa_sha1_init(&ctx
);
425 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
426 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
427 pCreateInfo
->pSetLayouts
[set
]);
428 layout
->set
[set
].layout
= set_layout
;
430 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
431 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
432 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
433 dynamic_shader_stages
|= set_layout
->dynamic_shader_stages
;
435 _mesa_sha1_update(&ctx
, set_layout
, set_layout
->layout_size
);
438 layout
->dynamic_offset_count
= dynamic_offset_count
;
439 layout
->dynamic_shader_stages
= dynamic_shader_stages
;
440 layout
->push_constant_size
= 0;
442 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
443 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
444 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
445 range
->offset
+ range
->size
);
448 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
449 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
450 sizeof(layout
->push_constant_size
));
451 _mesa_sha1_final(&ctx
, layout
->sha1
);
452 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
457 void radv_DestroyPipelineLayout(
459 VkPipelineLayout _pipelineLayout
,
460 const VkAllocationCallbacks
* pAllocator
)
462 RADV_FROM_HANDLE(radv_device
, device
, _device
);
463 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
465 if (!pipeline_layout
)
467 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
473 radv_descriptor_set_create(struct radv_device
*device
,
474 struct radv_descriptor_pool
*pool
,
475 const struct radv_descriptor_set_layout
*layout
,
476 const uint32_t *variable_count
,
477 struct radv_descriptor_set
**out_set
)
479 struct radv_descriptor_set
*set
;
480 uint32_t buffer_count
= layout
->buffer_count
;
481 if (variable_count
) {
483 if (layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
484 layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
486 buffer_count
= layout
->binding
[layout
->binding_count
- 1].buffer_offset
+
487 *variable_count
* stride
;
489 unsigned range_offset
= sizeof(struct radv_descriptor_set
) +
490 sizeof(struct radeon_winsys_bo
*) * buffer_count
;
491 unsigned mem_size
= range_offset
+
492 sizeof(struct radv_descriptor_range
) * layout
->dynamic_offset_count
;
494 if (pool
->host_memory_base
) {
495 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
496 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
498 set
= (struct radv_descriptor_set
*)pool
->host_memory_ptr
;
499 pool
->host_memory_ptr
+= mem_size
;
501 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
502 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
505 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
508 memset(set
, 0, mem_size
);
510 if (layout
->dynamic_offset_count
) {
511 set
->dynamic_descriptors
= (struct radv_descriptor_range
*)((uint8_t*)set
+ range_offset
);
514 set
->layout
= layout
;
515 uint32_t layout_size
= layout
->size
;
516 if (variable_count
) {
517 assert(layout
->has_variable_descriptors
);
518 uint32_t stride
= layout
->binding
[layout
->binding_count
- 1].size
;
519 if (layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
522 layout_size
= layout
->binding
[layout
->binding_count
- 1].offset
+
523 *variable_count
* stride
;
525 layout_size
= align_u32(layout_size
, 32);
527 set
->size
= layout_size
;
529 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
530 vk_free2(&device
->alloc
, NULL
, set
);
531 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
534 /* try to allocate linearly first, so that we don't spend
535 * time looking for gaps if the app only allocates &
536 * resets via the pool. */
537 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
539 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
540 set
->va
= radv_buffer_get_va(set
->bo
) + pool
->current_offset
;
541 if (!pool
->host_memory_base
) {
542 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
543 pool
->entries
[pool
->entry_count
].size
= layout_size
;
544 pool
->entries
[pool
->entry_count
].set
= set
;
547 pool
->current_offset
+= layout_size
;
548 } else if (!pool
->host_memory_base
) {
552 for (index
= 0; index
< pool
->entry_count
; ++index
) {
553 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
555 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
558 if (pool
->size
- offset
< layout_size
) {
559 vk_free2(&device
->alloc
, NULL
, set
);
560 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
563 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
564 set
->va
= radv_buffer_get_va(set
->bo
) + offset
;
565 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
566 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
567 pool
->entries
[index
].offset
= offset
;
568 pool
->entries
[index
].size
= layout_size
;
569 pool
->entries
[index
].set
= set
;
572 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
575 if (layout
->has_immutable_samplers
) {
576 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
577 if (!layout
->binding
[i
].immutable_samplers_offset
||
578 layout
->binding
[i
].immutable_samplers_equal
)
581 unsigned offset
= layout
->binding
[i
].offset
/ 4;
582 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
583 offset
+= radv_combined_image_descriptor_sampler_offset(layout
->binding
+ i
) / 4;
585 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
586 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
587 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
588 offset
+= layout
->binding
[i
].size
/ 4;
598 radv_descriptor_set_destroy(struct radv_device
*device
,
599 struct radv_descriptor_pool
*pool
,
600 struct radv_descriptor_set
*set
,
603 assert(!pool
->host_memory_base
);
605 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
606 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- pool
->mapped_ptr
;
607 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
608 if (pool
->entries
[i
].offset
== offset
) {
609 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
610 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
616 vk_free2(&device
->alloc
, NULL
, set
);
619 VkResult
radv_CreateDescriptorPool(
621 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
622 const VkAllocationCallbacks
* pAllocator
,
623 VkDescriptorPool
* pDescriptorPool
)
625 RADV_FROM_HANDLE(radv_device
, device
, _device
);
626 struct radv_descriptor_pool
*pool
;
627 uint64_t size
= sizeof(struct radv_descriptor_pool
);
628 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
630 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
631 switch (ext
->sType
) {
632 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
: {
633 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*info
=
634 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*)ext
;
635 /* the sizes are 4 aligned, and we need to align to at
636 * most 32, which needs at most 28 bytes extra per
638 bo_size
+= 28llu * info
->maxInlineUniformBlockBindings
;
646 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
647 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
648 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
650 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
651 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
652 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
653 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
655 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
656 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
657 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
658 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
659 case VK_DESCRIPTOR_TYPE_SAMPLER
:
660 /* 32 as we may need to align for images */
661 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
663 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
664 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
665 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
666 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
668 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
669 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
671 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
672 bo_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
675 unreachable("unknown descriptor type\n");
680 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
681 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
682 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
683 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
686 size
+= sizeof(struct radv_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
689 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
690 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
692 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
694 memset(pool
, 0, sizeof(*pool
));
696 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
697 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
698 pool
->host_memory_ptr
= pool
->host_memory_base
;
699 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
703 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
, 32,
705 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
706 RADEON_FLAG_READ_ONLY
|
708 RADV_BO_PRIORITY_DESCRIPTOR
);
709 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
711 pool
->size
= bo_size
;
712 pool
->max_entry_count
= pCreateInfo
->maxSets
;
714 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
718 void radv_DestroyDescriptorPool(
720 VkDescriptorPool _pool
,
721 const VkAllocationCallbacks
* pAllocator
)
723 RADV_FROM_HANDLE(radv_device
, device
, _device
);
724 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
729 if (!pool
->host_memory_base
) {
730 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
731 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
736 device
->ws
->buffer_destroy(pool
->bo
);
737 vk_free2(&device
->alloc
, pAllocator
, pool
);
740 VkResult
radv_ResetDescriptorPool(
742 VkDescriptorPool descriptorPool
,
743 VkDescriptorPoolResetFlags flags
)
745 RADV_FROM_HANDLE(radv_device
, device
, _device
);
746 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
748 if (!pool
->host_memory_base
) {
749 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
750 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
752 pool
->entry_count
= 0;
755 pool
->current_offset
= 0;
756 pool
->host_memory_ptr
= pool
->host_memory_base
;
761 VkResult
radv_AllocateDescriptorSets(
763 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
764 VkDescriptorSet
* pDescriptorSets
)
766 RADV_FROM_HANDLE(radv_device
, device
, _device
);
767 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
769 VkResult result
= VK_SUCCESS
;
771 struct radv_descriptor_set
*set
= NULL
;
773 const VkDescriptorSetVariableDescriptorCountAllocateInfo
*variable_counts
=
774 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO
);
775 const uint32_t zero
= 0;
777 /* allocate a set of buffers for each shader to contain descriptors */
778 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
779 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
780 pAllocateInfo
->pSetLayouts
[i
]);
782 const uint32_t *variable_count
= NULL
;
783 if (variable_counts
) {
784 if (i
< variable_counts
->descriptorSetCount
)
785 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
787 variable_count
= &zero
;
790 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
792 result
= radv_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
793 if (result
!= VK_SUCCESS
)
796 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
799 if (result
!= VK_SUCCESS
) {
800 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
802 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
803 pDescriptorSets
[i
] = VK_NULL_HANDLE
;
809 VkResult
radv_FreeDescriptorSets(
811 VkDescriptorPool descriptorPool
,
813 const VkDescriptorSet
* pDescriptorSets
)
815 RADV_FROM_HANDLE(radv_device
, device
, _device
);
816 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
818 for (uint32_t i
= 0; i
< count
; i
++) {
819 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
821 if (set
&& !pool
->host_memory_base
)
822 radv_descriptor_set_destroy(device
, pool
, set
, true);
827 static void write_texel_buffer_descriptor(struct radv_device
*device
,
828 struct radv_cmd_buffer
*cmd_buffer
,
830 struct radeon_winsys_bo
**buffer_list
,
831 const VkBufferView _buffer_view
)
833 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
835 memcpy(dst
, buffer_view
->state
, 4 * 4);
838 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer_view
->bo
);
840 *buffer_list
= buffer_view
->bo
;
843 static void write_buffer_descriptor(struct radv_device
*device
,
844 struct radv_cmd_buffer
*cmd_buffer
,
846 struct radeon_winsys_bo
**buffer_list
,
847 const VkDescriptorBufferInfo
*buffer_info
)
849 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
850 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
851 uint32_t range
= buffer_info
->range
;
853 if (buffer_info
->range
== VK_WHOLE_SIZE
)
854 range
= buffer
->size
- buffer_info
->offset
;
856 va
+= buffer_info
->offset
+ buffer
->offset
;
858 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
860 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
861 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
862 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
863 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
865 if (device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
866 dst
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
867 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
868 S_008F0C_RESOURCE_LEVEL(1);
870 dst
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
871 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
875 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer
->bo
);
877 *buffer_list
= buffer
->bo
;
880 static void write_block_descriptor(struct radv_device
*device
,
881 struct radv_cmd_buffer
*cmd_buffer
,
883 const VkWriteDescriptorSet
*writeset
)
885 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_ub
=
886 vk_find_struct_const(writeset
->pNext
, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
888 memcpy(dst
, inline_ub
->pData
, inline_ub
->dataSize
);
891 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
892 struct radv_descriptor_range
*range
,
893 struct radeon_winsys_bo
**buffer_list
,
894 const VkDescriptorBufferInfo
*buffer_info
)
896 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
897 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
898 unsigned size
= buffer_info
->range
;
900 if (buffer_info
->range
== VK_WHOLE_SIZE
)
901 size
= buffer
->size
- buffer_info
->offset
;
903 va
+= buffer_info
->offset
+ buffer
->offset
;
907 *buffer_list
= buffer
->bo
;
911 write_image_descriptor(struct radv_device
*device
,
912 struct radv_cmd_buffer
*cmd_buffer
,
913 unsigned size
, unsigned *dst
,
914 struct radeon_winsys_bo
**buffer_list
,
915 VkDescriptorType descriptor_type
,
916 const VkDescriptorImageInfo
*image_info
)
918 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
919 union radv_descriptor
*descriptor
;
921 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
922 descriptor
= &iview
->storage_descriptor
;
924 descriptor
= &iview
->descriptor
;
927 memcpy(dst
, descriptor
, size
);
930 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, iview
->bo
);
932 *buffer_list
= iview
->bo
;
936 write_combined_image_sampler_descriptor(struct radv_device
*device
,
937 struct radv_cmd_buffer
*cmd_buffer
,
938 unsigned sampler_offset
,
940 struct radeon_winsys_bo
**buffer_list
,
941 VkDescriptorType descriptor_type
,
942 const VkDescriptorImageInfo
*image_info
,
945 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
947 write_image_descriptor(device
, cmd_buffer
, sampler_offset
, dst
, buffer_list
,
948 descriptor_type
, image_info
);
949 /* copy over sampler state */
951 memcpy(dst
+ sampler_offset
/ sizeof(*dst
), sampler
->state
, 16);
956 write_sampler_descriptor(struct radv_device
*device
,
958 const VkDescriptorImageInfo
*image_info
)
960 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
962 memcpy(dst
, sampler
->state
, 16);
965 void radv_update_descriptor_sets(
966 struct radv_device
* device
,
967 struct radv_cmd_buffer
* cmd_buffer
,
968 VkDescriptorSet dstSetOverride
,
969 uint32_t descriptorWriteCount
,
970 const VkWriteDescriptorSet
* pDescriptorWrites
,
971 uint32_t descriptorCopyCount
,
972 const VkCopyDescriptorSet
* pDescriptorCopies
)
975 for (i
= 0; i
< descriptorWriteCount
; i
++) {
976 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
977 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
978 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
979 const struct radv_descriptor_set_binding_layout
*binding_layout
=
980 set
->layout
->binding
+ writeset
->dstBinding
;
981 uint32_t *ptr
= set
->mapped_ptr
;
982 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
983 /* Immutable samplers are not copied into push descriptors when they are
984 * allocated, so if we are writing push descriptors we have to copy the
985 * immutable samplers into them now.
987 const bool copy_immutable_samplers
= cmd_buffer
&&
988 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
989 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
991 ptr
+= binding_layout
->offset
/ 4;
993 if (writeset
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
994 write_block_descriptor(device
, cmd_buffer
, (uint8_t*)ptr
+ writeset
->dstArrayElement
, writeset
);
998 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
999 buffer_list
+= binding_layout
->buffer_offset
;
1000 buffer_list
+= writeset
->dstArrayElement
;
1001 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
1002 switch(writeset
->descriptorType
) {
1003 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1004 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1005 unsigned idx
= writeset
->dstArrayElement
+ j
;
1006 idx
+= binding_layout
->dynamic_offset_offset
;
1007 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1008 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1009 buffer_list
, writeset
->pBufferInfo
+ j
);
1012 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1013 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1014 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1015 writeset
->pBufferInfo
+ j
);
1017 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1018 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1019 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1020 writeset
->pTexelBufferView
[j
]);
1022 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1023 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1024 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1025 write_image_descriptor(device
, cmd_buffer
, 64, ptr
, buffer_list
,
1026 writeset
->descriptorType
,
1027 writeset
->pImageInfo
+ j
);
1029 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
: {
1030 unsigned sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
);
1031 write_combined_image_sampler_descriptor(device
, cmd_buffer
, sampler_offset
,
1033 writeset
->descriptorType
,
1034 writeset
->pImageInfo
+ j
,
1035 !binding_layout
->immutable_samplers_offset
);
1036 if (copy_immutable_samplers
) {
1037 const unsigned idx
= writeset
->dstArrayElement
+ j
;
1038 memcpy((char*)ptr
+ sampler_offset
, samplers
+ 4 * idx
, 16);
1042 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1043 if (!binding_layout
->immutable_samplers_offset
) {
1044 write_sampler_descriptor(device
, ptr
,
1045 writeset
->pImageInfo
+ j
);
1046 } else if (copy_immutable_samplers
) {
1047 unsigned idx
= writeset
->dstArrayElement
+ j
;
1048 memcpy(ptr
, samplers
+ 4 * idx
, 16);
1052 unreachable("unimplemented descriptor type");
1055 ptr
+= binding_layout
->size
/ 4;
1061 for (i
= 0; i
< descriptorCopyCount
; i
++) {
1062 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
1063 RADV_FROM_HANDLE(radv_descriptor_set
, src_set
,
1065 RADV_FROM_HANDLE(radv_descriptor_set
, dst_set
,
1067 const struct radv_descriptor_set_binding_layout
*src_binding_layout
=
1068 src_set
->layout
->binding
+ copyset
->srcBinding
;
1069 const struct radv_descriptor_set_binding_layout
*dst_binding_layout
=
1070 dst_set
->layout
->binding
+ copyset
->dstBinding
;
1071 uint32_t *src_ptr
= src_set
->mapped_ptr
;
1072 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
1073 struct radeon_winsys_bo
**src_buffer_list
= src_set
->descriptors
;
1074 struct radeon_winsys_bo
**dst_buffer_list
= dst_set
->descriptors
;
1076 src_ptr
+= src_binding_layout
->offset
/ 4;
1077 dst_ptr
+= dst_binding_layout
->offset
/ 4;
1079 if (src_binding_layout
->type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1080 src_ptr
+= copyset
->srcArrayElement
/ 4;
1081 dst_ptr
+= copyset
->dstArrayElement
/ 4;
1083 memcpy(dst_ptr
, src_ptr
, copyset
->descriptorCount
);
1087 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
1088 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
1090 src_buffer_list
+= src_binding_layout
->buffer_offset
;
1091 src_buffer_list
+= copyset
->srcArrayElement
;
1093 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
1094 dst_buffer_list
+= copyset
->dstArrayElement
;
1096 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
1097 switch (src_binding_layout
->type
) {
1098 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1099 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1100 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1101 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1102 struct radv_descriptor_range
*src_range
, *dst_range
;
1103 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
1104 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
1106 src_range
= src_set
->dynamic_descriptors
+ src_idx
;
1107 dst_range
= dst_set
->dynamic_descriptors
+ dst_idx
;
1108 *dst_range
= *src_range
;
1112 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
1114 src_ptr
+= src_binding_layout
->size
/ 4;
1115 dst_ptr
+= dst_binding_layout
->size
/ 4;
1117 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
1118 /* Sampler descriptors don't have a buffer list. */
1119 dst_buffer_list
[j
] = src_buffer_list
[j
];
1125 void radv_UpdateDescriptorSets(
1127 uint32_t descriptorWriteCount
,
1128 const VkWriteDescriptorSet
* pDescriptorWrites
,
1129 uint32_t descriptorCopyCount
,
1130 const VkCopyDescriptorSet
* pDescriptorCopies
)
1132 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1134 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
1135 descriptorCopyCount
, pDescriptorCopies
);
1138 VkResult
radv_CreateDescriptorUpdateTemplate(VkDevice _device
,
1139 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1140 const VkAllocationCallbacks
*pAllocator
,
1141 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1143 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1144 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
1145 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1146 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
1147 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
1148 struct radv_descriptor_update_template
*templ
;
1151 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1153 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1155 templ
->entry_count
= entry_count
;
1157 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
) {
1158 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, pCreateInfo
->pipelineLayout
);
1160 /* descriptorSetLayout should be ignored for push descriptors
1161 * and instead it refers to pipelineLayout and set.
1163 assert(pCreateInfo
->set
< MAX_SETS
);
1164 set_layout
= pipeline_layout
->set
[pCreateInfo
->set
].layout
;
1166 templ
->bind_point
= pCreateInfo
->pipelineBindPoint
;
1169 for (i
= 0; i
< entry_count
; i
++) {
1170 const VkDescriptorUpdateTemplateEntry
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1171 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1172 set_layout
->binding
+ entry
->dstBinding
;
1173 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
1174 const uint32_t *immutable_samplers
= NULL
;
1175 uint32_t dst_offset
;
1176 uint32_t dst_stride
;
1178 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1179 is dynamic, and an offset into mapped_ptr otherwise */
1180 switch (entry
->descriptorType
) {
1181 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1182 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1183 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
);
1184 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
1185 dst_stride
= 0; /* Not used */
1188 switch (entry
->descriptorType
) {
1189 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1190 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1191 /* Immutable samplers are copied into push descriptors when they are pushed */
1192 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
1193 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
1194 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
1200 dst_offset
= binding_layout
->offset
/ 4;
1201 if (entry
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
1202 dst_offset
+= entry
->dstArrayElement
/ 4;
1204 dst_offset
+= binding_layout
->size
* entry
->dstArrayElement
/ 4;
1206 dst_stride
= binding_layout
->size
/ 4;
1210 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
1211 .descriptor_type
= entry
->descriptorType
,
1212 .descriptor_count
= entry
->descriptorCount
,
1213 .src_offset
= entry
->offset
,
1214 .src_stride
= entry
->stride
,
1215 .dst_offset
= dst_offset
,
1216 .dst_stride
= dst_stride
,
1217 .buffer_offset
= buffer_offset
,
1218 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
1219 .sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
),
1220 .immutable_samplers
= immutable_samplers
1224 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
1228 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device
,
1229 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1230 const VkAllocationCallbacks
*pAllocator
)
1232 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1233 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1238 vk_free2(&device
->alloc
, pAllocator
, templ
);
1241 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
1242 struct radv_cmd_buffer
*cmd_buffer
,
1243 struct radv_descriptor_set
*set
,
1244 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1247 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1250 for (i
= 0; i
< templ
->entry_count
; ++i
) {
1251 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
1252 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
1253 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
1256 if (templ
->entry
[i
].descriptor_type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1257 memcpy((uint8_t*)pDst
, pSrc
, templ
->entry
[i
].descriptor_count
);
1261 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1262 switch (templ
->entry
[i
].descriptor_type
) {
1263 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1264 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1265 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
1266 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1267 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1268 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
1271 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1272 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1273 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1274 (struct VkDescriptorBufferInfo
*) pSrc
);
1276 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1277 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1278 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1279 *(VkBufferView
*) pSrc
);
1281 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1282 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1283 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1284 write_image_descriptor(device
, cmd_buffer
, 64, pDst
, buffer_list
,
1285 templ
->entry
[i
].descriptor_type
,
1286 (struct VkDescriptorImageInfo
*) pSrc
);
1288 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1289 write_combined_image_sampler_descriptor(device
, cmd_buffer
, templ
->entry
[i
].sampler_offset
,
1290 pDst
, buffer_list
, templ
->entry
[i
].descriptor_type
,
1291 (struct VkDescriptorImageInfo
*) pSrc
,
1292 templ
->entry
[i
].has_sampler
);
1293 if (templ
->entry
[i
].immutable_samplers
) {
1294 memcpy((char*)pDst
+ templ
->entry
[i
].sampler_offset
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1297 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1298 if (templ
->entry
[i
].has_sampler
)
1299 write_sampler_descriptor(device
, pDst
,
1300 (struct VkDescriptorImageInfo
*) pSrc
);
1301 else if (templ
->entry
[i
].immutable_samplers
)
1302 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1305 unreachable("unimplemented descriptor type");
1308 pSrc
+= templ
->entry
[i
].src_stride
;
1309 pDst
+= templ
->entry
[i
].dst_stride
;
1315 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device
,
1316 VkDescriptorSet descriptorSet
,
1317 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1320 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1321 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
1323 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);
1327 VkResult
radv_CreateSamplerYcbcrConversion(VkDevice _device
,
1328 const VkSamplerYcbcrConversionCreateInfo
* pCreateInfo
,
1329 const VkAllocationCallbacks
* pAllocator
,
1330 VkSamplerYcbcrConversion
* pYcbcrConversion
)
1332 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1333 struct radv_sampler_ycbcr_conversion
*conversion
= NULL
;
1335 conversion
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*conversion
), 8,
1336 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1338 if (conversion
== NULL
)
1339 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1341 conversion
->format
= pCreateInfo
->format
;
1342 conversion
->ycbcr_model
= pCreateInfo
->ycbcrModel
;
1343 conversion
->ycbcr_range
= pCreateInfo
->ycbcrRange
;
1344 conversion
->components
= pCreateInfo
->components
;
1345 conversion
->chroma_offsets
[0] = pCreateInfo
->xChromaOffset
;
1346 conversion
->chroma_offsets
[1] = pCreateInfo
->yChromaOffset
;
1347 conversion
->chroma_filter
= pCreateInfo
->chromaFilter
;
1349 *pYcbcrConversion
= radv_sampler_ycbcr_conversion_to_handle(conversion
);
1354 void radv_DestroySamplerYcbcrConversion(VkDevice _device
,
1355 VkSamplerYcbcrConversion ycbcrConversion
,
1356 const VkAllocationCallbacks
* pAllocator
)
1358 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1359 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion
, ycbcr_conversion
, ycbcrConversion
);
1361 if (ycbcr_conversion
)
1362 vk_free2(&device
->alloc
, pAllocator
, ycbcr_conversion
);