radv: Only allocate supplied number of descriptors when variable.
[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 layout_size = layout->binding[layout->binding_count - 1].offset +
510 *variable_count * layout->binding[layout->binding_count - 1].size;
511 }
512 layout_size = align_u32(layout_size, 32);
513 if (layout_size) {
514 set->size = layout_size;
515
516 if (!pool->host_memory_base && pool->entry_count == pool->max_entry_count) {
517 vk_free2(&device->alloc, NULL, set);
518 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
519 }
520
521 /* try to allocate linearly first, so that we don't spend
522 * time looking for gaps if the app only allocates &
523 * resets via the pool. */
524 if (pool->current_offset + layout_size <= pool->size) {
525 set->bo = pool->bo;
526 set->mapped_ptr = (uint32_t*)(pool->mapped_ptr + pool->current_offset);
527 set->va = radv_buffer_get_va(set->bo) + pool->current_offset;
528 if (!pool->host_memory_base) {
529 pool->entries[pool->entry_count].offset = pool->current_offset;
530 pool->entries[pool->entry_count].size = layout_size;
531 pool->entries[pool->entry_count].set = set;
532 pool->entry_count++;
533 }
534 pool->current_offset += layout_size;
535 } else if (!pool->host_memory_base) {
536 uint64_t offset = 0;
537 int index;
538
539 for (index = 0; index < pool->entry_count; ++index) {
540 if (pool->entries[index].offset - offset >= layout_size)
541 break;
542 offset = pool->entries[index].offset + pool->entries[index].size;
543 }
544
545 if (pool->size - offset < layout_size) {
546 vk_free2(&device->alloc, NULL, set);
547 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
548 }
549 set->bo = pool->bo;
550 set->mapped_ptr = (uint32_t*)(pool->mapped_ptr + offset);
551 set->va = radv_buffer_get_va(set->bo) + offset;
552 memmove(&pool->entries[index + 1], &pool->entries[index],
553 sizeof(pool->entries[0]) * (pool->entry_count - index));
554 pool->entries[index].offset = offset;
555 pool->entries[index].size = layout_size;
556 pool->entries[index].set = set;
557 pool->entry_count++;
558 } else
559 return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
560 }
561
562 if (layout->has_immutable_samplers) {
563 for (unsigned i = 0; i < layout->binding_count; ++i) {
564 if (!layout->binding[i].immutable_samplers_offset ||
565 layout->binding[i].immutable_samplers_equal)
566 continue;
567
568 unsigned offset = layout->binding[i].offset / 4;
569 if (layout->binding[i].type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
570 offset += radv_combined_image_descriptor_sampler_offset(layout->binding + i) / 4;
571
572 const uint32_t *samplers = (const uint32_t*)((const char*)layout + layout->binding[i].immutable_samplers_offset);
573 for (unsigned j = 0; j < layout->binding[i].array_size; ++j) {
574 memcpy(set->mapped_ptr + offset, samplers + 4 * j, 16);
575 offset += layout->binding[i].size / 4;
576 }
577
578 }
579 }
580 *out_set = set;
581 return VK_SUCCESS;
582 }
583
584 static void
585 radv_descriptor_set_destroy(struct radv_device *device,
586 struct radv_descriptor_pool *pool,
587 struct radv_descriptor_set *set,
588 bool free_bo)
589 {
590 assert(!pool->host_memory_base);
591
592 if (free_bo && set->size && !pool->host_memory_base) {
593 uint32_t offset = (uint8_t*)set->mapped_ptr - pool->mapped_ptr;
594 for (int i = 0; i < pool->entry_count; ++i) {
595 if (pool->entries[i].offset == offset) {
596 memmove(&pool->entries[i], &pool->entries[i+1],
597 sizeof(pool->entries[i]) * (pool->entry_count - i - 1));
598 --pool->entry_count;
599 break;
600 }
601 }
602 }
603 vk_free2(&device->alloc, NULL, set);
604 }
605
606 VkResult radv_CreateDescriptorPool(
607 VkDevice _device,
608 const VkDescriptorPoolCreateInfo* pCreateInfo,
609 const VkAllocationCallbacks* pAllocator,
610 VkDescriptorPool* pDescriptorPool)
611 {
612 RADV_FROM_HANDLE(radv_device, device, _device);
613 struct radv_descriptor_pool *pool;
614 uint64_t size = sizeof(struct radv_descriptor_pool);
615 uint64_t bo_size = 0, bo_count = 0, range_count = 0;
616
617 vk_foreach_struct(ext, pCreateInfo->pNext) {
618 switch (ext->sType) {
619 case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT: {
620 const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT *info =
621 (const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT*)ext;
622 /* the sizes are 4 aligned, and we need to align to at
623 * most 32, which needs at most 28 bytes extra per
624 * binding. */
625 bo_size += 28llu * info->maxInlineUniformBlockBindings;
626 break;
627 }
628 default:
629 break;
630 }
631 }
632
633 for (unsigned i = 0; i < pCreateInfo->poolSizeCount; ++i) {
634 if (pCreateInfo->pPoolSizes[i].type != VK_DESCRIPTOR_TYPE_SAMPLER)
635 bo_count += pCreateInfo->pPoolSizes[i].descriptorCount;
636
637 switch(pCreateInfo->pPoolSizes[i].type) {
638 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
639 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
640 range_count += pCreateInfo->pPoolSizes[i].descriptorCount;
641 break;
642 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
643 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
644 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
645 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
646 case VK_DESCRIPTOR_TYPE_SAMPLER:
647 /* 32 as we may need to align for images */
648 bo_size += 32 * pCreateInfo->pPoolSizes[i].descriptorCount;
649 break;
650 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
651 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
652 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
653 bo_size += 64 * pCreateInfo->pPoolSizes[i].descriptorCount;
654 break;
655 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
656 bo_size += 96 * pCreateInfo->pPoolSizes[i].descriptorCount;
657 break;
658 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
659 bo_size += pCreateInfo->pPoolSizes[i].descriptorCount;
660 break;
661 default:
662 unreachable("unknown descriptor type\n");
663 break;
664 }
665 }
666
667 if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
668 uint64_t host_size = pCreateInfo->maxSets * sizeof(struct radv_descriptor_set);
669 host_size += sizeof(struct radeon_winsys_bo*) * bo_count;
670 host_size += sizeof(struct radv_descriptor_range) * range_count;
671 size += host_size;
672 } else {
673 size += sizeof(struct radv_descriptor_pool_entry) * pCreateInfo->maxSets;
674 }
675
676 pool = vk_alloc2(&device->alloc, pAllocator, size, 8,
677 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
678 if (!pool)
679 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
680
681 memset(pool, 0, sizeof(*pool));
682
683 if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
684 pool->host_memory_base = (uint8_t*)pool + sizeof(struct radv_descriptor_pool);
685 pool->host_memory_ptr = pool->host_memory_base;
686 pool->host_memory_end = (uint8_t*)pool + size;
687 }
688
689 if (bo_size) {
690 pool->bo = device->ws->buffer_create(device->ws, bo_size, 32,
691 RADEON_DOMAIN_VRAM,
692 RADEON_FLAG_NO_INTERPROCESS_SHARING |
693 RADEON_FLAG_READ_ONLY |
694 RADEON_FLAG_32BIT,
695 RADV_BO_PRIORITY_DESCRIPTOR);
696 pool->mapped_ptr = (uint8_t*)device->ws->buffer_map(pool->bo);
697 }
698 pool->size = bo_size;
699 pool->max_entry_count = pCreateInfo->maxSets;
700
701 *pDescriptorPool = radv_descriptor_pool_to_handle(pool);
702 return VK_SUCCESS;
703 }
704
705 void radv_DestroyDescriptorPool(
706 VkDevice _device,
707 VkDescriptorPool _pool,
708 const VkAllocationCallbacks* pAllocator)
709 {
710 RADV_FROM_HANDLE(radv_device, device, _device);
711 RADV_FROM_HANDLE(radv_descriptor_pool, pool, _pool);
712
713 if (!pool)
714 return;
715
716 if (!pool->host_memory_base) {
717 for(int i = 0; i < pool->entry_count; ++i) {
718 radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
719 }
720 }
721
722 if (pool->bo)
723 device->ws->buffer_destroy(pool->bo);
724 vk_free2(&device->alloc, pAllocator, pool);
725 }
726
727 VkResult radv_ResetDescriptorPool(
728 VkDevice _device,
729 VkDescriptorPool descriptorPool,
730 VkDescriptorPoolResetFlags flags)
731 {
732 RADV_FROM_HANDLE(radv_device, device, _device);
733 RADV_FROM_HANDLE(radv_descriptor_pool, pool, descriptorPool);
734
735 if (!pool->host_memory_base) {
736 for(int i = 0; i < pool->entry_count; ++i) {
737 radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
738 }
739 pool->entry_count = 0;
740 }
741
742 pool->current_offset = 0;
743 pool->host_memory_ptr = pool->host_memory_base;
744
745 return VK_SUCCESS;
746 }
747
748 VkResult radv_AllocateDescriptorSets(
749 VkDevice _device,
750 const VkDescriptorSetAllocateInfo* pAllocateInfo,
751 VkDescriptorSet* pDescriptorSets)
752 {
753 RADV_FROM_HANDLE(radv_device, device, _device);
754 RADV_FROM_HANDLE(radv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
755
756 VkResult result = VK_SUCCESS;
757 uint32_t i;
758 struct radv_descriptor_set *set = NULL;
759
760 const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT *variable_counts =
761 vk_find_struct_const(pAllocateInfo->pNext, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT);
762 const uint32_t zero = 0;
763
764 /* allocate a set of buffers for each shader to contain descriptors */
765 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
766 RADV_FROM_HANDLE(radv_descriptor_set_layout, layout,
767 pAllocateInfo->pSetLayouts[i]);
768
769 const uint32_t *variable_count = NULL;
770 if (variable_counts) {
771 if (i < variable_counts->descriptorSetCount)
772 variable_count = variable_counts->pDescriptorCounts + i;
773 else
774 variable_count = &zero;
775 }
776
777 assert(!(layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
778
779 result = radv_descriptor_set_create(device, pool, layout, variable_count, &set);
780 if (result != VK_SUCCESS)
781 break;
782
783 pDescriptorSets[i] = radv_descriptor_set_to_handle(set);
784 }
785
786 if (result != VK_SUCCESS)
787 radv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
788 i, pDescriptorSets);
789 return result;
790 }
791
792 VkResult radv_FreeDescriptorSets(
793 VkDevice _device,
794 VkDescriptorPool descriptorPool,
795 uint32_t count,
796 const VkDescriptorSet* pDescriptorSets)
797 {
798 RADV_FROM_HANDLE(radv_device, device, _device);
799 RADV_FROM_HANDLE(radv_descriptor_pool, pool, descriptorPool);
800
801 for (uint32_t i = 0; i < count; i++) {
802 RADV_FROM_HANDLE(radv_descriptor_set, set, pDescriptorSets[i]);
803
804 if (set && !pool->host_memory_base)
805 radv_descriptor_set_destroy(device, pool, set, true);
806 }
807 return VK_SUCCESS;
808 }
809
810 static void write_texel_buffer_descriptor(struct radv_device *device,
811 struct radv_cmd_buffer *cmd_buffer,
812 unsigned *dst,
813 struct radeon_winsys_bo **buffer_list,
814 const VkBufferView _buffer_view)
815 {
816 RADV_FROM_HANDLE(radv_buffer_view, buffer_view, _buffer_view);
817
818 memcpy(dst, buffer_view->state, 4 * 4);
819
820 if (cmd_buffer)
821 radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer_view->bo);
822 else
823 *buffer_list = buffer_view->bo;
824 }
825
826 static void write_buffer_descriptor(struct radv_device *device,
827 struct radv_cmd_buffer *cmd_buffer,
828 unsigned *dst,
829 struct radeon_winsys_bo **buffer_list,
830 const VkDescriptorBufferInfo *buffer_info)
831 {
832 RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);
833 uint64_t va = radv_buffer_get_va(buffer->bo);
834 uint32_t range = buffer_info->range;
835
836 if (buffer_info->range == VK_WHOLE_SIZE)
837 range = buffer->size - buffer_info->offset;
838
839 va += buffer_info->offset + buffer->offset;
840 dst[0] = va;
841 dst[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
842 dst[2] = range;
843 dst[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
844 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
845 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
846 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
847 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
848 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
849
850 if (cmd_buffer)
851 radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer->bo);
852 else
853 *buffer_list = buffer->bo;
854 }
855
856 static void write_block_descriptor(struct radv_device *device,
857 struct radv_cmd_buffer *cmd_buffer,
858 void *dst,
859 const VkWriteDescriptorSet *writeset)
860 {
861 const VkWriteDescriptorSetInlineUniformBlockEXT *inline_ub =
862 vk_find_struct_const(writeset->pNext, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT);
863
864 memcpy(dst, inline_ub->pData, inline_ub->dataSize);
865 }
866
867 static void write_dynamic_buffer_descriptor(struct radv_device *device,
868 struct radv_descriptor_range *range,
869 struct radeon_winsys_bo **buffer_list,
870 const VkDescriptorBufferInfo *buffer_info)
871 {
872 RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);
873 uint64_t va = radv_buffer_get_va(buffer->bo);
874 unsigned size = buffer_info->range;
875
876 if (buffer_info->range == VK_WHOLE_SIZE)
877 size = buffer->size - buffer_info->offset;
878
879 va += buffer_info->offset + buffer->offset;
880 range->va = va;
881 range->size = size;
882
883 *buffer_list = buffer->bo;
884 }
885
886 static void
887 write_image_descriptor(struct radv_device *device,
888 struct radv_cmd_buffer *cmd_buffer,
889 unsigned size, unsigned *dst,
890 struct radeon_winsys_bo **buffer_list,
891 VkDescriptorType descriptor_type,
892 const VkDescriptorImageInfo *image_info)
893 {
894 RADV_FROM_HANDLE(radv_image_view, iview, image_info->imageView);
895 union radv_descriptor *descriptor;
896
897 if (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
898 descriptor = &iview->storage_descriptor;
899 } else {
900 descriptor = &iview->descriptor;
901 }
902
903 memcpy(dst, descriptor, size);
904
905 if (cmd_buffer)
906 radv_cs_add_buffer(device->ws, cmd_buffer->cs, iview->bo);
907 else
908 *buffer_list = iview->bo;
909 }
910
911 static void
912 write_combined_image_sampler_descriptor(struct radv_device *device,
913 struct radv_cmd_buffer *cmd_buffer,
914 unsigned sampler_offset,
915 unsigned *dst,
916 struct radeon_winsys_bo **buffer_list,
917 VkDescriptorType descriptor_type,
918 const VkDescriptorImageInfo *image_info,
919 bool has_sampler)
920 {
921 RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);
922
923 write_image_descriptor(device, cmd_buffer, sampler_offset, dst, buffer_list,
924 descriptor_type, image_info);
925 /* copy over sampler state */
926 if (has_sampler) {
927 memcpy(dst + sampler_offset / sizeof(*dst), sampler->state, 16);
928 }
929 }
930
931 static void
932 write_sampler_descriptor(struct radv_device *device,
933 unsigned *dst,
934 const VkDescriptorImageInfo *image_info)
935 {
936 RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);
937
938 memcpy(dst, sampler->state, 16);
939 }
940
941 void radv_update_descriptor_sets(
942 struct radv_device* device,
943 struct radv_cmd_buffer* cmd_buffer,
944 VkDescriptorSet dstSetOverride,
945 uint32_t descriptorWriteCount,
946 const VkWriteDescriptorSet* pDescriptorWrites,
947 uint32_t descriptorCopyCount,
948 const VkCopyDescriptorSet* pDescriptorCopies)
949 {
950 uint32_t i, j;
951 for (i = 0; i < descriptorWriteCount; i++) {
952 const VkWriteDescriptorSet *writeset = &pDescriptorWrites[i];
953 RADV_FROM_HANDLE(radv_descriptor_set, set,
954 dstSetOverride ? dstSetOverride : writeset->dstSet);
955 const struct radv_descriptor_set_binding_layout *binding_layout =
956 set->layout->binding + writeset->dstBinding;
957 uint32_t *ptr = set->mapped_ptr;
958 struct radeon_winsys_bo **buffer_list = set->descriptors;
959 /* Immutable samplers are not copied into push descriptors when they are
960 * allocated, so if we are writing push descriptors we have to copy the
961 * immutable samplers into them now.
962 */
963 const bool copy_immutable_samplers = cmd_buffer &&
964 binding_layout->immutable_samplers_offset && !binding_layout->immutable_samplers_equal;
965 const uint32_t *samplers = radv_immutable_samplers(set->layout, binding_layout);
966
967 ptr += binding_layout->offset / 4;
968
969 if (writeset->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
970 write_block_descriptor(device, cmd_buffer, (uint8_t*)ptr + writeset->dstArrayElement, writeset);
971 continue;
972 }
973
974 ptr += binding_layout->size * writeset->dstArrayElement / 4;
975 buffer_list += binding_layout->buffer_offset;
976 buffer_list += writeset->dstArrayElement;
977 for (j = 0; j < writeset->descriptorCount; ++j) {
978 switch(writeset->descriptorType) {
979 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
980 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
981 unsigned idx = writeset->dstArrayElement + j;
982 idx += binding_layout->dynamic_offset_offset;
983 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
984 write_dynamic_buffer_descriptor(device, set->dynamic_descriptors + idx,
985 buffer_list, writeset->pBufferInfo + j);
986 break;
987 }
988 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
989 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
990 write_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,
991 writeset->pBufferInfo + j);
992 break;
993 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
994 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
995 write_texel_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,
996 writeset->pTexelBufferView[j]);
997 break;
998 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
999 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1000 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1001 write_image_descriptor(device, cmd_buffer, 64, ptr, buffer_list,
1002 writeset->descriptorType,
1003 writeset->pImageInfo + j);
1004 break;
1005 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1006 unsigned sampler_offset = radv_combined_image_descriptor_sampler_offset(binding_layout);
1007 write_combined_image_sampler_descriptor(device, cmd_buffer, sampler_offset,
1008 ptr, buffer_list,
1009 writeset->descriptorType,
1010 writeset->pImageInfo + j,
1011 !binding_layout->immutable_samplers_offset);
1012 if (copy_immutable_samplers) {
1013 const unsigned idx = writeset->dstArrayElement + j;
1014 memcpy((char*)ptr + sampler_offset, samplers + 4 * idx, 16);
1015 }
1016 break;
1017 }
1018 case VK_DESCRIPTOR_TYPE_SAMPLER:
1019 if (!binding_layout->immutable_samplers_offset) {
1020 write_sampler_descriptor(device, ptr,
1021 writeset->pImageInfo + j);
1022 } else if (copy_immutable_samplers) {
1023 unsigned idx = writeset->dstArrayElement + j;
1024 memcpy(ptr, samplers + 4 * idx, 16);
1025 }
1026 break;
1027 default:
1028 unreachable("unimplemented descriptor type");
1029 break;
1030 }
1031 ptr += binding_layout->size / 4;
1032 ++buffer_list;
1033 }
1034
1035 }
1036
1037 for (i = 0; i < descriptorCopyCount; i++) {
1038 const VkCopyDescriptorSet *copyset = &pDescriptorCopies[i];
1039 RADV_FROM_HANDLE(radv_descriptor_set, src_set,
1040 copyset->srcSet);
1041 RADV_FROM_HANDLE(radv_descriptor_set, dst_set,
1042 copyset->dstSet);
1043 const struct radv_descriptor_set_binding_layout *src_binding_layout =
1044 src_set->layout->binding + copyset->srcBinding;
1045 const struct radv_descriptor_set_binding_layout *dst_binding_layout =
1046 dst_set->layout->binding + copyset->dstBinding;
1047 uint32_t *src_ptr = src_set->mapped_ptr;
1048 uint32_t *dst_ptr = dst_set->mapped_ptr;
1049 struct radeon_winsys_bo **src_buffer_list = src_set->descriptors;
1050 struct radeon_winsys_bo **dst_buffer_list = dst_set->descriptors;
1051
1052 src_ptr += src_binding_layout->offset / 4;
1053 dst_ptr += dst_binding_layout->offset / 4;
1054
1055 src_ptr += src_binding_layout->size * copyset->srcArrayElement / 4;
1056 dst_ptr += dst_binding_layout->size * copyset->dstArrayElement / 4;
1057
1058 src_buffer_list += src_binding_layout->buffer_offset;
1059 src_buffer_list += copyset->srcArrayElement;
1060
1061 dst_buffer_list += dst_binding_layout->buffer_offset;
1062 dst_buffer_list += copyset->dstArrayElement;
1063
1064 for (j = 0; j < copyset->descriptorCount; ++j) {
1065 switch (src_binding_layout->type) {
1066 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1067 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1068 unsigned src_idx = copyset->srcArrayElement + j;
1069 unsigned dst_idx = copyset->dstArrayElement + j;
1070 struct radv_descriptor_range *src_range, *dst_range;
1071 src_idx += src_binding_layout->dynamic_offset_offset;
1072 dst_idx += dst_binding_layout->dynamic_offset_offset;
1073
1074 src_range = src_set->dynamic_descriptors + src_idx;
1075 dst_range = dst_set->dynamic_descriptors + dst_idx;
1076 *dst_range = *src_range;
1077 break;
1078 }
1079 default:
1080 memcpy(dst_ptr, src_ptr, src_binding_layout->size);
1081 }
1082 src_ptr += src_binding_layout->size / 4;
1083 dst_ptr += dst_binding_layout->size / 4;
1084
1085 if (src_binding_layout->type != VK_DESCRIPTOR_TYPE_SAMPLER) {
1086 /* Sampler descriptors don't have a buffer list. */
1087 dst_buffer_list[j] = src_buffer_list[j];
1088 }
1089 }
1090 }
1091 }
1092
1093 void radv_UpdateDescriptorSets(
1094 VkDevice _device,
1095 uint32_t descriptorWriteCount,
1096 const VkWriteDescriptorSet* pDescriptorWrites,
1097 uint32_t descriptorCopyCount,
1098 const VkCopyDescriptorSet* pDescriptorCopies)
1099 {
1100 RADV_FROM_HANDLE(radv_device, device, _device);
1101
1102 radv_update_descriptor_sets(device, NULL, VK_NULL_HANDLE, descriptorWriteCount, pDescriptorWrites,
1103 descriptorCopyCount, pDescriptorCopies);
1104 }
1105
1106 VkResult radv_CreateDescriptorUpdateTemplate(VkDevice _device,
1107 const VkDescriptorUpdateTemplateCreateInfo *pCreateInfo,
1108 const VkAllocationCallbacks *pAllocator,
1109 VkDescriptorUpdateTemplate *pDescriptorUpdateTemplate)
1110 {
1111 RADV_FROM_HANDLE(radv_device, device, _device);
1112 RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout, pCreateInfo->descriptorSetLayout);
1113 const uint32_t entry_count = pCreateInfo->descriptorUpdateEntryCount;
1114 const size_t size = sizeof(struct radv_descriptor_update_template) +
1115 sizeof(struct radv_descriptor_update_template_entry) * entry_count;
1116 struct radv_descriptor_update_template *templ;
1117 uint32_t i;
1118
1119 templ = vk_alloc2(&device->alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1120 if (!templ)
1121 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1122
1123 templ->entry_count = entry_count;
1124 templ->bind_point = pCreateInfo->pipelineBindPoint;
1125
1126 for (i = 0; i < entry_count; i++) {
1127 const VkDescriptorUpdateTemplateEntry *entry = &pCreateInfo->pDescriptorUpdateEntries[i];
1128 const struct radv_descriptor_set_binding_layout *binding_layout =
1129 set_layout->binding + entry->dstBinding;
1130 const uint32_t buffer_offset = binding_layout->buffer_offset + entry->dstArrayElement;
1131 const uint32_t *immutable_samplers = NULL;
1132 uint32_t dst_offset;
1133 uint32_t dst_stride;
1134
1135 /* dst_offset is an offset into dynamic_descriptors when the descriptor
1136 is dynamic, and an offset into mapped_ptr otherwise */
1137 switch (entry->descriptorType) {
1138 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1139 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1140 assert(pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET);
1141 dst_offset = binding_layout->dynamic_offset_offset + entry->dstArrayElement;
1142 dst_stride = 0; /* Not used */
1143 break;
1144 default:
1145 switch (entry->descriptorType) {
1146 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1147 case VK_DESCRIPTOR_TYPE_SAMPLER:
1148 /* Immutable samplers are copied into push descriptors when they are pushed */
1149 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR &&
1150 binding_layout->immutable_samplers_offset && !binding_layout->immutable_samplers_equal) {
1151 immutable_samplers = radv_immutable_samplers(set_layout, binding_layout) + entry->dstArrayElement * 4;
1152 }
1153 break;
1154 default:
1155 break;
1156 }
1157 dst_offset = binding_layout->offset / 4;
1158 if (entry->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
1159 dst_offset += entry->dstArrayElement / 4;
1160 else
1161 dst_offset += binding_layout->size * entry->dstArrayElement / 4;
1162
1163 dst_stride = binding_layout->size / 4;
1164 break;
1165 }
1166
1167 templ->entry[i] = (struct radv_descriptor_update_template_entry) {
1168 .descriptor_type = entry->descriptorType,
1169 .descriptor_count = entry->descriptorCount,
1170 .src_offset = entry->offset,
1171 .src_stride = entry->stride,
1172 .dst_offset = dst_offset,
1173 .dst_stride = dst_stride,
1174 .buffer_offset = buffer_offset,
1175 .has_sampler = !binding_layout->immutable_samplers_offset,
1176 .sampler_offset = radv_combined_image_descriptor_sampler_offset(binding_layout),
1177 .immutable_samplers = immutable_samplers
1178 };
1179 }
1180
1181 *pDescriptorUpdateTemplate = radv_descriptor_update_template_to_handle(templ);
1182 return VK_SUCCESS;
1183 }
1184
1185 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device,
1186 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1187 const VkAllocationCallbacks *pAllocator)
1188 {
1189 RADV_FROM_HANDLE(radv_device, device, _device);
1190 RADV_FROM_HANDLE(radv_descriptor_update_template, templ, descriptorUpdateTemplate);
1191
1192 if (!templ)
1193 return;
1194
1195 vk_free2(&device->alloc, pAllocator, templ);
1196 }
1197
1198 void radv_update_descriptor_set_with_template(struct radv_device *device,
1199 struct radv_cmd_buffer *cmd_buffer,
1200 struct radv_descriptor_set *set,
1201 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1202 const void *pData)
1203 {
1204 RADV_FROM_HANDLE(radv_descriptor_update_template, templ, descriptorUpdateTemplate);
1205 uint32_t i;
1206
1207 for (i = 0; i < templ->entry_count; ++i) {
1208 struct radeon_winsys_bo **buffer_list = set->descriptors + templ->entry[i].buffer_offset;
1209 uint32_t *pDst = set->mapped_ptr + templ->entry[i].dst_offset;
1210 const uint8_t *pSrc = ((const uint8_t *) pData) + templ->entry[i].src_offset;
1211 uint32_t j;
1212
1213 if (templ->entry[i].descriptor_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1214 memcpy((uint8_t*)pDst, pSrc, templ->entry[i].descriptor_count);
1215 continue;
1216 }
1217
1218 for (j = 0; j < templ->entry[i].descriptor_count; ++j) {
1219 switch (templ->entry[i].descriptor_type) {
1220 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1221 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1222 const unsigned idx = templ->entry[i].dst_offset + j;
1223 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
1224 write_dynamic_buffer_descriptor(device, set->dynamic_descriptors + idx,
1225 buffer_list, (struct VkDescriptorBufferInfo *) pSrc);
1226 break;
1227 }
1228 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1229 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1230 write_buffer_descriptor(device, cmd_buffer, pDst, buffer_list,
1231 (struct VkDescriptorBufferInfo *) pSrc);
1232 break;
1233 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1234 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1235 write_texel_buffer_descriptor(device, cmd_buffer, pDst, buffer_list,
1236 *(VkBufferView *) pSrc);
1237 break;
1238 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1239 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1240 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1241 write_image_descriptor(device, cmd_buffer, 64, pDst, buffer_list,
1242 templ->entry[i].descriptor_type,
1243 (struct VkDescriptorImageInfo *) pSrc);
1244 break;
1245 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1246 write_combined_image_sampler_descriptor(device, cmd_buffer, templ->entry[i].sampler_offset,
1247 pDst, buffer_list, templ->entry[i].descriptor_type,
1248 (struct VkDescriptorImageInfo *) pSrc,
1249 templ->entry[i].has_sampler);
1250 if (templ->entry[i].immutable_samplers) {
1251 memcpy((char*)pDst + templ->entry[i].sampler_offset, templ->entry[i].immutable_samplers + 4 * j, 16);
1252 }
1253 break;
1254 case VK_DESCRIPTOR_TYPE_SAMPLER:
1255 if (templ->entry[i].has_sampler)
1256 write_sampler_descriptor(device, pDst,
1257 (struct VkDescriptorImageInfo *) pSrc);
1258 else if (templ->entry[i].immutable_samplers)
1259 memcpy(pDst, templ->entry[i].immutable_samplers + 4 * j, 16);
1260 break;
1261 default:
1262 unreachable("unimplemented descriptor type");
1263 break;
1264 }
1265 pSrc += templ->entry[i].src_stride;
1266 pDst += templ->entry[i].dst_stride;
1267 ++buffer_list;
1268 }
1269 }
1270 }
1271
1272 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device,
1273 VkDescriptorSet descriptorSet,
1274 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1275 const void *pData)
1276 {
1277 RADV_FROM_HANDLE(radv_device, device, _device);
1278 RADV_FROM_HANDLE(radv_descriptor_set, set, descriptorSet);
1279
1280 radv_update_descriptor_set_with_template(device, NULL, set, descriptorUpdateTemplate, pData);
1281 }
1282
1283
1284 VkResult radv_CreateSamplerYcbcrConversion(VkDevice _device,
1285 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
1286 const VkAllocationCallbacks* pAllocator,
1287 VkSamplerYcbcrConversion* pYcbcrConversion)
1288 {
1289 RADV_FROM_HANDLE(radv_device, device, _device);
1290 struct radv_sampler_ycbcr_conversion *conversion = NULL;
1291
1292 conversion = vk_zalloc2(&device->alloc, pAllocator, sizeof(*conversion), 8,
1293 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1294
1295 if (conversion == NULL)
1296 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1297
1298 conversion->format = pCreateInfo->format;
1299 conversion->ycbcr_model = pCreateInfo->ycbcrModel;
1300 conversion->ycbcr_range = pCreateInfo->ycbcrRange;
1301 conversion->components = pCreateInfo->components;
1302 conversion->chroma_offsets[0] = pCreateInfo->xChromaOffset;
1303 conversion->chroma_offsets[1] = pCreateInfo->yChromaOffset;
1304 conversion->chroma_filter = pCreateInfo->chromaFilter;
1305
1306 *pYcbcrConversion = radv_sampler_ycbcr_conversion_to_handle(conversion);
1307 return VK_SUCCESS;
1308 }
1309
1310
1311 void radv_DestroySamplerYcbcrConversion(VkDevice _device,
1312 VkSamplerYcbcrConversion ycbcrConversion,
1313 const VkAllocationCallbacks* pAllocator)
1314 {
1315 RADV_FROM_HANDLE(radv_device, device, _device);
1316 RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion, ycbcr_conversion, ycbcrConversion);
1317
1318 if (ycbcr_conversion)
1319 vk_free2(&device->alloc, pAllocator, ycbcr_conversion);
1320 }