Debian Codegraph @

src/asahi/lib/agx_nir_lower_vbo.c

raw
1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "agx_nir_lower_vbo.h"
7 #include "asahi/layout/layout.h"
8 #include "compiler/nir/nir_builder.h"
9 #include "compiler/nir/nir_format_convert.h"
10 #include "util/bitset.h"
11 #include "util/u_math.h"
12 #include "agx_linker.h"
13 #include "shader_enums.h"
14
15 struct ctx {
16 const struct agx_velem_key *attribs;
17 struct agx_robustness rs;
18 bool dynamic_strides;
19 };
20
21 static bool
22 is_rgb10_a2(const struct util_format_description *desc)
23 {
24 return desc->channel[0].shift == 0 && desc->channel[0].size == 10 &&
25 desc->channel[1].shift == 10 && desc->channel[1].size == 10 &&
26 desc->channel[2].shift == 20 && desc->channel[2].size == 10 &&
27 desc->channel[3].shift == 30 && desc->channel[3].size == 2;
28 }
29
30 enum pipe_format
31 agx_vbo_internal_format(enum pipe_format format)
32 {
33 const struct util_format_description *desc = util_format_description(format);
34
35 /* RGB10A2 formats are native for UNORM and unpacked otherwise */
36 if (is_rgb10_a2(desc)) {
37 if (desc->is_unorm)
38 return PIPE_FORMAT_R10G10B10A2_UNORM;
39 else
40 return PIPE_FORMAT_R32_UINT;
41 }
42
43 /* R11G11B10F is native and special */
44 if (format == PIPE_FORMAT_R11G11B10_FLOAT)
45 return format;
46
47 /* No other non-array formats handled */
48 if (!desc->is_array)
49 return PIPE_FORMAT_NONE;
50
51 /* Otherwise look at one (any) channel */
52 int idx = util_format_get_first_non_void_channel(format);
53 if (idx < 0)
54 return PIPE_FORMAT_NONE;
55
56 /* We only handle RGB formats (we could do SRGB if we wanted though?) */
57 if ((desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB) ||
58 (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN))
59 return PIPE_FORMAT_NONE;
60
61 /* We have native 8-bit and 16-bit normalized formats */
62 struct util_format_channel_description chan = desc->channel[idx];
63
64 if (chan.normalized) {
65 if (chan.size == 8)
66 return desc->is_unorm ? PIPE_FORMAT_R8_UNORM : PIPE_FORMAT_R8_SNORM;
67 else if (chan.size == 16)
68 return desc->is_unorm ? PIPE_FORMAT_R16_UNORM : PIPE_FORMAT_R16_SNORM;
69 }
70
71 /* Otherwise map to the corresponding integer format */
72 switch (chan.size) {
73 case 32:
74 return PIPE_FORMAT_R32_UINT;
75 case 16:
76 return PIPE_FORMAT_R16_UINT;
77 case 8:
78 return PIPE_FORMAT_R8_UINT;
79 default:
80 return PIPE_FORMAT_NONE;
81 }
82 }
83
84 bool
85 agx_vbo_supports_format(enum pipe_format format)
86 {
87 return agx_vbo_internal_format(format) != PIPE_FORMAT_NONE;
88 }
89
90 static nir_def *
91 apply_swizzle_channel(nir_builder *b, nir_def *vec, unsigned swizzle,
92 bool is_int)
93 {
94 switch (swizzle) {
95 case PIPE_SWIZZLE_X:
96 return nir_channel(b, vec, 0);
97 case PIPE_SWIZZLE_Y:
98 return nir_channel(b, vec, 1);
99 case PIPE_SWIZZLE_Z:
100 return nir_channel(b, vec, 2);
101 case PIPE_SWIZZLE_W:
102 return nir_channel(b, vec, 3);
103 case PIPE_SWIZZLE_0:
104 return nir_imm_intN_t(b, 0, vec->bit_size);
105 case PIPE_SWIZZLE_1:
106 return is_int ? nir_imm_intN_t(b, 1, vec->bit_size)
107 : nir_imm_floatN_t(b, 1.0, vec->bit_size);
108 default:
109 UNREACHABLE("Invalid swizzle channel");
110 }
111 }
112
113 static bool
114 pass(struct nir_builder *b, nir_intrinsic_instr *intr, void *data)
115 {
116 if (intr->intrinsic != nir_intrinsic_load_input)
117 return false;
118
119 struct ctx *ctx = data;
120 const struct agx_velem_key *attribs = ctx->attribs;
121 b->cursor = nir_instr_remove(&intr->instr);
122
123 nir_src *offset_src = nir_get_io_offset_src(intr);
124 assert(nir_src_is_const(*offset_src) && "no attribute indirects");
125 unsigned index = nir_intrinsic_base(intr) + nir_src_as_uint(*offset_src);
126
127 struct agx_velem_key attrib = attribs[index];
128 uint32_t stride = attrib.stride;
129
130 const struct util_format_description *desc =
131 util_format_description(attrib.format);
132 int chan = util_format_get_first_non_void_channel(attrib.format);
133 assert(chan >= 0);
134
135 bool is_float = desc->channel[chan].type == UTIL_FORMAT_TYPE_FLOAT;
136 bool is_unsigned = desc->channel[chan].type == UTIL_FORMAT_TYPE_UNSIGNED;
137 bool is_signed = desc->channel[chan].type == UTIL_FORMAT_TYPE_SIGNED;
138 bool is_fixed = desc->channel[chan].type == UTIL_FORMAT_TYPE_FIXED;
139 bool is_int = util_format_is_pure_integer(attrib.format);
140
141 assert((is_float ^ is_unsigned ^ is_signed ^ is_fixed) && "Invalid format");
142
143 enum pipe_format interchange_format = agx_vbo_internal_format(attrib.format);
144 assert(interchange_format != PIPE_FORMAT_NONE);
145
146 unsigned interchange_align = util_format_get_blocksize(interchange_format);
147 unsigned interchange_comps = util_format_get_nr_components(attrib.format);
148
149 /* In the hardware, uint formats zero-extend and float formats convert.
150 * However, non-uint formats using a uint interchange format shouldn't be
151 * zero extended.
152 */
153 unsigned interchange_register_size =
154 util_format_is_pure_uint(interchange_format) &&
155 !util_format_is_pure_uint(attrib.format)
156 ? (interchange_align * 8)
157 : intr->def.bit_size;
158
159 /* Non-UNORM R10G10B10A2 loaded as a scalar and unpacked */
160 if (interchange_format == PIPE_FORMAT_R32_UINT && !desc->is_array)
161 interchange_comps = 1;
162
163 /* Calculate the element to fetch the vertex for. Divide the instance ID by
164 * the divisor for per-instance data. Divisor=0 specifies per-vertex data.
165 */
166 nir_def *el;
167 if (attrib.instanced) {
168 if (attrib.divisor > 0)
169 el = nir_udiv_imm(b, nir_load_instance_id(b), attrib.divisor);
170 else
171 el = nir_imm_int(b, 0);
172
173 el = nir_iadd(b, el, nir_load_base_instance(b));
174
175 BITSET_SET(b->shader->info.system_values_read,
176 SYSTEM_VALUE_BASE_INSTANCE);
177 } else {
178 el = nir_load_vertex_id(b);
179 }
180
181 /* VBO bases are per-attribute, otherwise they're per-buffer. This allows
182 * memory sinks to work properly with robustness, allows folding
183 * the src_offset into the VBO base to save an add in the shader, and reduces
184 * the size of the vertex fetch key. That last piece allows reusing a linked
185 * VS with both separate and interleaved attributes.
186 */
187 nir_def *buf_handle = nir_imm_int(b, index);
188
189 /* Robustness is handled at the ID level */
190 nir_def *bounds = nir_load_attrib_clamp_agx(b, buf_handle);
191 nir_def *oob = nir_ult(b, bounds, el);
192
193 /* We clamp to handle GL robustness. This should be optimized further.
194 * However, with the fix up after the load for D3D robustness, we don't need
195 * this clamp if we can ignore the fault.
196 */
197 if (ctx->rs.level >= AGX_ROBUSTNESS_GL &&
198 !(ctx->rs.level >= AGX_ROBUSTNESS_D3D && ctx->rs.soft_fault)) {
199
200 el = nir_bcsel(b, oob, nir_imm_int(b, 0), el);
201 }
202
203 nir_def *base = nir_load_vbo_base_agx(b, buf_handle);
204
205 assert((stride % interchange_align) == 0 && "must be aligned");
206
207 unsigned stride_el = stride / interchange_align;
208 unsigned shift = 0;
209
210 /* Try to use the small shift on the load itself when possible. This can save
211 * an instruction. Shifts are only available for regular interchange formats,
212 * i.e. the set of formats that support masking.
213 */
214 if ((stride_el == 2 || stride_el == 4) &&
215 ail_isa_format_supports_mask((enum ail_isa_format)interchange_format)) {
216
217 shift = util_logbase2(stride_el);
218 stride_el = 1;
219 }
220
221 nir_def *stride_el_def = nir_imm_int(b, stride_el);
222 if (ctx->dynamic_strides) {
223 assert(stride_el == 0);
224 stride_el_def = nir_load_vbo_stride_agx(b, buf_handle);
225 }
226
227 nir_def *stride_offset_el = nir_imul(b, el, stride_el_def);
228
229 /* Fixing up the address is expected to be profitable for vec3 and above, as
230 * it requires 2 instructions. It is implemented with a 64GiB carveout at the
231 * bottom of memory, using soft fault to return zeroes.
232 */
233 bool rs_address_fixup = interchange_comps > 2 && ctx->rs.soft_fault;
234
235 if (ctx->rs.level >= AGX_ROBUSTNESS_D3D && rs_address_fixup) {
236 base = nir_bcsel(b, oob, nir_imm_int64(b, 0), base);
237 }
238
239 /* Load the raw vector */
240 nir_def *memory = nir_load_constant_agx(
241 b, interchange_comps, interchange_register_size, base, stride_offset_el,
242 .format = interchange_format, .base = shift);
243
244 /* For scalar loads, it's faster to fix up the output than the address. */
245 if (ctx->rs.level >= AGX_ROBUSTNESS_D3D && !rs_address_fixup) {
246 nir_def *zero = nir_imm_zero(b, memory->num_components, memory->bit_size);
247 memory = nir_bcsel(b, oob, zero, memory);
248 }
249
250 unsigned dest_size = intr->def.bit_size;
251
252 /* Unpack but do not convert non-native non-array formats */
253 if (is_rgb10_a2(desc) && interchange_format == PIPE_FORMAT_R32_UINT) {
254 unsigned bits[] = {10, 10, 10, 2};
255
256 if (is_signed)
257 memory = nir_format_unpack_sint(b, memory, bits, 4);
258 else
259 memory = nir_format_unpack_uint(b, memory, bits, 4);
260 }
261
262 if (desc->channel[chan].normalized) {
263 /* 8/16-bit normalized formats are native, others converted here */
264 if (is_rgb10_a2(desc) && is_signed) {
265 unsigned bits[] = {10, 10, 10, 2};
266 memory = nir_format_snorm_to_float(b, memory, bits);
267 } else if (desc->channel[chan].size == 32) {
268 assert(desc->is_array && "no non-array 32-bit norm formats");
269 unsigned bits[] = {32, 32, 32, 32};
270
271 if (is_signed)
272 memory = nir_format_snorm_to_float(b, memory, bits);
273 else
274 memory = nir_format_unorm_to_float(b, memory, bits);
275 }
276 } else if (desc->channel[chan].pure_integer) {
277 /* Zero-extension is native, may need to sign extend */
278 if (is_signed)
279 memory = nir_i2iN(b, memory, dest_size);
280 } else {
281 if (is_unsigned)
282 memory = nir_u2fN(b, memory, dest_size);
283 else if (is_signed || is_fixed)
284 memory = nir_i2fN(b, memory, dest_size);
285 else
286 memory = nir_f2fN(b, memory, dest_size);
287
288 /* 16.16 fixed-point weirdo GL formats need to be scaled */
289 if (is_fixed) {
290 assert(desc->is_array && desc->channel[chan].size == 32);
291 assert(dest_size == 32 && "overflow if smaller");
292 memory = nir_fmul_imm(b, memory, 1.0 / 65536.0);
293 }
294 }
295
296 /* We now have a properly formatted vector of the components in memory. Apply
297 * the format swizzle forwards to trim/pad/reorder as needed.
298 */
299 nir_def *channels[4] = {NULL};
300
301 for (unsigned i = 0; i < intr->num_components; ++i) {
302 unsigned c = nir_intrinsic_component(intr) + i;
303 channels[i] = apply_swizzle_channel(b, memory, desc->swizzle[c], is_int);
304 }
305
306 nir_def *logical = nir_vec(b, channels, intr->num_components);
307 nir_def_rewrite_uses(&intr->def, logical);
308 return true;
309 }
310
311 bool
312 agx_nir_lower_vbo(nir_shader *shader, const struct agx_velem_key *attribs,
313 struct agx_robustness robustness, bool dynamic_strides)
314 {
315 assert(shader->info.stage == MESA_SHADER_VERTEX);
316
317 /* To implement null vertex buffer descriptors, we need either soft fault or
318 * GL robustness with a vertex buffer at 0x0.
319 */
320 if (!robustness.soft_fault) {
321 robustness.level = MAX2(robustness.level, AGX_ROBUSTNESS_GL);
322 }
323
324 struct ctx ctx = {
325 .attribs = attribs,
326 .rs = robustness,
327 .dynamic_strides = dynamic_strides,
328 };
329
330 return nir_shader_intrinsics_pass(shader, pass, nir_metadata_control_flow,
331 &ctx);
332 }