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