From 3304bb1105c9ab056e8fe0edecc1e168257d860b Mon Sep 17 00:00:00 2001 From: Foniass <107039406+Foniass@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:31:44 +0100 Subject: [PATCH] MS and GS programs translate with the title's memory windows translate_mesh / translate_geometry used one memory window while VS / PS / CS get the title's four (pso::mem_windows): an NGG or legacy GS program reading title memory above the first window (a V#, an SGPR pair or a 64-bit address there) read window 0 at the same offset instead. pso::console_mesh / console_geometry pass the title's windows now, and --precompile translates MS programs with them too. gpu_mesh_mode_reads_memory_windows runs an NGG program's compute pass on unified memory with its vertex table in window 1 and decoys at the same offset in window 0: four windows read the table, one window (the old translation) the decoys. The GS test also translates with four windows and compiles the result. This changes these programs' HLSL, so their DXIL is compiled again once. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/mod.rs | 13 ++++-- crates/xcl/src/gpu/recomp/pso.rs | 4 +- crates/xcl/src/gpu/recomp/tests.rs | 74 +++++++++++++++++++++++++++--- crates/xcl/src/shaders.rs | 7 ++- crates/xcl/tests/gpu_pso_e2e.rs | 2 +- 5 files changed, 82 insertions(+), 18 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 00efeec..4510a44 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -1202,13 +1202,16 @@ pub fn geometry_wave32_hazards(code: &[u8]) -> BTreeSet<&'static str> { } /// Translate a complete ES+GS program; `split_wave64`: for a GPU with 32-lane waves only. -pub fn translate_geometry(code: &[u8], interface: GsInterface, split_wave64: bool) -> Result { - translate(code, &Options { mode: Mode::Geometry(interface), wave64: true, split_wave64, ..Default::default() }) +/// `mem_windows`: the title's memory windows (`pso::mem_windows`), as for VS / PS / CS. +pub fn translate_geometry(code: &[u8], interface: GsInterface, split_wave64: bool, mem_windows: u32) -> Result { + translate(code, &Options { mode: Mode::Geometry(interface), wave64: true, split_wave64, mem_windows, ..Default::default() }) } -/// Translate an NGG mesh program; `split_wave64`: for a GPU with 32-lane waves only. -pub fn translate_mesh(code: &[u8], ms: MsInterface, wave64: bool, split_wave64: bool) -> Result { - translate(code, &Options { mode: Mode::Mesh(ms), wave64, split_wave64, ..Default::default() }) +/// Translate an NGG mesh program; `split_wave64`: for a GPU with 32-lane waves only; +/// `mem_windows`: the title's memory windows (`pso::mem_windows`), as for VS / PS / CS – with +/// one, a program reading title memory above the first window read the wrong memory. +pub fn translate_mesh(code: &[u8], ms: MsInterface, wave64: bool, split_wave64: bool, mem_windows: u32) -> Result { + translate(code, &Options { mode: Mode::Mesh(ms), wave64, split_wave64, mem_windows, ..Default::default() }) } /// Wave size the program was compiled for: 64-bit EXEC/VCC writes mean wave64. diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index a910d1b..2e0187b 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -342,7 +342,7 @@ pub fn console_mesh(blob: &[u8], ps: Option<&[u8]>, split_wave64: bool) -> Resul 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]; - finish_two_pass(super::translate_mesh(code, ms.clone(), super::detect_wave64(code), split_wave64)?, record, ps) + finish_two_pass(super::translate_mesh(code, ms.clone(), super::detect_wave64(code), split_wave64, mem_windows())?, record, ps) } /// Whether the adapter runs only 32-lane waves (its wave range lacks 64): wave64 compute, @@ -380,7 +380,7 @@ pub fn wave32_only_for_device(device: &ID3D12Device) -> bool { pub fn console_geometry(vs: &[u8], gs: &[u8], ps: Option<&[u8]>, split_wave64: bool) -> Result { let (code, g) = super::geometry_program(vs, gs)?; let record = g.record(); - let translation = super::translate_geometry(&code, g, split_wave64)?; + let translation = super::translate_geometry(&code, g, split_wave64, mem_windows())?; finish_two_pass(translation, record, ps) } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 076b4fd..3d78480 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2359,6 +2359,63 @@ fn gpu_unified_memory_arena() { } } +/// An NGG program (mesh mode) reading title memory above the first window: its vertex table +/// is in window 1, decoys sit at the same offset in window 0. Translated with the title's +/// windows (`translate_mesh`'s `mem_windows`, as `pso::console_mesh` now does) it reads window +/// 1; with one window – what MS / GS programs got before – it read the decoys. +#[test] +fn gpu_mesh_mode_reads_memory_windows() { + if !gpu_ready() { + return; + } + use super::{dxc, MsInterface, MESH_HEADER}; + 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: 64, max_vertices: 64, max_primitives: 64 }; + let list: Vec<(&str, &str, Vec<(&str, u64)>, Option)> = vec![ + ("V_MBCNT_LO_U32_B32", "ENC_VOP3", vec![("VDST", 0), ("SRC0", 193), ("SRC1", 128)], None), + ("V_MBCNT_HI_U32_B32", "ENC_VOP3", vec![("VDST", 0), ("SRC0", 193), ("SRC1", V)], None), // thread + ("S_MOV_B32", "ENC_SOP1", vec![("SDST", 124), ("SSRC0", 255)], Some(1 << 12 | 3)), // 1 primitive, 3 vertices + ("S_SENDMSG", "ENC_SOPP", vec![("SIMM16", 9)], None), + ("V_CMPX_GT_U32", "ENC_VOPC", vec![("SRC0", 131), ("VSRC1", 0)], None), // threads 0..2 + ("BUFFER_LOAD_DWORDX4", "ENC_MUBUF", vec![("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("IDXEN", 1)], None), + ("EXP", "ENC_EXP", vec![("TGT", 12), ("EN", 0xF), ("VSRC0", 10), ("VSRC1", 11), ("VSRC2", 12), ("VSRC3", 13), ("DONE", 1)], None), + ]; + let refs: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect(); + let code = prog(&refs); + let arena = crate::gpu::uma::Uma::with_windows(2, 1 << 20).unwrap(); + let (w0, w1) = (arena.window(0).0 as u64, arena.window(1).0 as u64); + let word = |a: u64| unsafe { &mut *(a as *mut u32) }; + for (i, (x, y)) in [(-1.0f32, -1.0f32), (-1.0, 1.0), (0.0, -1.0)].iter().enumerate() { + for (k, v) in [*x, *y, 0.0, 1.0].iter().enumerate() { + *word(w1 + 0x100 + 16 * i as u64 + 4 * k as u64) = f(*v); + *word(w0 + 0x100 + 16 * i as u64 + 4 * k as u64) = f(9.0); // must not be read + } + } + let mut st = KernelState::default(); + // V#: the vertex table in window 1, stride 16, 3 records + st.sgpr[16..20].copy_from_slice(&[(w1 + 0x100) as u32, ((w1 >> 32) as u32) & 0xFFFF | 16 << 16, 3, 0]); + let vd = ms.vert_dwords(); + let positions = |windows: u32| -> Vec<[f32; 4]> { + let t = super::translate_mesh(&code, ms.clone(), true, runner::split_wave64(), windows).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + let cs = dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}")); + let r = runner::run_in_arena(&cs, &st, &arena).unwrap(); + assert_eq!((r.raw[0], r.raw[1]), (3, 1), "GS_ALLOC_REQ counts ({windows} windows)"); + (0..3).map(|v| std::array::from_fn(|c| f32::from_bits(r.raw[MESH_HEADER + v * vd + c]))).collect() + }; + assert_eq!(positions(2), vec![[-1.0, -1.0, 0.0, 1.0], [-1.0, 1.0, 0.0, 1.0], [0.0, -1.0, 0.0, 1.0]], "the vertex table from window 1"); + assert_eq!(positions(1)[0], [9.0; 4], "one window: window 0 at the same offset (the old MS / GS translation)"); +} + /// Memory above 4 GiB: two UMA windows 4 GiB apart; every access kind picks its window from /// the high address bits (V#, SGPR pair, 64-bit VGPR address, SADDR pair) and reaches the /// right one – s_load, buffer load / store, global load / store / atomic in window 1 next to @@ -2858,9 +2915,14 @@ fn gpu_geometry_wave32_prefix_count_over_a_mask() { let wrong: Vec<_> = (0..64).filter(|&l| got[l] != want[l]).map(|l| (l, got[l], want[l])).collect(); assert!(wrong.is_empty(), "lanes (got [vcc, s[10:11], exec], want): {wrong:?}"); }; - run(&translate_geometry(&code, g.clone(), true).unwrap()); + run(&translate_geometry(&code, g.clone(), true, 1).unwrap()); + // with a title's four memory windows (`pso::console_geometry`): all of them declared and + // the program still compiles + let windowed = translate_geometry(&code, g.clone(), true, 4).unwrap(); + assert!(windowed.hlsl.contains("RWByteAddressBuffer mem3 : register(u5)")); + super::dxc::compile(&windowed.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}")); // the native wave64 form keeps the program, and computes the reference where it can run - let native = translate_geometry(&code, g, false).unwrap(); + let native = translate_geometry(&code, g, false, 1).unwrap(); if runner::wave64_supported() == Ok(true) { run(&native); } else { @@ -4512,21 +4574,21 @@ fn mesh_split_wave64_form() { 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 in [&lane, &branch] { - let native = super::translate_mesh(code, ms.clone(), true, false).unwrap(); + let native = super::translate_mesh(code, ms.clone(), true, false, 1).unwrap(); assert!(native.hlsl.contains("[WaveSize(64)]") && native.hlsl.contains("[numthreads(128, 1, 1)]") && !native.hlsl.contains("xcl_x_")); - let split = super::translate_mesh(code, ms.clone(), true, true).unwrap(); + let split = super::translate_mesh(code, ms.clone(), true, true, 1).unwrap(); assert!(split.complete(), "{:?}", split.unsupported_why); assert!(split.hlsl.contains("[WaveSize(32)]") && split.hlsl.contains("[numthreads(128, 1, 1)]")); assert!(split.hlsl.contains("uint xcl_tid = xcl_x_thread(gi);") && split.hlsl.contains("#define XCL_X_SPIN 1")); assert!(split.hlsl.contains("xcl_x_ballot(true)")); // a wave32 program is not split - let w32 = super::translate_mesh(code, ms.clone(), false, true).unwrap(); + let w32 = super::translate_mesh(code, ms.clone(), false, true, 1).unwrap(); assert!(w32.hlsl.contains("[WaveSize(32)]") && !w32.hlsl.contains("xcl_x_")); if super::dxc::location().is_ok() { super::dxc::compile(&split.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", split.hlsl)); } } - assert!(super::translate_mesh(&lane, ms, true, true).unwrap().hlsl.contains("xcl_x_rla_uniform(")); + assert!(super::translate_mesh(&lane, ms, true, true, 1).unwrap().hlsl.contains("xcl_x_rla_uniform(")); } /// Operations whose result spans both 32-lane halves of a wave64 – what the split form diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 44358dd..bbcaf90 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -758,10 +758,9 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, t0 }; for p in &f.programs { let code = &f.data[p.offset..p.offset + p.len]; - // as `pso` translates: VS / PS / CS with the title's memory windows - // (`console_translation`), MS with one (`translate_mesh`) - let mem_windows = if matches!(mode, recomp::Mode::Mesh(_)) { 1 } else { windows }; - let opt = recomp::Options { mode: mode.clone(), wave64: recomp::detect_wave64(code), split_wave64: split, mem_windows, ..Default::default() }; + // as `pso` translates: with the title's memory windows (`console_translation`, + // `translate_mesh`) + let opt = recomp::Options { mode: mode.clone(), wave64: recomp::detect_wave64(code), split_wave64: split, mem_windows: windows, ..Default::default() }; let Ok(t) = recomp::translate(code, &opt) else { continue }; if !t.complete() { continue; diff --git a/crates/xcl/tests/gpu_pso_e2e.rs b/crates/xcl/tests/gpu_pso_e2e.rs index 24818df..32c283e 100644 --- a/crates/xcl/tests/gpu_pso_e2e.rs +++ b/crates/xcl/tests/gpu_pso_e2e.rs @@ -456,7 +456,7 @@ fn retail_es_gs_voxelize_geometry_mode() { let (code, g) = recomp::geometry_program(&blobs[0], &blobs[1]).expect("ES + GS pair"); assert_eq!((g.max_vertex_count, g.vertex_bytes()), (3, 64)); let split_wave32 = recomp::pso::wave32_only_for_device(&_dev); - let t = recomp::translate_geometry(&code, g.clone(), split_wave32).unwrap(); + let t = recomp::translate_geometry(&code, g.clone(), split_wave32, 1).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); let cs = dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}")); let rec = g.record();