Merge pull request #39 from wrxqed/fix/mesh-wave32

recomp: support wave32 split execution for console mesh shaders – with
grahamster2's check that refuses split mesh programs needing cross-half
wave64 semantics (as split geometry does)
This commit is contained in:
ivall
2026-11-22 22:32:04 +01:00
5 changed files with 65 additions and 13 deletions
+8 -2
View File
@@ -3606,13 +3606,19 @@ impl Em {
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());
+17 -3
View File
@@ -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 }
}
}
@@ -1074,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
@@ -1169,6 +1171,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<Translation, String> {
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;
+2 -7
View File
@@ -179,11 +179,6 @@ pub struct TwoPass {
pub samplers: Vec<StaticSampler>,
}
fn two_pass(code: &[u8], mode: Mode, wave64: bool, geometry_wave32: bool, record: super::MsInterface, ps: Option<&[u8]>) -> Result<TwoPass, String> {
let t = translate(code, &Options { mode, wave64, geometry_wave32, ..Default::default() })?;
finish_two_pass(t, record, ps)
}
fn finish_two_pass(t: super::Translation, record: super::MsInterface, ps: Option<&[u8]>) -> Result<TwoPass, String> {
if !t.complete() {
let missing: Vec<&String> = t.unsupported.keys().collect();
@@ -198,7 +193,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<TwoPass, String> {
pub fn console_mesh(blob: &[u8], ps: Option<&[u8]>, mesh_wave32: bool) -> Result<TwoPass, String> {
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 +201,7 @@ pub fn console_mesh(blob: &[u8], ps: Option<&[u8]>) -> Result<TwoPass, String> {
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)
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.
+37
View File
@@ -4044,3 +4044,40 @@ 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);
}
#[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);
}
+1 -1
View File
@@ -3102,7 +3102,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}"));