From a1e0ee891c99b353c1d9ffabcdc8d6fa196e711f Mon Sep 17 00:00:00 2001 From: emirduran707 Date: Wed, 25 Nov 2026 02:05:44 +0300 Subject: [PATCH] Direct DXIL: sign-extend D16 image_store halves for SINT textures `image_store` with D16 unpacked each 16-bit half with `& 0xFFFF` and passed it unchanged to the `RWTexture` store for SINT formats, so a negative half such as -1 (0xFFFF) became 65535 and the R16 SINT store clamped it to 32767. Sign-extend the half when the T# format class is SINT, as the D16 `buffer_store_format_*` path in `lower_mem.rs` already does. Assisted-by: Antigravity (Gemini 4 Neon, Claude Opus 5.5) --- crates/xcl/src/gpu/dxil/core_tests.rs | 33 ++++++++++++++++++++++++++ crates/xcl/src/gpu/dxil/lower_image.rs | 6 ++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index 01a48a1b42..f7f8defa0c 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -438,6 +438,39 @@ fn ps_get_lod_with_a_static_sampler() { } } +/// `image_store` with D16 on a SINT texture sign-extends each half: -1 stays -1 instead of +/// becoming 65535 (clamped to 32767 by the R16 SINT store). +#[test] +fn gpu_direct_image_store_d16_sint_sign_extends() { + use crate::gpu::recomp::runner::{self, KernelState, TestTexture, TexDim}; + use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R16G16B16A16_SINT; + // T# s[16:23] (SRSRC 4): 2D 16_16_16_16_SINT (format 70); lane 0 stores texel (0, 0) + let code = prog(&[ + ("V_CMPX_EQ_U32", "ENC_VOPC", &[("SRC0", 128), ("VSRC1", 0)], None), + ("IMAGE_STORE", "ENC_MIMG", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("DMASK", 0xF), ("DIM", 1), ("UNORM", 1), ("D16", 1)], None), + ]); + let k = runner::direct_kernel_full(&code, true).unwrap_or_else(|e| panic!("direct translation: {e}")); + super::validate::validate(&k.dxil).unwrap_or_else(|e| panic!("validator: {e}")); + if runner::adapter().is_err() || runner::wave64_programs_supported() != Ok(true) || crate::gpu::recomp::dxc::location().is_err() { + runner::skip_test("no GPU / DXC / wave64"); + return; + } + let mut st = KernelState::default(); + st.sgpr[16..24].copy_from_slice(&[0, 70 << 20, 0, 9 << 28, 0, 0, 0, 0]); + let h = |v: i16| v as u16 as u32; + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[0] = l as u32; + r[1] = 0; + r[10] = h(-1) | h(-100) << 16; + r[11] = h(42) | h(-32768) << 16; + } + let tex = TestTexture { dim: TexDim::D2, format: DXGI_FORMAT_R16G16B16A16_SINT, width: 4, height: 4, depth: 1, texel_bytes: 8, data: vec![0; 128], uav: true }; + let res = runner::run_with_statics(&k.dxil, &st, &[], &[tex], &[], &k.static_samplers).expect("GPU run"); + let t: Vec = res.textures[0].chunks(2).map(|c| i16::from_le_bytes(c.try_into().unwrap())).collect(); + assert_eq!(&t[..4], &[-1, -100, 42, -32768], "texel (0, 0) after a D16 IMAGE_STORE on a SINT texture"); + assert!(t[4..].iter().all(|&v| v == 0), "only texel (0, 0) is written"); +} + /// Without a program-set `MODE[3:2]`, `v_cvt_f16_f32`, `v_mul_f16` and `v_pk_mul_f16` /// must round to nearest-even (matching RDNA2 and the HLSL path) rather than using /// `dx.op.legacyF32ToF16` (which rounds toward zero / flushes below the smallest f16 denormal). diff --git a/crates/xcl/src/gpu/dxil/lower_image.rs b/crates/xcl/src/gpu/dxil/lower_image.rs index 07c5aba4a9..53ac5c524b 100644 --- a/crates/xcl/src/gpu/dxil/lower_image.rs +++ b/crates/xcl/src/gpu/dxil/lower_image.rs @@ -853,8 +853,12 @@ impl Lw { let hp = self.andc(hp, 0xFFFF); let fh = self.s.f16_to_f32(hp); let fb = self.bits(fh); + // SINT (cls 2) halves are signed 16-bit integers + let sx = self.sx16(hp); + let si = self.cmpc(Pred::Eq, cls, 2); + let iv = self.sel(si, sx, hp); let fl = self.cmpc(Pred::Eq, cls, 0); - self.sel(fl, fb, hp) + self.sel(fl, fb, iv) } else { self.vreg(vdata + k as u64) };