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 uint32_t ycbcr_sampler_count
= 0;
123 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
124 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
125 if ((pCreateInfo
->pBindings
[j
].descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
126 pCreateInfo
->pBindings
[j
].descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
127 pCreateInfo
->pBindings
[j
].pImmutableSamplers
) {
128 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
130 bool has_ycbcr_sampler
= false;
131 for (unsigned i
= 0; i
< pCreateInfo
->pBindings
[j
].descriptorCount
; ++i
) {
132 if (tu_sampler_from_handle(pCreateInfo
->pBindings
[j
].pImmutableSamplers
[i
])->ycbcr_sampler
)
133 has_ycbcr_sampler
= true;
136 if (has_ycbcr_sampler
)
137 ycbcr_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
141 uint32_t samplers_offset
= sizeof(struct tu_descriptor_set_layout
) +
142 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]);
143 /* note: only need to store TEX_SAMP_DWORDS for immutable samples,
144 * but using struct tu_sampler makes things simpler */
145 uint32_t size
= samplers_offset
+
146 immutable_sampler_count
* sizeof(struct tu_sampler
) +
147 ycbcr_sampler_count
* sizeof(struct tu_sampler_ycbcr_conversion
);
149 set_layout
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
150 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
152 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
154 set_layout
->flags
= pCreateInfo
->flags
;
156 /* We just allocate all the immutable samplers at the end of the struct */
157 struct tu_sampler
*samplers
= (void*) &set_layout
->binding
[max_binding
+ 1];
158 struct tu_sampler_ycbcr_conversion
*ycbcr_samplers
=
159 (void*) &samplers
[immutable_sampler_count
];
161 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(
162 pCreateInfo
->pBindings
, pCreateInfo
->bindingCount
);
164 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
165 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
168 set_layout
->binding_count
= max_binding
+ 1;
169 set_layout
->shader_stages
= 0;
170 set_layout
->has_immutable_samplers
= false;
171 set_layout
->size
= 0;
172 set_layout
->dynamic_ubo
= 0;
174 memset(set_layout
->binding
, 0,
175 size
- sizeof(struct tu_descriptor_set_layout
));
177 uint32_t dynamic_offset_count
= 0;
178 uint32_t input_attachment_count
= 0;
179 uint32_t buffer_count
= 0;
181 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
182 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ j
;
183 uint32_t b
= binding
->binding
;
185 set_layout
->binding
[b
].type
= binding
->descriptorType
;
186 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
187 set_layout
->binding
[b
].offset
= set_layout
->size
;
188 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
189 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
190 set_layout
->binding
[b
].input_attachment_offset
= input_attachment_count
;
191 set_layout
->binding
[b
].size
= descriptor_size(binding
->descriptorType
);
192 set_layout
->binding
[b
].shader_stages
= binding
->stageFlags
;
194 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
195 (variable_flags
->pBindingFlags
[binding
->binding
] &
196 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
197 assert(!binding
->pImmutableSamplers
); /* Terribly ill defined how
198 many samplers are valid */
199 assert(binding
->binding
== max_binding
);
201 set_layout
->has_variable_descriptors
= true;
204 if ((binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
||
205 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
) &&
206 binding
->pImmutableSamplers
) {
207 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
208 set_layout
->has_immutable_samplers
= true;
210 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
211 samplers
[i
] = *tu_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
213 samplers
+= binding
->descriptorCount
;
214 samplers_offset
+= sizeof(struct tu_sampler
) * binding
->descriptorCount
;
216 bool has_ycbcr_sampler
= false;
217 for (unsigned i
= 0; i
< pCreateInfo
->pBindings
[j
].descriptorCount
; ++i
) {
218 if (tu_sampler_from_handle(binding
->pImmutableSamplers
[i
])->ycbcr_sampler
)
219 has_ycbcr_sampler
= true;
222 if (has_ycbcr_sampler
) {
223 set_layout
->binding
[b
].ycbcr_samplers_offset
=
224 (const char*)ycbcr_samplers
- (const char*)set_layout
;
225 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++) {
226 struct tu_sampler
*sampler
= tu_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
227 if (sampler
->ycbcr_sampler
)
228 ycbcr_samplers
[i
] = *sampler
->ycbcr_sampler
;
230 ycbcr_samplers
[i
].ycbcr_model
= VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY
;
232 ycbcr_samplers
+= binding
->descriptorCount
;
234 set_layout
->binding
[b
].ycbcr_samplers_offset
= 0;
239 binding
->descriptorCount
* set_layout
->binding
[b
].size
;
240 if (binding
->descriptorType
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
241 binding
->descriptorType
!= VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
242 buffer_count
+= binding
->descriptorCount
;
243 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
||
244 binding
->descriptorType
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
) {
245 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
) {
246 STATIC_ASSERT(MAX_DYNAMIC_BUFFERS
<= 8 * sizeof(set_layout
->dynamic_ubo
));
247 set_layout
->dynamic_ubo
|=
248 ((1u << binding
->descriptorCount
) - 1) << dynamic_offset_count
;
251 dynamic_offset_count
+= binding
->descriptorCount
;
253 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
)
254 input_attachment_count
+= binding
->descriptorCount
;
255 set_layout
->shader_stages
|= binding
->stageFlags
;
260 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
261 set_layout
->input_attachment_count
= input_attachment_count
;
262 set_layout
->buffer_count
= buffer_count
;
264 *pSetLayout
= tu_descriptor_set_layout_to_handle(set_layout
);
270 tu_DestroyDescriptorSetLayout(VkDevice _device
,
271 VkDescriptorSetLayout _set_layout
,
272 const VkAllocationCallbacks
*pAllocator
)
274 TU_FROM_HANDLE(tu_device
, device
, _device
);
275 TU_FROM_HANDLE(tu_descriptor_set_layout
, set_layout
, _set_layout
);
280 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
284 tu_GetDescriptorSetLayoutSupport(
286 const VkDescriptorSetLayoutCreateInfo
*pCreateInfo
,
287 VkDescriptorSetLayoutSupport
*pSupport
)
289 VkDescriptorSetLayoutBinding
*bindings
= create_sorted_bindings(
290 pCreateInfo
->pBindings
, pCreateInfo
->bindingCount
);
292 pSupport
->supported
= false;
296 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT
*variable_flags
=
297 vk_find_struct_const(
299 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT
);
300 VkDescriptorSetVariableDescriptorCountLayoutSupportEXT
*variable_count
=
302 (void *) pCreateInfo
->pNext
,
303 DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT_EXT
);
304 if (variable_count
) {
305 variable_count
->maxVariableDescriptorCount
= 0;
308 bool supported
= true;
310 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
311 const VkDescriptorSetLayoutBinding
*binding
= bindings
+ i
;
313 uint64_t descriptor_sz
= descriptor_size(binding
->descriptorType
);
314 uint64_t descriptor_alignment
= 8;
316 if (size
&& !align_u64(size
, descriptor_alignment
)) {
319 size
= align_u64(size
, descriptor_alignment
);
321 uint64_t max_count
= UINT64_MAX
;
323 max_count
= (UINT64_MAX
- size
) / descriptor_sz
;
325 if (max_count
< binding
->descriptorCount
) {
328 if (variable_flags
&& binding
->binding
< variable_flags
->bindingCount
&&
330 (variable_flags
->pBindingFlags
[binding
->binding
] &
331 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT
)) {
332 variable_count
->maxVariableDescriptorCount
=
333 MIN2(UINT32_MAX
, max_count
);
335 size
+= binding
->descriptorCount
* descriptor_sz
;
340 pSupport
->supported
= supported
;
344 * Pipeline layouts. These have nothing to do with the pipeline. They are
345 * just multiple descriptor set layouts pasted together.
349 tu_CreatePipelineLayout(VkDevice _device
,
350 const VkPipelineLayoutCreateInfo
*pCreateInfo
,
351 const VkAllocationCallbacks
*pAllocator
,
352 VkPipelineLayout
*pPipelineLayout
)
354 TU_FROM_HANDLE(tu_device
, device
, _device
);
355 struct tu_pipeline_layout
*layout
;
356 struct mesa_sha1 ctx
;
358 assert(pCreateInfo
->sType
==
359 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
361 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
362 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
364 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
366 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
367 layout
->input_attachment_count
= 0;
368 layout
->dynamic_offset_count
= 0;
370 unsigned dynamic_offset_count
= 0, input_attachment_count
= 0;
372 _mesa_sha1_init(&ctx
);
373 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
374 TU_FROM_HANDLE(tu_descriptor_set_layout
, set_layout
,
375 pCreateInfo
->pSetLayouts
[set
]);
376 layout
->set
[set
].layout
= set_layout
;
377 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
378 layout
->set
[set
].input_attachment_start
= input_attachment_count
;
379 dynamic_offset_count
+= set_layout
->dynamic_offset_count
;
380 input_attachment_count
+= set_layout
->input_attachment_count
;
382 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
383 if (set_layout
->binding
[b
].immutable_samplers_offset
)
386 tu_immutable_samplers(set_layout
, set_layout
->binding
+ b
),
387 set_layout
->binding
[b
].array_size
* 4 * sizeof(uint32_t));
390 &ctx
, set_layout
->binding
,
391 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
394 layout
->dynamic_offset_count
= dynamic_offset_count
;
395 layout
->input_attachment_count
= input_attachment_count
;
396 layout
->push_constant_size
= 0;
398 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
399 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
400 layout
->push_constant_size
=
401 MAX2(layout
->push_constant_size
, range
->offset
+ range
->size
);
404 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
405 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
406 sizeof(layout
->push_constant_size
));
407 _mesa_sha1_final(&ctx
, layout
->sha1
);
408 *pPipelineLayout
= tu_pipeline_layout_to_handle(layout
);
414 tu_DestroyPipelineLayout(VkDevice _device
,
415 VkPipelineLayout _pipelineLayout
,
416 const VkAllocationCallbacks
*pAllocator
)
418 TU_FROM_HANDLE(tu_device
, device
, _device
);
419 TU_FROM_HANDLE(tu_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
421 if (!pipeline_layout
)
423 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
429 tu_descriptor_set_create(struct tu_device
*device
,
430 struct tu_descriptor_pool
*pool
,
431 const struct tu_descriptor_set_layout
*layout
,
432 const uint32_t *variable_count
,
433 struct tu_descriptor_set
**out_set
)
435 struct tu_descriptor_set
*set
;
436 uint32_t buffer_count
= layout
->buffer_count
;
437 if (variable_count
) {
439 if (layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_SAMPLER
||
440 layout
->binding
[layout
->binding_count
- 1].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
)
442 buffer_count
= layout
->binding
[layout
->binding_count
- 1].buffer_offset
+
443 *variable_count
* stride
;
445 unsigned dynamic_offset
= sizeof(struct tu_descriptor_set
) +
446 sizeof(struct tu_bo
*) * buffer_count
;
447 unsigned mem_size
= dynamic_offset
+
448 A6XX_TEX_CONST_DWORDS
* 4 * (layout
->dynamic_offset_count
+
449 layout
->input_attachment_count
);;
451 if (pool
->host_memory_base
) {
452 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
453 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
455 set
= (struct tu_descriptor_set
*)pool
->host_memory_ptr
;
456 pool
->host_memory_ptr
+= mem_size
;
458 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
459 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
462 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
465 memset(set
, 0, mem_size
);
467 if (layout
->dynamic_offset_count
+ layout
->input_attachment_count
> 0) {
468 set
->dynamic_descriptors
= (uint32_t *)((uint8_t*)set
+ dynamic_offset
);
471 set
->layout
= layout
;
473 uint32_t layout_size
= layout
->size
;
474 if (variable_count
) {
475 assert(layout
->has_variable_descriptors
);
476 uint32_t stride
= layout
->binding
[layout
->binding_count
- 1].size
;
477 layout_size
= layout
->binding
[layout
->binding_count
- 1].offset
+
478 *variable_count
* stride
;
482 set
->size
= layout_size
;
484 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
485 vk_free2(&device
->alloc
, NULL
, set
);
486 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
489 /* try to allocate linearly first, so that we don't spend
490 * time looking for gaps if the app only allocates &
491 * resets via the pool. */
492 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
493 set
->mapped_ptr
= (uint32_t*)(pool
->bo
.map
+ pool
->current_offset
);
494 set
->va
= pool
->bo
.iova
+ pool
->current_offset
;
495 if (!pool
->host_memory_base
) {
496 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
497 pool
->entries
[pool
->entry_count
].size
= layout_size
;
498 pool
->entries
[pool
->entry_count
].set
= set
;
501 pool
->current_offset
+= layout_size
;
502 } else if (!pool
->host_memory_base
) {
506 for (index
= 0; index
< pool
->entry_count
; ++index
) {
507 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
509 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
512 if (pool
->size
- offset
< layout_size
) {
513 vk_free2(&device
->alloc
, NULL
, set
);
514 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
517 set
->mapped_ptr
= (uint32_t*)(pool
->bo
.map
+ offset
);
518 set
->va
= pool
->bo
.iova
+ offset
;
519 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
520 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
521 pool
->entries
[index
].offset
= offset
;
522 pool
->entries
[index
].size
= layout_size
;
523 pool
->entries
[index
].set
= set
;
526 return vk_error(device
->instance
, VK_ERROR_OUT_OF_POOL_MEMORY
);
529 if (layout
->has_immutable_samplers
) {
530 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
531 if (!layout
->binding
[i
].immutable_samplers_offset
)
534 unsigned offset
= layout
->binding
[i
].offset
/ 4;
535 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
536 offset
+= A6XX_TEX_CONST_DWORDS
;
538 const struct tu_sampler
*samplers
=
539 (const struct tu_sampler
*)((const char *)layout
+
540 layout
->binding
[i
].immutable_samplers_offset
);
541 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
542 memcpy(set
->mapped_ptr
+ offset
, samplers
[j
].descriptor
,
543 sizeof(samplers
[j
].descriptor
));
544 offset
+= layout
->binding
[i
].size
/ 4;
554 tu_descriptor_set_destroy(struct tu_device
*device
,
555 struct tu_descriptor_pool
*pool
,
556 struct tu_descriptor_set
*set
,
559 assert(!pool
->host_memory_base
);
561 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
562 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- (uint8_t*)pool
->bo
.map
;
563 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
564 if (pool
->entries
[i
].offset
== offset
) {
565 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
566 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
572 vk_free2(&device
->alloc
, NULL
, set
);
576 tu_CreateDescriptorPool(VkDevice _device
,
577 const VkDescriptorPoolCreateInfo
*pCreateInfo
,
578 const VkAllocationCallbacks
*pAllocator
,
579 VkDescriptorPool
*pDescriptorPool
)
581 TU_FROM_HANDLE(tu_device
, device
, _device
);
582 struct tu_descriptor_pool
*pool
;
583 uint64_t size
= sizeof(struct tu_descriptor_pool
);
584 uint64_t bo_size
= 0, bo_count
= 0, dynamic_count
= 0;
586 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
587 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
588 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
590 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
591 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
592 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
593 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
594 dynamic_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
599 bo_size
+= descriptor_size(pCreateInfo
->pPoolSizes
[i
].type
) *
600 pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
603 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
604 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct tu_descriptor_set
);
605 host_size
+= sizeof(struct tu_bo
*) * bo_count
;
606 host_size
+= A6XX_TEX_CONST_DWORDS
* 4 * dynamic_count
;
609 size
+= sizeof(struct tu_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
612 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
613 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
615 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
617 memset(pool
, 0, sizeof(*pool
));
619 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
620 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct tu_descriptor_pool
);
621 pool
->host_memory_ptr
= pool
->host_memory_base
;
622 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
628 ret
= tu_bo_init_new(device
, &pool
->bo
, bo_size
);
629 assert(ret
== VK_SUCCESS
);
631 ret
= tu_bo_map(device
, &pool
->bo
);
632 assert(ret
== VK_SUCCESS
);
634 pool
->size
= bo_size
;
635 pool
->max_entry_count
= pCreateInfo
->maxSets
;
637 *pDescriptorPool
= tu_descriptor_pool_to_handle(pool
);
642 tu_DestroyDescriptorPool(VkDevice _device
,
643 VkDescriptorPool _pool
,
644 const VkAllocationCallbacks
*pAllocator
)
646 TU_FROM_HANDLE(tu_device
, device
, _device
);
647 TU_FROM_HANDLE(tu_descriptor_pool
, pool
, _pool
);
652 if (!pool
->host_memory_base
) {
653 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
654 tu_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
659 tu_bo_finish(device
, &pool
->bo
);
660 vk_free2(&device
->alloc
, pAllocator
, pool
);
664 tu_ResetDescriptorPool(VkDevice _device
,
665 VkDescriptorPool descriptorPool
,
666 VkDescriptorPoolResetFlags flags
)
668 TU_FROM_HANDLE(tu_device
, device
, _device
);
669 TU_FROM_HANDLE(tu_descriptor_pool
, pool
, descriptorPool
);
671 if (!pool
->host_memory_base
) {
672 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
673 tu_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
675 pool
->entry_count
= 0;
678 pool
->current_offset
= 0;
679 pool
->host_memory_ptr
= pool
->host_memory_base
;
685 tu_AllocateDescriptorSets(VkDevice _device
,
686 const VkDescriptorSetAllocateInfo
*pAllocateInfo
,
687 VkDescriptorSet
*pDescriptorSets
)
689 TU_FROM_HANDLE(tu_device
, device
, _device
);
690 TU_FROM_HANDLE(tu_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
692 VkResult result
= VK_SUCCESS
;
694 struct tu_descriptor_set
*set
= NULL
;
696 const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT
*variable_counts
=
697 vk_find_struct_const(pAllocateInfo
->pNext
, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT
);
698 const uint32_t zero
= 0;
700 /* allocate a set of buffers for each shader to contain descriptors */
701 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
702 TU_FROM_HANDLE(tu_descriptor_set_layout
, layout
,
703 pAllocateInfo
->pSetLayouts
[i
]);
705 const uint32_t *variable_count
= NULL
;
706 if (variable_counts
) {
707 if (i
< variable_counts
->descriptorSetCount
)
708 variable_count
= variable_counts
->pDescriptorCounts
+ i
;
710 variable_count
= &zero
;
713 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
715 result
= tu_descriptor_set_create(device
, pool
, layout
, variable_count
, &set
);
716 if (result
!= VK_SUCCESS
)
719 pDescriptorSets
[i
] = tu_descriptor_set_to_handle(set
);
722 if (result
!= VK_SUCCESS
) {
723 tu_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
725 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
726 pDescriptorSets
[i
] = VK_NULL_HANDLE
;
733 tu_FreeDescriptorSets(VkDevice _device
,
734 VkDescriptorPool descriptorPool
,
736 const VkDescriptorSet
*pDescriptorSets
)
738 TU_FROM_HANDLE(tu_device
, device
, _device
);
739 TU_FROM_HANDLE(tu_descriptor_pool
, pool
, descriptorPool
);
741 for (uint32_t i
= 0; i
< count
; i
++) {
742 TU_FROM_HANDLE(tu_descriptor_set
, set
, pDescriptorSets
[i
]);
744 if (set
&& !pool
->host_memory_base
)
745 tu_descriptor_set_destroy(device
, pool
, set
, true);
750 static void write_texel_buffer_descriptor(struct tu_device
*device
,
751 struct tu_cmd_buffer
*cmd_buffer
,
753 struct tu_bo
**buffer_list
,
754 const VkBufferView buffer_view
)
756 TU_FROM_HANDLE(tu_buffer_view
, view
, buffer_view
);
758 memcpy(dst
, view
->descriptor
, sizeof(view
->descriptor
));
761 tu_bo_list_add(&cmd_buffer
->bo_list
, view
->buffer
->bo
, MSM_SUBMIT_BO_READ
);
763 *buffer_list
= view
->buffer
->bo
;
766 static uint32_t get_range(struct tu_buffer
*buf
, VkDeviceSize offset
,
769 if (range
== VK_WHOLE_SIZE
) {
770 return buf
->size
- offset
;
776 static void write_buffer_descriptor(struct tu_device
*device
,
777 struct tu_cmd_buffer
*cmd_buffer
,
779 struct tu_bo
**buffer_list
,
780 const VkDescriptorBufferInfo
*buffer_info
)
782 TU_FROM_HANDLE(tu_buffer
, buffer
, buffer_info
->buffer
);
784 uint64_t va
= tu_buffer_iova(buffer
) + buffer_info
->offset
;
785 uint32_t range
= get_range(buffer
, buffer_info
->offset
, buffer_info
->range
);
786 range
= ALIGN_POT(range
, 4) / 4;
788 A6XX_IBO_0_TILE_MODE(TILE6_LINEAR
) | A6XX_IBO_0_FMT(FMT6_32_UINT
);
791 A6XX_IBO_2_UNK4
| A6XX_IBO_2_TYPE(A6XX_TEX_1D
) | A6XX_IBO_2_UNK31
;
793 dst
[4] = A6XX_IBO_4_BASE_LO(va
);
794 dst
[5] = A6XX_IBO_5_BASE_HI(va
>> 32);
795 for (int i
= 6; i
< A6XX_TEX_CONST_DWORDS
; i
++)
799 tu_bo_list_add(&cmd_buffer
->bo_list
, buffer
->bo
, MSM_SUBMIT_BO_READ
);
801 *buffer_list
= buffer
->bo
;
804 static void write_ubo_descriptor(struct tu_device
*device
,
805 struct tu_cmd_buffer
*cmd_buffer
,
807 struct tu_bo
**buffer_list
,
808 const VkDescriptorBufferInfo
*buffer_info
)
810 TU_FROM_HANDLE(tu_buffer
, buffer
, buffer_info
->buffer
);
812 uint32_t range
= get_range(buffer
, buffer_info
->offset
, buffer_info
->range
);
813 /* The HW range is in vec4 units */
814 range
= ALIGN_POT(range
, 16) / 16;
815 uint64_t va
= tu_buffer_iova(buffer
) + buffer_info
->offset
;
816 dst
[0] = A6XX_UBO_0_BASE_LO(va
);
817 dst
[1] = A6XX_UBO_1_BASE_HI(va
>> 32) | A6XX_UBO_1_SIZE(range
);
820 tu_bo_list_add(&cmd_buffer
->bo_list
, buffer
->bo
, MSM_SUBMIT_BO_READ
);
822 *buffer_list
= buffer
->bo
;
826 write_image_descriptor(struct tu_device
*device
,
827 struct tu_cmd_buffer
*cmd_buffer
,
829 struct tu_bo
**buffer_list
,
830 VkDescriptorType descriptor_type
,
831 const VkDescriptorImageInfo
*image_info
)
833 TU_FROM_HANDLE(tu_image_view
, iview
, image_info
->imageView
);
835 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
836 memcpy(dst
, iview
->storage_descriptor
, sizeof(iview
->storage_descriptor
));
838 memcpy(dst
, iview
->descriptor
, sizeof(iview
->descriptor
));
842 tu_bo_list_add(&cmd_buffer
->bo_list
, iview
->image
->bo
, MSM_SUBMIT_BO_READ
);
844 *buffer_list
= iview
->image
->bo
;
848 write_combined_image_sampler_descriptor(struct tu_device
*device
,
849 struct tu_cmd_buffer
*cmd_buffer
,
850 unsigned sampler_offset
,
852 struct tu_bo
**buffer_list
,
853 VkDescriptorType descriptor_type
,
854 const VkDescriptorImageInfo
*image_info
,
857 TU_FROM_HANDLE(tu_sampler
, sampler
, image_info
->sampler
);
859 write_image_descriptor(device
, cmd_buffer
, dst
, buffer_list
,
860 descriptor_type
, image_info
);
861 /* copy over sampler state */
863 memcpy(dst
+ A6XX_TEX_CONST_DWORDS
, sampler
, sizeof(*sampler
));
868 write_sampler_descriptor(struct tu_device
*device
,
870 const VkDescriptorImageInfo
*image_info
)
872 TU_FROM_HANDLE(tu_sampler
, sampler
, image_info
->sampler
);
874 memcpy(dst
, sampler
, sizeof(*sampler
));
878 tu_update_descriptor_sets(struct tu_device
*device
,
879 struct tu_cmd_buffer
*cmd_buffer
,
880 VkDescriptorSet dstSetOverride
,
881 uint32_t descriptorWriteCount
,
882 const VkWriteDescriptorSet
*pDescriptorWrites
,
883 uint32_t descriptorCopyCount
,
884 const VkCopyDescriptorSet
*pDescriptorCopies
)
887 for (i
= 0; i
< descriptorWriteCount
; i
++) {
888 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
889 TU_FROM_HANDLE(tu_descriptor_set
, set
,
890 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
891 const struct tu_descriptor_set_binding_layout
*binding_layout
=
892 set
->layout
->binding
+ writeset
->dstBinding
;
893 uint32_t *ptr
= set
->mapped_ptr
;
894 struct tu_bo
**buffer_list
= set
->buffers
;
896 ptr
+= binding_layout
->offset
/ 4;
898 ptr
+= (binding_layout
->size
/ 4) * writeset
->dstArrayElement
;
899 buffer_list
+= binding_layout
->buffer_offset
;
900 buffer_list
+= writeset
->dstArrayElement
;
901 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
902 switch(writeset
->descriptorType
) {
903 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
: {
904 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
905 unsigned idx
= writeset
->dstArrayElement
+ j
;
906 idx
+= set
->layout
->input_attachment_count
+ binding_layout
->dynamic_offset_offset
;
907 write_ubo_descriptor(device
, cmd_buffer
,
908 set
->dynamic_descriptors
+ A6XX_TEX_CONST_DWORDS
* idx
,
909 buffer_list
, writeset
->pBufferInfo
+ j
);
912 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
913 write_ubo_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
914 writeset
->pBufferInfo
+ j
);
916 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
917 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
918 unsigned idx
= writeset
->dstArrayElement
+ j
;
919 idx
+= set
->layout
->input_attachment_count
+ binding_layout
->dynamic_offset_offset
;
920 write_buffer_descriptor(device
, cmd_buffer
,
921 set
->dynamic_descriptors
+ A6XX_TEX_CONST_DWORDS
* idx
,
922 buffer_list
, writeset
->pBufferInfo
+ j
);
925 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
926 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
927 writeset
->pBufferInfo
+ j
);
929 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
930 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
931 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
932 writeset
->pTexelBufferView
[j
]);
934 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
935 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
936 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
937 writeset
->descriptorType
,
938 writeset
->pImageInfo
+ j
);
940 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
: {
941 unsigned idx
= writeset
->dstArrayElement
+ j
;
942 idx
+= binding_layout
->input_attachment_offset
;
943 write_image_descriptor(device
, cmd_buffer
,
944 set
->dynamic_descriptors
+ A6XX_TEX_CONST_DWORDS
* idx
,
945 buffer_list
, writeset
->descriptorType
,
946 writeset
->pImageInfo
+ j
);
949 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
950 write_combined_image_sampler_descriptor(device
, cmd_buffer
,
951 A6XX_TEX_CONST_DWORDS
* 4,
953 writeset
->descriptorType
,
954 writeset
->pImageInfo
+ j
,
955 !binding_layout
->immutable_samplers_offset
);
957 case VK_DESCRIPTOR_TYPE_SAMPLER
:
958 write_sampler_descriptor(device
, ptr
, writeset
->pImageInfo
+ j
);
961 unreachable("unimplemented descriptor type");
964 ptr
+= binding_layout
->size
/ 4;
969 for (i
= 0; i
< descriptorCopyCount
; i
++) {
970 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
971 TU_FROM_HANDLE(tu_descriptor_set
, src_set
,
973 TU_FROM_HANDLE(tu_descriptor_set
, dst_set
,
975 const struct tu_descriptor_set_binding_layout
*src_binding_layout
=
976 src_set
->layout
->binding
+ copyset
->srcBinding
;
977 const struct tu_descriptor_set_binding_layout
*dst_binding_layout
=
978 dst_set
->layout
->binding
+ copyset
->dstBinding
;
979 uint32_t *src_ptr
= src_set
->mapped_ptr
;
980 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
981 struct tu_bo
**src_buffer_list
= src_set
->buffers
;
982 struct tu_bo
**dst_buffer_list
= dst_set
->buffers
;
984 src_ptr
+= src_binding_layout
->offset
/ 4;
985 dst_ptr
+= dst_binding_layout
->offset
/ 4;
987 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
988 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
990 src_buffer_list
+= src_binding_layout
->buffer_offset
;
991 src_buffer_list
+= copyset
->srcArrayElement
;
993 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
994 dst_buffer_list
+= copyset
->dstArrayElement
;
996 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
997 switch (src_binding_layout
->type
) {
998 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
999 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1000 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1001 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1002 src_idx
+= src_set
->layout
->input_attachment_count
;
1003 dst_idx
+= dst_set
->layout
->input_attachment_count
;
1004 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
1005 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
1007 uint32_t *src_dynamic
, *dst_dynamic
;
1008 src_dynamic
= src_set
->dynamic_descriptors
+ src_idx
* A6XX_TEX_CONST_DWORDS
;
1009 dst_dynamic
= dst_set
->dynamic_descriptors
+ dst_idx
* A6XX_TEX_CONST_DWORDS
;
1010 memcpy(dst_dynamic
, src_dynamic
, A6XX_TEX_CONST_DWORDS
* 4);
1013 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
: {
1014 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
1015 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
1016 src_idx
+= src_binding_layout
->input_attachment_offset
;
1017 dst_idx
+= dst_binding_layout
->input_attachment_offset
;
1019 uint32_t *src_dynamic
, *dst_dynamic
;
1020 src_dynamic
= src_set
->dynamic_descriptors
+ src_idx
* A6XX_TEX_CONST_DWORDS
;
1021 dst_dynamic
= dst_set
->dynamic_descriptors
+ dst_idx
* A6XX_TEX_CONST_DWORDS
;
1022 memcpy(dst_dynamic
, src_dynamic
, A6XX_TEX_CONST_DWORDS
* 4);
1026 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
1029 src_ptr
+= src_binding_layout
->size
/ 4;
1030 dst_ptr
+= dst_binding_layout
->size
/ 4;
1032 if (src_binding_layout
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
) {
1033 /* Sampler descriptors don't have a buffer list. */
1034 dst_buffer_list
[j
] = src_buffer_list
[j
];
1041 tu_UpdateDescriptorSets(VkDevice _device
,
1042 uint32_t descriptorWriteCount
,
1043 const VkWriteDescriptorSet
*pDescriptorWrites
,
1044 uint32_t descriptorCopyCount
,
1045 const VkCopyDescriptorSet
*pDescriptorCopies
)
1047 TU_FROM_HANDLE(tu_device
, device
, _device
);
1049 tu_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
,
1050 descriptorWriteCount
, pDescriptorWrites
,
1051 descriptorCopyCount
, pDescriptorCopies
);
1055 tu_CreateDescriptorUpdateTemplate(
1057 const VkDescriptorUpdateTemplateCreateInfo
*pCreateInfo
,
1058 const VkAllocationCallbacks
*pAllocator
,
1059 VkDescriptorUpdateTemplate
*pDescriptorUpdateTemplate
)
1061 TU_FROM_HANDLE(tu_device
, device
, _device
);
1062 TU_FROM_HANDLE(tu_descriptor_set_layout
, set_layout
,
1063 pCreateInfo
->descriptorSetLayout
);
1064 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
1066 sizeof(struct tu_descriptor_update_template
) +
1067 sizeof(struct tu_descriptor_update_template_entry
) * entry_count
;
1068 struct tu_descriptor_update_template
*templ
;
1070 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1071 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1073 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1075 templ
->entry_count
= entry_count
;
1077 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
) {
1078 TU_FROM_HANDLE(tu_pipeline_layout
, pipeline_layout
, pCreateInfo
->pipelineLayout
);
1080 /* descriptorSetLayout should be ignored for push descriptors
1081 * and instead it refers to pipelineLayout and set.
1083 assert(pCreateInfo
->set
< MAX_SETS
);
1084 set_layout
= pipeline_layout
->set
[pCreateInfo
->set
].layout
;
1087 for (uint32_t i
= 0; i
< entry_count
; i
++) {
1088 const VkDescriptorUpdateTemplateEntry
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
1090 const struct tu_descriptor_set_binding_layout
*binding_layout
=
1091 set_layout
->binding
+ entry
->dstBinding
;
1092 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+
1093 entry
->dstArrayElement
;
1094 uint32_t dst_offset
, dst_stride
;
1096 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1097 * is dynamic, and an offset into mapped_ptr otherwise.
1099 switch (entry
->descriptorType
) {
1100 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1101 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1102 dst_offset
= (set_layout
->input_attachment_count
+
1103 binding_layout
->dynamic_offset_offset
+
1104 entry
->dstArrayElement
) * A6XX_TEX_CONST_DWORDS
;
1105 dst_stride
= A6XX_TEX_CONST_DWORDS
;
1107 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1108 dst_offset
= (binding_layout
->input_attachment_offset
+
1109 entry
->dstArrayElement
) * A6XX_TEX_CONST_DWORDS
;
1110 dst_stride
= A6XX_TEX_CONST_DWORDS
;
1113 dst_offset
= binding_layout
->offset
/ 4;
1114 dst_offset
+= (binding_layout
->size
* entry
->dstArrayElement
) / 4;
1115 dst_stride
= binding_layout
->size
/ 4;
1118 templ
->entry
[i
] = (struct tu_descriptor_update_template_entry
) {
1119 .descriptor_type
= entry
->descriptorType
,
1120 .descriptor_count
= entry
->descriptorCount
,
1121 .src_offset
= entry
->offset
,
1122 .src_stride
= entry
->stride
,
1123 .dst_offset
= dst_offset
,
1124 .dst_stride
= dst_stride
,
1125 .buffer_offset
= buffer_offset
,
1126 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
1130 *pDescriptorUpdateTemplate
=
1131 tu_descriptor_update_template_to_handle(templ
);
1137 tu_DestroyDescriptorUpdateTemplate(
1139 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1140 const VkAllocationCallbacks
*pAllocator
)
1142 TU_FROM_HANDLE(tu_device
, device
, _device
);
1143 TU_FROM_HANDLE(tu_descriptor_update_template
, templ
,
1144 descriptorUpdateTemplate
);
1149 vk_free2(&device
->alloc
, pAllocator
, templ
);
1153 tu_update_descriptor_set_with_template(
1154 struct tu_device
*device
,
1155 struct tu_cmd_buffer
*cmd_buffer
,
1156 struct tu_descriptor_set
*set
,
1157 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1160 TU_FROM_HANDLE(tu_descriptor_update_template
, templ
,
1161 descriptorUpdateTemplate
);
1163 for (uint32_t i
= 0; i
< templ
->entry_count
; i
++) {
1164 uint32_t *ptr
= set
->mapped_ptr
;
1165 const void *src
= ((const char *) pData
) + templ
->entry
[i
].src_offset
;
1166 struct tu_bo
**buffer_list
= set
->buffers
;
1168 ptr
+= templ
->entry
[i
].dst_offset
;
1169 buffer_list
+= templ
->entry
[i
].buffer_offset
;
1170 unsigned dst_offset
= templ
->entry
[i
].dst_offset
;
1171 for (unsigned j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1172 switch(templ
->entry
[i
].descriptor_type
) {
1173 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
: {
1174 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1175 write_ubo_descriptor(device
, cmd_buffer
,
1176 set
->dynamic_descriptors
+ dst_offset
,
1180 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1181 write_ubo_descriptor(device
, cmd_buffer
, ptr
, buffer_list
, src
);
1183 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1184 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1185 write_buffer_descriptor(device
, cmd_buffer
,
1186 set
->dynamic_descriptors
+ dst_offset
,
1190 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1191 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
, src
);
1193 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1194 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1195 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
,
1196 buffer_list
, *(VkBufferView
*) src
);
1198 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1199 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1200 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
1201 templ
->entry
[i
].descriptor_type
,
1204 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
: {
1205 write_image_descriptor(device
, cmd_buffer
,
1206 set
->dynamic_descriptors
+ dst_offset
,
1207 buffer_list
, templ
->entry
[i
].descriptor_type
,
1211 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1212 write_combined_image_sampler_descriptor(device
, cmd_buffer
,
1213 A6XX_TEX_CONST_DWORDS
* 4,
1215 templ
->entry
[i
].descriptor_type
,
1217 templ
->entry
[i
].has_sampler
);
1219 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1220 write_sampler_descriptor(device
, ptr
, src
);
1223 unreachable("unimplemented descriptor type");
1226 src
= (char *) src
+ templ
->entry
[i
].src_stride
;
1227 ptr
+= templ
->entry
[i
].dst_stride
;
1228 dst_offset
+= templ
->entry
[i
].dst_stride
;
1235 tu_UpdateDescriptorSetWithTemplate(
1237 VkDescriptorSet descriptorSet
,
1238 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1241 TU_FROM_HANDLE(tu_device
, device
, _device
);
1242 TU_FROM_HANDLE(tu_descriptor_set
, set
, descriptorSet
);
1244 tu_update_descriptor_set_with_template(device
, NULL
, set
,
1245 descriptorUpdateTemplate
, pData
);
1249 tu_CreateSamplerYcbcrConversion(
1251 const VkSamplerYcbcrConversionCreateInfo
*pCreateInfo
,
1252 const VkAllocationCallbacks
*pAllocator
,
1253 VkSamplerYcbcrConversion
*pYcbcrConversion
)
1255 TU_FROM_HANDLE(tu_device
, device
, _device
);
1256 struct tu_sampler_ycbcr_conversion
*conversion
;
1258 conversion
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*conversion
), 8,
1259 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1261 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1263 conversion
->format
= pCreateInfo
->format
;
1264 conversion
->ycbcr_model
= pCreateInfo
->ycbcrModel
;
1265 conversion
->ycbcr_range
= pCreateInfo
->ycbcrRange
;
1266 conversion
->components
= pCreateInfo
->components
;
1267 conversion
->chroma_offsets
[0] = pCreateInfo
->xChromaOffset
;
1268 conversion
->chroma_offsets
[1] = pCreateInfo
->yChromaOffset
;
1269 conversion
->chroma_filter
= pCreateInfo
->chromaFilter
;
1271 *pYcbcrConversion
= tu_sampler_ycbcr_conversion_to_handle(conversion
);
1276 tu_DestroySamplerYcbcrConversion(VkDevice _device
,
1277 VkSamplerYcbcrConversion ycbcrConversion
,
1278 const VkAllocationCallbacks
*pAllocator
)
1280 TU_FROM_HANDLE(tu_device
, device
, _device
);
1281 TU_FROM_HANDLE(tu_sampler_ycbcr_conversion
, ycbcr_conversion
, ycbcrConversion
);
1283 if (ycbcr_conversion
)
1284 vk_free2(&device
->alloc
, pAllocator
, ycbcr_conversion
);