diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 5e2caf758c..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); @@ -6084,10 +6095,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" => { @@ -6110,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 caa14c88c6..b4deeca423 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 @@ -1487,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 {