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