Debian Codegraph @

src/asahi/lib/agx_nir_lower_uvs.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "compiler/nir/nir.h"
7 #include "compiler/nir/nir_builder.h"
8 #include "util/bitscan.h"
9 #include "util/macros.h"
10 #include "agx_compile.h"
11 #include "agx_helpers.h"
12 #include "agx_pack.h"
13 #include "agx_uvs.h"
14 #include "nir_builder_opcodes.h"
15 #include "nir_intrinsics.h"
16 #include "nir_intrinsics_indices.h"
17 #include "shader_enums.h"
18
19 static bool
20 inline_uvs_index(nir_builder *b, nir_intrinsic_instr *intr, void *data)
21 {
22 struct agx_varyings_vs *varyings = data;
23 if (intr->intrinsic != nir_intrinsic_load_uvs_index_agx)
24 return false;
25
26 b->cursor = nir_before_instr(&intr->instr);
27 unsigned slot = nir_intrinsic_io_semantics(intr).location;
28
29 nir_def_replace(&intr->def, nir_imm_intN_t(b, varyings->slots[slot], 16));
30 return true;
31 }
32
33 struct ctx {
34 nir_def *layer, *viewport;
35 nir_cursor after_layer_viewport;
36 struct agx_unlinked_uvs_layout *layout;
37 };
38
39 static enum uvs_group
40 group_for_varying(gl_varying_slot loc)
41 {
42 switch (loc) {
43 case VARYING_SLOT_POS:
44 return UVS_POSITION;
45 case VARYING_SLOT_PSIZ:
46 return UVS_PSIZ;
47 default:
48 return UVS_VARYINGS;
49 }
50 }
51
52 static bool
53 lower(nir_builder *b, nir_intrinsic_instr *intr, void *data)
54 {
55 struct ctx *ctx = data;
56 if (intr->intrinsic != nir_intrinsic_store_output)
57 return false;
58
59 b->cursor = nir_instr_remove(&intr->instr);
60
61 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
62 unsigned component = nir_intrinsic_component(intr);
63
64 nir_def *value = intr->src[0].ssa;
65 nir_def *offset = intr->src[1].ssa;
66
67 /* If there is only 1 user varying, it is at the base of the varying section.
68 * This saves us an indirection on simple separate shaders.
69 */
70 bool single_vary = util_is_power_of_two_nonzero64(ctx->layout->written);
71 enum uvs_group group = group_for_varying(sem.location);
72
73 nir_def *base;
74 if ((group == UVS_VARYINGS) && !single_vary)
75 base = nir_load_uvs_index_agx(b, .io_semantics = sem);
76 else
77 base = nir_imm_intN_t(b, ctx->layout->group_offs[group], 16);
78
79 nir_def *index = nir_iadd(b, nir_iadd_imm(b, base, component),
80 nir_imul_imm(b, nir_u2u16(b, offset), 4));
81
82 if (sem.location != VARYING_SLOT_LAYER)
83 nir_store_uvs_agx(b, value, index);
84
85 /* Insert clip distance sysval writes, and gather layer/viewport writes so we
86 * can accumulate their system value. These are still lowered like normal to
87 * write them for the varying FS input.
88 */
89 if (sem.location == VARYING_SLOT_LAYER) {
90 assert(ctx->layer == NULL && "only written once");
91 ctx->layer = value;
92 ctx->after_layer_viewport = nir_after_def(index);
93 } else if (sem.location == VARYING_SLOT_VIEWPORT) {
94 assert(ctx->viewport == NULL && "only written once");
95 ctx->viewport = value;
96 ctx->after_layer_viewport = nir_after_def(index);
97 } else if (sem.location == VARYING_SLOT_CLIP_DIST0 ||
98 sem.location == VARYING_SLOT_CLIP_DIST1) {
99
100 unsigned clip_base = ctx->layout->group_offs[UVS_CLIP_DIST];
101 unsigned c = 4 * (sem.location - VARYING_SLOT_CLIP_DIST0) + component;
102
103 if (c < b->shader->info.clip_distance_array_size) {
104 nir_def *index = nir_iadd_imm(
105 b, nir_imul_imm(b, nir_u2u16(b, offset), 4), clip_base + c);
106
107 nir_store_uvs_agx(b, value, index);
108 }
109 }
110
111 /* Combined clip/cull used */
112 assert(sem.location != VARYING_SLOT_CULL_DIST0);
113 assert(sem.location != VARYING_SLOT_CULL_DIST1);
114
115 return true;
116 }
117
118 static void
119 write_layer_viewport_sysval(struct ctx *ctx)
120 {
121 nir_builder b = nir_builder_at(ctx->after_layer_viewport);
122
123 nir_def *zero = nir_imm_intN_t(&b, 0, 16);
124 nir_def *layer = ctx->layer ? nir_u2u16(&b, ctx->layer) : zero;
125 nir_def *viewport = ctx->viewport ? nir_u2u16(&b, ctx->viewport) : zero;
126
127 nir_store_uvs_agx(
128 &b, nir_pack_32_2x16_split(&b, layer, viewport),
129 nir_imm_int(&b, ctx->layout->group_offs[UVS_LAYER_VIEWPORT]));
130 }
131
132 static bool
133 gather_components(nir_builder *b, nir_intrinsic_instr *intr, void *data)
134 {
135 struct agx_unlinked_uvs_layout *layout = data;
136 if (intr->intrinsic != nir_intrinsic_store_output)
137 return false;
138
139 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
140 unsigned component = nir_intrinsic_component(intr);
141
142 if (nir_src_is_const(intr->src[1])) {
143 unsigned loc = sem.location + nir_src_as_uint(intr->src[1]);
144 layout->components[loc] = MAX2(layout->components[loc], component + 1);
145 } else {
146 for (unsigned i = 0; i < sem.num_slots; ++i) {
147 layout->components[sem.location + i] = 4;
148 }
149 }
150
151 return false;
152 }
153
154 bool
155 agx_nir_lower_uvs(nir_shader *s, struct agx_unlinked_uvs_layout *layout)
156 {
157 bool progress = false;
158
159 /* Scalarize up front so we can ignore vectors later */
160 NIR_PASS(progress, s, nir_lower_io_to_scalar, nir_var_shader_out, NULL,
161 NULL);
162
163 /* Determine the unlinked UVS layout */
164 NIR_PASS(progress, s, nir_shader_intrinsics_pass, gather_components,
165 nir_metadata_control_flow, layout);
166
167 unsigned sizes[UVS_NUM_GROUP] = {
168 [UVS_POSITION] = 4,
169 [UVS_PSIZ] = !!(s->info.outputs_written & VARYING_BIT_PSIZ),
170 [UVS_LAYER_VIEWPORT] = !!(s->info.outputs_written &
171 (VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT)),
172 [UVS_CLIP_DIST] = s->info.clip_distance_array_size,
173 };
174
175 for (unsigned i = 0; i < ARRAY_SIZE(layout->components); ++i) {
176 if (i != VARYING_SLOT_POS && i != VARYING_SLOT_PSIZ &&
177 i != VARYING_SLOT_LAYER && layout->components[i]) {
178
179 layout->written |= BITFIELD64_BIT(i);
180 sizes[UVS_VARYINGS] += layout->components[i];
181 }
182 }
183
184 unsigned offs = 0;
185 for (enum uvs_group g = 0; g < UVS_NUM_GROUP; ++g) {
186 layout->group_offs[g] = offs;
187 offs += sizes[g];
188 }
189
190 layout->size = offs;
191 layout->user_size = sizes[UVS_VARYINGS];
192
193 /* Now lower in terms of the unlinked layout */
194 struct ctx ctx = {.layout = layout};
195 NIR_PASS(progress, s, nir_shader_intrinsics_pass, lower,
196 nir_metadata_control_flow, &ctx);
197
198 if (ctx.layer || ctx.viewport) {
199 write_layer_viewport_sysval(&ctx);
200 }
201
202 /* Finally, pack what we can. It's much cheaper to do this at compile-time
203 * than draw-time.
204 */
205 agx_pack(&layout->osel, OUTPUT_SELECT, cfg) {
206 cfg.point_size = sizes[UVS_PSIZ];
207 cfg.viewport_target = sizes[UVS_LAYER_VIEWPORT];
208 cfg.render_target = cfg.viewport_target;
209
210 cfg.clip_distance_plane_0 = sizes[UVS_CLIP_DIST] > 0;
211 cfg.clip_distance_plane_1 = sizes[UVS_CLIP_DIST] > 1;
212 cfg.clip_distance_plane_2 = sizes[UVS_CLIP_DIST] > 2;
213 cfg.clip_distance_plane_3 = sizes[UVS_CLIP_DIST] > 3;
214 cfg.clip_distance_plane_4 = sizes[UVS_CLIP_DIST] > 4;
215 cfg.clip_distance_plane_5 = sizes[UVS_CLIP_DIST] > 5;
216 cfg.clip_distance_plane_6 = sizes[UVS_CLIP_DIST] > 6;
217 cfg.clip_distance_plane_7 = sizes[UVS_CLIP_DIST] > 7;
218 }
219
220 agx_pack(&layout->vdm, VDM_STATE_VERTEX_OUTPUTS, cfg) {
221 cfg.output_count_1 = offs;
222 cfg.output_count_2 = offs;
223 }
224
225 /* If we know the interpolation qualifiers in the vertex shader, we can
226 * inline UVS indices to eliminate some indirection.
227 */
228 if (s->info.known_interpolation_qualifiers) {
229 struct agx_varyings_vs v;
230 uint64_t flat = ~(s->info.linear_varyings | s->info.perspective_varyings);
231
232 agx_assign_uvs(&v, layout, flat, s->info.linear_varyings);
233
234 nir_shader_intrinsics_pass(s, inline_uvs_index, nir_metadata_control_flow,
235 &v);
236 }
237
238 return progress;
239 }
240
241 void
242 agx_assign_uvs(struct agx_varyings_vs *varyings,
243 struct agx_unlinked_uvs_layout *layout, uint64_t flat_mask,
244 uint64_t linear_mask)
245 {
246 *varyings = (struct agx_varyings_vs){0};
247
248 /* Layer/viewport always flat-shaded, no other special varyings are */
249 flat_mask &= BITFIELD64_RANGE(VARYING_SLOT_VAR0, 32);
250 flat_mask |= VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT;
251
252 /* The internal cull distance slots are always linearly-interpolated */
253 linear_mask |= BITFIELD64_RANGE(VARYING_SLOT_CULL_PRIMITIVE, 2);
254
255 assert(!(flat_mask & linear_mask & layout->written));
256
257 /* TODO: Link FP16 varyings */
258 unsigned num_32_smooth = 0, num_32_flat = 0, num_32_linear = 0;
259 struct {
260 uint32_t *num;
261 uint64_t mask;
262 } parts[] = {
263 {&num_32_smooth, ~flat_mask & ~linear_mask},
264 {&num_32_flat, flat_mask},
265 {&num_32_linear, linear_mask},
266 };
267
268 unsigned base = layout->group_offs[UVS_VARYINGS];
269
270 for (unsigned p = 0; p < ARRAY_SIZE(parts); ++p) {
271 u_foreach_bit64(loc, parts[p].mask & layout->written) {
272 assert(loc < ARRAY_SIZE(varyings->slots));
273 varyings->slots[loc] = base;
274
275 base += layout->components[loc];
276 (*parts[p].num) += layout->components[loc];
277 }
278 }
279
280 agx_pack(&varyings->counts_32, VARYING_COUNTS, cfg) {
281 cfg.smooth = num_32_smooth;
282 cfg.flat = num_32_flat;
283 cfg.linear = num_32_linear;
284 }
285
286 agx_pack(&varyings->counts_16, VARYING_COUNTS, cfg) {
287 cfg.smooth = 0;
288 cfg.flat = 0;
289 cfg.linear = 0;
290 }
291 }
292
293 static inline enum agx_shade_model
294 translate_flat_shade_model(unsigned provoking_vertex)
295 {
296 static_assert(AGX_SHADE_MODEL_FLAT_VERTEX_0 == 0, "hw");
297 static_assert(AGX_SHADE_MODEL_FLAT_VERTEX_2 == 2, "hw");
298
299 assert(provoking_vertex <= 2);
300
301 if (provoking_vertex == 1)
302 return AGX_SHADE_MODEL_FLAT_VERTEX_1;
303 else
304 return (enum agx_shade_model)provoking_vertex;
305 }
306
307 void
308 agx_link_varyings_vs_fs(void *out, struct agx_varyings_vs *vs,
309 unsigned nr_user_indices, struct agx_varyings_fs *fs,
310 unsigned provoking_vertex, uint8_t sprite_coord_enable,
311 bool *generate_primitive_id)
312 {
313 assert(fs->nr_bindings > 0);
314
315 *generate_primitive_id = false;
316
317 struct agx_cf_binding_header_packed *header = out;
318 struct agx_cf_binding_packed *bindings = (void *)(header + 1);
319
320 unsigned user_base = 1 + (fs->reads_z ? 1 : 0);
321 unsigned nr_slots = user_base + nr_user_indices;
322
323 agx_pack(header, CF_BINDING_HEADER, cfg) {
324 cfg.number_of_32_bit_slots = nr_slots;
325 cfg.number_of_coefficient_registers = fs->nr_cf;
326 }
327
328 for (unsigned i = 0; i < fs->nr_bindings; ++i) {
329 struct agx_cf_binding b = fs->bindings[i];
330
331 agx_pack(bindings + i, CF_BINDING, cfg) {
332 cfg.base_coefficient_register = b.cf_base;
333 cfg.components = b.count;
334
335 if (b.smooth) {
336 cfg.shade_model = b.perspective ? AGX_SHADE_MODEL_PERSPECTIVE
337 : AGX_SHADE_MODEL_LINEAR;
338 } else {
339 cfg.shade_model = translate_flat_shade_model(provoking_vertex);
340 }
341
342 if (b.slot == VARYING_SLOT_PNTC ||
343 (b.slot >= VARYING_SLOT_TEX0 && b.slot <= VARYING_SLOT_TEX7 &&
344 (sprite_coord_enable &
345 BITFIELD_BIT(b.slot - VARYING_SLOT_TEX0)))) {
346
347 assert(b.offset == 0);
348 cfg.source = AGX_COEFFICIENT_SOURCE_POINT_COORD;
349 } else if (b.slot == VARYING_SLOT_PRIMITIVE_ID &&
350 !vs->slots[VARYING_SLOT_PRIMITIVE_ID]) {
351 cfg.source = AGX_COEFFICIENT_SOURCE_PRIMITIVE_ID;
352 *generate_primitive_id = true;
353 } else if (b.slot == VARYING_SLOT_POS) {
354 assert(b.offset >= 2 && "gl_Position.xy are not varyings");
355 assert(fs->reads_z || b.offset != 2);
356
357 if (b.offset == 2) {
358 cfg.source = AGX_COEFFICIENT_SOURCE_FRAGCOORD_Z;
359 cfg.base_slot = 1;
360 } else {
361 assert(!b.perspective && "W must not be perspective divided");
362 }
363 } else {
364 unsigned vs_index = vs->slots[b.slot];
365 assert(b.offset < 4);
366
367 /* Varyings not written by vertex shader are undefined but we can't
368 * crash */
369 if (vs_index) {
370 assert(vs_index >= 4 &&
371 "gl_Position should have been the first 4 slots");
372
373 cfg.base_slot = user_base + (vs_index - 4) + b.offset;
374 }
375 }
376
377 assert(cfg.base_coefficient_register + cfg.components <= fs->nr_cf &&
378 "overflowed coefficient registers");
379 }
380 }
381 }