diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index d8d9939b27..2ec1f45ca2 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -367,10 +367,12 @@ fn translate_on_this_thread( cs.threads } Entry::Mesh(ms) => { - let (tid, _) = lw.setup_lanes_compute([ms.threads, 1, 1], uses_lds); + // (whole console waves: a program can widen EXEC past its declared threads) + let launched = ms.launch_threads(o.wave64); + let (tid, _) = lw.setup_lanes_compute([launched, 1, 1], uses_lds); lw.safe_barrier_init(); - mesh_init(&mut lw, ms, tid, io); - [ms.threads, 1, 1] + mesh_init(&mut lw, ms, tid, io, launched); + [launched, 1, 1] } Entry::Geometry(g) => { let (tid, _) = lw.setup_lanes_compute([64, 1, 1], uses_lds); @@ -796,7 +798,8 @@ impl Lw { Entry::Vertex(_) | Entry::Pixel(_) => None, Entry::Kernel => None, Entry::Compute(cs) => Some(cs.threads.iter().product()), - Entry::Mesh(ms) => Some(ms.threads), + // (the launched waves: `launch_threads`) + Entry::Mesh(ms) => Some(ms.launch_threads(o.wave64)), Entry::Geometry(_) => Some(64), }; if let (Some(t), true) = (group_threads, split) { @@ -1275,10 +1278,11 @@ fn loop_passes(lw: &mut Lw, cfg: &Cfg, threads: u32) -> (Value, BTreeMap (Value, Value) { let zero = lw.c(0); let wave_threads: u32 = if lw.wave64 { 64 } else { 32 }; @@ -1366,6 +1370,48 @@ fn entry_exec(lw: &mut Lw, total: u32, tid: Value) -> (Value, Value) { (lo, hi) } +/// The mesh entry's EXEC at entry: only declared threads are active, even when the group +/// launches extra whole waves (static per wave in the split form, with no exchange). +fn mesh_entry_exec(lw: &mut Lw, total: u32, launched: u32, tid: Value) -> (Value, Value) { + let zero = lw.c(0); + let wave_threads: u32 = if lw.wave64 { 64 } else { 32 }; + let (lo, hi) = if total == launched && total % wave_threads == 0 { + let all = lw.c(u32::MAX); + (all, if lw.wave64 { all } else { zero }) + } else if lw.split { + let base = lw.andc(tid, !63); + let t = lw.c(total); + // (a wave past the declared threads: none) + let base = lw.umin(base, t); + let n = lw.sub(t, base); + let k64 = lw.c(64); + let n = lw.umin(n, k64); + let all = lw.c(u32::MAX); + let one = lw.c(1); + let n31 = lw.andc(n, 31); + let part = lw.bin(super::module::BinOp::Shl, one, n31); + let part = lw.sub(part, one); + let ge32 = lw.cmpc(Pred::UGe, n, 32); + let lo = lw.sel(ge32, all, part); + let k32 = lw.c(32); + let nh = lw.sub(n, k32); + let nh31 = lw.andc(nh, 31); + let parth = lw.bin(super::module::BinOp::Shl, one, nh31); + let parth = lw.sub(parth, one); + let ge64 = lw.cmpc(Pred::UGe, n, 64); + let gt32 = lw.cmpc(Pred::UGt, n, 32); + let hi = lw.sel(gt32, parth, zero); + let hi = lw.sel(ge64, all, hi); + (lo, hi) + } else { + let t = lw.cmpc(Pred::ULt, tid, total); + let w = lw.s.wave_ballot(t); + // HLSL ballot(): (m.x, m.y) — the second word is 0 in a 32-lane wave + (w[0], if lw.wave64 { w[1] } else { zero }) + }; + (lo, hi) +} + /// The geometry entry's registers (the HLSL geometry mode's prologue): the group's /// primitives from `io`, s2 = the GS output area, s3 = ES vertices | GS primitives << 8, /// SCC 0, EXEC = every lane; v0 / v1 = LDS offsets of each GS lane's triangle vertices, diff --git a/crates/xcl/src/gpu/dxil/lower.rs b/crates/xcl/src/gpu/dxil/lower.rs index 07220732d1..7ada822551 100644 --- a/crates/xcl/src/gpu/dxil/lower.rs +++ b/crates/xcl/src/gpu/dxil/lower.rs @@ -71,6 +71,8 @@ pub struct ComputeInit { /// primitive slots are in `res` (dwords), and the per-vertex outputs. pub struct MeshCtx { pub rec: Value, + /// this console wave's index in the mesh group + pub wave: Value, pub vtx: Value, pub prm: Value, /// this thread has a vertex slot / a primitive slot in the record (its index is below the diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index 74a7821d9f..0c72160860 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -2642,7 +2642,7 @@ impl Lw { // ------------------------------------------------------------ exports /// Mesh mode `s_sendmsg`: GS_ALLOC_REQ (M0 = primitives << 12 | vertices) into the - /// record header by lane 0 of each wave. + /// record header by lane 0 of wave 0, regardless of its EXEC mask. pub fn mesh_sendmsg(&mut self, d: &Decoded) -> LResult<()> { if Self::fv(d, "SIMM16") & 0xF != 9 { return Ok(()); @@ -2650,8 +2650,13 @@ impl Lw { let m0 = self.sreg(124); let rec = self.mesh.as_ref().expect("mesh").rec; let res = self.res.expect("res"); + // GS_ALLOC_REQ is one request per group, issued by its first console wave; the scalar + // instruction ignores EXEC, and later waves may widen it themselves. let lane = self.lane; - let first = self.cmpc(Pred::Eq, lane, 0); + let lane0 = self.cmpc(Pred::Eq, lane, 0); + let wave = self.mesh.as_ref().expect("mesh").wave; + let wave0 = self.cmpc(Pred::Eq, wave, 0); + let first = self.land(lane0, wave0); self.guarded(first, &mut |lw| { let v = lw.andc(m0, 0x3FF); let p = lw.lshrc(m0, 12); diff --git a/crates/xcl/src/gpu/dxil/robustness_tests.rs b/crates/xcl/src/gpu/dxil/robustness_tests.rs index 9a82e67cae..3946deb77e 100644 --- a/crates/xcl/src/gpu/dxil/robustness_tests.rs +++ b/crates/xcl/src/gpu/dxil/robustness_tests.rs @@ -135,6 +135,15 @@ fn a_group_barrier_of_several_hardware_waves_uses_safe_barrier_directly() { } let one_ms = kernel::translate_mesh(&code, o(true, false), &ms(64)).expect("a mesh pass of one 64-lane wave"); assert!(!super::validate::disassemble(&one_ms.dxil).expect("dis").contains("@xcl_bar")); + // a mesh group runs in whole console waves covering its threads, vertices and primitives + // (`MsInterface::launch_threads`): 32 threads exporting 64 vertices are two 32-lane waves, + // whose barrier is the safe one; with 32 vertices and primitives they are one wave + let one_wave = crate::gpu::recomp::MsInterface { max_vertices: 32, max_primitives: 32, ..ms(32) }; + let one = kernel::translate_mesh(&code, o(false, false), &one_wave).expect("a mesh pass of one 32-lane wave"); + assert!(!super::validate::disassemble(&one.dxil).expect("dis").contains("@xcl_bar")); + let two = kernel::translate_mesh(&code, o(false, false), &ms(32)).expect("32 threads with 64 vertices"); + let dis = super::validate::disassemble(&two.dxil).expect("dis"); + assert!(dis.contains("@xcl_bar"), "32 threads with 64 vertices are two 32-lane waves: {dis}"); // With LDS, safe barrier counters live inside xcl_lds[8189..8190] so total groupshared never // exceeds D3D12's 32 KiB limit (no extra @xcl_bar allocation). let with_lds = prog(&[ diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index f60f533602..d63f7ac906 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -642,7 +642,7 @@ impl Em { fn compute_or_mesh_hw_waves(&self) -> Option { let threads = match &self.opt.mode { Mode::Compute(cs) => cs.threads.iter().product::(), - Mode::Mesh(ms) => ms.threads, + Mode::Mesh(ms) => ms.launch_threads(self.opt.wave64), _ => return None, }; let hw_lanes = if self.x64() || !self.opt.wave64 { 32 } else { 64 }; @@ -1371,11 +1371,11 @@ impl Em { Flow::Next } "s_sendmsg" if matches!(self.opt.mode, Mode::Mesh(_)) => { - // GS_ALLOC_REQ: M0 = primitives << 12 | vertices (scalar: whatever EXEC is) + // GS_ALLOC_REQ stores one per-group header from wave 0, independent of EXEC. if rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) & 0xF == 9 { let m0 = self.sreg(124); self.line(&format!( - "if (lane_id == 0u) {{ res.Store(xcl_rec * 4u, {m0} & 0x3FFu); res.Store(xcl_rec * 4u + 4u, ({m0} >> 12) & 0x7FFu); }}" + "if (lane_id == 0u && xcl_wave == 0u) {{ res.Store(xcl_rec * 4u, {m0} & 0x3FFu); res.Store(xcl_rec * 4u + 4u, ({m0} >> 12) & 0x7FFu); }}" )); } Flow::Next @@ -4945,7 +4945,7 @@ impl Em { fn x64_threads(&self) -> u32 { match &self.opt.mode { Mode::Compute(cs) => cs.threads.iter().product(), - Mode::Mesh(ms) => ms.threads, + Mode::Mesh(ms) => ms.launch_threads(self.opt.wave64), _ => 64, } } @@ -5378,7 +5378,8 @@ static uint xcl_cap = 1u << 16; h.push_str("groupshared uint xcl_lds[8192]; // D3D12 limit: 32 KB (the console allows 64 KB)\n"); } self.safe_barrier_prelude(&mut h); - let t = ms.threads; + // whole console waves: a program can widen EXEC past its declared threads + let t = ms.launch_threads(self.opt.wave64); let _ = writeln!(h, "[WaveSize({native_lanes})]\n[numthreads({t}, 1, 1)]\nvoid main(uint gi : SV_GroupIndex, uint3 gid : SV_GroupID)\n{{"); if self.safe_barrier { h.push_str(" xcl_s_barrier_init(gi);\n"); @@ -5407,13 +5408,15 @@ static uint xcl_cap = 1u << 16; if sg.contains(&3) { h.push_str(" s3 = (s3 & 0xF0FFFFFFu) | (xcl_wave << 24);\n"); } - let _ = writeln!(h, " uint scc = 0u;\n {{ uint2 e0 = {}(true); exec_lo = e0.x; exec_hi = e0.y; }}", self.ballot_fn()); + // EXEC at entry: the declared threads (`true`: no lane is launched past them) + let entry = if ms.threads == ms.launch_threads(self.opt.wave64) { "true".to_string() } else { format!("xcl_slot < {}u", ms.threads) }; + let _ = writeln!(h, " uint scc = 0u;\n {{ uint2 e0 = {}({entry}); exec_lo = e0.x; exec_hi = e0.y; }}", self.ballot_fn()); if self.x64() && self.x64_spin() { h.push_str(if self.safe_barrier { " if (xcl_x_dead) { xcl_s_barrier_exit(); return; }\n" } else { " if (xcl_x_dead) return;\n" }); } for &v in &self.vgprs { let init = if v == 5 { - format!("(gid.x + io.Load({}u)) * {t}u + xcl_slot", super::IO_MESH_GROUP_BASE * 4) + format!("(gid.x + io.Load({}u)) * {}u + xcl_slot", super::IO_MESH_GROUP_BASE * 4, ms.group_stride(self.opt.wave64)) } else { "0u".into() }; diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 0194a6a57c..5e542a407d 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -364,6 +364,22 @@ pub struct MsInterface { pub const MESH_HEADER: usize = 4; impl MsInterface { + /// The threads a group of this program has on the console: whole waves (`wave64`: of 64 + /// lanes, else 32) covering its threads and the vertices and primitives it may export. The + /// console launches those; the program's EXEC at entry has its declared `threads` only, and + /// it can widen EXEC to export from the other lanes. + pub fn launch_threads(&self, wave64: bool) -> u32 { + let lanes = if wave64 { 64 } else { 32 }; + self.threads.max(self.max_vertices).max(self.max_primitives).max(1).next_multiple_of(lanes).min(1024) + } + + /// The console's v5 per group: each group gets a disjoint range covering every launched + /// lane. Output extents can exceed declared threads, and guest code can widen EXEC later, + /// so this applies to every program, not only ones whose translation proves EXEC widening. + pub fn group_stride(&self, wave64: bool) -> u32 { + self.launch_threads(wave64) + } + /// dwords per vertex: pos0 + every param (4 each, raw bits) pub fn vert_dwords(&self) -> usize { 4 * (1 + ranked(&self.outputs, true).len()) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 753d9590b4..5178d04d61 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7,6 +7,27 @@ use super::runner::{self, KernelState}; use super::{translate, Mode, Options}; use crate::gpu::rdna2::encode; +#[test] +fn mesh_group_stride_covers_output_extents_even_without_exec_widening() { + use super::MsInterface; + let ms = MsInterface { outputs: vec![], threads: 32, max_vertices: 64, max_primitives: 126 }; + // The launch includes whole waves even when guest code never widens EXEC. + assert_eq!(ms.group_stride(true), 128); + assert_eq!(ms.group_stride(false), 128); +} + +#[test] +fn mesh_group_stride_rounds_declared_threads_to_whole_waves() { + use super::MsInterface; + let full_wave = MsInterface { outputs: vec![], threads: 32, max_vertices: 32, max_primitives: 32 }; + assert_eq!(full_wave.group_stride(true), 64); + assert_eq!(full_wave.group_stride(false), 32); + + let ms = MsInterface { outputs: vec![], threads: 65, max_vertices: 1, max_primitives: 1 }; + assert_eq!(ms.group_stride(true), 128); + assert_eq!(ms.group_stride(false), 96); +} + /// A GPU and DXC for the instruction tests. Wave64 programs run natively where the adapter /// has 64-lane waves and in the split form (`runner::options`) where it has only 32 (an /// adapter with neither, such as WARP, skips). @@ -4499,7 +4520,9 @@ fn gpu_mesh_mode_two_passes() { rw_mask: 0xF, stream: 0, }; - let ms = MsInterface { outputs: vec![sig("SV_Position", 1, 0), sig("TEXCOORD", 0, 1)], threads: 128, max_vertices: 128, max_primitives: 128 }; + // 32 declared threads launch 128 lanes to cover the output extents. The second group's + // v5 base must be 128 (not the unrounded extent 126), in both compute backends. + let ms = MsInterface { outputs: vec![sig("SV_Position", 1, 0), sig("TEXCOORD", 0, 1)], threads: 32, max_vertices: 64, max_primitives: 126 }; let exp = |tgt: u64, en: u64, v: [u64; 4], done: u64| -> (&'static str, &'static str, Vec<(&'static str, u64)>, Option) { ("EXP", "ENC_EXP", vec![("TGT", tgt), ("EN", en), ("VSRC0", v[0]), ("VSRC1", v[1]), ("VSRC2", v[2]), ("VSRC3", v[3]), ("DONE", done)], None) }; @@ -4556,7 +4579,7 @@ fn gpu_mesh_mode_two_passes() { assert_eq!(f32::from_bits(r[b]), x, "group {g} vertex {v} pos.x"); assert_eq!([f32::from_bits(r[b + 4]), f32::from_bits(r[b + 5]), f32::from_bits(r[b + 7])], [g as f32, v as f32, 1.0], "group {g} vertex {v} param0"); } - assert_eq!(r[MESH_HEADER + 128 * vd], 1 << 10 | 2 << 20, "group {g} primitive"); + assert_eq!(r[MESH_HEADER + 64 * vd], 1 << 10 | 2 << 20, "group {g} primitive"); } // A slice starting at group 1 must produce that group's geometry in record 0. let mut sliced_sg = sg.clone(); @@ -4567,7 +4590,7 @@ fn gpu_mesh_mode_two_passes() { assert_eq!(&record[..2], &ref_group[..2]); let vertices = MESH_HEADER..MESH_HEADER + 3 * vd; assert_eq!(&record[vertices.clone()], &ref_group[vertices]); - assert_eq!(record[MESH_HEADER + 128 * vd], ref_group[MESH_HEADER + 128 * vd]); + assert_eq!(record[MESH_HEADER + 64 * vd], ref_group[MESH_HEADER + 64 * vd]); }; check_slice(&cs); // The direct backend rejects barriers across hardware waves. This fixture has no @@ -4581,26 +4604,170 @@ fn gpu_mesh_mode_two_passes() { }, &ms, ).unwrap(); + let (direct_px, direct_rec) = runner::mesh_draw(&direct.dxil, &msb, &ps, 2, rd, &sg, &mem, 32, [-1.0; 4]).unwrap(); + assert_eq!(direct_rec, rec, "direct DXIL group records use the same v5 step"); + assert_eq!(direct_px, px, "direct DXIL draws both groups at the same positions"); check_slice(&direct.dxil); + + // Exercise the changed v5 step on an actual two-group draw. The extent 126 launches + // 128 lanes; group 1 must start at v5=128 rather than 126 on both backends. + let stride_ms = MsInterface { threads: 32, max_vertices: 64, max_primitives: 126, ..ms.clone() }; + let stride_opt = on_gpu(Options { mode: Mode::Mesh(stride_ms.clone()), wave64: true, ..Default::default() }); + let stride_hlsl = dxc::compile(&translate(&code, &stride_opt).unwrap().hlsl, "main", "cs_6_6", &[]).unwrap(); + let stride_direct = crate::gpu::dxil::kernel::translate_mesh( + &prog(&direct_ops), + crate::gpu::dxil::kernel::KernelOptions { + wave64: stride_opt.wave64, split: stride_opt.split_wave64 || runner::split_wave64(), + mem_windows: stride_opt.mem_windows, sampler_base: stride_opt.static_sampler_base, + }, + &stride_ms, + ).unwrap(); + let stride_pass = dxc::compile(&mesh_passthrough(&stride_ms), "main", "ms_6_6", &[]).unwrap(); + let stride_rd = stride_ms.record_dwords(); + let (stride_px, stride_rec) = runner::mesh_draw(&stride_hlsl, &stride_pass, &ps, 2, stride_rd, &sg, &mem, 32, [-1.0; 4]).unwrap(); + let (direct_stride_px, direct_stride_rec) = runner::mesh_draw(&stride_direct.dxil, &stride_pass, &ps, 2, stride_rd, &sg, &mem, 32, [-1.0; 4]).unwrap(); + assert_eq!(direct_stride_rec, stride_rec, "both backends use the rounded v5 step for group 1"); + assert_eq!(direct_stride_px, stride_px, "both backends draw group 1 at the same position"); + assert_eq!(f32::from_bits(stride_rec[stride_rd + MESH_HEADER]), 0.0, "group 1 left vertex uses v5 >> 7"); + assert_eq!(stride_px[29 * 32 + 19][0], 1.0, "group 1 triangle is drawn at its v5-derived position"); + let at = |x: usize, y: usize| px[y * 32 + x]; assert_eq!((at(3, 29)[0], at(3, 29)[3]), (0.0, 1.0), "group 0 triangle (left)"); assert_eq!((at(19, 29)[0], at(19, 29)[3]), (1.0, 1.0), "group 1 triangle (right)"); assert_eq!(at(29, 3), [-1.0; 4], "outside both triangles"); - // the same records through a pass-through for a pixel shader with 16 inputs: over the - // mesh output limit (17 · 16 B · 128 vertices), so 4 mesh groups per record, each a run - // of triangles with their own vertex copies; inputs the program does not write read 0 + // Keep the split pass-through test at the original 128-vertex shape: it exercises four + // compact mesh groups per record, independently of the rounded-v5 fixture above. + let split_ms = MsInterface { threads: 128, max_vertices: 128, max_primitives: 128, ..ms.clone() }; let ins: Vec = (0..16).map(|k| crate::gpu::dxbc::SigElement { index: k, register: k + 1, ..sig("TEXCOORD", 0, 0) }).collect(); - let split = super::emit::passthrough_split(&ms, super::emit::passthrough_elements(&ms, Some(&ins))); + let split = super::emit::passthrough_split(&split_ms, super::emit::passthrough_elements(&split_ms, Some(&ins))); assert_eq!(split, 4); - let pass16 = super::emit::mesh_passthrough_for(&ms, Some(&ins)); + let pass16 = super::emit::mesh_passthrough_for(&split_ms, Some(&ins)); let msb16 = dxc::compile(&pass16, "main", "ms_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{pass16}")); let fields: String = (0..16).map(|k| format!(" float4 t{k} : TEXCOORD{k};")).collect(); let ps16 = dxc::compile(&format!("struct V {{ float4 pos : SV_Position;{fields} }};\nfloat4 main(V v) : SV_Target0 {{ return v.t0 + v.t15; }}\n"), "main", "ps_6_6", &[]).unwrap(); - let (px16, _) = runner::mesh_draw_split(&cs, &msb16, &ps16, 2, split, ms.record_dwords(), &sg, &mem, 32, [-1.0; 4]).unwrap(); + let split_opt = on_gpu(Options { mode: Mode::Mesh(split_ms.clone()), wave64: true, ..Default::default() }); + let split_cs = dxc::compile(&translate(&code, &split_opt).unwrap().hlsl, "main", "cs_6_6", &[]).unwrap(); + let (px16, _) = runner::mesh_draw_split(&split_cs, &msb16, &ps16, 2, split, split_ms.record_dwords(), &sg, &mem, 32, [-1.0; 4]).unwrap(); assert!(px16 == px, "split pass-through draws the same pixels"); } +/// An extra console wave has an empty entry EXEC but still executes scalar instructions. Its +/// GS_ALLOC_REQ must not write the record header; otherwise a wave with no active lanes can +/// overwrite the counts selected by the declared threads. +#[test] +fn gpu_mesh_alloc_req_uses_wave0_independent_of_exec() { + if !gpu_ready() || !mesh_ready() { + return; + } + use super::{dxc, mesh_passthrough, MsInterface}; + let sig = |name: &str, sv: u32, reg: u32| crate::gpu::dxbc::SigElement { + name: name.into(), index: 0, system_value: sv, component_type: 3, register: reg, + mask: 0xF, rw_mask: 0xF, stream: 0, + }; + let ms = MsInterface { outputs: vec![sig("SV_Position", 1, 0)], threads: 32, max_vertices: 64, max_primitives: 126 }; + // Order the stores deterministically: wave 0 narrows EXEC to zero and stores the good + // header before the group barrier; wave 1 widens EXEC and stores its different header + // after it. The per-group wave-0 rule must handle both cases, regardless of EXEC. + let refs = [ + ("S_BFE_U32", "ENC_SOP2", vec![("SDST", 0), ("SSRC0", 3), ("SSRC1", 255)], Some(0x40018)), // wave index + ("S_MOV_B32", "ENC_SOP1", vec![("SDST", 124), ("SSRC0", 255)], Some(3 << 12 | 9)), + ("S_CMP_EQ_U32", "ENC_SOPC", vec![("SSRC0", 0), ("SSRC1", 128)], None), // wave 0 + ("S_CBRANCH_SCC0", "ENC_SOPP", vec![("SIMM16", 4)], None), // wave 1 branches to the barrier + ("S_MOV_B64", "ENC_SOP1", vec![("SDST", 126), ("SSRC0", 128)], None), // wave 0 narrows EXEC + ("S_MOV_B32", "ENC_SOP1", vec![("SDST", 124), ("SSRC0", 255)], Some(2 << 12 | 5)), + ("S_SENDMSG", "ENC_SOPP", vec![("SIMM16", 9)], None), + ("S_BARRIER", "ENC_SOPP", vec![], None), + ("S_CMP_LG_U32", "ENC_SOPC", vec![("SSRC0", 0), ("SSRC1", 128)], None), // wave 1 + ("S_CBRANCH_SCC0", "ENC_SOPP", vec![("SIMM16", 2)], None), // wave 0 skips the widen and send + ("S_MOV_B64", "ENC_SOP1", vec![("SDST", 126), ("SSRC0", 193)], None), // wave 1 widens EXEC + ("S_SENDMSG", "ENC_SOPP", vec![("SIMM16", 9)], None), + ]; + let refs: Vec<_> = refs.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect(); + let code = prog(&refs); + let opt = on_gpu(Options { mode: Mode::Mesh(ms.clone()), wave64: true, ..Default::default() }); + let translated = translate(&code, &opt).unwrap(); + assert!(translated.complete(), "{:?}", translated.unsupported_why); + let pass = dxc::compile(&mesh_passthrough(&ms), "main", "ms_6_6", &[]).unwrap(); + let ps = dxc::compile("float4 main() : SV_Target0 { return 1.0; }", "main", "ps_6_6", &[]).unwrap(); + let hlsl = dxc::compile(&translated.hlsl, "main", "cs_6_6", &[]).unwrap(); + let wave0_sendmsg = "if (lane_id == 0u && xcl_wave == 0u)"; + assert!(translated.hlsl.contains(wave0_sendmsg), "{}", translated.hlsl); + let unguarded = translated.hlsl.replace(wave0_sendmsg, "if (lane_id == 0u)"); + assert_ne!(unguarded, translated.hlsl, "negative control must remove the wave-0 guard"); + let unguarded = dxc::compile(&unguarded, "main", "cs_6_6", &[]).unwrap(); + let (_, unguarded_rec) = runner::mesh_draw(&unguarded, &pass, &ps, 1, ms.record_dwords(), &[], &[], 8, [0.0; 4]).unwrap(); + assert_eq!(&unguarded_rec[..2], &[9, 3], "negative control must expose wave 1's ordered store"); + let (_, hlsl_rec) = runner::mesh_draw(&hlsl, &pass, &ps, 1, ms.record_dwords(), &[], &[], 8, [0.0; 4]).unwrap(); + assert_eq!(&hlsl_rec[..2], &[5, 2], "GS_ALLOC_REQ is written by wave 0 even with EXEC empty"); + + let direct = crate::gpu::dxil::kernel::translate_mesh( + &code, + crate::gpu::dxil::kernel::KernelOptions { + wave64: true, split: opt.split_wave64 || runner::split_wave64(), + mem_windows: opt.mem_windows, sampler_base: opt.static_sampler_base, + }, + &ms, + ).unwrap_or_else(|e| panic!("direct DXIL mesh path refused fixture: {e}")); + let (_, direct_rec) = runner::mesh_draw(&direct.dxil, &pass, &ps, 1, ms.record_dwords(), &[], &[], 8, [0.0; 4]).unwrap(); + assert_eq!(&direct_rec[..2], &[5, 2], "direct-DXIL GS_ALLOC_REQ follows the wave-0 rule"); +} + +#[test] +fn gpu_mesh_two_pass_widens_exec_past_declared_threads() { + if !gpu_ready() || !mesh_ready() { + return; + } + use super::{dxc, mesh_passthrough, MsInterface}; + use crate::gpu::dxil::kernel::{translate_mesh, KernelOptions}; + let position = crate::gpu::dxbc::SigElement { + name: "SV_Position".into(), index: 0, system_value: 1, component_type: 3, register: 0, mask: 0xF, rw_mask: 0xF, stream: 0, + }; + let ms = MsInterface { outputs: vec![position], threads: 32, max_vertices: 36, max_primitives: 1 }; + // `widen`: EXEC to every lane before the vertex exports (else lanes 32..34 stay masked, as + // the console starts them past the declared threads) + let code = |widen: bool| { + let head: [(&str, &str, &[(&str, u64)], Option); 2] = [ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 255)], Some(1 << 12 | 36)), + ("S_SENDMSG", "ENC_SOPP", &[("SIMM16", 9)], None), + ]; + let open: [(&str, &str, &[(&str, u64)], Option); 1] = [("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)], None)]; + let tail: [(&str, &str, &[(&str, u64)], Option); 8] = [ + ("V_SUBREV_NC_U32", "ENC_VOP2", &[("VDST", 0), ("SRC0", 160), ("VSRC1", 5)], None), + ("V_CMPX_GT_U32", "ENC_VOPC", &[("SRC0", 131), ("VSRC1", 0)], None), // lanes 32..34 + ("BUFFER_LOAD_DWORDX4", "ENC_MUBUF", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("IDXEN", 1)], None), + ("EXP", "ENC_EXP", &[("TGT", 12), ("EN", 15), ("VSRC0", 10), ("VSRC1", 11), ("VSRC2", 12), ("VSRC3", 13), ("DONE", 1)], None), + ("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)], None), + ("V_CMPX_EQ_U32", "ENC_VOPC", &[("SRC0", 128), ("VSRC1", 5)], None), // lane 0 + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 20), ("SRC0", 255)], Some(32 | 33 << 10 | 34 << 20)), + ("EXP", "ENC_EXP", &[("TGT", 20), ("EN", 1), ("VSRC0", 20), ("DONE", 1)], None), + ]; + let list: Vec<_> = head.iter().chain(&open[..widen as usize]).chain(tail.iter()).cloned().collect(); + prog(&list) + }; + let opt = on_gpu(Options { mode: Mode::Mesh(ms.clone()), wave64: true, ..Default::default() }); + let pass = dxc::compile(&mesh_passthrough(&ms), "main", "ms_6_6", &[]).unwrap(); + let ps = dxc::compile("float4 main() : SV_Target0 { return 1; }", "main", "ps_6_6", &[]).unwrap(); + let mut io = [0; 128]; + io[16..20].copy_from_slice(&[0x100, 16 << 16, 3, 0]); + let mut mem = [0; 128]; + mem[64..76].copy_from_slice(&[-1.0, -1.0, 0.0, 1.0, -1.0, 1.0, 0.0, 1.0, 0.0, -1.0, 0.0, 1.0].map(f)); + // lit pixels of (HLSL, direct DXIL) + let lit = |widen: bool| { + let code = code(widen); + let hlsl = dxc::compile(&translate(&code, &opt).unwrap().hlsl, "main", "cs_6_6", &[]).unwrap(); + let direct = translate_mesh(&code, KernelOptions { wave64: true, split: opt.split_wave64, mem_windows: opt.mem_windows, sampler_base: opt.static_sampler_base }, &ms).unwrap(); + [&hlsl, &direct.dxil].map(|shader| { + let (pixels, _) = runner::mesh_draw(shader, &pass, &ps, 1, ms.record_dwords(), &io, &mem, 32, [0.0; 4]).unwrap(); + pixels.into_iter().filter(|p| p[0] == 1.0).count() + }) + }; + let (widened, masked) = (lit(true), lit(false)); + assert!(widened[0] > 0 && widened[1] > 0, "EXEC widened: lit pixels (HLSL, direct DXIL): {widened:?}"); + // the lanes past the declared threads start inactive: their vertices are not exported + assert_eq!(masked, [0, 0], "EXEC as at entry: lit pixels (HLSL, direct DXIL)"); +} + /// A mesh program's thread past its record's vertices or primitives exports nothing. A record /// has `max_vertices` vertex slots and `max_primitives` primitive slots, a group can have more /// threads, and the console's hardware drops what goes past the room the program asked for. @@ -11921,13 +12088,14 @@ fn programs_left_to_the_hlsl_path_for_their_barrier_translate_and_compile() { let Some(sh) = crate::gpu::xbox::parse(&blob) else { continue }; for f in &sh.fssm { let Some(mode) = super::interface(&blob, &sh, f) else { continue }; + let Some(p) = f.programs.first() else { continue }; + let code = &f.data[p.offset..p.offset + p.len]; + let wave64 = super::fssm_wave64(f, code); let threads = match &mode { Mode::Compute(cs) => cs.threads.iter().product::(), - Mode::Mesh(ms) => ms.threads, + Mode::Mesh(ms) => ms.launch_threads(wave64), _ => continue, }; - let Some(p) = f.programs.first() else { continue }; - let code = &f.data[p.offset..p.offset + p.len]; let has_barrier = { let mut off = 0; let mut found = false; @@ -11942,7 +12110,6 @@ fn programs_left_to_the_hlsl_path_for_their_barrier_translate_and_compile() { continue; } n.fetch_add(1, Ordering::Relaxed); - let wave64 = super::fssm_wave64(f, code); let o = crate::gpu::dxil::kernel::KernelOptions { wave64, split: wave64, mem_windows: windows, sampler_base: 0 }; let direct = match &mode { Mode::Compute(cs) => crate::gpu::dxil::kernel::translate_compute(code, o, cs).map(|_| ()), diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 9945b2849d..f84d85dc22 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -600,7 +600,7 @@ impl RecompAgg { let survey_cs = match &mode { recomp::Mode::Compute(cs) => Some(("CS", cs.clone())), // the mesh pass is a compute pass of the NGG program: its threads in a 1-D group - recomp::Mode::Mesh(ms) => Some(("MS", recomp::CsInterface { threads: [ms.threads, 1, 1], tid_components: 1, ..Default::default() })), + recomp::Mode::Mesh(ms) => Some(("MS", recomp::CsInterface { threads: [ms.launch_threads(wave64), 1, 1], tid_components: 1, ..Default::default() })), _ => None, }; if let Some((st, cs)) = survey_cs {