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"
36 static bool has_equal_immutable_samplers(const VkSampler
*samplers
, uint32_t count
)
40 for(uint32_t i
= 1; i
< count
; ++i
) {
41 if (memcmp(radv_sampler_from_handle(samplers
[0])->state
,
42 radv_sampler_from_handle(samplers
[i
])->state
, 16)) {
49 static int binding_compare(const void* av
, const void *bv
)
51 const VkDescriptorSetLayoutBinding
*a
= (const VkDescriptorSetLayoutBinding
*)av
;
52 const VkDescriptorSetLayoutBinding
*b
= (const VkDescriptorSetLayoutBinding
*)bv
;
54 return (a
->binding
< b
->binding
) ? -1 : (a
->binding
> b
->binding
) ? 1 : 0;
57 static VkDescriptorSetLayoutBinding
*
58 create_sorted_bindings(const VkDescriptorSetLayoutBinding
*bindings
, unsigned count
) {
59 VkDescriptorSetLayoutBinding
*sorted_bindings
= malloc(count
* sizeof(VkDescriptorSetLayoutBinding
));
63 memcpy(sorted_bindings
, bindings
, count
* sizeof(VkDescriptorSetLayoutBinding
));
65 qsort(sorted_bindings
, count
, sizeof(VkDescriptorSetLayoutBinding
), binding_compare
);
67 return sorted_bindings
;
70 VkResult
radv_CreateDescriptorSetLayout(
72 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
73 const VkAllocationCallbacks
* pAllocator
,
74 VkDescriptorSetLayout
* pSetLayout
)
76 RADV_FROM_HANDLE(radv_device
, device
, _device
);
77 struct radv_descriptor_set_layout
*set_layout
;
79 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
80 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*variable_flags
=
81 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
83 uint32_t max_binding
= 0;
84 uint32_t immutable_sampler_count
= 0;
85 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
86 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
87 if ((pCreateInfo
->pBindings
[j
].descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
88 pCreateInfo
->pBindings
[j
].descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
89 pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
90 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
93 uint32_t samplers_offset
= sizeof(struct radv_descriptor_set_layout
) +
94 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]);
95 size_t size
= samplers_offset
+ immutable_sampler_count
* 4 * sizeof(uint32_t);
97 set_layout
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
98 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
100 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
102 set_layout
->flags
= pCreateInfo
->flags
;
104 /* We just allocate all the samplers at the end of the struct */
105 uint32_t *samplers
= (uint32_t*)&set_layout
->binding
[max_binding
+ 1];
107 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
108 pCreateInfo
->bindingCount
);
110 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
111 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
114 set_layout
->binding_count
= max_binding
+ 1;
115 set_layout
->shader_stages
= 0;
116 set_layout
->dynamic_shader_stages
= 0;
117 set_layout
->has_immutable_samplers
= false;
118 set_layout
->size
= 0;
120 memset(set_layout
->binding
, 0, size
- sizeof(struct radv_descriptor_set_layout
));
122 uint32_t buffer_count
= 0;
123 uint32_t dynamic_offset_count
= 0;
125 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
126 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ j
;
127 uint32_t b
= binding
->binding
;
129 unsigned binding_buffer_count
= 0;
130 uint32_t descriptor_count
= binding
->descriptorCount
;
132 switch (binding
->descriptorType
) {
133 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
134 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
135 assert(!(pCreateInfo
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
136 set_layout
->binding
[b
].dynamic_offset_count
= 1;
137 set_layout
->dynamic_shader_stages
|= binding
->stageFlags
;
138 set_layout
->binding
[b
].size
= 0;
139 binding_buffer_count
= 1;
142 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
143 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
144 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
145 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
146 set_layout
->binding
[b
].size
= 16;
147 binding_buffer_count
= 1;
150 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
151 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
152 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
153 /* main descriptor + fmask descriptor */
154 set_layout
->binding
[b
].size
= 64;
155 binding_buffer_count
= 1;
158 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
159 /* main descriptor + fmask descriptor + sampler */
160 set_layout
->binding
[b
].size
= 96;
161 binding_buffer_count
= 1;
164 case VK_DESCRIPTOR_TYPE_SAMPLER
:
165 set_layout
->binding
[b
].size
= 16;
168 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
170 set_layout
->binding
[b
].size
= descriptor_count
;
171 descriptor_count
= 1;
174 unreachable("unknown descriptor type\n");
178 set_layout
->size
= align(set_layout
->size
, alignment
);
179 set_layout
->binding
[b
].type
= binding
->descriptorType
;
180 set_layout
->binding
[b
].array_size
= descriptor_count
;
181 set_layout
->binding
[b
].offset
= set_layout
->size
;
182 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
183 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
185 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
186 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
187 assert(!binding
->pImmutableSamplers
); /* Terribly ill defined how many samplers are valid */
188 assert(binding
->binding
== max_binding
);
190 set_layout
->has_variable_descriptors
= true;
193 if ((binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
194 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
195 binding
->pImmutableSamplers
) {
196 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
197 set_layout
->binding
[b
].immutable_samplers_equal
=
198 has_equal_immutable_samplers(binding
->pImmutableSamplers
, binding
->descriptorCount
);
199 set_layout
->has_immutable_samplers
= true;
202 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
203 memcpy(samplers
+ 4 * i
, &radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->state
, 16);
205 /* Don't reserve space for the samplers if they're not accessed. */
206 if (set_layout
->binding
[b
].immutable_samplers_equal
) {
207 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
208 set_layout
->binding
[b
].size
-= 32;
209 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
210 set_layout
->binding
[b
].size
-= 16;
212 samplers
+= 4 * binding
->descriptorCount
;
213 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
216 set_layout
->size
+= descriptor_count
* set_layout
->binding
[b
].size
;
217 buffer_count
+= descriptor_count
* binding_buffer_count
;
218 dynamic_offset_count
+= descriptor_count
*
219 set_layout
->binding
[b
].dynamic_offset_count
;
220 set_layout
->shader_stages
|= binding
->stageFlags
;
225 set_layout
->buffer_count
= buffer_count
;
226 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
228 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
233 void radv_DestroyDescriptorSetLayout(
235 VkDescriptorSetLayout _set_layout
,
236 const VkAllocationCallbacks
* pAllocator
)
238 RADV_FROM_HANDLE(radv_device
, device
, _device
);
239 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
244 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
247 void radv_GetDescriptorSetLayoutSupport(VkDevice device
,
248 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
249 VkDescriptorSetLayoutSupport
* pSupport
)
251 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(pCreateInfo
->pBindings
,
252 pCreateInfo
->bindingCount
);
254 pSupport
->supported
= false;
258 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*variable_flags
=
259 vk_find_struct_const(pCreateInfo
->pNext
, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
260 VkDescriptorSetVariableDescriptorCountLayoutSupportEXT
*variable_count
=
261 vk_find_struct((void*)pCreateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT_EXT
);
262 if (variable_count
) {
263 variable_count
->maxVariableDescriptorCount
= 0;
266 bool supported
= true;
268 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
269 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ i
;
271 uint64_t descriptor_size
= 0;
272 uint64_t descriptor_alignment
= 1;
273 uint32_t descriptor_count
= binding
->descriptorCount
;
274 switch (binding
->descriptorType
) {
275 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
276 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
278 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
279 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
280 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
281 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
282 descriptor_size
= 16;
283 descriptor_alignment
= 16;
285 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
286 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
287 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
288 descriptor_size
= 64;
289 descriptor_alignment
= 32;
291 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
292 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
293 descriptor_size
= 64;
295 descriptor_size
= 96;
297 descriptor_alignment
= 32;
299 case VK_DESCRIPTOR_TYPE_SAMPLER
:
300 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, descriptor_count
)) {
301 descriptor_size
= 16;
302 descriptor_alignment
= 16;
305 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
306 descriptor_alignment
= 16;
307 descriptor_size
= descriptor_count
;
308 descriptor_count
= 1;
311 unreachable("unknown descriptor type\n");
315 if (size
&& !align_u64(size
, descriptor_alignment
)) {
318 size
= align_u64(size
, descriptor_alignment
);
320 uint64_t max_count
= INT32_MAX
;
321 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
322 max_count
= INT32_MAX
- size
;
323 else if (descriptor_size
)
324 max_count
= (INT32_MAX
- size
) / descriptor_size
;
326 if (max_count
< descriptor_count
) {
329 if (variable_flags
&& binding
->binding
<variable_flags
->bindingCount
&& variable_count
&&
330 (variable_flags
->pBindingFlags
[binding
->binding
] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
331 variable_count
->maxVariableDescriptorCount
= MIN2(UINT32_MAX
, max_count
);
333 size
+= descriptor_count
* descriptor_size
;
338 pSupport
->supported
= supported
;
342 * Pipeline layouts. These have nothing to do with the pipeline. They are
343 * just multiple descriptor set layouts pasted together.
346 VkResult
radv_CreatePipelineLayout(
348 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
349 const VkAllocationCallbacks
* pAllocator
,
350 VkPipelineLayout
* pPipelineLayout
)
352 RADV_FROM_HANDLE(radv_device
, device
, _device
);
353 struct radv_pipeline_layout
*layout
;
354 struct mesa_sha1 ctx
;
356 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
358 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
359 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
361 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
363 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
365 unsigned dynamic_offset_count
= 0;
366 uint16_t dynamic_shader_stages
= 0;
369 _mesa_sha1_init(&ctx
);
370 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
371 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
372 pCreateInfo
->pSetLayouts
[set
]);
373 layout
->set
[set
].layout
= set_layout
;
375 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
376 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
377 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
378 dynamic_shader_stages
|= set_layout
->dynamic_shader_stages
;
379 if (set_layout
->binding
[b
].immutable_samplers_offset
)
380 _mesa_sha1_update(&ctx
, radv_immutable_samplers(set_layout
, set_layout
->binding
+ b
),
381 set_layout
->binding
[b
].array_size
* 4 * sizeof(uint32_t));
383 _mesa_sha1_update(&ctx
, set_layout
->binding
,
384 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
387 layout
->dynamic_offset_count
= dynamic_offset_count
;
388 layout
->dynamic_shader_stages
= dynamic_shader_stages
;
389 layout
->push_constant_size
= 0;
391 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
392 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
393 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
394 range
->offset
+ range
->size
);
397 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
398 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
399 sizeof(layout
->push_constant_size
));
400 _mesa_sha1_final(&ctx
, layout
->sha1
);
401 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
406 void radv_DestroyPipelineLayout(
408 VkPipelineLayout _pipelineLayout
,
409 const VkAllocationCallbacks
* pAllocator
)
411 RADV_FROM_HANDLE(radv_device
, device
, _device
);
412 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
414 if (!pipeline_layout
)
416 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
422 radv_descriptor_set_create(struct radv_device
*device
,
423 struct radv_descriptor_pool
*pool
,
424 const struct radv_descriptor_set_layout
*layout
,
425 const uint32_t *variable_count
,
426 struct radv_descriptor_set
**out_set
)
428 struct radv_descriptor_set
*set
;
429 unsigned range_offset
= sizeof(struct radv_descriptor_set
) +
430 sizeof(struct radeon_winsys_bo
*) * layout
->buffer_count
;
431 unsigned mem_size
= range_offset
+
432 sizeof(struct radv_descriptor_range
) * layout
->dynamic_offset_count
;
434 if (pool
->host_memory_base
) {
435 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
436 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
438 set
= (struct radv_descriptor_set
*)pool
->host_memory_ptr
;
439 pool
->host_memory_ptr
+= mem_size
;
441 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
442 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
445 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
448 memset(set
, 0, mem_size
);
450 if (layout
->dynamic_offset_count
) {
451 set
->dynamic_descriptors
= (struct radv_descriptor_range
*)((uint8_t*)set
+ range_offset
);
454 set
->layout
= layout
;
455 uint32_t layout_size
= align_u32(layout
->size
, 32);
457 set
->size
= layout_size
;
459 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
460 vk_free2(&device
->alloc
, NULL
, set
);
461 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
464 /* try to allocate linearly first, so that we don't spend
465 * time looking for gaps if the app only allocates &
466 * resets via the pool. */
467 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
469 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
470 set
->va
= radv_buffer_get_va(set
->bo
) + pool
->current_offset
;
471 if (!pool
->host_memory_base
) {
472 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
473 pool
->entries
[pool
->entry_count
].size
= layout_size
;
474 pool
->entries
[pool
->entry_count
].set
= set
;
477 pool
->current_offset
+= layout_size
;
478 } else if (!pool
->host_memory_base
) {
482 for (index
= 0; index
< pool
->entry_count
; ++index
) {
483 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
485 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
488 if (pool
->size
- offset
< layout_size
) {
489 vk_free2(&device
->alloc
, NULL
, set
);
490 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
493 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
494 set
->va
= radv_buffer_get_va(set
->bo
) + offset
;
495 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
496 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
497 pool
->entries
[index
].offset
= offset
;
498 pool
->entries
[index
].size
= layout_size
;
499 pool
->entries
[index
].set
= set
;
502 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
505 if (layout
->has_immutable_samplers
) {
506 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
507 if (!layout
->binding
[i
].immutable_samplers_offset
||
508 layout
->binding
[i
].immutable_samplers_equal
)
511 unsigned offset
= layout
->binding
[i
].offset
/ 4;
512 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
515 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
516 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
517 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
518 offset
+= layout
->binding
[i
].size
/ 4;
528 radv_descriptor_set_destroy(struct radv_device
*device
,
529 struct radv_descriptor_pool
*pool
,
530 struct radv_descriptor_set
*set
,
533 assert(!pool
->host_memory_base
);
535 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
536 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- pool
->mapped_ptr
;
537 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
538 if (pool
->entries
[i
].offset
== offset
) {
539 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
540 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
546 vk_free2(&device
->alloc
, NULL
, set
);
549 VkResult
radv_CreateDescriptorPool(
551 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
552 const VkAllocationCallbacks
* pAllocator
,
553 VkDescriptorPool
* pDescriptorPool
)
555 RADV_FROM_HANDLE(radv_device
, device
, _device
);
556 struct radv_descriptor_pool
*pool
;
557 uint64_t size
= sizeof(struct radv_descriptor_pool
);
558 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
560 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
561 switch (ext
->sType
) {
562 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT
: {
563 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*info
=
564 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT
*)ext
;
565 /* the sizes are 4 aligned, and we need to align to at
566 * most 32, which needs at most 28 bytes extra per
568 bo_size
+= 28llu * info
->maxInlineUniformBlockBindings
;
576 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
577 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
578 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
580 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
581 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
582 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
583 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
585 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
586 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
587 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
588 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
589 case VK_DESCRIPTOR_TYPE_SAMPLER
:
590 /* 32 as we may need to align for images */
591 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
593 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
594 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
595 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
596 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
598 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
599 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
601 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
:
602 bo_size
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
605 unreachable("unknown descriptor type\n");
610 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
611 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
612 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
613 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
616 size
+= sizeof(struct radv_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
619 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
620 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
622 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
624 memset(pool
, 0, sizeof(*pool
));
626 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
627 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
628 pool
->host_memory_ptr
= pool
->host_memory_base
;
629 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
633 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
, 32,
635 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
636 RADEON_FLAG_READ_ONLY
|
638 RADV_BO_PRIORITY_DESCRIPTOR
);
639 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
641 pool
->size
= bo_size
;
642 pool
->max_entry_count
= pCreateInfo
->maxSets
;
644 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
648 void radv_DestroyDescriptorPool(
650 VkDescriptorPool _pool
,
651 const VkAllocationCallbacks
* pAllocator
)
653 RADV_FROM_HANDLE(radv_device
, device
, _device
);
654 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
659 if (!pool
->host_memory_base
) {
660 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
661 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
666 device
->ws
->buffer_destroy(pool
->bo
);
667 vk_free2(&device
->alloc
, pAllocator
, pool
);
670 VkResult
radv_ResetDescriptorPool(
672 VkDescriptorPool descriptorPool
,
673 VkDescriptorPoolResetFlags flags
)
675 RADV_FROM_HANDLE(radv_device
, device
, _device
);
676 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
678 if (!pool
->host_memory_base
) {
679 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
680 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
682 pool
->entry_count
= 0;
685 pool
->current_offset
= 0;
686 pool
->host_memory_ptr
= pool
->host_memory_base
;
691 VkResult
radv_AllocateDescriptorSets(
693 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
694 VkDescriptorSet
* pDescriptorSets
)
696 RADV_FROM_HANDLE(radv_device
, device
, _device
);
697 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
699 VkResult result
= VK_SUCCESS
;
701 struct radv_descriptor_set
*set
= NULL
;
703 const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT
*variable_counts
=
704 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT
);
705 const uint32_t zero
= 0;
707 /* allocate a set of buffers for each shader to contain descriptors */
708 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
709 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
710 pAllocateInfo
->pSetLayouts
[i
]);
712 const uint32_t *variable_count
= NULL
;
713 if (variable_counts
) {
714 if (i
< variable_counts
->descriptorSetCount
)
715 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
717 variable_count
= &zero
;
720 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
722 result
= radv_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
723 if (result
!= VK_SUCCESS
)
726 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
729 if (result
!= VK_SUCCESS
)
730 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
735 VkResult
radv_FreeDescriptorSets(
737 VkDescriptorPool descriptorPool
,
739 const VkDescriptorSet
* pDescriptorSets
)
741 RADV_FROM_HANDLE(radv_device
, device
, _device
);
742 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
744 for (uint32_t i
= 0; i
< count
; i
++) {
745 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
747 if (set
&& !pool
->host_memory_base
)
748 radv_descriptor_set_destroy(device
, pool
, set
, true);
753 static void write_texel_buffer_descriptor(struct radv_device
*device
,
754 struct radv_cmd_buffer
*cmd_buffer
,
756 struct radeon_winsys_bo
**buffer_list
,
757 const VkBufferView _buffer_view
)
759 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
761 memcpy(dst
, buffer_view
->state
, 4 * 4);
764 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer_view
->bo
);
766 *buffer_list
= buffer_view
->bo
;
769 static void write_buffer_descriptor(struct radv_device
*device
,
770 struct radv_cmd_buffer
*cmd_buffer
,
772 struct radeon_winsys_bo
**buffer_list
,
773 const VkDescriptorBufferInfo
*buffer_info
)
775 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
776 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
777 uint32_t range
= buffer_info
->range
;
779 if (buffer_info
->range
== VK_WHOLE_SIZE
)
780 range
= buffer
->size
- buffer_info
->offset
;
782 va
+= buffer_info
->offset
+ buffer
->offset
;
784 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
786 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
787 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
788 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
789 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
790 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
791 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
794 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer
->bo
);
796 *buffer_list
= buffer
->bo
;
799 static void write_block_descriptor(struct radv_device
*device
,
800 struct radv_cmd_buffer
*cmd_buffer
,
802 const VkWriteDescriptorSet
*writeset
)
804 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_ub
=
805 vk_find_struct_const(writeset
->pNext
, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
807 memcpy(dst
, inline_ub
->pData
, inline_ub
->dataSize
);
810 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
811 struct radv_descriptor_range
*range
,
812 struct radeon_winsys_bo
**buffer_list
,
813 const VkDescriptorBufferInfo
*buffer_info
)
815 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
816 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
817 unsigned size
= buffer_info
->range
;
819 if (buffer_info
->range
== VK_WHOLE_SIZE
)
820 size
= buffer
->size
- buffer_info
->offset
;
822 va
+= buffer_info
->offset
+ buffer
->offset
;
826 *buffer_list
= buffer
->bo
;
830 write_image_descriptor(struct radv_device
*device
,
831 struct radv_cmd_buffer
*cmd_buffer
,
833 struct radeon_winsys_bo
**buffer_list
,
834 VkDescriptorType descriptor_type
,
835 const VkDescriptorImageInfo
*image_info
)
837 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
838 uint32_t *descriptor
;
840 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
841 descriptor
= iview
->storage_descriptor
;
843 descriptor
= iview
->descriptor
;
846 memcpy(dst
, descriptor
, 16 * 4);
849 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, iview
->bo
);
851 *buffer_list
= iview
->bo
;
855 write_combined_image_sampler_descriptor(struct radv_device
*device
,
856 struct radv_cmd_buffer
*cmd_buffer
,
858 struct radeon_winsys_bo
**buffer_list
,
859 VkDescriptorType descriptor_type
,
860 const VkDescriptorImageInfo
*image_info
,
863 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
865 write_image_descriptor(device
, cmd_buffer
, dst
, buffer_list
, descriptor_type
, image_info
);
866 /* copy over sampler state */
868 memcpy(dst
+ 16, sampler
->state
, 16);
872 write_sampler_descriptor(struct radv_device
*device
,
874 const VkDescriptorImageInfo
*image_info
)
876 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
878 memcpy(dst
, sampler
->state
, 16);
881 void radv_update_descriptor_sets(
882 struct radv_device
* device
,
883 struct radv_cmd_buffer
* cmd_buffer
,
884 VkDescriptorSet dstSetOverride
,
885 uint32_t descriptorWriteCount
,
886 const VkWriteDescriptorSet
* pDescriptorWrites
,
887 uint32_t descriptorCopyCount
,
888 const VkCopyDescriptorSet
* pDescriptorCopies
)
891 for (i
= 0; i
< descriptorWriteCount
; i
++) {
892 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
893 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
894 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
895 const struct radv_descriptor_set_binding_layout
*binding_layout
=
896 set
->layout
->binding
+ writeset
->dstBinding
;
897 uint32_t *ptr
= set
->mapped_ptr
;
898 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
899 /* Immutable samplers are not copied into push descriptors when they are
900 * allocated, so if we are writing push descriptors we have to copy the
901 * immutable samplers into them now.
903 const bool copy_immutable_samplers
= cmd_buffer
&&
904 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
905 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
907 ptr
+= binding_layout
->offset
/ 4;
909 if (writeset
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
910 write_block_descriptor(device
, cmd_buffer
, (uint8_t*)ptr
+ writeset
->dstArrayElement
, writeset
);
914 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
915 buffer_list
+= binding_layout
->buffer_offset
;
916 buffer_list
+= writeset
->dstArrayElement
;
917 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
918 switch(writeset
->descriptorType
) {
919 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
920 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
921 unsigned idx
= writeset
->dstArrayElement
+ j
;
922 idx
+= binding_layout
->dynamic_offset_offset
;
923 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
924 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
925 buffer_list
, writeset
->pBufferInfo
+ j
);
928 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
929 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
930 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
931 writeset
->pBufferInfo
+ j
);
933 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
934 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
935 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
936 writeset
->pTexelBufferView
[j
]);
938 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
939 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
940 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
941 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
942 writeset
->descriptorType
,
943 writeset
->pImageInfo
+ j
);
945 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
946 write_combined_image_sampler_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
947 writeset
->descriptorType
,
948 writeset
->pImageInfo
+ j
,
949 !binding_layout
->immutable_samplers_offset
);
950 if (copy_immutable_samplers
) {
951 const unsigned idx
= writeset
->dstArrayElement
+ j
;
952 memcpy(ptr
+ 16, samplers
+ 4 * idx
, 16);
955 case VK_DESCRIPTOR_TYPE_SAMPLER
:
956 if (!binding_layout
->immutable_samplers_offset
) {
957 write_sampler_descriptor(device
, ptr
,
958 writeset
->pImageInfo
+ j
);
959 } else if (copy_immutable_samplers
) {
960 unsigned idx
= writeset
->dstArrayElement
+ j
;
961 memcpy(ptr
, samplers
+ 4 * idx
, 16);
965 unreachable("unimplemented descriptor type");
968 ptr
+= binding_layout
->size
/ 4;
974 for (i
= 0; i
< descriptorCopyCount
; i
++) {
975 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
976 RADV_FROM_HANDLE(radv_descriptor_set
, src_set
,
978 RADV_FROM_HANDLE(radv_descriptor_set
, dst_set
,
980 const struct radv_descriptor_set_binding_layout
*src_binding_layout
=
981 src_set
->layout
->binding
+ copyset
->srcBinding
;
982 const struct radv_descriptor_set_binding_layout
*dst_binding_layout
=
983 dst_set
->layout
->binding
+ copyset
->dstBinding
;
984 uint32_t *src_ptr
= src_set
->mapped_ptr
;
985 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
986 struct radeon_winsys_bo
**src_buffer_list
= src_set
->descriptors
;
987 struct radeon_winsys_bo
**dst_buffer_list
= dst_set
->descriptors
;
989 src_ptr
+= src_binding_layout
->offset
/ 4;
990 dst_ptr
+= dst_binding_layout
->offset
/ 4;
992 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
993 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
995 src_buffer_list
+= src_binding_layout
->buffer_offset
;
996 src_buffer_list
+= copyset
->srcArrayElement
;
998 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
999 dst_buffer_list
+= copyset
->dstArrayElement
;
1001 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
1002 switch (src_binding_layout
->type
) {
1003 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1004 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1005 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1006 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1007 struct radv_descriptor_range
*src_range
, *dst_range
;
1008 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
1009 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
1011 src_range
= src_set
->dynamic_descriptors
+ src_idx
;
1012 dst_range
= dst_set
->dynamic_descriptors
+ dst_idx
;
1013 *dst_range
= *src_range
;
1017 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
1019 src_ptr
+= src_binding_layout
->size
/ 4;
1020 dst_ptr
+= dst_binding_layout
->size
/ 4;
1022 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
1023 /* Sampler descriptors don't have a buffer list. */
1024 dst_buffer_list
[j
] = src_buffer_list
[j
];
1030 void radv_UpdateDescriptorSets(
1032 uint32_t descriptorWriteCount
,
1033 const VkWriteDescriptorSet
* pDescriptorWrites
,
1034 uint32_t descriptorCopyCount
,
1035 const VkCopyDescriptorSet
* pDescriptorCopies
)
1037 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1039 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
1040 descriptorCopyCount
, pDescriptorCopies
);
1043 VkResult
radv_CreateDescriptorUpdateTemplate(VkDevice _device
,
1044 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1045 const VkAllocationCallbacks
*pAllocator
,
1046 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1048 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1049 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
1050 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1051 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
1052 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
1053 struct radv_descriptor_update_template
*templ
;
1056 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1058 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1060 templ
->entry_count
= entry_count
;
1061 templ
->bind_point
= pCreateInfo
->pipelineBindPoint
;
1063 for (i
= 0; i
< entry_count
; i
++) {
1064 const VkDescriptorUpdateTemplateEntry
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1065 const struct radv_descriptor_set_binding_layout
*binding_layout
=
1066 set_layout
->binding
+ entry
->dstBinding
;
1067 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
1068 const uint32_t *immutable_samplers
= NULL
;
1069 uint32_t dst_offset
;
1070 uint32_t dst_stride
;
1072 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1073 is dynamic, and an offset into mapped_ptr otherwise */
1074 switch (entry
->descriptorType
) {
1075 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1076 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1077 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
);
1078 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
1079 dst_stride
= 0; /* Not used */
1082 switch (entry
->descriptorType
) {
1083 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1084 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1085 /* Immutable samplers are copied into push descriptors when they are pushed */
1086 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
1087 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
1088 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
1094 dst_offset
= binding_layout
->offset
/ 4;
1095 if (entry
->descriptorType
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
1096 dst_offset
+= entry
->dstArrayElement
/ 4;
1098 dst_offset
+= binding_layout
->size
* entry
->dstArrayElement
/ 4;
1100 dst_stride
= binding_layout
->size
/ 4;
1104 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
1105 .descriptor_type
= entry
->descriptorType
,
1106 .descriptor_count
= entry
->descriptorCount
,
1107 .src_offset
= entry
->offset
,
1108 .src_stride
= entry
->stride
,
1109 .dst_offset
= dst_offset
,
1110 .dst_stride
= dst_stride
,
1111 .buffer_offset
= buffer_offset
,
1112 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
1113 .immutable_samplers
= immutable_samplers
1117 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
1121 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device
,
1122 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1123 const VkAllocationCallbacks
*pAllocator
)
1125 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1126 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1131 vk_free2(&device
->alloc
, pAllocator
, templ
);
1134 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
1135 struct radv_cmd_buffer
*cmd_buffer
,
1136 struct radv_descriptor_set
*set
,
1137 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1140 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1143 for (i
= 0; i
< templ
->entry_count
; ++i
) {
1144 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
1145 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
1146 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
1149 if (templ
->entry
[i
].descriptor_type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1150 memcpy((uint8_t*)pDst
, pSrc
, templ
->entry
[i
].descriptor_count
);
1154 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1155 switch (templ
->entry
[i
].descriptor_type
) {
1156 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1157 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1158 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
1159 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1160 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1161 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
1164 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1165 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1166 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1167 (struct VkDescriptorBufferInfo
*) pSrc
);
1169 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1170 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1171 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1172 *(VkBufferView
*) pSrc
);
1174 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1175 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1176 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1177 write_image_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1178 templ
->entry
[i
].descriptor_type
,
1179 (struct VkDescriptorImageInfo
*) pSrc
);
1181 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1182 write_combined_image_sampler_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1183 templ
->entry
[i
].descriptor_type
,
1184 (struct VkDescriptorImageInfo
*) pSrc
,
1185 templ
->entry
[i
].has_sampler
);
1186 if (templ
->entry
[i
].immutable_samplers
)
1187 memcpy(pDst
+ 16, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1189 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1190 if (templ
->entry
[i
].has_sampler
)
1191 write_sampler_descriptor(device
, pDst
,
1192 (struct VkDescriptorImageInfo
*) pSrc
);
1193 else if (templ
->entry
[i
].immutable_samplers
)
1194 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1197 unreachable("unimplemented descriptor type");
1200 pSrc
+= templ
->entry
[i
].src_stride
;
1201 pDst
+= templ
->entry
[i
].dst_stride
;
1207 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device
,
1208 VkDescriptorSet descriptorSet
,
1209 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1212 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1213 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
1215 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);
1219 VkResult
radv_CreateSamplerYcbcrConversion(VkDevice _device
,
1220 const VkSamplerYcbcrConversionCreateInfo
* pCreateInfo
,
1221 const VkAllocationCallbacks
* pAllocator
,
1222 VkSamplerYcbcrConversion
* pYcbcrConversion
)
1224 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1225 struct radv_sampler_ycbcr_conversion
*conversion
= NULL
;
1227 conversion
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*conversion
), 8,
1228 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1230 if (conversion
== NULL
)
1231 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1233 conversion
->format
= pCreateInfo
->format
;
1234 conversion
->ycbcr_model
= pCreateInfo
->ycbcrModel
;
1235 conversion
->ycbcr_range
= pCreateInfo
->ycbcrRange
;
1236 conversion
->components
= pCreateInfo
->components
;
1237 conversion
->chroma_offsets
[0] = pCreateInfo
->xChromaOffset
;
1238 conversion
->chroma_offsets
[1] = pCreateInfo
->yChromaOffset
;
1239 conversion
->chroma_filter
= pCreateInfo
->chromaFilter
;
1241 *pYcbcrConversion
= radv_sampler_ycbcr_conversion_to_handle(conversion
);
1246 void radv_DestroySamplerYcbcrConversion(VkDevice _device
,
1247 VkSamplerYcbcrConversion ycbcrConversion
,
1248 const VkAllocationCallbacks
* pAllocator
)
1250 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1251 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion
, ycbcr_conversion
, ycbcrConversion
);
1253 if (ycbcr_conversion
)
1254 vk_free2(&device
->alloc
, pAllocator
, ycbcr_conversion
);