Debian Codegraph @

src/panfrost/vulkan/bifrost/panvk_vX_meta_desc_copy.c

raw
1 /*
2 * Copyright © 2024 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <stddef.h>
7 #include <stdint.h>
8
9 #include "genxml/gen_macros.h"
10
11 #include "nir.h"
12 #include "nir_builder.h"
13
14 #include "pan_encoder.h"
15 #include "pan_shader.h"
16
17 #include "panvk_cmd_alloc.h"
18 #include "panvk_cmd_buffer.h"
19 #include "panvk_device.h"
20 #include "panvk_meta.h"
21 #include "panvk_shader.h"
22
23 struct pan_nir_desc_copy_info {
24 uint64_t sets[MAX_SETS];
25 uint64_t tables[PANVK_BIFROST_DESC_TABLE_COUNT];
26 uint64_t img_attrib_table;
27 struct {
28 uint64_t table;
29 uint32_t limits[PANVK_BIFROST_DESC_TABLE_COUNT];
30 uint32_t attrib_buf_idx_offset;
31 } desc_copy;
32 uint32_t set_desc_counts[MAX_SETS];
33 };
34
35 #define get_input_field(b, name) \
36 nir_load_push_constant( \
37 b, 1, sizeof(((struct pan_nir_desc_copy_info *)0)->name) * 8, \
38 nir_imm_int(b, offsetof(struct pan_nir_desc_copy_info, name)))
39
40 static nir_def *
41 get_array_entry(nir_builder *b, unsigned array_offset, unsigned array_size,
42 unsigned array_stride, nir_def *idx)
43 {
44 assert(array_size > 0);
45 assert(array_stride == 4 || array_stride == 8);
46
47 STACK_ARRAY(nir_def *, lut, array_size);
48
49 /* First we populate a lookup table covering the whole array. */
50 for (unsigned i = 0; i < array_size; i++) {
51 lut[i] = nir_load_push_constant(
52 b, 1, array_stride * 8,
53 nir_imm_int(b, (i * array_stride) + array_offset));
54 }
55
56 /* Then we test each bit in the index starting from the MSB of the biggest
57 * valid index in the array and select the entry accordingly. */
58 for (unsigned lut_stride = BITFIELD_BIT(util_last_bit(array_size - 1) - 1);
59 lut_stride > 0; lut_stride >>= 1) {
60 nir_def *bit_is_set = nir_i2b(b, nir_iand_imm(b, idx, lut_stride));
61
62 for (unsigned i = 0; i < lut_stride && i + lut_stride < array_size; i++)
63 lut[i] = nir_bcsel(b, bit_is_set, lut[i + lut_stride], lut[i]);
64 }
65
66 nir_def *result = lut[0];
67
68 STACK_ARRAY_FINISH(lut);
69
70 return result;
71 }
72
73 #define get_input_array_slot(b, name, index) \
74 get_array_entry(b, offsetof(struct pan_nir_desc_copy_info, name), \
75 ARRAY_SIZE(((struct pan_nir_desc_copy_info *)0)->name), \
76 sizeof(((struct pan_nir_desc_copy_info *)0)->name[0]), \
77 index)
78
79 static void
80 extract_desc_info_from_handle(nir_builder *b, nir_def *handle, nir_def **table,
81 nir_def **desc_idx)
82 {
83 *table = nir_ushr_imm(b, handle, 28);
84 *desc_idx = nir_iand_imm(b, handle, 0xfffffff);
85 }
86
87 static void
88 set_to_table_copy(nir_builder *b, nir_def *set_ptr, nir_def *set_desc_count,
89 nir_def *src_desc_idx, nir_def *table_ptr,
90 nir_def *dst_desc_idx, unsigned element_size)
91 {
92 /* The last binding can have
93 * VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT set, we need to make
94 * we don't do an out-of-bound access on the source set. */
95 nir_def *dst_offset =
96 nir_u2u64(b, nir_imul_imm(b, dst_desc_idx, element_size));
97
98 nir_push_if(b, nir_ult(b, src_desc_idx, set_desc_count));
99 {
100 nir_def *src_offset =
101 nir_u2u64(b, nir_imul_imm(b, src_desc_idx, PANVK_DESCRIPTOR_SIZE));
102 nir_def *desc = nir_load_global(b, element_size / 4, 32,
103 nir_iadd(b, set_ptr, src_offset),
104 .align_mul = element_size);
105 nir_store_global(b, desc, nir_iadd(b, table_ptr, dst_offset),
106 .align_mul = element_size);
107 }
108 nir_push_else(b, NULL);
109 {
110 nir_const_value v[] = {
111 nir_const_value_for_uint(0, 32), nir_const_value_for_uint(0, 32),
112 nir_const_value_for_uint(0, 32), nir_const_value_for_uint(0, 32),
113 nir_const_value_for_uint(0, 32), nir_const_value_for_uint(0, 32),
114 nir_const_value_for_uint(0, 32), nir_const_value_for_uint(0, 32),
115 };
116
117 nir_def *desc = nir_build_imm(b, element_size / 4, 32, v);
118 nir_store_global(b, desc, nir_iadd(b, table_ptr, dst_offset),
119 .align_mul = element_size);
120 }
121 nir_pop_if(b, NULL);
122 }
123
124 static void
125 set_to_table_img_copy(nir_builder *b, nir_def *set_ptr, nir_def *set_desc_count,
126 nir_def *src_desc_idx, nir_def *attrib_table_ptr,
127 nir_def *attrib_buf_table_ptr, nir_def *dst_desc_idx)
128 {
129 /* The last binding can have
130 * VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT set, we need to make
131 * sure we don't do an out-of-bound access on the source set. */
132 const unsigned element_size = pan_size(ATTRIBUTE_BUFFER) * 2;
133 const unsigned attrib_buf_comps = element_size / 4;
134 const unsigned attrib_comps = pan_size(ATTRIBUTE) / 4;
135 nir_def *attrib_offset =
136 nir_u2u64(b, nir_imul_imm(b, dst_desc_idx, pan_size(ATTRIBUTE)));
137 nir_def *attrib_buf_offset =
138 nir_u2u64(b, nir_imul_imm(b, dst_desc_idx, element_size));
139
140 nir_push_if(b, nir_ult(b, src_desc_idx, set_desc_count));
141 {
142 nir_def *attr_buf_idx_offset =
143 get_input_field(b, desc_copy.attrib_buf_idx_offset);
144 nir_def *src_offset =
145 nir_u2u64(b, nir_imul_imm(b, src_desc_idx, PANVK_DESCRIPTOR_SIZE));
146 nir_def *src_desc = nir_load_global(b, element_size / 4, 32,
147 nir_iadd(b, set_ptr, src_offset),
148 .align_mul = element_size);
149 nir_def *attr_buf_type =
150 nir_iand_imm(b, nir_channel(b, src_desc, 0), BITFIELD_MASK(5));
151
152 /* Texel buffer */
153 nir_push_if(b, nir_ieq_imm(b, attr_buf_type, MALI_ATTRIBUTE_TYPE_1D));
154 {
155 nir_def *fmt_offsetenable =
156 nir_iand_imm(b, nir_channel(b, src_desc, 4), ~BITFIELD_MASK(9));
157 nir_def *buf_idx =
158 nir_iadd(b, nir_imul_imm(b, dst_desc_idx, 2), attr_buf_idx_offset);
159 nir_def *attrib_w1 = nir_ior(b, fmt_offsetenable, buf_idx);
160 nir_def *attrib_desc =
161 nir_vec2(b, attrib_w1, nir_channel(b, src_desc, 5));
162
163 nir_store_global(b, attrib_desc,
164 nir_iadd(b, attrib_table_ptr, attrib_offset),
165 .align_mul = pan_size(ATTRIBUTE),
166 .write_mask = nir_component_mask(attrib_comps));
167
168 nir_const_value v[] = {
169 nir_const_value_for_uint(0, 32),
170 nir_const_value_for_uint(0, 32),
171 nir_const_value_for_uint(0, 32),
172 nir_const_value_for_uint(0, 32),
173 };
174
175 nir_def *null_desc = nir_build_imm(b, attrib_buf_comps / 2, 32, v);
176
177 nir_def *attrib_buf_desc = nir_vec8(
178 b, nir_channel(b, src_desc, 0), nir_channel(b, src_desc, 1),
179 nir_iand_imm(b, nir_channel(b, src_desc, 2), BITFIELD_MASK(7)),
180 nir_channel(b, src_desc, 3), nir_channel(b, null_desc, 0),
181 nir_channel(b, null_desc, 1), nir_channel(b, null_desc, 2),
182 nir_channel(b, null_desc, 3));
183 nir_store_global(b, attrib_buf_desc,
184 nir_iadd(b, attrib_buf_table_ptr, attrib_buf_offset),
185 .align_mul = element_size,
186 .write_mask = nir_component_mask(attrib_buf_comps));
187 }
188 /* Image */
189 nir_push_else(b, NULL);
190 {
191 nir_def *fmt =
192 nir_iand_imm(b, nir_channel(b, src_desc, 2), 0xfffffc00);
193
194 /* Each image descriptor takes two attribute buffer slots, and we need
195 * to add the attribute buffer offset to have images working with
196 * vertex shader. */
197 nir_def *buf_idx =
198 nir_iadd(b, nir_imul_imm(b, dst_desc_idx, 2), attr_buf_idx_offset);
199
200 nir_def *attrib_w1 = nir_ior(b, buf_idx, fmt);
201
202 nir_def *attrib_desc = nir_vec2(b, attrib_w1, nir_imm_int(b, 0));
203
204 nir_store_global(b, attrib_desc,
205 nir_iadd(b, attrib_table_ptr, attrib_offset),
206 .align_mul = pan_size(ATTRIBUTE),
207 .write_mask = nir_component_mask(attrib_comps));
208
209 nir_def *attrib_buf_desc = nir_vec8(
210 b, nir_channel(b, src_desc, 0), nir_channel(b, src_desc, 1),
211 nir_iand_imm(b, nir_channel(b, src_desc, 2), BITFIELD_MASK(7)),
212 nir_channel(b, src_desc, 3), nir_channel(b, src_desc, 4),
213 nir_channel(b, src_desc, 5), nir_channel(b, src_desc, 6),
214 nir_channel(b, src_desc, 7));
215 nir_store_global(b, attrib_buf_desc,
216 nir_iadd(b, attrib_buf_table_ptr, attrib_buf_offset),
217 .align_mul = element_size,
218 .write_mask = nir_component_mask(attrib_buf_comps));
219 }
220 nir_pop_if(b, NULL);
221 }
222 nir_push_else(b, NULL);
223 {
224 nir_const_value v[] = {
225 nir_const_value_for_uint(0, 32), nir_const_value_for_uint(0, 32),
226 nir_const_value_for_uint(0, 32), nir_const_value_for_uint(0, 32),
227 nir_const_value_for_uint(0, 32), nir_const_value_for_uint(0, 32),
228 nir_const_value_for_uint(0, 32), nir_const_value_for_uint(0, 32),
229 };
230
231 nir_def *desc =
232 nir_build_imm(b, MAX2(attrib_buf_comps, attrib_comps), 32, v);
233
234 nir_store_global(b, desc,
235 nir_iadd(b, attrib_buf_table_ptr, attrib_buf_offset),
236 .align_mul = pan_size(ATTRIBUTE),
237 .write_mask = nir_component_mask(attrib_buf_comps));
238 nir_store_global(b, desc, nir_iadd(b, attrib_table_ptr, attrib_offset),
239 .align_mul = element_size,
240 .write_mask = nir_component_mask(attrib_comps));
241 }
242 nir_pop_if(b, NULL);
243 }
244
245 static void
246 single_desc_copy(nir_builder *b, nir_def *desc_copy_idx)
247 {
248 nir_def *desc_copy_offset = nir_imul_imm(b, desc_copy_idx, sizeof(uint32_t));
249 nir_def *desc_copy_ptr = nir_iadd(b, get_input_field(b, desc_copy.table),
250 nir_u2u64(b, desc_copy_offset));
251 nir_def *src_copy_handle = nir_load_global(b, 1, 32, desc_copy_ptr);
252
253 nir_def *set_idx, *src_desc_idx;
254 extract_desc_info_from_handle(b, src_copy_handle, &set_idx, &src_desc_idx);
255
256 nir_def *set_ptr = get_input_array_slot(b, sets, set_idx);
257 nir_def *set_desc_count = get_input_array_slot(b, set_desc_counts, set_idx);
258 nir_def *ubo_end =
259 get_input_field(b, desc_copy.limits[PANVK_BIFROST_DESC_TABLE_UBO]);
260 nir_def *img_end =
261 get_input_field(b, desc_copy.limits[PANVK_BIFROST_DESC_TABLE_IMG]);
262 nir_def *tex_end =
263 get_input_field(b, desc_copy.limits[PANVK_BIFROST_DESC_TABLE_TEXTURE]);
264 nir_def *sampler_end =
265 get_input_field(b, desc_copy.limits[PANVK_BIFROST_DESC_TABLE_SAMPLER]);
266
267 nir_push_if(b, nir_ult(b, desc_copy_idx, ubo_end));
268 {
269 nir_def *table_ptr =
270 get_input_field(b, tables[PANVK_BIFROST_DESC_TABLE_UBO]);
271
272 set_to_table_copy(b, set_ptr, set_desc_count, src_desc_idx, table_ptr,
273 desc_copy_idx, sizeof(struct mali_attribute_packed));
274 }
275 nir_push_else(b, NULL);
276 {
277 nir_push_if(b, nir_ult(b, desc_copy_idx, img_end));
278 {
279 nir_def *table_ptr =
280 get_input_field(b, tables[PANVK_BIFROST_DESC_TABLE_IMG]);
281 nir_def *attrib_table_ptr = get_input_field(b, img_attrib_table);
282 nir_def *attrib_buf_table_ptr = table_ptr;
283
284 set_to_table_img_copy(b, set_ptr, set_desc_count, src_desc_idx,
285 attrib_table_ptr, attrib_buf_table_ptr,
286 nir_isub(b, desc_copy_idx, ubo_end));
287 }
288 nir_push_else(b, NULL);
289 {
290 nir_push_if(b, nir_ult(b, desc_copy_idx, tex_end));
291 {
292 nir_def *table_ptr =
293 get_input_field(b, tables[PANVK_BIFROST_DESC_TABLE_TEXTURE]);
294
295 set_to_table_copy(b, set_ptr, set_desc_count, src_desc_idx,
296 table_ptr, nir_isub(b, desc_copy_idx, img_end),
297 sizeof(struct mali_texture_packed));
298 }
299 nir_push_else(b, NULL);
300 {
301 nir_push_if(b, nir_ult(b, desc_copy_idx, sampler_end));
302 {
303 nir_def *table_ptr =
304 get_input_field(b, tables[PANVK_BIFROST_DESC_TABLE_SAMPLER]);
305
306 set_to_table_copy(b, set_ptr, set_desc_count, src_desc_idx,
307 table_ptr, nir_isub(b, desc_copy_idx, tex_end),
308 sizeof(struct mali_sampler_packed));
309 }
310 nir_pop_if(b, NULL);
311 }
312 nir_pop_if(b, NULL);
313 }
314 nir_pop_if(b, NULL);
315 }
316 nir_pop_if(b, NULL);
317 }
318
319 static uint64_t
320 panvk_meta_desc_copy_rsd(struct panvk_device *dev)
321 {
322 struct panvk_physical_device *phys_dev =
323 to_panvk_physical_device(dev->vk.physical);
324 enum panvk_meta_object_key_type key = PANVK_META_OBJECT_KEY_COPY_DESC_SHADER;
325 struct panvk_internal_shader *shader;
326
327 VkShaderEXT shader_handle = (VkShaderEXT)vk_meta_lookup_object(
328 &dev->meta, VK_OBJECT_TYPE_SHADER_EXT, &key, sizeof(key));
329 if (shader_handle != VK_NULL_HANDLE)
330 goto out;
331
332 nir_builder b = nir_builder_init_simple_shader(
333 MESA_SHADER_COMPUTE,
334 pan_get_nir_shader_compiler_options(
335 pan_arch(phys_dev->kmod.dev->props.gpu_id), false),
336 "%s", "desc_copy");
337
338 /* We actually customize that at execution time to issue the
339 * exact number of jobs. */
340 b.shader->info.workgroup_size[0] = 1;
341 b.shader->info.workgroup_size[1] = 1;
342 b.shader->info.workgroup_size[2] = 1;
343
344 nir_def *desc_copy_id =
345 nir_channel(&b, nir_load_global_invocation_id(&b, 32), 0);
346 single_desc_copy(&b, desc_copy_id);
347
348 struct pan_compile_inputs inputs = {
349 .gpu_id = phys_dev->kmod.dev->props.gpu_id,
350 .gpu_variant = phys_dev->kmod.dev->props.gpu_variant,
351 };
352
353 pan_preprocess_nir(b.shader, inputs.gpu_id);
354 pan_postprocess_nir(b.shader, inputs.gpu_id);
355
356 VkResult result = panvk_per_arch(create_internal_shader)(
357 dev, b.shader, &inputs, &shader);
358
359 ralloc_free(b.shader);
360
361 if (result != VK_SUCCESS)
362 return 0;
363
364 shader->info.push.count =
365 DIV_ROUND_UP(sizeof(struct pan_nir_desc_copy_info), 4);
366
367 shader->rsd = panvk_pool_alloc_desc(&dev->mempools.rw, RENDERER_STATE);
368 if (!panvk_priv_mem_check_alloc(shader->rsd)) {
369 vk_shader_destroy(&dev->vk, &shader->vk, NULL);
370 return 0;
371 }
372
373 panvk_priv_mem_write_desc(shader->rsd, 0, RENDERER_STATE, cfg) {
374 pan_shader_prepare_rsd(&shader->info,
375 panvk_priv_mem_dev_addr(shader->code_mem), &cfg);
376 }
377
378 shader_handle = (VkShaderEXT)vk_meta_cache_object(
379 &dev->vk, &dev->meta, &key, sizeof(key), VK_OBJECT_TYPE_SHADER_EXT,
380 (uint64_t)panvk_internal_shader_to_handle(shader));
381
382 out:
383 shader = panvk_internal_shader_from_handle(shader_handle);
384 return panvk_priv_mem_dev_addr(shader->rsd);
385 }
386
387 VkResult
388 panvk_per_arch(meta_get_copy_desc_job)(
389 struct panvk_cmd_buffer *cmdbuf, const struct panvk_shader_variant *shader,
390 const struct panvk_descriptor_state *desc_state,
391 const struct panvk_shader_desc_state *shader_desc_state,
392 uint32_t attrib_buf_idx_offset, struct pan_ptr *job_desc)
393 {
394 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
395
396 *job_desc = (struct pan_ptr){0};
397
398 if (!shader)
399 return VK_SUCCESS;
400
401 uint64_t copy_table = panvk_priv_mem_dev_addr(shader->desc_info.others.map);
402 if (!copy_table)
403 return VK_SUCCESS;
404
405 struct pan_nir_desc_copy_info copy_info = {
406 .img_attrib_table = shader_desc_state->img_attrib_table,
407 .desc_copy =
408 {
409 .table = copy_table,
410 .attrib_buf_idx_offset = attrib_buf_idx_offset,
411 },
412 };
413
414 for (uint32_t i = 0; i < ARRAY_SIZE(copy_info.desc_copy.limits); i++)
415 copy_info.desc_copy.limits[i] =
416 shader->desc_info.others.count[i] +
417 (i > 0 ? copy_info.desc_copy.limits[i - 1] : 0);
418
419 for (uint32_t i = 0; i < ARRAY_SIZE(desc_state->sets); i++) {
420 const struct panvk_descriptor_set *set = desc_state->sets[i];
421
422 if (!set)
423 continue;
424
425 copy_info.sets[i] = set->descs.dev;
426 copy_info.set_desc_counts[i] = set->desc_count;
427 }
428
429 for (uint32_t i = 0; i < ARRAY_SIZE(shader->desc_info.others.count); i++) {
430 uint32_t desc_count = shader->desc_info.others.count[i];
431
432 if (!desc_count)
433 continue;
434
435 copy_info.tables[i] = shader_desc_state->tables[i];
436 }
437
438 uint64_t desc_copy_rsd = panvk_meta_desc_copy_rsd(dev);
439 if (!desc_copy_rsd)
440 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
441
442 struct pan_ptr push_uniforms =
443 panvk_cmd_alloc_dev_mem(cmdbuf, desc, sizeof(copy_info), 16);
444
445 if (!push_uniforms.gpu)
446 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
447
448 memcpy(push_uniforms.cpu, &copy_info, sizeof(copy_info));
449
450 *job_desc = panvk_cmd_alloc_desc(cmdbuf, COMPUTE_JOB);
451 if (!job_desc->gpu)
452 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
453
454 /* Given the per-stage max descriptors limit, we should never
455 * reach the workgroup dimension limit. */
456 uint32_t copy_count =
457 copy_info.desc_copy.limits[PANVK_BIFROST_DESC_TABLE_COUNT - 1];
458
459 assert(copy_count - 1 < BITFIELD_MASK(10));
460
461 pan_pack_work_groups_compute(
462 pan_section_ptr(job_desc->cpu, COMPUTE_JOB, INVOCATION), 1, 1, 1,
463 copy_count, 1, 1, false, false);
464
465 pan_section_pack(job_desc->cpu, COMPUTE_JOB, PARAMETERS, cfg) {
466 cfg.job_task_split = util_logbase2_ceil(copy_count + 1) +
467 util_logbase2_ceil(1 + 1) +
468 util_logbase2_ceil(1 + 1);
469 }
470
471 struct pan_tls_info tlsinfo = {0};
472 struct pan_ptr tls = panvk_cmd_alloc_desc(cmdbuf, LOCAL_STORAGE);
473 if (!tls.gpu)
474 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
475
476 GENX(pan_emit_tls)(&tlsinfo, tls.cpu);
477
478 pan_section_pack(job_desc->cpu, COMPUTE_JOB, DRAW, cfg) {
479 cfg.state = desc_copy_rsd,
480 cfg.push_uniforms = push_uniforms.gpu;
481 cfg.thread_storage = tls.gpu;
482 }
483
484 return VK_SUCCESS;
485 }