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