Debian Codegraph @

src/asahi/vulkan/hk_descriptor_set_layout.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * SPDX-License-Identifier: MIT
6 */
7 #include "hk_descriptor_set_layout.h"
8
9 #include "hk_descriptor_set.h"
10 #include "hk_device.h"
11 #include "hk_entrypoints.h"
12 #include "hk_physical_device.h"
13 #include "hk_sampler.h"
14
15 #include "vk_pipeline_layout.h"
16
17 static bool
18 binding_has_immutable_samplers(const VkDescriptorSetLayoutBinding *binding)
19 {
20 switch (binding->descriptorType) {
21 case VK_DESCRIPTOR_TYPE_SAMPLER:
22 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
23 return binding->pImmutableSamplers != NULL;
24
25 default:
26 return false;
27 }
28 }
29
30 void
31 hk_descriptor_stride_align_for_type(
32 const struct hk_physical_device *pdev, VkDescriptorType type,
33 const VkMutableDescriptorTypeListEXT *type_list, uint32_t *stride,
34 uint32_t *alignment)
35 {
36 switch (type) {
37 case VK_DESCRIPTOR_TYPE_SAMPLER:
38 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
39 /* TODO: How do samplers work? */
40 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
41 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
42 *stride = *alignment = sizeof(struct hk_sampled_image_descriptor);
43 break;
44
45 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
46 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
47 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
48 *stride = sizeof(struct hk_storage_image_descriptor);
49 *alignment = 8;
50 break;
51
52 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
53 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
54 *stride = *alignment = sizeof(struct hk_buffer_address);
55 break;
56
57 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
58 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
59 *stride = *alignment = 0; /* These don't take up buffer space */
60 break;
61
62 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
63 *stride = 1; /* Array size is bytes */
64 *alignment = HK_MIN_UBO_ALIGNMENT;
65 break;
66
67 case VK_DESCRIPTOR_TYPE_MUTABLE_EXT:
68 *stride = *alignment = 0;
69 if (type_list == NULL)
70 *stride = *alignment = HK_MAX_DESCRIPTOR_SIZE;
71 for (unsigned i = 0; type_list && i < type_list->descriptorTypeCount;
72 i++) {
73 /* This shouldn't recurse */
74 assert(type_list->pDescriptorTypes[i] !=
75 VK_DESCRIPTOR_TYPE_MUTABLE_EXT);
76 uint32_t desc_stride, desc_align;
77 hk_descriptor_stride_align_for_type(pdev,
78 type_list->pDescriptorTypes[i],
79 NULL, &desc_stride, &desc_align);
80 *stride = MAX2(*stride, desc_stride);
81 *alignment = MAX2(*alignment, desc_align);
82 }
83 *stride = align(*stride, *alignment);
84 break;
85
86 default:
87 UNREACHABLE("Invalid descriptor type");
88 }
89
90 assert(*stride <= HK_MAX_DESCRIPTOR_SIZE);
91 }
92
93 static const VkMutableDescriptorTypeListEXT *
94 hk_descriptor_get_type_list(VkDescriptorType type,
95 const VkMutableDescriptorTypeCreateInfoEXT *info,
96 const uint32_t info_idx)
97 {
98 const VkMutableDescriptorTypeListEXT *type_list = NULL;
99 if (type == VK_DESCRIPTOR_TYPE_MUTABLE_EXT) {
100 assert(info != NULL);
101 assert(info_idx < info->mutableDescriptorTypeListCount);
102 type_list = &info->pMutableDescriptorTypeLists[info_idx];
103 }
104 return type_list;
105 }
106
107 VKAPI_ATTR VkResult VKAPI_CALL
108 hk_CreateDescriptorSetLayout(VkDevice device,
109 const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
110 const VkAllocationCallbacks *pAllocator,
111 VkDescriptorSetLayout *pSetLayout)
112 {
113 VK_FROM_HANDLE(hk_device, dev, device);
114 struct hk_physical_device *pdev = hk_device_physical(dev);
115
116 uint32_t num_bindings = 0;
117 uint32_t immutable_sampler_count = 0;
118 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
119 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
120 num_bindings = MAX2(num_bindings, binding->binding + 1);
121
122 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
123 *
124 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
125 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
126 * pImmutableSamplers can be used to initialize a set of immutable
127 * samplers. [...] If descriptorType is not one of these descriptor
128 * types, then pImmutableSamplers is ignored.
129 *
130 * We need to be careful here and only parse pImmutableSamplers if we
131 * have one of the right descriptor types.
132 */
133 if (binding_has_immutable_samplers(binding))
134 immutable_sampler_count += binding->descriptorCount;
135 }
136
137 VK_MULTIALLOC(ma);
138 VK_MULTIALLOC_DECL(&ma, struct hk_descriptor_set_layout, layout, 1);
139 VK_MULTIALLOC_DECL(&ma, struct hk_descriptor_set_binding_layout, bindings,
140 num_bindings);
141 VK_MULTIALLOC_DECL(&ma, struct hk_sampler *, samplers,
142 immutable_sampler_count);
143
144 if (!vk_descriptor_set_layout_multizalloc(&dev->vk, &ma, pCreateInfo))
145 return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
146
147 layout->binding_count = num_bindings;
148
149 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
150 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
151 uint32_t b = binding->binding;
152 /* We temporarily store pCreateInfo->pBindings[] index (plus one) in the
153 * immutable_samplers pointer. This provides us with a quick-and-dirty
154 * way to sort the bindings by binding number.
155 */
156 layout->binding[b].immutable_samplers = (void *)(uintptr_t)(j + 1);
157 }
158
159 const VkDescriptorSetLayoutBindingFlagsCreateInfo *binding_flags_info =
160 vk_find_struct_const(pCreateInfo->pNext,
161 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO);
162 const VkMutableDescriptorTypeCreateInfoEXT *mutable_info =
163 vk_find_struct_const(pCreateInfo->pNext,
164 MUTABLE_DESCRIPTOR_TYPE_CREATE_INFO_EXT);
165
166 uint32_t buffer_size = 0;
167 uint8_t dynamic_buffer_count = 0;
168 for (uint32_t b = 0; b < num_bindings; b++) {
169 /* We stashed the pCreateInfo->pBindings[] index (plus one) in the
170 * immutable_samplers pointer. Check for NULL (empty binding) and then
171 * reset it and compute the index.
172 */
173 if (layout->binding[b].immutable_samplers == NULL)
174 continue;
175 const uint32_t info_idx =
176 (uintptr_t)(void *)layout->binding[b].immutable_samplers - 1;
177 layout->binding[b].immutable_samplers = NULL;
178
179 const VkDescriptorSetLayoutBinding *binding =
180 &pCreateInfo->pBindings[info_idx];
181
182 if (binding->descriptorCount == 0)
183 continue;
184
185 layout->binding[b].type = binding->descriptorType;
186
187 if (binding_flags_info && binding_flags_info->bindingCount > 0) {
188 assert(binding_flags_info->bindingCount == pCreateInfo->bindingCount);
189 layout->binding[b].flags = binding_flags_info->pBindingFlags[info_idx];
190 }
191
192 layout->binding[b].array_size = binding->descriptorCount;
193
194 if (vk_descriptor_type_is_dynamic(binding->descriptorType)) {
195 layout->binding[b].dynamic_buffer_index = dynamic_buffer_count;
196 dynamic_buffer_count += binding->descriptorCount;
197 }
198
199 const VkMutableDescriptorTypeListEXT *type_list =
200 hk_descriptor_get_type_list(binding->descriptorType, mutable_info,
201 info_idx);
202
203 uint32_t stride, alignment;
204 hk_descriptor_stride_align_for_type(pdev, binding->descriptorType,
205 type_list, &stride, &alignment);
206
207 uint8_t max_plane_count = 1;
208
209 if (binding_has_immutable_samplers(binding)) {
210 layout->binding[b].immutable_samplers = samplers;
211 samplers += binding->descriptorCount;
212 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
213 VK_FROM_HANDLE(hk_sampler, sampler, binding->pImmutableSamplers[i]);
214 layout->binding[b].immutable_samplers[i] = sampler;
215 const uint8_t sampler_plane_count =
216 sampler->vk.ycbcr_conversion
217 ? vk_format_get_plane_count(
218 sampler->vk.ycbcr_conversion->state.format)
219 : 1;
220 if (max_plane_count < sampler_plane_count)
221 max_plane_count = sampler_plane_count;
222 }
223 }
224
225 stride *= max_plane_count;
226
227 if (stride > 0) {
228 assert(stride <= UINT8_MAX);
229 assert(util_is_power_of_two_nonzero(alignment));
230
231 buffer_size = align64(buffer_size, alignment);
232 layout->binding[b].offset = buffer_size;
233 layout->binding[b].stride = stride;
234
235 if (layout->binding[b].flags &
236 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT) {
237 /* From the Vulkan 1.3.256 spec:
238 *
239 * VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-pBindingFlags-03004
240 * "If an element of pBindingFlags includes
241 * VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT, then
242 * all other elements of
243 * VkDescriptorSetLayoutCreateInfo::pBindings must have a
244 * smaller value of binding"
245 *
246 * In other words, it has to be the last binding.
247 */
248 assert(b == num_bindings - 1);
249 } else {
250 /* the allocation size will be computed at descriptor allocation,
251 * but the buffer size will be already aligned as this binding will
252 * be the last
253 */
254 buffer_size += stride * binding->descriptorCount;
255 }
256 }
257 }
258
259 layout->non_variable_descriptor_buffer_size = buffer_size;
260 layout->vk.dynamic_descriptor_count = dynamic_buffer_count;
261
262 struct mesa_blake3 blake3_ctx;
263 _mesa_blake3_init(&blake3_ctx);
264
265 #define BLAKE3_UPDATE_VALUE(x) \
266 _mesa_blake3_update(&blake3_ctx, &(x), sizeof(x));
267 BLAKE3_UPDATE_VALUE(layout->non_variable_descriptor_buffer_size);
268 BLAKE3_UPDATE_VALUE(layout->vk.dynamic_descriptor_count);
269 BLAKE3_UPDATE_VALUE(layout->binding_count);
270
271 for (uint32_t b = 0; b < num_bindings; b++) {
272 BLAKE3_UPDATE_VALUE(layout->binding[b].type);
273 BLAKE3_UPDATE_VALUE(layout->binding[b].flags);
274 BLAKE3_UPDATE_VALUE(layout->binding[b].array_size);
275 BLAKE3_UPDATE_VALUE(layout->binding[b].offset);
276 BLAKE3_UPDATE_VALUE(layout->binding[b].stride);
277 BLAKE3_UPDATE_VALUE(layout->binding[b].dynamic_buffer_index);
278
279 if (layout->binding[b].immutable_samplers != NULL) {
280 for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
281 const struct hk_sampler *sampler =
282 layout->binding[b].immutable_samplers[i];
283
284 /* We zalloc the object, so it's safe to hash the whole thing */
285 if (sampler != NULL && sampler->vk.ycbcr_conversion != NULL)
286 BLAKE3_UPDATE_VALUE(sampler->vk.ycbcr_conversion->state);
287 }
288 }
289 }
290 #undef BLAKE3_UPDATE_VALUE
291
292 _mesa_blake3_final(&blake3_ctx, layout->vk.blake3);
293
294 *pSetLayout = hk_descriptor_set_layout_to_handle(layout);
295
296 return VK_SUCCESS;
297 }
298
299 VKAPI_ATTR void VKAPI_CALL
300 hk_GetDescriptorSetLayoutSupport(
301 VkDevice device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
302 VkDescriptorSetLayoutSupport *pSupport)
303 {
304 VK_FROM_HANDLE(hk_device, dev, device);
305 struct hk_physical_device *pdev = hk_device_physical(dev);
306
307 const VkMutableDescriptorTypeCreateInfoEXT *mutable_info =
308 vk_find_struct_const(pCreateInfo->pNext,
309 MUTABLE_DESCRIPTOR_TYPE_CREATE_INFO_EXT);
310 const VkDescriptorSetLayoutBindingFlagsCreateInfo *binding_flags =
311 vk_find_struct_const(pCreateInfo->pNext,
312 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO);
313
314 /* Figure out the maximum alignment up-front. Otherwise, we need to sort
315 * the list of descriptors by binding number in order to get the size
316 * accumulation right.
317 */
318 uint32_t max_align = 0;
319 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
320 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[i];
321 const VkMutableDescriptorTypeListEXT *type_list =
322 hk_descriptor_get_type_list(binding->descriptorType, mutable_info, i);
323
324 uint32_t stride, alignment;
325 hk_descriptor_stride_align_for_type(pdev, binding->descriptorType,
326 type_list, &stride, &alignment);
327 max_align = MAX2(max_align, alignment);
328 }
329
330 uint64_t non_variable_size = 0;
331 uint32_t variable_stride = 0;
332 uint32_t variable_count = 0;
333 bool variable_is_inline_uniform_block = false;
334 uint8_t dynamic_buffer_count = 0;
335
336 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
337 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[i];
338
339 VkDescriptorBindingFlags flags = 0;
340 if (binding_flags != NULL && binding_flags->bindingCount > 0)
341 flags = binding_flags->pBindingFlags[i];
342
343 if (vk_descriptor_type_is_dynamic(binding->descriptorType))
344 dynamic_buffer_count += binding->descriptorCount;
345
346 const VkMutableDescriptorTypeListEXT *type_list =
347 hk_descriptor_get_type_list(binding->descriptorType, mutable_info, i);
348
349 uint32_t stride, alignment;
350 hk_descriptor_stride_align_for_type(pdev, binding->descriptorType,
351 type_list, &stride, &alignment);
352
353 if (stride > 0) {
354 assert(stride <= UINT8_MAX);
355 assert(util_is_power_of_two_nonzero(alignment));
356
357 if (flags & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT) {
358 /* From the Vulkan 1.3.256 spec:
359 *
360 * "For the purposes of this command, a variable-sized
361 * descriptor binding with a descriptorCount of zero is treated
362 * as if the descriptorCount is one"
363 */
364 variable_count = MAX2(1, binding->descriptorCount);
365 variable_stride = stride;
366
367 variable_is_inline_uniform_block =
368 binding->descriptorType ==
369 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK;
370 } else {
371 /* Since we're aligning to the maximum and since this is just a
372 * check for whether or not the max buffer size is big enough, we
373 * keep non_variable_size aligned to max_align.
374 */
375 non_variable_size += stride * binding->descriptorCount;
376 non_variable_size = align64(non_variable_size, max_align);
377 }
378 }
379 }
380
381 uint64_t buffer_size = non_variable_size;
382 if (variable_stride > 0) {
383 buffer_size += variable_stride * variable_count;
384 buffer_size = align64(buffer_size, max_align);
385 }
386
387 uint32_t max_buffer_size;
388 if (pCreateInfo->flags &
389 VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR)
390 max_buffer_size = HK_PUSH_DESCRIPTOR_SET_SIZE;
391 else
392 max_buffer_size = HK_MAX_DESCRIPTOR_SET_SIZE;
393
394 pSupport->supported = dynamic_buffer_count <= HK_MAX_DYNAMIC_BUFFERS &&
395 buffer_size <= max_buffer_size;
396
397 vk_foreach_struct(ext, pSupport->pNext) {
398 switch (ext->sType) {
399 case VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT: {
400 VkDescriptorSetVariableDescriptorCountLayoutSupport *vs = (void *)ext;
401 uint32_t max_var_count;
402
403 if (variable_stride > 0) {
404 max_var_count =
405 (max_buffer_size - non_variable_size) / variable_stride;
406 } else {
407 max_var_count = 0;
408 }
409
410 if (variable_is_inline_uniform_block) {
411 max_var_count =
412 MIN2(max_var_count, HK_MAX_INLINE_UNIFORM_BLOCK_SIZE);
413 }
414
415 vs->maxVariableDescriptorCount = max_var_count;
416 break;
417 }
418
419 default:
420 vk_debug_ignored_stype(ext->sType);
421 break;
422 }
423 }
424 }