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_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
= 32 + 32 * max_sampled_image_descriptors
;
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 set_layout
->binding
[b
].size
-= 32;
252 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
253 set_layout
->binding
[b
].size
-= 16;
255 samplers
+= 4 * binding
->descriptorCount
;
256 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
258 if (has_ycbcr_sampler
) {
259 ycbcr_sampler_offsets
[b
] = (const char*)ycbcr_samplers
- (const char*)set_layout
;
260 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
261 if (radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
)
262 ycbcr_samplers
[i
] = *radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
;
264 ycbcr_samplers
[i
].format
= VK_FORMAT_UNDEFINED
;
266 ycbcr_samplers
+= binding
->descriptorCount
;
270 set_layout
->size
+= descriptor_count
* set_layout
->binding
[b
].size
;
271 buffer_count
+= descriptor_count
* binding_buffer_count
;
272 dynamic_offset_count
+= descriptor_count
*
273 set_layout
->binding
[b
].dynamic_offset_count
;
274 set_layout
->shader_stages
|= binding
->stageFlags
;
279 set_layout
->buffer_count
= buffer_count
;
280 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
282 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
287 void radv_DestroyDescriptorSetLayout(
289 VkDescriptorSetLayout _set_layout
,
290 const VkAllocationCallbacks
* pAllocator
)
292 RADV_FROM_HANDLE(radv_device
, device
, _device
);
293 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
298 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
301 void radv_GetDescriptorSetLayoutSupport(VkDevice device
,
302 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
303 VkDescriptorSetLayoutSupport
* pSupport
)
305 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
306 pCreateInfo
->bindingCount
);
308 pSupport
->supported
= false;
312 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*variable_flags
=
313 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
314 VkDescriptorSetVariableDescriptorCountLayoutSupportEXT
*variable_count
=
315 vk_find_struct((void*)pCreateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT_EXT
);
316 if (variable_count
) {
317 variable_count
->maxVariableDescriptorCount
= 0;
320 bool supported
= true;
322 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
323 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ i
;
325 uint64_t descriptor_size
= 0;
326 uint64_t descriptor_alignment
= 1;
327 uint32_t descriptor_count
= binding
->descriptorCount
;
328 switch (binding
->descriptorType
) {
329 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
330 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
332 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
333 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
334 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
335 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
336 descriptor_size
= 16;
337 descriptor_alignment
= 16;
339 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
340 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
341 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
342 descriptor_size
= 64;
343 descriptor_alignment
= 32;
345 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
346 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
347 descriptor_size
= 64;
349 descriptor_size
= 96;
351 descriptor_alignment
= 32;
353 case VK_DESCRIPTOR_TYPE_SAMPLER
:
354 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
355 descriptor_size
= 16;
356 descriptor_alignment
= 16;
359 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
360 descriptor_alignment
= 16;
361 descriptor_size
= descriptor_count
;
362 descriptor_count
= 1;
365 unreachable("unknown descriptor type\n");
369 if (size
&& !align_u64(size
, descriptor_alignment
)) {
372 size
= align_u64(size
, descriptor_alignment
);
374 uint64_t max_count
= INT32_MAX
;
375 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
376 max_count
= INT32_MAX
- size
;
377 else if (descriptor_size
)
378 max_count
= (INT32_MAX
- size
) / descriptor_size
;
380 if (max_count
< descriptor_count
) {
383 if (variable_flags
&& binding
->binding
<variable_flags
->bindingCount
&& variable_count
&&
384 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
385 variable_count
->maxVariableDescriptorCount
= MIN2(UINT32_MAX
, max_count
);
387 size
+= descriptor_count
* descriptor_size
;
392 pSupport
->supported
= supported
;
396 * Pipeline layouts. These have nothing to do with the pipeline. They are
397 * just multiple descriptor set layouts pasted together.
400 VkResult
radv_CreatePipelineLayout(
402 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
403 const VkAllocationCallbacks
* pAllocator
,
404 VkPipelineLayout
* pPipelineLayout
)
406 RADV_FROM_HANDLE(radv_device
, device
, _device
);
407 struct radv_pipeline_layout
*layout
;
408 struct mesa_sha1 ctx
;
410 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
412 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
413 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
415 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
417 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
419 unsigned dynamic_offset_count
= 0;
420 uint16_t dynamic_shader_stages
= 0;
423 _mesa_sha1_init(&ctx
);
424 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
425 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
426 pCreateInfo
->pSetLayouts
[set
]);
427 layout
->set
[set
].layout
= set_layout
;
429 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
430 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
431 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
432 dynamic_shader_stages
|= set_layout
->dynamic_shader_stages
;
434 _mesa_sha1_update(&ctx
, set_layout
, set_layout
->layout_size
);
437 layout
->dynamic_offset_count
= dynamic_offset_count
;
438 layout
->dynamic_shader_stages
= dynamic_shader_stages
;
439 layout
->push_constant_size
= 0;
441 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
442 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
443 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
444 range
->offset
+ range
->size
);
447 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
448 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
449 sizeof(layout
->push_constant_size
));
450 _mesa_sha1_final(&ctx
, layout
->sha1
);
451 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
456 void radv_DestroyPipelineLayout(
458 VkPipelineLayout _pipelineLayout
,
459 const VkAllocationCallbacks
* pAllocator
)
461 RADV_FROM_HANDLE(radv_device
, device
, _device
);
462 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
464 if (!pipeline_layout
)
466 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
472 radv_descriptor_set_create(struct radv_device
*device
,
473 struct radv_descriptor_pool
*pool
,
474 const struct radv_descriptor_set_layout
*layout
,
475 const uint32_t *variable_count
,
476 struct radv_descriptor_set
**out_set
)
478 struct radv_descriptor_set
*set
;
479 unsigned range_offset
= sizeof(struct radv_descriptor_set
) +
480 sizeof(struct radeon_winsys_bo
*) * layout
->buffer_count
;
481 unsigned mem_size
= range_offset
+
482 sizeof(struct radv_descriptor_range
) * layout
->dynamic_offset_count
;
484 if (pool
->host_memory_base
) {
485 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
486 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
488 set
= (struct radv_descriptor_set
*)pool
->host_memory_ptr
;
489 pool
->host_memory_ptr
+= mem_size
;
491 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
492 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
495 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
498 memset(set
, 0, mem_size
);
500 if (layout
->dynamic_offset_count
) {
501 set
->dynamic_descriptors
= (struct radv_descriptor_range
*)((uint8_t*)set
+ range_offset
);
504 set
->layout
= layout
;
505 uint32_t layout_size
= align_u32(layout
->size
, 32);
507 set
->size
= layout_size
;
509 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
510 vk_free2(&device
->alloc
, NULL
, set
);
511 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
514 /* try to allocate linearly first, so that we don't spend
515 * time looking for gaps if the app only allocates &
516 * resets via the pool. */
517 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
519 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
520 set
->va
= radv_buffer_get_va(set
->bo
) + pool
->current_offset
;
521 if (!pool
->host_memory_base
) {
522 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
523 pool
->entries
[pool
->entry_count
].size
= layout_size
;
524 pool
->entries
[pool
->entry_count
].set
= set
;
527 pool
->current_offset
+= layout_size
;
528 } else if (!pool
->host_memory_base
) {
532 for (index
= 0; index
< pool
->entry_count
; ++index
) {
533 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
535 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
538 if (pool
->size
- offset
< layout_size
) {
539 vk_free2(&device
->alloc
, NULL
, set
);
540 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
543 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
544 set
->va
= radv_buffer_get_va(set
->bo
) + offset
;
545 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
546 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
547 pool
->entries
[index
].offset
= offset
;
548 pool
->entries
[index
].size
= layout_size
;
549 pool
->entries
[index
].set
= set
;
552 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
555 if (layout
->has_immutable_samplers
) {
556 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
557 if (!layout
->binding
[i
].immutable_samplers_offset
||
558 layout
->binding
[i
].immutable_samplers_equal
)
561 unsigned offset
= layout
->binding
[i
].offset
/ 4;
562 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
563 offset
+= radv_combined_image_descriptor_sampler_offset(layout
->binding
+ i
) / 4;
565 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
566 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
567 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
568 offset
+= layout
->binding
[i
].size
/ 4;
578 radv_descriptor_set_destroy(struct radv_device
*device
,
579 struct radv_descriptor_pool
*pool
,
580 struct radv_descriptor_set
*set
,
583 assert(!pool
->host_memory_base
);
585 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
586 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- pool
->mapped_ptr
;
587 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
588 if (pool
->entries
[i
].offset
== offset
) {
589 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
590 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
596 vk_free2(&device
->alloc
, NULL
, set
);
599 VkResult
radv_CreateDescriptorPool(
601 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
602 const VkAllocationCallbacks
* pAllocator
,
603 VkDescriptorPool
* pDescriptorPool
)
605 RADV_FROM_HANDLE(radv_device
, device
, _device
);
606 struct radv_descriptor_pool
*pool
;
607 uint64_t size
= sizeof(struct radv_descriptor_pool
);
608 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
610 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
611 switch (ext
->sType
) {
612 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
: {
613 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*info
=
614 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*)ext
;
615 /* the sizes are 4 aligned, and we need to align to at
616 * most 32, which needs at most 28 bytes extra per
618 bo_size
+= 28llu * info
->maxInlineUniformBlockBindings
;
626 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
627 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
628 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
630 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
631 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
632 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
633 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
635 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
636 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
637 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
638 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
639 case VK_DESCRIPTOR_TYPE_SAMPLER
:
640 /* 32 as we may need to align for images */
641 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
643 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
644 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
645 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
646 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
648 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
649 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
651 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
652 bo_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
655 unreachable("unknown descriptor type\n");
660 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
661 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
662 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
663 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
666 size
+= sizeof(struct radv_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
669 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
670 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
672 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
674 memset(pool
, 0, sizeof(*pool
));
676 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
677 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
678 pool
->host_memory_ptr
= pool
->host_memory_base
;
679 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
683 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
, 32,
685 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
686 RADEON_FLAG_READ_ONLY
|
688 RADV_BO_PRIORITY_DESCRIPTOR
);
689 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
691 pool
->size
= bo_size
;
692 pool
->max_entry_count
= pCreateInfo
->maxSets
;
694 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
698 void radv_DestroyDescriptorPool(
700 VkDescriptorPool _pool
,
701 const VkAllocationCallbacks
* pAllocator
)
703 RADV_FROM_HANDLE(radv_device
, device
, _device
);
704 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
709 if (!pool
->host_memory_base
) {
710 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
711 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
716 device
->ws
->buffer_destroy(pool
->bo
);
717 vk_free2(&device
->alloc
, pAllocator
, pool
);
720 VkResult
radv_ResetDescriptorPool(
722 VkDescriptorPool descriptorPool
,
723 VkDescriptorPoolResetFlags flags
)
725 RADV_FROM_HANDLE(radv_device
, device
, _device
);
726 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
728 if (!pool
->host_memory_base
) {
729 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
730 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
732 pool
->entry_count
= 0;
735 pool
->current_offset
= 0;
736 pool
->host_memory_ptr
= pool
->host_memory_base
;
741 VkResult
radv_AllocateDescriptorSets(
743 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
744 VkDescriptorSet
* pDescriptorSets
)
746 RADV_FROM_HANDLE(radv_device
, device
, _device
);
747 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
749 VkResult result
= VK_SUCCESS
;
751 struct radv_descriptor_set
*set
= NULL
;
753 const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT
*variable_counts
=
754 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT
);
755 const uint32_t zero
= 0;
757 /* allocate a set of buffers for each shader to contain descriptors */
758 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
759 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
760 pAllocateInfo
->pSetLayouts
[i
]);
762 const uint32_t *variable_count
= NULL
;
763 if (variable_counts
) {
764 if (i
< variable_counts
->descriptorSetCount
)
765 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
767 variable_count
= &zero
;
770 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
772 result
= radv_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
773 if (result
!= VK_SUCCESS
)
776 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
779 if (result
!= VK_SUCCESS
)
780 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
785 VkResult
radv_FreeDescriptorSets(
787 VkDescriptorPool descriptorPool
,
789 const VkDescriptorSet
* pDescriptorSets
)
791 RADV_FROM_HANDLE(radv_device
, device
, _device
);
792 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
794 for (uint32_t i
= 0; i
< count
; i
++) {
795 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
797 if (set
&& !pool
->host_memory_base
)
798 radv_descriptor_set_destroy(device
, pool
, set
, true);
803 static void write_texel_buffer_descriptor(struct radv_device
*device
,
804 struct radv_cmd_buffer
*cmd_buffer
,
806 struct radeon_winsys_bo
**buffer_list
,
807 const VkBufferView _buffer_view
)
809 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
811 memcpy(dst
, buffer_view
->state
, 4 * 4);
814 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer_view
->bo
);
816 *buffer_list
= buffer_view
->bo
;
819 static void write_buffer_descriptor(struct radv_device
*device
,
820 struct radv_cmd_buffer
*cmd_buffer
,
822 struct radeon_winsys_bo
**buffer_list
,
823 const VkDescriptorBufferInfo
*buffer_info
)
825 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
826 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
827 uint32_t range
= buffer_info
->range
;
829 if (buffer_info
->range
== VK_WHOLE_SIZE
)
830 range
= buffer
->size
- buffer_info
->offset
;
832 va
+= buffer_info
->offset
+ buffer
->offset
;
834 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
836 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
837 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
838 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
839 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
840 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
841 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
844 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer
->bo
);
846 *buffer_list
= buffer
->bo
;
849 static void write_block_descriptor(struct radv_device
*device
,
850 struct radv_cmd_buffer
*cmd_buffer
,
852 const VkWriteDescriptorSet
*writeset
)
854 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_ub
=
855 vk_find_struct_const(writeset
->pNext
, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
857 memcpy(dst
, inline_ub
->pData
, inline_ub
->dataSize
);
860 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
861 struct radv_descriptor_range
*range
,
862 struct radeon_winsys_bo
**buffer_list
,
863 const VkDescriptorBufferInfo
*buffer_info
)
865 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
866 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
867 unsigned size
= buffer_info
->range
;
869 if (buffer_info
->range
== VK_WHOLE_SIZE
)
870 size
= buffer
->size
- buffer_info
->offset
;
872 va
+= buffer_info
->offset
+ buffer
->offset
;
876 *buffer_list
= buffer
->bo
;
880 write_image_descriptor(struct radv_device
*device
,
881 struct radv_cmd_buffer
*cmd_buffer
,
882 unsigned size
, unsigned *dst
,
883 struct radeon_winsys_bo
**buffer_list
,
884 VkDescriptorType descriptor_type
,
885 const VkDescriptorImageInfo
*image_info
)
887 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
888 union radv_descriptor
*descriptor
;
890 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
891 descriptor
= &iview
->storage_descriptor
;
893 descriptor
= &iview
->descriptor
;
896 memcpy(dst
, descriptor
, size
);
899 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, iview
->bo
);
901 *buffer_list
= iview
->bo
;
905 write_combined_image_sampler_descriptor(struct radv_device
*device
,
906 struct radv_cmd_buffer
*cmd_buffer
,
907 unsigned sampler_offset
,
909 struct radeon_winsys_bo
**buffer_list
,
910 VkDescriptorType descriptor_type
,
911 const VkDescriptorImageInfo
*image_info
,
914 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
916 write_image_descriptor(device
, cmd_buffer
, sampler_offset
, dst
, buffer_list
,
917 descriptor_type
, image_info
);
918 /* copy over sampler state */
920 memcpy(dst
+ sampler_offset
/ sizeof(*dst
), sampler
->state
, 16);
925 write_sampler_descriptor(struct radv_device
*device
,
927 const VkDescriptorImageInfo
*image_info
)
929 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
931 memcpy(dst
, sampler
->state
, 16);
934 void radv_update_descriptor_sets(
935 struct radv_device
* device
,
936 struct radv_cmd_buffer
* cmd_buffer
,
937 VkDescriptorSet dstSetOverride
,
938 uint32_t descriptorWriteCount
,
939 const VkWriteDescriptorSet
* pDescriptorWrites
,
940 uint32_t descriptorCopyCount
,
941 const VkCopyDescriptorSet
* pDescriptorCopies
)
944 for (i
= 0; i
< descriptorWriteCount
; i
++) {
945 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
946 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
947 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
948 const struct radv_descriptor_set_binding_layout
*binding_layout
=
949 set
->layout
->binding
+ writeset
->dstBinding
;
950 uint32_t *ptr
= set
->mapped_ptr
;
951 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
952 /* Immutable samplers are not copied into push descriptors when they are
953 * allocated, so if we are writing push descriptors we have to copy the
954 * immutable samplers into them now.
956 const bool copy_immutable_samplers
= cmd_buffer
&&
957 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
958 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
960 ptr
+= binding_layout
->offset
/ 4;
962 if (writeset
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
963 write_block_descriptor(device
, cmd_buffer
, (uint8_t*)ptr
+ writeset
->dstArrayElement
, writeset
);
967 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
968 buffer_list
+= binding_layout
->buffer_offset
;
969 buffer_list
+= writeset
->dstArrayElement
;
970 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
971 switch(writeset
->descriptorType
) {
972 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
973 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
974 unsigned idx
= writeset
->dstArrayElement
+ j
;
975 idx
+= binding_layout
->dynamic_offset_offset
;
976 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
977 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
978 buffer_list
, writeset
->pBufferInfo
+ j
);
981 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
982 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
983 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
984 writeset
->pBufferInfo
+ j
);
986 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
987 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
988 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
989 writeset
->pTexelBufferView
[j
]);
991 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
992 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
993 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
994 write_image_descriptor(device
, cmd_buffer
, 64, ptr
, buffer_list
,
995 writeset
->descriptorType
,
996 writeset
->pImageInfo
+ j
);
998 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
: {
999 unsigned sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
);
1000 write_combined_image_sampler_descriptor(device
, cmd_buffer
, sampler_offset
,
1002 writeset
->descriptorType
,
1003 writeset
->pImageInfo
+ j
,
1004 !binding_layout
->immutable_samplers_offset
);
1005 if (copy_immutable_samplers
) {
1006 const unsigned idx
= writeset
->dstArrayElement
+ j
;
1007 memcpy((char*)ptr
+ sampler_offset
, samplers
+ 4 * idx
, 16);
1011 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1012 if (!binding_layout
->immutable_samplers_offset
) {
1013 write_sampler_descriptor(device
, ptr
,
1014 writeset
->pImageInfo
+ j
);
1015 } else if (copy_immutable_samplers
) {
1016 unsigned idx
= writeset
->dstArrayElement
+ j
;
1017 memcpy(ptr
, samplers
+ 4 * idx
, 16);
1021 unreachable("unimplemented descriptor type");
1024 ptr
+= binding_layout
->size
/ 4;
1030 for (i
= 0; i
< descriptorCopyCount
; i
++) {
1031 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
1032 RADV_FROM_HANDLE(radv_descriptor_set
, src_set
,
1034 RADV_FROM_HANDLE(radv_descriptor_set
, dst_set
,
1036 const struct radv_descriptor_set_binding_layout
*src_binding_layout
=
1037 src_set
->layout
->binding
+ copyset
->srcBinding
;
1038 const struct radv_descriptor_set_binding_layout
*dst_binding_layout
=
1039 dst_set
->layout
->binding
+ copyset
->dstBinding
;
1040 uint32_t *src_ptr
= src_set
->mapped_ptr
;
1041 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
1042 struct radeon_winsys_bo
**src_buffer_list
= src_set
->descriptors
;
1043 struct radeon_winsys_bo
**dst_buffer_list
= dst_set
->descriptors
;
1045 src_ptr
+= src_binding_layout
->offset
/ 4;
1046 dst_ptr
+= dst_binding_layout
->offset
/ 4;
1048 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
1049 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
1051 src_buffer_list
+= src_binding_layout
->buffer_offset
;
1052 src_buffer_list
+= copyset
->srcArrayElement
;
1054 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
1055 dst_buffer_list
+= copyset
->dstArrayElement
;
1057 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
1058 switch (src_binding_layout
->type
) {
1059 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1060 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1061 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1062 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1063 struct radv_descriptor_range
*src_range
, *dst_range
;
1064 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
1065 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
1067 src_range
= src_set
->dynamic_descriptors
+ src_idx
;
1068 dst_range
= dst_set
->dynamic_descriptors
+ dst_idx
;
1069 *dst_range
= *src_range
;
1073 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
1075 src_ptr
+= src_binding_layout
->size
/ 4;
1076 dst_ptr
+= dst_binding_layout
->size
/ 4;
1078 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
1079 /* Sampler descriptors don't have a buffer list. */
1080 dst_buffer_list
[j
] = src_buffer_list
[j
];
1086 void radv_UpdateDescriptorSets(
1088 uint32_t descriptorWriteCount
,
1089 const VkWriteDescriptorSet
* pDescriptorWrites
,
1090 uint32_t descriptorCopyCount
,
1091 const VkCopyDescriptorSet
* pDescriptorCopies
)
1093 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1095 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
1096 descriptorCopyCount
, pDescriptorCopies
);
1099 VkResult
radv_CreateDescriptorUpdateTemplate(VkDevice _device
,
1100 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1101 const VkAllocationCallbacks
*pAllocator
,
1102 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1104 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1105 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
1106 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1107 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
1108 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
1109 struct radv_descriptor_update_template
*templ
;
1112 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1114 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1116 templ
->entry_count
= entry_count
;
1117 templ
->bind_point
= pCreateInfo
->pipelineBindPoint
;
1119 for (i
= 0; i
< entry_count
; i
++) {
1120 const VkDescriptorUpdateTemplateEntry
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1121 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1122 set_layout
->binding
+ entry
->dstBinding
;
1123 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
1124 const uint32_t *immutable_samplers
= NULL
;
1125 uint32_t dst_offset
;
1126 uint32_t dst_stride
;
1128 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1129 is dynamic, and an offset into mapped_ptr otherwise */
1130 switch (entry
->descriptorType
) {
1131 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1132 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1133 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
);
1134 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
1135 dst_stride
= 0; /* Not used */
1138 switch (entry
->descriptorType
) {
1139 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1140 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1141 /* Immutable samplers are copied into push descriptors when they are pushed */
1142 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
1143 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
1144 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
1150 dst_offset
= binding_layout
->offset
/ 4;
1151 if (entry
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
1152 dst_offset
+= entry
->dstArrayElement
/ 4;
1154 dst_offset
+= binding_layout
->size
* entry
->dstArrayElement
/ 4;
1156 dst_stride
= binding_layout
->size
/ 4;
1160 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
1161 .descriptor_type
= entry
->descriptorType
,
1162 .descriptor_count
= entry
->descriptorCount
,
1163 .src_offset
= entry
->offset
,
1164 .src_stride
= entry
->stride
,
1165 .dst_offset
= dst_offset
,
1166 .dst_stride
= dst_stride
,
1167 .buffer_offset
= buffer_offset
,
1168 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
1169 .sampler_offset
= radv_combined_image_descriptor_sampler_offset(binding_layout
),
1170 .immutable_samplers
= immutable_samplers
1174 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
1178 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device
,
1179 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1180 const VkAllocationCallbacks
*pAllocator
)
1182 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1183 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1188 vk_free2(&device
->alloc
, pAllocator
, templ
);
1191 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
1192 struct radv_cmd_buffer
*cmd_buffer
,
1193 struct radv_descriptor_set
*set
,
1194 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1197 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1200 for (i
= 0; i
< templ
->entry_count
; ++i
) {
1201 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
1202 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
1203 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
1206 if (templ
->entry
[i
].descriptor_type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1207 memcpy((uint8_t*)pDst
, pSrc
, templ
->entry
[i
].descriptor_count
);
1211 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1212 switch (templ
->entry
[i
].descriptor_type
) {
1213 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1214 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1215 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
1216 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1217 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1218 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
1221 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1222 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1223 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1224 (struct VkDescriptorBufferInfo
*) pSrc
);
1226 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1227 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1228 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1229 *(VkBufferView
*) pSrc
);
1231 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1232 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1233 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1234 write_image_descriptor(device
, cmd_buffer
, 64, pDst
, buffer_list
,
1235 templ
->entry
[i
].descriptor_type
,
1236 (struct VkDescriptorImageInfo
*) pSrc
);
1238 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1239 write_combined_image_sampler_descriptor(device
, cmd_buffer
, templ
->entry
[i
].sampler_offset
,
1240 pDst
, buffer_list
, templ
->entry
[i
].descriptor_type
,
1241 (struct VkDescriptorImageInfo
*) pSrc
,
1242 templ
->entry
[i
].has_sampler
);
1243 if (templ
->entry
[i
].immutable_samplers
) {
1244 memcpy((char*)pDst
+ templ
->entry
[i
].sampler_offset
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1247 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1248 if (templ
->entry
[i
].has_sampler
)
1249 write_sampler_descriptor(device
, pDst
,
1250 (struct VkDescriptorImageInfo
*) pSrc
);
1251 else if (templ
->entry
[i
].immutable_samplers
)
1252 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1255 unreachable("unimplemented descriptor type");
1258 pSrc
+= templ
->entry
[i
].src_stride
;
1259 pDst
+= templ
->entry
[i
].dst_stride
;
1265 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device
,
1266 VkDescriptorSet descriptorSet
,
1267 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1270 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1271 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
1273 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);
1277 VkResult
radv_CreateSamplerYcbcrConversion(VkDevice _device
,
1278 const VkSamplerYcbcrConversionCreateInfo
* pCreateInfo
,
1279 const VkAllocationCallbacks
* pAllocator
,
1280 VkSamplerYcbcrConversion
* pYcbcrConversion
)
1282 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1283 struct radv_sampler_ycbcr_conversion
*conversion
= NULL
;
1285 conversion
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*conversion
), 8,
1286 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1288 if (conversion
== NULL
)
1289 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1291 conversion
->format
= pCreateInfo
->format
;
1292 conversion
->ycbcr_model
= pCreateInfo
->ycbcrModel
;
1293 conversion
->ycbcr_range
= pCreateInfo
->ycbcrRange
;
1294 conversion
->components
= pCreateInfo
->components
;
1295 conversion
->chroma_offsets
[0] = pCreateInfo
->xChromaOffset
;
1296 conversion
->chroma_offsets
[1] = pCreateInfo
->yChromaOffset
;
1297 conversion
->chroma_filter
= pCreateInfo
->chromaFilter
;
1299 *pYcbcrConversion
= radv_sampler_ycbcr_conversion_to_handle(conversion
);
1304 void radv_DestroySamplerYcbcrConversion(VkDevice _device
,
1305 VkSamplerYcbcrConversion ycbcrConversion
,
1306 const VkAllocationCallbacks
* pAllocator
)
1308 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1309 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion
, ycbcr_conversion
, ycbcrConversion
);
1311 if (ycbcr_conversion
)
1312 vk_free2(&device
->alloc
, pAllocator
, ycbcr_conversion
);