Debian Codegraph @

src/panfrost/compiler/pan_nir_collect_varyings.c

raw
1 /*
2 * Copyright (c) 2022 Amazon.com, Inc. or its affiliates.
3 * Copyright (C) 2019-2022,2026 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "compiler/nir/nir.h"
8 #include "compiler/nir/nir_builder.h"
9 #include "pan_nir.h"
10 #include "panfrost/model/pan_model.h"
11
12 enum pipe_format
13 pan_varying_format(nir_alu_type t, unsigned ncomps)
14 {
15 assert(ncomps >= 1 && ncomps <= 4);
16
17 #define VARYING_FORMAT(ntype, nsz, ptype, psz) \
18 { \
19 .type = nir_type_##ntype##nsz, .formats = { \
20 PIPE_FORMAT_R##psz##_##ptype, \
21 PIPE_FORMAT_R##psz##G##psz##_##ptype, \
22 PIPE_FORMAT_R##psz##G##psz##B##psz##_##ptype, \
23 PIPE_FORMAT_R##psz##G##psz##B##psz##A##psz##_##ptype, \
24 } \
25 }
26
27 static const struct {
28 nir_alu_type type;
29 enum pipe_format formats[4];
30 } conv[] = {
31 VARYING_FORMAT(float, 32, FLOAT, 32),
32 VARYING_FORMAT(uint, 32, UINT, 32),
33 VARYING_FORMAT(int, 32, SINT, 32),
34 VARYING_FORMAT(float, 16, FLOAT, 16),
35 VARYING_FORMAT(uint, 16, UINT, 16),
36 VARYING_FORMAT(int, 16, SINT, 16),
37 };
38 #undef VARYING_FORMAT
39
40 assert(ncomps > 0 && ncomps <= ARRAY_SIZE(conv[0].formats));
41
42 for (unsigned i = 0; i < ARRAY_SIZE(conv); i++) {
43 if (conv[i].type == t)
44 return conv[i].formats[ncomps - 1];
45 }
46
47 UNREACHABLE("Invalid type");
48 }
49
50 struct slot_info {
51 nir_alu_type type;
52 bool any_highp;
53 unsigned count;
54 unsigned index;
55 };
56
57 struct walk_varyings_data {
58 struct slot_info *slots;
59 bool trust_varying_flat_highp_types;
60 };
61
62 static bool
63 walk_varyings(UNUSED nir_builder *b, nir_instr *instr, void *data)
64 {
65 struct walk_varyings_data *wv_data = data;
66 struct slot_info *slots = wv_data->slots;
67
68 if (instr->type != nir_instr_type_intrinsic)
69 return false;
70
71 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
72 unsigned count;
73 nir_alu_type type;
74 bool is_store;
75
76 /* Only consider intrinsics that access varyings */
77 switch (intr->intrinsic) {
78 case nir_intrinsic_store_output:
79 case nir_intrinsic_store_per_view_output:
80 if (b->shader->info.stage != MESA_SHADER_VERTEX)
81 return false;
82
83 count = nir_src_num_components(intr->src[0]);
84 type = nir_intrinsic_src_type(intr);
85 is_store = true;
86 break;
87
88 case nir_intrinsic_load_input:
89 case nir_intrinsic_load_interpolated_input:
90 if (b->shader->info.stage != MESA_SHADER_FRAGMENT)
91 return false;
92
93 count = intr->def.num_components;
94 type = nir_intrinsic_dest_type(intr);
95 is_store = false;
96 break;
97
98 default:
99 return false;
100 }
101
102 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
103
104 if (sem.no_varying)
105 return false;
106
107 nir_alu_type base_type = nir_alu_type_get_base_type(type);
108 unsigned size = nir_alu_type_get_type_size(type);
109 assert(base_type & (nir_type_int | nir_type_uint | nir_type_float));
110
111 bool untrusted_type = !wv_data->trust_varying_flat_highp_types &&
112 sem.location >= VARYING_SLOT_VAR0 &&
113 !sem.medium_precision &&
114 !b->shader->info.separate_shader;
115 if (untrusted_type) {
116 /* Don't trust the type, varying_opts might have smashed everything
117 * onto floats. Replace all flat varyings with ints and smooth varyings
118 * with floats, only exception is 16-bit flat varyings that should be
119 * stored/loaded as ints as the hardware cannot encode 16-bit flat ints.
120 * Read docs/drivers/panfrost/varyings.rst for details.
121 */
122 bool is_flat = intr->intrinsic != nir_intrinsic_load_interpolated_input;
123 base_type = (is_flat && size == 32) ? nir_type_uint : nir_type_float;
124 type = base_type | size;
125 if (is_store)
126 nir_intrinsic_set_src_type(intr, type);
127 else
128 nir_intrinsic_set_dest_type(intr, type);
129 }
130
131 /* Count currently contains the number of components accessed by this
132 * intrinsics. However, we may be accessing a fractional location,
133 * indicating by the NIR component. Add that in. The final value be the
134 * maximum (component + count), an upper bound on the number of
135 * components possibly used.
136 */
137 count += nir_intrinsic_component(intr);
138
139 /* Consider each slot separately */
140 for (unsigned offset = 0; offset < sem.num_slots; ++offset) {
141 unsigned location = sem.location + offset;
142 unsigned index = pan_res_handle_get_index(nir_intrinsic_base(intr)) + offset;
143
144 if (slots[location].type) {
145 assert(slots[location].type == type);
146 assert(slots[location].index == index);
147 } else {
148 slots[location].type = type;
149 slots[location].index = index;
150 }
151
152 if (size == 32 && !sem.medium_precision)
153 slots[location].any_highp = true;
154
155 slots[location].count = MAX2(slots[location].count, count);
156 }
157
158 return false;
159 }
160
161 static bool
162 collect_noperspective_varyings_fs(UNUSED nir_builder *b,
163 nir_intrinsic_instr *intr,
164 void *data)
165 {
166 uint32_t *noperspective_varyings = data;
167
168 if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
169 return false;
170
171 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
172 if (sem.location < VARYING_SLOT_VAR0)
173 return false;
174
175 nir_intrinsic_instr *bary_instr = nir_src_as_intrinsic(intr->src[0]);
176 assert(bary_instr);
177 if (nir_intrinsic_interp_mode(bary_instr) == INTERP_MODE_NOPERSPECTIVE) {
178 unsigned loc = sem.location - VARYING_SLOT_VAR0;
179 *noperspective_varyings |= BITFIELD_RANGE(loc, sem.num_slots);
180 }
181
182 return false;
183 }
184
185 uint32_t
186 pan_nir_collect_noperspective_varyings_fs(nir_shader *s)
187 {
188 assert(s->info.stage == MESA_SHADER_FRAGMENT);
189
190 uint32_t noperspective_varyings = 0;
191
192 /* Collect from variables */
193 nir_foreach_shader_in_variable(var, s) {
194 if (var->data.location < VARYING_SLOT_VAR0)
195 continue;
196
197 if (var->data.interpolation != INTERP_MODE_NOPERSPECTIVE)
198 continue;
199
200 unsigned loc = var->data.location - VARYING_SLOT_VAR0;
201 unsigned slots = glsl_count_attribute_slots(var->type, false);
202 noperspective_varyings |= BITFIELD_RANGE(loc, slots);
203 }
204
205 /* And collect from load_interpolated_input intrinsics */
206 nir_shader_intrinsics_pass(s, collect_noperspective_varyings_fs,
207 nir_metadata_all,
208 (void *)&noperspective_varyings);
209
210 return noperspective_varyings;
211 }
212
213 static const struct pan_varying_slot hw_varying_slots[] = {{
214 .location = VARYING_SLOT_POS,
215 .alu_type = nir_type_float32,
216 .ncomps = 4,
217 .section = PAN_VARYING_SECTION_POSITION,
218 .offset = 0,
219 }, {
220 .location = VARYING_SLOT_PSIZ,
221 .alu_type = nir_type_float16,
222 .ncomps = 1,
223 .section = PAN_VARYING_SECTION_ATTRIBS,
224 .offset = 0,
225 }, {
226 .location = VARYING_SLOT_LAYER,
227 .alu_type = nir_type_uint8,
228 .ncomps = 1,
229 .section = PAN_VARYING_SECTION_ATTRIBS,
230 .offset = 2,
231 }, {
232 .location = VARYING_SLOT_VIEWPORT,
233 .alu_type = nir_type_uint8,
234 .ncomps = 1,
235 .section = PAN_VARYING_SECTION_ATTRIBS,
236 .offset = 2,
237 }, {
238 .location = VARYING_SLOT_PRIMITIVE_ID,
239 .alu_type = nir_type_uint32,
240 .ncomps = 1,
241 .section = PAN_VARYING_SECTION_ATTRIBS,
242 .offset = 12,
243 }};
244
245 /* On Midgard some attributes are computed on-the-fly from the drawing state,
246 * those are called special and require a custom descriptor definition.
247 * From v6 onwards those use the LD_VAR_SPECIAL instruction.
248 * Also on Midgard, VARYING_SLOT_TEX* might be point coordinates depending on
249 * the rasterizer state, if they are they should be theoretically in the special
250 * section. Since we don't know this yet we "misplace" them in the generic
251 * section anyway, they won't end up in the memory layout and they'll be handled
252 * by the descriptor emitter code.
253 * It's not a mistake, just a "happy little accident".
254 */
255 static const struct pan_varying_slot special_varying_slots[] = {{
256 .location = VARYING_SLOT_POS,
257 .alu_type = nir_type_float32,
258 .ncomps = 4,
259 .section = PAN_VARYING_SECTION_SPECIAL,
260 .offset = 0,
261 }, {
262 .location = VARYING_SLOT_PNTC,
263 .alu_type = nir_type_float32,
264 .ncomps = 1,
265 .section = PAN_VARYING_SECTION_SPECIAL,
266 .offset = 0,
267 }, {
268 .location = VARYING_SLOT_FACE,
269 .alu_type = nir_type_uint32,
270 .ncomps = 1,
271 .section = PAN_VARYING_SECTION_SPECIAL,
272 .offset = 0,
273 }};
274
275 static struct pan_varying_slot
276 hw_varying_slot(unsigned arch, mesa_shader_stage stage, gl_varying_slot slot)
277 {
278 bool vs_pos = slot == VARYING_SLOT_POS && stage == MESA_SHADER_VERTEX;
279 /* pos is only special in fragment shader input, not vertex shader output */
280 if (arch < 6 && !vs_pos) {
281 for (unsigned i = 0; i < ARRAY_SIZE(special_varying_slots); i++) {
282 if (special_varying_slots[i].location == slot)
283 return special_varying_slots[i];
284 }
285 }
286 for (unsigned i = 0; i < ARRAY_SIZE(hw_varying_slots); i++) {
287 if (hw_varying_slots[i].location == slot)
288 return hw_varying_slots[i];
289 }
290 UNREACHABLE("Invalid HW varying slot");
291 }
292
293 void
294 pan_varying_collect_formats(struct pan_varying_layout *layout, nir_shader *nir,
295 uint64_t gpu_id,
296 bool trust_varying_flat_highp_types,
297 bool lower_mediump)
298 {
299 assert(nir->info.stage == MESA_SHADER_VERTEX ||
300 nir->info.stage == MESA_SHADER_FRAGMENT);
301 memset(layout, 0, sizeof(*layout));
302
303 struct slot_info slots[64] = {0};
304 struct walk_varyings_data wv_data = {
305 .slots = slots,
306 .trust_varying_flat_highp_types = trust_varying_flat_highp_types,
307 };
308
309 nir_shader_instructions_pass(nir, walk_varyings, nir_metadata_all, &wv_data);
310
311 const unsigned gpu_arch = pan_arch(gpu_id);
312 unsigned count = 0;
313 for (unsigned i = 0; i < ARRAY_SIZE(slots); i++) {
314 if (!slots[i].type)
315 continue;
316
317 /* It's possible that something has been dead code eliminated between
318 * when the driver locations were set on variables and here. Don't
319 * trust our compaction to match the driver. Just copy over the index
320 * and accept that there's a hole in the mapping.
321 */
322 unsigned idx = slots[i].index;
323 count = MAX2(count, idx + 1);
324 assert(count <= ARRAY_SIZE(layout->slots));
325 assert(layout->slots[idx].alu_type == nir_type_invalid);
326
327 if (BITFIELD64_BIT(i) & PAN_HARDWARE_VARYING_BITS) {
328 layout->slots[idx] = hw_varying_slot(gpu_arch, nir->info.stage, i);
329 } else {
330 nir_alu_type type = nir_alu_type_get_base_type(slots[i].type);
331 unsigned bit_size = nir_alu_type_get_type_size(slots[i].type);
332
333 /* The Vulkan spec requires types to match across all uses of a
334 * location but doesn't actually require RelaxedPrecision to match
335 * for the whole location. So we can only apply mediump if every use
336 * of the location is mediump.
337 * Don't lower mediump integers, it has no measured impact and causes
338 * lots of bugs due to gallium shenanigans.
339 * Also allow the client to remove mediump lowering and keep the
340 * original types
341 */
342 bool can_lower_size = lower_mediump &&
343 bit_size == 32 &&
344 type == nir_type_float &&
345 !slots[i].any_highp;
346 if (can_lower_size)
347 bit_size = 16;
348
349 layout->slots[idx] = (struct pan_varying_slot){
350 .location = i,
351 .alu_type = type | bit_size,
352 .ncomps = slots[i].count,
353 .section = PAN_VARYING_SECTION_GENERIC,
354 /* Don't know the offset yet */
355 .offset = -1,
356 };
357 }
358 }
359 layout->count = count;
360 layout->generic_size_B = 0;
361 layout->known |= PAN_VARYING_FORMAT_KNOWN;
362 }
363
364 void
365 pan_build_varying_layout_compact(struct pan_varying_layout *layout,
366 nir_shader *nir, uint64_t gpu_id)
367 {
368 pan_varying_layout_require_format(layout);
369
370 const unsigned gpu_arch = pan_arch(gpu_id);
371 unsigned generic_size_B = 0;
372 for (unsigned i = 0; i < layout->count; i++) {
373 struct pan_varying_slot *slot = &layout->slots[i];
374 if (pan_varying_slot_is_empty(slot))
375 continue;
376
377 if (slot->section != PAN_VARYING_SECTION_GENERIC) {
378 ASSERTED const struct pan_varying_slot hw_slot =
379 hw_varying_slot(gpu_arch, nir->info.stage, slot->location);
380
381 assert(memcmp(slot, &hw_slot, sizeof(*slot)) == 0);
382 } else {
383 unsigned bit_size = nir_alu_type_get_type_size(slot->alu_type);
384
385 unsigned size = slot->ncomps * (bit_size / 8);
386 unsigned alignment = util_next_power_of_two(size);
387 unsigned offset = align(generic_size_B, alignment);
388 generic_size_B = offset + size;
389
390 assert(slot->offset == -1);
391 assert(offset < 4096);
392 slot->offset = offset;
393 }
394 }
395 layout->generic_size_B = generic_size_B;
396 layout->known |= PAN_VARYING_LAYOUT_KNOWN;
397 }