Debian Codegraph @

src/imagination/vulkan/pvr_descriptor_set.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "pvr_descriptor_set.h"
25
26 #include <assert.h>
27 #include <limits.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <vulkan/vulkan.h>
32
33 #include "hwdef/rogue_hw_utils.h"
34 #include "pvr_bo.h"
35 #include "pvr_buffer.h"
36 #include "pvr_debug.h"
37 #include "pvr_device.h"
38 #include "pvr_entrypoints.h"
39 #include "pvr_image.h"
40 #include "pvr_physical_device.h"
41 #include "pvr_sampler.h"
42 #include "pvr_types.h"
43 #include "util/compiler.h"
44 #include "util/list.h"
45 #include "util/log.h"
46 #include "util/macros.h"
47 #include "util/vma.h"
48 #include "vk_alloc.h"
49 #include "vk_descriptor_update_template.h"
50 #include "vk_descriptors.h"
51 #include "vk_descriptor_set_layout.h"
52 #include "vk_format.h"
53 #include "vk_log.h"
54 #include "vk_object.h"
55 #include "vk_util.h"
56 #include "vulkan/util/vk_enum_to_str.h"
57
58 static bool
59 binding_has_immutable_samplers(const VkDescriptorSetLayoutBinding *binding)
60 {
61 switch (binding->descriptorType) {
62 case VK_DESCRIPTOR_TYPE_SAMPLER:
63 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
64 return binding->pImmutableSamplers != NULL;
65
66 default:
67 return false;
68 }
69 }
70
71 static bool
72 binding_has_dynamic_buffers(const VkDescriptorSetLayoutBinding *binding)
73 {
74 return vk_descriptor_type_is_dynamic(binding->descriptorType);
75 }
76
77 static bool
78 binding_has_combined_image_samplers(const VkDescriptorSetLayoutBinding *binding)
79 {
80 return binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
81 }
82
83 static unsigned pvr_descriptor_size(VkDescriptorType type)
84 {
85 switch (type) {
86 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
87 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
88 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
89 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
90 return sizeof(struct pvr_buffer_descriptor);
91
92 case VK_DESCRIPTOR_TYPE_SAMPLER:
93 return sizeof(struct pvr_sampler_descriptor);
94
95 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
96 return sizeof(struct pvr_combined_image_sampler_descriptor);
97
98 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
99 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
100 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
101 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
102 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
103 return sizeof(struct pvr_image_descriptor);
104
105 default:
106 mesa_loge("Unsupported descriptor type %s.\n",
107 vk_DescriptorType_to_str(type));
108 UNREACHABLE("");
109 }
110 }
111
112 VkResult pvr_CreateDescriptorSetLayout(
113 VkDevice _device,
114 const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
115 const VkAllocationCallbacks *pAllocator,
116 VkDescriptorSetLayout *pSetLayout)
117 {
118 VK_FROM_HANDLE(pvr_device, device, _device);
119 VkDescriptorSetLayoutBinding *bindings;
120 uint32_t binding_count = 0;
121 uint32_t immutable_sampler_count = 0;
122 uint32_t dynamic_buffer_count = 0;
123 uint32_t descriptor_count = 0;
124 VkResult result = VK_SUCCESS;
125 const VkDescriptorSetLayoutBindingFlagsCreateInfo *binding_flags_create_info =
126 NULL;
127 VkDescriptorBindingFlags *binding_flags = NULL;
128
129 assert(pCreateInfo->sType ==
130 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
131
132 vk_foreach_struct_const (ext, pCreateInfo->pNext) {
133 switch (ext->sType) {
134 case VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO:
135 binding_flags_create_info =
136 (const VkDescriptorSetLayoutBindingFlagsCreateInfo *)ext;
137 break;
138 default:
139 vk_debug_ignored_stype(ext->sType);
140 }
141 }
142
143 for (unsigned u = 0; u < pCreateInfo->bindingCount; ++u) {
144 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[u];
145
146 descriptor_count += binding->descriptorCount;
147
148 if (binding_has_immutable_samplers(binding))
149 immutable_sampler_count += binding->descriptorCount;
150 else if (binding_has_dynamic_buffers(binding))
151 dynamic_buffer_count += binding->descriptorCount;
152 }
153
154 result = vk_create_sorted_bindings(pCreateInfo->pBindings,
155 pCreateInfo->bindingCount,
156 &bindings,
157 binding_flags_create_info,
158 &binding_flags);
159
160 if (result != VK_SUCCESS)
161 return vk_error(device, result);
162
163 if (bindings)
164 binding_count = bindings[pCreateInfo->bindingCount - 1].binding + 1;
165
166 VK_MULTIALLOC(ma);
167 VK_MULTIALLOC_DECL(&ma, struct pvr_descriptor_set_layout, layout, 1);
168 VK_MULTIALLOC_DECL(&ma,
169 struct pvr_descriptor_set_layout_binding,
170 layout_bindings,
171 binding_count);
172 VK_MULTIALLOC_DECL(&ma,
173 struct pvr_sampler *,
174 immutable_samplers,
175 immutable_sampler_count);
176
177 if (!vk_descriptor_set_layout_multizalloc(&device->vk, &ma, pCreateInfo)) {
178 result = VK_ERROR_OUT_OF_HOST_MEMORY;
179 goto err_free_bindings;
180 }
181
182 layout->flags = pCreateInfo->flags;
183
184 layout->descriptor_count = descriptor_count;
185 layout->dynamic_buffer_count = dynamic_buffer_count;
186
187 layout->binding_count = binding_count;
188 layout->bindings = layout_bindings;
189
190 layout->immutable_sampler_count = immutable_sampler_count;
191 layout->immutable_samplers = immutable_samplers;
192
193 unsigned dynamic_buffer_idx = 0;
194 for (unsigned b = 0; b < pCreateInfo->bindingCount; ++b) {
195 const VkDescriptorSetLayoutBinding *binding = &bindings[b];
196
197 if (!binding->descriptorCount)
198 continue;
199
200 struct pvr_descriptor_set_layout_binding *layout_binding =
201 &layout_bindings[binding->binding];
202
203 layout_binding->stride = pvr_descriptor_size(binding->descriptorType);
204
205 if (vk_descriptor_type_is_dynamic(binding->descriptorType)) {
206 layout_binding->offset = ~0;
207 layout_binding->dynamic_buffer_idx = dynamic_buffer_idx;
208
209 dynamic_buffer_idx += binding->descriptorCount;
210 } else {
211 layout_binding->dynamic_buffer_idx = ~0;
212 layout_binding->offset = layout->size;
213
214 layout->size += binding->descriptorCount * layout_binding->stride;
215 }
216
217 layout_binding->type = binding->descriptorType;
218
219 layout_binding->flags = binding_flags ? binding_flags[b] : 0;
220
221 layout_binding->descriptor_count = binding->descriptorCount;
222 layout_binding->stage_flags = binding->stageFlags;
223 layout->stage_flags |= binding->stageFlags;
224
225 if (binding_has_immutable_samplers(binding)) {
226 layout_binding->immutable_sampler_count = binding->descriptorCount;
227 layout_binding->immutable_samplers = immutable_samplers;
228 immutable_samplers += binding->descriptorCount;
229
230 for (unsigned s = 0; s < layout_binding->immutable_sampler_count;
231 ++s) {
232 VK_FROM_HANDLE(pvr_sampler,
233 sampler,
234 binding->pImmutableSamplers[s]);
235 layout_binding->immutable_samplers[s] = sampler;
236 }
237 }
238 }
239
240 assert(dynamic_buffer_count == dynamic_buffer_idx);
241
242 free(bindings);
243 free(binding_flags);
244
245 *pSetLayout = pvr_descriptor_set_layout_to_handle(layout);
246
247 return VK_SUCCESS;
248
249 err_free_bindings:
250 free(bindings);
251
252 return result;
253 }
254
255 VkResult pvr_CreateDescriptorPool(VkDevice _device,
256 const VkDescriptorPoolCreateInfo *pCreateInfo,
257 const VkAllocationCallbacks *pAllocator,
258 VkDescriptorPool *pDescriptorPool)
259 {
260 VK_FROM_HANDLE(pvr_device, device, _device);
261 const uint32_t cache_line_size =
262 pvr_get_slc_cache_line_size(&device->pdevice->dev_info);
263 struct pvr_descriptor_pool *pool;
264 uint64_t bo_size = 0;
265 VkResult result;
266
267 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO);
268
269 pool = vk_object_alloc(&device->vk,
270 pAllocator,
271 sizeof(*pool),
272 VK_OBJECT_TYPE_DESCRIPTOR_POOL);
273 if (!pool)
274 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
275
276 if (pAllocator)
277 pool->alloc = *pAllocator;
278 else
279 pool->alloc = device->vk.alloc;
280
281 pool->flags = pCreateInfo->flags;
282
283 list_inithead(&pool->desc_sets);
284
285 if (pCreateInfo->maxSets > 0) {
286 for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
287 const VkDescriptorType type = pCreateInfo->pPoolSizes[i].type;
288 const uint32_t descriptor_count =
289 pCreateInfo->pPoolSizes[i].descriptorCount;
290
291 if (vk_descriptor_type_is_dynamic(type))
292 continue;
293
294 bo_size += descriptor_count * pvr_descriptor_size(type);
295 }
296 }
297
298 result = pvr_bo_suballoc(&device->suballoc_general,
299 bo_size,
300 cache_line_size,
301 false,
302 &pool->pvr_bo);
303
304 if (result != VK_SUCCESS)
305 goto err_free_pool;
306
307 pool->mapping = pvr_bo_suballoc_get_map_addr(pool->pvr_bo);
308 assert(pool->mapping);
309
310 util_vma_heap_init(&pool->heap, pool->pvr_bo->dev_addr.addr, bo_size);
311
312 *pDescriptorPool = pvr_descriptor_pool_to_handle(pool);
313
314 return VK_SUCCESS;
315
316 err_free_pool:
317 vk_object_free(&device->vk, pAllocator, pool);
318
319 return result;
320 }
321
322 static VkResult pvr_pool_alloc(struct pvr_descriptor_pool *pool,
323 unsigned size,
324 pvr_dev_addr_t *dev_addr,
325 void **mapping)
326 {
327 uint64_t _dev_addr = util_vma_heap_alloc(&pool->heap, size, 1);
328 if (!_dev_addr)
329 return VK_ERROR_OUT_OF_POOL_MEMORY;
330
331 *mapping =
332 (uint8_t *)pool->mapping + (_dev_addr - pool->pvr_bo->dev_addr.addr);
333
334 *dev_addr = PVR_DEV_ADDR(_dev_addr);
335
336 return VK_SUCCESS;
337 }
338
339 static void pvr_pool_free(struct pvr_descriptor_pool *pool,
340 pvr_dev_addr_t *dev_addr,
341 unsigned size)
342 {
343 util_vma_heap_free(&pool->heap, dev_addr->addr, size);
344 }
345
346 static void pvr_free_descriptor_set(struct pvr_device *device,
347 struct pvr_descriptor_pool *pool,
348 struct pvr_descriptor_set *set)
349 {
350 list_del(&set->link);
351 vk_descriptor_set_layout_unref(&device->vk, &set->layout->vk);
352 if (set->size > 0)
353 pvr_pool_free(pool, &set->dev_addr, set->size);
354 vk_object_free(&device->vk, &pool->alloc, set);
355 }
356
357 void pvr_DestroyDescriptorPool(VkDevice _device,
358 VkDescriptorPool _pool,
359 const VkAllocationCallbacks *pAllocator)
360 {
361 VK_FROM_HANDLE(pvr_device, device, _device);
362 VK_FROM_HANDLE(pvr_descriptor_pool, pool, _pool);
363
364 if (!pool)
365 return;
366
367 list_for_each_entry_safe (struct pvr_descriptor_set,
368 set,
369 &pool->desc_sets,
370 link) {
371 pvr_free_descriptor_set(device, pool, set);
372 }
373
374 util_vma_heap_finish(&pool->heap);
375 pvr_bo_suballoc_free(pool->pvr_bo);
376
377 vk_object_free(&device->vk, pAllocator, pool);
378 }
379
380 VkResult pvr_ResetDescriptorPool(VkDevice _device,
381 VkDescriptorPool descriptorPool,
382 VkDescriptorPoolResetFlags flags)
383 {
384 VK_FROM_HANDLE(pvr_descriptor_pool, pool, descriptorPool);
385 VK_FROM_HANDLE(pvr_device, device, _device);
386
387 list_for_each_entry_safe (struct pvr_descriptor_set,
388 set,
389 &pool->desc_sets,
390 link) {
391 pvr_free_descriptor_set(device, pool, set);
392 }
393
394 return VK_SUCCESS;
395 }
396
397 #define PER_ARCH_FUNCS(arch) \
398 void pvr_##arch##_descriptor_set_write_immutable_samplers( \
399 struct pvr_descriptor_set_layout *layout, \
400 struct pvr_descriptor_set *set)
401
402 PER_ARCH_FUNCS(rogue);
403
404 static VkResult
405 pvr_descriptor_set_create(struct pvr_device *device,
406 struct pvr_descriptor_pool *pool,
407 struct pvr_descriptor_set_layout *layout,
408 struct pvr_descriptor_set **const descriptor_set_out)
409 {
410 struct pvr_descriptor_set *set;
411 unsigned set_alloc_size;
412 VkResult result;
413
414 *descriptor_set_out = NULL;
415
416 set_alloc_size = sizeof(*set);
417 set_alloc_size +=
418 layout->dynamic_buffer_count * sizeof(*set->dynamic_buffers);
419
420 set = vk_object_zalloc(&device->vk,
421 &pool->alloc,
422 set_alloc_size,
423 VK_OBJECT_TYPE_DESCRIPTOR_SET);
424 if (!set)
425 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
426
427 set->size = layout->size;
428 if (set->size > 0) {
429 result = pvr_pool_alloc(pool, set->size, &set->dev_addr, &set->mapping);
430 if (result != VK_SUCCESS)
431 goto err_free_descriptor_set;
432 }
433
434 vk_descriptor_set_layout_ref(&layout->vk);
435 set->layout = layout;
436 set->pool = pool;
437
438 list_addtail(&set->link, &pool->desc_sets);
439
440 /* Setup immutable samplers. */
441 enum pvr_device_arch arch = device->pdevice->dev_info.ident.arch;
442 PVR_ARCH_DISPATCH(descriptor_set_write_immutable_samplers, arch, layout, set);
443
444 *descriptor_set_out = set;
445
446 return VK_SUCCESS;
447
448 err_free_descriptor_set:
449 vk_object_free(&device->vk, &pool->alloc, set);
450
451 return result;
452 }
453
454 VkResult
455 pvr_AllocateDescriptorSets(VkDevice _device,
456 const VkDescriptorSetAllocateInfo *pAllocateInfo,
457 VkDescriptorSet *pDescriptorSets)
458 {
459 VK_FROM_HANDLE(pvr_descriptor_pool, pool, pAllocateInfo->descriptorPool);
460 VK_FROM_HANDLE(pvr_device, device, _device);
461 VkResult result;
462 uint32_t i;
463
464 vk_foreach_struct_const (ext, pAllocateInfo->pNext) {
465 vk_debug_ignored_stype(ext->sType);
466 }
467
468 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
469 VK_FROM_HANDLE(pvr_descriptor_set_layout,
470 layout,
471 pAllocateInfo->pSetLayouts[i]);
472 struct pvr_descriptor_set *set;
473
474 result = pvr_descriptor_set_create(device, pool, layout, &set);
475 if (result != VK_SUCCESS)
476 goto err_free_descriptor_sets;
477
478 pDescriptorSets[i] = pvr_descriptor_set_to_handle(set);
479 }
480
481 return VK_SUCCESS;
482
483 err_free_descriptor_sets:
484 pvr_FreeDescriptorSets(_device,
485 pAllocateInfo->descriptorPool,
486 i,
487 pDescriptorSets);
488
489 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++)
490 pDescriptorSets[i] = VK_NULL_HANDLE;
491
492 return result;
493 }
494
495 VkResult pvr_FreeDescriptorSets(VkDevice _device,
496 VkDescriptorPool descriptorPool,
497 uint32_t count,
498 const VkDescriptorSet *pDescriptorSets)
499 {
500 VK_FROM_HANDLE(pvr_descriptor_pool, pool, descriptorPool);
501 VK_FROM_HANDLE(pvr_device, device, _device);
502
503 for (uint32_t i = 0; i < count; i++) {
504 struct pvr_descriptor_set *set;
505
506 if (!pDescriptorSets[i])
507 continue;
508
509 set = pvr_descriptor_set_from_handle(pDescriptorSets[i]);
510 pvr_free_descriptor_set(device, pool, set);
511 }
512
513 return VK_SUCCESS;
514 }
515
516 void pvr_GetDescriptorSetLayoutSupport(
517 VkDevice _device,
518 const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
519 VkDescriptorSetLayoutSupport *pSupport)
520 {
521 uint32_t descriptor_count = 0;
522
523 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++)
524 descriptor_count += pCreateInfo->pBindings[i].descriptorCount;
525
526 pSupport->supported = descriptor_count <= PVR_MAX_DESCRIPTORS_PER_SET;
527 }