Debian Codegraph @

src/asahi/lib/agx_nir_lower_tilebuffer.c

raw
1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <stdint.h>
7 #include "compiler/glsl_types.h"
8 #include "util/format/u_format.h"
9 #include "util/macros.h"
10 #include "agx_nir_format_helpers.h"
11 #include "agx_tilebuffer.h"
12 #include "nir.h"
13 #include "nir_builder.h"
14 #include "nir_builder_opcodes.h"
15
16 #define AGX_NUM_TEXTURE_STATE_REGS 16
17 #define ALL_SAMPLES 0xFF
18
19 struct ctx {
20 struct agx_tilebuffer_layout *tib;
21 uint8_t *colormasks;
22 bool *translucent;
23 unsigned bindless_base;
24 bool any_memory_stores;
25 uint8_t outputs_written;
26 nir_def *write_samples;
27 };
28
29 static void
30 store_tilebuffer(nir_builder *b, struct agx_tilebuffer_layout *tib,
31 enum pipe_format format, enum pipe_format logical_format,
32 unsigned rt, nir_def *value, nir_def *samples,
33 unsigned write_mask)
34 {
35 /* The hardware cannot extend for a 32-bit format. Extend ourselves. */
36 if (format == PIPE_FORMAT_R32_UINT && value->bit_size == 16) {
37 if (util_format_is_pure_sint(logical_format))
38 value = nir_i2i32(b, value);
39 else if (util_format_is_pure_uint(logical_format))
40 value = nir_u2u32(b, value);
41 else
42 value = nir_f2f32(b, value);
43 }
44
45 /* Pure integer formatss need to be clamped in software, at least in some
46 * cases. We do so on store. Piglit gl-3.0-render-integer checks this, as
47 * does KHR-GL33.packed_pixels.*.
48 */
49 const struct util_format_description *desc =
50 util_format_description(logical_format);
51 unsigned c = util_format_get_first_non_void_channel(logical_format);
52
53 if (desc->channel[c].size <= 16 &&
54 util_format_is_pure_integer(logical_format)) {
55
56 unsigned bits[4] = {
57 desc->channel[0].size,
58 desc->channel[1].size,
59 desc->channel[2].size,
60 desc->channel[3].size,
61 };
62
63 if (util_format_is_pure_sint(logical_format))
64 value = nir_format_clamp_sint(b, value, bits);
65 else
66 value = nir_format_clamp_uint(b, value, bits);
67
68 value = nir_u2u16(b, value);
69 }
70
71 if (!samples)
72 samples = nir_imm_intN_t(b, ALL_SAMPLES, 16);
73
74 uint8_t offset_B = agx_tilebuffer_offset_B(tib, rt);
75 nir_store_local_pixel_agx(b, value, samples, nir_undef(b, 2, 16),
76 .base = offset_B, .write_mask = write_mask,
77 .format = format);
78 }
79
80 static nir_def *
81 nir_build_fsat_signed(nir_builder *b, nir_def *x)
82 {
83 return nir_fclamp(b, x, nir_imm_floatN_t(b, -1.0, x->bit_size),
84 nir_imm_floatN_t(b, +1.0, x->bit_size));
85 }
86
87 static nir_def *
88 nir_fsat_to_format(nir_builder *b, nir_def *x, enum pipe_format format)
89 {
90 if (util_format_is_unorm(format))
91 return nir_fsat(b, x);
92 else if (util_format_is_snorm(format))
93 return nir_build_fsat_signed(b, x);
94 else
95 return x;
96 }
97
98 static nir_def *
99 load_tilebuffer(nir_builder *b, struct agx_tilebuffer_layout *tib,
100 uint8_t load_comps, uint8_t bit_size, unsigned rt,
101 enum pipe_format format, enum pipe_format logical_format)
102 {
103 unsigned comps = util_format_get_nr_components(logical_format);
104 bool f16 = (format == PIPE_FORMAT_R16_FLOAT);
105
106 /* Don't load with F16 */
107 if (f16)
108 format = PIPE_FORMAT_R16_UINT;
109
110 uint8_t offset_B = agx_tilebuffer_offset_B(tib, rt);
111 nir_def *res = nir_load_local_pixel_agx(
112 b, MIN2(load_comps, comps), f16 ? 16 : bit_size,
113 nir_imm_intN_t(b, ALL_SAMPLES, 16), .base = offset_B, .format = format);
114
115 /* Extend floats */
116 if (f16 && bit_size != 16) {
117 assert(bit_size == 32);
118 res = nir_f2f32(b, res);
119 }
120
121 /* Some formats like RGB565 are float in the tilebuffer but logically
122 * normalized. We need to clamp on load to get proper blending semantics, as
123 * the APIs require clamping here and nir_lower_blend (correctly) assumes
124 * load_output is clamped. The spilled path is unaffected as the clamping
125 * implicitly happens when roundtripping to memory.
126 */
127 if (f16)
128 res = nir_fsat_to_format(b, res, logical_format);
129
130 res = nir_sign_extend_if_sint(b, res, logical_format);
131 return nir_pad_vector(b, res, load_comps);
132 }
133
134 /*
135 * As a simple implementation, we use image load/store instructions to access
136 * spilled render targets. The driver will supply corresponding texture and PBE
137 * descriptors for each render target, accessed bindlessly
138 *
139 * Note that this lower happens after driver bindings are lowered, so the
140 * bindless handle is in the AGX-specific format.
141 */
142 static nir_def *
143 handle_for_rt(nir_builder *b, unsigned base, unsigned rt, bool pbe)
144 {
145 unsigned index = base + (2 * rt) + (pbe ? 1 : 0);
146 return nir_load_texture_handle_agx(b, nir_imm_int(b, index));
147 }
148
149 static enum glsl_sampler_dim
150 dim_for_rt(nir_builder *b, unsigned nr_samples, nir_def **sample)
151 {
152 if (nr_samples == 1) {
153 *sample = nir_imm_intN_t(b, 0, 16);
154 return GLSL_SAMPLER_DIM_2D;
155 } else {
156 *sample = nir_u2u16(b, nir_load_sample_id(b));
157 b->shader->info.fs.uses_sample_shading = true;
158 return GLSL_SAMPLER_DIM_MS;
159 }
160 }
161
162 static nir_def *
163 image_coords(nir_builder *b)
164 {
165 nir_def *xy__ = nir_pad_vec4(b, nir_u2u32(b, nir_load_pixel_coord(b)));
166 return nir_vector_insert_imm(b, xy__, nir_load_layer_id(b), 2);
167 }
168
169 static void
170 store_memory(nir_builder *b, unsigned bindless_base, unsigned nr_samples,
171 enum pipe_format format, unsigned rt, nir_def *value,
172 nir_def *samples)
173 {
174 nir_def *image = handle_for_rt(b, bindless_base, rt, true);
175 nir_def *tex_image = handle_for_rt(b, bindless_base, rt, false);
176 nir_def *zero = nir_imm_intN_t(b, 0, 16);
177 nir_def *lod = zero;
178
179 nir_def *sample;
180 enum glsl_sampler_dim dim = dim_for_rt(b, nr_samples, &sample);
181 nir_def *coords = image_coords(b);
182
183 nir_def *size =
184 nir_bindless_image_size(b, 3, 32, tex_image, nir_imm_int(b, 0),
185 .image_array = true, .image_dim = dim);
186
187 nir_begin_invocation_interlock(b);
188
189 /* XXX: We should not get out-of-bounds image coords. Yet here we are :-/
190 *
191 * Fixes faults in:
192 *
193 * dEQP-VK.pipeline.monolithic.multisample.misc.dynamic_rendering.multi_renderpass.r8g8b8a8_unorm_r16g16b16a16_sfloat_r32g32b32a32_uint_d16_unorm.random_68
194 *
195 * which hits eMRT with multisampled image stores on an odd framebuffer size,
196 * and we get coordinates that go all the way up to align((width,height),
197 * (32,32)) despite setting scissor and such.
198 *
199 * XXX: needs more investigation, macOS seems to not choke on this so what
200 * are we doing wrong?
201 */
202 nir_def *cond = nir_ball(b, nir_ult(b, nir_trim_vector(b, coords, 2),
203 nir_trim_vector(b, size, 2)));
204
205 if (nr_samples > 1) {
206 nir_def *coverage = nir_load_sample_mask(b);
207
208 if (samples != NULL)
209 coverage = nir_iand(b, coverage, nir_u2u32(b, samples));
210
211 nir_def *covered = nir_ubitfield_extract(
212 b, coverage, nir_u2u32(b, sample), nir_imm_int(b, 1));
213
214 cond = nir_iand(b, cond, nir_ine_imm(b, covered, 0));
215 } else if (samples != NULL) {
216 cond = nir_iand(b, cond, nir_ine_imm(b, samples, 0));
217 }
218
219 nir_push_if(b, cond);
220 {
221 nir_bindless_image_store(b, image, coords, sample, value, lod,
222 .image_dim = dim, .image_array = true,
223 .format = format);
224 }
225 nir_pop_if(b, NULL);
226 }
227
228 static nir_def *
229 load_memory(nir_builder *b, unsigned bindless_base, unsigned nr_samples,
230 uint8_t comps, uint8_t bit_size, unsigned rt,
231 enum pipe_format format)
232 {
233 nir_def *image = handle_for_rt(b, bindless_base, rt, false);
234 nir_def *zero = nir_imm_intN_t(b, 0, 16);
235 nir_def *lod = zero;
236
237 nir_def *sample;
238 enum glsl_sampler_dim dim = dim_for_rt(b, nr_samples, &sample);
239 nir_def *coords = image_coords(b);
240
241 /* Ensure pixels below this one have written out their results */
242 nir_begin_invocation_interlock(b);
243
244 return nir_bindless_image_load(b, comps, bit_size, image, coords, sample,
245 lod, .image_dim = dim, .image_array = true,
246 .format = format, .access = ACCESS_IN_BOUNDS);
247 }
248
249 static bool
250 pass(nir_builder *b, nir_intrinsic_instr *intr, void *data)
251 {
252 if (intr->intrinsic != nir_intrinsic_store_output &&
253 intr->intrinsic != nir_intrinsic_load_output)
254 return false;
255
256 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
257 assert(sem.dual_source_blend_index == 0 && "dual source blending lowered");
258 if (sem.location < FRAG_RESULT_DATA0)
259 return false;
260
261 b->cursor = nir_instr_remove(&intr->instr);
262
263 struct ctx *ctx = data;
264 struct agx_tilebuffer_layout *tib = ctx->tib;
265 unsigned rt = sem.location - FRAG_RESULT_DATA0;
266 assert(rt < ARRAY_SIZE(tib->logical_format));
267
268 enum pipe_format logical_format = tib->logical_format[rt];
269 enum pipe_format format = agx_tilebuffer_physical_format(tib, rt);
270 unsigned comps = util_format_get_nr_components(logical_format);
271
272 if (intr->intrinsic == nir_intrinsic_store_output) {
273 ctx->outputs_written |= BITFIELD_BIT(rt);
274
275 /* Only write components that actually exist */
276 uint16_t write_mask = (uint16_t)BITFIELD_MASK(comps);
277
278 /* Delete stores to nonexistent render targets */
279 if (logical_format == PIPE_FORMAT_NONE)
280 return NIR_LOWER_INSTR_PROGRESS_REPLACE;
281
282 /* Only write colours masked by the blend state */
283 if (ctx->colormasks)
284 write_mask &= ctx->colormasks[rt];
285
286 /* Masked stores require a translucent pass type */
287 if (write_mask != BITFIELD_MASK(comps)) {
288 assert(ctx->translucent != NULL &&
289 "colour masking requires translucency");
290
291 assert(agx_tilebuffer_supports_mask(tib, rt));
292 *(ctx->translucent) = true;
293 }
294
295 if (ctx->write_samples) {
296 assert(ctx->translucent != NULL &&
297 "sample masking requires translucency");
298
299 *(ctx->translucent) = true;
300 }
301
302 /* But we ignore the NIR write mask for that, since it's basically an
303 * optimization hint.
304 */
305 if (agx_tilebuffer_supports_mask(tib, rt))
306 write_mask &= nir_intrinsic_write_mask(intr);
307
308 /* Delete stores that are entirely masked out */
309 if (!write_mask)
310 return true;
311
312 nir_def *value = intr->src[0].ssa;
313
314 /* Trim to format as required by hardware */
315 value = nir_trim_vector(b, intr->src[0].ssa, comps);
316
317 if (tib->spilled[rt]) {
318 store_memory(b, ctx->bindless_base, tib->nr_samples, logical_format,
319 rt, value, ctx->write_samples);
320 ctx->any_memory_stores = true;
321 } else {
322 store_tilebuffer(b, tib, format, logical_format, rt, value,
323 ctx->write_samples, write_mask);
324 }
325 } else {
326 uint8_t bit_size = intr->def.bit_size;
327 nir_def *def;
328
329 /* Loads from non-existent render targets are undefined in NIR but not
330 * possible to encode in the hardware, delete them.
331 */
332 if (logical_format == PIPE_FORMAT_NONE) {
333 def = nir_undef(b, intr->num_components, bit_size);
334 } else if (tib->spilled[rt]) {
335 *(ctx->translucent) = true;
336
337 def = load_memory(b, ctx->bindless_base, tib->nr_samples,
338 intr->num_components, bit_size, rt, logical_format);
339 } else {
340 def = load_tilebuffer(b, tib, intr->num_components, bit_size, rt,
341 format, logical_format);
342 }
343
344 nir_def_rewrite_uses(&intr->def, def);
345 }
346
347 return true;
348 }
349
350 bool
351 agx_nir_lower_tilebuffer(nir_shader *shader, struct agx_tilebuffer_layout *tib,
352 uint8_t *colormasks, unsigned *bindless_base,
353 nir_def *write_samples, bool *translucent)
354 {
355 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
356
357 struct ctx ctx = {
358 .tib = tib,
359 .colormasks = colormasks,
360 .translucent = translucent,
361 .write_samples = write_samples,
362 };
363
364 /* Allocate 1 texture + 1 PBE descriptor for each spilled descriptor */
365 if (agx_tilebuffer_spills(tib)) {
366 assert(bindless_base != NULL && "must be specified if spilling");
367 ctx.bindless_base = *bindless_base;
368 *bindless_base += (AGX_MAX_RENDER_TARGETS * 2);
369 }
370
371 bool progress =
372 nir_shader_intrinsics_pass(shader, pass, nir_metadata_none, &ctx);
373
374 /* Flush at end */
375 if (ctx.any_memory_stores) {
376 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
377 nir_builder b = nir_builder_at(nir_after_impl(impl));
378 nir_fence_pbe_to_tex_pixel_agx(&b);
379 }
380
381 /* If there are any render targets bound to the framebuffer that aren't
382 * statically written by the fragment shader, that acts as an implicit mask
383 * and requires translucency.
384 *
385 * XXX: Could be optimized.
386 */
387 for (unsigned i = 0; i < ARRAY_SIZE(tib->logical_format); ++i) {
388 bool exists = tib->logical_format[i] != PIPE_FORMAT_NONE;
389 bool written = ctx.outputs_written & BITFIELD_BIT(i);
390
391 if (translucent)
392 *translucent |= (exists && !written);
393 }
394
395 return progress;
396 }