radv: wrap cs_add_buffer in an inline. (v2)
[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
34 VkResult radv_CreateDescriptorSetLayout(
35 VkDevice _device,
36 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
37 const VkAllocationCallbacks* pAllocator,
38 VkDescriptorSetLayout* pSetLayout)
39 {
40 RADV_FROM_HANDLE(radv_device, device, _device);
41 struct radv_descriptor_set_layout *set_layout;
42
43 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
44
45 uint32_t max_binding = 0;
46 uint32_t immutable_sampler_count = 0;
47 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
48 max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
49 if (pCreateInfo->pBindings[j].pImmutableSamplers)
50 immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
51 }
52
53 uint32_t samplers_offset = sizeof(struct radv_descriptor_set_layout) +
54 (max_binding + 1) * sizeof(set_layout->binding[0]);
55 size_t size = samplers_offset + immutable_sampler_count * 4 * sizeof(uint32_t);
56
57 set_layout = vk_alloc2(&device->alloc, pAllocator, size, 8,
58 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
59 if (!set_layout)
60 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
61
62 set_layout->flags = pCreateInfo->flags;
63
64 /* We just allocate all the samplers at the end of the struct */
65 uint32_t *samplers = (uint32_t*)&set_layout->binding[max_binding + 1];
66
67 set_layout->binding_count = max_binding + 1;
68 set_layout->shader_stages = 0;
69 set_layout->dynamic_shader_stages = 0;
70 set_layout->has_immutable_samplers = false;
71 set_layout->size = 0;
72
73 memset(set_layout->binding, 0, size - sizeof(struct radv_descriptor_set_layout));
74
75 uint32_t buffer_count = 0;
76 uint32_t dynamic_offset_count = 0;
77
78 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
79 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
80 uint32_t b = binding->binding;
81 uint32_t alignment;
82 unsigned binding_buffer_count = 0;
83
84 switch (binding->descriptorType) {
85 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
86 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
87 assert(!(pCreateInfo->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
88 set_layout->binding[b].dynamic_offset_count = 1;
89 set_layout->dynamic_shader_stages |= binding->stageFlags;
90 set_layout->binding[b].size = 0;
91 binding_buffer_count = 1;
92 alignment = 1;
93 break;
94 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
95 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
96 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
97 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
98 set_layout->binding[b].size = 16;
99 binding_buffer_count = 1;
100 alignment = 16;
101 break;
102 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
103 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
104 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
105 /* main descriptor + fmask descriptor */
106 set_layout->binding[b].size = 64;
107 binding_buffer_count = 1;
108 alignment = 32;
109 break;
110 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
111 /* main descriptor + fmask descriptor + sampler */
112 set_layout->binding[b].size = 96;
113 binding_buffer_count = 1;
114 alignment = 32;
115 break;
116 case VK_DESCRIPTOR_TYPE_SAMPLER:
117 set_layout->binding[b].size = 16;
118 alignment = 16;
119 break;
120 default:
121 unreachable("unknown descriptor type\n");
122 break;
123 }
124
125 set_layout->size = align(set_layout->size, alignment);
126 assert(binding->descriptorCount > 0);
127 set_layout->binding[b].type = binding->descriptorType;
128 set_layout->binding[b].array_size = binding->descriptorCount;
129 set_layout->binding[b].offset = set_layout->size;
130 set_layout->binding[b].buffer_offset = buffer_count;
131 set_layout->binding[b].dynamic_offset_offset = dynamic_offset_count;
132
133 if (binding->pImmutableSamplers) {
134 set_layout->binding[b].immutable_samplers_offset = samplers_offset;
135 set_layout->binding[b].immutable_samplers_equal = true;
136 set_layout->has_immutable_samplers = true;
137
138
139 for (uint32_t i = 0; i < binding->descriptorCount; i++)
140 memcpy(samplers + 4 * i, &radv_sampler_from_handle(binding->pImmutableSamplers[i])->state, 16);
141 for (uint32_t i = 1; i < binding->descriptorCount; i++)
142 if (memcmp(samplers + 4 * i, samplers, 16) != 0)
143 set_layout->binding[b].immutable_samplers_equal = false;
144
145 /* Don't reserve space for the samplers if they're not accessed. */
146 if (set_layout->binding[b].immutable_samplers_equal) {
147 if (binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
148 set_layout->binding[b].size -= 32;
149 else if (binding->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER)
150 set_layout->binding[b].size -= 16;
151 }
152 samplers += 4 * binding->descriptorCount;
153 samplers_offset += 4 * sizeof(uint32_t) * binding->descriptorCount;
154 }
155
156 set_layout->size += binding->descriptorCount * set_layout->binding[b].size;
157 buffer_count += binding->descriptorCount * binding_buffer_count;
158 dynamic_offset_count += binding->descriptorCount *
159 set_layout->binding[b].dynamic_offset_count;
160 set_layout->shader_stages |= binding->stageFlags;
161 }
162
163 set_layout->buffer_count = buffer_count;
164 set_layout->dynamic_offset_count = dynamic_offset_count;
165
166 *pSetLayout = radv_descriptor_set_layout_to_handle(set_layout);
167
168 return VK_SUCCESS;
169 }
170
171 void radv_DestroyDescriptorSetLayout(
172 VkDevice _device,
173 VkDescriptorSetLayout _set_layout,
174 const VkAllocationCallbacks* pAllocator)
175 {
176 RADV_FROM_HANDLE(radv_device, device, _device);
177 RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout, _set_layout);
178
179 if (!set_layout)
180 return;
181
182 vk_free2(&device->alloc, pAllocator, set_layout);
183 }
184
185 /*
186 * Pipeline layouts. These have nothing to do with the pipeline. They are
187 * just muttiple descriptor set layouts pasted together
188 */
189
190 VkResult radv_CreatePipelineLayout(
191 VkDevice _device,
192 const VkPipelineLayoutCreateInfo* pCreateInfo,
193 const VkAllocationCallbacks* pAllocator,
194 VkPipelineLayout* pPipelineLayout)
195 {
196 RADV_FROM_HANDLE(radv_device, device, _device);
197 struct radv_pipeline_layout *layout;
198 struct mesa_sha1 ctx;
199
200 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
201
202 layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
203 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
204 if (layout == NULL)
205 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
206
207 layout->num_sets = pCreateInfo->setLayoutCount;
208
209 unsigned dynamic_offset_count = 0;
210
211
212 _mesa_sha1_init(&ctx);
213 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
214 RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout,
215 pCreateInfo->pSetLayouts[set]);
216 layout->set[set].layout = set_layout;
217
218 layout->set[set].dynamic_offset_start = dynamic_offset_count;
219 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
220 dynamic_offset_count += set_layout->binding[b].array_size * set_layout->binding[b].dynamic_offset_count;
221 if (set_layout->binding[b].immutable_samplers_offset)
222 _mesa_sha1_update(&ctx, radv_immutable_samplers(set_layout, set_layout->binding + b),
223 set_layout->binding[b].array_size * 4 * sizeof(uint32_t));
224 }
225 _mesa_sha1_update(&ctx, set_layout->binding,
226 sizeof(set_layout->binding[0]) * set_layout->binding_count);
227 }
228
229 layout->dynamic_offset_count = dynamic_offset_count;
230 layout->push_constant_size = 0;
231 for (unsigned i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
232 const VkPushConstantRange *range = pCreateInfo->pPushConstantRanges + i;
233 layout->push_constant_size = MAX2(layout->push_constant_size,
234 range->offset + range->size);
235 }
236
237 layout->push_constant_size = align(layout->push_constant_size, 16);
238 _mesa_sha1_update(&ctx, &layout->push_constant_size,
239 sizeof(layout->push_constant_size));
240 _mesa_sha1_final(&ctx, layout->sha1);
241 *pPipelineLayout = radv_pipeline_layout_to_handle(layout);
242
243 return VK_SUCCESS;
244 }
245
246 void radv_DestroyPipelineLayout(
247 VkDevice _device,
248 VkPipelineLayout _pipelineLayout,
249 const VkAllocationCallbacks* pAllocator)
250 {
251 RADV_FROM_HANDLE(radv_device, device, _device);
252 RADV_FROM_HANDLE(radv_pipeline_layout, pipeline_layout, _pipelineLayout);
253
254 if (!pipeline_layout)
255 return;
256 vk_free2(&device->alloc, pAllocator, pipeline_layout);
257 }
258
259 #define EMPTY 1
260
261 static VkResult
262 radv_descriptor_set_create(struct radv_device *device,
263 struct radv_descriptor_pool *pool,
264 const struct radv_descriptor_set_layout *layout,
265 struct radv_descriptor_set **out_set)
266 {
267 struct radv_descriptor_set *set;
268 unsigned range_offset = sizeof(struct radv_descriptor_set) +
269 sizeof(struct radeon_winsys_bo *) * layout->buffer_count;
270 unsigned mem_size = range_offset +
271 sizeof(struct radv_descriptor_range) * layout->dynamic_offset_count;
272
273 if (pool->host_memory_base) {
274 if (pool->host_memory_end - pool->host_memory_ptr < mem_size)
275 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR);
276
277 set = (struct radv_descriptor_set*)pool->host_memory_ptr;
278 pool->host_memory_ptr += mem_size;
279 } else {
280 set = vk_alloc2(&device->alloc, NULL, mem_size, 8,
281 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
282
283 if (!set)
284 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
285 }
286
287 memset(set, 0, mem_size);
288
289 if (layout->dynamic_offset_count) {
290 set->dynamic_descriptors = (struct radv_descriptor_range*)((uint8_t*)set + range_offset);
291 }
292
293 set->layout = layout;
294 if (layout->size) {
295 uint32_t layout_size = align_u32(layout->size, 32);
296 set->size = layout->size;
297
298 if (!pool->host_memory_base && pool->entry_count == pool->max_entry_count) {
299 vk_free2(&device->alloc, NULL, set);
300 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR);
301 }
302
303 /* try to allocate linearly first, so that we don't spend
304 * time looking for gaps if the app only allocates &
305 * resets via the pool. */
306 if (pool->current_offset + layout_size <= pool->size) {
307 set->bo = pool->bo;
308 set->mapped_ptr = (uint32_t*)(pool->mapped_ptr + pool->current_offset);
309 set->va = radv_buffer_get_va(set->bo) + pool->current_offset;
310 if (!pool->host_memory_base) {
311 pool->entries[pool->entry_count].offset = pool->current_offset;
312 pool->entries[pool->entry_count].size = layout_size;
313 pool->entries[pool->entry_count].set = set;
314 pool->entry_count++;
315 }
316 pool->current_offset += layout_size;
317 } else if (!pool->host_memory_base) {
318 uint64_t offset = 0;
319 int index;
320
321 for (index = 0; index < pool->entry_count; ++index) {
322 if (pool->entries[index].offset - offset >= layout_size)
323 break;
324 offset = pool->entries[index].offset + pool->entries[index].size;
325 }
326
327 if (pool->size - offset < layout_size) {
328 vk_free2(&device->alloc, NULL, set);
329 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR);
330 }
331 set->bo = pool->bo;
332 set->mapped_ptr = (uint32_t*)(pool->mapped_ptr + offset);
333 set->va = radv_buffer_get_va(set->bo) + offset;
334 memmove(&pool->entries[index + 1], &pool->entries[index],
335 sizeof(pool->entries[0]) * (pool->entry_count - index));
336 pool->entries[index].offset = offset;
337 pool->entries[index].size = layout_size;
338 pool->entries[index].set = set;
339 pool->entry_count++;
340 } else
341 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR);
342 }
343
344 if (layout->has_immutable_samplers) {
345 for (unsigned i = 0; i < layout->binding_count; ++i) {
346 if (!layout->binding[i].immutable_samplers_offset ||
347 layout->binding[i].immutable_samplers_equal)
348 continue;
349
350 unsigned offset = layout->binding[i].offset / 4;
351 if (layout->binding[i].type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
352 offset += 16;
353
354 const uint32_t *samplers = (const uint32_t*)((const char*)layout + layout->binding[i].immutable_samplers_offset);
355 for (unsigned j = 0; j < layout->binding[i].array_size; ++j) {
356 memcpy(set->mapped_ptr + offset, samplers + 4 * j, 16);
357 offset += layout->binding[i].size / 4;
358 }
359
360 }
361 }
362 *out_set = set;
363 return VK_SUCCESS;
364 }
365
366 static void
367 radv_descriptor_set_destroy(struct radv_device *device,
368 struct radv_descriptor_pool *pool,
369 struct radv_descriptor_set *set,
370 bool free_bo)
371 {
372 assert(!pool->host_memory_base);
373
374 if (free_bo && set->size && !pool->host_memory_base) {
375 uint32_t offset = (uint8_t*)set->mapped_ptr - pool->mapped_ptr;
376 for (int i = 0; i < pool->entry_count; ++i) {
377 if (pool->entries[i].offset == offset) {
378 memmove(&pool->entries[i], &pool->entries[i+1],
379 sizeof(pool->entries[i]) * (pool->entry_count - i - 1));
380 --pool->entry_count;
381 break;
382 }
383 }
384 }
385 vk_free2(&device->alloc, NULL, set);
386 }
387
388 VkResult radv_CreateDescriptorPool(
389 VkDevice _device,
390 const VkDescriptorPoolCreateInfo* pCreateInfo,
391 const VkAllocationCallbacks* pAllocator,
392 VkDescriptorPool* pDescriptorPool)
393 {
394 RADV_FROM_HANDLE(radv_device, device, _device);
395 struct radv_descriptor_pool *pool;
396 int size = sizeof(struct radv_descriptor_pool);
397 uint64_t bo_size = 0, bo_count = 0, range_count = 0;
398
399
400 for (unsigned i = 0; i < pCreateInfo->poolSizeCount; ++i) {
401 if (pCreateInfo->pPoolSizes[i].type != VK_DESCRIPTOR_TYPE_SAMPLER)
402 bo_count += pCreateInfo->pPoolSizes[i].descriptorCount;
403
404 switch(pCreateInfo->pPoolSizes[i].type) {
405 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
406 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
407 range_count += pCreateInfo->pPoolSizes[i].descriptorCount;
408 break;
409 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
410 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
411 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
412 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
413 case VK_DESCRIPTOR_TYPE_SAMPLER:
414 /* 32 as we may need to align for images */
415 bo_size += 32 * pCreateInfo->pPoolSizes[i].descriptorCount;
416 break;
417 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
418 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
419 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
420 bo_size += 64 * pCreateInfo->pPoolSizes[i].descriptorCount;
421 break;
422 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
423 bo_size += 96 * pCreateInfo->pPoolSizes[i].descriptorCount;
424 break;
425 default:
426 unreachable("unknown descriptor type\n");
427 break;
428 }
429 }
430
431 if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
432 uint64_t host_size = pCreateInfo->maxSets * sizeof(struct radv_descriptor_set);
433 host_size += sizeof(struct radeon_winsys_bo*) * bo_count;
434 host_size += sizeof(struct radv_descriptor_range) * range_count;
435 size += host_size;
436 } else {
437 size += sizeof(struct radv_descriptor_pool_entry) * pCreateInfo->maxSets;
438 }
439
440 pool = vk_alloc2(&device->alloc, pAllocator, size, 8,
441 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
442 if (!pool)
443 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
444
445 memset(pool, 0, sizeof(*pool));
446
447 if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
448 pool->host_memory_base = (uint8_t*)pool + sizeof(struct radv_descriptor_pool);
449 pool->host_memory_ptr = pool->host_memory_base;
450 pool->host_memory_end = (uint8_t*)pool + size;
451 }
452
453 if (bo_size) {
454 pool->bo = device->ws->buffer_create(device->ws, bo_size,
455 32, RADEON_DOMAIN_VRAM, RADEON_FLAG_NO_INTERPROCESS_SHARING);
456 pool->mapped_ptr = (uint8_t*)device->ws->buffer_map(pool->bo);
457 }
458 pool->size = bo_size;
459 pool->max_entry_count = pCreateInfo->maxSets;
460
461 *pDescriptorPool = radv_descriptor_pool_to_handle(pool);
462 return VK_SUCCESS;
463 }
464
465 void radv_DestroyDescriptorPool(
466 VkDevice _device,
467 VkDescriptorPool _pool,
468 const VkAllocationCallbacks* pAllocator)
469 {
470 RADV_FROM_HANDLE(radv_device, device, _device);
471 RADV_FROM_HANDLE(radv_descriptor_pool, pool, _pool);
472
473 if (!pool)
474 return;
475
476 if (!pool->host_memory_base) {
477 for(int i = 0; i < pool->entry_count; ++i) {
478 radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
479 }
480 }
481
482 if (pool->bo)
483 device->ws->buffer_destroy(pool->bo);
484 vk_free2(&device->alloc, pAllocator, pool);
485 }
486
487 VkResult radv_ResetDescriptorPool(
488 VkDevice _device,
489 VkDescriptorPool descriptorPool,
490 VkDescriptorPoolResetFlags flags)
491 {
492 RADV_FROM_HANDLE(radv_device, device, _device);
493 RADV_FROM_HANDLE(radv_descriptor_pool, pool, descriptorPool);
494
495 if (!pool->host_memory_base) {
496 for(int i = 0; i < pool->entry_count; ++i) {
497 radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
498 }
499 pool->entry_count = 0;
500 }
501
502 pool->current_offset = 0;
503 pool->host_memory_ptr = pool->host_memory_base;
504
505 return VK_SUCCESS;
506 }
507
508 VkResult radv_AllocateDescriptorSets(
509 VkDevice _device,
510 const VkDescriptorSetAllocateInfo* pAllocateInfo,
511 VkDescriptorSet* pDescriptorSets)
512 {
513 RADV_FROM_HANDLE(radv_device, device, _device);
514 RADV_FROM_HANDLE(radv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
515
516 VkResult result = VK_SUCCESS;
517 uint32_t i;
518 struct radv_descriptor_set *set;
519
520 /* allocate a set of buffers for each shader to contain descriptors */
521 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
522 RADV_FROM_HANDLE(radv_descriptor_set_layout, layout,
523 pAllocateInfo->pSetLayouts[i]);
524
525 assert(!(layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
526
527 result = radv_descriptor_set_create(device, pool, layout, &set);
528 if (result != VK_SUCCESS)
529 break;
530
531 pDescriptorSets[i] = radv_descriptor_set_to_handle(set);
532 }
533
534 if (result != VK_SUCCESS)
535 radv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
536 i, pDescriptorSets);
537 return result;
538 }
539
540 VkResult radv_FreeDescriptorSets(
541 VkDevice _device,
542 VkDescriptorPool descriptorPool,
543 uint32_t count,
544 const VkDescriptorSet* pDescriptorSets)
545 {
546 RADV_FROM_HANDLE(radv_device, device, _device);
547 RADV_FROM_HANDLE(radv_descriptor_pool, pool, descriptorPool);
548
549 for (uint32_t i = 0; i < count; i++) {
550 RADV_FROM_HANDLE(radv_descriptor_set, set, pDescriptorSets[i]);
551
552 if (set && !pool->host_memory_base)
553 radv_descriptor_set_destroy(device, pool, set, true);
554 }
555 return VK_SUCCESS;
556 }
557
558 static void write_texel_buffer_descriptor(struct radv_device *device,
559 struct radv_cmd_buffer *cmd_buffer,
560 unsigned *dst,
561 struct radeon_winsys_bo **buffer_list,
562 const VkBufferView _buffer_view)
563 {
564 RADV_FROM_HANDLE(radv_buffer_view, buffer_view, _buffer_view);
565
566 memcpy(dst, buffer_view->state, 4 * 4);
567
568 if (cmd_buffer)
569 radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer_view->bo, 7);
570 else
571 *buffer_list = buffer_view->bo;
572 }
573
574 static void write_buffer_descriptor(struct radv_device *device,
575 struct radv_cmd_buffer *cmd_buffer,
576 unsigned *dst,
577 struct radeon_winsys_bo **buffer_list,
578 const VkDescriptorBufferInfo *buffer_info)
579 {
580 RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);
581 uint64_t va = radv_buffer_get_va(buffer->bo);
582 uint32_t range = buffer_info->range;
583
584 if (buffer_info->range == VK_WHOLE_SIZE)
585 range = buffer->size - buffer_info->offset;
586
587 va += buffer_info->offset + buffer->offset;
588 dst[0] = va;
589 dst[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
590 dst[2] = range;
591 dst[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
592 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
593 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
594 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
595 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
596 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
597
598 if (cmd_buffer)
599 radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer->bo, 7);
600 else
601 *buffer_list = buffer->bo;
602 }
603
604 static void write_dynamic_buffer_descriptor(struct radv_device *device,
605 struct radv_descriptor_range *range,
606 struct radeon_winsys_bo **buffer_list,
607 const VkDescriptorBufferInfo *buffer_info)
608 {
609 RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);
610 uint64_t va = radv_buffer_get_va(buffer->bo);
611 unsigned size = buffer_info->range;
612
613 if (buffer_info->range == VK_WHOLE_SIZE)
614 size = buffer->size - buffer_info->offset;
615
616 va += buffer_info->offset + buffer->offset;
617 range->va = va;
618 range->size = size;
619
620 *buffer_list = buffer->bo;
621 }
622
623 static void
624 write_image_descriptor(struct radv_device *device,
625 struct radv_cmd_buffer *cmd_buffer,
626 unsigned *dst,
627 struct radeon_winsys_bo **buffer_list,
628 VkDescriptorType descriptor_type,
629 const VkDescriptorImageInfo *image_info)
630 {
631 RADV_FROM_HANDLE(radv_image_view, iview, image_info->imageView);
632
633 if (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
634 memcpy(dst, iview->storage_descriptor, 8 * 4);
635 memcpy(dst + 8, iview->storage_fmask_descriptor, 8 * 4);
636 } else {
637 memcpy(dst, iview->descriptor, 8 * 4);
638 memcpy(dst + 8, iview->fmask_descriptor, 8 * 4);
639 }
640
641 if (cmd_buffer)
642 radv_cs_add_buffer(device->ws, cmd_buffer->cs, iview->bo, 7);
643 else
644 *buffer_list = iview->bo;
645 }
646
647 static void
648 write_combined_image_sampler_descriptor(struct radv_device *device,
649 struct radv_cmd_buffer *cmd_buffer,
650 unsigned *dst,
651 struct radeon_winsys_bo **buffer_list,
652 VkDescriptorType descriptor_type,
653 const VkDescriptorImageInfo *image_info,
654 bool has_sampler)
655 {
656 RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);
657
658 write_image_descriptor(device, cmd_buffer, dst, buffer_list, descriptor_type, image_info);
659 /* copy over sampler state */
660 if (has_sampler)
661 memcpy(dst + 16, sampler->state, 16);
662 }
663
664 static void
665 write_sampler_descriptor(struct radv_device *device,
666 unsigned *dst,
667 const VkDescriptorImageInfo *image_info)
668 {
669 RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);
670
671 memcpy(dst, sampler->state, 16);
672 }
673
674 void radv_update_descriptor_sets(
675 struct radv_device* device,
676 struct radv_cmd_buffer* cmd_buffer,
677 VkDescriptorSet dstSetOverride,
678 uint32_t descriptorWriteCount,
679 const VkWriteDescriptorSet* pDescriptorWrites,
680 uint32_t descriptorCopyCount,
681 const VkCopyDescriptorSet* pDescriptorCopies)
682 {
683 uint32_t i, j;
684 for (i = 0; i < descriptorWriteCount; i++) {
685 const VkWriteDescriptorSet *writeset = &pDescriptorWrites[i];
686 RADV_FROM_HANDLE(radv_descriptor_set, set,
687 dstSetOverride ? dstSetOverride : writeset->dstSet);
688 const struct radv_descriptor_set_binding_layout *binding_layout =
689 set->layout->binding + writeset->dstBinding;
690 uint32_t *ptr = set->mapped_ptr;
691 struct radeon_winsys_bo **buffer_list = set->descriptors;
692 /* Immutable samplers are not copied into push descriptors when they are
693 * allocated, so if we are writing push descriptors we have to copy the
694 * immutable samplers into them now.
695 */
696 const bool copy_immutable_samplers = cmd_buffer &&
697 binding_layout->immutable_samplers_offset && !binding_layout->immutable_samplers_equal;
698 const uint32_t *samplers = radv_immutable_samplers(set->layout, binding_layout);
699
700 ptr += binding_layout->offset / 4;
701 ptr += binding_layout->size * writeset->dstArrayElement / 4;
702 buffer_list += binding_layout->buffer_offset;
703 buffer_list += writeset->dstArrayElement;
704 for (j = 0; j < writeset->descriptorCount; ++j) {
705 switch(writeset->descriptorType) {
706 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
707 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
708 unsigned idx = writeset->dstArrayElement + j;
709 idx += binding_layout->dynamic_offset_offset;
710 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
711 write_dynamic_buffer_descriptor(device, set->dynamic_descriptors + idx,
712 buffer_list, writeset->pBufferInfo + j);
713 break;
714 }
715 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
716 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
717 write_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,
718 writeset->pBufferInfo + j);
719 break;
720 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
721 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
722 write_texel_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,
723 writeset->pTexelBufferView[j]);
724 break;
725 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
726 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
727 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
728 write_image_descriptor(device, cmd_buffer, ptr, buffer_list,
729 writeset->descriptorType,
730 writeset->pImageInfo + j);
731 break;
732 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
733 write_combined_image_sampler_descriptor(device, cmd_buffer, ptr, buffer_list,
734 writeset->descriptorType,
735 writeset->pImageInfo + j,
736 !binding_layout->immutable_samplers_offset);
737 if (copy_immutable_samplers) {
738 const unsigned idx = writeset->dstArrayElement + j;
739 memcpy(ptr + 16, samplers + 4 * idx, 16);
740 }
741 break;
742 case VK_DESCRIPTOR_TYPE_SAMPLER:
743 if (!binding_layout->immutable_samplers_offset) {
744 write_sampler_descriptor(device, ptr,
745 writeset->pImageInfo + j);
746 } else if (copy_immutable_samplers) {
747 unsigned idx = writeset->dstArrayElement + j;
748 memcpy(ptr, samplers + 4 * idx, 16);
749 }
750 break;
751 default:
752 unreachable("unimplemented descriptor type");
753 break;
754 }
755 ptr += binding_layout->size / 4;
756 ++buffer_list;
757 }
758
759 }
760
761 for (i = 0; i < descriptorCopyCount; i++) {
762 const VkCopyDescriptorSet *copyset = &pDescriptorCopies[i];
763 RADV_FROM_HANDLE(radv_descriptor_set, src_set,
764 copyset->srcSet);
765 RADV_FROM_HANDLE(radv_descriptor_set, dst_set,
766 copyset->dstSet);
767 const struct radv_descriptor_set_binding_layout *src_binding_layout =
768 src_set->layout->binding + copyset->srcBinding;
769 const struct radv_descriptor_set_binding_layout *dst_binding_layout =
770 dst_set->layout->binding + copyset->dstBinding;
771 uint32_t *src_ptr = src_set->mapped_ptr;
772 uint32_t *dst_ptr = dst_set->mapped_ptr;
773 struct radeon_winsys_bo **src_buffer_list = src_set->descriptors;
774 struct radeon_winsys_bo **dst_buffer_list = dst_set->descriptors;
775
776 src_ptr += src_binding_layout->offset / 4;
777 dst_ptr += dst_binding_layout->offset / 4;
778
779 src_ptr += src_binding_layout->size * copyset->srcArrayElement / 4;
780 dst_ptr += dst_binding_layout->size * copyset->dstArrayElement / 4;
781
782 src_buffer_list += src_binding_layout->buffer_offset;
783 src_buffer_list += copyset->srcArrayElement;
784
785 dst_buffer_list += dst_binding_layout->buffer_offset;
786 dst_buffer_list += copyset->dstArrayElement;
787
788 for (j = 0; j < copyset->descriptorCount; ++j) {
789 switch (src_binding_layout->type) {
790 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
791 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
792 unsigned src_idx = copyset->srcArrayElement + j;
793 unsigned dst_idx = copyset->dstArrayElement + j;
794 struct radv_descriptor_range *src_range, *dst_range;
795 src_idx += src_binding_layout->dynamic_offset_offset;
796 dst_idx += dst_binding_layout->dynamic_offset_offset;
797
798 src_range = src_set->dynamic_descriptors + src_idx;
799 dst_range = dst_set->dynamic_descriptors + dst_idx;
800 *dst_range = *src_range;
801 break;
802 }
803 default:
804 memcpy(dst_ptr, src_ptr, src_binding_layout->size);
805 }
806 src_ptr += src_binding_layout->size / 4;
807 dst_ptr += dst_binding_layout->size / 4;
808 dst_buffer_list[j] = src_buffer_list[j];
809 ++src_buffer_list;
810 ++dst_buffer_list;
811 }
812 }
813 }
814
815 void radv_UpdateDescriptorSets(
816 VkDevice _device,
817 uint32_t descriptorWriteCount,
818 const VkWriteDescriptorSet* pDescriptorWrites,
819 uint32_t descriptorCopyCount,
820 const VkCopyDescriptorSet* pDescriptorCopies)
821 {
822 RADV_FROM_HANDLE(radv_device, device, _device);
823
824 radv_update_descriptor_sets(device, NULL, VK_NULL_HANDLE, descriptorWriteCount, pDescriptorWrites,
825 descriptorCopyCount, pDescriptorCopies);
826 }
827
828 VkResult radv_CreateDescriptorUpdateTemplateKHR(VkDevice _device,
829 const VkDescriptorUpdateTemplateCreateInfoKHR *pCreateInfo,
830 const VkAllocationCallbacks *pAllocator,
831 VkDescriptorUpdateTemplateKHR *pDescriptorUpdateTemplate)
832 {
833 RADV_FROM_HANDLE(radv_device, device, _device);
834 RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout, pCreateInfo->descriptorSetLayout);
835 const uint32_t entry_count = pCreateInfo->descriptorUpdateEntryCount;
836 const size_t size = sizeof(struct radv_descriptor_update_template) +
837 sizeof(struct radv_descriptor_update_template_entry) * entry_count;
838 struct radv_descriptor_update_template *templ;
839 uint32_t i;
840
841 templ = vk_alloc2(&device->alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
842 if (!templ)
843 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
844
845 templ->entry_count = entry_count;
846
847 for (i = 0; i < entry_count; i++) {
848 const VkDescriptorUpdateTemplateEntryKHR *entry = &pCreateInfo->pDescriptorUpdateEntries[i];
849 const struct radv_descriptor_set_binding_layout *binding_layout =
850 set_layout->binding + entry->dstBinding;
851 const uint32_t buffer_offset = binding_layout->buffer_offset + entry->dstArrayElement;
852 const uint32_t *immutable_samplers = NULL;
853 uint32_t dst_offset;
854 uint32_t dst_stride;
855
856 /* dst_offset is an offset into dynamic_descriptors when the descriptor
857 is dynamic, and an offset into mapped_ptr otherwise */
858 switch (entry->descriptorType) {
859 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
860 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
861 assert(pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR);
862 dst_offset = binding_layout->dynamic_offset_offset + entry->dstArrayElement;
863 dst_stride = 0; /* Not used */
864 break;
865 default:
866 switch (entry->descriptorType) {
867 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
868 case VK_DESCRIPTOR_TYPE_SAMPLER:
869 /* Immutable samplers are copied into push descriptors when they are pushed */
870 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR &&
871 binding_layout->immutable_samplers_offset && !binding_layout->immutable_samplers_equal) {
872 immutable_samplers = radv_immutable_samplers(set_layout, binding_layout) + entry->dstArrayElement * 4;
873 }
874 break;
875 default:
876 break;
877 }
878 dst_offset = binding_layout->offset / 4 + binding_layout->size * entry->dstArrayElement / 4;
879 dst_stride = binding_layout->size / 4;
880 break;
881 }
882
883 templ->entry[i] = (struct radv_descriptor_update_template_entry) {
884 .descriptor_type = entry->descriptorType,
885 .descriptor_count = entry->descriptorCount,
886 .src_offset = entry->offset,
887 .src_stride = entry->stride,
888 .dst_offset = dst_offset,
889 .dst_stride = dst_stride,
890 .buffer_offset = buffer_offset,
891 .has_sampler = !binding_layout->immutable_samplers_offset,
892 .immutable_samplers = immutable_samplers
893 };
894 }
895
896 *pDescriptorUpdateTemplate = radv_descriptor_update_template_to_handle(templ);
897 return VK_SUCCESS;
898 }
899
900 void radv_DestroyDescriptorUpdateTemplateKHR(VkDevice _device,
901 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
902 const VkAllocationCallbacks *pAllocator)
903 {
904 RADV_FROM_HANDLE(radv_device, device, _device);
905 RADV_FROM_HANDLE(radv_descriptor_update_template, templ, descriptorUpdateTemplate);
906
907 if (!templ)
908 return;
909
910 vk_free2(&device->alloc, pAllocator, templ);
911 }
912
913 void radv_update_descriptor_set_with_template(struct radv_device *device,
914 struct radv_cmd_buffer *cmd_buffer,
915 struct radv_descriptor_set *set,
916 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
917 const void *pData)
918 {
919 RADV_FROM_HANDLE(radv_descriptor_update_template, templ, descriptorUpdateTemplate);
920 uint32_t i;
921
922 for (i = 0; i < templ->entry_count; ++i) {
923 struct radeon_winsys_bo **buffer_list = set->descriptors + templ->entry[i].buffer_offset;
924 uint32_t *pDst = set->mapped_ptr + templ->entry[i].dst_offset;
925 const uint8_t *pSrc = ((const uint8_t *) pData) + templ->entry[i].src_offset;
926 uint32_t j;
927
928 for (j = 0; j < templ->entry[i].descriptor_count; ++j) {
929 switch (templ->entry[i].descriptor_type) {
930 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
931 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
932 const unsigned idx = templ->entry[i].dst_offset + j;
933 assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
934 write_dynamic_buffer_descriptor(device, set->dynamic_descriptors + idx,
935 buffer_list, (struct VkDescriptorBufferInfo *) pSrc);
936 break;
937 }
938 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
939 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
940 write_buffer_descriptor(device, cmd_buffer, pDst, buffer_list,
941 (struct VkDescriptorBufferInfo *) pSrc);
942 break;
943 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
944 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
945 write_texel_buffer_descriptor(device, cmd_buffer, pDst, buffer_list,
946 *(VkBufferView *) pSrc);
947 break;
948 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
949 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
950 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
951 write_image_descriptor(device, cmd_buffer, pDst, buffer_list,
952 templ->entry[i].descriptor_type,
953 (struct VkDescriptorImageInfo *) pSrc);
954 break;
955 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
956 write_combined_image_sampler_descriptor(device, cmd_buffer, pDst, buffer_list,
957 templ->entry[i].descriptor_type,
958 (struct VkDescriptorImageInfo *) pSrc,
959 templ->entry[i].has_sampler);
960 if (templ->entry[i].immutable_samplers)
961 memcpy(pDst + 16, templ->entry[i].immutable_samplers + 4 * j, 16);
962 break;
963 case VK_DESCRIPTOR_TYPE_SAMPLER:
964 if (templ->entry[i].has_sampler)
965 write_sampler_descriptor(device, pDst,
966 (struct VkDescriptorImageInfo *) pSrc);
967 else if (templ->entry[i].immutable_samplers)
968 memcpy(pDst, templ->entry[i].immutable_samplers + 4 * j, 16);
969 break;
970 default:
971 unreachable("unimplemented descriptor type");
972 break;
973 }
974 pSrc += templ->entry[i].src_stride;
975 pDst += templ->entry[i].dst_stride;
976 ++buffer_list;
977 }
978 }
979 }
980
981 void radv_UpdateDescriptorSetWithTemplateKHR(VkDevice _device,
982 VkDescriptorSet descriptorSet,
983 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
984 const void *pData)
985 {
986 RADV_FROM_HANDLE(radv_device, device, _device);
987 RADV_FROM_HANDLE(radv_descriptor_set, set, descriptorSet);
988
989 radv_update_descriptor_set_with_template(device, NULL, set, descriptorUpdateTemplate, pData);
990 }