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