Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 99 additions & 1 deletion src/asahi/compiler/agx3_pack_binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions src/asahi/compiler/agx_mesh_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
49 changes: 43 additions & 6 deletions src/asahi/compiler/agx_nir_gen1_mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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] ||
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -1029,6 +1062,10 @@ agx_nir_lower_gen1_mesh(nir_shader *s, const struct agx_m5_mesh_varyings *varyin
}
}
for(unsigned vertex=0;vertex<s->info.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;plane<layout.planes;plane++) {
const unsigned address=layout.position+layout.plane_stride*plane+4*vertex;
Expand Down
6 changes: 6 additions & 0 deletions src/asahi/compiler/agx_nir_m5_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ struct agx_m5_mesh_varyings {
/* G17P and M5 lines: root byte offset of the signed line row bias
* (0: none), see agx_nir_lower_gen1_mesh and m5_mesh_line_bias. */
uint16_t line_bias_root_B;
/* G17P: the layer is a per-vertex VARYING_SLOT_LAYER output that every
* vertex of a workgroup writes with the same value (hk's multiview
* lowering writes the view index). It goes straight to each vertex's
* layer word, so primitives keep their shared vertices and no primitive
* record is written. lv is set; no per-primitive layer or viewport. */
bool vertex_layer;
};


Expand Down
2 changes: 2 additions & 0 deletions src/asahi/compiler/agx_opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ def subgroup_op(name, opc):
can_reorder=False, schedule_class="store", agx3_mnemonic="allocate_uvb")
op("store_uvb_m5", _, dests=0, srcs=1, imms=[IMM], can_eliminate=False,
can_reorder=False, schedule_class="store", agx3_mnemonic="uvst")
op("fence_uvb_gen1", _, dests=0, srcs=0, imms=[IMM], can_eliminate=False,
can_reorder=False, schedule_class="barrier")

op("store_object_childcount_m5", _, dests=0, srcs=1, can_eliminate=False,
can_reorder=False, schedule_class="store", agx3_mnemonic="uvsw")
Expand Down
Loading