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 VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*variable_flags
=
82 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
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_alloc2(&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
;
119 /* We just allocate all the samplers at the end of the struct */
120 uint32_t *samplers
= (uint32_t*)&set_layout
->binding
[max_binding
+ 1];
121 struct radv_sampler_ycbcr_conversion
*ycbcr_samplers
= NULL
;
122 uint32_t *ycbcr_sampler_offsets
= NULL
;
124 if (ycbcr_sampler_count
> 0) {
125 ycbcr_sampler_offsets
= samplers
+ 4 * immutable_sampler_count
;
126 set_layout
->ycbcr_sampler_offsets_offset
= (char*)ycbcr_sampler_offsets
- (char*)set_layout
;
127 ycbcr_samplers
= (struct radv_sampler_ycbcr_conversion
*)(ycbcr_sampler_offsets
+ max_binding
+ 1);
129 set_layout
->ycbcr_sampler_offsets_offset
= 0;
131 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
132 pCreateInfo
->bindingCount
);
134 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
135 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
138 set_layout
->binding_count
= max_binding
+ 1;
139 set_layout
->shader_stages
= 0;
140 set_layout
->dynamic_shader_stages
= 0;
141 set_layout
->has_immutable_samplers
= false;
142 set_layout
->size
= 0;
144 memset(set_layout
->binding
, 0, size
- sizeof(struct radv_descriptor_set_layout
));
146 uint32_t buffer_count
= 0;
147 uint32_t dynamic_offset_count
= 0;
149 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
150 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ j
;
151 uint32_t b
= binding
->binding
;
153 unsigned binding_buffer_count
= 0;
154 uint32_t descriptor_count
= binding
->descriptorCount
;
155 bool has_ycbcr_sampler
= false;
157 /* main image + fmask */
158 uint32_t max_sampled_image_descriptors
= 2;
160 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
&&
161 binding
->pImmutableSamplers
) {
162 for (unsigned i
= 0; i
< binding
->descriptorCount
; ++i
) {
163 struct radv_sampler_ycbcr_conversion
*conversion
=
164 radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
;
167 has_ycbcr_sampler
= true;
168 max_sampled_image_descriptors
= MAX2(max_sampled_image_descriptors
,
169 vk_format_get_plane_count(conversion
->format
));
174 switch (binding
->descriptorType
) {
175 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
176 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
177 assert(!(pCreateInfo
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
178 set_layout
->binding
[b
].dynamic_offset_count
= 1;
179 set_layout
->dynamic_shader_stages
|= binding
->stageFlags
;
180 set_layout
->binding
[b
].size
= 0;
181 binding_buffer_count
= 1;
184 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
185 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
186 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
187 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
188 set_layout
->binding
[b
].size
= 16;
189 binding_buffer_count
= 1;
192 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
193 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
194 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
195 /* main descriptor + fmask descriptor */
196 set_layout
->binding
[b
].size
= 64;
197 binding_buffer_count
= 1;
200 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
201 /* main descriptor + fmask descriptor + sampler */
202 set_layout
->binding
[b
].size
= 32 + 32 * max_sampled_image_descriptors
;
203 binding_buffer_count
= 1;
206 case VK_DESCRIPTOR_TYPE_SAMPLER
:
207 set_layout
->binding
[b
].size
= 16;
210 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
212 set_layout
->binding
[b
].size
= descriptor_count
;
213 descriptor_count
= 1;
216 unreachable("unknown descriptor type\n");
220 set_layout
->size
= align(set_layout
->size
, alignment
);
221 set_layout
->binding
[b
].type
= binding
->descriptorType
;
222 set_layout
->binding
[b
].array_size
= descriptor_count
;
223 set_layout
->binding
[b
].offset
= set_layout
->size
;
224 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
225 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
227 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
228 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
229 assert(!binding
->pImmutableSamplers
); /* Terribly ill defined how many samplers are valid */
230 assert(binding
->binding
== max_binding
);
232 set_layout
->has_variable_descriptors
= true;
235 if ((binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
236 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
237 binding
->pImmutableSamplers
) {
238 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
239 set_layout
->binding
[b
].immutable_samplers_equal
=
240 has_equal_immutable_samplers(binding
->pImmutableSamplers
, binding
->descriptorCount
);
241 set_layout
->has_immutable_samplers
= true;
244 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
245 memcpy(samplers
+ 4 * i
, &radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->state
, 16);
247 /* Don't reserve space for the samplers if they're not accessed. */
248 if (set_layout
->binding
[b
].immutable_samplers_equal
) {
249 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
250 set_layout
->binding
[b
].size
-= 32;
251 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
252 set_layout
->binding
[b
].size
-= 16;
254 samplers
+= 4 * binding
->descriptorCount
;
255 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
257 if (has_ycbcr_sampler
) {
258 ycbcr_sampler_offsets
[b
] = (const char*)ycbcr_samplers
- (const char*)set_layout
;
259 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
260 if (radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
)
261 ycbcr_samplers
[i
] = *radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
;
263 ycbcr_samplers
[i
].format
= VK_FORMAT_UNDEFINED
;
265 ycbcr_samplers
+= binding
->descriptorCount
;
269 set_layout
->size
+= descriptor_count
* set_layout
->binding
[b
].size
;
270 buffer_count
+= descriptor_count
* binding_buffer_count
;
271 dynamic_offset_count
+= descriptor_count
*
272 set_layout
->binding
[b
].dynamic_offset_count
;
273 set_layout
->shader_stages
|= binding
->stageFlags
;
278 set_layout
->buffer_count
= buffer_count
;
279 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
281 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
286 void radv_DestroyDescriptorSetLayout(
288 VkDescriptorSetLayout _set_layout
,
289 const VkAllocationCallbacks
* pAllocator
)
291 RADV_FROM_HANDLE(radv_device
, device
, _device
);
292 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
297 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
300 void radv_GetDescriptorSetLayoutSupport(VkDevice device
,
301 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
302 VkDescriptorSetLayoutSupport
* pSupport
)
304 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
305 pCreateInfo
->bindingCount
);
307 pSupport
->supported
= false;
311 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*variable_flags
=
312 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
313 VkDescriptorSetVariableDescriptorCountLayoutSupportEXT
*variable_count
=
314 vk_find_struct((void*)pCreateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT_EXT
);
315 if (variable_count
) {
316 variable_count
->maxVariableDescriptorCount
= 0;
319 bool supported
= true;
321 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
322 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ i
;
324 uint64_t descriptor_size
= 0;
325 uint64_t descriptor_alignment
= 1;
326 uint32_t descriptor_count
= binding
->descriptorCount
;
327 switch (binding
->descriptorType
) {
328 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
329 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
331 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
332 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
333 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
334 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
335 descriptor_size
= 16;
336 descriptor_alignment
= 16;
338 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
339 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
340 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
341 descriptor_size
= 64;
342 descriptor_alignment
= 32;
344 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
345 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
346 descriptor_size
= 64;
348 descriptor_size
= 96;
350 descriptor_alignment
= 32;
352 case VK_DESCRIPTOR_TYPE_SAMPLER
:
353 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
354 descriptor_size
= 16;
355 descriptor_alignment
= 16;
358 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
359 descriptor_alignment
= 16;
360 descriptor_size
= descriptor_count
;
361 descriptor_count
= 1;
364 unreachable("unknown descriptor type\n");
368 if (size
&& !align_u64(size
, descriptor_alignment
)) {
371 size
= align_u64(size
, descriptor_alignment
);
373 uint64_t max_count
= INT32_MAX
;
374 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
375 max_count
= INT32_MAX
- size
;
376 else if (descriptor_size
)
377 max_count
= (INT32_MAX
- size
) / descriptor_size
;
379 if (max_count
< descriptor_count
) {
382 if (variable_flags
&& binding
->binding
<variable_flags
->bindingCount
&& variable_count
&&
383 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
384 variable_count
->maxVariableDescriptorCount
= MIN2(UINT32_MAX
, max_count
);
386 size
+= descriptor_count
* descriptor_size
;
391 pSupport
->supported
= supported
;
395 * Pipeline layouts. These have nothing to do with the pipeline. They are
396 * just multiple descriptor set layouts pasted together.
399 VkResult
radv_CreatePipelineLayout(
401 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
402 const VkAllocationCallbacks
* pAllocator
,
403 VkPipelineLayout
* pPipelineLayout
)
405 RADV_FROM_HANDLE(radv_device
, device
, _device
);
406 struct radv_pipeline_layout
*layout
;
407 struct mesa_sha1 ctx
;
409 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
411 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
412 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
414 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
416 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
418 unsigned dynamic_offset_count
= 0;
419 uint16_t dynamic_shader_stages
= 0;
422 _mesa_sha1_init(&ctx
);
423 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
424 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
425 pCreateInfo
->pSetLayouts
[set
]);
426 layout
->set
[set
].layout
= set_layout
;
428 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
429 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
430 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
431 dynamic_shader_stages
|= set_layout
->dynamic_shader_stages
;
432 if (set_layout
->binding
[b
].immutable_samplers_offset
)
433 _mesa_sha1_update(&ctx
, radv_immutable_samplers(set_layout
, set_layout
->binding
+ b
),
434 set_layout
->binding
[b
].array_size
* 4 * sizeof(uint32_t));
436 _mesa_sha1_update(&ctx
, set_layout
->binding
,
437 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
440 layout
->dynamic_offset_count
= dynamic_offset_count
;
441 layout
->dynamic_shader_stages
= dynamic_shader_stages
;
442 layout
->push_constant_size
= 0;
444 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
445 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
446 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
447 range
->offset
+ range
->size
);
450 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
451 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
452 sizeof(layout
->push_constant_size
));
453 _mesa_sha1_final(&ctx
, layout
->sha1
);
454 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
459 void radv_DestroyPipelineLayout(
461 VkPipelineLayout _pipelineLayout
,
462 const VkAllocationCallbacks
* pAllocator
)
464 RADV_FROM_HANDLE(radv_device
, device
, _device
);
465 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
467 if (!pipeline_layout
)
469 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
475 radv_descriptor_set_create(struct radv_device
*device
,
476 struct radv_descriptor_pool
*pool
,
477 const struct radv_descriptor_set_layout
*layout
,
478 const uint32_t *variable_count
,
479 struct radv_descriptor_set
**out_set
)
481 struct radv_descriptor_set
*set
;
482 unsigned range_offset
= sizeof(struct radv_descriptor_set
) +
483 sizeof(struct radeon_winsys_bo
*) * layout
->buffer_count
;
484 unsigned mem_size
= range_offset
+
485 sizeof(struct radv_descriptor_range
) * layout
->dynamic_offset_count
;
487 if (pool
->host_memory_base
) {
488 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
489 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
491 set
= (struct radv_descriptor_set
*)pool
->host_memory_ptr
;
492 pool
->host_memory_ptr
+= mem_size
;
494 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
495 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
498 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
501 memset(set
, 0, mem_size
);
503 if (layout
->dynamic_offset_count
) {
504 set
->dynamic_descriptors
= (struct radv_descriptor_range
*)((uint8_t*)set
+ range_offset
);
507 set
->layout
= layout
;
508 uint32_t layout_size
= align_u32(layout
->size
, 32);
510 set
->size
= layout_size
;
512 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
513 vk_free2(&device
->alloc
, NULL
, set
);
514 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
517 /* try to allocate linearly first, so that we don't spend
518 * time looking for gaps if the app only allocates &
519 * resets via the pool. */
520 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
522 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
523 set
->va
= radv_buffer_get_va(set
->bo
) + pool
->current_offset
;
524 if (!pool
->host_memory_base
) {
525 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
526 pool
->entries
[pool
->entry_count
].size
= layout_size
;
527 pool
->entries
[pool
->entry_count
].set
= set
;
530 pool
->current_offset
+= layout_size
;
531 } else if (!pool
->host_memory_base
) {
535 for (index
= 0; index
< pool
->entry_count
; ++index
) {
536 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
538 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
541 if (pool
->size
- offset
< layout_size
) {
542 vk_free2(&device
->alloc
, NULL
, set
);
543 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
546 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
547 set
->va
= radv_buffer_get_va(set
->bo
) + offset
;
548 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
549 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
550 pool
->entries
[index
].offset
= offset
;
551 pool
->entries
[index
].size
= layout_size
;
552 pool
->entries
[index
].set
= set
;
555 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
558 if (layout
->has_immutable_samplers
) {
559 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
560 if (!layout
->binding
[i
].immutable_samplers_offset
||
561 layout
->binding
[i
].immutable_samplers_equal
)
564 unsigned offset
= layout
->binding
[i
].offset
/ 4;
565 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
566 offset
+= radv_combined_image_descriptor_sampler_offset(layout
->binding
+ i
) / 4;
568 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
569 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
570 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
571 offset
+= layout
->binding
[i
].size
/ 4;
581 radv_descriptor_set_destroy(struct radv_device
*device
,
582 struct radv_descriptor_pool
*pool
,
583 struct radv_descriptor_set
*set
,
586 assert(!pool
->host_memory_base
);
588 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
589 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- pool
->mapped_ptr
;
590 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
591 if (pool
->entries
[i
].offset
== offset
) {
592 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
593 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
599 vk_free2(&device
->alloc
, NULL
, set
);
602 VkResult
radv_CreateDescriptorPool(
604 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
605 const VkAllocationCallbacks
* pAllocator
,
606 VkDescriptorPool
* pDescriptorPool
)
608 RADV_FROM_HANDLE(radv_device
, device
, _device
);
609 struct radv_descriptor_pool
*pool
;
610 uint64_t size
= sizeof(struct radv_descriptor_pool
);
611 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
613 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
614 switch (ext
->sType
) {
615 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
: {
616 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*info
=
617 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*)ext
;
618 /* the sizes are 4 aligned, and we need to align to at
619 * most 32, which needs at most 28 bytes extra per
621 bo_size
+= 28llu * info
->maxInlineUniformBlockBindings
;
629 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
630 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
631 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
633 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
634 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
635 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
636 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
638 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
639 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
640 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
641 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
642 case VK_DESCRIPTOR_TYPE_SAMPLER
:
643 /* 32 as we may need to align for images */
644 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
646 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
647 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
648 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
649 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
651 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
652 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
654 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
655 bo_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
658 unreachable("unknown descriptor type\n");
663 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
664 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
665 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
666 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
669 size
+= sizeof(struct radv_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
672 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
673 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
675 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
677 memset(pool
, 0, sizeof(*pool
));
679 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
680 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
681 pool
->host_memory_ptr
= pool
->host_memory_base
;
682 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
686 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
, 32,
688 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
689 RADEON_FLAG_READ_ONLY
|
691 RADV_BO_PRIORITY_DESCRIPTOR
);
692 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
694 pool
->size
= bo_size
;
695 pool
->max_entry_count
= pCreateInfo
->maxSets
;
697 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
701 void radv_DestroyDescriptorPool(
703 VkDescriptorPool _pool
,
704 const VkAllocationCallbacks
* pAllocator
)
706 RADV_FROM_HANDLE(radv_device
, device
, _device
);
707 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
712 if (!pool
->host_memory_base
) {
713 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
714 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
719 device
->ws
->buffer_destroy(pool
->bo
);
720 vk_free2(&device
->alloc
, pAllocator
, pool
);
723 VkResult
radv_ResetDescriptorPool(
725 VkDescriptorPool descriptorPool
,
726 VkDescriptorPoolResetFlags flags
)
728 RADV_FROM_HANDLE(radv_device
, device
, _device
);
729 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
731 if (!pool
->host_memory_base
) {
732 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
733 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
735 pool
->entry_count
= 0;
738 pool
->current_offset
= 0;
739 pool
->host_memory_ptr
= pool
->host_memory_base
;
744 VkResult
radv_AllocateDescriptorSets(
746 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
747 VkDescriptorSet
* pDescriptorSets
)
749 RADV_FROM_HANDLE(radv_device
, device
, _device
);
750 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
752 VkResult result
= VK_SUCCESS
;
754 struct radv_descriptor_set
*set
= NULL
;
756 const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT
*variable_counts
=
757 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT
);
758 const uint32_t zero
= 0;
760 /* allocate a set of buffers for each shader to contain descriptors */
761 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
762 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
763 pAllocateInfo
->pSetLayouts
[i
]);
765 const uint32_t *variable_count
= NULL
;
766 if (variable_counts
) {
767 if (i
< variable_counts
->descriptorSetCount
)
768 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
770 variable_count
= &zero
;
773 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
775 result
= radv_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
776 if (result
!= VK_SUCCESS
)
779 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
782 if (result
!= VK_SUCCESS
)
783 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
788 VkResult
radv_FreeDescriptorSets(
790 VkDescriptorPool descriptorPool
,
792 const VkDescriptorSet
* pDescriptorSets
)
794 RADV_FROM_HANDLE(radv_device
, device
, _device
);
795 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
797 for (uint32_t i
= 0; i
< count
; i
++) {
798 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
800 if (set
&& !pool
->host_memory_base
)
801 radv_descriptor_set_destroy(device
, pool
, set
, true);
806 static void write_texel_buffer_descriptor(struct radv_device
*device
,
807 struct radv_cmd_buffer
*cmd_buffer
,
809 struct radeon_winsys_bo
**buffer_list
,
810 const VkBufferView _buffer_view
)
812 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
814 memcpy(dst
, buffer_view
->state
, 4 * 4);
817 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer_view
->bo
);
819 *buffer_list
= buffer_view
->bo
;
822 static void write_buffer_descriptor(struct radv_device
*device
,
823 struct radv_cmd_buffer
*cmd_buffer
,
825 struct radeon_winsys_bo
**buffer_list
,
826 const VkDescriptorBufferInfo
*buffer_info
)
828 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
829 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
830 uint32_t range
= buffer_info
->range
;
832 if (buffer_info
->range
== VK_WHOLE_SIZE
)
833 range
= buffer
->size
- buffer_info
->offset
;
835 va
+= buffer_info
->offset
+ buffer
->offset
;
837 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
839 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
840 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
841 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
842 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
843 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
844 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
847 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer
->bo
);
849 *buffer_list
= buffer
->bo
;
852 static void write_block_descriptor(struct radv_device
*device
,
853 struct radv_cmd_buffer
*cmd_buffer
,
855 const VkWriteDescriptorSet
*writeset
)
857 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_ub
=
858 vk_find_struct_const(writeset
->pNext
, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
860 memcpy(dst
, inline_ub
->pData
, inline_ub
->dataSize
);
863 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
864 struct radv_descriptor_range
*range
,
865 struct radeon_winsys_bo
**buffer_list
,
866 const VkDescriptorBufferInfo
*buffer_info
)
868 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
869 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
870 unsigned size
= buffer_info
->range
;
872 if (buffer_info
->range
== VK_WHOLE_SIZE
)
873 size
= buffer
->size
- buffer_info
->offset
;
875 va
+= buffer_info
->offset
+ buffer
->offset
;
879 *buffer_list
= buffer
->bo
;
883 write_image_descriptor(struct radv_device
*device
,
884 struct radv_cmd_buffer
*cmd_buffer
,
885 unsigned size
, unsigned *dst
,
886 struct radeon_winsys_bo
**buffer_list
,
887 VkDescriptorType descriptor_type
,
888 const VkDescriptorImageInfo
*image_info
)
890 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
891 union radv_descriptor
*descriptor
;
893 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
894 descriptor
= &iview
->storage_descriptor
;
896 descriptor
= &iview
->descriptor
;
899 memcpy(dst
, descriptor
, size
);
902 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, iview
->bo
);
904 *buffer_list
= iview
->bo
;
908 write_combined_image_sampler_descriptor(struct radv_device
*device
,
909 struct radv_cmd_buffer
*cmd_buffer
,
910 unsigned sampler_offset
,
912 struct radeon_winsys_bo
**buffer_list
,
913 VkDescriptorType descriptor_type
,
914 const VkDescriptorImageInfo
*image_info
,
917 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
919 write_image_descriptor(device
, cmd_buffer
, sampler_offset
, dst
, buffer_list
,
920 descriptor_type
, image_info
);
921 /* copy over sampler state */
923 memcpy(dst
+ sampler_offset
/ sizeof(*dst
), sampler
->state
, 16);
928 write_sampler_descriptor(struct radv_device
*device
,
930 const VkDescriptorImageInfo
*image_info
)
932 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
934 memcpy(dst
, sampler
->state
, 16);
937 void radv_update_descriptor_sets(
938 struct radv_device
* device
,
939 struct radv_cmd_buffer
* cmd_buffer
,
940 VkDescriptorSet dstSetOverride
,
941 uint32_t descriptorWriteCount
,
942 const VkWriteDescriptorSet
* pDescriptorWrites
,
943 uint32_t descriptorCopyCount
,
944 const VkCopyDescriptorSet
* pDescriptorCopies
)
947 for (i
= 0; i
< descriptorWriteCount
; i
++) {
948 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
949 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
950 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
951 const struct radv_descriptor_set_binding_layout
*binding_layout
=
952 set
->layout
->binding
+ writeset
->dstBinding
;
953 uint32_t *ptr
= set
->mapped_ptr
;
954 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
955 /* Immutable samplers are not copied into push descriptors when they are
956 * allocated, so if we are writing push descriptors we have to copy the
957 * immutable samplers into them now.
959 const bool copy_immutable_samplers
= cmd_buffer
&&
960 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
961 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
963 ptr
+= binding_layout
->offset
/ 4;
965 if (writeset
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
966 write_block_descriptor(device
, cmd_buffer
, (uint8_t*)ptr
+ writeset
->dstArrayElement
, writeset
);
970 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
971 buffer_list
+= binding_layout
->buffer_offset
;
972 buffer_list
+= writeset
->dstArrayElement
;
973 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
974 switch(writeset
->descriptorType
) {
975 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
976 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
977 unsigned idx
= writeset
->dstArrayElement
+ j
;
978 idx
+= binding_layout
->dynamic_offset_offset
;
979 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
980 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
981 buffer_list
, writeset
->pBufferInfo
+ j
);
984 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
985 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
986 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
987 writeset
->pBufferInfo
+ j
);
989 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
990 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
991 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
992 writeset
->pTexelBufferView
[j
]);
994 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
995 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
996 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
997 write_image_descriptor(device
, cmd_buffer
, 64, ptr
, buffer_list
,
998 writeset
->descriptorType
,
999 writeset
->pImageInfo
+ j
);
1001 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
: {
1002 unsigned sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
);
1003 write_combined_image_sampler_descriptor(device
, cmd_buffer
, sampler_offset
,
1005 writeset
->descriptorType
,
1006 writeset
->pImageInfo
+ j
,
1007 !binding_layout
->immutable_samplers_offset
);
1008 if (copy_immutable_samplers
) {
1009 const unsigned idx
= writeset
->dstArrayElement
+ j
;
1010 memcpy((char*)ptr
+ sampler_offset
, samplers
+ 4 * idx
, 16);
1014 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1015 if (!binding_layout
->immutable_samplers_offset
) {
1016 write_sampler_descriptor(device
, ptr
,
1017 writeset
->pImageInfo
+ j
);
1018 } else if (copy_immutable_samplers
) {
1019 unsigned idx
= writeset
->dstArrayElement
+ j
;
1020 memcpy(ptr
, samplers
+ 4 * idx
, 16);
1024 unreachable("unimplemented descriptor type");
1027 ptr
+= binding_layout
->size
/ 4;
1033 for (i
= 0; i
< descriptorCopyCount
; i
++) {
1034 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
1035 RADV_FROM_HANDLE(radv_descriptor_set
, src_set
,
1037 RADV_FROM_HANDLE(radv_descriptor_set
, dst_set
,
1039 const struct radv_descriptor_set_binding_layout
*src_binding_layout
=
1040 src_set
->layout
->binding
+ copyset
->srcBinding
;
1041 const struct radv_descriptor_set_binding_layout
*dst_binding_layout
=
1042 dst_set
->layout
->binding
+ copyset
->dstBinding
;
1043 uint32_t *src_ptr
= src_set
->mapped_ptr
;
1044 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
1045 struct radeon_winsys_bo
**src_buffer_list
= src_set
->descriptors
;
1046 struct radeon_winsys_bo
**dst_buffer_list
= dst_set
->descriptors
;
1048 src_ptr
+= src_binding_layout
->offset
/ 4;
1049 dst_ptr
+= dst_binding_layout
->offset
/ 4;
1051 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
1052 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
1054 src_buffer_list
+= src_binding_layout
->buffer_offset
;
1055 src_buffer_list
+= copyset
->srcArrayElement
;
1057 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
1058 dst_buffer_list
+= copyset
->dstArrayElement
;
1060 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
1061 switch (src_binding_layout
->type
) {
1062 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1063 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1064 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1065 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1066 struct radv_descriptor_range
*src_range
, *dst_range
;
1067 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
1068 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
1070 src_range
= src_set
->dynamic_descriptors
+ src_idx
;
1071 dst_range
= dst_set
->dynamic_descriptors
+ dst_idx
;
1072 *dst_range
= *src_range
;
1076 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
1078 src_ptr
+= src_binding_layout
->size
/ 4;
1079 dst_ptr
+= dst_binding_layout
->size
/ 4;
1081 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
1082 /* Sampler descriptors don't have a buffer list. */
1083 dst_buffer_list
[j
] = src_buffer_list
[j
];
1089 void radv_UpdateDescriptorSets(
1091 uint32_t descriptorWriteCount
,
1092 const VkWriteDescriptorSet
* pDescriptorWrites
,
1093 uint32_t descriptorCopyCount
,
1094 const VkCopyDescriptorSet
* pDescriptorCopies
)
1096 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1098 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
1099 descriptorCopyCount
, pDescriptorCopies
);
1102 VkResult
radv_CreateDescriptorUpdateTemplate(VkDevice _device
,
1103 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1104 const VkAllocationCallbacks
*pAllocator
,
1105 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1107 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1108 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
1109 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1110 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
1111 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
1112 struct radv_descriptor_update_template
*templ
;
1115 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1117 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1119 templ
->entry_count
= entry_count
;
1120 templ
->bind_point
= pCreateInfo
->pipelineBindPoint
;
1122 for (i
= 0; i
< entry_count
; i
++) {
1123 const VkDescriptorUpdateTemplateEntry
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1124 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1125 set_layout
->binding
+ entry
->dstBinding
;
1126 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
1127 const uint32_t *immutable_samplers
= NULL
;
1128 uint32_t dst_offset
;
1129 uint32_t dst_stride
;
1131 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1132 is dynamic, and an offset into mapped_ptr otherwise */
1133 switch (entry
->descriptorType
) {
1134 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1135 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1136 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
);
1137 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
1138 dst_stride
= 0; /* Not used */
1141 switch (entry
->descriptorType
) {
1142 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1143 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1144 /* Immutable samplers are copied into push descriptors when they are pushed */
1145 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
1146 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
1147 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
1153 dst_offset
= binding_layout
->offset
/ 4;
1154 if (entry
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
1155 dst_offset
+= entry
->dstArrayElement
/ 4;
1157 dst_offset
+= binding_layout
->size
* entry
->dstArrayElement
/ 4;
1159 dst_stride
= binding_layout
->size
/ 4;
1163 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
1164 .descriptor_type
= entry
->descriptorType
,
1165 .descriptor_count
= entry
->descriptorCount
,
1166 .src_offset
= entry
->offset
,
1167 .src_stride
= entry
->stride
,
1168 .dst_offset
= dst_offset
,
1169 .dst_stride
= dst_stride
,
1170 .buffer_offset
= buffer_offset
,
1171 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
1172 .sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
),
1173 .immutable_samplers
= immutable_samplers
1177 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
1181 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device
,
1182 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1183 const VkAllocationCallbacks
*pAllocator
)
1185 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1186 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1191 vk_free2(&device
->alloc
, pAllocator
, templ
);
1194 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
1195 struct radv_cmd_buffer
*cmd_buffer
,
1196 struct radv_descriptor_set
*set
,
1197 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1200 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1203 for (i
= 0; i
< templ
->entry_count
; ++i
) {
1204 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
1205 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
1206 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
1209 if (templ
->entry
[i
].descriptor_type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1210 memcpy((uint8_t*)pDst
, pSrc
, templ
->entry
[i
].descriptor_count
);
1214 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1215 switch (templ
->entry
[i
].descriptor_type
) {
1216 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1217 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1218 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
1219 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1220 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1221 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
1224 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1225 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1226 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1227 (struct VkDescriptorBufferInfo
*) pSrc
);
1229 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1230 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1231 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1232 *(VkBufferView
*) pSrc
);
1234 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1235 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1236 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1237 write_image_descriptor(device
, cmd_buffer
, 64, pDst
, buffer_list
,
1238 templ
->entry
[i
].descriptor_type
,
1239 (struct VkDescriptorImageInfo
*) pSrc
);
1241 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1242 write_combined_image_sampler_descriptor(device
, cmd_buffer
, templ
->entry
[i
].sampler_offset
,
1243 pDst
, buffer_list
, templ
->entry
[i
].descriptor_type
,
1244 (struct VkDescriptorImageInfo
*) pSrc
,
1245 templ
->entry
[i
].has_sampler
);
1246 if (templ
->entry
[i
].immutable_samplers
) {
1247 memcpy((char*)pDst
+ templ
->entry
[i
].sampler_offset
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1250 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1251 if (templ
->entry
[i
].has_sampler
)
1252 write_sampler_descriptor(device
, pDst
,
1253 (struct VkDescriptorImageInfo
*) pSrc
);
1254 else if (templ
->entry
[i
].immutable_samplers
)
1255 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1258 unreachable("unimplemented descriptor type");
1261 pSrc
+= templ
->entry
[i
].src_stride
;
1262 pDst
+= templ
->entry
[i
].dst_stride
;
1268 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device
,
1269 VkDescriptorSet descriptorSet
,
1270 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1273 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1274 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
1276 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);
1280 VkResult
radv_CreateSamplerYcbcrConversion(VkDevice _device
,
1281 const VkSamplerYcbcrConversionCreateInfo
* pCreateInfo
,
1282 const VkAllocationCallbacks
* pAllocator
,
1283 VkSamplerYcbcrConversion
* pYcbcrConversion
)
1285 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1286 struct radv_sampler_ycbcr_conversion
*conversion
= NULL
;
1288 conversion
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*conversion
), 8,
1289 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1291 if (conversion
== NULL
)
1292 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1294 conversion
->format
= pCreateInfo
->format
;
1295 conversion
->ycbcr_model
= pCreateInfo
->ycbcrModel
;
1296 conversion
->ycbcr_range
= pCreateInfo
->ycbcrRange
;
1297 conversion
->components
= pCreateInfo
->components
;
1298 conversion
->chroma_offsets
[0] = pCreateInfo
->xChromaOffset
;
1299 conversion
->chroma_offsets
[1] = pCreateInfo
->yChromaOffset
;
1300 conversion
->chroma_filter
= pCreateInfo
->chromaFilter
;
1302 *pYcbcrConversion
= radv_sampler_ycbcr_conversion_to_handle(conversion
);
1307 void radv_DestroySamplerYcbcrConversion(VkDevice _device
,
1308 VkSamplerYcbcrConversion ycbcrConversion
,
1309 const VkAllocationCallbacks
* pAllocator
)
1311 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1312 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion
, ycbcr_conversion
, ycbcrConversion
);
1314 if (ycbcr_conversion
)
1315 vk_free2(&device
->alloc
, pAllocator
, ycbcr_conversion
);