diff --git a/src/asahi/compiler/agx3_pack_binary.c b/src/asahi/compiler/agx3_pack_binary.c index 59d9f755aaea..41554a691d6d 100644 --- a/src/asahi/compiler/agx3_pack_binary.c +++ b/src/asahi/compiler/agx3_pack_binary.c @@ -46,6 +46,7 @@ #include "agx3_rt_m5_core.h" #include "agx3_select_core.h" #include "agx3_slot_core.h" +#include "agx_mesh_abi.h" #include "agx3_sr_core.h" #include "agx3_zs_core.h" #include "agx3_stage_core.h" @@ -13320,7 +13321,8 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) (I->imm == AGX3_BARRIER_SCOPE_DEVICE || I->imm == AGX3_BARRIER_SCOPE_DEVICE_FENCE || agx3_m5_rendezvous_barrier(ctx, enc, I->imm))) || - I->op == AGX_OPCODE_RT_BARRIER_GEN1; + I->op == AGX_OPCODE_RT_BARRIER_GEN1 || + I->op == AGX_OPCODE_FENCE_UVB_GEN1; e->reissues_live_slot = I->op == AGX_OPCODE_LDCF; if (I->agx3_tex_coord_start) e->preferred_slot = native_coord_preferred_slot; @@ -13648,6 +13650,34 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) } } + const bool gen1_mesh = enc == AGX_ISA_ENCODING_AGX3B && + ctx->key->dev.chip == AGX_CHIP_G17P && + ctx->stage == MESA_SHADER_MESH && !ctx->is_preamble; + if (gen1_mesh) { + bool exports_pending = false; + for (unsigned i = 0; i < count; ++i) { + if (ordered[i]->op == AGX_OPCODE_FENCE_UVB_GEN1) + exports_pending = false; + else if (ordered[i]->op == AGX_OPCODE_STORE_UVB_M5) + exports_pending = true; + else if (exports_pending && sl[i].is_async && + !sl[i].preferred_slot && !sl[i].transient_slots && + !sl[i].reissues_live_slot && !sl[i].join_class) { + /* Unlinked mesh interfaces load UVS selectors from the root + * table between vertex exports. Restrict every ordinary async + * producer in this region, not just loads: a restricted load + * changes the shared cursor and could otherwise move a later + * system-value read onto an observed phase channel. + * Slot 1 is outside both phase channels. Keep it an ordinary + * allocation so reuse retires a live generation; retain normal + * RAW/WAW waits and all emitter guards. Required compound, + * preferred, reissuable and joined protocols stay unchanged. + * This does not assign an implicit UVST producer or lifetime. */ + sl[i].max_slot = 1; + } + } + } + const struct agx3_slot_entry input_entry = { .token = AGX3_SLOT_CPROG_INPUT_TOKEN, .slot = 6, }; @@ -13891,6 +13921,10 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) * agx3_r0_inflight once that instruction has been emitted. */ uint16_t r0_issued = 0; + bool mesh_allocated = false, mesh_indices_fenced = false; + bool mesh_exports_pending = false; + unsigned mesh_vertex_fences = 0; + agx_foreach_block(ctx, block) { block->offset = emission->size; block->loop_exit_offset = -1; @@ -13958,6 +13992,51 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) if (slot) outstanding |= (uint16_t)(1u << (slot - 1)); + /* The native phase sequence does not establish an asynchronous + * UVST producer or its source-register lifetime. Do not allow an + * explicit producer to reuse the phase's observed wait channel + * after an export and before its boundary. Refuse an ambiguous + * lowering instead of treating a reused slot as export completion. + * Other explicit channels retain their ordinary dependency model. */ + if (gen1_mesh && mesh_exports_pending && slot && + (1u << (slot - 1)) == (mesh_indices_fenced ? 4u : 2u)) + AGX3_REFUSE(I); + + if (I->op == AGX_OPCODE_FENCE_UVB_GEN1) { + if (!gen1_mesh || !mesh_allocated || I->nr_srcs || I->nr_dests || + slot || I->imm > AGX_MESH_EXPORT_FENCE_VERTEX_BATCH || + (I->imm == AGX_MESH_EXPORT_FENCE_INDICES + ? mesh_indices_fenced : !mesh_indices_fenced)) + AGX3_REFUSE(I); + const uint16_t retire = wait | outstanding_here; + const uint16_t phase = I->imm == AGX_MESH_EXPORT_FENCE_INDICES ? 2 : 4; + const uint16_t src[] = {0, 0}; + const enum agx3_alu_hint hints[] = { + AGX3_ALU_HINT_NONE, AGX3_ALU_HINT_NONE}; + uint8_t bytes[AGX3_ALU_MAX_LENGTH], len = 0; + /* Drain ALL modeled explicit producers, including any pending + * writer of either half of r0, before this live identity reads + * r0. The source-less AIR fence has drain semantics in the slot + * pass; the emission ledger independently covers any residual. + * Use the exact native bitop waits, without retire-noop's extra + * slot6 wait. No comparison flag, nesting state or value changes. + * Phase masks are the G17P exporter protocol at these source + * boundaries, not universal UVST producer-slot assignments. */ + if (!agx3_alu_bit_reg_native(enc, AGX3_ALU_BAND, 0, src, hints, + false, retire | phase, bytes, &len) || + !agx3_append(emission, bytes, len)) + AGX3_REFUSE(I); + outstanding = 0; + image_write_slots &= (uint16_t)~retire; + agx3_staging_forget(&staging_banks, retire); + mesh_exports_pending = false; + if (I->imm == AGX_MESH_EXPORT_FENCE_INDICES) + mesh_indices_fenced = true; + else + ++mesh_vertex_fences; + continue; + } + if (I->op == AGX_OPCODE_MATMAD16_AGX3) { if (enc != AGX_ISA_ENCODING_AGX3C || ctx->is_preamble || ctx->stage != MESA_SHADER_COMPUTE || I->nr_dests != 1 || @@ -16687,6 +16766,18 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) * generation before the allocation claims the slot, then wait * for the allocation itself. */ const bool alloc_slot = I->op == AGX_OPCODE_ALLOCATE_UVB_M5; + if (gen1_mesh) { + if (alloc_slot) { + if (mesh_allocated) + AGX3_REFUSE(I); + mesh_allocated = true; + } else { + if (!mesh_allocated || outstanding_here || + (I->imm ? mesh_indices_fenced : !mesh_indices_fenced)) + AGX3_REFUSE(I); + mesh_exports_pending = true; + } + } const uint16_t pre_wait = alloc_slot ? (wait | (outstanding_here & 1u)) : wait; if (!encoded || (pre_wait && !agx3_retire_slots_noop(emission, enc, pre_wait)) || !agx3_append(emission, bytes, length)) @@ -23255,6 +23346,13 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) goto fail; } + if (gen1_mesh && mesh_allocated && + (!mesh_indices_fenced || + (ctx->nir && ctx->nir->info.mesh.max_vertices_out && + mesh_vertex_fences + 1 != + DIV_ROUND_UP(ctx->nir->info.mesh.max_vertices_out, 32)))) + AGX3_PACK_FAIL(); + /* Native1061's attachmentless1x/2x/4x fragment is pixel entry, final * release and end even when its epilog has no stores. This terminal * secondary part has no emitted instructions or outstanding work. Mark diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index b046895443e7..3ec5744ed988 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -4228,6 +4228,8 @@ agx_emit_intrinsic(agx_builder *b, nir_intrinsic_instr *instr) case nir_intrinsic_allocate_mesh_uvb_agx: return agx_allocate_uvb_m5(b, agx_src_index(&instr->src[0]), agx_src_index(&instr->src[1])); + case nir_intrinsic_fence_mesh_uvb_agx: + return agx_fence_uvb_gen1(b, nir_intrinsic_base(instr)); case nir_intrinsic_store_mesh_uvb_agx: return agx_store_uvb_m5(b, agx_src_index(&instr->src[0]), nir_intrinsic_flags(instr)); diff --git a/src/asahi/compiler/agx_mesh_abi.h b/src/asahi/compiler/agx_mesh_abi.h index 998d68554505..a8d71d6917bb 100644 --- a/src/asahi/compiler/agx_mesh_abi.h +++ b/src/asahi/compiler/agx_mesh_abi.h @@ -2,3 +2,8 @@ #include "compiler/shader_enums.h" #define AGX_MESH_PRIMITIVE_PAGE_SLOT VARYING_SLOT_FACE + +enum agx_mesh_export_fence { + AGX_MESH_EXPORT_FENCE_INDICES, + AGX_MESH_EXPORT_FENCE_VERTEX_BATCH, +}; diff --git a/src/asahi/compiler/agx_nir_gen1_mesh.c b/src/asahi/compiler/agx_nir_gen1_mesh.c index d3ac074f55c4..a2781a83baec 100644 --- a/src/asahi/compiler/agx_nir_gen1_mesh.c +++ b/src/asahi/compiler/agx_nir_gen1_mesh.c @@ -176,6 +176,16 @@ lower(nir_builder *b,nir_intrinsic_instr *i,void *data) b->cursor=nir_before_instr(&i->instr); nir_def *element=nir_imul_imm(b,i->src[1].ssa,4); const unsigned slot=vertex_output_slot(i); + if (slot == VARYING_SLOT_LAYER) { + /* A workgroup-uniform layer (vertex_layer): the vertex's own layer + * word, layer low, viewport 0 high, as the export packs it. */ + nir_def *layer = nir_umin(b, nir_u2u32(b, i->src[0].ssa), + nir_imm_int(b, 0xffff)); + nir_store_shared(b, layer, nir_iadd_imm(b, element, + layout->position + layout->plane_stride * layout->varyings->lv_first), + .write_mask = 1, .align_mul = 4); + break; + } if (slot != VARYING_SLOT_POS && !layout->varyings->components[slot]) break; const unsigned first=first_plane(layout->varyings,slot); @@ -346,6 +356,14 @@ agx_gen1_mesh_gather_varyings(const nir_shader *s, } if (slot == VARYING_SLOT_POS) continue; + if (slot == VARYING_SLOT_LAYER) { + /* Only hk's workgroup-uniform form (see vertex_layer). */ + if (nir_intrinsic_component(i) != 0 || + nir_intrinsic_write_mask(i) != 1) + M5_REFUSE(); + varyings->vertex_layer = true; + continue; + } if (slot == VARYING_SLOT_PSIZ && s->info.mesh.primitive_type != MESA_PRIM_POINTS) continue; @@ -399,8 +417,12 @@ agx_gen1_mesh_gather_varyings(const nir_shader *s, varyings->components[VARYING_SLOT_PSIZ] = 1; varyings->first[VARYING_SLOT_PSIZ] = next++; } - if (varyings->primitive_comps[VARYING_SLOT_LAYER] || - varyings->primitive_comps[VARYING_SLOT_VIEWPORT]) { + const bool primitive_lv = varyings->primitive_comps[VARYING_SLOT_LAYER] || + varyings->primitive_comps[VARYING_SLOT_VIEWPORT]; + /* A per-vertex layer leaves no word for a per-primitive one. */ + if (varyings->vertex_layer && primitive_lv) + M5_REFUSE(); + if (primitive_lv || varyings->vertex_layer) { varyings->lv = true; varyings->lv_first = next++; } @@ -492,9 +514,13 @@ agx_nir_lower_gen1_mesh(nir_shader *s, const struct agx_m5_mesh_varyings *varyin prim_vertices(s)==0 || (s->info.per_primitive_outputs & ~(VARYING_BIT_PRIMITIVE_INDICES | PRIMITIVE_SLOTS | VARYING_BIT_CULL_PRIMITIVE)) || - (s->info.outputs_written & ~s->info.per_primitive_outputs) & ~(VARYING_BIT_POS|VARYING_BIT_PSIZ|MESH_CLIP_SLOTS|USER_VARYINGS) || + (s->info.outputs_written & ~s->info.per_primitive_outputs) & + ~(VARYING_BIT_POS|VARYING_BIT_PSIZ|MESH_CLIP_SLOTS|USER_VARYINGS| + (varyings->vertex_layer ? VARYING_BIT_LAYER : 0)) || !varyings_valid(varyings)) M5_REFUSE(); + /* The layer and viewport words of primitives (not a vertex_layer). */ + const bool primitive_lv = varyings->lv && !varyings->vertex_layer; struct mesh_layout layout = mesh_layout(s, varyings); if (varyings->cull_primitive) { layout.cull = layout.output_base + layout.bytes; @@ -521,7 +547,7 @@ agx_nir_lower_gen1_mesh(nir_shader *s, const struct agx_m5_mesh_varyings *varyin const unsigned unweld_vertices = prim_vertices(s) * s->info.mesh.max_primitives_out; const unsigned unweld_stride = ALIGN_POT(unweld_vertices, 4) * 4; const unsigned unweld_base = layout.output_base + layout.bytes; - const bool unweld = varyings->lv && unweld_vertices <= 256 && + const bool unweld = primitive_lv && unweld_vertices <= 256 && unweld_base + layout.planes * unweld_stride <= 32768; /* The layer and viewport the rasterizer takes from the exported vertex * word are staged in threadgroup memory, after the export arena. They @@ -532,7 +558,7 @@ agx_nir_lower_gen1_mesh(nir_shader *s, const struct agx_m5_mesh_varyings *varyin * (a view 1 primitive of a multiview mesh draw rasterised into layer 0). * A shader too close to the threadgroup memory limit keeps the read-back * rather than losing its export arena or failing to compile. */ - if (varyings->lv) { + if (primitive_lv) { const unsigned lv_bytes = ALIGN_POT(4 * s->info.mesh.max_primitives_out, 64); const unsigned end = unweld ? unweld_base + layout.planes * unweld_stride : layout.output_base + layout.bytes; @@ -577,6 +603,8 @@ agx_nir_lower_gen1_mesh(nir_shader *s, const struct agx_m5_mesh_varyings *varyin M5_REFUSE(); if(slot==VARYING_SLOT_POS) { if(bits!=32)M5_REFUSE(); + } else if(slot==VARYING_SLOT_LAYER) { + if(!varyings->vertex_layer || end>1)M5_REFUSE(); } else if(slot==VARYING_SLOT_PSIZ) { if(varyings->components[slot] && (bits!=32 || end>1))M5_REFUSE(); } else if(!varyings->components[slot] || @@ -914,7 +942,7 @@ agx_nir_lower_gen1_mesh(nir_shader *s, const struct agx_m5_mesh_varyings *varyin layout.output_base; s->info.mesh.max_vertices_out = unweld_vertices; } - if (varyings->lv) { + if (primitive_lv) { nir_def *lane = nir_load_subgroup_invocation(&b); for (unsigned prim = 0; prim < s->info.mesh.max_primitives_out; prim += 32) { nir_def *index = nir_iadd_imm(&b, lane, prim); @@ -1017,6 +1045,11 @@ agx_nir_lower_gen1_mesh(nir_shader *s, const struct agx_m5_mesh_varyings *varyin + /* G17P's export phases are separate from logical workgroup dimensions, + * API topology and the declared output limits. Complete every index + * batch before opening the vertex phase. This is outside the index IFs: + * zero and partial counts must reconverge before the phase boundary. */ + nir_fence_mesh_uvb_agx(&b, .base = AGX_MESH_EXPORT_FENCE_INDICES); nir_def *selector[4 + 128] = {NULL}; if (varyings->dynamic) { for (unsigned slot = 0; slot < 64; ++slot) { @@ -1029,6 +1062,10 @@ agx_nir_lower_gen1_mesh(nir_shader *s, const struct agx_m5_mesh_varyings *varyin } } for(unsigned vertex=0;vertexinfo.mesh.max_vertices_out;vertex+=32) { + /* No terminal fence: only order a preceding batch before the next + * batch's predicate, in the same SIMD0 epilogue scope. */ + if (vertex) + nir_fence_mesh_uvb_agx(&b, .base = AGX_MESH_EXPORT_FENCE_VERTEX_BATCH); nir_push_if(&b,nir_ult(&b,nir_iadd_imm(&b,lane,vertex),nir_u2u32(&b,v))); for(unsigned plane=0;planeshader->stage = MESA_SHADER_MESH; + agx_mov_imm_to(b, agx_register(8, AGX_SIZE_16), 32); + agx_mov_imm_to(b, agx_register(10, AGX_SIZE_16), 64); + agx_allocate_uvb_m5(b, agx_register(8, AGX_SIZE_16), + agx_register(10, AGX_SIZE_16)); + agx_index desc = agx_register(12, AGX_SIZE_32); desc.channels_m1 = 1; + agx_mov_imm_to(b, agx_register(12, AGX_SIZE_32), 0); + agx_mov_imm_to(b, agx_register(14, AGX_SIZE_32), 256); + agx_store_uvb_m5(b, desc, 1); + agx_fence_uvb_gen1(b, AGX_MESH_EXPORT_FENCE_INDICES); + agx_store_uvb_m5(b, desc, 0); + agx_fence_uvb_gen1(b, AGX_MESH_EXPORT_FENCE_VERTEX_BATCH); + agx_store_uvb_m5(b, desc, 0); + agx_stop(b); + const auto code = pack(b); + const uint8_t index[] = {0x0b,0x01,0x07,0x01,0,0x40,0,0,0,0}; + const uint8_t vertex[] = {0x0b,0x01,0x07,0x01,0,0x80,0,0,0,0}; + EXPECT_NE(std::search(code.begin(), code.end(), index, index + sizeof(index)), code.end()); + EXPECT_NE(std::search(code.begin(), code.end(), vertex, vertex + sizeof(vertex)), code.end()); + /* Neither the final export nor END gains a terminal phase wait. */ + ASSERT_GE(code.size(), 10u); + EXPECT_EQ(std::vector(code.end() - 4, code.end()), + (std::vector{0x0e,0,0,0})); +} + +TEST_F(AGX3PackBinary, MeshExportFenceDrainsBothPendingR0Halves) +{ + agx_builder *b = builder(AGX_ISA_ENCODING_AGX3B); + key.dev.chip = AGX_CHIP_G17P; + b->shader->stage = MESA_SHADER_MESH; + agx_mov_imm_to(b, agx_register(8, AGX_SIZE_16), 0); + agx_allocate_uvb_m5(b, agx_register(8, AGX_SIZE_16), + agx_register(8, AGX_SIZE_16)); + /* Zero exports can still have unrelated explicit producers. Their two + * current generations must complete before the carrier touches h0/h1. */ + agx_device_load_to(b, agx_register(0, AGX_SIZE_16), + agx_uniform(0, AGX_SIZE_64), agx_immediate(0), AGX_FORMAT_I16, 1, 0, false); + agx_device_load_to(b, agx_register(1, AGX_SIZE_16), + agx_uniform(0, AGX_SIZE_64), agx_immediate(2), AGX_FORMAT_I16, 1, 0, false); + agx_fence_uvb_gen1(b, AGX_MESH_EXPORT_FENCE_INDICES); + agx_iadd_to(b, agx_register(4, AGX_SIZE_16), + agx_register(0, AGX_SIZE_16), agx_register(1, AGX_SIZE_16), 0); + agx_stop(b); + const auto code = pack(b); + const uint8_t complete[] = {0x0b,0x01,0x07,0x01,0,0x60,0,0,0,0}; + EXPECT_NE(std::search(code.begin(), code.end(), complete, + complete + sizeof(complete)), code.end()); +} + +TEST_F(AGX3PackBinary, MeshExportFenceRefusesInvalidProtocolOrChannelReuse) +{ + for (unsigned bad : {0u,1u,2u,3u,4u,5u}) { + agx_builder *b = builder(bad == 5 ? AGX_ISA_ENCODING_AGX3C : AGX_ISA_ENCODING_AGX3B); + key.dev.chip = bad == 5 ? AGX_CHIP_G17S : AGX_CHIP_G17P; + b->shader->stage = MESA_SHADER_MESH; + agx_mov_imm_to(b, agx_register(8, AGX_SIZE_16), 1); + if (bad != 0) + agx_allocate_uvb_m5(b, agx_register(8, AGX_SIZE_16), agx_register(8, AGX_SIZE_16)); + agx_index desc = agx_register(12, AGX_SIZE_32); desc.channels_m1 = 1; + if (bad == 4) { + agx_store_uvb_m5(b, desc, 1); + /* Ordinary producers are now allocated away from phase channels. + * Force the native ISEL pair instead: its required slot2 must not + * be rewritten by the policy or reuse a pending index phase. */ + agx_index first = agx_register(4, AGX_SIZE_32); + agx_index second = agx_register(6, AGX_SIZE_32); + agx_device_load_to(b, first, agx_uniform(0, AGX_SIZE_64), + agx_immediate(0), AGX_FORMAT_I32, 1, 0, false); + agx_device_load_to(b, second, agx_uniform(0, AGX_SIZE_64), + agx_immediate(4), AGX_FORMAT_I32, 1, 0, false); + agx_icmpsel_to(b, agx_register(16, AGX_SIZE_32), first, second, + first, second, AGX_ICOND_UEQ); + } + agx_fence_uvb_gen1(b, bad == 1 ? AGX_MESH_EXPORT_FENCE_VERTEX_BATCH : + bad == 2 ? 2 : AGX_MESH_EXPORT_FENCE_INDICES); + if (bad == 3) + agx_fence_uvb_gen1(b, AGX_MESH_EXPORT_FENCE_INDICES); + agx_stop(b); + EXPECT_TRUE(pack(b, false).empty()) << bad; + } +} + +/* Actual unlinked GPL failure: position UVSTs precede root-table loads of + * the page carrier and location0 selectors. A round-robin slot3 load after + * the first vertex UVST violates the phase protocol even when its result is + * immediately consumed by the next descriptor. Vary the incoming cursor so + * success cannot depend on how many API-body loads happened to precede it. */ +TEST_F(AGX3PackBinary, MeshUnlinkedSelectorsBetweenExports) +{ + for (bool with_system_reads : {false, true}) { + SCOPED_TRACE(with_system_reads); + for (unsigned warmup = 0; warmup < 6; ++warmup) { + SCOPED_TRACE(warmup); + agx_builder *b = builder(AGX_ISA_ENCODING_AGX3B); + key.dev.chip = AGX_CHIP_G17P; + b->shader->stage = MESA_SHADER_MESH; + for (unsigned i = 0; i < warmup; ++i) + agx_device_load_to(b, agx_register(32 + 2 * i, AGX_SIZE_32), + agx_uniform(0, AGX_SIZE_64), agx_immediate(i), + AGX_FORMAT_I32, 1, 0, false); + for (unsigned i = 0; i < warmup; ++i) + agx_mov_to(b, agx_register(64 + 2 * i, AGX_SIZE_32), + agx_register(32 + 2 * i, AGX_SIZE_32)); + agx_mov_imm_to(b, agx_register(8, AGX_SIZE_16), 2); + agx_mov_imm_to(b, agx_register(10, AGX_SIZE_16), 6); + agx_allocate_uvb_m5(b, agx_register(8, AGX_SIZE_16), + agx_register(10, AGX_SIZE_16)); + agx_index desc = agx_register(12, AGX_SIZE_32); + desc.channels_m1 = 1; + agx_mov_imm_to(b, agx_register(12, AGX_SIZE_32), 0); + agx_mov_imm_to(b, agx_register(14, AGX_SIZE_32), 0); + agx_store_uvb_m5(b, desc, 1); + agx_fence_uvb_gen1(b, AGX_MESH_EXPORT_FENCE_INDICES); + for (unsigned plane = 0; plane < 4; ++plane) { + agx_mov_imm_to(b, agx_register(12, AGX_SIZE_32), plane); + agx_mov_imm_to(b, agx_register(14, AGX_SIZE_32), 192 + 32 * plane); + agx_store_uvb_m5(b, desc, 0); + } + for (unsigned selector : {128u, 130u}) { + agx_device_load_to(b, agx_register(2, AGX_SIZE_32), + agx_uniform(0, AGX_SIZE_64), agx_immediate(selector), + AGX_FORMAT_I8, 1, 2, false); + agx_mov_to(b, agx_register(12, AGX_SIZE_32), + agx_register(2, AGX_SIZE_32)); + agx_mov_imm_to(b, agx_register(14, AGX_SIZE_32), 320); + agx_store_uvb_m5(b, desc, 0); + } + if (with_system_reads) { + /* With warmup3, old allocation used selectors4/5 then SR6/1. + * Restricting only the selectors instead leaves SR2/3, making + * the second SR collide with the still-pending vertex phase. */ + agx_get_sr_to(b, agx_register(16, AGX_SIZE_32), + AGX_SR_THREADGROUP_POSITION_IN_GRID_X); + agx_mov_to(b, agx_register(20, AGX_SIZE_32), + agx_register(16, AGX_SIZE_32)); + agx_get_sr_to(b, agx_register(18, AGX_SIZE_32), + AGX_SR_THREADGROUP_POSITION_IN_GRID_Y); + agx_mov_to(b, agx_register(22, AGX_SIZE_32), + agx_register(18, AGX_SIZE_32)); + } + agx_stop(b); + /* Match production: MOV is a pseudo, lowered to a bitwise copy + * before native packing. Keep the actual selector/SR reads. */ + agx_lower_pseudo(b->shader); + const auto code = pack(b); + ASSERT_FALSE(code.empty()) << agx3_last_pack_refusal(); + const uint8_t index[] = {0x0b,0x01,0x07,0x01,0,0x40,0,0,0,0}; + EXPECT_NE(std::search(code.begin(), code.end(), index, + index + sizeof(index)), code.end()); + EXPECT_EQ(std::vector(code.end() - 4, code.end()), + (std::vector{0x0e,0,0,0})); + } + } +} + +/* Choosing a non-phase channel is insufficient by itself. A selector not + * consumed yet must still make the existing outstanding-operation UVST gate + * fail closed; the policy must not become an unconditional export drain. */ +TEST_F(AGX3PackBinary, MeshUnconsumedSelectorStillRefusesExport) +{ + agx_builder *b = builder(AGX_ISA_ENCODING_AGX3B); + key.dev.chip = AGX_CHIP_G17P; + b->shader->stage = MESA_SHADER_MESH; + agx_mov_imm_to(b, agx_register(8, AGX_SIZE_16), 1); + agx_allocate_uvb_m5(b, agx_register(8, AGX_SIZE_16), + agx_register(8, AGX_SIZE_16)); + agx_index desc = agx_register(12, AGX_SIZE_32); + desc.channels_m1 = 1; + agx_mov_imm_to(b, agx_register(12, AGX_SIZE_32), 0); + agx_mov_imm_to(b, agx_register(14, AGX_SIZE_32), 192); + agx_store_uvb_m5(b, desc, 1); + agx_fence_uvb_gen1(b, AGX_MESH_EXPORT_FENCE_INDICES); + agx_store_uvb_m5(b, desc, 0); + agx_device_load_to(b, agx_register(2, AGX_SIZE_32), + agx_uniform(0, AGX_SIZE_64), agx_immediate(128), + AGX_FORMAT_I8, 1, 2, false); + agx_store_uvb_m5(b, desc, 0); + agx_stop(b); + EXPECT_TRUE(pack(b, false).empty()); +} + TEST_F(AGX3PackBinary, MultisampleTileBankCountsPartitionsNotStores) { for (unsigned targets : {2u, 3u, 6u}) { diff --git a/src/asahi/compiler/test/test-native-form-regressions.cpp b/src/asahi/compiler/test/test-native-form-regressions.cpp index 9b12813763d4..7c763ddf911e 100644 --- a/src/asahi/compiler/test/test-native-form-regressions.cpp +++ b/src/asahi/compiler/test/test-native-form-regressions.cpp @@ -19,6 +19,7 @@ extern "C" { #include "compiler/nir/nir_builder.h" #include "agx_nir_m5_mesh.h" #include "agx3_mesh_core.h" +#include "agx_mesh_abi.h" #include #include #include @@ -6177,6 +6178,91 @@ m5_mesh_provoking_shader(mesa_prim primitive, unsigned vertices, return b.shader; } +TEST(NativeForms, Gen1MeshExportFencesCoverGenericShapesAndPartialCounts) +{ + const struct { + mesa_prim topology; + unsigned x, y, z, vertices, primitives; + } rows[] = { + {MESA_PRIM_TRIANGLES, 3,1,1,3,1}, + {MESA_PRIM_LINES, 16,1,1,32,16}, + {MESA_PRIM_LINES, 32,1,1,64,32}, + {MESA_PRIM_LINES, 10,4,1,256,256}, + {MESA_PRIM_POINTS, 1,1,128,128,128}, + {MESA_PRIM_LINES, 128,1,1,256,128}, + {MESA_PRIM_POINTS, 32,1,1,33,1}, + }; + for (const auto &r : rows) { + for (unsigned count : {0u, 1u, r.vertices}) { + nir_shader *s = m5_mesh_provoking_shader(r.topology, r.vertices, + r.primitives, 0, false); + s->info.workgroup_size[0]=r.x; s->info.workgroup_size[1]=r.y; + s->info.workgroup_size[2]=r.z; + nir_foreach_block(block, nir_shader_get_entrypoint(s)) { + nir_foreach_instr(in, block) { + if (in->type != nir_instr_type_intrinsic || + nir_instr_as_intrinsic(in)->intrinsic != + nir_intrinsic_set_vertex_and_primitive_count) + continue; + nir_builder b = nir_builder_at(nir_before_instr(in)); + nir_intrinsic_instr *i = nir_instr_as_intrinsic(in); + nir_src_rewrite(&i->src[0], nir_imm_int(&b, count)); + nir_src_rewrite(&i->src[1], nir_imm_int(&b, MIN2(count, r.primitives))); + } + } + struct agx_m5_mesh_varyings v; + ASSERT_TRUE(agx_gen1_mesh_gather_varyings(s, &v)); + ASSERT_TRUE(agx_nir_lower_gen1_mesh(s, &v)); + nir_validate_shader(s, "gen1 mesh export phase scopes"); + unsigned phase=0, index_sites=0, vertex_sites=0; + nir_foreach_block(block, nir_shader_get_entrypoint(s)) { + nir_foreach_instr(in, block) { + if (in->type != nir_instr_type_intrinsic) + continue; + nir_intrinsic_instr *i = nir_instr_as_intrinsic(in); + if (i->intrinsic == nir_intrinsic_fence_mesh_uvb_agx) { + EXPECT_EQ(nir_intrinsic_base(i), phase ? + AGX_MESH_EXPORT_FENCE_VERTEX_BATCH : AGX_MESH_EXPORT_FENCE_INDICES); + /* Each fence is in SIMD0's then-list, after the prior + * index/batch IF and before the NEXT vertex IF. This + * remains true at count0, count1 and the full limit. */ + ASSERT_NE(block->cf_node.parent, nullptr); + EXPECT_EQ(block->cf_node.parent->type, nir_cf_node_if); + EXPECT_EQ(nir_cf_node_prev(&block->cf_node)->type, nir_cf_node_if); + EXPECT_EQ(nir_cf_node_next(&block->cf_node)->type, nir_cf_node_if); + ++phase; + } else if (i->intrinsic == nir_intrinsic_store_mesh_uvb_agx) { + if (nir_intrinsic_flags(i)) { + EXPECT_EQ(phase, 0u); ++index_sites; + } else { + ASSERT_GT(phase, 0u); ++vertex_sites; + ASSERT_TRUE(nir_src_is_const(i->src[0])); + EXPECT_EQ(nir_src_comp_as_uint(i->src[0],0) >> 16, + 32 * (phase - 1)); + } + } + } + } + EXPECT_EQ(phase, DIV_ROUND_UP(r.vertices,32)); + const unsigned corners = r.topology == MESA_PRIM_POINTS ? 1 : + r.topology == MESA_PRIM_LINES ? 2 : 3; + EXPECT_EQ(index_sites, DIV_ROUND_UP(DIV_ROUND_UP(corners*r.primitives,4),32)); + EXPECT_GT(vertex_sites, 0u); + ralloc_free(s); + } + } + /* The separate G17S exporter has no new fence or changed protocol. */ + nir_shader *s = m5_mesh_provoking_shader(MESA_PRIM_POINTS,128,128,0,false); + ASSERT_TRUE(agx_nir_lower_m5_mesh_triangle_probe(s)); + nir_foreach_block(block, nir_shader_get_entrypoint(s)) { + nir_foreach_instr(in, block) { + if (in->type == nir_instr_type_intrinsic) + EXPECT_NE(nir_instr_as_intrinsic(in)->intrinsic, nir_intrinsic_fence_mesh_uvb_agx); + } + } + ralloc_free(s); +} + static bool m5_test_provoking_last(nir_builder *b, nir_intrinsic_instr *i, void *data) { diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 0c3ae38f2c30..6bb18769db23 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -11688,7 +11688,7 @@ hk_flush_dynamic_state_slow(struct hk_cmd_buffer *cmd, struct hk_cs *cs, .raw_object_template = 0, - .raw_mesh_template = dev->dev.chip == AGX_CHIP_G17P ? 0 : 0x40, + .raw_mesh_template = 0x40, @@ -11702,16 +11702,22 @@ hk_flush_dynamic_state_slow(struct hk_cmd_buffer *cmd, struct hk_cs *cs, dev->dev.chip == AGX_CHIP_G17P ? DIV_ROUND_UP(hw_vs->info.mesh.primitive_stride_B * hw_vs->info.mesh.max_primitives, 4) : 0, - dev->dev.chip == AGX_CHIP_G17P - ? DIV_ROUND_UP(hw_vs->b.info.local_size, 16) : 9}, + 0}, }; - if (dev->dev.chip == AGX_CHIP_G17S) { + /* G17P replays views rather than amplifying them in hardware. Its + * ordinary mesh output extent uses the same encoding as G17S. + * Threadgroup memory is allocated separately by the shader program. + */ + if (dev->dev.chip == AGX_CHIP_G17P || + dev->dev.chip == AGX_CHIP_G17S) { if (!agx_vdm_m5_mesh_set_output_limits( &state, hw_vs->info.mesh.max_vertices, hw_vs->info.mesh.max_primitives, hw_vs->info.uvs.size)) { vk_command_buffer_set_error(&cmd->vk, VK_ERROR_FEATURE_NOT_PRESENT); return; } + } + if (dev->dev.chip == AGX_CHIP_G17S) { /* Native object->mesh kicks (native726 mesh-object, 0x1ca48 * bound) set tail[0]; task kicks with the native record do too, * with or without the grid at +0 (the buffer stays bound). diff --git a/src/asahi/vulkan/hk_shader.c b/src/asahi/vulkan/hk_shader.c index a5e637f810de..1b8d23b9ebeb 100644 --- a/src/asahi/vulkan/hk_shader.c +++ b/src/asahi/vulkan/hk_shader.c @@ -2462,8 +2462,25 @@ glsl_type_size(const struct glsl_type *type, bool bindless) * This must run on the last pre-rasterization stage, since that is the stage * whose layer output the rasterizer consumes: see hk_lower_multiview_stage(). */ +/* A multiview fragment shader's gl_Layer is the view index, as + * hk_lower_multiview() makes the last pre-rasterization stage write it. Read + * it from the root, like gl_ViewIndex: a G17P mesh shader carries no + * per-primitive layer for it (hk_lower_multiview()). */ +static bool +hk_lower_multiview_fs_layer(nir_builder *b, nir_intrinsic_instr *intr, + UNUSED void *data) +{ + if (intr->intrinsic != nir_intrinsic_load_layer_id) + return false; + + b->cursor = nir_before_instr(&intr->instr); + nir_def_replace(&intr->def, nir_u2uN(b, nir_load_view_index(b), + intr->def.bit_size)); + return true; +} + static void -hk_lower_multiview(nir_shader *nir) +hk_lower_multiview(nir_shader *nir, bool g17p) { /* If there's an existing layer ID write, ignore it. This avoids validation * splat with vk_meta. @@ -2493,6 +2510,36 @@ hk_lower_multiview(nir_shader *nir) ? nir_before_impl(impl) : nir_after_impl(impl)); + if (nir->info.stage == MESA_SHADER_MESH && g17p && + !nir_find_variable_with_location(nir, nir_var_shader_out, + VARYING_SLOT_VIEWPORT)) { + /* The view is the same for the whole workgroup, so on G17P it goes to + * every vertex's layer word (agx_nir_lower_gen1_mesh, vertex_layer): + * a per-primitive layer would give each primitive private vertices + * and a primitive record. Stored first, before any early return: a + * vertex word nothing writes is undefined. */ + const unsigned verts = nir->info.mesh.max_vertices_out; + const unsigned threads = nir->info.workgroup_size[0] * + nir->info.workgroup_size[1] * + nir->info.workgroup_size[2]; + b.cursor = nir_before_impl(impl); + nir_variable *layer = nir_variable_create( + nir, nir_var_shader_out, glsl_array_type(glsl_uint_type(), verts, 0), + "view_layer"); + layer->data.location = VARYING_SLOT_LAYER; + nir_def *view = nir_load_view_index(&b); + nir_def *lane = nir_load_local_invocation_index(&b); + for (unsigned first = 0; first < verts; first += threads) { + nir_def *vertex = nir_iadd_imm(&b, lane, first); + nir_push_if(&b, nir_ult_imm(&b, vertex, verts)); + nir_store_deref(&b, nir_build_deref_array(&b, + nir_build_deref_var(&b, layer), vertex), view, 1); + nir_pop_if(&b, NULL); + } + nir->info.outputs_written |= VARYING_BIT_LAYER; + return; + } + if (nir->info.stage == MESA_SHADER_MESH) { /* Every primitive the workgroup may output goes to the view's layer. */ const unsigned prims = nir->info.mesh.max_primitives_out; @@ -3223,7 +3270,12 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, if (is_multiview && hk_lower_multiview_stage(nir->info.stage, next_stage_mask)) - hk_lower_multiview(nir); + hk_lower_multiview(nir, dev->dev.chip == AGX_CHIP_G17P); + + if (is_multiview && nir->info.stage == MESA_SHADER_FRAGMENT && + dev->dev.chip == AGX_CHIP_G17P) + NIR_PASS(_, nir, nir_shader_intrinsics_pass, hk_lower_multiview_fs_layer, + nir_metadata_control_flow, NULL); if (nir->info.stage == MESA_SHADER_TESS_EVAL) { NIR_PASS(_, nir, nir_lower_patch_vertices, diff --git a/src/compiler/nir/nir_intrinsics.py b/src/compiler/nir/nir_intrinsics.py index 62371220f020..3f4d3b58f45f 100644 --- a/src/compiler/nir/nir_intrinsics.py +++ b/src/compiler/nir/nir_intrinsics.py @@ -3445,6 +3445,11 @@ def store(name, srcs, indices=[], flags=[]): # a contiguous vec2-u32 descriptor; FLAGS selects the witnessed primitive leaf. intrinsic("allocate_mesh_uvb_agx", [1, 1]) intrinsic("store_mesh_uvb_agx", [2], indices=[FLAGS]) +# G17P exporter phase boundary. BASE is enum agx_mesh_export_fence: complete +# the index phase, or the preceding 32-vertex batch. Kept outside the export +# predicates, including zero/partial counts. Not an API memory barrier or a +# descriptor-lifetime annotation; no payload or SSA lifetime is introduced. +intrinsic("fence_mesh_uvb_agx", indices=[BASE]) # Object invocation0/1/2 publishes one child-grid dimension through UVSW. intrinsic("store_object_childcount_agx", [1])