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
22 * DEALINGS IN THE SOFTWARE.
28 * We use the bindless descriptor model, which maps fairly closely to how
29 * Vulkan descriptor sets work. The two exceptions are input attachments and
30 * dynamic descriptors, which have to be patched when recording command
31 * buffers. We reserve an extra descriptor set for these. This descriptor set
32 * contains all the input attachments in the pipeline, in order, and then all
33 * the dynamic descriptors. The dynamic descriptors are stored in the CPU-side
34 * datastructure for each tu_descriptor_set, and then combined into one big
35 * descriptor set at CmdBindDescriptors time/draw time.
38 #include "tu_private.h"
46 #include "util/mesa-sha1.h"
50 binding_compare(const void *av
, const void *bv
)
52 const VkDescriptorSetLayoutBinding
*a
=
53 (const VkDescriptorSetLayoutBinding
*) av
;
54 const VkDescriptorSetLayoutBinding
*b
=
55 (const VkDescriptorSetLayoutBinding
*) bv
;
57 return (a
->binding
< b
->binding
) ? -1 : (a
->binding
> b
->binding
) ? 1 : 0;
60 static VkDescriptorSetLayoutBinding
*
61 create_sorted_bindings(const VkDescriptorSetLayoutBinding
*bindings
,
64 VkDescriptorSetLayoutBinding
*sorted_bindings
=
65 malloc(count
* sizeof(VkDescriptorSetLayoutBinding
));
69 memcpy(sorted_bindings
, bindings
,
70 count
* sizeof(VkDescriptorSetLayoutBinding
));
72 qsort(sorted_bindings
, count
, sizeof(VkDescriptorSetLayoutBinding
),
75 return sorted_bindings
;
79 descriptor_size(VkDescriptorType type
)
82 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
83 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
84 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
85 /* These are remapped to the special driver-managed descriptor set,
86 * hence they don't take up any space in the original descriptor set:
89 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
90 /* We make offsets and sizes all 16 dwords, to match how the hardware
91 * interprets indices passed to sample/load/store instructions in
92 * multiples of 16 dwords. This means that "normal" descriptors are all
93 * of size 16, with padding for smaller descriptors like uniform storage
94 * descriptors which are less than 16 dwords. However combined images
95 * and samplers are actually two descriptors, so they have size 2.
97 return A6XX_TEX_CONST_DWORDS
* 4 * 2;
99 return A6XX_TEX_CONST_DWORDS
* 4;
104 tu_CreateDescriptorSetLayout(
106 const VkDescriptorSetLayoutCreateInfo
*pCreateInfo
,
107 const VkAllocationCallbacks
*pAllocator
,
108 VkDescriptorSetLayout
*pSetLayout
)
110 TU_FROM_HANDLE(tu_device
, device
, _device
);
111 struct tu_descriptor_set_layout
*set_layout
;
113 assert(pCreateInfo
->sType
==
114 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
115 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*variable_flags
=
116 vk_find_struct_const(
118 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
120 uint32_t max_binding
= 0;
121 uint32_t immutable_sampler_count
= 0;
122 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
123 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
124 if ((pCreateInfo
->pBindings
[j
].descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
125 pCreateInfo
->pBindings
[j
].descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
126 pCreateInfo
->pBindings
[j
].pImmutableSamplers
) {
127 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
131 uint32_t samplers_offset
= sizeof(struct tu_descriptor_set_layout
) +
132 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]);
133 uint32_t size
= samplers_offset
+ immutable_sampler_count
* A6XX_TEX_SAMP_DWORDS
* 4;
135 set_layout
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
136 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
138 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
140 set_layout
->flags
= pCreateInfo
->flags
;
142 /* We just allocate all the immutable samplers at the end of the struct */
143 struct tu_sampler
*samplers
= (void*) &set_layout
->binding
[max_binding
+ 1];
145 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(
146 pCreateInfo
->pBindings
, pCreateInfo
->bindingCount
);
148 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
149 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
152 set_layout
->binding_count
= max_binding
+ 1;
153 set_layout
->shader_stages
= 0;
154 set_layout
->has_immutable_samplers
= false;
155 set_layout
->size
= 0;
156 set_layout
->dynamic_ubo
= 0;
158 memset(set_layout
->binding
, 0,
159 size
- sizeof(struct tu_descriptor_set_layout
));
161 uint32_t dynamic_offset_count
= 0;
162 uint32_t input_attachment_count
= 0;
163 uint32_t buffer_count
= 0;
165 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
166 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ j
;
167 uint32_t b
= binding
->binding
;
169 set_layout
->binding
[b
].type
= binding
->descriptorType
;
170 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
171 set_layout
->binding
[b
].offset
= set_layout
->size
;
172 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
173 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
174 set_layout
->binding
[b
].input_attachment_offset
= input_attachment_count
;
175 set_layout
->binding
[b
].size
= descriptor_size(binding
->descriptorType
);
176 set_layout
->binding
[b
].shader_stages
= binding
->stageFlags
;
178 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
179 (variable_flags
->pBindingFlags
[binding
->binding
] &
180 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
181 assert(!binding
->pImmutableSamplers
); /* Terribly ill defined how
182 many samplers are valid */
183 assert(binding
->binding
== max_binding
);
185 set_layout
->has_variable_descriptors
= true;
188 if ((binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
189 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
190 binding
->pImmutableSamplers
) {
191 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
192 set_layout
->has_immutable_samplers
= true;
194 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
195 samplers
[i
] = *tu_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
197 samplers
+= binding
->descriptorCount
;
198 samplers_offset
+= sizeof(struct tu_sampler
) * binding
->descriptorCount
;
202 binding
->descriptorCount
* set_layout
->binding
[b
].size
;
203 if (binding
->descriptorType
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
204 binding
->descriptorType
!= VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
205 buffer_count
+= binding
->descriptorCount
;
206 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
||
207 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
) {
208 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
) {
209 STATIC_ASSERT(MAX_DYNAMIC_BUFFERS
<= 8 * sizeof(set_layout
->dynamic_ubo
));
210 set_layout
->dynamic_ubo
|=
211 ((1u << binding
->descriptorCount
) - 1) << dynamic_offset_count
;
214 dynamic_offset_count
+= binding
->descriptorCount
;
216 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
)
217 input_attachment_count
+= binding
->descriptorCount
;
218 set_layout
->shader_stages
|= binding
->stageFlags
;
223 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
224 set_layout
->input_attachment_count
= input_attachment_count
;
225 set_layout
->buffer_count
= buffer_count
;
227 *pSetLayout
= tu_descriptor_set_layout_to_handle(set_layout
);
233 tu_DestroyDescriptorSetLayout(VkDevice _device
,
234 VkDescriptorSetLayout _set_layout
,
235 const VkAllocationCallbacks
*pAllocator
)
237 TU_FROM_HANDLE(tu_device
, device
, _device
);
238 TU_FROM_HANDLE(tu_descriptor_set_layout
, set_layout
, _set_layout
);
243 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
247 tu_GetDescriptorSetLayoutSupport(
249 const VkDescriptorSetLayoutCreateInfo
*pCreateInfo
,
250 VkDescriptorSetLayoutSupport
*pSupport
)
252 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(
253 pCreateInfo
->pBindings
, pCreateInfo
->bindingCount
);
255 pSupport
->supported
= false;
259 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*variable_flags
=
260 vk_find_struct_const(
262 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
263 VkDescriptorSetVariableDescriptorCountLayoutSupportEXT
*variable_count
=
265 (void *) pCreateInfo
->pNext
,
266 DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT_EXT
);
267 if (variable_count
) {
268 variable_count
->maxVariableDescriptorCount
= 0;
271 bool supported
= true;
273 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
274 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ i
;
276 uint64_t descriptor_sz
= descriptor_size(binding
->descriptorType
);
277 uint64_t descriptor_alignment
= 8;
279 if (size
&& !align_u64(size
, descriptor_alignment
)) {
282 size
= align_u64(size
, descriptor_alignment
);
284 uint64_t max_count
= UINT64_MAX
;
286 max_count
= (UINT64_MAX
- size
) / descriptor_sz
;
288 if (max_count
< binding
->descriptorCount
) {
291 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
293 (variable_flags
->pBindingFlags
[binding
->binding
] &
294 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
295 variable_count
->maxVariableDescriptorCount
=
296 MIN2(UINT32_MAX
, max_count
);
298 size
+= binding
->descriptorCount
* descriptor_sz
;
303 pSupport
->supported
= supported
;
307 * Pipeline layouts. These have nothing to do with the pipeline. They are
308 * just multiple descriptor set layouts pasted together.
312 tu_CreatePipelineLayout(VkDevice _device
,
313 const VkPipelineLayoutCreateInfo
*pCreateInfo
,
314 const VkAllocationCallbacks
*pAllocator
,
315 VkPipelineLayout
*pPipelineLayout
)
317 TU_FROM_HANDLE(tu_device
, device
, _device
);
318 struct tu_pipeline_layout
*layout
;
319 struct mesa_sha1 ctx
;
321 assert(pCreateInfo
->sType
==
322 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
324 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
325 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
327 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
329 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
330 layout
->input_attachment_count
= 0;
331 layout
->dynamic_offset_count
= 0;
333 unsigned dynamic_offset_count
= 0, input_attachment_count
= 0;
335 _mesa_sha1_init(&ctx
);
336 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
337 TU_FROM_HANDLE(tu_descriptor_set_layout
, set_layout
,
338 pCreateInfo
->pSetLayouts
[set
]);
339 layout
->set
[set
].layout
= set_layout
;
340 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
341 layout
->set
[set
].input_attachment_start
= input_attachment_count
;
342 dynamic_offset_count
+= set_layout
->dynamic_offset_count
;
343 input_attachment_count
+= set_layout
->input_attachment_count
;
345 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
346 if (set_layout
->binding
[b
].immutable_samplers_offset
)
349 tu_immutable_samplers(set_layout
, set_layout
->binding
+ b
),
350 set_layout
->binding
[b
].array_size
* 4 * sizeof(uint32_t));
353 &ctx
, set_layout
->binding
,
354 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
357 layout
->dynamic_offset_count
= dynamic_offset_count
;
358 layout
->input_attachment_count
= input_attachment_count
;
359 layout
->push_constant_size
= 0;
361 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
362 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
363 layout
->push_constant_size
=
364 MAX2(layout
->push_constant_size
, range
->offset
+ range
->size
);
367 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
368 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
369 sizeof(layout
->push_constant_size
));
370 _mesa_sha1_final(&ctx
, layout
->sha1
);
371 *pPipelineLayout
= tu_pipeline_layout_to_handle(layout
);
377 tu_DestroyPipelineLayout(VkDevice _device
,
378 VkPipelineLayout _pipelineLayout
,
379 const VkAllocationCallbacks
*pAllocator
)
381 TU_FROM_HANDLE(tu_device
, device
, _device
);
382 TU_FROM_HANDLE(tu_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
384 if (!pipeline_layout
)
386 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
392 tu_descriptor_set_create(struct tu_device
*device
,
393 struct tu_descriptor_pool
*pool
,
394 const struct tu_descriptor_set_layout
*layout
,
395 const uint32_t *variable_count
,
396 struct tu_descriptor_set
**out_set
)
398 struct tu_descriptor_set
*set
;
399 uint32_t buffer_count
= layout
->buffer_count
;
400 if (variable_count
) {
402 if (layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
403 layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
405 buffer_count
= layout
->binding
[layout
->binding_count
- 1].buffer_offset
+
406 *variable_count
* stride
;
408 unsigned dynamic_offset
= sizeof(struct tu_descriptor_set
) +
409 sizeof(struct tu_bo
*) * buffer_count
;
410 unsigned mem_size
= dynamic_offset
+
411 A6XX_TEX_CONST_DWORDS
* 4 * (layout
->dynamic_offset_count
+
412 layout
->input_attachment_count
);;
414 if (pool
->host_memory_base
) {
415 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
416 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
418 set
= (struct tu_descriptor_set
*)pool
->host_memory_ptr
;
419 pool
->host_memory_ptr
+= mem_size
;
421 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
422 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
425 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
428 memset(set
, 0, mem_size
);
430 if (layout
->dynamic_offset_count
+ layout
->input_attachment_count
> 0) {
431 set
->dynamic_descriptors
= (uint32_t *)((uint8_t*)set
+ dynamic_offset
);
434 set
->layout
= layout
;
436 uint32_t layout_size
= layout
->size
;
437 if (variable_count
) {
438 assert(layout
->has_variable_descriptors
);
439 uint32_t stride
= layout
->binding
[layout
->binding_count
- 1].size
;
440 layout_size
= layout
->binding
[layout
->binding_count
- 1].offset
+
441 *variable_count
* stride
;
445 set
->size
= layout_size
;
447 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
448 vk_free2(&device
->alloc
, NULL
, set
);
449 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
452 /* try to allocate linearly first, so that we don't spend
453 * time looking for gaps if the app only allocates &
454 * resets via the pool. */
455 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
456 set
->mapped_ptr
= (uint32_t*)(pool
->bo
.map
+ pool
->current_offset
);
457 set
->va
= pool
->bo
.iova
+ pool
->current_offset
;
458 if (!pool
->host_memory_base
) {
459 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
460 pool
->entries
[pool
->entry_count
].size
= layout_size
;
461 pool
->entries
[pool
->entry_count
].set
= set
;
464 pool
->current_offset
+= layout_size
;
465 } else if (!pool
->host_memory_base
) {
469 for (index
= 0; index
< pool
->entry_count
; ++index
) {
470 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
472 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
475 if (pool
->size
- offset
< layout_size
) {
476 vk_free2(&device
->alloc
, NULL
, set
);
477 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
480 set
->mapped_ptr
= (uint32_t*)(pool
->bo
.map
+ offset
);
481 set
->va
= pool
->bo
.iova
+ offset
;
482 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
483 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
484 pool
->entries
[index
].offset
= offset
;
485 pool
->entries
[index
].size
= layout_size
;
486 pool
->entries
[index
].set
= set
;
489 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
497 tu_descriptor_set_destroy(struct tu_device
*device
,
498 struct tu_descriptor_pool
*pool
,
499 struct tu_descriptor_set
*set
,
502 assert(!pool
->host_memory_base
);
504 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
505 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- (uint8_t*)pool
->bo
.map
;
506 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
507 if (pool
->entries
[i
].offset
== offset
) {
508 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
509 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
515 vk_free2(&device
->alloc
, NULL
, set
);
519 tu_CreateDescriptorPool(VkDevice _device
,
520 const VkDescriptorPoolCreateInfo
*pCreateInfo
,
521 const VkAllocationCallbacks
*pAllocator
,
522 VkDescriptorPool
*pDescriptorPool
)
524 TU_FROM_HANDLE(tu_device
, device
, _device
);
525 struct tu_descriptor_pool
*pool
;
526 uint64_t size
= sizeof(struct tu_descriptor_pool
);
527 uint64_t bo_size
= 0, bo_count
= 0, dynamic_count
= 0;
529 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
530 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
531 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
533 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
534 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
535 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
536 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
537 dynamic_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
542 bo_size
+= descriptor_size(pCreateInfo
->pPoolSizes
[i
].type
) *
543 pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
546 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
547 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct tu_descriptor_set
);
548 host_size
+= sizeof(struct tu_bo
*) * bo_count
;
549 host_size
+= A6XX_TEX_CONST_DWORDS
* 4 * dynamic_count
;
552 size
+= sizeof(struct tu_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
555 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
556 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
558 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
560 memset(pool
, 0, sizeof(*pool
));
562 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
563 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct tu_descriptor_pool
);
564 pool
->host_memory_ptr
= pool
->host_memory_base
;
565 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
571 ret
= tu_bo_init_new(device
, &pool
->bo
, bo_size
);
572 assert(ret
== VK_SUCCESS
);
574 ret
= tu_bo_map(device
, &pool
->bo
);
575 assert(ret
== VK_SUCCESS
);
577 pool
->size
= bo_size
;
578 pool
->max_entry_count
= pCreateInfo
->maxSets
;
580 *pDescriptorPool
= tu_descriptor_pool_to_handle(pool
);
585 tu_DestroyDescriptorPool(VkDevice _device
,
586 VkDescriptorPool _pool
,
587 const VkAllocationCallbacks
*pAllocator
)
589 TU_FROM_HANDLE(tu_device
, device
, _device
);
590 TU_FROM_HANDLE(tu_descriptor_pool
, pool
, _pool
);
595 if (!pool
->host_memory_base
) {
596 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
597 tu_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
602 tu_bo_finish(device
, &pool
->bo
);
603 vk_free2(&device
->alloc
, pAllocator
, pool
);
607 tu_ResetDescriptorPool(VkDevice _device
,
608 VkDescriptorPool descriptorPool
,
609 VkDescriptorPoolResetFlags flags
)
611 TU_FROM_HANDLE(tu_device
, device
, _device
);
612 TU_FROM_HANDLE(tu_descriptor_pool
, pool
, descriptorPool
);
614 if (!pool
->host_memory_base
) {
615 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
616 tu_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
618 pool
->entry_count
= 0;
621 pool
->current_offset
= 0;
622 pool
->host_memory_ptr
= pool
->host_memory_base
;
628 tu_AllocateDescriptorSets(VkDevice _device
,
629 const VkDescriptorSetAllocateInfo
*pAllocateInfo
,
630 VkDescriptorSet
*pDescriptorSets
)
632 TU_FROM_HANDLE(tu_device
, device
, _device
);
633 TU_FROM_HANDLE(tu_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
635 VkResult result
= VK_SUCCESS
;
637 struct tu_descriptor_set
*set
= NULL
;
639 const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT
*variable_counts
=
640 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT
);
641 const uint32_t zero
= 0;
643 /* allocate a set of buffers for each shader to contain descriptors */
644 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
645 TU_FROM_HANDLE(tu_descriptor_set_layout
, layout
,
646 pAllocateInfo
->pSetLayouts
[i
]);
648 const uint32_t *variable_count
= NULL
;
649 if (variable_counts
) {
650 if (i
< variable_counts
->descriptorSetCount
)
651 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
653 variable_count
= &zero
;
656 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
658 result
= tu_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
659 if (result
!= VK_SUCCESS
)
662 pDescriptorSets
[i
] = tu_descriptor_set_to_handle(set
);
665 if (result
!= VK_SUCCESS
) {
666 tu_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
668 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
669 pDescriptorSets
[i
] = VK_NULL_HANDLE
;
676 tu_FreeDescriptorSets(VkDevice _device
,
677 VkDescriptorPool descriptorPool
,
679 const VkDescriptorSet
*pDescriptorSets
)
681 TU_FROM_HANDLE(tu_device
, device
, _device
);
682 TU_FROM_HANDLE(tu_descriptor_pool
, pool
, descriptorPool
);
684 for (uint32_t i
= 0; i
< count
; i
++) {
685 TU_FROM_HANDLE(tu_descriptor_set
, set
, pDescriptorSets
[i
]);
687 if (set
&& !pool
->host_memory_base
)
688 tu_descriptor_set_destroy(device
, pool
, set
, true);
693 static void write_texel_buffer_descriptor(struct tu_device
*device
,
694 struct tu_cmd_buffer
*cmd_buffer
,
696 struct tu_bo
**buffer_list
,
697 const VkBufferView buffer_view
)
699 TU_FROM_HANDLE(tu_buffer_view
, view
, buffer_view
);
701 memcpy(dst
, view
->descriptor
, sizeof(view
->descriptor
));
704 tu_bo_list_add(&cmd_buffer
->bo_list
, view
->buffer
->bo
, MSM_SUBMIT_BO_READ
);
706 *buffer_list
= view
->buffer
->bo
;
709 static uint32_t get_range(struct tu_buffer
*buf
, VkDeviceSize offset
,
712 if (range
== VK_WHOLE_SIZE
) {
713 return buf
->size
- offset
;
719 static void write_buffer_descriptor(struct tu_device
*device
,
720 struct tu_cmd_buffer
*cmd_buffer
,
722 struct tu_bo
**buffer_list
,
723 const VkDescriptorBufferInfo
*buffer_info
)
725 TU_FROM_HANDLE(tu_buffer
, buffer
, buffer_info
->buffer
);
727 uint64_t va
= tu_buffer_iova(buffer
) + buffer_info
->offset
;
728 uint32_t range
= get_range(buffer
, buffer_info
->offset
, buffer_info
->range
);
729 range
= ALIGN_POT(range
, 4) / 4;
731 A6XX_IBO_0_TILE_MODE(TILE6_LINEAR
) | A6XX_IBO_0_FMT(FMT6_32_UINT
);
734 A6XX_IBO_2_UNK4
| A6XX_IBO_2_TYPE(A6XX_TEX_1D
) | A6XX_IBO_2_UNK31
;
736 dst
[4] = A6XX_IBO_4_BASE_LO(va
);
737 dst
[5] = A6XX_IBO_5_BASE_HI(va
>> 32);
738 for (int i
= 6; i
< A6XX_TEX_CONST_DWORDS
; i
++)
742 tu_bo_list_add(&cmd_buffer
->bo_list
, buffer
->bo
, MSM_SUBMIT_BO_READ
);
744 *buffer_list
= buffer
->bo
;
747 static void write_ubo_descriptor(struct tu_device
*device
,
748 struct tu_cmd_buffer
*cmd_buffer
,
750 struct tu_bo
**buffer_list
,
751 const VkDescriptorBufferInfo
*buffer_info
)
753 TU_FROM_HANDLE(tu_buffer
, buffer
, buffer_info
->buffer
);
755 uint32_t range
= get_range(buffer
, buffer_info
->offset
, buffer_info
->range
);
756 /* The HW range is in vec4 units */
757 range
= ALIGN_POT(range
, 16) / 16;
758 uint64_t va
= tu_buffer_iova(buffer
) + buffer_info
->offset
;
759 dst
[0] = A6XX_UBO_0_BASE_LO(va
);
760 dst
[1] = A6XX_UBO_1_BASE_HI(va
>> 32) | A6XX_UBO_1_SIZE(range
);
763 tu_bo_list_add(&cmd_buffer
->bo_list
, buffer
->bo
, MSM_SUBMIT_BO_READ
);
765 *buffer_list
= buffer
->bo
;
769 write_image_descriptor(struct tu_device
*device
,
770 struct tu_cmd_buffer
*cmd_buffer
,
772 struct tu_bo
**buffer_list
,
773 VkDescriptorType descriptor_type
,
774 const VkDescriptorImageInfo
*image_info
)
776 TU_FROM_HANDLE(tu_image_view
, iview
, image_info
->imageView
);
778 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
779 memcpy(dst
, iview
->storage_descriptor
, sizeof(iview
->storage_descriptor
));
781 memcpy(dst
, iview
->descriptor
, sizeof(iview
->descriptor
));
785 tu_bo_list_add(&cmd_buffer
->bo_list
, iview
->image
->bo
, MSM_SUBMIT_BO_READ
);
787 *buffer_list
= iview
->image
->bo
;
791 write_combined_image_sampler_descriptor(struct tu_device
*device
,
792 struct tu_cmd_buffer
*cmd_buffer
,
793 unsigned sampler_offset
,
795 struct tu_bo
**buffer_list
,
796 VkDescriptorType descriptor_type
,
797 const VkDescriptorImageInfo
*image_info
,
800 TU_FROM_HANDLE(tu_sampler
, sampler
, image_info
->sampler
);
802 write_image_descriptor(device
, cmd_buffer
, dst
, buffer_list
,
803 descriptor_type
, image_info
);
804 /* copy over sampler state */
806 memcpy(dst
+ A6XX_TEX_CONST_DWORDS
, sampler
, sizeof(*sampler
));
811 write_sampler_descriptor(struct tu_device
*device
,
813 const VkDescriptorImageInfo
*image_info
)
815 TU_FROM_HANDLE(tu_sampler
, sampler
, image_info
->sampler
);
817 memcpy(dst
, sampler
, sizeof(*sampler
));
821 tu_update_descriptor_sets(struct tu_device
*device
,
822 struct tu_cmd_buffer
*cmd_buffer
,
823 VkDescriptorSet dstSetOverride
,
824 uint32_t descriptorWriteCount
,
825 const VkWriteDescriptorSet
*pDescriptorWrites
,
826 uint32_t descriptorCopyCount
,
827 const VkCopyDescriptorSet
*pDescriptorCopies
)
830 for (i
= 0; i
< descriptorWriteCount
; i
++) {
831 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
832 TU_FROM_HANDLE(tu_descriptor_set
, set
,
833 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
834 const struct tu_descriptor_set_binding_layout
*binding_layout
=
835 set
->layout
->binding
+ writeset
->dstBinding
;
836 uint32_t *ptr
= set
->mapped_ptr
;
837 struct tu_bo
**buffer_list
= set
->buffers
;
839 ptr
+= binding_layout
->offset
/ 4;
841 ptr
+= (binding_layout
->size
/ 4) * writeset
->dstArrayElement
;
842 buffer_list
+= binding_layout
->buffer_offset
;
843 buffer_list
+= writeset
->dstArrayElement
;
844 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
845 switch(writeset
->descriptorType
) {
846 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
: {
847 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
848 unsigned idx
= writeset
->dstArrayElement
+ j
;
849 idx
+= set
->layout
->input_attachment_count
+ binding_layout
->dynamic_offset_offset
;
850 write_ubo_descriptor(device
, cmd_buffer
,
851 set
->dynamic_descriptors
+ A6XX_TEX_CONST_DWORDS
* idx
,
852 buffer_list
, writeset
->pBufferInfo
+ j
);
855 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
856 write_ubo_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
857 writeset
->pBufferInfo
+ j
);
859 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
860 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
861 unsigned idx
= writeset
->dstArrayElement
+ j
;
862 idx
+= set
->layout
->input_attachment_count
+ binding_layout
->dynamic_offset_offset
;
863 write_buffer_descriptor(device
, cmd_buffer
,
864 set
->dynamic_descriptors
+ A6XX_TEX_CONST_DWORDS
* idx
,
865 buffer_list
, writeset
->pBufferInfo
+ j
);
868 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
869 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
870 writeset
->pBufferInfo
+ j
);
872 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
873 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
874 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
875 writeset
->pTexelBufferView
[j
]);
877 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
878 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
879 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
880 writeset
->descriptorType
,
881 writeset
->pImageInfo
+ j
);
883 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
: {
884 unsigned idx
= writeset
->dstArrayElement
+ j
;
885 idx
+= binding_layout
->input_attachment_offset
;
886 write_image_descriptor(device
, cmd_buffer
,
887 set
->dynamic_descriptors
+ A6XX_TEX_CONST_DWORDS
* idx
,
888 buffer_list
, writeset
->descriptorType
,
889 writeset
->pImageInfo
+ j
);
892 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
893 write_combined_image_sampler_descriptor(device
, cmd_buffer
,
894 A6XX_TEX_CONST_DWORDS
* 4,
896 writeset
->descriptorType
,
897 writeset
->pImageInfo
+ j
,
898 !binding_layout
->immutable_samplers_offset
);
900 case VK_DESCRIPTOR_TYPE_SAMPLER
:
901 write_sampler_descriptor(device
, ptr
, writeset
->pImageInfo
+ j
);
904 unreachable("unimplemented descriptor type");
907 ptr
+= binding_layout
->size
/ 4;
912 for (i
= 0; i
< descriptorCopyCount
; i
++) {
913 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
914 TU_FROM_HANDLE(tu_descriptor_set
, src_set
,
916 TU_FROM_HANDLE(tu_descriptor_set
, dst_set
,
918 const struct tu_descriptor_set_binding_layout
*src_binding_layout
=
919 src_set
->layout
->binding
+ copyset
->srcBinding
;
920 const struct tu_descriptor_set_binding_layout
*dst_binding_layout
=
921 dst_set
->layout
->binding
+ copyset
->dstBinding
;
922 uint32_t *src_ptr
= src_set
->mapped_ptr
;
923 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
924 struct tu_bo
**src_buffer_list
= src_set
->buffers
;
925 struct tu_bo
**dst_buffer_list
= dst_set
->buffers
;
927 src_ptr
+= src_binding_layout
->offset
/ 4;
928 dst_ptr
+= dst_binding_layout
->offset
/ 4;
930 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
931 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
933 src_buffer_list
+= src_binding_layout
->buffer_offset
;
934 src_buffer_list
+= copyset
->srcArrayElement
;
936 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
937 dst_buffer_list
+= copyset
->dstArrayElement
;
939 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
940 switch (src_binding_layout
->type
) {
941 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
942 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
943 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
944 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
945 src_idx
+= src_set
->layout
->input_attachment_count
;
946 dst_idx
+= dst_set
->layout
->input_attachment_count
;
947 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
948 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
950 uint32_t *src_dynamic
, *dst_dynamic
;
951 src_dynamic
= src_set
->dynamic_descriptors
+ src_idx
* A6XX_TEX_CONST_DWORDS
;
952 dst_dynamic
= dst_set
->dynamic_descriptors
+ dst_idx
* A6XX_TEX_CONST_DWORDS
;
953 memcpy(dst_dynamic
, src_dynamic
, A6XX_TEX_CONST_DWORDS
* 4);
956 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
: {
957 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
958 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
959 src_idx
+= src_binding_layout
->input_attachment_offset
;
960 dst_idx
+= dst_binding_layout
->input_attachment_offset
;
962 uint32_t *src_dynamic
, *dst_dynamic
;
963 src_dynamic
= src_set
->dynamic_descriptors
+ src_idx
* A6XX_TEX_CONST_DWORDS
;
964 dst_dynamic
= dst_set
->dynamic_descriptors
+ dst_idx
* A6XX_TEX_CONST_DWORDS
;
965 memcpy(dst_dynamic
, src_dynamic
, A6XX_TEX_CONST_DWORDS
* 4);
969 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
972 src_ptr
+= src_binding_layout
->size
/ 4;
973 dst_ptr
+= dst_binding_layout
->size
/ 4;
975 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
976 /* Sampler descriptors don't have a buffer list. */
977 dst_buffer_list
[j
] = src_buffer_list
[j
];
984 tu_UpdateDescriptorSets(VkDevice _device
,
985 uint32_t descriptorWriteCount
,
986 const VkWriteDescriptorSet
*pDescriptorWrites
,
987 uint32_t descriptorCopyCount
,
988 const VkCopyDescriptorSet
*pDescriptorCopies
)
990 TU_FROM_HANDLE(tu_device
, device
, _device
);
992 tu_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
,
993 descriptorWriteCount
, pDescriptorWrites
,
994 descriptorCopyCount
, pDescriptorCopies
);
998 tu_CreateDescriptorUpdateTemplate(
1000 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1001 const VkAllocationCallbacks
*pAllocator
,
1002 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1004 TU_FROM_HANDLE(tu_device
, device
, _device
);
1005 TU_FROM_HANDLE(tu_descriptor_set_layout
, set_layout
,
1006 pCreateInfo
->descriptorSetLayout
);
1007 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1009 sizeof(struct tu_descriptor_update_template
) +
1010 sizeof(struct tu_descriptor_update_template_entry
) * entry_count
;
1011 struct tu_descriptor_update_template
*templ
;
1013 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1014 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1016 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1018 *pDescriptorUpdateTemplate
=
1019 tu_descriptor_update_template_to_handle(templ
);
1021 tu_use_args(set_layout
);
1027 tu_DestroyDescriptorUpdateTemplate(
1029 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1030 const VkAllocationCallbacks
*pAllocator
)
1032 TU_FROM_HANDLE(tu_device
, device
, _device
);
1033 TU_FROM_HANDLE(tu_descriptor_update_template
, templ
,
1034 descriptorUpdateTemplate
);
1039 vk_free2(&device
->alloc
, pAllocator
, templ
);
1043 tu_update_descriptor_set_with_template(
1044 struct tu_device
*device
,
1045 struct tu_cmd_buffer
*cmd_buffer
,
1046 struct tu_descriptor_set
*set
,
1047 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1050 TU_FROM_HANDLE(tu_descriptor_update_template
, templ
,
1051 descriptorUpdateTemplate
);
1056 tu_UpdateDescriptorSetWithTemplate(
1058 VkDescriptorSet descriptorSet
,
1059 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1062 TU_FROM_HANDLE(tu_device
, device
, _device
);
1063 TU_FROM_HANDLE(tu_descriptor_set
, set
, descriptorSet
);
1065 tu_update_descriptor_set_with_template(device
, NULL
, set
,
1066 descriptorUpdateTemplate
, pData
);
1070 tu_CreateSamplerYcbcrConversion(
1072 const VkSamplerYcbcrConversionCreateInfo
*pCreateInfo
,
1073 const VkAllocationCallbacks
*pAllocator
,
1074 VkSamplerYcbcrConversion
*pYcbcrConversion
)
1076 *pYcbcrConversion
= VK_NULL_HANDLE
;
1081 tu_DestroySamplerYcbcrConversion(VkDevice device
,
1082 VkSamplerYcbcrConversion ycbcrConversion
,
1083 const VkAllocationCallbacks
*pAllocator
)