diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index 11be1c0ce4..998a414c70 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -931,6 +931,107 @@ fn gpu_direct_unaligned_multi_dword_format_store() { assert_eq!(&got[0xFC..0x118], &want[0xFC..0x118], "bytes 0xFC..0x118 after the store at 0x102"); } +/// MUBUF `buffer_store_format_*` (including `d16`) applies the inverse of V# `DST_SEL` when +/// routing VGPR components to memory (e.g. `B8G8R8A8_UNORM` `[6,5,4,7]`, `B8G8R8X8_UNORM` +/// `[6,5,4,1]`, `A8_UNORM` `[0,0,0,4]`, and swapped `YX01` `[5,4,0,1]`), while MTBUF +/// `tbuffer_store_format_*` ignores `DST_SEL`. Verified on both Direct DXIL and HLSL. +#[test] +fn gpu_buffer_store_format_honors_vsharp_dst_sel() { + use crate::gpu::recomp::{dxc, runner::{self, KernelState}, Options}; + if runner::adapter().is_err() || runner::wave32_supported() != Ok(true) || dxc::location().is_err() { + runner::skip_test("no GPU / wave32 / DXC"); + return; + } + let code = prog(&[ + ("V_CMPX_EQ_U32", "ENC_VOPC", &[("SRC0", 128), ("VSRC1", 0)], None), + // SRSRC 3 (s[12:15]): BGRA8 [6,5,4,7] -> 0x100 + ("BUFFER_STORE_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 4), ("SRSRC", 3), ("SOFFSET", 128)], None), + // SRSRC 4 (s[16:19]): BGRX8 [6,5,4,1] -> 0x104 + ("BUFFER_STORE_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 4), ("SRSRC", 4), ("SOFFSET", 128)], None), + // SRSRC 5 (s[20:23]): A8_UNORM [0,0,0,4] -> 0x108 + ("BUFFER_STORE_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 8), ("SRSRC", 5), ("SOFFSET", 128)], None), + // SRSRC 6 (s[24:27]): 16_16_FLOAT YX01 [5,4,0,1] -> 0x10C + ("BUFFER_STORE_FORMAT_XY", "ENC_MUBUF", &[("VDATA", 12), ("SRSRC", 6), ("SOFFSET", 128)], None), + // SRSRC 7 (s[28:31]): D16 BGRA8 [6,5,4,7] -> 0x110 + ("BUFFER_STORE_FORMAT_D16_XYZW", "ENC_MUBUF", &[("VDATA", 14), ("SRSRC", 7), ("SOFFSET", 128)], None), + // SRSRC 8 (s[32:35]): MTBUF with BGRA8 DST_SEL [6,5,4,7] (MTBUF ignores DST_SEL) -> 0x114 + ("TBUFFER_STORE_FORMAT_XYZW", "ENC_MTBUF", &[("VDATA", 4), ("SRSRC", 8), ("SOFFSET", 128), ("FORMAT", 56)], None), + // Round-trip loads from BGRA8 (s[12:15]) and A8 (s[20:23]) + ("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 20), ("SRSRC", 3), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 24), ("SRSRC", 5), ("SOFFSET", 128)], None), + ]); + let dxil_direct = translate(&code, KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }).expect("direct dxil"); + super::validate::validate(&dxil_direct).expect("direct dxil validate"); + let hlsl = crate::gpu::recomp::translate(&code, &Options { wave64: false, ..Default::default() }).expect("hlsl"); + assert!(hlsl.complete(), "hlsl incomplete: {:?}", hlsl.unsupported); + let dxil_hlsl = dxc::compile(&hlsl.hlsl, "main", "cs_6_6", &[]).expect("dxc"); + + let sel = |a: u32, b: u32, c: u32, d: u32| a | (b << 3) | (c << 6) | (d << 9); + let mut st = KernelState::default(); + st.sgpr[12..16].copy_from_slice(&[0x100, 0, 0x100, sel(6, 5, 4, 7) | (56 << 12)]); + st.sgpr[16..20].copy_from_slice(&[0x104, 0, 0x100, sel(6, 5, 4, 1) | (56 << 12)]); + st.sgpr[20..24].copy_from_slice(&[0x108, 0, 0x100, sel(0, 0, 0, 4) | (1 << 12)]); + st.sgpr[24..28].copy_from_slice(&[0x10C, 0, 0x100, sel(5, 4, 0, 1) | (29 << 12)]); + st.sgpr[28..32].copy_from_slice(&[0x110, 0, 0x100, sel(6, 5, 4, 7) | (56 << 12)]); + st.sgpr[32..36].copy_from_slice(&[0x114, 0, 0x100, sel(6, 5, 4, 7) | (56 << 12)]); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[0] = l as u32; + // RGBA = (1.0, 128/255, 0.0, 64/255) + r[4] = 1.0f32.to_bits(); + r[5] = (128.0f32 / 255.0).to_bits(); + r[6] = 0.0f32.to_bits(); + r[7] = (64.0f32 / 255.0).to_bits(); + // A8 = (0, 0, 0, 128/255) + r[8] = 0.0f32.to_bits(); + r[9] = 0.0f32.to_bits(); + r[10] = 0.0f32.to_bits(); + r[11] = (128.0f32 / 255.0).to_bits(); + // XY = (1.0, -2.0) + r[12] = 1.0f32.to_bits(); + r[13] = (-2.0f32).to_bits(); + // D16 RGBA = f16(1.0, 0.5, 0.0, 0.25) -> (0x3C00, 0x3800), (0x0000, 0x3400) + r[14] = 0x3800_3C00; + r[15] = 0x3400_0000; + } + let near = |got: u32, want: f32| (got as i64 - want.to_bits() as i64).abs() <= 2; + for (label, blob) in [("direct-dxil", &dxil_direct), ("hlsl", &dxil_hlsl)] { + let r = runner::run(blob, &st, &vec![0u32; 128]).unwrap_or_else(|e| panic!("{label}: {e}")); + assert_eq!(r.mem[0x100 / 4].to_le_bytes(), [0x00, 0x80, 0xFF, 0x40], "{label} BGRA8 store"); + assert_eq!(r.mem[0x104 / 4].to_le_bytes(), [0x00, 0x80, 0xFF, 0x00], "{label} BGRX8 store"); + assert_eq!(r.mem[0x108 / 4].to_le_bytes()[0], 0x80, "{label} A8_UNORM store"); + assert_eq!(r.mem[0x10C / 4], 0x3C00_C000, "{label} 16_16_FLOAT YX01 store"); + assert_eq!(r.mem[0x110 / 4].to_le_bytes(), [0x00, 0x80, 0xFF, 0x40], "{label} D16 BGRA8 store"); + assert_eq!(r.mem[0x114 / 4].to_le_bytes(), [0xFF, 0x80, 0x00, 0x40], "{label} MTBUF ignores DST_SEL"); + let v = &r.vgpr[0]; + assert!(v[20] == 1.0f32.to_bits() && near(v[21], 128.0 / 255.0) && v[22] == 0 && near(v[23], 64.0 / 255.0), "{label} BGRA8 round-trip"); + assert!(v[24] == 0 && v[25] == 0 && v[26] == 0 && near(v[27], 128.0 / 255.0), "{label} A8 round-trip"); + } +} + +/// No-GPU codegen check: MUBUF `buffer_store_format_*` emits `dst_sel_store` in HLSL and valid +/// DXIL, whereas MTBUF `tbuffer_store_format_*` does not wrap its payload in `dst_sel_store`. +#[test] +fn buffer_store_format_emits_dst_sel_store_for_mubuf_only() { + use crate::gpu::recomp::Options; + let mubuf = prog(&[ + ("BUFFER_STORE_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 4), ("SRSRC", 3), ("SOFFSET", 128)], None), + ("BUFFER_STORE_FORMAT_D16_XYZW", "ENC_MUBUF", &[("VDATA", 8), ("SRSRC", 3), ("SOFFSET", 128)], None), + ]); + let mtbuf = prog(&[ + ("TBUFFER_STORE_FORMAT_XYZW", "ENC_MTBUF", &[("VDATA", 4), ("SRSRC", 3), ("SOFFSET", 128), ("FORMAT", 56)], None), + ]); + for code in [&mubuf, &mtbuf] { + let dxil = translate(code, KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }).expect("direct dxil"); + super::validate::validate(&dxil).expect("dxil validate"); + } + let hlsl_mubuf = crate::gpu::recomp::translate(&mubuf, &Options::default()).expect("hlsl mubuf"); + assert!(hlsl_mubuf.complete()); + assert!(hlsl_mubuf.hlsl.contains("fmt_pack(dst_sel_store("), "expected dst_sel_store for MUBUF:\n{}", hlsl_mubuf.hlsl); + let hlsl_mtbuf = crate::gpu::recomp::translate(&mtbuf, &Options::default()).expect("hlsl mtbuf"); + assert!(hlsl_mtbuf.complete()); + assert!(!hlsl_mtbuf.hlsl.contains("fmt_pack(dst_sel_store("), "MTBUF must not wrap in dst_sel_store:\n{}", hlsl_mtbuf.hlsl); +} + #[test] fn window_at_lowers_to_32bit_carry_without_int64() { let ko = KernelOptions { wave64: false, split: false, mem_windows: 4, sampler_base: 0 }; diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index a0b25df6df..6fd8804cfc 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -1075,6 +1075,23 @@ impl Lw { let s0 = self.cmpc(Pred::Eq, sel, 0); self.selc(s0, 0, v) } + /// Inverse of `dst_sel` for MUBUF `buffer_store_format_*`: routes each memory channel `m` + /// (`0..4`, selector `4 + m`) from the VGPR component `k` whose `DST_SEL[k] == 4 + m`. + fn dst_sel_store(&mut self, vals: [Value; 4], d3: Value) -> [Value; 4] { + let sels = [0u32, 3, 6, 9].map(|sh| { + let s = if sh == 0 { d3 } else { self.lshrc(d3, sh) }; + self.andc(s, 7) + }); + let z = self.c(0); + [4u32, 5, 6, 7].map(|target| { + let mut v = z; + for k in (0..4).rev() { + let m = self.cmpc(Pred::Eq, sels[k], target); + v = self.sel(m, vals[k], v); + } + v + }) + } fn fmt_encode(&mut self, x: Value, info: Value, c: u32) -> Value { let ty = self.lshrc(info, 3); let ty = self.andc(ty, 7); @@ -1645,8 +1662,10 @@ impl Lw { out }; let c = self.land(act, ok); + let is_mubuf = mtbuf_fmt.is_none(); self.guarded(c, &mut |lw| { - let packed = lw.fmt_pack(vals, info); + let store_vals = if is_mubuf { lw.dst_sel_store(vals, d3) } else { vals }; + let packed = lw.fmt_pack(store_vals, info); lw.st_bytes(a, packed, nb) }) } diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 1c51cfd0e2..c0aa03a4c5 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -2271,9 +2271,11 @@ impl Em { self.line(&format!("if ({act}) {{ {} }}", assigns.join(" "))); } BufKind::FormatStore => { - let fmt = if d.enc.name == "ENC_MTBUF" { format!("{}u", f("FORMAT")) } else { format!("(({d3} >> 12) & 0x7Fu)") }; + let is_mtbuf = d.enc.name == "ENC_MTBUF"; + let fmt = if is_mtbuf { format!("{}u", f("FORMAT")) } else { format!("(({d3} >> 12) & 0x7Fu)") }; self.line(&format!("uint {t}_info = FMT_INFO[min({fmt}, 77u)];")); self.line(&format!("bool {t}_ok = and(({t}_info & 7u) != 0u, {});", in_range(format!("fmt_bytes({t}_info)")))); + let wrap = |v: String| if is_mtbuf { v } else { format!("dst_sel_store({v}, {d3})") }; if let Some((_, components, high)) = d16_format { self.line(&format!("uint {t}_ty = ({t}_info >> 3) & 7u;")); let mut source = |component: u64| { @@ -2283,11 +2285,13 @@ impl Em { format!("select({t}_ty == 4u, {h}, select({t}_ty == 5u, (uint)(((int)({h} << 16)) >> 16), asuint(f16tof32({h}))))") }; let vals: Vec = (0..4).map(|i| if i < components { source(i) } else { "0u".into() }).collect(); - self.line(&format!("if (and({act}, {t}_ok)) st_bytes({t}_a, fmt_pack(uint4({}), {t}_info), fmt_bytes({t}_info));", vals.join(", "))); + let packed = wrap(format!("uint4({})", vals.join(", "))); + self.line(&format!("if (and({act}, {t}_ok)) st_bytes({t}_a, fmt_pack({packed}, {t}_info), fmt_bytes({t}_info));")); return Ok(()); } let vals: Vec = (0..4).map(|i| if i < count { self.vreg(vdata + i) } else { "0u".into() }).collect(); - self.line(&format!("if (and({act}, {t}_ok)) st_bytes({t}_a, fmt_pack(uint4({}), {t}_info), fmt_bytes({t}_info));", vals.join(", "))); + let packed = wrap(format!("uint4({})", vals.join(", "))); + self.line(&format!("if (and({act}, {t}_ok)) st_bytes({t}_a, fmt_pack({packed}, {t}_info), fmt_bytes({t}_info));")); } } Ok(()) @@ -6445,6 +6449,14 @@ uint d16_fmt(uint x, uint sel, uint info) { return h | select(f < 0.0f, 0x8000u, 0u); } uint dst_sel(uint4 c, uint sel, bool isint) { return select(sel == 0u, 0u, select(sel == 1u, select(isint, 1u, 0x3F800000u), pick4(c, (sel - 4u) & 3u))); } +uint4 dst_sel_store(uint4 v, uint d3) { + uint s0 = d3 & 7u, s1 = (d3 >> 3u) & 7u, s2 = (d3 >> 6u) & 7u, s3 = (d3 >> 9u) & 7u; + uint x = select(s0 == 4u, v.x, select(s1 == 4u, v.y, select(s2 == 4u, v.z, select(s3 == 4u, v.w, 0u)))); + uint y = select(s0 == 5u, v.x, select(s1 == 5u, v.y, select(s2 == 5u, v.z, select(s3 == 5u, v.w, 0u)))); + uint z = select(s0 == 6u, v.x, select(s1 == 6u, v.y, select(s2 == 6u, v.z, select(s3 == 6u, v.w, 0u)))); + uint w = select(s0 == 7u, v.x, select(s1 == 7u, v.y, select(s2 == 7u, v.z, select(s3 == 7u, v.w, 0u)))); + return uint4(x, y, z, w); +} "#; /// Split wave64: whether the halves of a hardware group of `threads` meet through bounded