Skip to content
Open
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
162 changes: 154 additions & 8 deletions crates/xcl/src/gpu/dxil/geometry_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,61 @@ fn branchy_program() -> (Vec<u8>, GsInterface) {
(code, g)
}

/// ES: nothing but the jump into the GS. GS: a fixed three-vertex strip whose last
/// emit + cut is folded into MSG_GS_DONE (`s_sendmsg(GS_DONE, EMIT_CUT)`, the final
/// message real GS code sends); vertex = (lane, k, primitive, 1.0), (k, lane, lane, lane).
fn done_emit_cut_program() -> (Vec<u8>, GsInterface) {
let e = |n: &str, enc: &str, f: &[(&str, u64)], lit: Option<u32>| encode(n, enc, f, lit).unwrap_or_else(|x| panic!("{n}: {x}"));
const V: u64 = 256;
let es = e("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 6)], None);
let mut gs: Vec<Vec<u8>> = Vec::new();
gs.push(e("S_BFE_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 3), ("SSRC1", 255)], Some(0x80008)));
gs.push(e("S_BFM_B64", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 0), ("SSRC1", 128)], None));
gs.push(e("V_MBCNT_LO_U32_B32", "ENC_VOP3", &[("VDST", 20), ("SRC0", 193), ("SRC1", 128)], None));
gs.push(e("V_MBCNT_HI_U32_B32", "ENC_VOP3", &[("VDST", 20), ("SRC0", 193), ("SRC1", V + 20)], None));
gs.push(e("V_MAD_U32_U24", "ENC_VOP3", &[("VDST", 50), ("SRC0", V + 20), ("SRC1", 255), ("SRC2", 2)], Some(160)));
gs.push(e("V_MOV_B32", "ENC_VOP1", &[("VDST", 52), ("SRC0", 242)], None)); // 1.0
for k in 0..3u64 {
gs.push(e("V_MOV_B32", "ENC_VOP1", &[("VDST", 51), ("SRC0", 128 + k)], None)); // k
gs.push(e("DS_WRITE2_B32", "ENC_DS", &[("ADDR", 50), ("DATA0", 20), ("DATA1", 51), ("OFFSET0", 0), ("OFFSET1", 1)], None));
gs.push(e("DS_WRITE2_B32", "ENC_DS", &[("ADDR", 50), ("DATA0", 2), ("DATA1", 52), ("OFFSET0", 2), ("OFFSET1", 3)], None));
gs.push(e("DS_WRITE2_B32", "ENC_DS", &[("ADDR", 50), ("DATA0", 51), ("DATA1", 20), ("OFFSET0", 4), ("OFFSET1", 5)], None));
gs.push(e("DS_WRITE2_B32", "ENC_DS", &[("ADDR", 50), ("DATA0", 20), ("DATA1", 20), ("OFFSET0", 6), ("OFFSET1", 7)], None));
let simm = if k == 2 { 0x33 } else { 0x22 }; // GS_DONE|EMIT_CUT last, GS|EMIT before
gs.push(e("S_SENDMSG", "ENC_SOPP", &[("SIMM16", simm)], None));
if k != 2 {
gs.push(e("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 50), ("SRC0", 160), ("VSRC1", 50)], None)); // + 32 bytes
}
}
gs.push(e("S_ENDPGM", "ENC_SOPP", &[], None));
let g = GsInterface {
es_inputs: vec![],
es_outputs: vec![sig("SV_Position", 1, 0, 3)],
outputs: vec![sig("SV_Position", 1, 0, 3), sig("TEXCOORD", 0, 1, 1)],
max_vertex_count: 5,
gs_start: Some(es.len() as u64),
};
let mut code = es;
code.extend(gs.concat());
(code, g)
}

fn opts(split: bool, windows: u32) -> KernelOptions {
KernelOptions { wave64: true, split, mem_windows: windows, sampler_base: 0 }
}

/// Both forms, ES alone, GS alone and four memory windows translate and validate.
#[test]
fn geometry_forms_validate() {
let (code, g) = branchy_program();
for split in [false, true] {
translate_geometry(&code, opts(split, 1), &g).unwrap_or_else(|e| panic!("split {split}: {e}"));
translate_geometry(&code, opts(split, 4), &g).unwrap_or_else(|e| panic!("split {split}, 4 windows: {e}"));
let es_alone = GsInterface { gs_start: None, ..g.clone() };
translate_geometry(&code[..4], opts(split, 1), &es_alone).unwrap_or_else(|e| panic!("ES alone: {e}"));
let gs_alone = GsInterface { gs_start: Some(0), ..g.clone() };
translate_geometry(&code[4..], opts(split, 1), &gs_alone).unwrap_or_else(|e| panic!("GS alone: {e}"));
for (code, g) in [branchy_program(), done_emit_cut_program()] {
for split in [false, true] {
translate_geometry(&code, opts(split, 1), &g).unwrap_or_else(|e| panic!("split {split}: {e}"));
translate_geometry(&code, opts(split, 4), &g).unwrap_or_else(|e| panic!("split {split}, 4 windows: {e}"));
let es_alone = GsInterface { gs_start: None, ..g.clone() };
translate_geometry(&code[..4], opts(split, 1), &es_alone).unwrap_or_else(|e| panic!("ES alone: {e}"));
let gs_alone = GsInterface { gs_start: Some(0), ..g.clone() };
translate_geometry(&code[4..], opts(split, 1), &gs_alone).unwrap_or_else(|e| panic!("GS alone: {e}"));
}
}
}

Expand Down Expand Up @@ -117,6 +157,63 @@ fn expected_records(g: &GsInterface, prims: usize) -> Vec<u32> {
out
}

/// A GS_DONE emit lands in the module: `gs_emit_cut` is the only writer of the
/// local strip-triangle array, so a GS whose only op rides MSG_GS_DONE must still
/// produce its store; the HLSL path emits the emit + cut code likewise (#2412).
#[test]
fn geometry_done_emit_cut_lowers() {
let e = |n: &str, enc: &str, f: &[(&str, u64)], lit: Option<u32>| encode(n, enc, f, lit).unwrap_or_else(|x| panic!("{n}: {x}"));
let es = e("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 6)], None);
let gs = [
e("S_SENDMSG", "ENC_SOPP", &[("SIMM16", 0x23)], None), // GS_DONE|EMIT
e("S_ENDPGM", "ENC_SOPP", &[], None),
];
let g = GsInterface {
es_inputs: vec![],
es_outputs: vec![sig("SV_Position", 1, 0, 3)],
outputs: vec![sig("SV_Position", 1, 0, 3)],
max_vertex_count: 1,
gs_start: Some(es.len() as u64),
};
let mut code = es;
code.extend(gs.concat());
for split in [false, true] {
let k = translate_geometry(&code, opts(split, 1), &g).expect("translate geometry");
let dis = super::validate::disassemble(&k.dxil).expect("disassemble geometry");
let stores = dis.lines().filter(|l| l.trim_start().starts_with("store i32")).count();
assert!(stores > 0, "split {split}: no strip-triangle store in:\n{dis}");
let t = hlsl_geometry(&code, g.clone(), split, 1).expect("HLSL geometry");
assert!(t.hlsl.contains("xcl_emitted++"), "split {split}: no emit in the HLSL:\n{}", t.hlsl);
}
}

/// The records of `done_emit_cut_program` for `prims` primitives: every lane emits a
/// (vb, vb+1, vb+2) triangle; the last emit + cut rides MSG_GS_DONE, not MSG_GS.
fn expected_done_emit_cut_records(g: &GsInterface, prims: usize) -> Vec<u32> {
let rec = g.record();
let (rd, vd, maxv) = (rec.record_dwords(), rec.vert_dwords(), rec.max_vertices as usize);
let groups = prims.div_ceil(21);
let mut out = vec![0u32; groups * rd];
for grp in 0..groups {
let r = &mut out[grp * rd..(grp + 1) * rd];
let (mut vb, mut tb) = (0usize, 0usize);
for lane in 0..21.min(prims - grp * 21) {
let p = grp * 21 + lane;
for k in 0..3u32 {
let v = MESH_HEADER + (vb + k as usize) * vd;
r[v..v + 8].copy_from_slice(&[lane as u32, k, p as u32, 0x3F80_0000, k, lane as u32, lane as u32, lane as u32]);
}
let b = vb as u32;
r[MESH_HEADER + maxv * vd + tb] = b | (b + 1) << 10 | (b + 2) << 20;
tb += 1;
vb += 3;
}
r[0] = vb as u32;
r[1] = tb as u32;
}
out
}

/// The direct translation's records (GPU, mesh pass-through included) for per-lane loops,
/// cuts and a partial last group, against a model of the program, in the split form and
/// (64-lane hardware) the native one; the HLSL path's records in the same forms must agree too.
Expand Down Expand Up @@ -167,6 +264,55 @@ fn gpu_geometry_direct_records() {
}
}

/// The records of `done_emit_cut_program` (GPU, mesh pass-through included): a
/// `s_sendmsg(GS_DONE, EMIT_CUT)` performs its emit and cut like MSG_GS would
/// (#2412), on the direct path and the HLSL path alike.
#[test]
fn gpu_geometry_done_emit_cut_records() {
if runner::adapter().is_err() || runner::wave64_programs_supported() != Ok(true) || dxc::location().is_err() || runner::mesh_shaders_supported() != Ok(true) {
runner::skip_test("no GPU / DXC / mesh shaders");
return;
}
let (code, g) = done_emit_cut_program();
let rec = g.record();
let msb = dxc::compile(&mesh_passthrough(&rec), "main", "ms_6_6", &[]).unwrap();
let ps_src = "struct V { float4 pos : SV_Position; nointerpolation uint4 p0 : TEXCOORD0; };\nfloat4 main(V v) : SV_Target0 { return float4(1.0, (float)v.p0.y, 0.0, 1.0); }\n";
let ps = dxc::compile(ps_src, "main", "ps_6_6", &[]).unwrap();
let prims = 25;
let mut io = vec![0u32; 128];
io[IO_GS_PRIMS] = prims as u32;
let mem = vec![0u32; 64];
let want = expected_done_emit_cut_records(&g, prims);
let check = |what: &str, got: &[u32]| {
let rd = rec.record_dwords();
let (vd, maxv) = (rec.vert_dwords(), rec.max_vertices as usize);
let diff: Vec<usize> = (0..want.len())
.filter(|&i| {
let (grp, o) = (i / rd, i % rd);
let r = &want[grp * rd..];
let used = o < MESH_HEADER || (o < MESH_HEADER + maxv * vd && (o - MESH_HEADER) / vd < r[0] as usize) || (o >= MESH_HEADER + maxv * vd && o - MESH_HEADER - maxv * vd < r[1] as usize);
used && want[i] != got[i]
})
.take(16)
.collect();
assert!(diff.is_empty(), "{what}: records differ at {diff:?}: got {:?}, want {:?}", diff.iter().map(|&i| got[i]).collect::<Vec<_>>(), diff.iter().map(|&i| want[i]).collect::<Vec<_>>());
};
let mut forms = vec![true];
if runner::wave64_supported() == Ok(true) {
forms.push(false);
}
for split in forms {
let direct = translate_geometry(&code, opts(split, 1), &g).unwrap().dxil;
let (_, got) = runner::mesh_draw(&direct, &msb, &ps, 2, rec.record_dwords(), &io, &mem, 32, [0.0; 4]).unwrap();
check(&format!("direct, split {split}"), &got);
let t = hlsl_geometry(&code, g.clone(), split, 1).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
let hlsl = dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}"));
let (_, got) = runner::mesh_draw(&hlsl, &msb, &ps, 2, rec.record_dwords(), &io, &mem, 32, [0.0; 4]).unwrap();
check(&format!("HLSL, split {split}"), &got);
}
}

/// The GS epilogue coalesces the 2-dword header store and each 4-dword vertex attribute store
/// into wide `rawBufferStore` calls (mask 3 and mask 15) instead of per-dword scalar stores.
#[test]
Expand Down
5 changes: 3 additions & 2 deletions crates/xcl/src/gpu/dxil/lower_geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ impl Lw {
match name {
"s_barrier" if self.split && gs_phase => Some(()),
"s_sendmsg" => {
// MSG_GS (2): op 1 cut, 2 emit, 3 emit + cut (stream 0); the rest: nothing
// MSG_GS (2) and MSG_GS_DONE (3): op 1 cut, 2 emit, 3 emit + cut
// (stream 0); the rest: nothing
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0);
let (msg, op, stream) = (simm & 0xF, (simm >> 4) & 3, (simm >> 8) & 3);
if msg == 2 && stream == 0 {
if (msg == 2 || msg == 3) && stream == 0 {
self.gs_emit_cut(op);
}
Some(())
Expand Down
4 changes: 2 additions & 2 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,10 +1318,10 @@ impl Em {
Flow::Next
}
"s_sendmsg" if matches!(self.opt.mode, Mode::Geometry(_)) => {
// MSG_GS (2): op 1 cut, 2 emit, 3 emit + cut (stream 0); GS_DONE (3) – nothing to do
// MSG_GS (2) and MSG_GS_DONE (3): op 1 cut, 2 emit, 3 emit + cut (stream 0)
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0);
let (msg, op, stream) = (simm & 0xF, (simm >> 4) & 3, (simm >> 8) & 3);
if let (2, 0, Mode::Geometry(g)) = (msg, stream, &self.opt.mode) {
if let (2 | 3, 0, Mode::Geometry(g)) = (msg, stream, &self.opt.mode) {
let maxt = g.max_vertex_count.saturating_sub(2).max(1);
let act = self.act();
if op & 2 != 0 {
Expand Down
Loading