Debian Codegraph @

src/panfrost/vulkan/panvk_vX_shader.c

raw
1 /*
2 * Copyright © 2025 Arm Ltd.
3 * Copyright © 2021 Collabora Ltd.
4 *
5 * Derived from tu_shader.c which is:
6 * Copyright © 2019 Google LLC
7 *
8 * Also derived from anv_pipeline.c which is
9 * Copyright © 2015 Intel Corporation
10 *
11 * SPDX-License-Identifier: MIT
12 */
13
14 #include "genxml/gen_macros.h"
15
16 #include "panvk_cmd_buffer.h"
17 #include "panvk_descriptor_set_layout.h"
18 #include "panvk_device.h"
19 #include "panvk_instance.h"
20 #include "panvk_mempool.h"
21 #include "panvk_physical_device.h"
22 #include "panvk_sampler.h"
23 #include "panvk_shader.h"
24
25 #include "spirv/nir_spirv.h"
26 #include "util/memstream.h"
27 #include "util/mesa-blake3.h"
28 #include "util/shader_stats.h"
29 #include "util/u_dynarray.h"
30 #include "nir_builder.h"
31 #include "nir_conversion_builder.h"
32 #include "nir_deref.h"
33
34 #include "shader_enums.h"
35 #include "vk_graphics_state.h"
36 #include "vk_nir_convert_ycbcr.h"
37 #include "vk_shader_module.h"
38 #include "vk_ycbcr_conversion.h"
39
40 #include "compiler/bifrost/bifrost_nir.h"
41 #include "compiler/bifrost/bifrost_compile.h"
42 #include "compiler/pan_compiler.h"
43 #include "compiler/pan_nir.h"
44 #include "pan_shader.h"
45
46 #include "vk_log.h"
47 #include "vk_pipeline.h"
48 #include "vk_pipeline_layout.h"
49 #include "vk_shader.h"
50 #include "vk_util.h"
51
52 #define FAU_WORD_COUNT 64
53
54 struct panvk_lower_sysvals_context {
55 struct panvk_shader_variant *shader;
56 const struct vk_graphics_pipeline_state *state;
57 };
58
59 static bool
60 panvk_lower_sysvals(nir_builder *b, nir_instr *instr, void *data)
61 {
62 if (instr->type != nir_instr_type_intrinsic)
63 return false;
64
65 const struct panvk_lower_sysvals_context *ctx = data;
66 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
67 unsigned bit_size = intr->def.bit_size;
68 nir_def *val = NULL;
69 b->cursor = nir_before_instr(instr);
70
71 switch (intr->intrinsic) {
72 case nir_intrinsic_load_base_workgroup_id:
73 val = load_sysval(b, compute, bit_size, base);
74 break;
75 case nir_intrinsic_load_num_workgroups:
76 val = load_sysval(b, compute, bit_size, num_work_groups);
77 break;
78 case nir_intrinsic_load_workgroup_size:
79 val = load_sysval(b, compute, bit_size, local_group_size);
80 break;
81 case nir_intrinsic_load_viewport_scale:
82 val = load_sysval(b, graphics, bit_size, viewport.scale);
83 break;
84 case nir_intrinsic_load_viewport_offset:
85 val = load_sysval(b, graphics, bit_size, viewport.offset);
86 break;
87 case nir_intrinsic_load_first_vertex:
88 val = load_sysval(b, graphics, bit_size, vs.first_vertex);
89 break;
90 case nir_intrinsic_load_base_instance:
91 val = load_sysval(b, graphics, bit_size, vs.base_instance);
92 break;
93 case nir_intrinsic_load_noperspective_varyings_pan:
94 /* TODO: use a VS epilog specialized on constant noperspective_varyings
95 * with VK_EXT_graphics_pipeline_libraries and VK_EXT_shader_object */
96 assert(b->shader->info.stage == MESA_SHADER_VERTEX);
97 val = load_sysval(b, graphics, bit_size, vs.noperspective_varyings);
98 break;
99
100 #if PAN_ARCH < 9
101 case nir_intrinsic_load_raw_vertex_offset_pan:
102 val = load_sysval(b, graphics, bit_size, vs.raw_vertex_offset);
103 break;
104 case nir_intrinsic_load_layer_id:
105 assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
106 val = load_sysval(b, graphics, bit_size, layer_id);
107 break;
108 case nir_intrinsic_load_view_index:
109 assert(b->shader->info.stage != MESA_SHADER_COMPUTE);
110 if (ctx->state->mv->view_mask == 0)
111 val = nir_imm_zero(b, 1, 32);
112 else
113 val = load_sysval(b, graphics, bit_size, layer_id);
114 break;
115 #endif
116
117 case nir_intrinsic_load_draw_id:
118 /* Multidraw is supported on v10. */
119 if (PAN_ARCH >= 10)
120 return false;
121
122 /* TODO: We only implement single-draw direct and indirect draws, so this
123 * is sufficient. We'll revisit this when we get around to implementing
124 * multidraw. */
125 assert(b->shader->info.stage == MESA_SHADER_VERTEX);
126 val = nir_imm_int(b, 0);
127 break;
128
129 case nir_intrinsic_load_printf_buffer_address:
130 val = load_sysval(b, common, bit_size, printf_buffer_address);
131 break;
132
133 case nir_intrinsic_load_blend_descriptor_pan: {
134 uint32_t loc = nir_intrinsic_base(intr);
135 val = load_sysval(b, graphics, bit_size, fs.blend_descs[loc]);
136 break;
137 }
138
139 case nir_intrinsic_load_input_attachment_target_pan: {
140 const struct vk_input_attachment_location_state *ial =
141 ctx->state ? ctx->state->ial : NULL;
142
143 if (ial && nir_src_is_const(intr->src[0])) {
144 uint32_t index = nir_src_as_uint(intr->src[0]);
145 uint32_t depth_idx = ial->depth_att == MESA_VK_ATTACHMENT_NO_INDEX
146 ? 0
147 : ial->depth_att + 1;
148 uint32_t stencil_idx = ial->stencil_att == MESA_VK_ATTACHMENT_NO_INDEX
149 ? 0
150 : ial->stencil_att + 1;
151 uint32_t target = ~0;
152
153 if (depth_idx == index || stencil_idx == index) {
154 target = PANVK_ZS_ATTACHMENT;
155 } else {
156 for (unsigned i = 0; i < ial->color_attachment_count; i++) {
157 if (ial->color_map[i] == MESA_VK_ATTACHMENT_UNUSED)
158 continue;
159
160 if (ial->color_map[i] + 1 == index) {
161 target = PANVK_COLOR_ATTACHMENT(i);
162 break;
163 }
164 }
165 }
166
167 val = nir_imm_int(b, target);
168 } else {
169 nir_def *ia_info =
170 load_sysval_entry(b, graphics, bit_size, iam, intr->src[0].ssa);
171
172 val = nir_channel(b, ia_info, 0);
173 }
174 break;
175 }
176
177 case nir_intrinsic_load_input_attachment_conv_pan: {
178 nir_def *ia_info =
179 load_sysval_entry(b, graphics, bit_size, iam, intr->src[0].ssa);
180
181 val = nir_channel(b, ia_info, 1);
182 break;
183 }
184
185 case nir_intrinsic_load_ro_sink_address_poly:
186 val = nir_imm_int64(b, PAN_SHADER_OOB_ADDRESS);
187 break;
188
189 default:
190 return false;
191 }
192
193 assert(val->num_components == intr->def.num_components);
194
195 b->cursor = nir_after_instr(instr);
196 nir_def_rewrite_uses(&intr->def, val);
197 return true;
198 }
199
200 static bool
201 panvk_lower_load_vs_input(nir_builder *b, nir_intrinsic_instr *intrin,
202 UNUSED void *data)
203 {
204 if (intrin->intrinsic != nir_intrinsic_load_input)
205 return false;
206
207 b->cursor = nir_before_instr(&intrin->instr);
208 nir_def *ld_attr = nir_load_attribute_pan(
209 b, intrin->def.num_components, intrin->def.bit_size,
210 PAN_ARCH < 9 ?
211 nir_load_raw_vertex_id_pan(b) :
212 nir_load_vertex_id(b),
213 nir_load_instance_id(b),
214 nir_get_io_offset_src(intrin)->ssa,
215 .base = nir_intrinsic_base(intrin),
216 .component = nir_intrinsic_component(intrin),
217 .dest_type = nir_intrinsic_dest_type(intrin));
218 nir_def_replace(&intrin->def, ld_attr);
219
220 return true;
221 }
222
223 #if PAN_ARCH < 9
224 static bool
225 lower_gl_pos_layer_writes(nir_builder *b, nir_instr *instr, void *data)
226 {
227 if (instr->type != nir_instr_type_intrinsic)
228 return false;
229
230 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
231
232 if (intr->intrinsic != nir_intrinsic_copy_deref)
233 return false;
234
235 nir_variable *dst_var = nir_intrinsic_get_var(intr, 0);
236 nir_variable *src_var = nir_intrinsic_get_var(intr, 1);
237
238 if (!dst_var || dst_var->data.mode != nir_var_shader_out || !src_var ||
239 src_var->data.mode != nir_var_shader_temp)
240 return false;
241
242 if (dst_var->data.location == VARYING_SLOT_LAYER) {
243 /* We don't really write the layer, we just make sure primitives are
244 * discarded if gl_Layer doesn't match the layer passed to the draw.
245 */
246 b->cursor = nir_instr_remove(instr);
247 return true;
248 }
249
250 if (dst_var->data.location == VARYING_SLOT_POS) {
251 nir_variable *temp_layer_var = data;
252 nir_variable *temp_pos_var = src_var;
253
254 b->cursor = nir_before_instr(instr);
255 nir_def *layer = nir_load_var(b, temp_layer_var);
256 nir_def *pos = nir_load_var(b, temp_pos_var);
257 nir_def *inf_pos = nir_imm_vec4(b, INFINITY, INFINITY, INFINITY, 1.0f);
258 nir_def *ref_layer = load_sysval(b, graphics, 32, layer_id);
259
260 nir_store_var(b, temp_pos_var,
261 nir_bcsel(b, nir_ieq(b, layer, ref_layer), pos, inf_pos),
262 0xf);
263 return true;
264 }
265
266 return false;
267 }
268
269 static bool
270 lower_layer_writes(nir_shader *nir)
271 {
272 if (nir->info.stage == MESA_SHADER_FRAGMENT)
273 return false;
274
275 nir_variable *temp_layer_var = NULL;
276 bool has_layer_var = false;
277
278 nir_foreach_variable_with_modes(var, nir,
279 nir_var_shader_out | nir_var_shader_temp) {
280 if (var->data.mode == nir_var_shader_out &&
281 var->data.location == VARYING_SLOT_LAYER)
282 has_layer_var = true;
283
284 if (var->data.mode == nir_var_shader_temp &&
285 var->data.location == VARYING_SLOT_LAYER)
286 temp_layer_var = var;
287 }
288
289 if (!has_layer_var)
290 return false;
291
292 assert(temp_layer_var);
293
294 return nir_shader_instructions_pass(nir, lower_gl_pos_layer_writes,
295 nir_metadata_control_flow,
296 temp_layer_var);
297 }
298 #endif
299
300 #if PAN_ARCH >= 10
301 static bool
302 mark_all_access_non_uniform(nir_builder *b, nir_instr *instr, void *data)
303 {
304 switch (instr->type) {
305 case nir_instr_type_tex: {
306 nir_tex_instr *tex = nir_instr_as_tex(instr);
307
308 for (unsigned i = 0; i < tex->num_srcs; i++) {
309 switch (tex->src[i].src_type) {
310 case nir_tex_src_texture_offset:
311 case nir_tex_src_texture_handle:
312 tex->texture_non_uniform = true;
313 break;
314
315 case nir_tex_src_sampler_offset:
316 case nir_tex_src_sampler_handle:
317 tex->sampler_non_uniform = true;
318 break;
319
320 case nir_tex_src_offset:
321 tex->offset_non_uniform = true;
322 break;
323
324 default:
325 break;
326 }
327 }
328
329 return true;
330 }
331 case nir_instr_type_intrinsic: {
332 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
333
334 switch (intrin->intrinsic) {
335 case nir_intrinsic_load_ubo:
336 case nir_intrinsic_load_ssbo:
337 case nir_intrinsic_store_ssbo:
338 case nir_intrinsic_ssbo_atomic:
339 case nir_intrinsic_ssbo_atomic_swap:
340 case nir_intrinsic_image_load:
341 case nir_intrinsic_image_sparse_load:
342 case nir_intrinsic_image_store:
343 case nir_intrinsic_image_atomic:
344 case nir_intrinsic_image_atomic_swap:
345 case nir_intrinsic_image_size:
346 case nir_intrinsic_image_samples:
347 case nir_intrinsic_image_deref_load:
348 case nir_intrinsic_image_deref_sparse_load:
349 case nir_intrinsic_image_deref_store:
350 case nir_intrinsic_image_deref_atomic:
351 case nir_intrinsic_image_deref_atomic_swap:
352 case nir_intrinsic_image_deref_levels:
353 case nir_intrinsic_image_deref_size:
354 case nir_intrinsic_image_deref_samples:
355 case nir_intrinsic_image_deref_samples_identical:
356 nir_intrinsic_set_access(
357 intrin, nir_intrinsic_access(intrin) | ACCESS_NON_UNIFORM);
358 return true;
359 default:
360 return false;
361 }
362 }
363 default:
364 return false;
365 }
366 }
367 #endif
368
369 static void
370 shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align)
371 {
372 assert(glsl_type_is_vector_or_scalar(type));
373
374 uint32_t comp_size =
375 glsl_type_is_boolean(type) ? 4 : glsl_get_bit_size(type) / 8;
376 unsigned length = glsl_get_vector_elements(type);
377 *size = comp_size * length, *align = comp_size * (length == 3 ? 4 : length);
378 }
379
380 static inline nir_address_format
381 panvk_buffer_ubo_addr_format(VkPipelineRobustnessBufferBehaviorEXT robustness)
382 {
383 switch (robustness) {
384 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT:
385 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_EXT:
386 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_2_EXT:
387 return PAN_ARCH < 9 ? nir_address_format_32bit_index_offset
388 : nir_address_format_vec2_index_32bit_offset;
389 default:
390 UNREACHABLE("Invalid robust buffer access behavior");
391 }
392 }
393
394 static inline nir_address_format
395 panvk_buffer_ssbo_addr_format(VkPipelineRobustnessBufferBehaviorEXT robustness)
396 {
397 switch (robustness) {
398 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT:
399 return PAN_ARCH < 9 ? nir_address_format_64bit_global_32bit_offset
400 : nir_address_format_vec2_index_32bit_offset;
401 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_EXT:
402 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_2_EXT:
403 return PAN_ARCH < 9 ? nir_address_format_64bit_bounded_global
404 : nir_address_format_vec2_index_32bit_offset;
405 default:
406 UNREACHABLE("Invalid robust buffer access behavior");
407 }
408 }
409
410 static const nir_shader_compiler_options *
411 panvk_get_nir_options(UNUSED struct vk_physical_device *vk_pdev,
412 UNUSED mesa_shader_stage stage,
413 UNUSED const struct vk_pipeline_robustness_state *rs)
414 {
415 struct panvk_physical_device *phys_dev = to_panvk_physical_device(vk_pdev);
416 return pan_get_nir_shader_compiler_options(
417 pan_arch(phys_dev->kmod.dev->props.gpu_id), false);
418 }
419
420 static struct spirv_to_nir_options
421 panvk_get_spirv_options(UNUSED struct vk_physical_device *vk_pdev,
422 UNUSED mesa_shader_stage stage,
423 const struct vk_pipeline_robustness_state *rs)
424 {
425 return (struct spirv_to_nir_options){
426 .ubo_addr_format = panvk_buffer_ubo_addr_format(rs->uniform_buffers),
427 .ssbo_addr_format = panvk_buffer_ssbo_addr_format(rs->storage_buffers),
428 .phys_ssbo_addr_format = nir_address_format_64bit_global,
429 .shared_addr_format = nir_address_format_32bit_offset,
430 .min_ubo_alignment = 16,
431 .min_ssbo_alignment = 16,
432 .debug_info = pan_want_debug_info(PAN_ARCH),
433 };
434 }
435
436 static void
437 panvk_preprocess_nir(struct vk_physical_device *vk_pdev,
438 nir_shader *nir,
439 UNUSED const struct vk_pipeline_robustness_state *rs)
440 {
441 struct panvk_physical_device *pdev = to_panvk_physical_device(vk_pdev);
442
443 /* Ensure to regroup output variables at the same location */
444 if (nir->info.stage == MESA_SHADER_FRAGMENT)
445 NIR_PASS(_, nir, nir_opt_vectorize_io_vars, nir_var_shader_out);
446
447 NIR_PASS(_, nir, nir_lower_io_vars_to_temporaries,
448 nir_shader_get_entrypoint(nir), nir_var_shader_out);
449
450 #if PAN_ARCH < 9
451 /* This needs to be done just after the io_to_temporaries pass, because we
452 * rely on out temporaries to collect the final layer_id value.
453 */
454 NIR_PASS(_, nir, lower_layer_writes);
455 #endif
456
457 NIR_PASS(_, nir, nir_lower_global_vars_to_local);
458 NIR_PASS(_, nir, nir_split_var_copies);
459
460 NIR_PASS(_, nir, nir_opt_copy_prop_vars);
461 NIR_PASS(_, nir, nir_opt_combine_stores, nir_var_all);
462 NIR_PASS(_, nir, nir_opt_loop);
463
464 NIR_PASS(_, nir, nir_opt_barrier_modes);
465 NIR_PASS(_, nir, nir_opt_acquire_release_barriers, SCOPE_DEVICE);
466
467 /* Do texture lowering here. We need to lower texture stuff
468 * now, before we call panvk_per_arch(nir_lower_descriptors)() because some
469 * of the texture lowering generates nir_texop_txs which we handle as part
470 * of descriptor lowering.
471 *
472 * TODO: We really should be doing this in common code, not duplicated in
473 * panvk. In order to do that, we need to rework the panfrost compile
474 * flow to look more like the Intel flow:
475 *
476 * 1. Compile SPIR-V to NIR and maybe do a tiny bit of lowering that needs
477 * to be done really early.
478 *
479 * 2. pan_preprocess_nir: Does common lowering and runs the optimization
480 * loop. Nothing here should be API-specific.
481 *
482 * 3. Do additional lowering in panvk
483 *
484 * 4. pan_postprocess_nir: Does final lowering and runs the optimization
485 * loop again. This can happen as part of the final compile.
486 *
487 * This would give us a better place to do panvk-specific lowering.
488 */
489 NIR_PASS(_, nir, nir_lower_system_values);
490
491 nir_lower_compute_system_values_options options = {
492 .has_base_workgroup_id = true,
493 };
494
495 NIR_PASS(_, nir, nir_lower_compute_system_values, &options);
496
497 if (nir->info.stage == MESA_SHADER_FRAGMENT)
498 NIR_PASS(_, nir, nir_lower_wpos_center);
499
500 pan_optimize_nir(nir, pdev->kmod.dev->props.gpu_id);
501
502 NIR_PASS(_, nir, nir_split_var_copies);
503 NIR_PASS(_, nir, nir_lower_var_copies);
504
505 assert(pdev->kmod.dev->props.shader_present != 0);
506 uint64_t core_max_id =
507 util_last_bit(pdev->kmod.dev->props.shader_present) - 1;
508 NIR_PASS(_, nir, nir_inline_sysval, nir_intrinsic_load_core_max_id_arm,
509 core_max_id);
510
511 pan_preprocess_nir(nir, pdev->kmod.dev->props.gpu_id);
512 }
513
514 static void
515 panvk_hash_state(struct vk_physical_device *device,
516 const struct vk_graphics_pipeline_state *state,
517 const struct vk_features *enabled_features,
518 VkShaderStageFlags stages, blake3_hash blake3_out)
519 {
520 struct mesa_blake3 blake3_ctx;
521 _mesa_blake3_init(&blake3_ctx);
522
523 if (state != NULL) {
524 /* This doesn't impact the shader compile but it does go in the
525 * panvk_shader and gets [de]serialized along with the binary so
526 * we need to hash it.
527 */
528 bool sample_shading_enable =
529 state->ms && state->ms->sample_shading_enable;
530 _mesa_blake3_update(&blake3_ctx, &sample_shading_enable,
531 sizeof(sample_shading_enable));
532
533 _mesa_blake3_update(&blake3_ctx, &state->mv->view_mask,
534 sizeof(state->mv->view_mask));
535
536 if (state->ial)
537 _mesa_blake3_update(&blake3_ctx, state->ial, sizeof(*state->ial));
538 }
539
540 _mesa_blake3_final(&blake3_ctx, blake3_out);
541 }
542
543 #if PAN_ARCH >= 9
544 static bool
545 valhall_pack_buf_idx(nir_builder *b, nir_instr *instr, UNUSED void *data)
546 {
547 if (instr->type != nir_instr_type_intrinsic)
548 return false;
549
550 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
551 unsigned index_src;
552
553 switch (intrin->intrinsic) {
554 case nir_intrinsic_load_ubo:
555 case nir_intrinsic_load_ssbo:
556 case nir_intrinsic_ssbo_atomic:
557 case nir_intrinsic_ssbo_atomic_swap:
558 index_src = 0;
559 break;
560
561 case nir_intrinsic_store_ssbo:
562 index_src = 1;
563 break;
564
565 default:
566 return false;
567 }
568
569 nir_def *index = intrin->src[index_src].ssa;
570
571 /* The descriptor lowering pass can add UBO loads, and those already have the
572 * right index format. */
573 if (index->num_components == 1)
574 return false;
575
576 b->cursor = nir_before_instr(&intrin->instr);
577
578 /* The valhall backend expects nir_address_format_32bit_index_offset,
579 * but address mode is nir_address_format_vec2_index_32bit_offset to allow
580 * us to store the array size, set and index without losing information
581 * while walking the descriptor deref chain (needed to do a bound check on
582 * the array index when we reach the end of the chain).
583 * Turn it back to nir_address_format_32bit_index_offset after IOs
584 * have been lowered. */
585 nir_def *packed_index =
586 nir_iadd(b, nir_channel(b, index, 0), nir_channel(b, index, 1));
587 nir_src_rewrite(&intrin->src[index_src], packed_index);
588 return true;
589 }
590 #endif
591
592 static bool
593 valhall_lower_get_ssbo_size(struct nir_builder *b,
594 nir_intrinsic_instr *intr, void *data)
595 {
596 if (intr->intrinsic != nir_intrinsic_get_ssbo_size)
597 return false;
598
599 b->cursor = nir_before_instr(&intr->instr);
600
601 nir_def *res_handle = nir_channel(b, intr->src[0].ssa, 0);
602 nir_def *table_idx = nir_ushr_imm(b, res_handle, 24);
603 nir_def *res_idx = nir_iand_imm(b, res_handle, BITFIELD_MASK(24));
604 nir_def *res_table = nir_ior_imm(b, table_idx, pan_res_handle(62, 0));
605 nir_def *buf_idx = nir_iadd(b, res_idx, nir_channel(b, intr->src[0].ssa, 1));
606 nir_def *desc_offset = nir_imul_imm(b, buf_idx, PANVK_DESCRIPTOR_SIZE);
607 nir_def *size = nir_load_ubo(
608 b, 1, 32, res_table, nir_iadd_imm(b, desc_offset, 4), .range = ~0u,
609 .align_mul = PANVK_DESCRIPTOR_SIZE, .align_offset = 4);
610
611 nir_def_replace(&intr->def, size);
612 return true;
613 }
614
615 static bool
616 collect_push_constant(struct nir_builder *b, nir_intrinsic_instr *intr,
617 void *data)
618 {
619 if (intr->intrinsic != nir_intrinsic_load_push_constant)
620 return false;
621
622 struct panvk_shader_variant *shader = data;
623 uint32_t base = nir_intrinsic_base(intr);
624 bool is_sysval = base >= SYSVALS_PUSH_CONST_BASE;
625 uint32_t offset, size;
626
627 if (is_sysval)
628 base -= SYSVALS_PUSH_CONST_BASE;
629
630 /* If the offset is dynamic, we need to flag [base:base+range] as used, to
631 * allow global mem access. */
632 if (!nir_src_is_const(intr->src[0])) {
633 offset = base;
634 size = nir_intrinsic_range(intr);
635
636 /* Flag the push_uniforms sysval as needed if we have an indirect offset.
637 */
638 shader_use_sysval(shader, common, push_uniforms);
639 } else {
640 offset = base + nir_src_as_uint(intr->src[0]);
641 size = (intr->def.bit_size / 8) * intr->def.num_components;
642 }
643
644 if (is_sysval)
645 shader_use_sysval_range(shader, offset, size);
646 else
647 shader_use_push_const_range(shader, offset, size);
648
649 return true;
650 }
651
652 static bool
653 move_push_constant(struct nir_builder *b, nir_intrinsic_instr *intr, void *data)
654 {
655 if (intr->intrinsic != nir_intrinsic_load_push_constant)
656 return false;
657
658 struct panvk_shader_variant *shader = data;
659 unsigned base = nir_intrinsic_base(intr);
660 bool is_sysval = base >= SYSVALS_PUSH_CONST_BASE;
661
662 if (is_sysval)
663 base -= SYSVALS_PUSH_CONST_BASE;
664
665 b->cursor = nir_before_instr(&intr->instr);
666
667 if (nir_src_is_const(intr->src[0])) {
668 unsigned offset = base + nir_src_as_uint(intr->src[0]);
669
670 /* We place the sysvals first, and then comes the user push constants.
671 * We do that so we always have the blend constants at offset 0 for
672 * blend shaders. */
673 if (is_sysval)
674 offset = shader_remapped_sysval_offset(shader, offset);
675 else
676 offset = shader_remapped_push_const_offset(shader, offset);
677
678 nir_src_rewrite(&intr->src[0], nir_imm_int(b, offset));
679
680 /* We always set the range/base to zero, to make sure no pass is using it
681 * after that point. */
682 nir_intrinsic_set_base(intr, 0);
683 nir_intrinsic_set_range(intr, 0);
684 } else {
685 /* We don't use load_sysval() on purpose, because it would set
686 * .base=SYSVALS_PUSH_CONST_BASE, and we're supposed to force a base of
687 * zero in this pass. */
688 unsigned push_const_buf_offset = shader_remapped_sysval_offset(
689 shader, sysval_offset(common, push_uniforms));
690 nir_def *push_const_buf = nir_load_push_constant(
691 b, 1, 64, nir_imm_int(b, push_const_buf_offset));
692 unsigned push_const_offset = is_sysval ?
693 shader_remapped_sysval_offset(shader, base) :
694 shader_remapped_push_const_offset(shader, base);
695 nir_def *offset = nir_iadd_imm(b, intr->src[0].ssa, push_const_offset);
696 unsigned align = nir_combined_align(nir_intrinsic_align_mul(intr),
697 nir_intrinsic_align_offset(intr));
698
699 /* We assume an alignment of 64-bit max for packed push-constants. */
700 align = MIN2(align, FAU_WORD_SIZE);
701 nir_def *value = nir_load_global(
702 b, intr->def.num_components, intr->def.bit_size,
703 nir_iadd(b, push_const_buf, nir_u2u64(b, offset)), .align_mul = align);
704
705 nir_def_replace(&intr->def, value);
706 }
707
708 return true;
709 }
710
711 static void
712 lower_load_push_consts(nir_shader *nir, struct panvk_shader_variant *shader)
713 {
714 /* Before we lower load_push_constant()s with a dynamic offset to global
715 * loads, we want to run a few optimization passes to get rid of offset
716 * calculation involving only constant values. */
717 bool progress = false;
718 do {
719 progress = false;
720 NIR_PASS(progress, nir, nir_opt_copy_prop);
721 NIR_PASS(progress, nir, nir_opt_remove_phis);
722 NIR_PASS(progress, nir, nir_opt_dce);
723 NIR_PASS(progress, nir, nir_opt_dead_cf);
724 NIR_PASS(progress, nir, nir_opt_cse);
725
726 nir_opt_peephole_select_options peephole_select_options = {
727 .limit = 64,
728 .expensive_alu_ok = true,
729 };
730 NIR_PASS(progress, nir, nir_opt_peephole_select, &peephole_select_options);
731 NIR_PASS(progress, nir, nir_opt_algebraic);
732 NIR_PASS(progress, nir, nir_opt_constant_folding);
733 } while (progress);
734
735 /* We always reserve the 4 blend constant words for fragment shaders,
736 * because we don't know the blend configuration at this point, and
737 * we might end up with a blend shader reading those blend constants. */
738 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
739 /* We rely on blend constants being placed first and covering 4 words. */
740 STATIC_ASSERT(
741 offsetof(struct panvk_graphics_sysvals, blend.constants) == 0 &&
742 sizeof(((struct panvk_graphics_sysvals *)NULL)->blend.constants) ==
743 16);
744
745 shader_use_sysval(shader, graphics, blend.constants);
746 }
747
748 progress = false;
749 NIR_PASS(progress, nir, nir_shader_intrinsics_pass, collect_push_constant,
750 nir_metadata_all, shader);
751
752 /* Some load_push_constant instructions might be eliminated after
753 * scalarization+dead-code-elimination. Since these pass happen in
754 * bifrost_compile(), we can't run the push_constant packing after the
755 * optimization took place, so let's just have our own FAU count instead
756 * of using info.push.count to make it consistent with the
757 * used_{sysvals,push_consts} bitmaps, even if it sometimes implies loading
758 * more than we really need. Doing that also takes into account the fact
759 * blend constants are never loaded from the fragment shader, but might be
760 * needed in the blend shader. */
761 shader->fau.sysval_count = BITSET_COUNT(shader->fau.used_sysvals);
762 /* 32 FAUs (256 bytes) are reserved for API push constants */
763 assert(shader->fau.sysval_count <= FAU_WORD_COUNT - 32 &&
764 "too many sysval FAUs");
765 shader->fau.total_count =
766 shader->fau.sysval_count + BITSET_COUNT(shader->fau.used_push_consts);
767 assert(shader->fau.total_count <= FAU_WORD_COUNT &&
768 "asking for more FAUs than the hardware has to offer");
769
770 if (!progress)
771 return;
772
773 NIR_PASS(_, nir, nir_shader_intrinsics_pass, move_push_constant,
774 nir_metadata_control_flow, shader);
775 }
776
777 struct lower_ycbcr_state {
778 uint32_t set_layout_count;
779 struct vk_descriptor_set_layout *const *set_layouts;
780 };
781
782 static const struct vk_ycbcr_conversion_state *
783 lookup_ycbcr_conversion(const void *_state, uint32_t set,
784 uint32_t binding, uint32_t array_index)
785 {
786 const struct lower_ycbcr_state *state = _state;
787 assert(set < state->set_layout_count);
788 assert(state->set_layouts[set] != NULL);
789 const struct panvk_descriptor_set_layout *set_layout =
790 to_panvk_descriptor_set_layout(state->set_layouts[set]);
791 assert(binding < set_layout->binding_count);
792
793 const struct panvk_descriptor_set_binding_layout *bind_layout =
794 &set_layout->bindings[binding];
795
796 if (bind_layout->immutable_samplers == NULL)
797 return NULL;
798
799 array_index = MIN2(array_index, bind_layout->desc_count - 1);
800
801 const struct panvk_sampler *sampler =
802 bind_layout->immutable_samplers[array_index];
803
804 return sampler && sampler->vk.ycbcr_conversion ?
805 &sampler->vk.ycbcr_conversion->state : NULL;
806 }
807
808 static int
809 glsl_type_size(const struct glsl_type *type, bool bindless)
810 {
811 return glsl_count_attribute_slots(type, false);
812 }
813
814 static void
815 panvk_lower_nir(struct panvk_device *dev, nir_shader *nir,
816 uint32_t set_layout_count,
817 struct vk_descriptor_set_layout *const *set_layouts,
818 const struct vk_pipeline_robustness_state *rs,
819 const struct vk_graphics_pipeline_state *state,
820 struct panvk_shader_desc_info *desc_info,
821 bool allow_merging_workgroups)
822 {
823 mesa_shader_stage stage = nir->info.stage;
824
825 const nir_opt_access_options access_options = {
826 .is_vulkan = true,
827 };
828 NIR_PASS(_, nir, nir_opt_access, &access_options);
829
830 const struct lower_ycbcr_state ycbcr_state = {
831 .set_layout_count = set_layout_count,
832 .set_layouts = set_layouts,
833 };
834 NIR_PASS(_, nir, nir_vk_lower_ycbcr_tex, lookup_ycbcr_conversion,
835 &ycbcr_state);
836
837 /* We need to do this before nir_lower_descriptors so any image_deref_size
838 * intrinsics generated can be lowered there.
839 */
840 if (PAN_ARCH < 9)
841 NIR_PASS(_, nir, pan_nir_lower_image_ms);
842
843 panvk_per_arch(nir_lower_descriptors)(nir, dev, rs, set_layout_count,
844 set_layouts, state, desc_info);
845
846 NIR_PASS(_, nir, nir_split_var_copies);
847 NIR_PASS(_, nir, nir_lower_var_copies);
848 NIR_PASS(_, nir, nir_lower_memcpy);
849
850 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_ubo,
851 panvk_buffer_ubo_addr_format(rs->uniform_buffers));
852 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_ssbo,
853 panvk_buffer_ssbo_addr_format(rs->storage_buffers));
854 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_push_const,
855 nir_address_format_32bit_offset);
856 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_global,
857 nir_address_format_64bit_global);
858
859 #if PAN_ARCH >= 10
860 if (allow_merging_workgroups) {
861 /* Accesses that were uniform in the source shader may now be
862 * nonuniform. To handle this, we just flag everything as nonuniform and
863 * then let nir_opt_non_uniform_access figure out which ones can really
864 * be nonuniform based on divergence analysis */
865 NIR_PASS(_, nir, nir_shader_instructions_pass,
866 mark_all_access_non_uniform, nir_metadata_all, NULL);
867 }
868 #endif
869
870 /* nir_lower_non_uniform_access needs to run after lowering UBO and SSBO
871 * IO. This means we run it after nir_lower_descriptors, which reads the
872 * array indices, but it's okay because lower_descriptors treats all
873 * dynamic indices the same. */
874 enum nir_lower_non_uniform_access_type lower_non_uniform_access_types =
875 nir_lower_non_uniform_ubo_access |
876 nir_lower_non_uniform_ssbo_access |
877 nir_lower_non_uniform_texture_access |
878 nir_lower_non_uniform_texture_query |
879 nir_lower_non_uniform_image_access |
880 nir_lower_non_uniform_image_query |
881 nir_lower_non_uniform_get_ssbo_size;
882 #if PAN_ARCH < 9
883 lower_non_uniform_access_types |=
884 nir_lower_non_uniform_texture_offset_access;
885 #endif
886
887 /* In practice, most shaders do not have non-uniform-qualified accesses
888 * thus a cheaper and likely to fail check is run first. */
889 if (allow_merging_workgroups ||
890 nir_has_non_uniform_access(nir, lower_non_uniform_access_types)) {
891 NIR_PASS(_, nir, nir_opt_non_uniform_access);
892 struct nir_lower_non_uniform_access_options opts = {
893 .types = lower_non_uniform_access_types,
894 };
895 NIR_PASS(_, nir, nir_lower_non_uniform_access, &opts);
896 }
897
898 #if PAN_ARCH >= 9
899 NIR_PASS(_, nir, nir_shader_intrinsics_pass, valhall_lower_get_ssbo_size,
900 nir_metadata_control_flow, NULL);
901 NIR_PASS(_, nir, nir_shader_instructions_pass, valhall_pack_buf_idx,
902 nir_metadata_control_flow, NULL);
903 #endif
904
905 if (mesa_shader_stage_uses_workgroup(stage)) {
906 NIR_PASS(_, nir, nir_lower_vars_to_explicit_types, nir_var_mem_shared,
907 shared_type_info);
908
909 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_shared,
910 nir_address_format_32bit_offset);
911 }
912
913 if (nir->info.zero_initialize_shared_memory && nir->info.shared_size > 0) {
914 /* Align everything up to 16 bytes to take advantage of load store
915 * vectorization. */
916 nir->info.shared_size = align(nir->info.shared_size, 16);
917 NIR_PASS(_, nir, nir_zero_initialize_shared_memory, nir->info.shared_size,
918 16);
919
920 /* We need to call lower_compute_system_values again because
921 * nir_zero_initialize_shared_memory generates load_invocation_id which
922 * has to be lowered to load_invocation_index.
923 */
924 NIR_PASS(_, nir, nir_lower_compute_system_values, NULL);
925 }
926
927 /* Needed to turn shader_temp into function_temp since the backend only
928 * handles the latter for now.
929 */
930 NIR_PASS(_, nir, nir_lower_global_vars_to_local);
931
932 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
933 if (PANVK_DEBUG(NIR)) {
934 mesa_logi("translated nir:");
935 nir_log_shaderi(nir);
936 }
937 }
938
939 static void
940 panvk_lower_nir_io(nir_shader *nir)
941 {
942 NIR_PASS(_, nir, nir_lower_var_copies);
943 NIR_PASS(_, nir, nir_lower_indirect_derefs_to_if_else_trees,
944 nir_var_shader_in | nir_var_shader_out, UINT32_MAX);
945 NIR_PASS(_, nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
946 glsl_type_size, nir_lower_io_use_interpolated_input_intrinsics);
947
948 /* nir_lower_io just computes offsets based on the original deref and
949 * lower_indirect_derefs ensures that the array derefs have a constant
950 * index. Constant-fold to get us actual constants in in load/store
951 * instructions.
952 */
953 NIR_PASS(_, nir, nir_opt_constant_folding);
954 }
955
956 static VkResult
957 panvk_compile_nir(struct panvk_device *dev, nir_shader *nir,
958 VkShaderCreateFlagsEXT shader_flags,
959 const struct pan_compile_inputs *compile_input,
960 const struct vk_graphics_pipeline_state *state,
961 const uint32_t *noperspective_varyings,
962 struct panvk_shader_variant *shader)
963 {
964 const bool dump_asm =
965 shader_flags & VK_SHADER_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_MESA;
966
967 /* We're going to modify this so make our own copy to be nicer to callers */
968 struct pan_compile_inputs input = *compile_input;
969
970 if (nir->info.stage == MESA_SHADER_VERTEX)
971 NIR_PASS(_, nir, nir_shader_intrinsics_pass, panvk_lower_load_vs_input,
972 nir_metadata_control_flow, NULL);
973
974 /* since valhall, panvk_per_arch(nir_lower_descriptors) separates the
975 * driver set and the user sets, and does not need pan_nir_lower_image_index
976 */
977 if (PAN_ARCH < 9 && nir->info.stage == MESA_SHADER_VERTEX) {
978 NIR_PASS(_, nir, pan_nir_lower_image_index, MAX_VS_ATTRIBS);
979 NIR_PASS(_, nir, pan_nir_lower_texel_buffer_fetch_index, MAX_VS_ATTRIBS);
980 }
981
982 pan_postprocess_nir(nir, input.gpu_id);
983
984 if (noperspective_varyings && nir->info.stage == MESA_SHADER_VERTEX) {
985 NIR_PASS(_, nir, nir_inline_sysval,
986 nir_intrinsic_load_noperspective_varyings_pan,
987 *noperspective_varyings);
988 }
989
990 struct panvk_lower_sysvals_context lower_sysvals_ctx = {
991 .shader = shader,
992 .state = state,
993 };
994
995 NIR_PASS(_, nir, nir_shader_instructions_pass, panvk_lower_sysvals,
996 nir_metadata_control_flow, &lower_sysvals_ctx);
997
998 lower_load_push_consts(nir, shader);
999
1000 /* Allow the remaining FAU space to be filled with constants. */
1001 input.fau_consts.max_amount =
1002 2 * (FAU_WORD_COUNT - shader->fau.total_count);
1003 input.fau_consts.offset = shader->fau.total_count * 2;
1004 input.fau_consts.values = &shader->info.fau_consts[0];
1005 assert(input.fau_consts.max_amount <= ARRAY_SIZE(shader->info.fau_consts));
1006
1007 struct util_dynarray binary = UTIL_DYNARRAY_INIT;
1008 pan_shader_compile(nir, &input, &binary, &shader->info);
1009
1010 /* Propagate potential additional FAU values into the panvk info struct. */
1011 /* FAU consts are pushed as 32bit values, but total_count is for 64bit
1012 * ones. */
1013 shader->fau.total_count += DIV_ROUND_UP(shader->info.fau_consts_count, 2);
1014
1015 void *bin_ptr = util_dynarray_element(&binary, uint8_t, 0);
1016 unsigned bin_size = util_dynarray_num_elements(&binary, uint8_t);
1017
1018 shader->bin_size = 0;
1019 shader->bin_ptr = NULL;
1020
1021 if (bin_size) {
1022 void *data = malloc(bin_size);
1023
1024 if (data == NULL)
1025 return panvk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
1026
1027 memcpy(data, bin_ptr, bin_size);
1028 shader->bin_size = bin_size;
1029 shader->bin_ptr = data;
1030 }
1031 util_dynarray_fini(&binary);
1032
1033 if (dump_asm) {
1034 shader->nir_str = nir_shader_as_str(nir, NULL);
1035
1036 char *data = NULL;
1037 size_t disasm_size = 0;
1038
1039 if (shader->bin_size) {
1040 struct u_memstream mem;
1041 if (u_memstream_open(&mem, &data, &disasm_size)) {
1042 FILE *const stream = u_memstream_get(&mem);
1043 pan_disassemble(stream, shader->bin_ptr, shader->bin_size,
1044 compile_input->gpu_id, false);
1045 u_memstream_close(&mem);
1046 }
1047 }
1048
1049 char *asm_str = malloc(disasm_size + 1);
1050 memcpy(asm_str, data, disasm_size);
1051 asm_str[disasm_size] = '\0';
1052 free(data);
1053
1054 shader->asm_str = asm_str;
1055 }
1056
1057 /* We need to update info.push.count because it's used to initialize the
1058 * RSD in pan_shader_prepare_rsd().
1059 */
1060 shader->info.push.count = shader->fau.total_count * 2;
1061
1062 #if PAN_ARCH < 9
1063 /* Patch the descriptor count */
1064 shader->info.ubo_count =
1065 shader->desc_info.others.count[PANVK_BIFROST_DESC_TABLE_UBO] +
1066 shader->desc_info.dyn_ubos.count;
1067 shader->info.texture_count =
1068 shader->desc_info.others.count[PANVK_BIFROST_DESC_TABLE_TEXTURE];
1069 shader->info.sampler_count =
1070 shader->desc_info.others.count[PANVK_BIFROST_DESC_TABLE_SAMPLER];
1071
1072 /* Dummy sampler. */
1073 if (!shader->info.sampler_count && shader->info.texture_count)
1074 shader->info.sampler_count++;
1075
1076 if (nir->info.stage == MESA_SHADER_VERTEX) {
1077 /* We leave holes in the attribute locations, but pan_shader.c assumes the
1078 * opposite. Patch attribute_count accordingly, so
1079 * pan_shader_prepare_rsd() does what we expect.
1080 */
1081 uint32_t gen_attribs =
1082 (shader->info.attributes_read & VERT_BIT_GENERIC_ALL) >>
1083 VERT_ATTRIB_GENERIC0;
1084
1085 shader->info.attribute_count = util_last_bit(gen_attribs);
1086
1087 /* NULL IDVS shaders are not allowed. */
1088 if (!bin_size)
1089 shader->info.vs.idvs = false;
1090 }
1091
1092 /* Image attributes start at MAX_VS_ATTRIBS in the VS attribute table,
1093 * and zero in other stages.
1094 */
1095 if (shader->desc_info.others.count[PANVK_BIFROST_DESC_TABLE_IMG] > 0)
1096 shader->info.attribute_count =
1097 shader->desc_info.others.count[PANVK_BIFROST_DESC_TABLE_IMG] +
1098 (nir->info.stage == MESA_SHADER_VERTEX ? MAX_VS_ATTRIBS : 0);
1099 #endif
1100
1101 switch (nir->info.stage) {
1102 case MESA_SHADER_COMPUTE:
1103 case MESA_SHADER_KERNEL:
1104 shader->cs.local_size.x = nir->info.workgroup_size[0];
1105 shader->cs.local_size.y = nir->info.workgroup_size[1];
1106 shader->cs.local_size.z = nir->info.workgroup_size[2];
1107 break;
1108
1109 case MESA_SHADER_FRAGMENT:
1110 shader->fs.earlyzs_lut = pan_earlyzs_analyze(&shader->info, PAN_ARCH);
1111 break;
1112
1113 default:
1114 break;
1115 }
1116
1117 return VK_SUCCESS;
1118 }
1119
1120 #if PAN_ARCH >= 9
1121 static enum mali_flush_to_zero_mode
1122 shader_ftz_mode(struct panvk_shader_variant *shader)
1123 {
1124 if (shader->info.ftz_fp32) {
1125 if (shader->info.ftz_fp16)
1126 return MALI_FLUSH_TO_ZERO_MODE_ALWAYS;
1127 else
1128 return MALI_FLUSH_TO_ZERO_MODE_DX11;
1129 } else {
1130 /* We don't have a "flush FP16, preserve FP32" mode, but APIs
1131 * should not be able to generate that.
1132 */
1133 assert(!shader->info.ftz_fp16 && !shader->info.ftz_fp32);
1134 return MALI_FLUSH_TO_ZERO_MODE_PRESERVE_SUBNORMALS;
1135 }
1136 }
1137 #endif
1138
1139 static VkResult
1140 panvk_shader_upload(struct panvk_device *dev,
1141 struct panvk_shader_variant *shader,
1142 const VkAllocationCallbacks *pAllocator)
1143 {
1144 shader->code_mem = (struct panvk_priv_mem){0};
1145
1146 #if PAN_ARCH < 9
1147 shader->rsd = (struct panvk_priv_mem){0};
1148 #else
1149 shader->spd = (struct panvk_priv_mem){0};
1150 #endif
1151
1152 if (!shader->bin_size)
1153 return VK_SUCCESS;
1154
1155 shader->code_mem = panvk_pool_upload_aligned(
1156 &dev->mempools.exec, shader->bin_ptr, shader->bin_size, 128);
1157 if (!panvk_priv_mem_check_alloc(shader->code_mem))
1158 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1159
1160 #if PAN_ARCH < 9
1161 if (shader->info.stage == MESA_SHADER_FRAGMENT)
1162 return VK_SUCCESS;
1163
1164 shader->rsd = panvk_pool_alloc_desc(&dev->mempools.rw, RENDERER_STATE);
1165 if (!panvk_priv_mem_check_alloc(shader->rsd))
1166 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1167
1168 panvk_priv_mem_write_desc(shader->rsd, 0, RENDERER_STATE, cfg) {
1169 pan_shader_prepare_rsd(&shader->info,
1170 panvk_shader_variant_get_dev_addr(shader), &cfg);
1171 }
1172 #else
1173 if (shader->info.stage != MESA_SHADER_VERTEX) {
1174 shader->spd = panvk_pool_alloc_desc(&dev->mempools.rw, SHADER_PROGRAM);
1175 if (!panvk_priv_mem_check_alloc(shader->spd))
1176 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1177
1178 panvk_priv_mem_write_desc(shader->spd, 0, SHADER_PROGRAM, cfg) {
1179 cfg.stage = pan_shader_stage(&shader->info);
1180
1181 if (cfg.stage == MALI_SHADER_STAGE_FRAGMENT)
1182 cfg.fragment_coverage_bitmask_type = MALI_COVERAGE_BITMASK_TYPE_GL;
1183 #if PAN_ARCH < 12
1184 else if (cfg.stage == MALI_SHADER_STAGE_VERTEX)
1185 cfg.vertex_warp_limit = MALI_WARP_LIMIT_HALF;
1186 #endif
1187
1188 cfg.register_allocation =
1189 pan_register_allocation(shader->info.work_reg_count);
1190 cfg.binary = panvk_shader_variant_get_dev_addr(shader);
1191 cfg.preload.r48_r63 = (shader->info.preload >> 48);
1192 cfg.flush_to_zero_mode = shader_ftz_mode(shader);
1193
1194 if (cfg.stage == MALI_SHADER_STAGE_FRAGMENT)
1195 cfg.requires_helper_threads = shader->info.contains_barrier;
1196 }
1197 } else {
1198 #if PAN_ARCH >= 12
1199 shader->spds.all_points =
1200 panvk_pool_alloc_desc(&dev->mempools.rw, SHADER_PROGRAM);
1201 if (!panvk_priv_mem_check_alloc(shader->spds.all_points))
1202 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1203
1204 panvk_priv_mem_write_desc(shader->spds.all_points, 0, SHADER_PROGRAM,
1205 cfg) {
1206 cfg.stage = pan_shader_stage(&shader->info);
1207 cfg.register_allocation =
1208 pan_register_allocation(shader->info.work_reg_count);
1209 cfg.binary = panvk_shader_variant_get_dev_addr(shader);
1210 cfg.preload.r48_r63 = (shader->info.preload >> 48);
1211 cfg.flush_to_zero_mode = shader_ftz_mode(shader);
1212 }
1213
1214 shader->spds.all_triangles =
1215 panvk_pool_alloc_desc(&dev->mempools.rw, SHADER_PROGRAM);
1216 if (!panvk_priv_mem_check_alloc(shader->spds.all_triangles))
1217 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1218
1219 panvk_priv_mem_write_desc(shader->spds.all_triangles, 0, SHADER_PROGRAM,
1220 cfg) {
1221 cfg.stage = pan_shader_stage(&shader->info);
1222 cfg.register_allocation =
1223 pan_register_allocation(shader->info.work_reg_count);
1224 cfg.binary = panvk_shader_variant_get_dev_addr(shader) +
1225 shader->info.vs.no_psiz_offset;
1226 cfg.preload.r48_r63 = (shader->info.preload >> 48);
1227 cfg.flush_to_zero_mode = shader_ftz_mode(shader);
1228 }
1229 #else
1230 shader->spds.pos_points =
1231 panvk_pool_alloc_desc(&dev->mempools.rw, SHADER_PROGRAM);
1232 if (!panvk_priv_mem_check_alloc(shader->spds.pos_points))
1233 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1234
1235 panvk_priv_mem_write_desc(shader->spds.pos_points, 0, SHADER_PROGRAM,
1236 cfg) {
1237 cfg.stage = pan_shader_stage(&shader->info);
1238 cfg.vertex_warp_limit = MALI_WARP_LIMIT_HALF;
1239 cfg.register_allocation =
1240 pan_register_allocation(shader->info.work_reg_count);
1241 cfg.binary = panvk_shader_variant_get_dev_addr(shader);
1242 cfg.preload.r48_r63 = (shader->info.preload >> 48);
1243 cfg.flush_to_zero_mode = shader_ftz_mode(shader);
1244 }
1245
1246 shader->spds.pos_triangles =
1247 panvk_pool_alloc_desc(&dev->mempools.rw, SHADER_PROGRAM);
1248 if (!panvk_priv_mem_check_alloc(shader->spds.pos_triangles))
1249 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1250
1251 panvk_priv_mem_write_desc(shader->spds.pos_triangles, 0, SHADER_PROGRAM,
1252 cfg) {
1253 cfg.stage = pan_shader_stage(&shader->info);
1254 cfg.vertex_warp_limit = MALI_WARP_LIMIT_HALF;
1255 cfg.register_allocation =
1256 pan_register_allocation(shader->info.work_reg_count);
1257 cfg.binary = panvk_shader_variant_get_dev_addr(shader) +
1258 shader->info.vs.no_psiz_offset;
1259 cfg.preload.r48_r63 = (shader->info.preload >> 48);
1260 cfg.flush_to_zero_mode = shader_ftz_mode(shader);
1261 }
1262
1263 if (shader->info.vs.secondary_enable) {
1264 shader->spds.var =
1265 panvk_pool_alloc_desc(&dev->mempools.rw, SHADER_PROGRAM);
1266 if (!panvk_priv_mem_check_alloc(shader->spds.var))
1267 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1268
1269 panvk_priv_mem_write_desc(shader->spds.var, 0, SHADER_PROGRAM, cfg) {
1270 unsigned work_count = shader->info.vs.secondary_work_reg_count;
1271
1272 cfg.stage = pan_shader_stage(&shader->info);
1273 cfg.vertex_warp_limit = MALI_WARP_LIMIT_FULL;
1274 cfg.register_allocation = pan_register_allocation(work_count);
1275 cfg.binary = panvk_shader_variant_get_dev_addr(shader) +
1276 shader->info.vs.secondary_offset;
1277 cfg.preload.r48_r63 = (shader->info.vs.secondary_preload >> 48);
1278 cfg.flush_to_zero_mode = shader_ftz_mode(shader);
1279 }
1280 }
1281 #endif
1282 }
1283 #endif
1284
1285 return VK_SUCCESS;
1286 }
1287
1288 static void
1289 panvk_shader_variant_destroy(struct panvk_shader_variant *shader)
1290 {
1291 free((void *)shader->asm_str);
1292 ralloc_free((void *)shader->nir_str);
1293
1294 panvk_pool_free_mem(&shader->code_mem);
1295
1296 #if PAN_ARCH < 9
1297 panvk_pool_free_mem(&shader->rsd);
1298 panvk_pool_free_mem(&shader->desc_info.others.map);
1299 #else
1300 if (shader->info.stage != MESA_SHADER_VERTEX) {
1301 panvk_pool_free_mem(&shader->spd);
1302 } else {
1303 #if PAN_ARCH >= 12
1304 panvk_pool_free_mem(&shader->spds.all_points);
1305 panvk_pool_free_mem(&shader->spds.all_triangles);
1306 #else
1307 panvk_pool_free_mem(&shader->spds.var);
1308 panvk_pool_free_mem(&shader->spds.pos_points);
1309 panvk_pool_free_mem(&shader->spds.pos_triangles);
1310 #endif
1311 }
1312 #endif
1313
1314 if (shader->own_bin)
1315 free((void *)shader->bin_ptr);
1316 }
1317
1318 static void
1319 panvk_shader_destroy(struct vk_device *vk_dev, struct vk_shader *vk_shader,
1320 const VkAllocationCallbacks *pAllocator)
1321 {
1322 struct panvk_shader *shader =
1323 container_of(vk_shader, struct panvk_shader, vk);
1324
1325 panvk_shader_foreach_variant(shader, variant) {
1326 panvk_shader_variant_destroy(variant);
1327 }
1328
1329 vk_shader_free(vk_dev, pAllocator, &shader->vk);
1330 }
1331
1332 static const struct vk_shader_ops panvk_shader_ops;
1333
1334 static VkResult
1335 panvk_compile_shader(struct panvk_device *dev,
1336 struct vk_shader_compile_info *info,
1337 const struct vk_graphics_pipeline_state *state,
1338 const struct pan_varying_layout *vs_varying_layout,
1339 const uint32_t *noperspective_varyings,
1340 const VkAllocationCallbacks *pAllocator,
1341 struct vk_shader **shader_out)
1342 {
1343 struct panvk_physical_device *phys_dev =
1344 to_panvk_physical_device(dev->vk.physical);
1345
1346 struct panvk_shader *shader;
1347 VkResult result;
1348
1349 size_t size =
1350 sizeof(struct panvk_shader) + sizeof(struct panvk_shader_variant) *
1351 panvk_shader_num_variants(info->stage);
1352 shader = vk_shader_zalloc(&dev->vk, &panvk_shader_ops, info->stage,
1353 pAllocator, size);
1354 if (shader == NULL)
1355 return panvk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
1356
1357 nir_variable_mode robust_modes = 0;
1358 if (info->robustness->uniform_buffers != VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT)
1359 robust_modes |= nir_var_mem_ubo;
1360 if (info->robustness->storage_buffers != VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT)
1361 robust_modes |= nir_var_mem_ssbo;
1362
1363 struct pan_compile_inputs inputs = {
1364 .gpu_id = phys_dev->kmod.dev->props.gpu_id,
1365 .gpu_variant = phys_dev->kmod.dev->props.gpu_variant,
1366 .view_mask = (state && state->rp) ? state->mv->view_mask : 0,
1367 .robust_modes = robust_modes,
1368 .robust_descriptors = dev->vk.enabled_features.nullDescriptor,
1369 };
1370
1371 switch (info->stage) {
1372 case MESA_SHADER_VERTEX: {
1373 const enum panvk_vs_variant last_variant = PANVK_VS_VARIANT_HW;
1374 for (enum panvk_vs_variant v = 0; v <= last_variant; v++) {
1375 struct panvk_shader_variant *variant = &shader->variants[v];
1376
1377 /* Each variant gets its own NIR. To save an extra clone, we use the
1378 * original NIR for the last stage.
1379 */
1380 const bool clone_nir = (v != last_variant);
1381 nir_shader *nir =
1382 clone_nir ? nir_shader_clone(NULL, info->nir) : info->nir;
1383
1384 #if PAN_ARCH >= 10
1385 if (inputs.view_mask) {
1386 nir_lower_multiview_options options = {
1387 .view_mask = inputs.view_mask,
1388 .allowed_per_view_outputs = ~0
1389 };
1390
1391 /* The only case where this should fail is with memory/image
1392 * writes, which we don't support in vertex shaders
1393 */
1394 assert(nir_can_lower_multiview(nir, options));
1395 NIR_PASS(_, nir, nir_lower_multiview, options);
1396
1397 /* Pull output writes out of the loop and give them constant
1398 * offsets for pan_lower_store_components
1399 */
1400 NIR_PASS(_, nir, nir_lower_io_vars_to_temporaries,
1401 nir_shader_get_entrypoint(nir), nir_var_shader_out);
1402 }
1403 #endif
1404
1405 panvk_lower_nir(dev, nir, info->set_layout_count,
1406 info->set_layouts, info->robustness,
1407 state, &variant->desc_info, false);
1408
1409 /* We need the driver_location to match the vertex attribute
1410 * location, so we can use the attribute layout described by
1411 * vk_vertex_input_state where there are holes in the attribute
1412 * locations.
1413 */
1414 nir_foreach_shader_in_variable(var, nir) {
1415 assert(var->data.location >= VERT_ATTRIB_GENERIC0 &&
1416 var->data.location <= VERT_ATTRIB_GENERIC15);
1417 var->data.driver_location =
1418 var->data.location - VERT_ATTRIB_GENERIC0;
1419 }
1420 nir_assign_io_var_locations(nir, nir_var_shader_out);
1421 panvk_lower_nir_io(nir);
1422 /* This somehow folds the location for multi-slot nir_load/nir_store */
1423 NIR_PASS(_, nir, nir_opt_constant_folding);
1424
1425 inputs.trust_varying_flat_highp_types = true;
1426 struct pan_varying_layout varying_layout;
1427 if (v == PANVK_VS_VARIANT_HW) {
1428 pan_varying_collect_formats(&varying_layout, nir, inputs.gpu_id,
1429 inputs.trust_varying_flat_highp_types,
1430 true);
1431 pan_build_varying_layout_compact(&varying_layout, nir,
1432 inputs.gpu_id);
1433 inputs.varying_layout = &varying_layout;
1434 }
1435
1436 variant->own_bin = true;
1437
1438 result = panvk_compile_nir(dev, nir, info->flags, &inputs, state,
1439 noperspective_varyings, variant);
1440
1441 /* If we cloned, it's our job to clean up */
1442 if (clone_nir)
1443 ralloc_free(nir);
1444
1445 if (result != VK_SUCCESS) {
1446 panvk_shader_destroy(&dev->vk, &shader->vk, pAllocator);
1447 return result;
1448 }
1449 }
1450 break;
1451 }
1452
1453 case MESA_SHADER_FRAGMENT: {
1454 struct panvk_shader_variant *variant =
1455 (struct panvk_shader_variant *)panvk_shader_only_variant(shader);
1456
1457 nir_shader *nir = info->nir;
1458
1459 if (state && state->ms && state->ms->sample_shading_enable)
1460 nir->info.fs.uses_sample_shading = true;
1461
1462 /* We need to lower input attachments before we lower descriptors */
1463 NIR_PASS(_, nir, panvk_per_arch(nir_lower_input_attachment_loads),
1464 state, &variant->fs.input_attachment_read);
1465
1466 /* Lower input intrinsics for fragment shaders early to get the max
1467 * number of varying loads, as this number is required during descriptor
1468 * lowering for v9+.
1469 */
1470 nir_assign_io_var_locations(nir, nir_var_shader_in);
1471
1472 /* VS (if known) decides the memory layout */
1473 inputs.varying_layout = vs_varying_layout;
1474
1475 panvk_lower_nir(dev, nir, info->set_layout_count, info->set_layouts,
1476 info->robustness, state, &variant->desc_info, false);
1477
1478 nir_assign_io_var_locations(nir, nir_var_shader_out);
1479 panvk_lower_nir_io(nir);
1480
1481 /* Lower FS outputs now so that we can lower load_blend_descriptor_pan
1482 * to a driver-provided FAU instead of using the blend descriptors
1483 * uploaded by the hardware. See panvk_vX_blend.c for details.
1484 */
1485 NIR_PASS(_, nir, pan_nir_lower_fs_outputs, false);
1486
1487 variant->own_bin = true;
1488
1489 result = panvk_compile_nir(dev, nir, info->flags, &inputs, state,
1490 noperspective_varyings, variant);
1491 if (result != VK_SUCCESS) {
1492 panvk_shader_destroy(&dev->vk, &shader->vk, pAllocator);
1493 return result;
1494 }
1495
1496 #if PAN_ARCH >= 9
1497 /* LD_VAR_BUF[_IMM] has a fixed-size offset, when shaders overflow
1498 * that they fall back to LD_VAR[_IMM] and require descriptors.
1499 * TODO: We could only emit descriptors that overflow the offset,
1500 * saving a bit of space.
1501 */
1502 variant->desc_info.fs_varying_attr_desc_count =
1503 variant->info.bifrost.uses_ld_var ? nir->num_inputs : 0;
1504 #endif
1505
1506 break;
1507 }
1508
1509 case MESA_SHADER_COMPUTE: {
1510 struct panvk_shader_variant *variant =
1511 (struct panvk_shader_variant *)panvk_shader_only_variant(shader);
1512
1513 nir_shader *nir = info->nir;
1514
1515 #if PAN_ARCH >= 9
1516 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
1517 variant->info.cs.allow_merging_workgroups =
1518 valhall_can_merge_workgroups(nir);
1519
1520 /* With merged workgroups, we need to use different divergence analysis
1521 * options to take into account that threads from different workgroups
1522 * may be in the same subgroup */
1523 if (variant->info.cs.allow_merging_workgroups) {
1524 nir->options = pan_get_nir_shader_compiler_options(PAN_ARCH, true);
1525 /* Invalidate the old divergence analysis */
1526 nir_foreach_function_impl(impl, nir)
1527 nir_progress(true, impl, ~nir_metadata_divergence);
1528 }
1529 #endif
1530
1531 panvk_lower_nir(dev, nir, info->set_layout_count, info->set_layouts,
1532 info->robustness, state, &variant->desc_info,
1533 variant->info.cs.allow_merging_workgroups);
1534
1535 variant->own_bin = true;
1536
1537 result = panvk_compile_nir(dev, nir, info->flags, &inputs, state,
1538 noperspective_varyings, variant);
1539 if (result != VK_SUCCESS) {
1540 panvk_shader_destroy(&dev->vk, &shader->vk, pAllocator);
1541 return result;
1542 }
1543 break;
1544 }
1545
1546 default:
1547 UNREACHABLE("Unknown shader stage");
1548 }
1549
1550 panvk_shader_foreach_variant(shader, variant) {
1551 result = panvk_shader_upload(dev, variant, pAllocator);
1552 if (result != VK_SUCCESS) {
1553 panvk_shader_destroy(&dev->vk, &shader->vk, pAllocator);
1554 return result;
1555 }
1556 }
1557
1558 *shader_out = &shader->vk;
1559
1560 return result;
1561 }
1562
1563 VkResult
1564 panvk_per_arch(create_shader_from_binary)(struct panvk_device *dev,
1565 const struct pan_shader_info *info,
1566 struct pan_compute_dim local_size,
1567 const void *bin_ptr, size_t bin_size,
1568 struct panvk_shader **shader_out)
1569 {
1570 struct panvk_shader *shader;
1571 VkResult result;
1572
1573 size_t size =
1574 sizeof(struct panvk_shader) + sizeof(struct panvk_shader_variant) *
1575 panvk_shader_num_variants(info->stage);
1576 shader = vk_shader_zalloc(&dev->vk, &panvk_shader_ops, info->stage,
1577 &dev->vk.alloc, size);
1578 if (shader == NULL)
1579 return panvk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
1580
1581 assert(panvk_shader_num_variants(info->stage) == 1);
1582
1583 struct panvk_shader_variant *variant = &shader->variants[0];
1584 variant->info = *info;
1585 variant->cs.local_size = local_size;
1586 variant->bin_ptr = bin_ptr;
1587 variant->bin_size = bin_size;
1588 variant->own_bin = false;
1589 variant->nir_str = NULL;
1590 variant->asm_str = NULL;
1591
1592 result = panvk_shader_upload(dev, variant, &dev->vk.alloc);
1593
1594 if (result != VK_SUCCESS) {
1595 panvk_shader_destroy(&dev->vk, &shader->vk, &dev->vk.alloc);
1596 return result;
1597 }
1598
1599 *shader_out = shader;
1600
1601 return result;
1602 }
1603
1604 static VkResult
1605 compile_shaders(struct vk_device *vk_dev, uint32_t shader_count,
1606 struct vk_shader_compile_info *infos,
1607 const struct vk_graphics_pipeline_state *state,
1608 const struct vk_features *enabled_features,
1609 const VkAllocationCallbacks *pAllocator,
1610 struct vk_shader **shaders_out)
1611 {
1612 struct panvk_device *dev = to_panvk_device(vk_dev);
1613 bool use_static_noperspective = false;
1614 uint32_t noperspective_varyings = 0;
1615 VkResult result;
1616 int32_t i;
1617
1618 /* If we are linking VS and FS, we can use the static interpolation
1619 * qualifiers from the FS in the VS. Vulkan runtime passes us shaders in
1620 * stage order, so the FS will always be last if it exists.
1621 */
1622 if (infos[shader_count - 1].nir->info.stage == MESA_SHADER_FRAGMENT) {
1623 nir_shader *nir = infos[shader_count - 1].nir;
1624 noperspective_varyings = pan_nir_collect_noperspective_varyings_fs(nir);
1625 use_static_noperspective = true;
1626 }
1627
1628 /* Vulkan runtime passes us shaders in stage order */
1629 for (i = 0; i < shader_count; i++) {
1630 const uint32_t *noperspective_varyings_ptr =
1631 use_static_noperspective ? &noperspective_varyings : NULL;
1632
1633 /* For fragment shaders, Look at the previous stage to see if we can
1634 * find a varying layout we can use instead of making up our own.
1635 */
1636 const struct pan_varying_layout *vs_varying_layout = NULL;
1637 if (infos[i].stage == MESA_SHADER_FRAGMENT && i > 0 &&
1638 infos[i - 1].next_stage_mask == VK_SHADER_STAGE_FRAGMENT_BIT) {
1639 struct panvk_shader *prev_shader =
1640 container_of(shaders_out[i - 1], struct panvk_shader, vk);
1641
1642 /* The last geometry stage before the FS will always have a HW vertex
1643 * shader variant.
1644 */
1645 panvk_shader_foreach_variant(prev_shader, variant) {
1646 if (variant->info.stage == MESA_SHADER_VERTEX) {
1647 vs_varying_layout = &variant->info.varyings.formats;
1648 pan_varying_layout_require_layout(vs_varying_layout);
1649 break;
1650 }
1651 }
1652 }
1653
1654 result = panvk_compile_shader(dev, &infos[i], state, vs_varying_layout,
1655 noperspective_varyings_ptr, pAllocator,
1656 &shaders_out[i]);
1657
1658 if (result != VK_SUCCESS)
1659 goto err_cleanup;
1660
1661 /* Clean up NIR for the current shader */
1662 ralloc_free(infos[i].nir);
1663 }
1664
1665 /* TODO: If we get multiple shaders here, we can perform part of the link
1666 * logic at compile time. */
1667
1668 return VK_SUCCESS;
1669
1670 err_cleanup:
1671 /* Clean up all the shaders before this point */
1672 for (int32_t j = 0; j < i; j++)
1673 panvk_shader_destroy(&dev->vk, shaders_out[j], pAllocator);
1674
1675 /* Clean up all the NIR from this point */
1676 for (int32_t j = i; j < shader_count; j++)
1677 ralloc_free(infos[j].nir);
1678
1679 /* Memset the output array */
1680 memset(shaders_out, 0, shader_count * sizeof(*shaders_out));
1681
1682 return result;
1683 }
1684
1685 static simple_mtx_t compiler_mutex = SIMPLE_MTX_INITIALIZER;
1686
1687 void
1688 panvk_per_arch(compiler_lock)(void)
1689 {
1690 if (pan_will_dump_shaders(PAN_ARCH) || PANVK_DEBUG(NIR))
1691 simple_mtx_lock(&compiler_mutex);
1692 }
1693
1694 void
1695 panvk_per_arch(compiler_unlock)(void)
1696 {
1697 if (pan_will_dump_shaders(PAN_ARCH) || PANVK_DEBUG(NIR))
1698 simple_mtx_unlock(&compiler_mutex);
1699 }
1700
1701 static VkResult
1702 panvk_compile_shaders(struct vk_device *vk_dev, uint32_t shader_count,
1703 struct vk_shader_compile_info *infos,
1704 const struct vk_graphics_pipeline_state *state,
1705 const struct vk_features *enabled_features,
1706 const VkAllocationCallbacks *pAllocator,
1707 struct vk_shader **shaders_out)
1708 {
1709 panvk_per_arch(compiler_lock)();
1710
1711 VkResult result = compile_shaders(vk_dev, shader_count, infos, state,
1712 enabled_features, pAllocator,
1713 shaders_out);
1714
1715 panvk_per_arch(compiler_unlock)();
1716
1717 return result;
1718 }
1719
1720 static VkResult
1721 shader_desc_info_deserialize(struct panvk_device *dev,
1722 struct blob_reader *blob,
1723 struct panvk_shader_variant *shader)
1724 {
1725 shader->desc_info.used_set_mask = blob_read_uint32(blob);
1726
1727 #if PAN_ARCH < 9
1728 shader->desc_info.dyn_ubos.count = blob_read_uint32(blob);
1729 blob_copy_bytes(blob, shader->desc_info.dyn_ubos.map,
1730 sizeof(*shader->desc_info.dyn_ubos.map) *
1731 shader->desc_info.dyn_ubos.count);
1732 shader->desc_info.dyn_ssbos.count = blob_read_uint32(blob);
1733 blob_copy_bytes(blob, shader->desc_info.dyn_ssbos.map,
1734 sizeof(*shader->desc_info.dyn_ssbos.map) *
1735 shader->desc_info.dyn_ssbos.count);
1736
1737 uint32_t others_count = 0;
1738 for (unsigned i = 0; i < ARRAY_SIZE(shader->desc_info.others.count); i++) {
1739 shader->desc_info.others.count[i] = blob_read_uint32(blob);
1740 others_count += shader->desc_info.others.count[i];
1741 }
1742
1743 if (others_count) {
1744 struct panvk_pool_alloc_info alloc_info = {
1745 .size = others_count * sizeof(uint32_t),
1746 .alignment = sizeof(uint32_t),
1747 };
1748 shader->desc_info.others.map =
1749 panvk_pool_alloc_mem(&dev->mempools.rw, alloc_info);
1750 if (!panvk_priv_mem_check_alloc(shader->desc_info.others.map))
1751 return panvk_error(shader, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1752
1753 panvk_priv_mem_write_array(shader->desc_info.others.map, 0, uint32_t,
1754 others_count, copy_table) {
1755 blob_copy_bytes(blob, copy_table, others_count * sizeof(*copy_table));
1756 }
1757 }
1758 #else
1759 shader->desc_info.dyn_bufs.count = blob_read_uint32(blob);
1760 blob_copy_bytes(blob, shader->desc_info.dyn_bufs.map,
1761 sizeof(*shader->desc_info.dyn_bufs.map) *
1762 shader->desc_info.dyn_bufs.count);
1763 shader->desc_info.fs_varying_attr_desc_count = blob_read_uint32(blob);
1764 #endif
1765
1766 return VK_SUCCESS;
1767 }
1768
1769 static VkResult
1770 panvk_deserialize_shader_variant(struct vk_device *vk_dev,
1771 struct blob_reader *blob,
1772 const VkAllocationCallbacks *pAllocator,
1773 struct panvk_shader_variant *shader)
1774 {
1775 struct panvk_device *device = to_panvk_device(vk_dev);
1776 struct pan_shader_info info;
1777 VkResult result;
1778
1779 blob_copy_bytes(blob, &info, sizeof(info));
1780 if (blob->overrun)
1781 return panvk_error(device, VK_ERROR_INCOMPATIBLE_SHADER_BINARY_EXT);
1782
1783 shader->info = info;
1784 blob_copy_bytes(blob, &shader->fau, sizeof(shader->fau));
1785
1786 switch (shader->info.stage) {
1787 case MESA_SHADER_COMPUTE:
1788 case MESA_SHADER_KERNEL:
1789 blob_copy_bytes(blob, &shader->cs.local_size,
1790 sizeof(shader->cs.local_size));
1791 break;
1792
1793 case MESA_SHADER_FRAGMENT:
1794 shader->fs.earlyzs_lut = pan_earlyzs_analyze(&shader->info, PAN_ARCH);
1795 blob_copy_bytes(blob, &shader->fs.input_attachment_read,
1796 sizeof(shader->fs.input_attachment_read));
1797 break;
1798
1799 default:
1800 break;
1801 }
1802
1803 shader->bin_size = blob_read_uint32(blob);
1804
1805 if (blob->overrun)
1806 return panvk_error(device, VK_ERROR_INCOMPATIBLE_SHADER_BINARY_EXT);
1807
1808 shader->bin_ptr = malloc(shader->bin_size);
1809 if (shader->bin_ptr == NULL)
1810 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1811
1812 shader->own_bin = true;
1813 blob_copy_bytes(blob, (void *)shader->bin_ptr, shader->bin_size);
1814
1815 result = shader_desc_info_deserialize(device, blob, shader);
1816
1817 if (result != VK_SUCCESS)
1818 return panvk_error(device, result);
1819
1820 uint32_t nir_str_size = blob_read_uint32(blob);
1821 uint32_t asm_str_size = blob_read_uint32(blob);
1822 const char *nir_str = blob_read_bytes(blob, nir_str_size);
1823 const char *asm_str = blob_read_bytes(blob, asm_str_size);
1824
1825 if (blob->overrun)
1826 return panvk_error(device, VK_ERROR_INCOMPATIBLE_SHADER_BINARY_EXT);
1827
1828 if (nir_str_size > 0) {
1829 shader->nir_str = ralloc_strndup(NULL, nir_str, nir_str_size);
1830 if (shader->nir_str == NULL)
1831 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1832 }
1833
1834 if (asm_str_size > 0) {
1835 shader->asm_str = strndup(asm_str, asm_str_size);
1836 if (shader->asm_str == NULL)
1837 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1838 }
1839
1840 result = panvk_shader_upload(device, shader, pAllocator);
1841
1842 if (result != VK_SUCCESS)
1843 return result;
1844
1845 return result;
1846 }
1847
1848 static VkResult
1849 panvk_deserialize_shader(struct vk_device *vk_dev, struct blob_reader *blob,
1850 uint32_t binary_version,
1851 const VkAllocationCallbacks *pAllocator,
1852 struct vk_shader **shader_out)
1853 {
1854 struct panvk_device *device = to_panvk_device(vk_dev);
1855 struct panvk_shader *shader;
1856
1857 mesa_shader_stage stage = blob_read_uint8(blob);
1858 if (blob->overrun)
1859 return vk_error(device, VK_ERROR_INCOMPATIBLE_SHADER_BINARY_EXT);
1860
1861 size_t size =
1862 sizeof(struct panvk_shader) +
1863 sizeof(struct panvk_shader_variant) * panvk_shader_num_variants(stage);
1864 shader =
1865 vk_shader_zalloc(vk_dev, &panvk_shader_ops, stage, pAllocator, size);
1866 if (shader == NULL)
1867 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1868
1869 panvk_shader_foreach_variant(shader, variant) {
1870 VkResult result =
1871 panvk_deserialize_shader_variant(vk_dev, blob, pAllocator, variant);
1872
1873 if (result != VK_SUCCESS) {
1874 panvk_shader_destroy(vk_dev, &shader->vk, pAllocator);
1875 return result;
1876 }
1877 }
1878
1879 *shader_out = &shader->vk;
1880
1881 return VK_SUCCESS;
1882 }
1883
1884 static void
1885 shader_desc_info_serialize(struct blob *blob,
1886 const struct panvk_shader_variant *shader)
1887 {
1888 blob_write_uint32(blob, shader->desc_info.used_set_mask);
1889
1890 #if PAN_ARCH < 9
1891 blob_write_uint32(blob, shader->desc_info.dyn_ubos.count);
1892 blob_write_bytes(blob, shader->desc_info.dyn_ubos.map,
1893 sizeof(*shader->desc_info.dyn_ubos.map) *
1894 shader->desc_info.dyn_ubos.count);
1895 blob_write_uint32(blob, shader->desc_info.dyn_ssbos.count);
1896 blob_write_bytes(blob, shader->desc_info.dyn_ssbos.map,
1897 sizeof(*shader->desc_info.dyn_ssbos.map) *
1898 shader->desc_info.dyn_ssbos.count);
1899
1900 unsigned others_count = 0;
1901 for (unsigned i = 0; i < ARRAY_SIZE(shader->desc_info.others.count); i++) {
1902 blob_write_uint32(blob, shader->desc_info.others.count[i]);
1903 others_count += shader->desc_info.others.count[i];
1904 }
1905
1906 /* No need to wrap this one in panvk_priv_mem_readback(), because the
1907 * GPU is not supposed to touch it. */
1908 blob_write_bytes(blob,
1909 panvk_priv_mem_host_addr(shader->desc_info.others.map),
1910 sizeof(uint32_t) * others_count);
1911 #else
1912 blob_write_uint32(blob, shader->desc_info.dyn_bufs.count);
1913 blob_write_bytes(blob, shader->desc_info.dyn_bufs.map,
1914 sizeof(*shader->desc_info.dyn_bufs.map) *
1915 shader->desc_info.dyn_bufs.count);
1916 blob_write_uint32(blob, shader->desc_info.fs_varying_attr_desc_count);
1917 #endif
1918 }
1919
1920 static bool
1921 panvk_shader_serialize_variant(struct vk_device *vk_dev,
1922 const struct panvk_shader_variant *shader,
1923 struct blob *blob)
1924 {
1925 blob_write_bytes(blob, &shader->info, sizeof(shader->info));
1926 blob_write_bytes(blob, &shader->fau, sizeof(shader->fau));
1927
1928 switch (shader->info.stage) {
1929 case MESA_SHADER_COMPUTE:
1930 case MESA_SHADER_KERNEL:
1931 blob_write_bytes(blob, &shader->cs.local_size,
1932 sizeof(shader->cs.local_size));
1933 break;
1934
1935 case MESA_SHADER_FRAGMENT:
1936 blob_write_bytes(blob, &shader->fs.input_attachment_read,
1937 sizeof(shader->fs.input_attachment_read));
1938 break;
1939
1940 default:
1941 break;
1942 }
1943
1944 blob_write_uint32(blob, shader->bin_size);
1945 blob_write_bytes(blob, shader->bin_ptr, shader->bin_size);
1946 shader_desc_info_serialize(blob, shader);
1947
1948 /* Include the terminating NULL in the serialization */
1949 uint32_t nir_str_size = shader->nir_str ? strlen(shader->nir_str) + 1 : 0;
1950 uint32_t asm_str_size = shader->asm_str ? strlen(shader->asm_str) + 1 : 0;
1951 blob_write_uint32(blob, nir_str_size);
1952 blob_write_uint32(blob, asm_str_size);
1953 blob_write_bytes(blob, shader->nir_str, nir_str_size);
1954 blob_write_bytes(blob, shader->asm_str, asm_str_size);
1955
1956 return !blob->out_of_memory;
1957 }
1958
1959 static bool
1960 panvk_shader_serialize(struct vk_device *vk_dev,
1961 const struct vk_shader *vk_shader, struct blob *blob)
1962 {
1963 struct panvk_shader *shader =
1964 container_of(vk_shader, struct panvk_shader, vk);
1965
1966 blob_write_uint8(blob, vk_shader->stage);
1967
1968 panvk_shader_foreach_variant(shader, variant) {
1969 panvk_shader_serialize_variant(vk_dev, variant, blob);
1970 }
1971
1972 return !blob->out_of_memory;
1973 }
1974
1975 static VkResult
1976 panvk_shader_get_executable_properties(
1977 UNUSED struct vk_device *device, const struct vk_shader *vk_shader,
1978 uint32_t *executable_count, VkPipelineExecutablePropertiesKHR *properties)
1979 {
1980 struct panvk_shader *shader =
1981 container_of(vk_shader, struct panvk_shader, vk);
1982
1983 VK_OUTARRAY_MAKE_TYPED(VkPipelineExecutablePropertiesKHR, out, properties,
1984 executable_count);
1985
1986 panvk_shader_foreach_variant(shader, variant) {
1987 /* Ignore absent variants */
1988 if (variant->bin_size == 0)
1989 continue;
1990
1991 const char *variant_name = panvk_shader_variant_name(shader, variant);
1992 const char *stage_name = _mesa_shader_stage_to_string(shader->vk.stage);
1993
1994 vk_outarray_append_typed(VkPipelineExecutablePropertiesKHR, &out, props)
1995 {
1996 props->stages = mesa_to_vk_shader_stage(shader->vk.stage);
1997 props->subgroupSize = pan_subgroup_size(PAN_ARCH);
1998
1999 if (variant_name != NULL) {
2000 VK_PRINT_STR(props->name, "%s %s", variant_name, stage_name);
2001 VK_PRINT_STR(props->description, "%s %s shader", variant_name,
2002 stage_name);
2003 } else {
2004 VK_COPY_STR(props->name, stage_name);
2005 VK_PRINT_STR(props->description, "%s shader", stage_name);
2006 }
2007 }
2008
2009 if (variant->info.stage == MESA_SHADER_VERTEX &&
2010 variant->info.vs.secondary_offset) {
2011 vk_outarray_append_typed(VkPipelineExecutablePropertiesKHR, &out,
2012 props)
2013 {
2014 props->stages = mesa_to_vk_shader_stage(shader->vk.stage);
2015 props->subgroupSize = pan_subgroup_size(PAN_ARCH);
2016
2017 if (variant_name != NULL) {
2018 VK_PRINT_STR(props->name, "%s %s varying", variant_name,
2019 stage_name);
2020 VK_PRINT_STR(props->description, "%s %s varying shader",
2021 variant_name, stage_name);
2022 } else {
2023 VK_PRINT_STR(props->name, "%s varying", stage_name);
2024 VK_PRINT_STR(props->description, "%s varying shader",
2025 stage_name);
2026 }
2027 }
2028 }
2029 }
2030
2031 return vk_outarray_status(&out);
2032 }
2033
2034 static const struct panvk_shader_variant *
2035 get_variant_from_executable_index(struct panvk_shader *shader,
2036 uint32_t executable_index,
2037 bool *idvs_varying)
2038 {
2039 *idvs_varying = false;
2040 uint32_t i = 0;
2041
2042 panvk_shader_foreach_variant(shader, variant) {
2043 /* Ignore absent variants */
2044 if (variant->bin_size == 0)
2045 continue;
2046
2047 if (i == executable_index)
2048 return variant;
2049
2050 i++;
2051
2052 /* VS variants that have a separate IDVS varying shader get two indices,
2053 * the first for the position shader and the second for the varying
2054 * shader
2055 */
2056 if (variant->info.stage == MESA_SHADER_VERTEX &&
2057 variant->info.vs.secondary_offset) {
2058 if (i == executable_index) {
2059 *idvs_varying = true;
2060 return variant;
2061 }
2062
2063 i++;
2064 }
2065 }
2066
2067 return NULL;
2068 }
2069
2070 static VkResult
2071 panvk_shader_get_executable_statistics(
2072 UNUSED struct vk_device *device, const struct vk_shader *vk_shader,
2073 uint32_t executable_index, uint32_t *statistic_count,
2074 VkPipelineExecutableStatisticKHR *statistics)
2075 {
2076 struct panvk_shader *shader =
2077 container_of(vk_shader, struct panvk_shader, vk);
2078
2079 bool needs_vary = false;
2080
2081 const struct panvk_shader_variant *variant =
2082 get_variant_from_executable_index(shader, executable_index, &needs_vary);
2083 assert(variant != NULL);
2084
2085 VK_OUTARRAY_MAKE_TYPED(VkPipelineExecutableStatisticKHR, out, statistics,
2086 statistic_count);
2087
2088 const struct pan_stats *stats =
2089 needs_vary ? &variant->info.stats_idvs_varying : &variant->info.stats;
2090
2091 vk_add_pan_stats(out, stats);
2092 return vk_outarray_status(&out);
2093 }
2094
2095 static bool
2096 write_ir_text(VkPipelineExecutableInternalRepresentationKHR *ir,
2097 const char *data)
2098 {
2099 ir->isText = VK_TRUE;
2100
2101 size_t data_len = strlen(data) + 1;
2102
2103 if (ir->pData == NULL) {
2104 ir->dataSize = data_len;
2105 return true;
2106 }
2107
2108 strncpy(ir->pData, data, ir->dataSize);
2109 if (ir->dataSize < data_len)
2110 return false;
2111
2112 ir->dataSize = data_len;
2113 return true;
2114 }
2115
2116 static VkResult
2117 panvk_shader_get_executable_internal_representations(
2118 UNUSED struct vk_device *device, const struct vk_shader *vk_shader,
2119 uint32_t executable_index, uint32_t *internal_representation_count,
2120 VkPipelineExecutableInternalRepresentationKHR *internal_representations)
2121 {
2122 struct panvk_shader *shader =
2123 container_of(vk_shader, struct panvk_shader, vk);
2124
2125 VK_OUTARRAY_MAKE_TYPED(VkPipelineExecutableInternalRepresentationKHR, out,
2126 internal_representations,
2127 internal_representation_count);
2128
2129 bool needs_vary = false;
2130
2131 const struct panvk_shader_variant *variant =
2132 get_variant_from_executable_index(shader, executable_index, &needs_vary);
2133 assert(variant != NULL);
2134
2135 /* XXX: Varying shader assembly */
2136 if (needs_vary)
2137 return vk_outarray_status(&out);
2138
2139 bool incomplete_text = false;
2140
2141 if (variant->nir_str != NULL) {
2142 vk_outarray_append_typed(VkPipelineExecutableInternalRepresentationKHR,
2143 &out, ir)
2144 {
2145 VK_COPY_STR(ir->name, "NIR shader");
2146 VK_COPY_STR(ir->description,
2147 "NIR shader before sending to the back-end compiler");
2148 if (!write_ir_text(ir, variant->nir_str))
2149 incomplete_text = true;
2150 }
2151 }
2152
2153 if (variant->asm_str != NULL) {
2154 vk_outarray_append_typed(VkPipelineExecutableInternalRepresentationKHR,
2155 &out, ir)
2156 {
2157 VK_COPY_STR(ir->name, "Assembly");
2158 VK_COPY_STR(ir->description, "Final Assembly");
2159 if (!write_ir_text(ir, variant->asm_str))
2160 incomplete_text = true;
2161 }
2162 }
2163
2164 return incomplete_text ? VK_INCOMPLETE : vk_outarray_status(&out);
2165 }
2166
2167 #if PAN_ARCH < 9
2168 static mali_pixel_format
2169 get_varying_format(mesa_shader_stage stage, gl_varying_slot loc,
2170 enum pipe_format pfmt)
2171 {
2172 switch (loc) {
2173 case VARYING_SLOT_PNTC:
2174 case VARYING_SLOT_PSIZ:
2175 #if PAN_ARCH <= 6
2176 return (MALI_R16F << 12) | pan_get_default_swizzle(1);
2177 #else
2178 return (MALI_R16F << 12) | MALI_RGB_COMPONENT_ORDER_R000;
2179 #endif
2180 case VARYING_SLOT_POS:
2181 #if PAN_ARCH <= 6
2182 return (MALI_SNAP_4 << 12) | pan_get_default_swizzle(4);
2183 #else
2184 return (MALI_SNAP_4 << 12) | MALI_RGB_COMPONENT_ORDER_RGBA;
2185 #endif
2186 default:
2187 assert(pfmt != PIPE_FORMAT_NONE);
2188 return GENX(pan_format_from_pipe_format)(pfmt)->hw;
2189 }
2190 }
2191
2192 static inline enum panvk_varying_buf_id
2193 varying_buf_id(gl_varying_slot loc)
2194 {
2195 switch (loc) {
2196 case VARYING_SLOT_POS:
2197 return PANVK_VARY_BUF_POSITION;
2198 case VARYING_SLOT_PSIZ:
2199 return PANVK_VARY_BUF_PSIZ;
2200 default:
2201 return PANVK_VARY_BUF_GENERAL;
2202 }
2203 }
2204
2205 static mali_pixel_format
2206 varying_format(gl_varying_slot loc, enum pipe_format pfmt)
2207 {
2208 switch (loc) {
2209 case VARYING_SLOT_PNTC:
2210 case VARYING_SLOT_PSIZ:
2211 #if PAN_ARCH <= 6
2212 return (MALI_R16F << 12) | pan_get_default_swizzle(1);
2213 #else
2214 return (MALI_R16F << 12) | MALI_RGB_COMPONENT_ORDER_R000;
2215 #endif
2216 case VARYING_SLOT_POS:
2217 #if PAN_ARCH <= 6
2218 return (MALI_SNAP_4 << 12) | pan_get_default_swizzle(4);
2219 #else
2220 return (MALI_SNAP_4 << 12) | MALI_RGB_COMPONENT_ORDER_RGBA;
2221 #endif
2222 default:
2223 return GENX(pan_format_from_pipe_format)(pfmt)->hw;
2224 }
2225 }
2226
2227 static VkResult
2228 emit_varying_attrs(struct panvk_pool *desc_pool,
2229 const struct pan_varying_layout *varyings,
2230 const unsigned *bit_sizes, const unsigned *buf_offsets,
2231 struct panvk_priv_mem *mem)
2232 {
2233 const unsigned varying_count = varyings->count;
2234 if (!varying_count) {
2235 *mem = (struct panvk_priv_mem){0};
2236 return VK_SUCCESS;
2237 }
2238
2239 *mem = panvk_pool_alloc_desc_array(desc_pool, varying_count, ATTRIBUTE);
2240 if (!panvk_priv_mem_check_alloc(*mem))
2241 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2242
2243 panvk_priv_mem_write_array(*mem, 0, struct mali_attribute_packed,
2244 varying_count, attrs) {
2245 unsigned attr_idx = 0;
2246
2247 for (unsigned i = 0; i < varying_count; i++) {
2248 pan_pack(&attrs[attr_idx++], ATTRIBUTE, cfg) {
2249 const struct pan_varying_slot *slot =
2250 pan_varying_layout_slot_at(varyings, i);
2251 if (!slot)
2252 continue;
2253
2254 gl_varying_slot loc = slot->location;
2255
2256 enum pipe_format pfmt = PIPE_FORMAT_NONE;
2257 bool is_hw = loc < VARYING_SLOT_VAR0;
2258 bool is_present = is_hw || bit_sizes[loc] != 0;
2259
2260 if (is_present && !is_hw) {
2261 nir_alu_type base_type =
2262 nir_alu_type_get_base_type(slot->alu_type);
2263 pfmt =
2264 pan_varying_format(base_type | bit_sizes[loc], slot->ncomps);
2265 }
2266
2267 if (!is_present) {
2268 #if PAN_ARCH >= 7
2269 cfg.format =
2270 (MALI_CONSTANT << 12) | MALI_RGB_COMPONENT_ORDER_0000;
2271 #else
2272 cfg.format = (MALI_CONSTANT << 12) | PAN_V6_SWIZZLE(0, 0, 0, 0);
2273 #endif
2274 } else {
2275 cfg.buffer_index = varying_buf_id(loc);
2276 cfg.offset = buf_offsets[loc];
2277 cfg.format = varying_format(loc, pfmt);
2278 }
2279 cfg.offset_enable = false;
2280 }
2281 }
2282 }
2283
2284 return VK_SUCCESS;
2285 }
2286
2287 VkResult
2288 panvk_per_arch(link_shaders)(struct panvk_pool *desc_pool,
2289 const struct panvk_shader_variant *vs,
2290 const struct panvk_shader_variant *fs,
2291 struct panvk_shader_link *link)
2292 {
2293 unsigned buf_strides[PANVK_VARY_BUF_MAX] = {0};
2294 unsigned buf_offsets[VARYING_SLOT_MAX] = {0};
2295 unsigned buf_sizes[VARYING_SLOT_MAX] = {0};
2296
2297 assert(vs && vs->info.stage == MESA_SHADER_VERTEX);
2298 assert(!fs || fs->info.stage == MESA_SHADER_FRAGMENT);
2299 const struct pan_varying_layout *vs_layout = &vs->info.varyings.formats;
2300 pan_varying_layout_require_layout(vs_layout);
2301
2302 /* Handle the position and point size buffers explicitly, as they are
2303 * passed through separate buffer pointers to the tiler job.
2304 */
2305 const struct pan_varying_slot *vs_pos_slot =
2306 pan_varying_layout_find_slot(vs_layout, VARYING_SLOT_POS);
2307 const struct pan_varying_slot *vs_psiz_slot =
2308 pan_varying_layout_find_slot(vs_layout, VARYING_SLOT_PSIZ);
2309
2310 if (vs_pos_slot) {
2311 buf_strides[PANVK_VARY_BUF_POSITION] = sizeof(uint32_t) * 4;
2312 buf_sizes[VARYING_SLOT_POS] =
2313 nir_alu_type_get_type_size(vs_pos_slot->alu_type);
2314 }
2315 if (vs_psiz_slot) {
2316 buf_strides[PANVK_VARY_BUF_PSIZ] = sizeof(uint16_t);
2317 buf_sizes[VARYING_SLOT_PSIZ] =
2318 nir_alu_type_get_type_size(vs_psiz_slot->alu_type);
2319 }
2320 buf_strides[PANVK_VARY_BUF_GENERAL] = vs_layout->generic_size_B;
2321
2322 /* When no fragment shader is present we can just ignore all
2323 * generic varyings writes.
2324 */
2325 const uint32_t fs_var_count = fs ? fs->info.varyings.formats.count : 0;
2326 for (uint32_t i = 0; i < fs_var_count; i++) {
2327 const struct pan_varying_slot *fs_var =
2328 pan_varying_layout_slot_at(&fs->info.varyings.formats, i);
2329 if (!fs_var)
2330 continue;
2331
2332 const gl_varying_slot pos = fs_var->location;
2333 /* Skip special varyings. */
2334 if (pos < VARYING_SLOT_VAR0)
2335 continue;
2336
2337 /* Special buffers are handled explicitly before this loop, everything
2338 * else should be laid out in the general varying buffer.
2339 */
2340 assert(varying_buf_id(pos) == PANVK_VARY_BUF_GENERAL);
2341
2342 const struct pan_varying_slot *vs_slot =
2343 pan_varying_layout_find_slot(vs_layout, fs_var->location);
2344 if (vs_slot) {
2345 buf_offsets[pos] = vs_slot->offset;
2346 buf_sizes[pos] = nir_alu_type_get_type_size(vs_slot->alu_type);
2347 }
2348 }
2349
2350 panvk_shader_link_cleanup(link);
2351 VkResult result = emit_varying_attrs(desc_pool, vs_layout, buf_sizes,
2352 buf_offsets, &link->vs.attribs);
2353 if (result != VK_SUCCESS)
2354 return result;
2355
2356 if (fs) {
2357 pan_varying_layout_require_format(&fs->info.varyings.formats);
2358 result = emit_varying_attrs(desc_pool, &fs->info.varyings.formats,
2359 buf_sizes, buf_offsets, &link->fs.attribs);
2360 if (result != VK_SUCCESS)
2361 return result;
2362 }
2363
2364 memcpy(link->buf_strides, buf_strides, sizeof(link->buf_strides));
2365 return VK_SUCCESS;
2366 }
2367 #endif
2368
2369 static const struct vk_shader_ops panvk_shader_ops = {
2370 .destroy = panvk_shader_destroy,
2371 .serialize = panvk_shader_serialize,
2372 .get_executable_properties = panvk_shader_get_executable_properties,
2373 .get_executable_statistics = panvk_shader_get_executable_statistics,
2374 .get_executable_internal_representations =
2375 panvk_shader_get_executable_internal_representations,
2376 };
2377
2378 static void
2379 panvk_cmd_bind_shader(struct panvk_cmd_buffer *cmd, const mesa_shader_stage stage,
2380 struct panvk_shader *shader)
2381 {
2382 switch (stage) {
2383 case MESA_SHADER_COMPUTE:
2384 if (cmd->state.compute.shader != shader) {
2385 cmd->state.compute.shader = shader;
2386 compute_state_set_dirty(cmd, CS);
2387 compute_state_set_dirty(cmd, PUSH_UNIFORMS);
2388 }
2389 break;
2390 case MESA_SHADER_VERTEX:
2391 if (cmd->state.gfx.vs.shader != shader) {
2392 cmd->state.gfx.vs.shader = shader;
2393 gfx_state_set_dirty(cmd, VS);
2394 gfx_state_set_dirty(cmd, VS_PUSH_UNIFORMS);
2395 }
2396 break;
2397 case MESA_SHADER_FRAGMENT:
2398 if (cmd->state.gfx.fs.shader != shader) {
2399 cmd->state.gfx.fs.shader = shader;
2400 gfx_state_set_dirty(cmd, FS);
2401 gfx_state_set_dirty(cmd, FS_PUSH_UNIFORMS);
2402 }
2403 break;
2404 default:
2405 assert(!"Unsupported stage");
2406 break;
2407 }
2408 }
2409
2410 static void
2411 panvk_cmd_bind_shaders(struct vk_command_buffer *vk_cmd, uint32_t stage_count,
2412 const mesa_shader_stage *stages,
2413 struct vk_shader **const shaders)
2414 {
2415 struct panvk_cmd_buffer *cmd =
2416 container_of(vk_cmd, struct panvk_cmd_buffer, vk);
2417
2418 for (uint32_t i = 0; i < stage_count; i++) {
2419 struct panvk_shader *shader =
2420 container_of(shaders[i], struct panvk_shader, vk);
2421
2422 panvk_cmd_bind_shader(cmd, stages[i], shader);
2423 }
2424 }
2425
2426 const struct vk_device_shader_ops panvk_per_arch(device_shader_ops) = {
2427 .get_nir_options = panvk_get_nir_options,
2428 .get_spirv_options = panvk_get_spirv_options,
2429 .preprocess_nir = panvk_preprocess_nir,
2430 .hash_state = panvk_hash_state,
2431 .compile = panvk_compile_shaders,
2432 .deserialize = panvk_deserialize_shader,
2433 .cmd_set_dynamic_graphics_state = vk_cmd_set_dynamic_graphics_state,
2434 .cmd_bind_shaders = panvk_cmd_bind_shaders,
2435 };
2436
2437 static void
2438 panvk_internal_shader_destroy(struct vk_device *vk_dev,
2439 struct vk_shader *vk_shader,
2440 const VkAllocationCallbacks *pAllocator)
2441 {
2442 struct panvk_device *dev = to_panvk_device(vk_dev);
2443 struct panvk_internal_shader *shader =
2444 container_of(vk_shader, struct panvk_internal_shader, vk);
2445
2446 panvk_pool_free_mem(&shader->code_mem);
2447
2448 #if PAN_ARCH < 9
2449 panvk_pool_free_mem(&shader->rsd);
2450 #else
2451 panvk_pool_free_mem(&shader->spd);
2452 #endif
2453
2454 vk_shader_free(&dev->vk, pAllocator, &shader->vk);
2455 }
2456
2457 static const struct vk_shader_ops panvk_internal_shader_ops = {
2458 .destroy = panvk_internal_shader_destroy,
2459 };
2460
2461 VkResult
2462 panvk_per_arch(create_internal_shader)(
2463 struct panvk_device *dev, nir_shader *nir,
2464 struct pan_compile_inputs *compiler_inputs,
2465 struct panvk_internal_shader **shader_out)
2466 {
2467 struct panvk_internal_shader *shader =
2468 vk_shader_zalloc(&dev->vk, &panvk_internal_shader_ops, nir->info.stage,
2469 NULL, sizeof(*shader));
2470 if (shader == NULL)
2471 return panvk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
2472
2473 VkResult result;
2474 struct util_dynarray binary;
2475
2476 panvk_per_arch(compiler_lock)();
2477
2478 util_dynarray_init(&binary, nir);
2479 pan_shader_compile(nir, compiler_inputs, &binary, &shader->info);
2480
2481 panvk_per_arch(compiler_unlock)();
2482
2483 unsigned bin_size = util_dynarray_num_elements(&binary, uint8_t);
2484 if (bin_size) {
2485 shader->code_mem = panvk_pool_upload_aligned(&dev->mempools.exec,
2486 binary.data, bin_size, 128);
2487 if (!panvk_priv_mem_check_alloc(shader->code_mem)) {
2488 result = panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
2489 goto err_free_shader;
2490 }
2491 }
2492
2493 *shader_out = shader;
2494 return VK_SUCCESS;
2495
2496 err_free_shader:
2497 vk_shader_free(&dev->vk, NULL, &shader->vk);
2498 return result;
2499 }