Debian Codegraph @

src/panfrost/compiler/pan_nir_lower_vs_outputs.c

raw
1 /*
2 * Copyright (C) 2025-2026 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "bifrost_compile.h"
7 #include "pan_nir.h"
8 #include "nir_builder.h"
9
10 #include "panfrost/model/pan_model.h"
11 #include "bifrost/valhall/valhall.h"
12 #include "util/bitscan.h"
13
14 struct lower_vs_outputs_ctx {
15 unsigned arch;
16 const struct pan_varying_layout *varying_layout;
17 bool has_idvs;
18 bool has_extended_fifo;
19
20 nir_variable *variables[PAN_MAX_VARYINGS];
21 uint8_t per_view_written[PAN_MAX_VARYINGS];
22 unsigned used_buckets;
23 bool uses_multiview;
24 };
25
26 static bool
27 valhal_writes_extended_fifo(uint64_t outputs_written,
28 bool no_psiz, bool multiview)
29 {
30 uint64_t ex_fifo_written = outputs_written & VALHAL_EX_FIFO_VARYING_BITS;
31 if (ex_fifo_written == 0)
32 return false;
33
34 /* Multiview shaders depend on the FIFO format for indexing per-view
35 * output writes. We don't currently patch these offsets in the no_psiz
36 * variant, so we need the extended format, regardless of point size.
37 */
38 if (multiview)
39 return true;
40
41 /* If we're not rendering in points mode, the no_psiz variant has point
42 * size write patched out for us.
43 */
44 if (no_psiz)
45 ex_fifo_written &= ~VARYING_BIT_PSIZ;
46
47 return ex_fifo_written != 0;
48 }
49
50 static void
51 build_attr_buf_write(struct nir_builder *b, nir_def *data, uint32_t idx,
52 uint32_t view_index,
53 const struct lower_vs_outputs_ctx *ctx)
54 {
55 /* We need the precise memory layout */
56 pan_varying_layout_require_layout(ctx->varying_layout);
57 const struct pan_varying_slot *slot =
58 pan_varying_layout_slot_at(ctx->varying_layout, idx);
59 assert(slot);
60
61 nir_def *index = nir_load_idvs_output_buf_index_pan(b);
62
63 uint32_t res, view_stride;
64 if (slot->section == PAN_VARYING_SECTION_GENERIC) {
65 /* The varying buffer is bound at index 1 on v12+ */
66 uint32_t res_index = ctx->arch >= 12 ? 1 : 0;
67 res = pan_res_handle(61, res_index);
68 view_stride = 4;
69 } else {
70 res = pan_res_handle(61, 0);
71 view_stride = ctx->has_extended_fifo ? 8 : 4;
72 }
73
74 uint32_t index_offset = view_index * view_stride;
75 if (slot->section == PAN_VARYING_SECTION_ATTRIBS)
76 index_offset += 4;
77
78 /* v9+ cache hints, generic varyings don't need caching while
79 * position/attribute varyings are reused by other units inside of the GPU.
80 * TODO: Do we really want ESTREAM on generic varyings?
81 */
82 enum gl_access_qualifier access =
83 slot->section == PAN_VARYING_SECTION_GENERIC ? ACCESS_ESTREAM_PAN :
84 ACCESS_ISTREAM_PAN;
85
86 index = nir_iadd_imm(b, index, index_offset);
87 nir_def *addr = nir_lea_buf_pan(b, nir_imm_int(b, res), index);
88 addr = nir_pack_64_2x32(b, addr);
89 addr = nir_iadd(b, addr, nir_imm_int64(b, slot->offset));
90
91 /* Tag writes to gl_PointSize with a special intrinsic */
92 if (slot->location == VARYING_SLOT_PSIZ) {
93 nir_store_global_psiz_pan(b, data, addr, .access = access);
94 } else {
95 nir_store_global(b, data, addr, .access = access);
96 }
97 }
98
99 static void
100 build_attr_desc_write(struct nir_builder *b, nir_def *data, uint32_t base,
101 nir_alu_type src_type,
102 const struct lower_vs_outputs_ctx *ctx)
103 {
104 nir_def *index = nir_imm_int(b, base);
105 nir_def *vertex_id = nir_load_raw_vertex_id_pan(b);
106 nir_def *instance_id = nir_load_instance_id(b);
107
108 nir_def *addr_cvt = nir_lea_attr_pan(b, index, vertex_id, instance_id,
109 .src_type = src_type);
110 nir_def *addr = nir_pack_64_2x32(b, nir_trim_vector(b, addr_cvt, 2));
111 nir_def *cvt = nir_channel(b, addr_cvt, 2);
112
113 nir_store_global_cvt_pan(b, data, addr, cvt, .src_type = src_type);
114 }
115
116 static void
117 build_store_output(struct nir_builder *b, nir_def *data, uint32_t idx,
118 nir_alu_type src_type,
119 const struct lower_vs_outputs_ctx *ctx)
120 {
121 pan_varying_layout_require_format(ctx->varying_layout);
122
123 const struct pan_varying_slot *slot =
124 pan_varying_layout_slot_at(ctx->varying_layout, idx);
125 assert(slot);
126
127 nir_io_semantics sem = {
128 .location = slot->location,
129 .num_slots = 1,
130 };
131
132 nir_store_output(b, data, nir_imm_int(b, 0), .base = idx, .range = 1,
133 .write_mask = BITFIELD_MASK(slot->ncomps), .component = 0,
134 .src_type = src_type, .io_semantics = sem);
135 }
136
137 static void
138 lower_vs_output_store(struct nir_builder *b,
139 unsigned view_index,
140 unsigned idx, const struct lower_vs_outputs_ctx *ctx)
141 {
142 nir_variable *var = ctx->variables[idx];
143 assert(var != NULL);
144
145 nir_alu_type src_type =
146 nir_get_nir_type_for_glsl_type(glsl_without_array(var->type));
147
148 bool is_per_view = glsl_type_is_array(var->type);
149 assert(is_per_view || view_index == 0);
150 nir_def *data = is_per_view ? nir_load_array_var_imm(b, var, view_index)
151 : nir_load_var(b, var);
152
153 if (ctx->arch >= 9 && ctx->has_idvs) {
154 build_attr_buf_write(b, data, idx, view_index, ctx);
155 } else if (ctx->arch >= 6) {
156 assert(!is_per_view);
157 build_attr_desc_write(b, data, idx, src_type, ctx);
158 } else {
159 assert(!is_per_view);
160 build_store_output(b, data, idx, src_type, ctx);
161 }
162 }
163
164 static nir_variable *
165 get_or_create_var(nir_builder *b, struct lower_vs_outputs_ctx *ctx,
166 nir_intrinsic_instr *intr)
167 {
168 assert(intr->intrinsic == nir_intrinsic_store_output ||
169 intr->intrinsic == nir_intrinsic_store_per_view_output);
170 bool is_per_view = intr->intrinsic == nir_intrinsic_store_per_view_output;
171 ASSERTED nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
172 unsigned slot_idx = nir_intrinsic_base(intr);
173 nir_alu_type src_type = nir_intrinsic_src_type(intr);
174 enum glsl_base_type base_type = nir_get_glsl_base_type_for_nir_type(src_type);
175
176 /* Indirect array varyings are not yet supported (num_slots > 1) */
177 assert(sem.num_slots == 1);
178 assert(nir_src_as_uint(*nir_get_io_offset_src(intr)) == 0);
179
180 nir_variable *var = ctx->variables[slot_idx];
181 if (var != NULL) {
182 /* All stores should agree per-location */
183 assert(glsl_type_is_array(var->type) == is_per_view);
184 assert(glsl_get_base_type(glsl_without_array(var->type)) == base_type);
185 return var;
186 }
187
188 /* We need the slot section for the number of components */
189 pan_varying_layout_require_format(ctx->varying_layout);
190 const struct pan_varying_slot *slot =
191 pan_varying_layout_slot_at(ctx->varying_layout, slot_idx);
192 /* Special slots are read only */
193 assert(slot && slot->section != PAN_VARYING_SECTION_SPECIAL &&
194 slot->location == sem.location);
195
196 const glsl_type *var_type = glsl_vector_type(base_type, slot->ncomps);
197 if (is_per_view)
198 var_type = glsl_array_type(var_type, PAN_MAX_MULTIVIEW_VIEW_COUNT, false);
199
200 var = nir_local_variable_create(b->impl, var_type, "vs_out_tmp");
201 ctx->variables[slot_idx] = var;
202 return var;
203 }
204
205 static bool
206 gather_vs_outputs(struct nir_builder *b,
207 nir_intrinsic_instr *intr, void *cb_data)
208 {
209 struct lower_vs_outputs_ctx *ctx = cb_data;
210
211 if (intr->intrinsic != nir_intrinsic_store_output &&
212 intr->intrinsic != nir_intrinsic_store_per_view_output)
213 return false;
214
215 unsigned mask = nir_intrinsic_write_mask(intr);
216 unsigned slot_idx = nir_intrinsic_base(intr);
217 unsigned component = nir_intrinsic_component(intr);
218 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
219
220 bool is_per_view = intr->intrinsic == nir_intrinsic_store_per_view_output;
221 unsigned view_index = is_per_view ? nir_src_as_uint(intr->src[1]) : 0;
222
223 ctx->per_view_written[slot_idx] |= BITFIELD_BIT(view_index);
224 ctx->used_buckets |= BITFIELD_BIT(va_shader_output_from_loc(sem.location));
225 ctx->uses_multiview |= is_per_view;
226
227 b->cursor = nir_instr_remove(&intr->instr);
228 nir_variable *var = get_or_create_var(b, ctx, intr);
229
230 unsigned ncomps = glsl_get_vector_elements(glsl_without_array(var->type));
231
232 /* store_output semantics differ from store_var when we write a subset of
233 * the components, we need to re-swizzle the channels manually.
234 */
235 nir_def *stored = intr->src[0].ssa;
236 if (stored->num_components != ncomps) {
237 nir_def *undef = nir_undef(b, 1, stored->bit_size);
238 assert(ncomps <= 4);
239 nir_def *channels[4] = {undef, undef, undef, undef};
240
241 for (unsigned i = 0; i < stored->num_components; i++) {
242 assert(component + i < 4);
243 channels[component + i] = nir_channel(b, stored, i);
244 }
245
246 stored = nir_vec(b, channels, ncomps);
247 mask = mask << component; /* mask was relative to intr->component */
248 } else {
249 assert(component == 0);
250 }
251
252 if (is_per_view)
253 nir_store_array_var_imm(b, var, view_index, stored, mask);
254 else
255 nir_store_var(b, var, stored, mask);
256
257 return true;
258 }
259
260 bool
261 pan_nir_lower_vs_outputs(nir_shader *shader, uint64_t gpu_id,
262 const struct pan_varying_layout *varying_layout,
263 bool has_idvs, bool *needs_extended_fifo)
264 {
265 assert(shader->info.stage == MESA_SHADER_VERTEX);
266 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
267
268 struct lower_vs_outputs_ctx ctx = {
269 .arch = pan_arch(gpu_id),
270 .varying_layout = varying_layout,
271 .has_idvs = has_idvs,
272 .has_extended_fifo = false, /* computed after gather_vs_outputs */
273 .variables = {NULL, },
274 .per_view_written = {0, },
275 .used_buckets = 0,
276 .uses_multiview = false,
277 };
278 /* We use uint8 as a viewcount bitmask */
279 assert(PAN_MAX_MULTIVIEW_VIEW_COUNT <= 8 * sizeof(ctx.per_view_written[0]));
280 bool progress = nir_shader_intrinsics_pass(shader, gather_vs_outputs,
281 nir_metadata_control_flow,
282 (void *)&ctx);
283 if (!progress)
284 return false;
285
286 /* Should we use extended FIFO? */
287 if (ctx.arch >= 9) {
288 assert(needs_extended_fifo);
289 const uint64_t outputs = shader->info.outputs_written;
290 ctx.has_extended_fifo =
291 valhal_writes_extended_fifo(outputs, false, ctx.uses_multiview);
292 /* Export if we need ex_fifo even without psiz. The backend needs to
293 * know this because we can patch psiz out.
294 */
295 *needs_extended_fifo =
296 valhal_writes_extended_fifo(outputs, true, ctx.uses_multiview);
297 }
298
299 nir_builder builder = nir_builder_at(nir_after_impl(impl));
300 nir_builder *b = &builder;
301 nir_def *shader_output = has_idvs ? nir_load_shader_output_pan(b) : NULL;
302
303 for (int out = 0; out < VA_SHADER_OUTPUT_COUNT; out++) {
304 /* Avoid looping a lot and adding ifs (in case of IDVS) */
305 if (!(ctx.used_buckets & BITFIELD_BIT(out)))
306 continue;
307
308 nir_if *nif = NULL;
309 if (has_idvs) {
310 nir_def *should_write =
311 nir_i2b(b, nir_iand_imm(b, shader_output, BITFIELD_BIT(out)));
312 nif = nir_push_if(b, should_write);
313 }
314
315 for (unsigned idx = 0; idx < varying_layout->count; idx++) {
316 nir_variable *var = ctx.variables[idx];
317 if (var == NULL)
318 continue;
319
320 const struct pan_varying_slot *slot =
321 pan_varying_layout_slot_at(varying_layout, idx);
322 if (slot == NULL || va_shader_output_from_loc(slot->location) != out)
323 continue;
324
325 /* Non-multiview stores are treated as writing just view index 0 */
326 unsigned view_mask = ctx.per_view_written[idx];
327 assert(view_mask != 0);
328
329 u_foreach_bit(view_index, view_mask)
330 lower_vs_output_store(b, view_index, idx, &ctx);
331 }
332
333 if (nif)
334 nir_pop_if(b, nif);
335 }
336
337 return nir_progress(true, impl, nir_metadata_none);
338 }