From 7144b33efbc63b6abe25854aa5544cea206860e6 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Sun, 22 Nov 2026 17:09:26 -0500 Subject: [PATCH 1/2] asahi: use disjoint staging banks for 2D-array samples and texel fetches Allow consecutive 2D-array texture samples (including shadow PCF, gathers, immediate offsets, immediate bias/LOD, and compute/vertex forms) and immediate-LOD 2D-array texel fetches to allocate disjoint 8-half staging banks on both AGX3B and AGX3C instead of serializing through fixed bank 0. In agx3_allocate_staging(), every span == 8 allocation now aligns to 8 halves rather than 4 on AGX3B as well as AGX3C so consecutive 8-half banks stay disjoint and satisfy PACK_SMPIR's bank alignment. This also 8-aligns the existing AGX3B vertex 3D and 2D-array relocations, which previously used 4-half alignment. In agx3_emit_texture_fetch(), immediate-LOD 2D-array fetches allocate one 8-half bank per scoreboard slot (6 on AGX3B, 8 on AGX3C) up to the f19 <= 60 bound and stage their layer into staging_coord + 6. Bank retirement on AGX3B now uses agx3_retire_wait_on(I->src[0], bank_retire | wait_mask) rather than the r0 identity carrier so bank wrap cannot race an unrelated in-flight write to r0, and clears wait_mask after that carrier so staging_wait does not emit a redundant second wait through r0. This bank_retire carrier applies to every AGX3B fetch taking the bank_retire path, including consecutive multisample fetches and register-LOD fetches (max_banks == 1), plain 2D fetches past the ring, and 16-bit coordinate fetches (whose half-word carrier form waits slot 6 as well). Plain 2D AGX3B fetch bank count is left unchanged here and widened in a separate follow-up commit. Signed-off-by: Diljit Singh --- src/asahi/compiler/agx3_pack_binary.c | 98 ++- .../compiler/test/test-agx3-pack-binary.cpp | 589 ++++++++++++++++++ 2 files changed, 653 insertions(+), 34 deletions(-) diff --git a/src/asahi/compiler/agx3_pack_binary.c b/src/asahi/compiler/agx3_pack_binary.c index b4f21ac447de..e496e0bf12ed 100644 --- a/src/asahi/compiler/agx3_pack_binary.c +++ b/src/asahi/compiler/agx3_pack_binary.c @@ -2352,23 +2352,31 @@ agx3_emit_texture_fetch(struct util_dynarray *emission, /* A staged read consumes, and kills, its coordinate bank asynchronously, * so consecutive reads stage in disjoint banks, as Apple's do (smp f19=0 * then f19=4). Callers that pass no `banks` keep one bank. Halves 4..7 - * carry PACK_SMPIR operands, so the second plain bank starts at half 8; a - * read with PACK_SMPIR operands keeps the base bank and occupies halves - * 0..7. An outstanding read sharing the halves is retired first. */ + * carry PACK_SMPIR operands, so banks step by eight halves; plain 2D + * reads, immediate-LOD 2D-array reads, and M5 multisample reads allocate + * disjoint banks from `staging_base`, while other reads with PACK_SMPIR + * operands keep the base bank and occupy halves 0..7. An outstanding read + * sharing the halves is retired first. */ const bool plain_bank = !ms_2d && !ms_array && !array && !register_lod; + const bool array_bank = array && !register_lod; const bool m5_ms_banks = m5 && (ms_2d || ms_array); - const bool banked_read = plain_bank || m5_ms_banks; + const bool banked_read = plain_bank || array_bank || m5_ms_banks; /* Plain M5 fetch coordinates can name any aligned bank through half 60. * Use independent banks for all eight scoreboard slots before recycling * one. */ const unsigned plain_banks = m5 ? MIN2(AGX3_M5_FETCH_BANKS, 1 + (60 - staging_base) / 8) : 2; + /* 2D-array fetch coordinates can name any aligned bank through half 60 + * (f19); allocate one eight-half bank per scoreboard slot (6 on AGX3B, + * 8 on AGX3C) before recycling one. */ + const unsigned array_banks = + MIN2(m5 ? AGX3_M5_FETCH_BANKS : 6u, 1 + (60 - staging_base) / 8); /* Keep independent non-array MS publications in separate operand banks. */ const unsigned ms_banks = m5_ms_banks && !ms_array ? AGX3_M5_MS_FETCH_BANKS : 4; const uint16_t bank_span = - m5_ms_banks ? 8 : plain_bank ? 2 * coord_count - : staging_base + 8; + (m5_ms_banks || array_bank) ? 8 : plain_bank ? 2 * coord_count + : staging_base + 8; uint16_t staging_coord = staging_base; uint16_t bank_retire = 0; const bool reuse = group && group->reuse; @@ -2387,11 +2395,14 @@ agx3_emit_texture_fetch(struct util_dynarray *emission, } else if (stage_coordinates && banks) { agx3_staging_release(banks, outstanding); - /* A read with PACK_SMPIR operands occupies halves 0..bank_span. */ + /* Non-banked reads with PACK_SMPIR operands occupy halves 0..bank_span. */ const uint16_t first = banked_read ? staging_base : 0; + const unsigned max_banks = + m5_ms_banks ? ms_banks : + array_bank ? array_banks : + plain_bank ? plain_banks : 1u; bool placed = false; - for (unsigned c = 0; c < (m5_ms_banks ? ms_banks : plain_bank ? plain_banks : 1u) && - !placed; ++c) { + for (unsigned c = 0; c < max_banks && !placed; ++c) { const uint16_t start = first + 8 * c; const uint16_t conflict = agx3_staging_conflicts( banks, banked_read ? start : 0, bank_span); @@ -2408,20 +2419,21 @@ agx3_emit_texture_fetch(struct util_dynarray *emission, agx3_staging_forget(banks, bank_retire); } /* The r0 carrier reads and rewrites r0, so it is sound only while r0 is - * ready or its producer is in the mask. When M5 multisample reads fill - * all four operand banks, the next one retires the first bank's read, - * and r0 may be the destination of a later read still in flight: in the - * stencil resolve of dEQP-VK.renderpasses.renderpass2. - * depth_stencil_resolve.*.stencil_min, five texel fetches were in - * flight, the second writing r0..r3, and the carrier waited for the - * first alone. Carry the retirement on this fetch's coordinates with - * the fetch's own wait, which covers every pending write of them; the - * staging COPYs right after it wait for the same producers. */ - if (bank_retire && - !(m5 ? agx3_retire_wait_on(emission, enc, I->src[0], - bank_retire | wait_mask) - : agx3_retire_slots_noop(emission, enc, bank_retire))) - return false; + * ready or its producer is in the mask. When banked reads fill all banks, + * the next one retires the first bank's read, and r0 may be the + * destination of a later read still in flight: in the stencil resolve of + * dEQP-VK.renderpasses.renderpass2.depth_stencil_resolve.*.stencil_min, + * five texel fetches were in flight, the second writing r0..r3, and the + * carrier waited for the first alone. Carry the retirement on this + * fetch's coordinates with the fetch's own wait, which covers every + * pending write of them; the staging COPYs right after it wait for the + * same producers. */ + if (bank_retire) { + if (!agx3_retire_wait_on(emission, enc, I->src[0], + bank_retire | wait_mask)) + return false; + wait_mask = 0; + } if (staged_end) *staged_end = banked_read ? staging_coord + bank_span : bank_span; @@ -2545,7 +2557,7 @@ agx3_emit_texture_fetch(struct util_dynarray *emission, uint8_t packed[AGX3_TEX_MAX_LENGTH], packed_len = 0; const agx_index layer = agx3_tex_coord(I, 2); if (!agx3_tex_stage_array_layer_at( - enc, staging_base, layer.value, staging_slot, 0, + enc, staging_coord, layer.value, staging_slot, 0, layer.cache, layer.discard, packed, &packed_len) || !agx3_append(emission, packed, packed_len)) @@ -4613,11 +4625,18 @@ agx3_emit_texture_sample(struct util_dynarray *emission, !shadow && !offset && !query_lod && !min_lod && (((vertex_stage || compute_stage) && (immediate_lod || gradient_lod)) || m5_fragment_3d); + const bool combined_array_lod = array && !cube && !staged_offset && + ((explicit_lod && !immediate_lod) || register_bias || min_lod); + const bool relocatable_array = array && !cube && !register_operands && + !query_lod && !staged_offset && !register_offset && + !combined_array_lod && !gradient_lod && !bias_min && !gradient_min && + !(bank_base & 7); if (bank_base != 0 && ((enc != AGX_ISA_ENCODING_AGX3B && enc != AGX_ISA_ENCODING_AGX3C) || native_staged_coords || - (array && !(enc == AGX_ISA_ENCODING_AGX3B && vertex_stage && - explicit_lod && immediate_lod && !shadow && !offset)) || + (array && !relocatable_array && + !(enc == AGX_ISA_ENCODING_AGX3B && vertex_stage && + explicit_lod && immediate_lod && !shadow && !offset)) || (three_d && !m5_banked_3d && !(enc == AGX_ISA_ENCODING_AGX3B && vertex_stage)) || cube || (gradient_lod && !m5_banked_3d) || bias_min || gradient_min || query_lod || @@ -4641,8 +4660,6 @@ agx3_emit_texture_sample(struct util_dynarray *emission, uint8_t bytes[AGX3_TEX_MAX_LENGTH]; uint8_t len = 0; - const bool combined_array_lod = array && !cube && !staged_offset && - ((explicit_lod && !immediate_lod) || register_bias || min_lod); const uint16_t offset_base = I->src[5].value + (shadow ? 2 : 0); /* The comparison 2D-array bias and the staged 3D bias read their minimum * from a packed register. */ @@ -8312,10 +8329,11 @@ agx3_allocate_staging(struct util_dynarray *emission, unsigned span, unsigned avoid_end, uint16_t *base) { uint16_t waits; - /* C's packed per-lane LOD occupies bank+6 in an eight-half group. - * Ordinary coordinates keep their native four-half alignment. */ - const unsigned alignment = - enc == AGX_ISA_ENCODING_AGX3C && span == 8 ? 8 : 4; + /* Eight-half sample banks (2D-array layer + comparison, packed per-lane + * LOD, 3D) align to eight halves so consecutive banks stay disjoint and + * satisfy PACK_SMPIR's bank alignment on both AGX3B and AGX3C. Ordinary + * four-half coordinates keep their native four-half alignment. */ + const unsigned alignment = span == 8 ? 8 : 4; if (!agx3_staging_plan_above(banks, span, alignment, AGX3_STAGING_HALFS, avoid_end, base, &waits)) return false; @@ -15099,11 +15117,22 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) I->src[1].type == AGX_INDEX_IMMEDIATE) || I->lod_mode == AGX_LOD_MODE_LOD_GRAD)) || m5_fragment_3d); + const bool relocatable_2d_array = + (enc == AGX_ISA_ENCODING_AGX3B || + (enc == AGX_ISA_ENCODING_AGX3C && !ctx->key->dev.emit_agx3d)) && + I->dim == AGX_DIM_2D_ARRAY && !I->query_lod && + !(I->offset && !I->offset_xyz) && + I->src[3].type != AGX_INDEX_REGISTER && + I->src[4].type != AGX_INDEX_REGISTER && + (I->lod_mode == AGX_LOD_MODE_AUTO_LOD || + ((I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS || + I->lod_mode == AGX_LOD_MODE_LOD_MIN) && + I->src[1].type == AGX_INDEX_IMMEDIATE)); const bool relocatable_coordinates = (enc == AGX_ISA_ENCODING_AGX3B || enc == AGX_ISA_ENCODING_AGX3C) && slot != 0 && !native_staged_coords && - (m5_banked_3d || I->dim == AGX_DIM_2D || + (m5_banked_3d || relocatable_2d_array || I->dim == AGX_DIM_2D || (enc == AGX_ISA_ENCODING_AGX3B && (I->dim == AGX_DIM_3D || (I->dim == AGX_DIM_2D_ARRAY && !I->shadow && !I->offset)) && @@ -15112,7 +15141,8 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) I->lod_mode == AGX_LOD_MODE_LOD_MIN && I->src[1].type == AGX_INDEX_IMMEDIATE)) && !I->query_lod && !(I->offset && !I->offset_xyz) && - (m5_banked_3d || I->lod_mode == AGX_LOD_MODE_AUTO_LOD || + (m5_banked_3d || relocatable_2d_array || + I->lod_mode == AGX_LOD_MODE_AUTO_LOD || ((I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS || I->lod_mode == AGX_LOD_MODE_LOD_MIN) && (I->src[1].type == AGX_INDEX_IMMEDIATE || diff --git a/src/asahi/compiler/test/test-agx3-pack-binary.cpp b/src/asahi/compiler/test/test-agx3-pack-binary.cpp index bfb64f0f43d6..2bd7cb878cdb 100644 --- a/src/asahi/compiler/test/test-agx3-pack-binary.cpp +++ b/src/asahi/compiler/test/test-agx3-pack-binary.cpp @@ -17124,3 +17124,592 @@ TEST_F(AGX3PackBinary, TwoImmediateFaddPacksAfterLoweringOnEveryAgx3) } } } + +/* Consecutive 2D-array shadow samples (for example a PCF kernel) have no + * fixed-bank operands: X/Y land at bank+0/+2, the shadow reference at bank+4, + * and PACK_SMPIR publishes the array layer at bank+6. Each sample claims its + * own 8-half bank on both AGX3B and AGX3C without retiring earlier slots. */ +TEST_F(AGX3PackBinary, TextureSample2DArrayShadowUsesDisjointBanks) +{ + for (enum agx_isa_encoding enc : + {AGX_ISA_ENCODING_AGX3B, AGX_ISA_ENCODING_AGX3C}) { + SCOPED_TRACE(enc); + agx_builder *b = builder(enc); + key.dev.chip = enc == AGX_ISA_ENCODING_AGX3B ? AGX_CHIP_G17P + : AGX_CHIP_G17S; + key.dev.emit_agx3d = false; + b->shader->stage = MESA_SHADER_FRAGMENT; + b->shader->max_reg = 63; + + for (unsigned i = 0; i < 4; ++i) { + agx_index dest = agx_register(40 + 8 * i, AGX_SIZE_32); + dest.channels_m1 = 3; + agx_index coord = agx_register(4 + 8 * i, AGX_SIZE_32); + coord.channels_m1 = 2; + agx_index ref = agx_register(36, AGX_SIZE_32); + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), ref, AGX_DIM_2D_ARRAY, + AGX_LOD_MODE_AUTO_LOD, 0, false, true, false, AGX_GATHER_NONE, + false); + I->mask = 1; + } + agx_stop(b); + + const auto code = pack(b); + ASSERT_FALSE(code.empty()); + EXPECT_TRUE(b->shader->agx3_staging_known); + EXPECT_EQ(b->shader->agx3_staging_halfs, 32u); + + for (unsigned i = 0; i < 4; ++i) { + const uint16_t bank = 8 * i; + const uint8_t slot = 1 + i; + uint8_t bytes[AGX3_TEX_MAX_LENGTH], len = 0; + ASSERT_TRUE(agx3_tex_stage_array_layer_at( + enc, bank, 4 + 8 * i + 4, slot, 0, + enc == AGX_ISA_ENCODING_AGX3C, false, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing array layer staging at bank " << bank; + + ASSERT_TRUE(agx3_tex_stage_copy_hints( + enc, bank + 4, 36, slot, 0, + enc == AGX_ISA_ENCODING_AGX3C, false, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing shadow ref staging at bank+4 " << (bank + 4); + + const struct agx3_tex_sample_desc want = { + .dest = (uint16_t)(40 + 8 * i), + .coord = bank, + .width = AGX3_TEX_W32, + .mask = 1, + .lod = AGX3_TEX_LOD_AUTO, + .slot = slot, + .wait_mask = (uint16_t)(1u << (slot - 1)), + .coord_kill = enc == AGX_ISA_ENCODING_AGX3C, + .final = i == 3, + .sample_group_nonterminal = i < 3, + .sample_group_end = i == 3, + .array = true, + .array_layer = (uint16_t)(4 + 8 * i + 4), + .shadow = true, + .sample_abi = AGX3_TEX_SAMPLE_ABI_DRIVER, + }; + ASSERT_TRUE(agx3_tex_encode_sample(enc, &want, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing SMP naming coord=" << bank << " for shadow sample " + << i; + } + + /* No intervening slot-1 retirement between the four samples. */ + uint8_t retire[AGX3_ALU_MAX_LENGTH], rlen = 0; + ASSERT_TRUE(agx3_alu_band_identity_wait(enc, 0, 1, retire, &rlen)); + EXPECT_EQ(std::search(code.begin(), code.end(), retire, retire + rlen), + code.end()); + } +} + +/* A 2D-array sample with a register texture descriptor stages through fixed + * slot 6; keep it on bank 0 rather than relocating it. */ +TEST_F(AGX3PackBinary, TextureSample2DArrayRegisterOperandStaysOnBankZero) +{ + const auto enc = AGX_ISA_ENCODING_AGX3B; + agx_builder *b = builder(enc); + key.dev.chip = AGX_CHIP_G17P; + b->shader->stage = MESA_SHADER_FRAGMENT; + b->shader->max_reg = 63; + + for (unsigned i = 0; i < 2; ++i) { + agx_index dest = agx_register(40 + 8 * i, AGX_SIZE_32); + dest.channels_m1 = 3; + agx_index coord = agx_register(4 + 8 * i, AGX_SIZE_32); + coord.channels_m1 = 2; + agx_index tex = agx_register(28, AGX_SIZE_64); + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), tex, + agx_immediate(0), agx_null(), AGX_DIM_2D_ARRAY, AGX_LOD_MODE_AUTO_LOD, + 0, false, false, false, AGX_GATHER_NONE, false); + I->mask = 15; + } + agx_stop(b); + + const auto code = pack(b); + ASSERT_FALSE(code.empty()); + EXPECT_TRUE(b->shader->agx3_staging_known); + EXPECT_EQ(b->shader->agx3_staging_halfs, 8u); +} + +/* Newly relocated 2D-array sample shapes (gather, immediate offset with and + * without shadow comparison, immediate bias, immediate explicit LOD, and + * compute/vertex stages without g17p_uniform_dma) all allocate disjoint + * 8-half banks on both AGX3B and AGX3C. */ +TEST_F(AGX3PackBinary, TextureSample2DArrayNewShapesRelocate) +{ + struct shape { + const char *name; + mesa_shader_stage stage; + enum agx_lod_mode lod_mode; + uint16_t lod_imm; + bool shadow; + bool offset; + enum agx_gather gather; + }; + static const shape shapes[] = { + {"frag_gather", MESA_SHADER_FRAGMENT, AGX_LOD_MODE_AUTO_LOD, 0, false, + false, AGX_GATHER_R}, + {"frag_imm_offset", MESA_SHADER_FRAGMENT, AGX_LOD_MODE_AUTO_LOD, 0, false, + true, AGX_GATHER_NONE}, + {"frag_shadow_imm_offset", MESA_SHADER_FRAGMENT, AGX_LOD_MODE_AUTO_LOD, 0, + true, true, AGX_GATHER_NONE}, + {"frag_imm_bias", MESA_SHADER_FRAGMENT, AGX_LOD_MODE_AUTO_LOD_BIAS, 0x38, + false, false, AGX_GATHER_NONE}, + {"frag_imm_lod", MESA_SHADER_FRAGMENT, AGX_LOD_MODE_LOD_MIN, 0x38, false, + false, AGX_GATHER_NONE}, + {"compute_imm_lod", MESA_SHADER_COMPUTE, AGX_LOD_MODE_LOD_MIN, 0, false, + false, AGX_GATHER_NONE}, + {"vertex_imm_lod_no_dma", MESA_SHADER_VERTEX, AGX_LOD_MODE_LOD_MIN, 0, + false, false, AGX_GATHER_NONE}, + }; + + for (enum agx_isa_encoding enc : + {AGX_ISA_ENCODING_AGX3B, AGX_ISA_ENCODING_AGX3C}) { + for (const auto &s : shapes) { + SCOPED_TRACE(testing::Message() << s.name << " enc=" << enc); + agx_builder *b = builder(enc); + key.dev.chip = enc == AGX_ISA_ENCODING_AGX3B ? AGX_CHIP_G17P + : AGX_CHIP_G17S; + key.dev.emit_agx3d = false; + key.g17p_uniform_dma = false; + b->shader->stage = s.stage; + b->shader->max_reg = 63; + + for (unsigned i = 0; i < 2; ++i) { + agx_index dest = agx_register(40 + 8 * i, AGX_SIZE_32); + dest.channels_m1 = 3; + agx_index coord = agx_register(4 + 8 * i, AGX_SIZE_32); + coord.channels_m1 = 2; + agx_index compare_or_offset = + s.shadow ? agx_register(36, AGX_SIZE_32) + : s.offset ? agx_immediate(0x11) + : agx_null(); + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(s.lod_imm), agx_immediate(0), + agx_immediate(0), agx_immediate(0), compare_or_offset, + AGX_DIM_2D_ARRAY, s.lod_mode, 0, s.offset, s.shadow, false, + s.gather, false); + if (s.offset) + I->offset_xyz = 0x11; + I->mask = s.shadow && s.gather == AGX_GATHER_NONE ? 1 : 15; + } + agx_stop(b); + + const auto code = pack(b); + ASSERT_FALSE(code.empty()) << agx3_last_pack_refusal(); + EXPECT_TRUE(b->shader->agx3_staging_known); + EXPECT_EQ(b->shader->agx3_staging_halfs, 16u); + + for (unsigned i = 0; i < 2; ++i) { + const uint16_t bank = 8 * i; + const uint8_t slot = 1 + i; + uint8_t bytes[AGX3_TEX_MAX_LENGTH], len = 0; + ASSERT_TRUE(agx3_tex_stage_array_layer_at( + enc, bank, 4 + 8 * i + 4, slot, 0, + enc == AGX_ISA_ENCODING_AGX3C, false, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing array layer staging at bank " << bank; + + const bool is_frag = s.stage == MESA_SHADER_FRAGMENT; + const bool is_imm_lod = s.lod_mode == AGX_LOD_MODE_LOD_MIN; + const bool is_bias = s.lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS; + const uint16_t lod_x64 = + (is_imm_lod || is_bias) + ? (uint16_t)(int16_t)(agx_minifloat_decode(s.lod_imm) * 64.0f) + : 0; + const struct agx3_tex_sample_desc want = { + .dest = (uint16_t)(40 + 8 * i), + .coord = bank, + .width = AGX3_TEX_W32, + .mask = (uint8_t)(s.shadow && s.gather == AGX_GATHER_NONE ? 1 : 15), + .lod = is_bias ? AGX3_TEX_LOD_BIAS + : is_imm_lod ? AGX3_TEX_LOD_LEVEL + : AGX3_TEX_LOD_AUTO, + .lod_x64 = lod_x64, + .slot = slot, + .wait_mask = (uint16_t)(1u << (slot - 1)), + .coord_kill = enc == AGX_ISA_ENCODING_AGX3C, + .final = i == 1, + .sample_group_nonterminal = is_frag && i == 0, + .sample_group_end = is_frag && i == 1, + .vertex_stage = s.stage == MESA_SHADER_VERTEX, + .compute_stage = s.stage == MESA_SHADER_COMPUTE, + .array = true, + .array_layer = (uint16_t)(4 + 8 * i + 4), + .gather = s.gather == AGX_GATHER_R ? AGX3_TEX_GATHER_R + : AGX3_TEX_GATHER_NONE, + .shadow = s.shadow, + .offset = s.offset, + .offset_xyz = (uint16_t)(s.offset ? 0x11 : 0), + .sample_abi = AGX3_TEX_SAMPLE_ABI_DRIVER, + }; + ASSERT_TRUE(agx3_tex_encode_sample(enc, &want, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing SMP naming coord=" << bank << " for " << s.name + << " sample " << i; + } + } + } +} + +/* When a 2D gradient sample first holds staging halves 0..15 on slot 1, + * subsequent 2D-array samples on slots 2 and 3 allocate banks 16 and 24 + * (out of step with 8 * (slot - 1)) and encode SMP with coord = 16 and 24 + * on both AGX3B and AGX3C. */ +TEST_F(AGX3PackBinary, TextureSample2DArrayBankOutOfStepWithSlot) +{ + for (enum agx_isa_encoding enc : + {AGX_ISA_ENCODING_AGX3B, AGX_ISA_ENCODING_AGX3C}) { + SCOPED_TRACE(enc); + agx_builder *b = builder(enc); + key.dev.chip = enc == AGX_ISA_ENCODING_AGX3B ? AGX_CHIP_G17P + : AGX_CHIP_G17S; + key.dev.emit_agx3d = false; + b->shader->stage = MESA_SHADER_FRAGMENT; + b->shader->max_reg = 63; + + /* Slot 1: 2D gradient sample occupying staging halves 0..15. */ + { + agx_index dest = agx_register(40, AGX_SIZE_32); + dest.channels_m1 = 3; + agx_index coord = agx_register(4, AGX_SIZE_32); + coord.channels_m1 = 1; + agx_index grad = agx_register(16, AGX_SIZE_32); + grad.channels_m1 = 3; + auto *G = agx_texture_sample_to( + b, dest, coord, grad, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_null(), AGX_DIM_2D, AGX_LOD_MODE_LOD_GRAD, 0, + false, false, false, AGX_GATHER_NONE, false); + G->mask = 15; + } + + /* Slots 2 and 3: 2D-array samples must land at banks 16 and 24. */ + for (unsigned i = 0; i < 2; ++i) { + agx_index dest = agx_register(48 + 8 * i, AGX_SIZE_32); + dest.channels_m1 = 3; + agx_index coord = agx_register(24 + 8 * i, AGX_SIZE_32); + coord.channels_m1 = 2; + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), AGX_DIM_2D_ARRAY, + AGX_LOD_MODE_AUTO_LOD, 0, false, false, false, AGX_GATHER_NONE, + false); + I->mask = 15; + } + agx_stop(b); + + const auto code = pack(b); + ASSERT_FALSE(code.empty()) << agx3_last_pack_refusal(); + EXPECT_TRUE(b->shader->agx3_staging_known); + EXPECT_EQ(b->shader->agx3_staging_halfs, 32u); + + for (unsigned i = 0; i < 2; ++i) { + const uint16_t bank = 16 + 8 * i; + const uint8_t slot = 2 + i; + uint8_t bytes[AGX3_TEX_MAX_LENGTH], len = 0; + ASSERT_TRUE(agx3_tex_stage_array_layer_at( + enc, bank, 24 + 8 * i + 4, slot, 0, + enc == AGX_ISA_ENCODING_AGX3C, false, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing array layer staging at bank " << bank << " slot " + << unsigned(slot); + + const struct agx3_tex_sample_desc want = { + .dest = (uint16_t)(48 + 8 * i), + .coord = bank, + .width = AGX3_TEX_W32, + .mask = 15, + .lod = AGX3_TEX_LOD_AUTO, + .slot = slot, + .wait_mask = (uint16_t)(1u << (slot - 1)), + .coord_kill = enc == AGX_ISA_ENCODING_AGX3C, + .final = i == 1, + .sample_group_nonterminal = i == 0, + .sample_group_end = i == 1, + .array = true, + .array_layer = (uint16_t)(24 + 8 * i + 4), + .sample_abi = AGX3_TEX_SAMPLE_ABI_DRIVER, + }; + ASSERT_TRUE(agx3_tex_encode_sample(enc, &want, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing SMP naming coord=" << bank << " on slot " + << unsigned(slot); + } + } +} + +/* Consecutive 2D-array texel fetches allocate disjoint 8-half staging banks + * across every scoreboard slot (6 on AGX3B, 8 on AGX3C) and wrap cleanly via + * bank_retire on the fetch's coordinate register without clobbering an + * in-flight r0 destination. */ +TEST_F(AGX3PackBinary, TextureFetch2DArrayUsesDisjointBanksAndWraps) +{ + for (enum agx_isa_encoding enc : + {AGX_ISA_ENCODING_AGX3B, AGX_ISA_ENCODING_AGX3C}) { + const unsigned banks = enc == AGX_ISA_ENCODING_AGX3B ? 6u : 8u; + const unsigned ops = banks + 2; + SCOPED_TRACE(enc); + agx_builder *b = builder(enc); + key.dev.chip = enc == AGX_ISA_ENCODING_AGX3B ? AGX_CHIP_G17P + : AGX_CHIP_G17S; + key.dev.emit_agx3d = false; + b->shader->stage = MESA_SHADER_FRAGMENT; + b->shader->max_reg = 127; + + for (unsigned i = 0; i < ops; ++i) { + /* Put fetch 1's destination in r0..r1 so wrapping onto bank 0 cannot + * safely retire through an r0 identity carrier. Use 16-bit results + * in 88..127 so all coordinates (8..85) and destinations stay + * disjoint within AGX3B's 128-half destination bound. */ + const uint16_t dst_reg = i == 1 ? 0 : (88 + 4 * i); + agx_index dest = agx_register(dst_reg, AGX_SIZE_16); + dest.channels_m1 = 3; + agx_index coord = agx_register(8 + 8 * i, AGX_SIZE_32); + coord.channels_m1 = 2; + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), + AGX_DIM_2D_ARRAY, AGX_LOD_MODE_LOD_MIN, 0, + false, false, false, AGX_GATHER_NONE, false); + I->op = AGX_OPCODE_TEXTURE_LOAD; + I->mask = 15; + } + agx_stop(b); + + const auto code = pack(b); + ASSERT_FALSE(code.empty()) << agx3_last_pack_refusal(); + EXPECT_TRUE(b->shader->agx3_staging_known); + EXPECT_EQ(b->shader->agx3_staging_halfs, 8u * banks); + + for (unsigned i = 0; i < ops; ++i) { + const uint16_t bank = 8 * (i % banks); + const uint8_t slot = 1 + (i % banks); + uint8_t bytes[AGX3_TEX_MAX_LENGTH], len = 0; + ASSERT_TRUE(agx3_tex_stage_array_layer_at( + enc, bank, 8 + 8 * i + 4, slot, 0, false, false, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing fetch array layer staging for op " << i << " at bank " + << bank; + + const uint16_t dst_reg = i == 1 ? 0 : (88 + 4 * i); + const uint16_t copy_wait = i >= banks ? (uint16_t)(1u << (slot - 1)) : 0; + ASSERT_TRUE(agx3_tex_stage_copy_hints( + enc, bank, 8 + 8 * i, slot, copy_wait, + enc == AGX_ISA_ENCODING_AGX3C, false, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing fetch coord x staging for op " << i << " at bank " + << bank << " wait " << copy_wait; + + const struct agx3_tex_fetch_desc want = { + .dest = dst_reg, + .coord = bank, + .width = AGX3_TEX_W16, + .mask = 15, + .slot = slot, + .wait_mask = (uint16_t)(1u << (slot - 1)), + .coord_kill = true, + .final = i == ops - 1, + .array = true, + .sampler_value = + (uint16_t)(enc == AGX_ISA_ENCODING_AGX3C ? 1 : 0), + }; + ASSERT_TRUE(agx3_tex_encode_fetch(enc, &want, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing fetch instruction for op " << i << " at bank " << bank; + } + } +} + +/* When an earlier multi-bank op (a 2D gradient sample holding halves 0..15 on + * slot 1) leaves fewer than six free fetch banks on AGX3B, the fifth 2D-array + * fetch hits bank_retire != 0 while slot 2 is still writing r0. Verify it + * retires slot 1 through its own coordinate register rather than r0 and reuses + * bank 0. */ +TEST_F(AGX3PackBinary, TextureFetch2DArrayBankRetireAvoidsR0) +{ + const auto enc = AGX_ISA_ENCODING_AGX3B; + agx_builder *b = builder(enc); + key.dev.chip = AGX_CHIP_G17P; + key.dev.emit_agx3d = false; + b->shader->stage = MESA_SHADER_FRAGMENT; + b->shader->max_reg = 127; + + /* Slot 1: 2D gradient sample occupying staging halves 0..15 (banks 0, 8). */ + { + agx_index dest = agx_register(84, AGX_SIZE_16); + dest.channels_m1 = 3; + agx_index coord = agx_register(4, AGX_SIZE_32); + coord.channels_m1 = 1; + agx_index grad = agx_register(16, AGX_SIZE_32); + grad.channels_m1 = 3; + auto *S = agx_texture_sample_to( + b, dest, coord, grad, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_null(), AGX_DIM_2D, AGX_LOD_MODE_LOD_GRAD, 0, + false, false, false, AGX_GATHER_NONE, false); + S->mask = 15; + } + + /* Slots 2..6: five 2D-array fetches. Fetches 0..3 take banks 16, 24, 32, 40 + * (with fetch 0 writing r0..r1 on slot 2); fetch 4 exhausts the 6-bank ring + * and retires slot 1 via bank_retire on its own coordinate register (56). */ + for (unsigned i = 0; i < 5; ++i) { + const uint16_t dst_reg = i == 0 ? 0 : (88 + 4 * i); + agx_index dest = agx_register(dst_reg, AGX_SIZE_16); + dest.channels_m1 = 3; + agx_index coord = agx_register(24 + 8 * i, AGX_SIZE_32); + coord.channels_m1 = 2; + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), + AGX_DIM_2D_ARRAY, AGX_LOD_MODE_LOD_MIN, 0, + false, false, false, AGX_GATHER_NONE, false); + I->op = AGX_OPCODE_TEXTURE_LOAD; + I->mask = 15; + } + agx_stop(b); + + const auto code = pack(b); + ASSERT_FALSE(code.empty()) << agx3_last_pack_refusal(); + EXPECT_TRUE(b->shader->agx3_staging_known); + EXPECT_EQ(b->shader->agx3_staging_halfs, 48u); + + uint8_t retire_coord[AGX3_ALU_MAX_LENGTH], rlen = 0; + ASSERT_TRUE(agx3_alu_band_identity_wait(enc, 24 + 8 * 4, 1, retire_coord, + &rlen)); + EXPECT_NE(std::search(code.begin(), code.end(), retire_coord, + retire_coord + rlen), + code.end()); + + uint8_t retire_r0[AGX3_ALU_MAX_LENGTH], r0len = 0; + ASSERT_TRUE(agx3_alu_band_identity_wait(enc, 0, 1, retire_r0, &r0len)); + EXPECT_EQ(std::search(code.begin(), code.end(), retire_r0, + retire_r0 + r0len), + code.end()); + + /* Fetch 4 reuses bank 0 on slot 6 after retiring slot 1. */ + const struct agx3_tex_fetch_desc want4 = { + .dest = 88 + 4 * 4, + .coord = 0, + .width = AGX3_TEX_W16, + .mask = 15, + .slot = 6, + .wait_mask = (uint16_t)(1u << 5), + .coord_kill = true, + .final = true, + .array = true, + .sampler_value = 0, + }; + uint8_t bytes[AGX3_TEX_MAX_LENGTH], len = 0; + ASSERT_TRUE(agx3_tex_encode_fetch(enc, &want4, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()); +} + +/* When agx3_retire_wait_on() retires bank_retire | wait_mask on a fetch's + * coordinate register, wait_mask is cleared so the AGX3B multi-wait staging + * guard does not emit a redundant second wait through r0. */ +TEST_F(AGX3PackBinary, TextureFetchBankRetireClearsWaitMask) +{ + const auto enc = AGX_ISA_ENCODING_AGX3B; + agx_builder *b = builder(enc); + key.dev.chip = AGX_CHIP_G17P; + key.dev.emit_agx3d = false; + b->shader->stage = MESA_SHADER_FRAGMENT; + b->shader->max_reg = 127; + + /* Slot 1: 2D-array fetch holding staging bank 0 (halves 0..7). */ + { + agx_index dest = agx_register(80, AGX_SIZE_16); + dest.channels_m1 = 3; + agx_index coord = agx_register(4, AGX_SIZE_32); + coord.channels_m1 = 2; + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), + AGX_DIM_2D_ARRAY, AGX_LOD_MODE_LOD_MIN, 0, + false, false, false, AGX_GATHER_NONE, false); + I->op = AGX_OPCODE_TEXTURE_LOAD; + I->mask = 15; + } + + /* Slot 2: 2D-array fetch (bank 8) producing r40..r47 (coords for op 3). */ + { + agx_index dest = agx_register(40, AGX_SIZE_32); + dest.channels_m1 = 3; + agx_index coord = agx_register(12, AGX_SIZE_32); + coord.channels_m1 = 2; + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), + AGX_DIM_2D_ARRAY, AGX_LOD_MODE_LOD_MIN, 0, + false, false, false, AGX_GATHER_NONE, false); + I->op = AGX_OPCODE_TEXTURE_LOAD; + I->mask = 3; + } + + /* Slot 3: 2D-array fetch (bank 16) producing r48..r55 (dynamic LOD for op 3). */ + { + agx_index dest = agx_register(48, AGX_SIZE_32); + dest.channels_m1 = 3; + agx_index coord = agx_register(20, AGX_SIZE_32); + coord.channels_m1 = 2; + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), + AGX_DIM_2D_ARRAY, AGX_LOD_MODE_LOD_MIN, 0, + false, false, false, AGX_GATHER_NONE, false); + I->op = AGX_OPCODE_TEXTURE_LOAD; + I->mask = 1; + } + + /* Slot 4: plain 2D fetch with register LOD (max_banks == 1 -> bank 0). + * Retires bank_retire (slot 1) | wait_mask (slots 2, 3) on coord r40 in + * one carrier without a redundant r0 noop for slots 2, 3. */ + { + agx_index dest = agx_register(0, AGX_SIZE_16); + dest.channels_m1 = 3; + agx_index coord = agx_register(40, AGX_SIZE_32); + coord.channels_m1 = 1; + agx_index lod = agx_register(48, AGX_SIZE_32); + auto *I = agx_texture_sample_to( + b, dest, coord, lod, agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), + AGX_DIM_2D, AGX_LOD_MODE_LOD_MIN, 0, + false, false, false, AGX_GATHER_NONE, false); + I->op = AGX_OPCODE_TEXTURE_LOAD; + I->mask = 15; + } + agx_stop(b); + + const auto code = pack(b); + ASSERT_FALSE(code.empty()) << agx3_last_pack_refusal(); + + uint8_t retire_all[AGX3_ALU_MAX_LENGTH], alen = 0; + ASSERT_TRUE(agx3_alu_band_identity_wait(enc, 40, 0x7, retire_all, &alen)); + EXPECT_NE(std::search(code.begin(), code.end(), retire_all, + retire_all + alen), + code.end()); + + uint8_t redundant_r0[AGX3_ALU_MAX_LENGTH], rlen = 0; + ASSERT_TRUE(agx3_alu_band_identity_wait(enc, 0, 0x6, redundant_r0, &rlen)); + EXPECT_EQ(std::search(code.begin(), code.end(), redundant_r0, + redundant_r0 + rlen), + code.end()); +} From f014065e27b5ab22de166b75a50cb7b54f88f796 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Sun, 22 Nov 2026 19:14:53 -0500 Subject: [PATCH 2/2] asahi: expand AGX3B plain 2D texel fetch staging ring from 2 to 6 banks Allow plain 2D texel fetches on AGX3B to allocate up to 6 disjoint 8-half staging banks (bounded by f19 <= 60, matching AGX3B's 6 scoreboard slots) before retiring and wrapping, unifying plain_banks with the 2D-array fetch ring. Signed-off-by: Diljit Singh --- src/asahi/compiler/agx3_pack_binary.c | 14 ++-- .../compiler/test/test-agx3-pack-binary.cpp | 68 +++++++++++++++++++ 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/asahi/compiler/agx3_pack_binary.c b/src/asahi/compiler/agx3_pack_binary.c index e496e0bf12ed..e3db1f9f99be 100644 --- a/src/asahi/compiler/agx3_pack_binary.c +++ b/src/asahi/compiler/agx3_pack_binary.c @@ -2361,15 +2361,10 @@ agx3_emit_texture_fetch(struct util_dynarray *emission, const bool array_bank = array && !register_lod; const bool m5_ms_banks = m5 && (ms_2d || ms_array); const bool banked_read = plain_bank || array_bank || m5_ms_banks; - /* Plain M5 fetch coordinates can name any aligned bank through half 60. - * Use independent banks for all eight scoreboard slots before recycling - * one. */ - const unsigned plain_banks = m5 ? - MIN2(AGX3_M5_FETCH_BANKS, 1 + (60 - staging_base) / 8) : 2; - /* 2D-array fetch coordinates can name any aligned bank through half 60 - * (f19); allocate one eight-half bank per scoreboard slot (6 on AGX3B, + /* Fetch coordinates can name any aligned bank through half 60 (f19). + * Use independent eight-half banks for all scoreboard slots (6 on AGX3B, * 8 on AGX3C) before recycling one. */ - const unsigned array_banks = + const unsigned plain_banks = MIN2(m5 ? AGX3_M5_FETCH_BANKS : 6u, 1 + (60 - staging_base) / 8); /* Keep independent non-array MS publications in separate operand banks. */ const unsigned ms_banks = @@ -2399,8 +2394,7 @@ agx3_emit_texture_fetch(struct util_dynarray *emission, const uint16_t first = banked_read ? staging_base : 0; const unsigned max_banks = m5_ms_banks ? ms_banks : - array_bank ? array_banks : - plain_bank ? plain_banks : 1u; + (plain_bank || array_bank) ? plain_banks : 1u; bool placed = false; for (unsigned c = 0; c < max_banks && !placed; ++c) { const uint16_t start = first + 8 * c; diff --git a/src/asahi/compiler/test/test-agx3-pack-binary.cpp b/src/asahi/compiler/test/test-agx3-pack-binary.cpp index 2bd7cb878cdb..114fd4649d22 100644 --- a/src/asahi/compiler/test/test-agx3-pack-binary.cpp +++ b/src/asahi/compiler/test/test-agx3-pack-binary.cpp @@ -17713,3 +17713,71 @@ TEST_F(AGX3PackBinary, TextureFetchBankRetireClearsWaitMask) redundant_r0 + rlen), code.end()); } + +/* On AGX3B, consecutive plain 2D texel fetches allocate up to six disjoint + * banks (0, 8, 16, 24, 32, 40) matching the six scoreboard slots without + * early waits before wrapping cleanly onto bank 0 and bank 8. */ +TEST_F(AGX3PackBinary, TextureFetchPlain2DUsesSixBanksOnAGX3B) +{ + const auto enc = AGX_ISA_ENCODING_AGX3B; + const unsigned banks = 6u; + const unsigned ops = 8u; + agx_builder *b = builder(enc); + key.dev.chip = AGX_CHIP_G17P; + key.dev.emit_agx3d = false; + b->shader->stage = MESA_SHADER_FRAGMENT; + b->shader->max_reg = 127; + + for (unsigned i = 0; i < ops; ++i) { + const uint16_t dst_reg = i == 1 ? 0 : (88 + 4 * i); + agx_index dest = agx_register(dst_reg, AGX_SIZE_16); + dest.channels_m1 = 3; + agx_index coord = agx_register(8 + 8 * i, AGX_SIZE_32); + coord.channels_m1 = 1; + auto *I = agx_texture_sample_to( + b, dest, coord, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), + AGX_DIM_2D, AGX_LOD_MODE_LOD_MIN, 0, + false, false, false, AGX_GATHER_NONE, false); + I->op = AGX_OPCODE_TEXTURE_LOAD; + I->mask = 15; + } + agx_stop(b); + + const auto code = pack(b); + ASSERT_FALSE(code.empty()) << agx3_last_pack_refusal(); + EXPECT_TRUE(b->shader->agx3_staging_known); + EXPECT_EQ(b->shader->agx3_staging_halfs, 8u * (banks - 1) + 4u); + + for (unsigned i = 0; i < ops; ++i) { + const uint16_t bank = 8 * (i % banks); + const uint8_t slot = 1 + (i % banks); + const uint16_t dst_reg = i == 1 ? 0 : (88 + 4 * i); + const uint16_t copy_wait = i >= banks ? (uint16_t)(1u << (slot - 1)) : 0; + uint8_t bytes[AGX3_TEX_MAX_LENGTH], len = 0; + ASSERT_TRUE(agx3_tex_stage_copy_hints( + enc, bank, 8 + 8 * i, slot, copy_wait, false, false, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing plain 2D fetch coord x staging for op " << i + << " at bank " << bank << " wait " << copy_wait; + + const struct agx3_tex_fetch_desc want = { + .dest = dst_reg, + .coord = bank, + .width = AGX3_TEX_W16, + .mask = 15, + .slot = slot, + .wait_mask = (uint16_t)(1u << (slot - 1)), + .coord_kill = true, + .final = i == ops - 1, + .array = false, + .sampler_value = 0, + }; + ASSERT_TRUE(agx3_tex_encode_fetch(enc, &want, bytes, &len)); + EXPECT_NE(std::search(code.begin(), code.end(), bytes, bytes + len), + code.end()) + << "missing plain 2D fetch instruction for op " << i << " at bank " + << bank; + } +}