From 077de0d9e6ebec78456e2177959f3e9283071934 Mon Sep 17 00:00:00 2001 From: mastrr9 <333127948+mastrr9@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:58:51 -0500 Subject: [PATCH] Recompiler: tbuffer_load_format_* ignores the V#'s DST_SEL The typed-buffer load path applied the V#'s DST_SEL (dword3 [11:0]) to every format load, tbuffer_load_format_* (MTBUF) included. The RDNA2 ISA's "Buffer Data" table gives the DST_SEL source per instruction: buffer_load_format_* / buffer_store_format_* use the resource's, tbuffer_*_format_* take FORMAT from the instruction and use the identity select (X, Y, Z, W), as do the raw and atomic forms. With a V# whose DST_SEL is not identity (a swizzled or all-SEL_0 word3, as on a raw buffer descriptor), tbuffer loads came out reordered or zero. MTBUF format loads (and their d16 forms) now take the converted components in order; missing components still get the format fill (0, 0, 0, 1). buffer_load_format_* is unchanged. Tests: tbuffer_load_ignores_vsharp_dst_sel checks the emitted HLSL (buffer_load_format keeps dst_sel(), the tbuffer xyzw and d16_xyz forms read t_c.x.. in order) and compiles it when DXC is present; gpu_tbuffer_load_identity_dst_sel loads through a V# with DST_SEL (z, y, x, w) and one with SEL_0 everywhere: buffer_load_format_xyzw returns (3, 2, 1, 4), tbuffer xyzw (1, 2, 3, 4), tbuffer 16_16_FLOAT (1.0, -2.0, 0, 1.0) and its d16_xyzw / d16_xy forms packed in order. Checked off-tree: a Python port of fmt_comp / dst_sel / the d16 packing (check_formats.py) gives the expected test vectors; the same script compares fmt_comp with an independent decoder over all 77 formats (random elements plus 0, 1, max and sign-boundary components) and round-trips decode/encode for every representable element. Not built or run on Windows. Assisted-by: Claude Code (Claude Opus 5.5) --- crates/xcl/src/gpu/recomp/emit.rs | 17 +++++++-- crates/xcl/src/gpu/recomp/tests.rs | 59 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2c..b82ac42bb5 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -1775,8 +1775,17 @@ impl Em { self.line(&format!("if ({act}) {{ {} }}", st.join(" "))); } BufKind::FormatLoad => { - // format from the instruction (tbuffer) or from the V# (dword3 [18:12]); DST_SEL from the V# - let fmt = if d.enc.name == "ENC_MTBUF" { format!("{}u", f("FORMAT")) } else { format!("(({d3} >> 12) & 0x7Fu)") }; + // buffer_load_format: format (dword3 [18:12]) and DST_SEL from the V#; tbuffer: format + // from the instruction and the identity DST_SEL (the V#'s is ignored; ISA "Buffer Data") + let mtbuf = d.enc.name == "ENC_MTBUF"; + let fmt = if mtbuf { format!("{}u", f("FORMAT")) } else { format!("(({d3} >> 12) & 0x7Fu)") }; + let pick = |k: u64| { + if mtbuf { + format!("{t}_c.{}", ["x", "y", "z", "w"][k as usize]) + } else { + format!("dst_sel({t}_c, ({d3} >> {}u) & 7u, {t}_int)", 3 * k) + } + }; self.line(&format!("uint {t}_info = FMT_INFO[min({fmt}, 77u)];")); self.line(&format!("bool {t}_int = ((({t}_info >> 3) & 7u) == 4u) || ((({t}_info >> 3) & 7u) == 5u);")); self.line(&format!("bool {t}_ok = and(({t}_info & 7u) != 0u, {});", in_range(format!("fmt_bytes({t}_info)")))); @@ -1786,7 +1795,7 @@ impl Em { )); if let Some((_, components, high)) = d16_format { let half = |component: u64| { - let selected = format!("dst_sel({t}_c, ({d3} >> {}u) & 7u, {t}_int)", 3 * component); + let selected = pick(component); format!("select({t}_int, {selected} & 0xFFFFu, f32tof16(asfloat({selected})))") }; let mut assigns = Vec::new(); @@ -1807,7 +1816,7 @@ impl Em { let mut assigns = Vec::new(); for i in 0..count { let v = self.vreg(vdata + i); - assigns.push(format!("{v} = select({t}_ok, dst_sel({t}_c, ({d3} >> {}u) & 7u, {t}_int), 0u);", 3 * i)); + assigns.push(format!("{v} = select({t}_ok, {}, 0u);", pick(i))); } self.line(&format!("if ({act}) {{ {} }}", assigns.join(" "))); } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 4e032b68d1..ed900698b3 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1576,6 +1576,65 @@ fn gpu_typed_buffer_loads_and_stores() { assert_eq!(r.sgpr[50], 1 | 1 << 5, "s_bitset1"); } +/// RDNA2 ISA "Buffer Data": tbuffer_* take the format from the instruction and the identity +/// DST_SEL; only buffer_*_format apply the V#'s DST_SEL. +#[test] +fn tbuffer_load_ignores_vsharp_dst_sel() { + let code = prog(&[ + ("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 10), ("SRSRC", 3), ("SOFFSET", 128)], None), + ("TBUFFER_LOAD_FORMAT_XYZW", "ENC_MTBUF", &[("VDATA", 14), ("SRSRC", 3), ("SOFFSET", 128), ("FORMAT", 60)], None), + ("TBUFFER_LOAD_FORMAT_D16_XYZ", "ENC_MTBUF", &[("VDATA", 18), ("SRSRC", 3), ("SOFFSET", 128), ("FORMAT", 29)], None), + ]); + let t = translate(&code, &Options::default()).unwrap(); + assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl); + assert!(t.hlsl.contains("v10 = select(t1_ok, dst_sel(t1_c, (s15 >> 0u) & 7u, t1_int), 0u);"), "{}", t.hlsl); + for (k, c) in ["x", "y", "z", "w"].iter().enumerate() { + assert!(t.hlsl.contains(&format!("v{} = select(t2_ok, t2_c.{c}, 0u);", 14 + k)), "{}", t.hlsl); + } + assert!(!t.hlsl.contains("dst_sel(t2_c") && !t.hlsl.contains("dst_sel(t3_c"), "{}", t.hlsl); + assert!(t.hlsl.contains("v18 = t3_ok ? (select(t3_int, t3_c.x & 0xFFFFu, f32tof16(asfloat(t3_c.x))) | (select(t3_int, t3_c.y"), "{}", t.hlsl); + assert!(t.hlsl.contains("v19 = t3_ok ? (select(t3_int, t3_c.z & 0xFFFFu, f32tof16(asfloat(t3_c.z))) | (0u << 16)) : 0u;"), "{}", t.hlsl); + if super::dxc::location().is_ok() { + super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl)); + } +} + +#[test] +fn gpu_tbuffer_load_identity_dst_sel() { + if !gpu_ready() { + return; + } + let bgra = 6 | 5 << 3 | 4 << 6 | 7 << 9; + let code = prog(&[ + ("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 10), ("SRSRC", 3), ("SOFFSET", 128)], None), + ("TBUFFER_LOAD_FORMAT_XYZW", "ENC_MTBUF", &[("VDATA", 14), ("SRSRC", 3), ("SOFFSET", 128), ("FORMAT", 60)], None), + ("TBUFFER_LOAD_FORMAT_XYZW", "ENC_MTBUF", &[("VDATA", 18), ("SRSRC", 4), ("SOFFSET", 128), ("FORMAT", 29)], None), + ("TBUFFER_LOAD_FORMAT_D16_XYZW", "ENC_MTBUF", &[("VDATA", 22), ("SRSRC", 4), ("SOFFSET", 128), ("FORMAT", 29)], None), + ("TBUFFER_LOAD_FORMAT_D16_XY", "ENC_MTBUF", &[("VDATA", 24), ("SRSRC", 3), ("SOFFSET", 128), ("FORMAT", 60)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 25), ("SRSRC", 4), ("SOFFSET", 128)], None), + ]); + let mut mem = vec![0u32; 128]; + mem[0x100 / 4] = u32::from_le_bytes([1, 2, 3, 4]); // 8_8_8_8_UINT + mem[0x110 / 4] = 0x3C00 | 0xC000 << 16; // 16_16_FLOAT (1.0, -2.0) + let mut st = KernelState::default(); + // V# A: 8_8_8_8_UINT, DST_SEL = (z, y, x, w); V# B: 16_UINT, every DST_SEL = SEL_0 + st.sgpr[12..16].copy_from_slice(&[0x100, 0, 0x100, bgra | 60 << 12]); + st.sgpr[16..20].copy_from_slice(&[0x110, 0, 0x100, 11 << 12]); + for r in st.vgpr.iter_mut() { + r[25] = 0xDEAD_BEEF; + } + let r = runner::run_code(&code, &st, &mem).unwrap(); + for l in 0..64 { + let v = &r.vgpr[l]; + assert_eq!(v[10..14], [3, 2, 1, 4], "lane {l} buffer_load_format applies the V# DST_SEL"); + assert_eq!(v[14..18], [1, 2, 3, 4], "lane {l} tbuffer xyzw: identity DST_SEL"); + assert_eq!(v[18..22], [f(1.0), f(-2.0), 0, f(1.0)], "lane {l} tbuffer 16_16_FLOAT: V# SEL_0 ignored, z = 0, w = 1"); + assert_eq!(v[22..24], [0xC000_3C00, 0x3C00_0000], "lane {l} tbuffer d16 xyzw"); + assert_eq!(v[24], 1 | 2 << 16, "lane {l} tbuffer d16 xy: identity DST_SEL"); + assert_eq!(v[25], 0, "lane {l} buffer_load_format 16_UINT (0x3C00) with SEL_0 everywhere"); + } +} + fn el(name: &str, index: u32, sv: u32, ct: u32, reg: u32, mask: u8) -> crate::gpu::dxbc::SigElement { crate::gpu::dxbc::SigElement { name: name.into(), index, system_value: sv, component_type: ct, register: reg, mask, rw_mask: mask, stream: 0 } }