From 3712609ee364c50962998acb6553e0dc2e9f052e Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Sun, 7 Aug 2022 16:25:13 -0400 Subject: [PATCH] agx: Only emit the used components of gl_FragCoord In case a shader only use gl_FragCoord.xy, this avoids wasting coefficient registers for gl_FragCoord.zw which should be a small optimization. It's also less work for DCE but I'm less worried about that. Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/compiler/agx_compile.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 0a732788c46..2b0c29b2528 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -522,21 +522,25 @@ agx_emit_load_ubo(agx_builder *b, agx_index dst, nir_intrinsic_instr *instr) return NULL; } +/* + * Emit code to generate gl_FragCoord. The xy components are calculated from + * special registers, whereas the zw components are interpolated varyings. + * Because interpolating varyings requires allocating coefficient registers that + * might not be used, we only emit code for components that are actually used. + */ static void agx_emit_load_frag_coord(agx_builder *b, agx_index *dests, nir_intrinsic_instr *instr) { - /* xy */ - for (unsigned i = 0; i < 2; ++i) { - dests[i] = agx_fadd(b, agx_convert(b, agx_immediate(AGX_CONVERT_U32_TO_F), - agx_get_sr(b, 32, AGX_SR_THREAD_POSITION_IN_GRID_X + i), - AGX_ROUND_RTE), agx_immediate_f(0.5f)); + u_foreach_bit(i, nir_ssa_def_components_read(&instr->dest.ssa)) { + if (i < 2) { + dests[i] = agx_fadd(b, agx_convert(b, agx_immediate(AGX_CONVERT_U32_TO_F), + agx_get_sr(b, 32, AGX_SR_THREAD_POSITION_IN_GRID_X + i), + AGX_ROUND_RTE), agx_immediate_f(0.5f)); + } else { + agx_index cf = agx_get_cf(b->shader, true, false, VARYING_SLOT_POS, i, 1); + dests[i] = agx_iter(b, cf, agx_null(), 1, false); + } } - - agx_index w = agx_get_cf(b->shader, true, false, VARYING_SLOT_POS, 3, 1); - agx_index z = agx_get_cf(b->shader, true, false, VARYING_SLOT_POS, 2, 1); - - dests[2] = agx_iter(b, z, agx_null(), 1, false); - dests[3] = agx_iter(b, w, agx_null(), 1, false); } static agx_instr *