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