From 004b3db378b5a0f83b93d074caf2ac50371974fd Mon Sep 17 00:00:00 2001 From: wrxqed Date: Sun, 22 Nov 2026 22:17:36 +0300 Subject: [PATCH 1/2] recomp: support wave32 split execution for console mesh shaders --- crates/xcl/src/gpu/recomp/emit.rs | 10 ++++++++-- crates/xcl/src/gpu/recomp/mod.rs | 4 +++- crates/xcl/src/gpu/recomp/pso.rs | 8 ++++---- crates/xcl/src/gpu/recomp/tests.rs | 18 ++++++++++++++++++ crates/xcl/src/hle/d3d12x/real.rs | 2 +- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 7b96f2b..3236e30 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3615,13 +3615,19 @@ else {{ mem.{neg}({t}_a, {v}, {t}_old); }}" h.push_str("}\n"); } Mode::Mesh(ms) => { + let split_wave32 = self.opt.wave64 && self.opt.mesh_wave32; + let native_lanes = if !self.opt.wave64 || split_wave32 { 32 } else { 64 }; let lanes = if self.opt.wave64 { 64 } else { 32 }; if self.uses_lds { h.push_str("groupshared uint xcl_lds[8192]; // D3D12 limit: 32 KB (the console allows 64 KB)\n"); } let t = ms.threads; - let _ = writeln!(h, "[WaveSize({lanes})]\n[numthreads({t}, 1, 1)]\nvoid main(uint gi : SV_GroupIndex, uint3 gid : SV_GroupID)\n{{"); - h.push_str(" lane_id = WaveGetLaneIndex();\n"); + let _ = writeln!(h, "[WaveSize({native_lanes})]\n[numthreads({t}, 1, 1)]\nvoid main(uint gi : SV_GroupIndex, uint3 gid : SV_GroupID)\n{{"); + if split_wave32 { + h.push_str(" xcl_half = (gi >> 5) & 1u;\n xcl_logical_wave64 = true;\n lane_id = WaveGetLaneIndex() + 32u * xcl_half;\n"); + } else { + h.push_str(" lane_id = WaveGetLaneIndex();\n"); + } // thread index in the group as the console numbers it: wave ยท lanes + lane let _ = writeln!(h, " uint xcl_wave = gi / {lanes}u;\n uint xcl_slot = xcl_wave * {lanes}u + lane_id;"); let _ = writeln!(h, " uint xcl_rec = gid.x * {}u;", ms.record_dwords()); diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 77d6d87..2adb651 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -467,6 +467,8 @@ pub struct Options { pub hw_wave: u32, /// geometry mode: emulate one logical wave64 with two native wave32 phases pub geometry_wave32: bool, + /// mesh mode: run a logical wave64 mesh program as two native wave32 halves + pub mesh_wave32: bool, /// first register (space 1) of the program's static samplers (another stage of the same /// pipeline may come first) pub static_sampler_base: u32, @@ -481,7 +483,7 @@ pub struct Options { impl Default for Options { fn default() -> Self { - Options { mode: Mode::Kernel, wave64: true, hw_wave: 0, geometry_wave32: false, static_sampler_base: 0, mem_windows: 1, structured: true } + Options { mode: Mode::Kernel, wave64: true, hw_wave: 0, geometry_wave32: false, mesh_wave32: false, static_sampler_base: 0, mem_windows: 1, structured: true } } } diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index b889db5..5432885 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -179,8 +179,8 @@ pub struct TwoPass { pub samplers: Vec, } -fn two_pass(code: &[u8], mode: Mode, wave64: bool, geometry_wave32: bool, record: super::MsInterface, ps: Option<&[u8]>) -> Result { - let t = translate(code, &Options { mode, wave64, geometry_wave32, ..Default::default() })?; +fn two_pass(code: &[u8], mode: Mode, wave64: bool, geometry_wave32: bool, mesh_wave32: bool, record: super::MsInterface, ps: Option<&[u8]>) -> Result { + let t = translate(code, &Options { mode, wave64, geometry_wave32, mesh_wave32, ..Default::default() })?; finish_two_pass(t, record, ps) } @@ -198,7 +198,7 @@ fn finish_two_pass(t: super::Translation, record: super::MsInterface, ps: Option /// The mesh-shader (NGG) program of a console MS container; the pass-through carries what /// the pixel shader `ps` (its container: console or PC) reads. -pub fn console_mesh(blob: &[u8], ps: Option<&[u8]>) -> Result { +pub fn console_mesh(blob: &[u8], ps: Option<&[u8]>, mesh_wave32: bool) -> Result { let sh = xbox::parse(blob).ok_or("not a console shader container")?; let f = sh.fssm.iter().find(|f| f.stages.first().is_some_and(|s| s.hw == 7)).ok_or("no NGG hardware variant")?; let mode = interface(blob, &sh, f).ok_or("unknown MS interface (e.g. lines / points)")?; @@ -206,7 +206,7 @@ pub fn console_mesh(blob: &[u8], ps: Option<&[u8]>) -> Result { let record = ms.clone(); let p = f.programs.first().ok_or("FSSM without a program")?; let code = &f.data[p.offset..p.offset + p.len]; - two_pass(code, mode.clone(), super::detect_wave64(code), false, record, ps) + two_pass(code, mode.clone(), super::detect_wave64(code), false, mesh_wave32, record, ps) } /// Whether the adapter's supported wave range requires split geometry execution. diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 6ca697c..f0032a0 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -3630,3 +3630,21 @@ fn far_jumps_are_direct_branches() { assert_eq!((r.vgpr[l][1], r.vgpr[l][2]), (1, 9), "lane {l}"); } } + +#[test] +fn mesh_wave32_emits_wave32_logical_wave64() { + let code = 0xBF81_0000u32.to_le_bytes(); // s_endpgm + let ms = super::MsInterface { threads: 128, max_vertices: 64, max_primitives: 64, outputs: vec![] }; + let native = translate(&code, &Options { mode: Mode::Mesh(ms.clone()), wave64: true, mesh_wave32: false, ..Default::default() }).unwrap(); + assert!(native.hlsl.contains("[WaveSize(64)]")); + assert!(native.hlsl.contains("[numthreads(128, 1, 1)]")); + assert!(!native.hlsl.contains("xcl_logical_wave64 = true")); + + let split = translate(&code, &Options { mode: Mode::Mesh(ms), wave64: true, mesh_wave32: true, ..Default::default() }).unwrap(); + assert!(split.hlsl.contains("[WaveSize(32)]")); + assert!(split.hlsl.contains("[numthreads(128, 1, 1)]")); + assert!(split.hlsl.contains("xcl_half = (gi >> 5) & 1u;")); + assert!(split.hlsl.contains("xcl_logical_wave64 = true;")); + assert!(split.hlsl.contains("lane_id = WaveGetLaneIndex() + 32u * xcl_half;")); + assert!(split.complete(), "{:?}", split.unsupported_why); +} diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index ddb4275..730aeae 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2879,7 +2879,7 @@ unsafe fn translated_mesh_stream(this: *mut c_void, desc: *const c_void, riid: * return Some(E_INVALIDARG); } let ps = subs.iter().find(|s| s.0 == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS.0 as u32).and_then(|s| payload_shader(s.1)); - let (tp, (ps, ps_samplers)) = match (console_mesh(ms, ps), two_pass_ps(ps)) { + let (tp, (ps, ps_samplers)) = match (console_mesh(ms, ps, geometry_wave32_required(this)), two_pass_ps(ps)) { (Ok(tp), Ok(ps)) => (tp, ps), (Err(e), _) => { note_translation_error(format!("MS: {e}")); From 798ff0dbde42c3f21c455f19d6f2a02fa2fe4ff3 Mon Sep 17 00:00:00 2001 From: Graham Calihan Date: Sun, 22 Nov 2026 15:37:06 -0500 Subject: [PATCH 2/2] recomp: refuse split wave32 mesh programs with cross-half wave64 semantics A wave64 NGG program split into wave32 halves now runs the geometry hazard check first, as split geometry does, instead of building a pipeline that computes wrong results or hangs. --- crates/xcl/src/gpu/recomp/mod.rs | 16 ++++++++++++++-- crates/xcl/src/gpu/recomp/pso.rs | 7 +------ crates/xcl/src/gpu/recomp/tests.rs | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 2adb651..1708451 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -1076,8 +1076,8 @@ pub fn wave_hazards(code: &[u8]) -> BTreeSet<&'static str> { } /// Operations that cannot be split into two independent native wave32 halves while -/// preserving one logical wave64. Split geometry translation rejects these rather than -/// silently changing cross-half lane or mask semantics. +/// preserving one logical wave64. Split geometry and split mesh translation reject these +/// rather than silently changing cross-half lane or mask semantics. pub fn geometry_wave32_hazards(code: &[u8]) -> BTreeSet<&'static str> { let mut out = BTreeSet::new(); let mut mask_regs = BTreeSet::from([106u64, 107, 126, 127]); // VCC and EXEC @@ -1161,6 +1161,18 @@ pub fn translate_geometry(code: &[u8], interface: GsInterface, geometry_wave32: translate(code, &Options { mode: Mode::Geometry(interface), wave64: true, geometry_wave32, ..Default::default() }) } +/// Translate an NGG mesh program; a wave64 program split into wave32 halves is refused +/// when it needs cross-half semantics, as for split geometry. +pub fn translate_mesh(code: &[u8], ms: MsInterface, wave64: bool, mesh_wave32: bool) -> Result { + if wave64 && mesh_wave32 { + let hazards = geometry_wave32_hazards(code); + if !hazards.is_empty() { + return Err(format!("mesh program requires unsupported cross-half wave64 semantics: {hazards:?}")); + } + } + translate(code, &Options { mode: Mode::Mesh(ms), wave64, mesh_wave32, ..Default::default() }) +} + /// Wave size the program was compiled for: 64-bit EXEC/VCC writes mean wave64. pub fn detect_wave64(code: &[u8]) -> bool { let mut off = 0; diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index 5432885..15ac366 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -179,11 +179,6 @@ pub struct TwoPass { pub samplers: Vec, } -fn two_pass(code: &[u8], mode: Mode, wave64: bool, geometry_wave32: bool, mesh_wave32: bool, record: super::MsInterface, ps: Option<&[u8]>) -> Result { - let t = translate(code, &Options { mode, wave64, geometry_wave32, mesh_wave32, ..Default::default() })?; - finish_two_pass(t, record, ps) -} - fn finish_two_pass(t: super::Translation, record: super::MsInterface, ps: Option<&[u8]>) -> Result { if !t.complete() { let missing: Vec<&String> = t.unsupported.keys().collect(); @@ -206,7 +201,7 @@ pub fn console_mesh(blob: &[u8], ps: Option<&[u8]>, mesh_wave32: bool) -> Result let record = ms.clone(); let p = f.programs.first().ok_or("FSSM without a program")?; let code = &f.data[p.offset..p.offset + p.len]; - two_pass(code, mode.clone(), super::detect_wave64(code), false, mesh_wave32, record, ps) + finish_two_pass(super::translate_mesh(code, ms.clone(), super::detect_wave64(code), mesh_wave32)?, record, ps) } /// Whether the adapter's supported wave range requires split geometry execution. diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index f0032a0..9c57f88 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -3648,3 +3648,22 @@ fn mesh_wave32_emits_wave32_logical_wave64() { assert!(split.hlsl.contains("lane_id = WaveGetLaneIndex() + 32u * xcl_half;")); assert!(split.complete(), "{:?}", split.unsupported_why); } + +#[test] +fn mesh_wave32_rejects_cross_half_operations() { + let ms = super::MsInterface { threads: 128, max_vertices: 64, max_primitives: 64, outputs: vec![] }; + let lane = prog(&[("V_READFIRSTLANE_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", V)], None)]); + let branch = prog(&[("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 0)], None)]); + for (code, hazard) in [(&lane, "cross-half lane exchange"), (&branch, "branch on a whole-wave mask")] { + let e = super::translate_mesh(code, ms.clone(), true, true).err().expect("split mesh must refuse"); + assert!(e.contains("mesh program requires unsupported cross-half wave64 semantics") && e.contains(hazard), "{e}"); + // native wave64 and wave32 programs are not split, so nothing is refused + assert!(super::translate_mesh(code, ms.clone(), true, false).is_ok()); + assert!(super::translate_mesh(code, ms.clone(), false, true).is_ok()); + } + + let plain = prog(&[("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V)], None)]); + let split = super::translate_mesh(&plain, ms, true, true).unwrap(); + assert!(split.hlsl.contains("xcl_logical_wave64 = true;")); + assert!(split.complete(), "{:?}", split.unsupported_why); +}