radv: Fix interactions between variable descriptor count and inline uniform blocks.
[mesa.git] / src / amd / vulkan / radv_descriptor_set.c
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 DEALINGS
22 * IN THE SOFTWARE.
23 */
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "util/mesa-sha1.h"
31 #include "radv_private.h"
32 #include "sid.h"
33 #include "vk_format.h"
34 #include "vk_util.h"
35
36
37 static bool has_equal_immutable_samplers(const VkSampler *samplers, uint32_t count)
38 {
39 if (!samplers)
40 return false;
41 for(uint32_t i = 1; i < count; ++i) {
42 if (memcmp(radv_sampler_from_handle(samplers[0])->state,
43 radv_sampler_from_handle(samplers[i])->state, 16)) {
44 return false;
45 }
46 }
47 return true;
48 }
49
50 static int binding_compare(const void* av, const void *bv)
51 {
52 const VkDescriptorSetLayoutBinding *a = (const VkDescriptorSetLayoutBinding*)av;
53 const VkDescriptorSetLayoutBinding *b = (const VkDescriptorSetLayoutBinding*)bv;
54
55 return (a->binding < b->binding) ? -1 : (a->binding > b->binding) ? 1 : 0;
56 }
57
58 static VkDescriptorSetLayoutBinding *
59 create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings, unsigned count) {
60 VkDescriptorSetLayoutBinding *sorted_bindings = malloc(count * sizeof(VkDescriptorSetLayoutBinding));
61 if (!sorted_bindings)
62 return NULL;
63
64 memcpy(sorted_bindings, bindings, count * sizeof(VkDescriptorSetLayoutBinding));
65
66 qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding), binding_compare);
67
68 return sorted_bindings;
69 }
70
71 VkResult radv_CreateDescriptorSetLayout(
72 VkDevice _device,
73 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
74 const VkAllocationCallbacks* pAllocator,
75 VkDescriptorSetLayout* pSetLayout)
76 {
77 RADV_FROM_HANDLE(radv_device, device, _device);
78 struct radv_descriptor_set_layout *set_layout;
79
80 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
81 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT *variable_flags =
82 vk_find_struct_const(pCreateInfo->pNext, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT);
83
84 uint32_t max_binding = 0;
85 uint32_t immutable_sampler_count = 0;
86 uint32_t ycbcr_sampler_count = 0;
87 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
88 max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
89 if ((pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
90 pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&
91 pCreateInfo->pBindings[j].pImmutableSamplers) {
92 immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
93
94 bool has_ycbcr_sampler = false;
95 for (unsigned i = 0; i < pCreateInfo->pBindings[j].descriptorCount; ++i) {
96 if (radv_sampler_from_handle(pCreateInfo->pBindings[j].pImmutableSamplers[i])->ycbcr_sampler)
97 has_ycbcr_sampler = true;
98 }
99
100 if (has_ycbcr_sampler)
101 ycbcr_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
102 }
103 }
104
105 uint32_t samplers_offset = sizeof(struct radv_descriptor_set_layout) +
106 (max_binding + 1) * sizeof(set_layout->binding[0]);
107 size_t size = samplers_offset + immutable_sampler_count * 4 * sizeof(uint32_t);
108 if (ycbcr_sampler_count > 0) {
109 size += ycbcr_sampler_count * sizeof(struct radv_sampler_ycbcr_conversion) + (max_binding + 1) * sizeof(uint32_t);
110 }
111
112 set_layout = vk_zalloc2(&device->alloc, pAllocator, size, 8,
113 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
114 if (!set_layout)
115 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
116
117 set_layout->flags = pCreateInfo->flags;
118 set_layout->layout_size = size;
119
120 /* We just allocate all the samplers at the end of the struct */
121 uint32_t *samplers = (uint32_t*)&set_layout->binding[max_binding + 1];
122 struct radv_sampler_ycbcr_conversion *ycbcr_samplers = NULL;
123 uint32_t *ycbcr_sampler_offsets = NULL;
124
125 if (ycbcr_sampler_count > 0) {
126 ycbcr_sampler_offsets = samplers + 4 * immutable_sampler_count;
127 set_layout->ycbcr_sampler_offsets_offset = (char*)ycbcr_sampler_offsets - (char*)set_layout;
128 ycbcr_samplers = (struct radv_sampler_ycbcr_conversion *)(ycbcr_sampler_offsets + max_binding + 1);
129 } else
130 set_layout->ycbcr_sampler_offsets_offset = 0;
131
132 VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(pCreateInfo->pBindings,
133 pCreateInfo->bindingCount);
134 if (!bindings) {
135 vk_free2(&device->alloc, pAllocator, set_layout);
136 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
137 }
138
139 set_layout->binding_count = max_binding + 1;
140 set_layout->shader_stages = 0;
141 set_layout->dynamic_shader_stages = 0;
142 set_layout->has_immutable_samplers = false;
143 set_layout->size = 0;
144
145 memset(set_layout->binding, 0, size - sizeof(struct radv_descriptor_set_layout));
146
147 uint32_t buffer_count = 0;
148 uint32_t dynamic_offset_count = 0;
149
150 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
151 const VkDescriptorSetLayoutBinding *binding = bindings + j;
152 uint32_t b = binding->binding;
153 uint32_t alignment;
154 unsigned binding_buffer_count = 0;
155 uint32_t descriptor_count = binding->descriptorCount;
156 bool has_ycbcr_sampler = false;
157
158 /* main image + fmask */
159 uint32_t max_sampled_image_descriptors = 2;
160
161 if (binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER &&
162 binding->pImmutableSamplers) {
163 for (unsigned i = 0; i < binding->descriptorCount; ++i) {
164 struct radv_sampler_ycbcr_conversion *conversion =
165 radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler;
166
167 if (conversion) {
168 has_ycbcr_sampler = true;
169 max_sampled_image_descriptors = MAX2(max_sampled_image_descriptors,
170 vk_format_get_plane_count(conversion->format));
171 }
172 }
173 }
174
175 switch (binding->descriptorType) {
176 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
177 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
178 assert(!(pCreateInfo->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
179 set_layout->binding[b].dynamic_offset_count = 1;
180 set_layout->dynamic_shader_stages |= binding->stageFlags;
181 set_layout->binding[b].size = 0;
182 binding_buffer_count = 1;
183 alignment = 1;
184 break;
185 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
186 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
187 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
188 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
189 set_layout->binding[b].size = 16;
190 binding_buffer_count = 1;
191 alignment = 16;
192 break;
193 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
194 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
195 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
196 /* main descriptor + fmask descriptor */
197 set_layout->binding[b].size = 64;
198 binding_buffer_count = 1;
199 alignment = 32;
200 break;
201 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
202 /* main descriptor + fmask descriptor + sampler */
203 set_layout->binding[b].size = 96;
204 binding_buffer_count = 1;
205 alignment = 32;
206 break;
207 case VK_DESCRIPTOR_TYPE_SAMPLER:
208 set_layout->binding[b].size = 16;
209 alignment = 16;
210 break;
211 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
212 alignment = 16;
213 set_layout->binding[b].size = descriptor_count;
214 descriptor_count = 1;
215 break;
216 default:
217 unreachable("unknown descriptor type\n");
218 break;
219 }
220
221 set_layout->size = align(set_layout->size, alignment);
222 set_layout->binding[b].type = binding->descriptorType;
223 set_layout->binding[b].array_size = descriptor_count;
224 set_layout->binding[b].offset = set_layout->size;
225 set_layout->binding[b].buffer_offset = buffer_count;
226 set_layout->binding[b].dynamic_offset_offset = dynamic_offset_count;
227
228 if (variable_flags && binding->binding < variable_flags->bindingCount &&
229 (variable_flags->pBindingFlags[binding->binding] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT)) {
230 assert(!binding->pImmutableSamplers); /* Terribly ill defined how many samplers are valid */
231 assert(binding->binding == max_binding);
232
233 set_layout->has_variable_descriptors = true;
234 }
235
236 if ((binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
237 binding->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&
238 binding->pImmutableSamplers) {
239 set_layout->binding[b].immutable_samplers_offset = samplers_offset;
240 set_layout->binding[b].immutable_samplers_equal =
241 has_equal_immutable_samplers(binding->pImmutableSamplers, binding->descriptorCount);
242 set_layout->has_immutable_samplers = true;
243
244
245 for (uint32_t i = 0; i < binding->descriptorCount; i++)
246 memcpy(samplers + 4 * i, &radv_sampler_from_handle(binding->pImmutableSamplers[i])->state, 16);
247
248 /* Don't reserve space for the samplers if they're not accessed. */
249 if (set_layout->binding[b].immutable_samplers_equal) {
250 if (binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER &&
251 max_sampled_image_descriptors <= 2)
252 set_layout->binding[b].size -= 32;
253 else if (binding->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER)
254 set_layout->binding[b].size -= 16;
255 }
256 samplers += 4 * binding->descriptorCount;
257 samplers_offset += 4 * sizeof(uint32_t) * binding->descriptorCount;
258
259 if (has_ycbcr_sampler) {
260 ycbcr_sampler_offsets[b] = (const char*)ycbcr_samplers - (const char*)set_layout;
261 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
262 if (radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler)
263 ycbcr_samplers[i] = *radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler;
264 else
265 ycbcr_samplers[i].format = VK_FORMAT_UNDEFINED;
266 }
267 ycbcr_samplers += binding->descriptorCount;
268 }
269 }
270
271 set_layout->size += descriptor_count * set_layout->binding[b].size;
272 buffer_count += descriptor_count * binding_buffer_count;
273 dynamic_offset_count += descriptor_count *
274 set_layout->binding[b].dynamic_offset_count;
275 set_layout->shader_stages |= binding->stageFlags;
276 }
277
278 free(bindings);
279
280 set_layout->buffer_count = buffer_count;
281 set_layout->dynamic_offset_count = dynamic_offset_count;
282
283 *pSetLayout = radv_descriptor_set_layout_to_handle(set_layout);
284
285 return VK_SUCCESS;
286 }
287
288 void radv_DestroyDescriptorSetLayout(
289 VkDevice _device,
290 VkDescriptorSetLayout _set_layout,
291 const VkAllocationCallbacks* pAllocator)
292 {
293 RADV_FROM_HANDLE(radv_device, device, _device);
294 RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout, _set_layout);
295
296 if (!set_layout)
297 return;
298
299 vk_free2(&device->alloc, pAllocator, set_layout);
300 }
301
302 void radv_GetDescriptorSetLayoutSupport(VkDevice device,
303 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
304 VkDescriptorSetLayoutSupport* pSupport)
305 {
306 VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(pCreateInfo->pBindings,
307 pCreateInfo->bindingCount);
308 if (!bindings) {
309 pSupport->supported = false;
310 return;
311 }
312
313 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT *variable_flags =
314 vk_find_struct_const(pCreateInfo->pNext, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT);
315 VkDescriptorSetVariableDescriptorCountLayoutSupportEXT *variable_count =
316 vk_find_struct((void*)pCreateInfo->pNext, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT_EXT);
317 if (variable_count) {
318 variable_count->maxVariableDescriptorCount = 0;
319 }
320
321 bool supported = true;
322 uint64_t size = 0;
323 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
324 const VkDescriptorSetLayoutBinding *binding = bindings + i;
325
326 uint64_t descriptor_size = 0;
327 uint64_t descriptor_alignment = 1;
328 uint32_t descriptor_count = binding->descriptorCount;
329 switch (binding->descriptorType) {
330 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
331 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
332 break;
333 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
334 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
335 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
336 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
337 descriptor_size = 16;
338 descriptor_alignment = 16;
339 break;
340 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
341 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
342 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
343 descriptor_size = 64;
344 descriptor_alignment = 32;
345 break;
346 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
347 if (!has_equal_immutable_samplers(binding->pImmutableSamplers, descriptor_count)) {
348 descriptor_size = 64;
349 } else {
350 descriptor_size = 96;
351 }
352 descriptor_alignment = 32;
353 break;
354 case VK_DESCRIPTOR_TYPE_SAMPLER:
355 if (!has_equal_immutable_samplers(binding->pImmutableSamplers, descriptor_count)) {
356 descriptor_size = 16;
357 descriptor_alignment = 16;
358 }
359 break;
360 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
361 descriptor_alignment = 16;
362 descriptor_size = descriptor_count;
363 descriptor_count = 1;
364 break;
365 default:
366 unreachable("unknown descriptor type\n");
367 break;
368 }
369
370 if (size && !align_u64(size, descriptor_alignment)) {
371 supported = false;
372 }
373 size = align_u64(size, descriptor_alignment);
374
375 uint64_t max_count = INT32_MAX;
376 if (binding->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
377 max_count = INT32_MAX - size;
378 else if (descriptor_size)
379 max_count = (INT32_MAX - size) / descriptor_size;
380
381 if (max_count < descriptor_count) {
382 supported = false;
383 }
384 if (variable_flags && binding->binding <variable_flags->bindingCount && variable_count &&
385 (variable_flags->pBindingFlags[binding->binding] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT)) {
386 variable_count->maxVariableDescriptorCount = MIN2(UINT32_MAX, max_count);
387 }
388 size += descriptor_count * descriptor_size;
389 }
390
391 free(bindings);
392
393 pSupport->supported = supported;
394 }
395
396 /*
397 * Pipeline layouts. These have nothing to do with the pipeline. They are
398 * just multiple descriptor set layouts pasted together.
399 */
400
401 VkResult radv_CreatePipelineLayout(
402 VkDevice _device,
403 const VkPipelineLayoutCreateInfo* pCreateInfo,
404 const VkAllocationCallbacks* pAllocator,
405 VkPipelineLayout* pPipelineLayout)
406 {
407 RADV_FROM_HANDLE(radv_device, device, _device);
408 struct radv_pipeline_layout *layout;
409 struct mesa_sha1 ctx;
410
411 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
412
413 layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
414 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
415 if (layout == NULL)
416 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
417
418 layout->num_sets = pCreateInfo->setLayoutCount;
419
420 unsigned dynamic_offset_count = 0;
421 uint16_t dynamic_shader_stages = 0;
422
423
424 _mesa_sha1_init(&ctx);
425 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
426 RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout,
427 pCreateInfo->pSetLayouts[set]);
428 layout->set[set].layout = set_layout;
429
430 layout->set[set].dynamic_offset_start = dynamic_offset_count;
431 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
432 dynamic_offset_count += set_layout->binding[b].array_size * set_layout->binding[b].dynamic_offset_count;
433 dynamic_shader_stages |= set_layout->dynamic_shader_stages;
434 }
435 _mesa_sha1_update(&ctx, set_layout, set_layout->layout_size);
436 }
437
438 layout->dynamic_offset_count = dynamic_offset_count;
439 layout->dynamic_shader_stages = dynamic_shader_stages;
440 layout->push_constant_size = 0;
441
442 for (unsigned i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
443 const VkPushConstantRange *range = pCreateInfo->pPushConstantRanges + i;
444 layout->push_constant_size = MAX2(layout->push_constant_size,
445 range->offset + range->size);
446 }
447
448 layout->push_constant_size = align(layout->push_constant_size, 16);
449 _mesa_sha1_update(&ctx, &layout->push_constant_size,
450 sizeof(layout->push_constant_size));
451 _mesa_sha1_final(&ctx, layout->sha1);
452 *pPipelineLayout = radv_pipeline_layout_to_handle(layout);
453
454 return VK_SUCCESS;
455 }
456
457 void radv_DestroyPipelineLayout(
458 VkDevice _device,
459 VkPipelineLayout _pipelineLayout,
460 const VkAllocationCallbacks* pAllocator)
461 {
462 RADV_FROM_HANDLE(radv_device, device, _device);
463 RADV_FROM_HANDLE(radv_pipeline_layout, pipeline_layout, _pipelineLayout);
464
465 if (!pipeline_layout)
466 return;
467 vk_free2(&device->alloc, pAllocator, pipeline_layout);
468 }
469
470 #define EMPTY 1
471
472 static VkResult
473 radv_descriptor_set_create(struct radv_device *device,
474 struct radv_descriptor_pool *pool,
475 const struct radv_descriptor_set_layout *layout,
476 const uint32_t *variable_count,
477 struct radv_descriptor_set **out_set)
478 {
479 struct radv_descriptor_set *set;
480 unsigned range_offset = sizeof(struct radv_descriptor_set) +
481 sizeof(struct radeon_winsys_bo *) * layout->buffer_count;
482 unsigned mem_size = range_offset +
483 sizeof(struct radv_descriptor_range) * layout->dynamic_offset_count;
484
485 if (pool->host_memory_base) {
486 if (pool->host_memory_end - pool->host_memory_ptr < mem_size)
487 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
488
489 set = (struct radv_descriptor_set*)pool->host_memory_ptr;
490 pool->host_memory_ptr += mem_size;
491 } else {
492 set = vk_alloc2(&device->alloc, NULL, mem_size, 8,
493 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
494
495 if (!set)
496 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
497 }
498
499 memset(set, 0, mem_size);
500
501 if (layout->dynamic_offset_count) {
502 set->dynamic_descriptors = (struct radv_descriptor_range*)((uint8_t*)set + range_offset);
503 }
504
505 set->layout = layout;
506 uint32_t layout_size = layout->size;
507 if (variable_count) {
508 assert(layout->has_variable_descriptors);
509 uint32_t stride = layout->binding[layout->binding_count - 1].size;
510 if (layout->binding[layout->binding_count - 1].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
511 stride = 1;
512
513 layout_size = layout->binding[layout->binding_count - 1].offset +
514 *variable_count * stride;
515 }
516 layout_size = align_u32(layout_size, 32);
517 if (layout_size) {
518 set->size = layout_size;
519
520 if (!pool->host_memory_base && pool->entry_count == pool->max_entry_count) {
521 vk_free2(&device->alloc, NULL, set);
522 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
523 }
524
525 /* try to allocate linearly first, so that we don't spend
526 * time looking for gaps if the app only allocates &
527 * resets via the pool. */
528 if (pool->current_offset + layout_size <= pool->size) {
529 set->bo = pool->bo;
530 set->mapped_ptr = (uint32_t*)(pool->mapped_ptr + pool->current_offset);
531 set->va = radv_buffer_get_va(set->bo) + pool->current_offset;
532 if (!pool->host_memory_base) {
533 pool->entries[pool->entry_count].offset = pool->current_offset;
534 pool->entries[pool->entry_count].size = layout_size;
535 pool->entries[pool->entry_count].set = set;
536 pool->entry_count++;
537 }
538 pool->current_offset += layout_size;
539 } else if (!pool->host_memory_base) {
540 uint64_t offset = 0;
541 int index;
542
543 for (index = 0; index < pool->entry_count; ++index) {
544 if (pool->entries[index].offset - offset >= layout_size)
545 break;
546 offset = pool->entries[index].offset + pool->entries[index].size;
547 }
548
549 if (pool->size - offset < layout_size) {
550 vk_free2(&device->alloc, NULL, set);
551 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
552 }
553 set->bo = pool->bo;
554 set->mapped_ptr = (uint32_t*)(pool->mapped_ptr + offset);
555 set->va = radv_buffer_get_va(set->bo) + offset;
556 memmove(&pool->entries[index + 1], &pool->entries[index],
557 sizeof(pool->entries[0]) * (pool->entry_count - index));
558 pool->entries[index].offset = offset;
559 pool->entries[index].size = layout_size;
560 pool->entries[index].set = set;
561 pool->entry_count++;
562 } else
563 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
564 }
565
566 if (layout->has_immutable_samplers) {
567 for (unsigned i = 0; i < layout->binding_count; ++i) {
568 if (!layout->binding[i].immutable_samplers_offset ||
569 layout->binding[i].immutable_samplers_equal)
570 continue;
571
572 unsigned offset = layout->binding[i].offset / 4;
573 if (layout->binding[i].type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
574 offset += radv_combined_image_descriptor_sampler_offset(layout->binding + i) / 4;
575
576 const uint32_t *samplers = (const uint32_t*)((const char*)layout + layout->binding[i].immutable_samplers_offset);
577 for (unsigned j = 0; j < layout->binding[i].array_size; ++j) {
578 memcpy(set->mapped_ptr + offset, samplers + 4 * j, 16);
579 offset += layout->binding[i].size / 4;
580 }
581
582 }
583 }
584 *out_set = set;
585 return VK_SUCCESS;
586 }
587
588 static void
589 radv_descriptor_set_destroy(struct radv_device *device,
590 struct radv_descriptor_pool *pool,
591 struct radv_descriptor_set *set,
592 bool free_bo)
593 {
594 assert(!pool->host_memory_base);
595
596 if (free_bo && set->size && !pool->host_memory_base) {
597 uint32_t offset = (uint8_t*)set->mapped_ptr - pool->mapped_ptr;
598 for (int i = 0; i < pool->entry_count; ++i) {
599 if (pool->entries[i].offset == offset) {
600 memmove(&pool->entries[i], &pool->entries[i+1],
601 sizeof(pool->entries[i]) * (pool->entry_count - i - 1));
602 --pool->entry_count;
603 break;
604 }
605 }
606 }
607 vk_free2(&device->alloc, NULL, set);
608 }
609
610 VkResult radv_CreateDescriptorPool(
611 VkDevice _device,
612 const VkDescriptorPoolCreateInfo* pCreateInfo,
613 const VkAllocationCallbacks* pAllocator,
614 VkDescriptorPool* pDescriptorPool)
615 {
616 RADV_FROM_HANDLE(radv_device, device, _device);
617 struct radv_descriptor_pool *pool;
618 uint64_t size = sizeof(struct radv_descriptor_pool);
619 uint64_t bo_size = 0, bo_count = 0, range_count = 0;
620
621 vk_foreach_struct(ext, pCreateInfo->pNext) {
622 switch (ext->sType) {
623 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT: {
624 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT *info =
625 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT*)ext;
626 /* the sizes are 4 aligned, and we need to align to at
627 * most 32, which needs at most 28 bytes extra per
628 * binding. */
629 bo_size += 28llu * info->maxInlineUniformBlockBindings;
630 break;
631 }
632 default:
633 break;
634 }
635 }
636
637 for (unsigned i = 0; i < pCreateInfo->poolSizeCount; ++i) {
638 if (pCreateInfo->pPoolSizes[i].type != VK_DESCRIPTOR_TYPE_SAMPLER)
639 bo_count += pCreateInfo->pPoolSizes[i].descriptorCount;
640
641 switch(pCreateInfo->pPoolSizes[i].type) {
642 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
643 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
644 range_count += pCreateInfo->pPoolSizes[i].descriptorCount;
645 break;
646 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
647 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
648 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
649 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
650 case VK_DESCRIPTOR_TYPE_SAMPLER:
651 /* 32 as we may need to align for images */
652 bo_size += 32 * pCreateInfo->pPoolSizes[i].descriptorCount;
653 break;
654 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
655 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
656 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
657 bo_size += 64 * pCreateInfo->pPoolSizes[i].descriptorCount;
658 break;
659 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
660 bo_size += 96 * pCreateInfo->pPoolSizes[i].descriptorCount;
661 break;
662 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
663 bo_size += pCreateInfo->pPoolSizes[i].descriptorCount;
664 break;
665 default:
666 unreachable("unknown descriptor type\n");
667 break;
668 }
669 }
670
671 if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
672 uint64_t host_size = pCreateInfo->maxSets * sizeof(struct radv_descriptor_set);
673 host_size += sizeof(struct radeon_winsys_bo*) * bo_count;
674 host_size += sizeof(struct radv_descriptor_range) * range_count;
675 size += host_size;
676 } else {
677 size += sizeof(struct radv_descriptor_pool_entry) * pCreateInfo->maxSets;
678 }
679
680 pool = vk_alloc2(&device->alloc, pAllocator, size, 8,
681 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
682 if (!pool)
683 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
684
685 memset(pool, 0, sizeof(*pool));
686
687 if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
688 pool->host_memory_base = (uint8_t*)pool + sizeof(struct radv_descriptor_pool);
689 pool->host_memory_ptr = pool->host_memory_base;
690 pool->host_memory_end = (uint8_t*)pool + size;
691 }
692
693 if (bo_size) {
694 pool->bo = device->ws->buffer_create(device->ws, bo_size, 32,
695 RADEON_DOMAIN_VRAM,
696 RADEON_FLAG_NO_INTERPROCESS_SHARING |
697 RADEON_FLAG_READ_ONLY |
698 RADEON_FLAG_32BIT,
699 RADV_BO_PRIORITY_DESCRIPTOR);
700 pool->mapped_ptr = (uint8_t*)device->ws->buffer_map(pool->bo);
701 }
702 pool->size = bo_size;
703 pool->max_entry_count = pCreateInfo->maxSets;
704
705 *pDescriptorPool = radv_descriptor_pool_to_handle(pool);
706 return VK_SUCCESS;
707 }
708
709 void radv_DestroyDescriptorPool(
710 VkDevice _device,
711 VkDescriptorPool _pool,
712 const VkAllocationCallbacks* pAllocator)
713 {
714 RADV_FROM_HANDLE(radv_device, device, _device);
715 RADV_FROM_HANDLE(radv_descriptor_pool, pool, _pool);
716
717 if (!pool)
718 return;
719
720 if (!pool->host_memory_base) {
721 for(int i = 0; i < pool->entry_count; ++i) {
722 radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
723 }
724 }
725
726 if (pool->bo)
727 device->ws->buffer_destroy(pool->bo);
728 vk_free2(&device->alloc, pAllocator, pool);
729 }
730
731 VkResult radv_ResetDescriptorPool(
732 VkDevice _device,
733 VkDescriptorPool descriptorPool,
734 VkDescriptorPoolResetFlags flags)
735 {
736 RADV_FROM_HANDLE(radv_device, device, _device);
737 RADV_FROM_HANDLE(radv_descriptor_pool, pool, descriptorPool);
738
739 if (!pool->host_memory_base) {
740 for(int i = 0; i < pool->entry_count; ++i) {
741 radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
742 }
743 pool->entry_count = 0;
744 }
745
746 pool->current_offset = 0;
747 pool->host_memory_ptr = pool->host_memory_base;
748
749 return VK_SUCCESS;
750 }
751
752 VkResult radv_AllocateDescriptorSets(
753 VkDevice _device,
754 const VkDescriptorSetAllocateInfo* pAllocateInfo,
755 VkDescriptorSet* pDescriptorSets)
756 {
757 RADV_FROM_HANDLE(radv_device, device, _device);
758 RADV_FROM_HANDLE(radv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
759
760 VkResult result = VK_SUCCESS;
761 uint32_t i;
762 struct radv_descriptor_set *set = NULL;
763
764 const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT *variable_counts =
765 vk_find_struct_const(pAllocateInfo->pNext, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT);
766 const uint32_t zero = 0;
767
768 /* allocate a set of buffers for each shader to contain descriptors */
769 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
770 RADV_FROM_HANDLE(radv_descriptor_set_layout, layout,
771 pAllocateInfo->pSetLayouts[i]);
772
773 const uint32_t *variable_count = NULL;
774 if (variable_counts) {
775 if (i < variable_counts->descriptorSetCount)
776 variable_count = variable_counts->pDescriptorCounts + i;
777 else
778 variable_count = &zero;
779 }
780
781 assert(!(layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
782
783 result = radv_descriptor_set_create(device, pool, layout, variable_count, &set);
784 if (result != VK_SUCCESS)
785 break;
786
787 pDescriptorSets[i] = radv_descriptor_set_to_handle(set);
788 }
789
790 if (result != VK_SUCCESS)
791 radv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
792 i, pDescriptorSets);
793 return result;
794 }
795
796 VkResult radv_FreeDescriptorSets(
797 VkDevice _device,
798 VkDescriptorPool descriptorPool,
799 uint32_t count,
800 const VkDescriptorSet* pDescriptorSets)
801 {
802 RADV_FROM_HANDLE(radv_device, device, _device);
803 RADV_FROM_HANDLE(radv_descriptor_pool, pool, descriptorPool);
804
805 for (uint32_t i = 0; i < count; i++) {
806 RADV_FROM_HANDLE(radv_descriptor_set, set, pDescriptorSets[i]);
807
808 if (set && !pool->host_memory_base)
809 radv_descriptor_set_destroy(device, pool, set, true);
810 }
811 return VK_SUCCESS;
812 }
813
814 static void write_texel_buffer_descriptor(struct radv_device *device,
815 struct radv_cmd_buffer *cmd_buffer,
816 unsigned *dst,
817 struct radeon_winsys_bo **buffer_list,
818 const VkBufferView _buffer_view)
819 {
820 RADV_FROM_HANDLE(radv_buffer_view, buffer_view, _buffer_view);
821
822 memcpy(dst, buffer_view->state, 4 * 4);
823
824 if (cmd_buffer)
825 radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer_view->bo);
826 else
827 *buffer_list = buffer_view->bo;
828 }
829
830 static void write_buffer_descriptor(struct radv_device *device,
831 struct radv_cmd_buffer *cmd_buffer,
832 unsigned *dst,
833 struct radeon_winsys_bo **buffer_list,
834 const VkDescriptorBufferInfo *buffer_info)
835 {
836 RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);
837 uint64_t va = radv_buffer_get_va(buffer->bo);
838 uint32_t range = buffer_info->range;
839
840 if (buffer_info->range == VK_WHOLE_SIZE)
841 range = buffer->size - buffer_info->offset;
842
843 va += buffer_info->offset + buffer->offset;
844 dst[0] = va;
845 dst[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
846 dst[2] = range;
847 dst[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
848 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
849 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
850 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
851 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
852 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
853
854 if (cmd_buffer)
855 radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer->bo);
856 else
857 *buffer_list = buffer->bo;
858 }
859
860 static void write_block_descriptor(struct radv_device *device,
861 struct radv_cmd_buffer *cmd_buffer,
862 void *dst,
863 const VkWriteDescriptorSet *writeset)
864 {
865 const VkWriteDescriptorSetInlineUniformBlockEXT *inline_ub =
866 vk_find_struct_const(writeset->pNext, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT);
867
868 memcpy(dst, inline_ub->pData, inline_ub->dataSize);
869 }
870
871 static void write_dynamic_buffer_descriptor(struct radv_device *device,
872 struct radv_descriptor_range *range,
873 struct radeon_winsys_bo **buffer_list,
874 const VkDescriptorBufferInfo *buffer_info)
875 {
876 RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);
877 uint64_t va = radv_buffer_get_va(buffer->bo);
878 unsigned size = buffer_info->range;
879
880 if (buffer_info->range == VK_WHOLE_SIZE)
881 size = buffer->size - buffer_info->offset;
882
883 va += buffer_info->offset + buffer->offset;
884 range->va = va;
885 range->size = size;
886
887 *buffer_list = buffer->bo;
888 }
889
890 static void
891 write_image_descriptor(struct radv_device *device,
892 struct radv_cmd_buffer *cmd_buffer,
893 unsigned size, unsigned *dst,
894 struct radeon_winsys_bo **buffer_list,
895 VkDescriptorType descriptor_type,
896 const VkDescriptorImageInfo *image_info)
897 {
898 RADV_FROM_HANDLE(radv_image_view, iview, image_info->imageView);
899 union radv_descriptor *descriptor;
900
901 if (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
902 descriptor = &iview->storage_descriptor;
903 } else {
904 descriptor = &iview->descriptor;
905 }
906
907 memcpy(dst, descriptor, size);
908
909 if (cmd_buffer)
910 radv_cs_add_buffer(device->ws, cmd_buffer->cs, iview->bo);
911 else
912 *buffer_list = iview->bo;
913 }
914
915 static void
916 write_combined_image_sampler_descriptor(struct radv_device *device,
917 struct radv_cmd_buffer *cmd_buffer,
918 unsigned sampler_offset,
919 unsigned *dst,
920 struct radeon_winsys_bo **buffer_list,
921 VkDescriptorType descriptor_type,
922 const VkDescriptorImageInfo *image_info,
923 bool has_sampler)
924 {
925 RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);
926
927 write_image_descriptor(device, cmd_buffer, sampler_offset, dst, buffer_list,
928 descriptor_type, image_info);
929 /* copy over sampler state */
930 if (has_sampler) {
931 memcpy(dst + sampler_offset / sizeof(*dst), sampler->state, 16);
932 }
933 }
934
935 static void
936 write_sampler_descriptor(struct radv_device *device,
937 unsigned *dst,
938 const VkDescriptorImageInfo *image_info)
939 {
940 RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);
941
942 memcpy(dst, sampler->state, 16);
943 }
944
945 void radv_update_descriptor_sets(
946 struct radv_device* device,
947 struct radv_cmd_buffer* cmd_buffer,
948 VkDescriptorSet dstSetOverride,
949 uint32_t descriptorWriteCount,
950 const VkWriteDescriptorSet* pDescriptorWrites,
951 uint32_t descriptorCopyCount,
952 const VkCopyDescriptorSet* pDescriptorCopies)
953 {
954 uint32_t i, j;
955 for (i = 0; i < descriptorWriteCount; i++) {
956 const VkWriteDescriptorSet *writeset = &pDescriptorWrites[i];
957 RADV_FROM_HANDLE(radv_descriptor_set, set,
958 dstSetOverride ? dstSetOverride : writeset->dstSet);
959 const struct radv_descriptor_set_binding_layout *binding_layout =
960 set->layout->binding + writeset->dstBinding;
961 uint32_t *ptr = set->mapped_ptr;
962 struct radeon_winsys_bo **buffer_list = set->descriptors;
963 /* Immutable samplers are not copied into push descriptors when they are
964 * allocated, so if we are writing push descriptors we have to copy the
965 * immutable samplers into them now.
966 */
967 const bool copy_immutable_samplers = cmd_buffer &&
968 binding_layout->immutable_samplers_offset && !binding_layout->immutable_samplers_equal;
969 const uint32_t *samplers = radv_immutable_samplers(set->layout, binding_layout);
970
971 ptr += binding_layout->offset / 4;
972
973 if (writeset->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
974 write_block_descriptor(device, cmd_buffer, (uint8_t*)ptr + writeset->dstArrayElement, writeset);
975 continue;
976 }
977
978 ptr += binding_layout->size * writeset->dstArrayElement / 4;
979 buffer_list += binding_layout->buffer_offset;
980 buffer_list += writeset->dstArrayElement;
981 for (j = 0; j < writeset->descriptorCount; ++j) {
982 switch(writeset->descriptorType) {
983 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
984 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
985 unsigned idx = writeset->dstArrayElement + j;
986 idx += binding_layout->dynamic_offset_offset;
987 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
988 write_dynamic_buffer_descriptor(device, set->dynamic_descriptors + idx,
989 buffer_list, writeset->pBufferInfo + j);
990 break;
991 }
992 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
993 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
994 write_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,
995 writeset->pBufferInfo + j);
996 break;
997 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
998 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
999 write_texel_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,
1000 writeset->pTexelBufferView[j]);
1001 break;
1002 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1003 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1004 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1005 write_image_descriptor(device, cmd_buffer, 64, ptr, buffer_list,
1006 writeset->descriptorType,
1007 writeset->pImageInfo + j);
1008 break;
1009 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1010 unsigned sampler_offset = radv_combined_image_descriptor_sampler_offset(binding_layout);
1011 write_combined_image_sampler_descriptor(device, cmd_buffer, sampler_offset,
1012 ptr, buffer_list,
1013 writeset->descriptorType,
1014 writeset->pImageInfo + j,
1015 !binding_layout->immutable_samplers_offset);
1016 if (copy_immutable_samplers) {
1017 const unsigned idx = writeset->dstArrayElement + j;
1018 memcpy((char*)ptr + sampler_offset, samplers + 4 * idx, 16);
1019 }
1020 break;
1021 }
1022 case VK_DESCRIPTOR_TYPE_SAMPLER:
1023 if (!binding_layout->immutable_samplers_offset) {
1024 write_sampler_descriptor(device, ptr,
1025 writeset->pImageInfo + j);
1026 } else if (copy_immutable_samplers) {
1027 unsigned idx = writeset->dstArrayElement + j;
1028 memcpy(ptr, samplers + 4 * idx, 16);
1029 }
1030 break;
1031 default:
1032 unreachable("unimplemented descriptor type");
1033 break;
1034 }
1035 ptr += binding_layout->size / 4;
1036 ++buffer_list;
1037 }
1038
1039 }
1040
1041 for (i = 0; i < descriptorCopyCount; i++) {
1042 const VkCopyDescriptorSet *copyset = &pDescriptorCopies[i];
1043 RADV_FROM_HANDLE(radv_descriptor_set, src_set,
1044 copyset->srcSet);
1045 RADV_FROM_HANDLE(radv_descriptor_set, dst_set,
1046 copyset->dstSet);
1047 const struct radv_descriptor_set_binding_layout *src_binding_layout =
1048 src_set->layout->binding + copyset->srcBinding;
1049 const struct radv_descriptor_set_binding_layout *dst_binding_layout =
1050 dst_set->layout->binding + copyset->dstBinding;
1051 uint32_t *src_ptr = src_set->mapped_ptr;
1052 uint32_t *dst_ptr = dst_set->mapped_ptr;
1053 struct radeon_winsys_bo **src_buffer_list = src_set->descriptors;
1054 struct radeon_winsys_bo **dst_buffer_list = dst_set->descriptors;
1055
1056 src_ptr += src_binding_layout->offset / 4;
1057 dst_ptr += dst_binding_layout->offset / 4;
1058
1059 src_ptr += src_binding_layout->size * copyset->srcArrayElement / 4;
1060 dst_ptr += dst_binding_layout->size * copyset->dstArrayElement / 4;
1061
1062 src_buffer_list += src_binding_layout->buffer_offset;
1063 src_buffer_list += copyset->srcArrayElement;
1064
1065 dst_buffer_list += dst_binding_layout->buffer_offset;
1066 dst_buffer_list += copyset->dstArrayElement;
1067
1068 for (j = 0; j < copyset->descriptorCount; ++j) {
1069 switch (src_binding_layout->type) {
1070 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1071 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1072 unsigned src_idx = copyset->srcArrayElement + j;
1073 unsigned dst_idx = copyset->dstArrayElement + j;
1074 struct radv_descriptor_range *src_range, *dst_range;
1075 src_idx += src_binding_layout->dynamic_offset_offset;
1076 dst_idx += dst_binding_layout->dynamic_offset_offset;
1077
1078 src_range = src_set->dynamic_descriptors + src_idx;
1079 dst_range = dst_set->dynamic_descriptors + dst_idx;
1080 *dst_range = *src_range;
1081 break;
1082 }
1083 default:
1084 memcpy(dst_ptr, src_ptr, src_binding_layout->size);
1085 }
1086 src_ptr += src_binding_layout->size / 4;
1087 dst_ptr += dst_binding_layout->size / 4;
1088
1089 if (src_binding_layout->type != VK_DESCRIPTOR_TYPE_SAMPLER) {
1090 /* Sampler descriptors don't have a buffer list. */
1091 dst_buffer_list[j] = src_buffer_list[j];
1092 }
1093 }
1094 }
1095 }
1096
1097 void radv_UpdateDescriptorSets(
1098 VkDevice _device,
1099 uint32_t descriptorWriteCount,
1100 const VkWriteDescriptorSet* pDescriptorWrites,
1101 uint32_t descriptorCopyCount,
1102 const VkCopyDescriptorSet* pDescriptorCopies)
1103 {
1104 RADV_FROM_HANDLE(radv_device, device, _device);
1105
1106 radv_update_descriptor_sets(device, NULL, VK_NULL_HANDLE, descriptorWriteCount, pDescriptorWrites,
1107 descriptorCopyCount, pDescriptorCopies);
1108 }
1109
1110 VkResult radv_CreateDescriptorUpdateTemplate(VkDevice _device,
1111 const VkDescriptorUpdateTemplateCreateInfo *pCreateInfo,
1112 const VkAllocationCallbacks *pAllocator,
1113 VkDescriptorUpdateTemplate *pDescriptorUpdateTemplate)
1114 {
1115 RADV_FROM_HANDLE(radv_device, device, _device);
1116 RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout, pCreateInfo->descriptorSetLayout);
1117 const uint32_t entry_count = pCreateInfo->descriptorUpdateEntryCount;
1118 const size_t size = sizeof(struct radv_descriptor_update_template) +
1119 sizeof(struct radv_descriptor_update_template_entry) * entry_count;
1120 struct radv_descriptor_update_template *templ;
1121 uint32_t i;
1122
1123 templ = vk_alloc2(&device->alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1124 if (!templ)
1125 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1126
1127 templ->entry_count = entry_count;
1128 templ->bind_point = pCreateInfo->pipelineBindPoint;
1129
1130 for (i = 0; i < entry_count; i++) {
1131 const VkDescriptorUpdateTemplateEntry *entry = &pCreateInfo->pDescriptorUpdateEntries[i];
1132 const struct radv_descriptor_set_binding_layout *binding_layout =
1133 set_layout->binding + entry->dstBinding;
1134 const uint32_t buffer_offset = binding_layout->buffer_offset + entry->dstArrayElement;
1135 const uint32_t *immutable_samplers = NULL;
1136 uint32_t dst_offset;
1137 uint32_t dst_stride;
1138
1139 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1140 is dynamic, and an offset into mapped_ptr otherwise */
1141 switch (entry->descriptorType) {
1142 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1143 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1144 assert(pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET);
1145 dst_offset = binding_layout->dynamic_offset_offset + entry->dstArrayElement;
1146 dst_stride = 0; /* Not used */
1147 break;
1148 default:
1149 switch (entry->descriptorType) {
1150 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1151 case VK_DESCRIPTOR_TYPE_SAMPLER:
1152 /* Immutable samplers are copied into push descriptors when they are pushed */
1153 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR &&
1154 binding_layout->immutable_samplers_offset && !binding_layout->immutable_samplers_equal) {
1155 immutable_samplers = radv_immutable_samplers(set_layout, binding_layout) + entry->dstArrayElement * 4;
1156 }
1157 break;
1158 default:
1159 break;
1160 }
1161 dst_offset = binding_layout->offset / 4;
1162 if (entry->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
1163 dst_offset += entry->dstArrayElement / 4;
1164 else
1165 dst_offset += binding_layout->size * entry->dstArrayElement / 4;
1166
1167 dst_stride = binding_layout->size / 4;
1168 break;
1169 }
1170
1171 templ->entry[i] = (struct radv_descriptor_update_template_entry) {
1172 .descriptor_type = entry->descriptorType,
1173 .descriptor_count = entry->descriptorCount,
1174 .src_offset = entry->offset,
1175 .src_stride = entry->stride,
1176 .dst_offset = dst_offset,
1177 .dst_stride = dst_stride,
1178 .buffer_offset = buffer_offset,
1179 .has_sampler = !binding_layout->immutable_samplers_offset,
1180 .sampler_offset = radv_combined_image_descriptor_sampler_offset(binding_layout),
1181 .immutable_samplers = immutable_samplers
1182 };
1183 }
1184
1185 *pDescriptorUpdateTemplate = radv_descriptor_update_template_to_handle(templ);
1186 return VK_SUCCESS;
1187 }
1188
1189 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device,
1190 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1191 const VkAllocationCallbacks *pAllocator)
1192 {
1193 RADV_FROM_HANDLE(radv_device, device, _device);
1194 RADV_FROM_HANDLE(radv_descriptor_update_template, templ, descriptorUpdateTemplate);
1195
1196 if (!templ)
1197 return;
1198
1199 vk_free2(&device->alloc, pAllocator, templ);
1200 }
1201
1202 void radv_update_descriptor_set_with_template(struct radv_device *device,
1203 struct radv_cmd_buffer *cmd_buffer,
1204 struct radv_descriptor_set *set,
1205 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1206 const void *pData)
1207 {
1208 RADV_FROM_HANDLE(radv_descriptor_update_template, templ, descriptorUpdateTemplate);
1209 uint32_t i;
1210
1211 for (i = 0; i < templ->entry_count; ++i) {
1212 struct radeon_winsys_bo **buffer_list = set->descriptors + templ->entry[i].buffer_offset;
1213 uint32_t *pDst = set->mapped_ptr + templ->entry[i].dst_offset;
1214 const uint8_t *pSrc = ((const uint8_t *) pData) + templ->entry[i].src_offset;
1215 uint32_t j;
1216
1217 if (templ->entry[i].descriptor_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1218 memcpy((uint8_t*)pDst, pSrc, templ->entry[i].descriptor_count);
1219 continue;
1220 }
1221
1222 for (j = 0; j < templ->entry[i].descriptor_count; ++j) {
1223 switch (templ->entry[i].descriptor_type) {
1224 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1225 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1226 const unsigned idx = templ->entry[i].dst_offset + j;
1227 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
1228 write_dynamic_buffer_descriptor(device, set->dynamic_descriptors + idx,
1229 buffer_list, (struct VkDescriptorBufferInfo *) pSrc);
1230 break;
1231 }
1232 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1233 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1234 write_buffer_descriptor(device, cmd_buffer, pDst, buffer_list,
1235 (struct VkDescriptorBufferInfo *) pSrc);
1236 break;
1237 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1238 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1239 write_texel_buffer_descriptor(device, cmd_buffer, pDst, buffer_list,
1240 *(VkBufferView *) pSrc);
1241 break;
1242 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1243 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1244 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1245 write_image_descriptor(device, cmd_buffer, 64, pDst, buffer_list,
1246 templ->entry[i].descriptor_type,
1247 (struct VkDescriptorImageInfo *) pSrc);
1248 break;
1249 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1250 write_combined_image_sampler_descriptor(device, cmd_buffer, templ->entry[i].sampler_offset,
1251 pDst, buffer_list, templ->entry[i].descriptor_type,
1252 (struct VkDescriptorImageInfo *) pSrc,
1253 templ->entry[i].has_sampler);
1254 if (templ->entry[i].immutable_samplers) {
1255 memcpy((char*)pDst + templ->entry[i].sampler_offset, templ->entry[i].immutable_samplers + 4 * j, 16);
1256 }
1257 break;
1258 case VK_DESCRIPTOR_TYPE_SAMPLER:
1259 if (templ->entry[i].has_sampler)
1260 write_sampler_descriptor(device, pDst,
1261 (struct VkDescriptorImageInfo *) pSrc);
1262 else if (templ->entry[i].immutable_samplers)
1263 memcpy(pDst, templ->entry[i].immutable_samplers + 4 * j, 16);
1264 break;
1265 default:
1266 unreachable("unimplemented descriptor type");
1267 break;
1268 }
1269 pSrc += templ->entry[i].src_stride;
1270 pDst += templ->entry[i].dst_stride;
1271 ++buffer_list;
1272 }
1273 }
1274 }
1275
1276 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device,
1277 VkDescriptorSet descriptorSet,
1278 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1279 const void *pData)
1280 {
1281 RADV_FROM_HANDLE(radv_device, device, _device);
1282 RADV_FROM_HANDLE(radv_descriptor_set, set, descriptorSet);
1283
1284 radv_update_descriptor_set_with_template(device, NULL, set, descriptorUpdateTemplate, pData);
1285 }
1286
1287
1288 VkResult radv_CreateSamplerYcbcrConversion(VkDevice _device,
1289 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
1290 const VkAllocationCallbacks* pAllocator,
1291 VkSamplerYcbcrConversion* pYcbcrConversion)
1292 {
1293 RADV_FROM_HANDLE(radv_device, device, _device);
1294 struct radv_sampler_ycbcr_conversion *conversion = NULL;
1295
1296 conversion = vk_zalloc2(&device->alloc, pAllocator, sizeof(*conversion), 8,
1297 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1298
1299 if (conversion == NULL)
1300 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1301
1302 conversion->format = pCreateInfo->format;
1303 conversion->ycbcr_model = pCreateInfo->ycbcrModel;
1304 conversion->ycbcr_range = pCreateInfo->ycbcrRange;
1305 conversion->components = pCreateInfo->components;
1306 conversion->chroma_offsets[0] = pCreateInfo->xChromaOffset;
1307 conversion->chroma_offsets[1] = pCreateInfo->yChromaOffset;
1308 conversion->chroma_filter = pCreateInfo->chromaFilter;
1309
1310 *pYcbcrConversion = radv_sampler_ycbcr_conversion_to_handle(conversion);
1311 return VK_SUCCESS;
1312 }
1313
1314
1315 void radv_DestroySamplerYcbcrConversion(VkDevice _device,
1316 VkSamplerYcbcrConversion ycbcrConversion,
1317 const VkAllocationCallbacks* pAllocator)
1318 {
1319 RADV_FROM_HANDLE(radv_device, device, _device);
1320 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion, ycbcr_conversion, ycbcrConversion);
1321
1322 if (ycbcr_conversion)
1323 vk_free2(&device->alloc, pAllocator, ycbcr_conversion);
1324 }