Debian Codegraph @

src/panfrost/vulkan/panvk_vX_descriptor_set_layout.c

raw
1 /*
2 * Copyright © 2024 Collabora Ltd.
3 * Copyright © 2025 Arm Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <stdbool.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include "util/macros.h"
14 #include "util/mesa-blake3.h"
15
16 #include "vk_descriptor_update_template.h"
17 #include "vk_descriptors.h"
18 #include "vk_format.h"
19 #include "vk_log.h"
20 #include "vk_util.h"
21 #include "vk_ycbcr_conversion.h"
22
23 #include "util/bitset.h"
24
25 #include "genxml/gen_macros.h"
26
27 #include "panvk_descriptor_set_layout.h"
28 #include "panvk_device.h"
29 #include "panvk_entrypoints.h"
30 #include "panvk_macros.h"
31 #include "panvk_sampler.h"
32
33 #define PANVK_MAX_DESCS_PER_SET (1 << 24)
34
35 static bool
36 is_texture(VkDescriptorType type)
37 {
38 switch (type) {
39 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
40 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
41 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
42 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
43 return true;
44
45 default:
46 return false;
47 }
48 }
49
50 static bool
51 is_sampler(VkDescriptorType type)
52 {
53 switch (type) {
54 case VK_DESCRIPTOR_TYPE_SAMPLER:
55 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
56 return true;
57
58 default:
59 return false;
60 }
61 }
62
63 static bool
64 binding_has_immutable_samplers(const VkDescriptorSetLayoutBinding *binding)
65 {
66 return is_sampler(binding->descriptorType) &&
67 binding->pImmutableSamplers != NULL;
68 }
69
70 static void
71 set_immutable_sampler(struct panvk_descriptor_set_binding_layout *binding_layout,
72 uint32_t index, struct panvk_sampler *sampler)
73 {
74 binding_layout->immutable_samplers[index] = sampler;
75 if (!sampler->vk.ycbcr_conversion)
76 return;
77
78 binding_layout->textures_per_desc =
79 MAX2(vk_format_get_plane_count(sampler->vk.ycbcr_conversion->state.format),
80 binding_layout->textures_per_desc);
81 binding_layout->samplers_per_desc =
82 MAX2(sampler->desc_count, binding_layout->samplers_per_desc);
83 }
84
85 VkResult
86 panvk_per_arch(CreateDescriptorSetLayout)(
87 VkDevice _device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
88 const VkAllocationCallbacks *pAllocator, VkDescriptorSetLayout *pSetLayout)
89 {
90 VK_FROM_HANDLE(panvk_device, device, _device);
91 VkDescriptorSetLayoutBinding *bindings = NULL;
92 unsigned num_bindings = 0;
93 VkResult result;
94 const VkDescriptorSetLayoutBindingFlagsCreateInfo *binding_flags_info =
95 vk_find_struct_const(pCreateInfo->pNext,
96 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO);
97 VkDescriptorBindingFlags *sorted_binding_flags = NULL;
98
99 unsigned immutable_sampler_count = 0;
100 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
101 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
102 num_bindings = MAX2(num_bindings, binding->binding + 1);
103
104 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
105 *
106 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
107 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
108 * pImmutableSamplers can be used to initialize a set of immutable
109 * samplers. [...] If descriptorType is not one of these descriptor
110 * types, then pImmutableSamplers is ignored.
111 *
112 * We need to be careful here and only parse pImmutableSamplers if we
113 * have one of the right descriptor types.
114 */
115 if (binding_has_immutable_samplers(binding))
116 immutable_sampler_count += binding->descriptorCount;
117 }
118
119 if (pCreateInfo->bindingCount) {
120 result = vk_create_sorted_bindings(pCreateInfo->pBindings,
121 pCreateInfo->bindingCount, &bindings,
122 binding_flags_info,
123 &sorted_binding_flags);
124 if (result != VK_SUCCESS)
125 return panvk_error(device, result);
126
127 num_bindings = bindings[pCreateInfo->bindingCount - 1].binding + 1;
128 }
129
130 VK_MULTIALLOC(ma);
131 VK_MULTIALLOC_DECL(&ma, struct panvk_descriptor_set_layout, layout, 1);
132 VK_MULTIALLOC_DECL(&ma, struct panvk_descriptor_set_binding_layout,
133 binding_layouts, num_bindings);
134 VK_MULTIALLOC_DECL(&ma, struct panvk_sampler *, samplers,
135 immutable_sampler_count);
136
137 if (!vk_descriptor_set_layout_multizalloc(&device->vk, &ma, pCreateInfo)) {
138 free(bindings);
139 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
140 }
141
142 layout->flags = pCreateInfo->flags;
143 layout->bindings = binding_layouts;
144 layout->binding_count = num_bindings;
145
146 unsigned desc_idx = 0;
147 unsigned dyn_buf_idx = 0;
148 uint32_t dyn_ssbos = 0;
149 for (unsigned i = 0; i < pCreateInfo->bindingCount; i++) {
150 const VkDescriptorSetLayoutBinding *binding = &bindings[i];
151 struct panvk_descriptor_set_binding_layout *binding_layout =
152 &layout->bindings[binding->binding];
153
154 if (binding->descriptorCount == 0)
155 continue;
156
157 binding_layout->type = binding->descriptorType;
158
159 if (sorted_binding_flags) {
160 binding_layout->flags = sorted_binding_flags[i];
161 }
162
163 if (binding_layout->type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK) {
164 binding_layout->desc_count =
165 panvk_get_iub_desc_count(binding->descriptorCount);
166 } else {
167 binding_layout->desc_count = binding->descriptorCount;
168 }
169
170 if (is_texture(binding_layout->type))
171 binding_layout->textures_per_desc = 1;
172
173 if (is_sampler(binding_layout->type))
174 binding_layout->samplers_per_desc = 1;
175
176 if (binding_has_immutable_samplers(binding)) {
177 binding_layout->immutable_samplers = samplers;
178 samplers += binding->descriptorCount;
179 for (uint32_t j = 0; j < binding->descriptorCount; j++) {
180 VK_FROM_HANDLE(panvk_sampler, sampler,
181 binding->pImmutableSamplers[j]);
182 set_immutable_sampler(binding_layout, j, sampler);
183 }
184 }
185
186 if (vk_descriptor_type_is_dynamic(binding_layout->type)) {
187 binding_layout->desc_idx = dyn_buf_idx;
188 if (binding_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)
189 dyn_ssbos |= BITFIELD_RANGE(dyn_buf_idx, binding->descriptorCount);
190 dyn_buf_idx += binding_layout->desc_count;
191 } else {
192 binding_layout->desc_idx = desc_idx;
193 desc_idx += panvk_get_desc_stride(binding_layout) *
194 binding_layout->desc_count;
195 }
196 }
197
198 layout->desc_count = desc_idx;
199 layout->dyn_buf_count = dyn_buf_idx;
200 layout->dyn_ssbos = dyn_ssbos;
201
202 struct mesa_blake3 hash_ctx;
203 _mesa_blake3_init(&hash_ctx);
204
205 _mesa_blake3_update(&hash_ctx, &layout->binding_count,
206 sizeof(layout->binding_count));
207 _mesa_blake3_update(&hash_ctx, &layout->desc_count,
208 sizeof(layout->desc_count));
209 _mesa_blake3_update(&hash_ctx, &layout->dyn_buf_count,
210 sizeof(layout->dyn_buf_count));
211
212 for (uint32_t b = 0; b < num_bindings; b++) {
213 _mesa_blake3_update(&hash_ctx, &layout->bindings[b].type,
214 sizeof(layout->bindings[b].type));
215 _mesa_blake3_update(&hash_ctx, &layout->bindings[b].flags,
216 sizeof(layout->bindings[b].flags));
217 _mesa_blake3_update(&hash_ctx, &layout->bindings[b].desc_count,
218 sizeof(layout->bindings[b].desc_count));
219 _mesa_blake3_update(&hash_ctx, &layout->bindings[b].textures_per_desc,
220 sizeof(layout->bindings[b].textures_per_desc));
221 _mesa_blake3_update(&hash_ctx, &layout->bindings[b].samplers_per_desc,
222 sizeof(layout->bindings[b].samplers_per_desc));
223
224 if (layout->bindings[b].immutable_samplers != NULL) {
225 for (uint32_t i = 0; i < layout->bindings[b].desc_count; i++) {
226 const struct panvk_sampler *sampler =
227 layout->bindings[b].immutable_samplers[i];
228
229 /* We zalloc the object, so it's safe to hash the whole thing */
230 if (sampler != NULL && sampler->vk.ycbcr_conversion != NULL)
231 _mesa_blake3_update(&hash_ctx,
232 &sampler->vk.ycbcr_conversion->state,
233 sizeof(sampler->vk.ycbcr_conversion->state));
234 }
235 }
236 }
237
238 _mesa_blake3_final(&hash_ctx, layout->vk.blake3);
239
240 free(bindings);
241 free(sorted_binding_flags);
242 *pSetLayout = panvk_descriptor_set_layout_to_handle(layout);
243
244 return VK_SUCCESS;
245 }
246
247 static bool
248 is_supported_mutable_type(VkDescriptorType t)
249 {
250 switch (t) {
251 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
252 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
253 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
254 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
255 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
256 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
257 return true;
258 default:
259 return false;
260 }
261 }
262
263 static bool
264 is_mutable_type_list_supported(const VkMutableDescriptorTypeListEXT *const lst)
265 {
266 for (uint32_t i = 0; i < lst->descriptorTypeCount; i++) {
267 if (!is_supported_mutable_type(lst->pDescriptorTypes[i]))
268 return false;
269 }
270 return true;
271 }
272
273 void
274 panvk_per_arch(GetDescriptorSetLayoutSupport)(
275 VkDevice _device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
276 VkDescriptorSetLayoutSupport *pSupport)
277 {
278 const VkDescriptorSetLayoutBindingFlagsCreateInfo *binding_flags =
279 vk_find_struct_const(pCreateInfo->pNext,
280 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO);
281 VkDescriptorSetVariableDescriptorCountLayoutSupport *var_desc_count =
282 vk_find_struct(pSupport->pNext,
283 DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT);
284
285 const VkMutableDescriptorTypeCreateInfoEXT *mut_info = vk_find_struct_const(
286 pCreateInfo->pNext, MUTABLE_DESCRIPTOR_TYPE_CREATE_INFO_EXT);
287
288 pSupport->supported = false;
289
290 unsigned desc_count = 0, dyn_buf_count = 0, non_variable_count = 0,
291 variable_stride = 0;
292 VkDescriptorType variable_type = {0};
293 for (unsigned i = 0; i < pCreateInfo->bindingCount; i++) {
294 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[i];
295 VkDescriptorType type = binding->descriptorType;
296 VkDescriptorBindingFlags flags =
297 binding_flags && binding_flags->bindingCount > 0 ?
298 binding_flags->pBindingFlags[i] : 0;
299
300 if (vk_descriptor_type_is_dynamic(type)) {
301 dyn_buf_count += binding->descriptorCount;
302 continue;
303 }
304
305 if (binding->descriptorType == VK_DESCRIPTOR_TYPE_MUTABLE_EXT) {
306 /* VUID-VkDescriptorSetLayoutCreateInfo-pBindings-07303 */
307 assert(mut_info->mutableDescriptorTypeListCount > i);
308 if (!is_mutable_type_list_supported(
309 &mut_info->pMutableDescriptorTypeLists[i])) {
310 pSupport->supported = false;
311 return;
312 }
313 }
314
315 unsigned textures_per_desc = is_texture(type) ? 1 : 0;
316 unsigned samplers_per_desc = is_sampler(type) ? 1 : 0;
317 if (binding_has_immutable_samplers(binding)) {
318 for (uint32_t j = 0; j < binding->descriptorCount; j++) {
319 VK_FROM_HANDLE(panvk_sampler, sampler,
320 binding->pImmutableSamplers[j]);
321 if (!sampler->vk.ycbcr_conversion)
322 continue;
323
324 textures_per_desc =
325 MAX2(vk_format_get_plane_count(
326 sampler->vk.ycbcr_conversion->state.format),
327 textures_per_desc);
328 samplers_per_desc =
329 MAX2(sampler->desc_count, samplers_per_desc);
330 }
331 }
332
333 const struct panvk_descriptor_set_binding_layout layout = {
334 .type = type,
335 .textures_per_desc = textures_per_desc,
336 .samplers_per_desc = samplers_per_desc,
337 };
338
339 unsigned stride = panvk_get_desc_stride(&layout);
340 unsigned binding_desc_count = binding->descriptorCount;
341 bool has_variable_count =
342 flags & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT;
343
344 if (has_variable_count) {
345 /* From the Vulkan 1.4.318 spec for
346 * VkDescriptorSetLayoutBindingFlagsCreateInfo:
347 *
348 * "If an element of pBindingFlags includes
349 * VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT, then it
350 * must be the element with the highest binding number"
351 *
352 * Which implies only a single binding can have a variable count.
353 */
354 assert(!variable_stride);
355
356 if (binding_desc_count == 0) {
357 /* From the Vulkan 1.4.318 spec for
358 * VkDescriptorSetVariableDescriptorCountLayoutSupport:
359 *
360 * "For the purposes of this command, a variable-sized
361 * descriptor binding with a descriptorCount of zero is treated
362 * as having a descriptorCount of four if descriptorType is
363 * VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK, or one otherwise,
364 * and thus the binding is not ignored and the maximum
365 * descriptor count will be returned."
366 */
367 binding_desc_count =
368 type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK ? 4 : 1;
369 }
370
371 variable_type = type;
372 variable_stride = stride;
373 assert(variable_stride);
374 }
375
376 unsigned count = type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK
377 ? panvk_get_iub_desc_count(binding_desc_count)
378 : stride * binding_desc_count;
379
380 desc_count += count;
381
382 if (!has_variable_count)
383 non_variable_count += count;
384 }
385
386 if (desc_count > PANVK_MAX_DESCS_PER_SET ||
387 dyn_buf_count > MAX_DYNAMIC_BUFFERS)
388 return;
389
390 pSupport->supported = true;
391
392 if (!var_desc_count)
393 return;
394
395 var_desc_count->maxVariableDescriptorCount = 0;
396
397 if (!variable_stride)
398 return;
399
400 if (variable_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK) {
401 /* Maximum byte size for inline uniform block */
402 unsigned available_size =
403 panvk_get_iub_size(PANVK_MAX_DESCS_PER_SET - non_variable_count);
404 var_desc_count->maxVariableDescriptorCount =
405 MIN2(available_size, MAX_INLINE_UNIFORM_BLOCK_SIZE);
406 } else {
407 /* Maximum descriptor count for any other descriptor type */
408 var_desc_count->maxVariableDescriptorCount =
409 (PANVK_MAX_DESCS_PER_SET - non_variable_count) / variable_stride;
410 }
411 }