From 788cff980e261c668603656ab982c0a2794a026d Mon Sep 17 00:00:00 2001 From: Yaohui Xie Date: Thu, 26 Nov 2026 03:43:39 -0600 Subject: [PATCH 1/3] Shaders: select the cube SC coordinate before applying its sign A negative Z face negates X by changing its sign bit alone. Selecting the signed values directly lost the subnormal payload on the RTX 4090 Laptop. Select the operand and its sign mask separately, then apply one integer XOR. Check both Z faces and both signs at the endpoints of the subnormal range. --- crates/victory-shader/src/emit/lower.rs | 12 ++++++++---- .../victory-shader/src/emit/semantics_tests.rs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 5e2caf758c..c427f7e0ea 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -6084,10 +6084,14 @@ impl<'a> Lower<'a> { "v_cubesc_f32" => { let (ix, iz) = (self.as_int(rx), self.as_int(rz)); let (nz, nx) = (neg(self, z), neg(self, x)); - let (mx, mz) = (neg_bits(self, ix), neg_bits(self, iz)); - let zf = self.s.m.select(c, nz, mx, ix); - let xf = self.s.m.select(c, nx, iz, mz); - let bits = pick(self, zf, ix, xf); + // Select the coordinate and its sign adjustment separately, so + // the selected result stays integer data even when subnormal. + let (sign, keep) = (self.s.m.i32(0x8000_0000), self.s.m.i32(0)); + let zmask = self.s.m.select(c, nz, sign, keep); + let xmask = self.s.m.select(c, nx, keep, sign); + let raw = pick(self, ix, ix, iz); + let mask = pick(self, zmask, keep, xmask); + let bits = self.s.m.bin(c, Bin::Xor, raw, mask); self.as_float(bits) } "v_cubetc_f32" => { diff --git a/crates/victory-shader/src/emit/semantics_tests.rs b/crates/victory-shader/src/emit/semantics_tests.rs index caa14c88c6..e4acf406c2 100644 --- a/crates/victory-shader/src/emit/semantics_tests.rs +++ b/crates/victory-shader/src/emit/semantics_tests.rs @@ -1390,6 +1390,22 @@ fn cube_faces_match_the_hardware() { assert_clean(check(&cube_probes())); } +/// When z is the major axis, SC selects x. Only the negative-z face flips +/// its sign; the subnormal payload must survive either choice. +#[test] +fn cube_sc_keeps_subnormal_x_on_both_z_faces() { + let inputs = [0x0000_0001, 0x8000_0001, 0x007f_ffff, 0x807f_ffff] + .into_iter() + .flat_map(|x| [0x3f80_0000, 0xbf80_0000].map(|z| [x, 0, z, OLD])) + .collect(); + assert_clean(check(&[probe( + vop3(0x145, 5, [v(1), v(2), v(3)], NONE), + inputs, + INT, + |[x, _, z, _]| vec![x ^ (z & 0x8000_0000)], + )])); +} + /// RDNA2 hardware oracle check for `v_cubesc_f32`, `v_cubetc_f32` and /// `v_cubema_f32` on signed zeros and denormal coordinates: `v_cubesc_f32` and /// `v_cubetc_f32` preserve subnormal coordinate bits (flipping only the sign From 7fceb14548cb783fa28b5261315002fce8340c40 Mon Sep 17 00:00:00 2001 From: Yaohui Xie Date: Thu, 26 Nov 2026 03:46:21 -0600 Subject: [PATCH 2/3] Shaders: keep positive subnormal cube coordinates through clamp The measured RDNA2 SC and TC clamp rows retain a selected positive subnormal without an output multiplier. Preserve that bit pattern while using the ordinary saturate for other values. Leave output multipliers on their existing path. --- crates/victory-shader/src/emit/lower.rs | 28 +++++++++++++++-- .../src/emit/semantics_tests.rs | 30 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index c427f7e0ea..801fa9d5b0 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -2829,7 +2829,18 @@ impl<'a> Lower<'a> { if v.clamp { f = self.unary(7, f); } - let Some(Operand::Vector { reg, .. }) = v.dst else { + self.store_float(inst, v.dst, f, guard) + } + + /// Store a float after its instruction's output modifiers have been applied. + fn store_float( + &mut self, + inst: &Inst, + dst: Option, + f: V, + guard: Option, + ) -> Result<(), Why> { + let Some(Operand::Vector { reg, .. }) = dst else { return Err("a float result outside a VGPR"); }; let x = self.as_int(f); @@ -6114,7 +6125,20 @@ impl<'a> Lower<'a> { self.as_float(bits) } }; - float(self, r) + if v.clamp && v.omod == 0 && matches!(name, "v_cubesc_f32" | "v_cubetc_f32") { + // A selected positive subnormal survives cube clamp on RDNA2. + // For the other bit patterns use the ordinary float clamp. + let bits = self.as_int(r); + let min_normal = self.s.m.i32(0x0080_0000); + let keep = self.s.m.cmp(c, pred::IULT, bits, min_normal); + let saturated = self.unary(7, r); + let clipped = self.as_int(saturated); + let bits = self.s.m.select(c, keep, bits, clipped); + let result = self.as_float(bits); + self.store_float(inst, v.dst, result, guard) + } else { + float(self, r) + } } "v_movrels_b32" | "v_movreld_b32" | "v_movrelsd_b32" => { // M0 indexes the source (movrels), the destination (movreld) diff --git a/crates/victory-shader/src/emit/semantics_tests.rs b/crates/victory-shader/src/emit/semantics_tests.rs index e4acf406c2..0c4f12d235 100644 --- a/crates/victory-shader/src/emit/semantics_tests.rs +++ b/crates/victory-shader/src/emit/semantics_tests.rs @@ -1503,6 +1503,36 @@ fn cube_zero_and_denormals_match_rdna2_oracle() { ])); } +/// The clamped SC/TC outputs measured in AnyPS5's Vop3CubeClamp.cpp +/// (f457d87b, rows 6, 7, 8, 11 and 16), with no output multiplier. +#[test] +fn clamped_cube_coordinates_match_rdna2_oracle() { + const CASES: [([u32; 3], [u32; 2]); 5] = [ + ([0xff80_0000, 0x8000_0007, 0x0000_0007], [7, 7]), + ([0x7fc0_0000, 0x0000_0007, 0xff80_0000], [0x3f80_0000, 0]), + ([0xbf00_0000, 0, 0x8000_0007], [0, 0]), + ([7, 7, 0x8000_0007], [7, 0]), + ([0xbf00_0000, 0xbf00_0000, 0x3f00_0000], [0, 0x3f00_0000]), + ]; + let inputs: Vec<_> = CASES + .iter() + .map(|&([x, y, z], _)| [x, y, z, OLD]) + .collect(); + let probes = [0x145, 0x146].map(|op| { + let clamp = M { clamp: true, ..NONE }; + probe( + vop3(op, 5, [v(1), v(2), v(3)], clamp), + inputs.clone(), + INT, + move |[x, y, z, _]| { + let (_, output) = CASES.iter().find(|(xyz, _)| *xyz == [x, y, z]).unwrap(); + vec![output[usize::from(op - 0x145)]] + }, + ) + }); + assert_clean(check(&probes)); +} + /// The 16 float predicates of `v_cmp_*_f32` (RDNA2 ISA: `o` ordered, `u` /// unordered, `n*` true when unordered). fn compare(op: u16, a: u32, b: u32) -> bool { From 30ddc891e3ffab4bf1f98b4de52b1bb7241d2583 Mon Sep 17 00:00:00 2001 From: Yaohui Xie Date: Thu, 26 Nov 2026 03:55:06 -0600 Subject: [PATCH 3/3] Format the clamped cube regression fixture --- crates/victory-shader/src/emit/semantics_tests.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/victory-shader/src/emit/semantics_tests.rs b/crates/victory-shader/src/emit/semantics_tests.rs index 0c4f12d235..b4deeca423 100644 --- a/crates/victory-shader/src/emit/semantics_tests.rs +++ b/crates/victory-shader/src/emit/semantics_tests.rs @@ -1514,12 +1514,12 @@ fn clamped_cube_coordinates_match_rdna2_oracle() { ([7, 7, 0x8000_0007], [7, 0]), ([0xbf00_0000, 0xbf00_0000, 0x3f00_0000], [0, 0x3f00_0000]), ]; - let inputs: Vec<_> = CASES - .iter() - .map(|&([x, y, z], _)| [x, y, z, OLD]) - .collect(); + let inputs: Vec<_> = CASES.iter().map(|&([x, y, z], _)| [x, y, z, OLD]).collect(); let probes = [0x145, 0x146].map(|op| { - let clamp = M { clamp: true, ..NONE }; + let clamp = M { + clamp: true, + ..NONE + }; probe( vop3(op, 5, [v(1), v(2), v(3)], clamp), inputs.clone(),