1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 /**
26 * @file
27 *
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.
36 */
37
38 #include "tu_private.h"
39
40 #include <assert.h>
41 #include <fcntl.h>
42 #include <stdbool.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include "util/mesa-sha1.h"
47 #include "vk_util.h"
48
49 static int
binding_compare(const void * av,const void * bv)50 binding_compare(const void *av, const void *bv)
51 {
52 const VkDescriptorSetLayoutBinding *a =
53 (const VkDescriptorSetLayoutBinding *) av;
54 const VkDescriptorSetLayoutBinding *b =
55 (const VkDescriptorSetLayoutBinding *) bv;
56
57 return (a->binding < b->binding) ? -1 : (a->binding > b->binding) ? 1 : 0;
58 }
59
60 static VkDescriptorSetLayoutBinding *
create_sorted_bindings(const VkDescriptorSetLayoutBinding * bindings,unsigned count)61 create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings,
62 unsigned count)
63 {
64 VkDescriptorSetLayoutBinding *sorted_bindings =
65 malloc(count * sizeof(VkDescriptorSetLayoutBinding));
66 if (!sorted_bindings)
67 return NULL;
68
69 memcpy(sorted_bindings, bindings,
70 count * sizeof(VkDescriptorSetLayoutBinding));
71
72 qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding),
73 binding_compare);
74
75 return sorted_bindings;
76 }
77
78 static uint32_t
descriptor_size(VkDescriptorType type)79 descriptor_size(VkDescriptorType type)
80 {
81 switch (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:
87 * Input attachment doesn't use descriptor sets at all
88 */
89 return 0;
90 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
91 /* We make offsets and sizes all 16 dwords, to match how the hardware
92 * interprets indices passed to sample/load/store instructions in
93 * multiples of 16 dwords. This means that "normal" descriptors are all
94 * of size 16, with padding for smaller descriptors like uniform storage
95 * descriptors which are less than 16 dwords. However combined images
96 * and samplers are actually two descriptors, so they have size 2.
97 */
98 return A6XX_TEX_CONST_DWORDS * 4 * 2;
99 default:
100 return A6XX_TEX_CONST_DWORDS * 4;
101 }
102 }
103
104 VkResult
tu_CreateDescriptorSetLayout(VkDevice _device,const VkDescriptorSetLayoutCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkDescriptorSetLayout * pSetLayout)105 tu_CreateDescriptorSetLayout(
106 VkDevice _device,
107 const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
108 const VkAllocationCallbacks *pAllocator,
109 VkDescriptorSetLayout *pSetLayout)
110 {
111 TU_FROM_HANDLE(tu_device, device, _device);
112 struct tu_descriptor_set_layout *set_layout;
113
114 assert(pCreateInfo->sType ==
115 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
116 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT *variable_flags =
117 vk_find_struct_const(
118 pCreateInfo->pNext,
119 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT);
120
121 uint32_t max_binding = 0;
122 uint32_t immutable_sampler_count = 0;
123 uint32_t ycbcr_sampler_count = 0;
124 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
125 max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
126 if ((pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
127 pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&
128 pCreateInfo->pBindings[j].pImmutableSamplers) {
129 immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
130
131 bool has_ycbcr_sampler = false;
132 for (unsigned i = 0; i < pCreateInfo->pBindings[j].descriptorCount; ++i) {
133 if (tu_sampler_from_handle(pCreateInfo->pBindings[j].pImmutableSamplers[i])->ycbcr_sampler)
134 has_ycbcr_sampler = true;
135 }
136
137 if (has_ycbcr_sampler)
138 ycbcr_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
139 }
140 }
141
142 uint32_t samplers_offset =
143 offsetof(struct tu_descriptor_set_layout, binding[max_binding + 1]);
144
145 /* note: only need to store TEX_SAMP_DWORDS for immutable samples,
146 * but using struct tu_sampler makes things simpler */
147 uint32_t size = samplers_offset +
148 immutable_sampler_count * sizeof(struct tu_sampler) +
149 ycbcr_sampler_count * sizeof(struct tu_sampler_ycbcr_conversion);
150
151 set_layout = vk_object_zalloc(&device->vk, pAllocator, size,
152 VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT);
153 if (!set_layout)
154 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
155
156 set_layout->flags = pCreateInfo->flags;
157
158 /* We just allocate all the immutable samplers at the end of the struct */
159 struct tu_sampler *samplers = (void*) &set_layout->binding[max_binding + 1];
160 struct tu_sampler_ycbcr_conversion *ycbcr_samplers =
161 (void*) &samplers[immutable_sampler_count];
162
163 VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(
164 pCreateInfo->pBindings, pCreateInfo->bindingCount);
165 if (!bindings) {
166 vk_object_free(&device->vk, pAllocator, set_layout);
167 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
168 }
169
170 set_layout->binding_count = max_binding + 1;
171 set_layout->shader_stages = 0;
172 set_layout->has_immutable_samplers = false;
173 set_layout->size = 0;
174 set_layout->dynamic_ubo = 0;
175
176 uint32_t dynamic_offset_count = 0;
177
178 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
179 const VkDescriptorSetLayoutBinding *binding = bindings + j;
180 uint32_t b = binding->binding;
181
182 set_layout->binding[b].type = binding->descriptorType;
183 set_layout->binding[b].array_size = binding->descriptorCount;
184 set_layout->binding[b].offset = set_layout->size;
185 set_layout->binding[b].dynamic_offset_offset = dynamic_offset_count;
186 set_layout->binding[b].size = descriptor_size(binding->descriptorType);
187 set_layout->binding[b].shader_stages = binding->stageFlags;
188
189 if (variable_flags && binding->binding < variable_flags->bindingCount &&
190 (variable_flags->pBindingFlags[binding->binding] &
191 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT)) {
192 assert(!binding->pImmutableSamplers); /* Terribly ill defined how
193 many samplers are valid */
194 assert(binding->binding == max_binding);
195
196 set_layout->has_variable_descriptors = true;
197 }
198
199 if ((binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
200 binding->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&
201 binding->pImmutableSamplers) {
202 set_layout->binding[b].immutable_samplers_offset = samplers_offset;
203 set_layout->has_immutable_samplers = true;
204
205 for (uint32_t i = 0; i < binding->descriptorCount; i++)
206 samplers[i] = *tu_sampler_from_handle(binding->pImmutableSamplers[i]);
207
208 samplers += binding->descriptorCount;
209 samplers_offset += sizeof(struct tu_sampler) * binding->descriptorCount;
210
211 bool has_ycbcr_sampler = false;
212 for (unsigned i = 0; i < pCreateInfo->pBindings[j].descriptorCount; ++i) {
213 if (tu_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler)
214 has_ycbcr_sampler = true;
215 }
216
217 if (has_ycbcr_sampler) {
218 set_layout->binding[b].ycbcr_samplers_offset =
219 (const char*)ycbcr_samplers - (const char*)set_layout;
220 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
221 struct tu_sampler *sampler = tu_sampler_from_handle(binding->pImmutableSamplers[i]);
222 if (sampler->ycbcr_sampler)
223 ycbcr_samplers[i] = *sampler->ycbcr_sampler;
224 else
225 ycbcr_samplers[i].ycbcr_model = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
226 }
227 ycbcr_samplers += binding->descriptorCount;
228 } else {
229 set_layout->binding[b].ycbcr_samplers_offset = 0;
230 }
231 }
232
233 set_layout->size +=
234 binding->descriptorCount * set_layout->binding[b].size;
235 if (binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC ||
236 binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
237 if (binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
238 STATIC_ASSERT(MAX_DYNAMIC_BUFFERS <= 8 * sizeof(set_layout->dynamic_ubo));
239 set_layout->dynamic_ubo |=
240 ((1u << binding->descriptorCount) - 1) << dynamic_offset_count;
241 }
242
243 dynamic_offset_count += binding->descriptorCount;
244 }
245
246 set_layout->shader_stages |= binding->stageFlags;
247 }
248
249 free(bindings);
250
251 set_layout->dynamic_offset_count = dynamic_offset_count;
252
253 *pSetLayout = tu_descriptor_set_layout_to_handle(set_layout);
254
255 return VK_SUCCESS;
256 }
257
258 void
tu_DestroyDescriptorSetLayout(VkDevice _device,VkDescriptorSetLayout _set_layout,const VkAllocationCallbacks * pAllocator)259 tu_DestroyDescriptorSetLayout(VkDevice _device,
260 VkDescriptorSetLayout _set_layout,
261 const VkAllocationCallbacks *pAllocator)
262 {
263 TU_FROM_HANDLE(tu_device, device, _device);
264 TU_FROM_HANDLE(tu_descriptor_set_layout, set_layout, _set_layout);
265
266 if (!set_layout)
267 return;
268
269 vk_object_free(&device->vk, pAllocator, set_layout);
270 }
271
272 void
tu_GetDescriptorSetLayoutSupport(VkDevice device,const VkDescriptorSetLayoutCreateInfo * pCreateInfo,VkDescriptorSetLayoutSupport * pSupport)273 tu_GetDescriptorSetLayoutSupport(
274 VkDevice device,
275 const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
276 VkDescriptorSetLayoutSupport *pSupport)
277 {
278 VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(
279 pCreateInfo->pBindings, pCreateInfo->bindingCount);
280 if (!bindings) {
281 pSupport->supported = false;
282 return;
283 }
284
285 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT *variable_flags =
286 vk_find_struct_const(
287 pCreateInfo->pNext,
288 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT);
289 VkDescriptorSetVariableDescriptorCountLayoutSupportEXT *variable_count =
290 vk_find_struct(
291 (void *) pCreateInfo->pNext,
292 DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT_EXT);
293 if (variable_count) {
294 variable_count->maxVariableDescriptorCount = 0;
295 }
296
297 bool supported = true;
298 uint64_t size = 0;
299 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
300 const VkDescriptorSetLayoutBinding *binding = bindings + i;
301
302 uint64_t descriptor_sz = descriptor_size(binding->descriptorType);
303 uint64_t descriptor_alignment = 8;
304
305 if (size && !ALIGN_POT(size, descriptor_alignment)) {
306 supported = false;
307 }
308 size = ALIGN_POT(size, descriptor_alignment);
309
310 uint64_t max_count = UINT64_MAX;
311 if (descriptor_sz)
312 max_count = (UINT64_MAX - size) / descriptor_sz;
313
314 if (max_count < binding->descriptorCount) {
315 supported = false;
316 }
317 if (variable_flags && binding->binding < variable_flags->bindingCount &&
318 variable_count &&
319 (variable_flags->pBindingFlags[binding->binding] &
320 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT)) {
321 variable_count->maxVariableDescriptorCount =
322 MIN2(UINT32_MAX, max_count);
323 }
324 size += binding->descriptorCount * descriptor_sz;
325 }
326
327 free(bindings);
328
329 pSupport->supported = supported;
330 }
331
332 /*
333 * Pipeline layouts. These have nothing to do with the pipeline. They are
334 * just multiple descriptor set layouts pasted together.
335 */
336
337 VkResult
tu_CreatePipelineLayout(VkDevice _device,const VkPipelineLayoutCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkPipelineLayout * pPipelineLayout)338 tu_CreatePipelineLayout(VkDevice _device,
339 const VkPipelineLayoutCreateInfo *pCreateInfo,
340 const VkAllocationCallbacks *pAllocator,
341 VkPipelineLayout *pPipelineLayout)
342 {
343 TU_FROM_HANDLE(tu_device, device, _device);
344 struct tu_pipeline_layout *layout;
345
346 assert(pCreateInfo->sType ==
347 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
348
349 layout = vk_object_alloc(&device->vk, pAllocator, sizeof(*layout),
350 VK_OBJECT_TYPE_PIPELINE_LAYOUT);
351 if (layout == NULL)
352 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
353
354 layout->num_sets = pCreateInfo->setLayoutCount;
355 layout->dynamic_offset_count = 0;
356
357 unsigned dynamic_offset_count = 0;
358
359 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
360 TU_FROM_HANDLE(tu_descriptor_set_layout, set_layout,
361 pCreateInfo->pSetLayouts[set]);
362 layout->set[set].layout = set_layout;
363 layout->set[set].dynamic_offset_start = dynamic_offset_count;
364 dynamic_offset_count += set_layout->dynamic_offset_count;
365 }
366
367 layout->dynamic_offset_count = dynamic_offset_count;
368 layout->push_constant_size = 0;
369
370 for (unsigned i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
371 const VkPushConstantRange *range = pCreateInfo->pPushConstantRanges + i;
372 layout->push_constant_size =
373 MAX2(layout->push_constant_size, range->offset + range->size);
374 }
375
376 layout->push_constant_size = align(layout->push_constant_size, 16);
377 *pPipelineLayout = tu_pipeline_layout_to_handle(layout);
378
379 return VK_SUCCESS;
380 }
381
382 void
tu_DestroyPipelineLayout(VkDevice _device,VkPipelineLayout _pipelineLayout,const VkAllocationCallbacks * pAllocator)383 tu_DestroyPipelineLayout(VkDevice _device,
384 VkPipelineLayout _pipelineLayout,
385 const VkAllocationCallbacks *pAllocator)
386 {
387 TU_FROM_HANDLE(tu_device, device, _device);
388 TU_FROM_HANDLE(tu_pipeline_layout, pipeline_layout, _pipelineLayout);
389
390 if (!pipeline_layout)
391 return;
392
393 vk_object_free(&device->vk, pAllocator, pipeline_layout);
394 }
395
396 #define EMPTY 1
397
398 static VkResult
tu_descriptor_set_create(struct tu_device * device,struct tu_descriptor_pool * pool,const struct tu_descriptor_set_layout * layout,const uint32_t * variable_count,struct tu_descriptor_set ** out_set)399 tu_descriptor_set_create(struct tu_device *device,
400 struct tu_descriptor_pool *pool,
401 const struct tu_descriptor_set_layout *layout,
402 const uint32_t *variable_count,
403 struct tu_descriptor_set **out_set)
404 {
405 struct tu_descriptor_set *set;
406 unsigned dynamic_offset = sizeof(struct tu_descriptor_set);
407 unsigned mem_size = dynamic_offset +
408 A6XX_TEX_CONST_DWORDS * 4 * layout->dynamic_offset_count;
409
410 if (pool->host_memory_base) {
411 if (pool->host_memory_end - pool->host_memory_ptr < mem_size)
412 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
413
414 set = (struct tu_descriptor_set*)pool->host_memory_ptr;
415 pool->host_memory_ptr += mem_size;
416 } else {
417 set = vk_alloc2(&device->vk.alloc, NULL, mem_size, 8,
418 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
419
420 if (!set)
421 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
422 }
423
424 memset(set, 0, mem_size);
425 vk_object_base_init(&device->vk, &set->base, VK_OBJECT_TYPE_DESCRIPTOR_SET);
426
427 if (layout->dynamic_offset_count) {
428 set->dynamic_descriptors = (uint32_t *)((uint8_t*)set + dynamic_offset);
429 }
430
431 set->layout = layout;
432 set->pool = pool;
433 uint32_t layout_size = layout->size;
434 if (variable_count) {
435 assert(layout->has_variable_descriptors);
436 uint32_t stride = layout->binding[layout->binding_count - 1].size;
437 layout_size = layout->binding[layout->binding_count - 1].offset +
438 *variable_count * stride;
439 }
440
441 if (layout_size) {
442 set->size = layout_size;
443
444 if (!pool->host_memory_base && pool->entry_count == pool->max_entry_count) {
445 vk_object_free(&device->vk, NULL, set);
446 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
447 }
448
449 /* try to allocate linearly first, so that we don't spend
450 * time looking for gaps if the app only allocates &
451 * resets via the pool. */
452 if (pool->current_offset + layout_size <= pool->size) {
453 set->mapped_ptr = (uint32_t*)(pool->bo.map + pool->current_offset);
454 set->va = pool->bo.iova + pool->current_offset;
455 if (!pool->host_memory_base) {
456 pool->entries[pool->entry_count].offset = pool->current_offset;
457 pool->entries[pool->entry_count].size = layout_size;
458 pool->entries[pool->entry_count].set = set;
459 pool->entry_count++;
460 }
461 pool->current_offset += layout_size;
462 } else if (!pool->host_memory_base) {
463 uint64_t offset = 0;
464 int index;
465
466 for (index = 0; index < pool->entry_count; ++index) {
467 if (pool->entries[index].offset - offset >= layout_size)
468 break;
469 offset = pool->entries[index].offset + pool->entries[index].size;
470 }
471
472 if (pool->size - offset < layout_size) {
473 vk_object_free(&device->vk, NULL, set);
474 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
475 }
476
477 set->mapped_ptr = (uint32_t*)(pool->bo.map + offset);
478 set->va = pool->bo.iova + offset;
479 memmove(&pool->entries[index + 1], &pool->entries[index],
480 sizeof(pool->entries[0]) * (pool->entry_count - index));
481 pool->entries[index].offset = offset;
482 pool->entries[index].size = layout_size;
483 pool->entries[index].set = set;
484 pool->entry_count++;
485 } else
486 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
487 }
488
489 if (layout->has_immutable_samplers) {
490 for (unsigned i = 0; i < layout->binding_count; ++i) {
491 if (!layout->binding[i].immutable_samplers_offset)
492 continue;
493
494 unsigned offset = layout->binding[i].offset / 4;
495 if (layout->binding[i].type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
496 offset += A6XX_TEX_CONST_DWORDS;
497
498 const struct tu_sampler *samplers =
499 (const struct tu_sampler *)((const char *)layout +
500 layout->binding[i].immutable_samplers_offset);
501 for (unsigned j = 0; j < layout->binding[i].array_size; ++j) {
502 memcpy(set->mapped_ptr + offset, samplers[j].descriptor,
503 sizeof(samplers[j].descriptor));
504 offset += layout->binding[i].size / 4;
505 }
506 }
507 }
508
509 *out_set = set;
510 return VK_SUCCESS;
511 }
512
513 static void
tu_descriptor_set_destroy(struct tu_device * device,struct tu_descriptor_pool * pool,struct tu_descriptor_set * set,bool free_bo)514 tu_descriptor_set_destroy(struct tu_device *device,
515 struct tu_descriptor_pool *pool,
516 struct tu_descriptor_set *set,
517 bool free_bo)
518 {
519 assert(!pool->host_memory_base);
520
521 if (free_bo && set->size && !pool->host_memory_base) {
522 uint32_t offset = (uint8_t*)set->mapped_ptr - (uint8_t*)pool->bo.map;
523 for (int i = 0; i < pool->entry_count; ++i) {
524 if (pool->entries[i].offset == offset) {
525 memmove(&pool->entries[i], &pool->entries[i+1],
526 sizeof(pool->entries[i]) * (pool->entry_count - i - 1));
527 --pool->entry_count;
528 break;
529 }
530 }
531 }
532
533 vk_object_free(&device->vk, NULL, set);
534 }
535
536 VkResult
tu_CreateDescriptorPool(VkDevice _device,const VkDescriptorPoolCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkDescriptorPool * pDescriptorPool)537 tu_CreateDescriptorPool(VkDevice _device,
538 const VkDescriptorPoolCreateInfo *pCreateInfo,
539 const VkAllocationCallbacks *pAllocator,
540 VkDescriptorPool *pDescriptorPool)
541 {
542 TU_FROM_HANDLE(tu_device, device, _device);
543 struct tu_descriptor_pool *pool;
544 uint64_t size = sizeof(struct tu_descriptor_pool);
545 uint64_t bo_size = 0, bo_count = 0, dynamic_count = 0;
546 VkResult ret;
547
548 for (unsigned i = 0; i < pCreateInfo->poolSizeCount; ++i) {
549 if (pCreateInfo->pPoolSizes[i].type != VK_DESCRIPTOR_TYPE_SAMPLER)
550 bo_count += pCreateInfo->pPoolSizes[i].descriptorCount;
551
552 switch(pCreateInfo->pPoolSizes[i].type) {
553 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
554 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
555 dynamic_count += pCreateInfo->pPoolSizes[i].descriptorCount;
556 default:
557 break;
558 }
559
560 bo_size += descriptor_size(pCreateInfo->pPoolSizes[i].type) *
561 pCreateInfo->pPoolSizes[i].descriptorCount;
562 }
563
564 if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
565 uint64_t host_size = pCreateInfo->maxSets * sizeof(struct tu_descriptor_set);
566 host_size += sizeof(struct tu_bo*) * bo_count;
567 host_size += A6XX_TEX_CONST_DWORDS * 4 * dynamic_count;
568 size += host_size;
569 } else {
570 size += sizeof(struct tu_descriptor_pool_entry) * pCreateInfo->maxSets;
571 }
572
573 pool = vk_object_zalloc(&device->vk, pAllocator, size,
574 VK_OBJECT_TYPE_DESCRIPTOR_POOL);
575 if (!pool)
576 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
577
578 if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
579 pool->host_memory_base = (uint8_t*)pool + sizeof(struct tu_descriptor_pool);
580 pool->host_memory_ptr = pool->host_memory_base;
581 pool->host_memory_end = (uint8_t*)pool + size;
582 }
583
584 if (bo_size) {
585 ret = tu_bo_init_new(device, &pool->bo, bo_size, true);
586 if (ret)
587 goto fail_alloc;
588
589 ret = tu_bo_map(device, &pool->bo);
590 if (ret)
591 goto fail_map;
592 }
593 pool->size = bo_size;
594 pool->max_entry_count = pCreateInfo->maxSets;
595
596 *pDescriptorPool = tu_descriptor_pool_to_handle(pool);
597 return VK_SUCCESS;
598
599 fail_map:
600 tu_bo_finish(device, &pool->bo);
601 fail_alloc:
602 vk_object_free(&device->vk, pAllocator, pool);
603 return ret;
604 }
605
606 void
tu_DestroyDescriptorPool(VkDevice _device,VkDescriptorPool _pool,const VkAllocationCallbacks * pAllocator)607 tu_DestroyDescriptorPool(VkDevice _device,
608 VkDescriptorPool _pool,
609 const VkAllocationCallbacks *pAllocator)
610 {
611 TU_FROM_HANDLE(tu_device, device, _device);
612 TU_FROM_HANDLE(tu_descriptor_pool, pool, _pool);
613
614 if (!pool)
615 return;
616
617 if (!pool->host_memory_base) {
618 for(int i = 0; i < pool->entry_count; ++i) {
619 tu_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
620 }
621 }
622
623 if (pool->size)
624 tu_bo_finish(device, &pool->bo);
625
626 vk_object_free(&device->vk, pAllocator, pool);
627 }
628
629 VkResult
tu_ResetDescriptorPool(VkDevice _device,VkDescriptorPool descriptorPool,VkDescriptorPoolResetFlags flags)630 tu_ResetDescriptorPool(VkDevice _device,
631 VkDescriptorPool descriptorPool,
632 VkDescriptorPoolResetFlags flags)
633 {
634 TU_FROM_HANDLE(tu_device, device, _device);
635 TU_FROM_HANDLE(tu_descriptor_pool, pool, descriptorPool);
636
637 if (!pool->host_memory_base) {
638 for(int i = 0; i < pool->entry_count; ++i) {
639 tu_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
640 }
641 pool->entry_count = 0;
642 }
643
644 pool->current_offset = 0;
645 pool->host_memory_ptr = pool->host_memory_base;
646
647 return VK_SUCCESS;
648 }
649
650 VkResult
tu_AllocateDescriptorSets(VkDevice _device,const VkDescriptorSetAllocateInfo * pAllocateInfo,VkDescriptorSet * pDescriptorSets)651 tu_AllocateDescriptorSets(VkDevice _device,
652 const VkDescriptorSetAllocateInfo *pAllocateInfo,
653 VkDescriptorSet *pDescriptorSets)
654 {
655 TU_FROM_HANDLE(tu_device, device, _device);
656 TU_FROM_HANDLE(tu_descriptor_pool, pool, pAllocateInfo->descriptorPool);
657
658 VkResult result = VK_SUCCESS;
659 uint32_t i;
660 struct tu_descriptor_set *set = NULL;
661
662 const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT *variable_counts =
663 vk_find_struct_const(pAllocateInfo->pNext, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT);
664 const uint32_t zero = 0;
665
666 /* allocate a set of buffers for each shader to contain descriptors */
667 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
668 TU_FROM_HANDLE(tu_descriptor_set_layout, layout,
669 pAllocateInfo->pSetLayouts[i]);
670
671 const uint32_t *variable_count = NULL;
672 if (variable_counts) {
673 if (i < variable_counts->descriptorSetCount)
674 variable_count = variable_counts->pDescriptorCounts + i;
675 else
676 variable_count = &zero;
677 }
678
679 assert(!(layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
680
681 result = tu_descriptor_set_create(device, pool, layout, variable_count, &set);
682 if (result != VK_SUCCESS)
683 break;
684
685 pDescriptorSets[i] = tu_descriptor_set_to_handle(set);
686 }
687
688 if (result != VK_SUCCESS) {
689 tu_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
690 i, pDescriptorSets);
691 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
692 pDescriptorSets[i] = VK_NULL_HANDLE;
693 }
694 }
695 return result;
696 }
697
698 VkResult
tu_FreeDescriptorSets(VkDevice _device,VkDescriptorPool descriptorPool,uint32_t count,const VkDescriptorSet * pDescriptorSets)699 tu_FreeDescriptorSets(VkDevice _device,
700 VkDescriptorPool descriptorPool,
701 uint32_t count,
702 const VkDescriptorSet *pDescriptorSets)
703 {
704 TU_FROM_HANDLE(tu_device, device, _device);
705 TU_FROM_HANDLE(tu_descriptor_pool, pool, descriptorPool);
706
707 for (uint32_t i = 0; i < count; i++) {
708 TU_FROM_HANDLE(tu_descriptor_set, set, pDescriptorSets[i]);
709
710 if (set && !pool->host_memory_base)
711 tu_descriptor_set_destroy(device, pool, set, true);
712 }
713 return VK_SUCCESS;
714 }
715
716 static void
write_texel_buffer_descriptor(uint32_t * dst,const VkBufferView buffer_view)717 write_texel_buffer_descriptor(uint32_t *dst, const VkBufferView buffer_view)
718 {
719 TU_FROM_HANDLE(tu_buffer_view, view, buffer_view);
720
721 memcpy(dst, view->descriptor, sizeof(view->descriptor));
722 }
723
get_range(struct tu_buffer * buf,VkDeviceSize offset,VkDeviceSize range)724 static uint32_t get_range(struct tu_buffer *buf, VkDeviceSize offset,
725 VkDeviceSize range)
726 {
727 if (range == VK_WHOLE_SIZE) {
728 return buf->size - offset;
729 } else {
730 return range;
731 }
732 }
733
734 static void
write_buffer_descriptor(uint32_t * dst,const VkDescriptorBufferInfo * buffer_info)735 write_buffer_descriptor(uint32_t *dst, const VkDescriptorBufferInfo *buffer_info)
736 {
737 TU_FROM_HANDLE(tu_buffer, buffer, buffer_info->buffer);
738
739 uint64_t va = tu_buffer_iova(buffer) + buffer_info->offset;
740 uint32_t range = get_range(buffer, buffer_info->offset, buffer_info->range);
741 range = ALIGN_POT(range, 4) / 4;
742 dst[0] =
743 A6XX_IBO_0_TILE_MODE(TILE6_LINEAR) | A6XX_IBO_0_FMT(FMT6_32_UINT);
744 dst[1] = range;
745 dst[2] =
746 A6XX_IBO_2_UNK4 | A6XX_IBO_2_TYPE(A6XX_TEX_1D) | A6XX_IBO_2_UNK31;
747 dst[3] = 0;
748 dst[4] = A6XX_IBO_4_BASE_LO(va);
749 dst[5] = A6XX_IBO_5_BASE_HI(va >> 32);
750 for (int i = 6; i < A6XX_TEX_CONST_DWORDS; i++)
751 dst[i] = 0;
752 }
753
754 static void
write_ubo_descriptor(uint32_t * dst,const VkDescriptorBufferInfo * buffer_info)755 write_ubo_descriptor(uint32_t *dst, const VkDescriptorBufferInfo *buffer_info)
756 {
757 TU_FROM_HANDLE(tu_buffer, buffer, buffer_info->buffer);
758
759 uint32_t range = get_range(buffer, buffer_info->offset, buffer_info->range);
760 /* The HW range is in vec4 units */
761 range = ALIGN_POT(range, 16) / 16;
762 uint64_t va = tu_buffer_iova(buffer) + buffer_info->offset;
763 dst[0] = A6XX_UBO_0_BASE_LO(va);
764 dst[1] = A6XX_UBO_1_BASE_HI(va >> 32) | A6XX_UBO_1_SIZE(range);
765 }
766
767 static void
write_image_descriptor(uint32_t * dst,VkDescriptorType descriptor_type,const VkDescriptorImageInfo * image_info)768 write_image_descriptor(uint32_t *dst,
769 VkDescriptorType descriptor_type,
770 const VkDescriptorImageInfo *image_info)
771 {
772 TU_FROM_HANDLE(tu_image_view, iview, image_info->imageView);
773
774 if (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
775 memcpy(dst, iview->storage_descriptor, sizeof(iview->storage_descriptor));
776 } else {
777 memcpy(dst, iview->descriptor, sizeof(iview->descriptor));
778 }
779 }
780
781 static void
write_combined_image_sampler_descriptor(uint32_t * dst,VkDescriptorType descriptor_type,const VkDescriptorImageInfo * image_info,bool has_sampler)782 write_combined_image_sampler_descriptor(uint32_t *dst,
783 VkDescriptorType descriptor_type,
784 const VkDescriptorImageInfo *image_info,
785 bool has_sampler)
786 {
787 TU_FROM_HANDLE(tu_sampler, sampler, image_info->sampler);
788
789 write_image_descriptor(dst, descriptor_type, image_info);
790 /* copy over sampler state */
791 if (has_sampler) {
792 memcpy(dst + A6XX_TEX_CONST_DWORDS, sampler->descriptor, sizeof(sampler->descriptor));
793 }
794 }
795
796 static void
write_sampler_descriptor(uint32_t * dst,const VkDescriptorImageInfo * image_info)797 write_sampler_descriptor(uint32_t *dst, const VkDescriptorImageInfo *image_info)
798 {
799 TU_FROM_HANDLE(tu_sampler, sampler, image_info->sampler);
800
801 memcpy(dst, sampler->descriptor, sizeof(sampler->descriptor));
802 }
803
804 /* note: this is used with immutable samplers in push descriptors */
805 static void
write_sampler_push(uint32_t * dst,const struct tu_sampler * sampler)806 write_sampler_push(uint32_t *dst, const struct tu_sampler *sampler)
807 {
808 memcpy(dst, sampler->descriptor, sizeof(sampler->descriptor));
809 }
810
811 void
tu_update_descriptor_sets(VkDescriptorSet dstSetOverride,uint32_t descriptorWriteCount,const VkWriteDescriptorSet * pDescriptorWrites,uint32_t descriptorCopyCount,const VkCopyDescriptorSet * pDescriptorCopies)812 tu_update_descriptor_sets(VkDescriptorSet dstSetOverride,
813 uint32_t descriptorWriteCount,
814 const VkWriteDescriptorSet *pDescriptorWrites,
815 uint32_t descriptorCopyCount,
816 const VkCopyDescriptorSet *pDescriptorCopies)
817 {
818 uint32_t i, j;
819 for (i = 0; i < descriptorWriteCount; i++) {
820 const VkWriteDescriptorSet *writeset = &pDescriptorWrites[i];
821 TU_FROM_HANDLE(tu_descriptor_set, set, dstSetOverride ?: writeset->dstSet);
822 const struct tu_descriptor_set_binding_layout *binding_layout =
823 set->layout->binding + writeset->dstBinding;
824 uint32_t *ptr = set->mapped_ptr;
825 /* for immutable samplers with push descriptors: */
826 const bool copy_immutable_samplers =
827 dstSetOverride && binding_layout->immutable_samplers_offset;
828 const struct tu_sampler *samplers =
829 tu_immutable_samplers(set->layout, binding_layout);
830
831 ptr += binding_layout->offset / 4;
832
833 ptr += (binding_layout->size / 4) * writeset->dstArrayElement;
834 for (j = 0; j < writeset->descriptorCount; ++j) {
835 switch(writeset->descriptorType) {
836 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: {
837 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
838 unsigned idx = writeset->dstArrayElement + j;
839 idx += binding_layout->dynamic_offset_offset;
840 write_ubo_descriptor(set->dynamic_descriptors + A6XX_TEX_CONST_DWORDS * idx,
841 writeset->pBufferInfo + j);
842 break;
843 }
844 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
845 write_ubo_descriptor(ptr, writeset->pBufferInfo + j);
846 break;
847 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
848 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
849 unsigned idx = writeset->dstArrayElement + j;
850 idx += binding_layout->dynamic_offset_offset;
851 write_buffer_descriptor(set->dynamic_descriptors + A6XX_TEX_CONST_DWORDS * idx,
852 writeset->pBufferInfo + j);
853 break;
854 }
855 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
856 write_buffer_descriptor(ptr, writeset->pBufferInfo + j);
857 break;
858 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
859 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
860 write_texel_buffer_descriptor(ptr, writeset->pTexelBufferView[j]);
861 break;
862 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
863 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
864 write_image_descriptor(ptr, writeset->descriptorType, writeset->pImageInfo + j);
865 break;
866 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
867 write_combined_image_sampler_descriptor(ptr,
868 writeset->descriptorType,
869 writeset->pImageInfo + j,
870 !binding_layout->immutable_samplers_offset);
871
872 if (copy_immutable_samplers)
873 write_sampler_push(ptr + A6XX_TEX_CONST_DWORDS, &samplers[writeset->dstArrayElement + j]);
874 break;
875 case VK_DESCRIPTOR_TYPE_SAMPLER:
876 if (!binding_layout->immutable_samplers_offset)
877 write_sampler_descriptor(ptr, writeset->pImageInfo + j);
878 else if (copy_immutable_samplers)
879 write_sampler_push(ptr, &samplers[writeset->dstArrayElement + j]);
880 break;
881 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
882 /* nothing in descriptor set - framebuffer state is used instead */
883 break;
884 default:
885 unreachable("unimplemented descriptor type");
886 break;
887 }
888 ptr += binding_layout->size / 4;
889 }
890 }
891
892 for (i = 0; i < descriptorCopyCount; i++) {
893 const VkCopyDescriptorSet *copyset = &pDescriptorCopies[i];
894 TU_FROM_HANDLE(tu_descriptor_set, src_set,
895 copyset->srcSet);
896 TU_FROM_HANDLE(tu_descriptor_set, dst_set,
897 copyset->dstSet);
898 const struct tu_descriptor_set_binding_layout *src_binding_layout =
899 src_set->layout->binding + copyset->srcBinding;
900 const struct tu_descriptor_set_binding_layout *dst_binding_layout =
901 dst_set->layout->binding + copyset->dstBinding;
902 uint32_t *src_ptr = src_set->mapped_ptr;
903 uint32_t *dst_ptr = dst_set->mapped_ptr;
904
905 src_ptr += src_binding_layout->offset / 4;
906 dst_ptr += dst_binding_layout->offset / 4;
907
908 src_ptr += src_binding_layout->size * copyset->srcArrayElement / 4;
909 dst_ptr += dst_binding_layout->size * copyset->dstArrayElement / 4;
910
911 for (j = 0; j < copyset->descriptorCount; ++j) {
912 switch (src_binding_layout->type) {
913 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
914 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
915 unsigned src_idx = copyset->srcArrayElement + j;
916 unsigned dst_idx = copyset->dstArrayElement + j;
917 src_idx += src_binding_layout->dynamic_offset_offset;
918 dst_idx += dst_binding_layout->dynamic_offset_offset;
919
920 uint32_t *src_dynamic, *dst_dynamic;
921 src_dynamic = src_set->dynamic_descriptors + src_idx * A6XX_TEX_CONST_DWORDS;
922 dst_dynamic = dst_set->dynamic_descriptors + dst_idx * A6XX_TEX_CONST_DWORDS;
923 memcpy(dst_dynamic, src_dynamic, A6XX_TEX_CONST_DWORDS * 4);
924 break;
925 }
926 default:
927 memcpy(dst_ptr, src_ptr, src_binding_layout->size);
928 }
929
930 src_ptr += src_binding_layout->size / 4;
931 dst_ptr += dst_binding_layout->size / 4;
932 }
933 }
934 }
935
936 void
tu_UpdateDescriptorSets(VkDevice _device,uint32_t descriptorWriteCount,const VkWriteDescriptorSet * pDescriptorWrites,uint32_t descriptorCopyCount,const VkCopyDescriptorSet * pDescriptorCopies)937 tu_UpdateDescriptorSets(VkDevice _device,
938 uint32_t descriptorWriteCount,
939 const VkWriteDescriptorSet *pDescriptorWrites,
940 uint32_t descriptorCopyCount,
941 const VkCopyDescriptorSet *pDescriptorCopies)
942 {
943 tu_update_descriptor_sets(VK_NULL_HANDLE,
944 descriptorWriteCount, pDescriptorWrites,
945 descriptorCopyCount, pDescriptorCopies);
946 }
947
948 VkResult
tu_CreateDescriptorUpdateTemplate(VkDevice _device,const VkDescriptorUpdateTemplateCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkDescriptorUpdateTemplate * pDescriptorUpdateTemplate)949 tu_CreateDescriptorUpdateTemplate(
950 VkDevice _device,
951 const VkDescriptorUpdateTemplateCreateInfo *pCreateInfo,
952 const VkAllocationCallbacks *pAllocator,
953 VkDescriptorUpdateTemplate *pDescriptorUpdateTemplate)
954 {
955 TU_FROM_HANDLE(tu_device, device, _device);
956 TU_FROM_HANDLE(tu_descriptor_set_layout, set_layout,
957 pCreateInfo->descriptorSetLayout);
958 const uint32_t entry_count = pCreateInfo->descriptorUpdateEntryCount;
959 const size_t size =
960 sizeof(struct tu_descriptor_update_template) +
961 sizeof(struct tu_descriptor_update_template_entry) * entry_count;
962 struct tu_descriptor_update_template *templ;
963
964 templ = vk_object_alloc(&device->vk, pAllocator, size,
965 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE);
966 if (!templ)
967 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
968
969 templ->entry_count = entry_count;
970
971 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR) {
972 TU_FROM_HANDLE(tu_pipeline_layout, pipeline_layout, pCreateInfo->pipelineLayout);
973
974 /* descriptorSetLayout should be ignored for push descriptors
975 * and instead it refers to pipelineLayout and set.
976 */
977 assert(pCreateInfo->set < MAX_SETS);
978 set_layout = pipeline_layout->set[pCreateInfo->set].layout;
979
980 templ->bind_point = pCreateInfo->pipelineBindPoint;
981 }
982
983 for (uint32_t i = 0; i < entry_count; i++) {
984 const VkDescriptorUpdateTemplateEntry *entry = &pCreateInfo->pDescriptorUpdateEntries[i];
985
986 const struct tu_descriptor_set_binding_layout *binding_layout =
987 set_layout->binding + entry->dstBinding;
988 uint32_t dst_offset, dst_stride;
989 const struct tu_sampler *immutable_samplers = NULL;
990
991 /* dst_offset is an offset into dynamic_descriptors when the descriptor
992 * is dynamic, and an offset into mapped_ptr otherwise.
993 */
994 switch (entry->descriptorType) {
995 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
996 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
997 dst_offset = (binding_layout->dynamic_offset_offset +
998 entry->dstArrayElement) * A6XX_TEX_CONST_DWORDS;
999 dst_stride = A6XX_TEX_CONST_DWORDS;
1000 break;
1001 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1002 case VK_DESCRIPTOR_TYPE_SAMPLER:
1003 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR &&
1004 binding_layout->immutable_samplers_offset) {
1005 immutable_samplers =
1006 tu_immutable_samplers(set_layout, binding_layout) + entry->dstArrayElement;
1007 }
1008 /* fallthrough */
1009 default:
1010 dst_offset = binding_layout->offset / 4;
1011 dst_offset += (binding_layout->size * entry->dstArrayElement) / 4;
1012 dst_stride = binding_layout->size / 4;
1013 }
1014
1015 templ->entry[i] = (struct tu_descriptor_update_template_entry) {
1016 .descriptor_type = entry->descriptorType,
1017 .descriptor_count = entry->descriptorCount,
1018 .src_offset = entry->offset,
1019 .src_stride = entry->stride,
1020 .dst_offset = dst_offset,
1021 .dst_stride = dst_stride,
1022 .has_sampler = !binding_layout->immutable_samplers_offset,
1023 .immutable_samplers = immutable_samplers,
1024 };
1025 }
1026
1027 *pDescriptorUpdateTemplate =
1028 tu_descriptor_update_template_to_handle(templ);
1029
1030 return VK_SUCCESS;
1031 }
1032
1033 void
tu_DestroyDescriptorUpdateTemplate(VkDevice _device,VkDescriptorUpdateTemplate descriptorUpdateTemplate,const VkAllocationCallbacks * pAllocator)1034 tu_DestroyDescriptorUpdateTemplate(
1035 VkDevice _device,
1036 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1037 const VkAllocationCallbacks *pAllocator)
1038 {
1039 TU_FROM_HANDLE(tu_device, device, _device);
1040 TU_FROM_HANDLE(tu_descriptor_update_template, templ,
1041 descriptorUpdateTemplate);
1042
1043 if (!templ)
1044 return;
1045
1046 vk_object_free(&device->vk, pAllocator, templ);
1047 }
1048
1049 void
tu_update_descriptor_set_with_template(struct tu_descriptor_set * set,VkDescriptorUpdateTemplate descriptorUpdateTemplate,const void * pData)1050 tu_update_descriptor_set_with_template(
1051 struct tu_descriptor_set *set,
1052 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1053 const void *pData)
1054 {
1055 TU_FROM_HANDLE(tu_descriptor_update_template, templ,
1056 descriptorUpdateTemplate);
1057
1058 for (uint32_t i = 0; i < templ->entry_count; i++) {
1059 uint32_t *ptr = set->mapped_ptr;
1060 const void *src = ((const char *) pData) + templ->entry[i].src_offset;
1061 const struct tu_sampler *samplers = templ->entry[i].immutable_samplers;
1062
1063 ptr += templ->entry[i].dst_offset;
1064 unsigned dst_offset = templ->entry[i].dst_offset;
1065 for (unsigned j = 0; j < templ->entry[i].descriptor_count; ++j) {
1066 switch(templ->entry[i].descriptor_type) {
1067 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: {
1068 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
1069 write_ubo_descriptor(set->dynamic_descriptors + dst_offset, src);
1070 break;
1071 }
1072 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1073 write_ubo_descriptor(ptr, src);
1074 break;
1075 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1076 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
1077 write_buffer_descriptor(set->dynamic_descriptors + dst_offset, src);
1078 break;
1079 }
1080 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1081 write_buffer_descriptor(ptr, src);
1082 break;
1083 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1084 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1085 write_texel_buffer_descriptor(ptr, *(VkBufferView *) src);
1086 break;
1087 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1088 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1089 write_image_descriptor(ptr, templ->entry[i].descriptor_type, src);
1090 break;
1091 }
1092 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1093 write_combined_image_sampler_descriptor(ptr,
1094 templ->entry[i].descriptor_type,
1095 src,
1096 templ->entry[i].has_sampler);
1097 if (samplers)
1098 write_sampler_push(ptr + A6XX_TEX_CONST_DWORDS, &samplers[j]);
1099 break;
1100 case VK_DESCRIPTOR_TYPE_SAMPLER:
1101 if (templ->entry[i].has_sampler)
1102 write_sampler_descriptor(ptr, src);
1103 else if (samplers)
1104 write_sampler_push(ptr, &samplers[j]);
1105 break;
1106 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1107 /* nothing in descriptor set - framebuffer state is used instead */
1108 break;
1109 default:
1110 unreachable("unimplemented descriptor type");
1111 break;
1112 }
1113 src = (char *) src + templ->entry[i].src_stride;
1114 ptr += templ->entry[i].dst_stride;
1115 dst_offset += templ->entry[i].dst_stride;
1116 }
1117 }
1118 }
1119
1120 void
tu_UpdateDescriptorSetWithTemplate(VkDevice _device,VkDescriptorSet descriptorSet,VkDescriptorUpdateTemplate descriptorUpdateTemplate,const void * pData)1121 tu_UpdateDescriptorSetWithTemplate(
1122 VkDevice _device,
1123 VkDescriptorSet descriptorSet,
1124 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1125 const void *pData)
1126 {
1127 TU_FROM_HANDLE(tu_descriptor_set, set, descriptorSet);
1128
1129 tu_update_descriptor_set_with_template(set, descriptorUpdateTemplate, pData);
1130 }
1131
1132 VkResult
tu_CreateSamplerYcbcrConversion(VkDevice _device,const VkSamplerYcbcrConversionCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkSamplerYcbcrConversion * pYcbcrConversion)1133 tu_CreateSamplerYcbcrConversion(
1134 VkDevice _device,
1135 const VkSamplerYcbcrConversionCreateInfo *pCreateInfo,
1136 const VkAllocationCallbacks *pAllocator,
1137 VkSamplerYcbcrConversion *pYcbcrConversion)
1138 {
1139 TU_FROM_HANDLE(tu_device, device, _device);
1140 struct tu_sampler_ycbcr_conversion *conversion;
1141
1142 conversion = vk_object_alloc(&device->vk, pAllocator, sizeof(*conversion),
1143 VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION);
1144 if (!conversion)
1145 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1146
1147 conversion->format = pCreateInfo->format;
1148 conversion->ycbcr_model = pCreateInfo->ycbcrModel;
1149 conversion->ycbcr_range = pCreateInfo->ycbcrRange;
1150 conversion->components = pCreateInfo->components;
1151 conversion->chroma_offsets[0] = pCreateInfo->xChromaOffset;
1152 conversion->chroma_offsets[1] = pCreateInfo->yChromaOffset;
1153 conversion->chroma_filter = pCreateInfo->chromaFilter;
1154
1155 *pYcbcrConversion = tu_sampler_ycbcr_conversion_to_handle(conversion);
1156 return VK_SUCCESS;
1157 }
1158
1159 void
tu_DestroySamplerYcbcrConversion(VkDevice _device,VkSamplerYcbcrConversion ycbcrConversion,const VkAllocationCallbacks * pAllocator)1160 tu_DestroySamplerYcbcrConversion(VkDevice _device,
1161 VkSamplerYcbcrConversion ycbcrConversion,
1162 const VkAllocationCallbacks *pAllocator)
1163 {
1164 TU_FROM_HANDLE(tu_device, device, _device);
1165 TU_FROM_HANDLE(tu_sampler_ycbcr_conversion, ycbcr_conversion, ycbcrConversion);
1166
1167 if (!ycbcr_conversion)
1168 return;
1169
1170 vk_object_free(&device->vk, pAllocator, ycbcr_conversion);
1171 }
1172