Pārlūkot izejas kodu

Shaders: med3 with NaN, fract below 1.0, signed bit fields past bit 31

Three lowerings gave results the console's GPU does not (issue #2, from
hardware measurements published by another project):

- v_med3_f32/f16: with a NaN operand the hardware gives the minimum of
  the three; we gave the median of the other two.
- v_fract_f32/f16: the hardware never reaches 1.0 (a tiny negative
  input gives the largest value below it); the host's own fract differs
  by vendor and can give 1.0.
- v_bfe_i32/s_bfe_i32: a signed field running past bit 31 extends from
  bit 31; the shift is arithmetic now. Fields that fit are unchanged.

The fract case is asserted in float_semantics_are_the_consoles (the
console compiler emits v_fract_f32 for frac). The other two cannot be
produced from HLSL with the compiler the tests use and rest on the
measured values in the issue.

Still open from the issue: range reduction before sine and cosine, and
denormal operands of the legacy multiply.

Refs #2

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Marvis 3 dienas atpakaļ
vecāks
revīzija
36eb0b4ea7

+ 10 - 3
crates/victory-shader/src/emit/diff_tests.rs

@@ -977,8 +977,9 @@ fn translated_mesh_shaders_draw_as_pc_ones() {
 
 /// Where PC code and the console differ by design, the translation must
 /// give the console's results: float-to-int conversions saturate and give
-/// 0 for NaN (`v_cvt_i32/u32_f32`), and the legacy multiply the compiler
-/// uses for `*` gives 0 for 0 × NaN.
+/// 0 for NaN (`v_cvt_i32/u32_f32`), the legacy multiply the compiler
+/// uses for `*` gives 0 for 0 × NaN, and `frac` never reaches 1.0 (it gives
+/// the largest value below; some hosts' own `frac` gives 1.0).
 #[test]
 fn float_semantics_are_the_consoles() {
     use super::host::{Dispatch, View};
@@ -998,6 +999,8 @@ fn float_semantics_are_the_consoles() {
             outb.Store(4 * i, asuint(int(x)));
             outb.Store(16 + 4 * i, uint(x));
             outb.Store(32 + 4 * i, asuint(x * k.w));
+            float y = i == 0 ? k.w - 1e-10 : i == 1 ? k.z : i == 2 ? k.w - 1e-8 : k.z + 0.25;
+            outb.Store(48 + 4 * i, asuint(frac(y)));
         }"#;
     let cs = translate(&compile(&console, &dir, src, "cs", "cs_6_5")).unwrap();
     let (root, pso) = host.compute_pipeline(&cs).unwrap();
@@ -1009,7 +1012,7 @@ fn float_semantics_are_the_consoles() {
         View::Texture(1, 1, &[[0.0; 4]]),
         View::Texture(1, 1, &[[0.0; 4]]),
         View::Texture(1, 1, &[[0.0; 4]]),
-        View::RawUav(12),
+        View::RawUav(16),
         View::TextureUav(1, 1),
     ];
     let out = host
@@ -1040,6 +1043,10 @@ fn float_semantics_are_the_consoles() {
             0,
             0,
             0, // x * 0, NaN included
+            0x3F7F_FFFF,
+            0.5f32.to_bits(),
+            0x3F7F_FFFF,
+            0.75f32.to_bits(), // frac
         ]
     );
 }

+ 46 - 16
crates/victory-shader/src/emit/lower.rs

@@ -1356,7 +1356,9 @@ impl<'a> Lower<'a> {
     }
 
     /// `(a >> off[4:0]) & ((1 << width) - 1)`, sign-extended from the field's
-    /// top bit when `signed`; widths from 32 on take every bit.
+    /// top bit when `signed`; widths from 32 on take every bit. A signed
+    /// field that runs past bit 31 extends from bit 31, as the hardware
+    /// does: the shift is arithmetic then.
     fn bfe(&mut self, a: V, off: V, width: V, signed: bool) -> V {
         let c = self.cur;
         let (k31, k32, zero, one, ones) = (
@@ -1367,7 +1369,8 @@ impl<'a> Lower<'a> {
             self.s.m.i32(u32::MAX),
         );
         let off = self.s.m.bin(c, Bin::And, off, k31);
-        let shifted = self.s.m.bin(c, Bin::LShr, a, off);
+        let shift = if signed { Bin::AShr } else { Bin::LShr };
+        let shifted = self.s.m.bin(c, shift, a, off);
         let full = self.s.m.cmp(c, pred::IUGE, width, k32);
         let w = self.s.m.bin(c, Bin::And, width, k31);
         let mask = self.s.m.bin(c, Bin::Shl, one, w);
@@ -1387,6 +1390,34 @@ impl<'a> Lower<'a> {
         self.s.m.select(c, keep, r, t)
     }
 
+    /// The median of three. With a NaN among them the hardware gives the
+    /// minimum of the three instead (the host's minimum skips NaNs, so that
+    /// is the smallest number among them).
+    fn med3(&mut self, a: V, b: V, third: V) -> V {
+        let c = self.cur;
+        let lo = self.binary(36, a, b);
+        let hi = self.binary(35, a, b);
+        let t = self.binary(36, hi, third);
+        let median = self.binary(35, lo, t);
+        let least = self.binary(36, lo, third);
+        let ab = self.s.m.cmp(c, pred::UNO, a, b);
+        let nan3 = self.s.m.cmp(c, pred::UNO, third, third);
+        let nan = self.s.m.bin(c, Bin::Or, ab, nan3);
+        self.s.m.select(c, nan, least, median)
+    }
+
+    /// `a - floor(a)`, which on the hardware never reaches 1.0: where it
+    /// would round to it (a tiny negative `a`), the result is `below_one`,
+    /// the largest value under 1.0 in the operation's width. Hosts differ
+    /// here (some give 1.0). NaN stays NaN.
+    fn fract(&mut self, a: V, below_one: f32) -> V {
+        let c = self.cur;
+        let r = self.unary(22, a);
+        let k = self.s.m.f32(below_one.to_bits());
+        let over = self.s.m.cmp(c, pred::OGT, r, k);
+        self.s.m.select(c, over, k, r)
+    }
+
     /// The high dword of a 32 × 32-bit product.
     fn mul_hi(&mut self, a: V, b: V, signed: bool) -> V {
         let c = self.cur;
@@ -3624,12 +3655,7 @@ impl<'a> Lower<'a> {
                         let t = self.binary(36, a, bb);
                         self.binary(36, t, cc)
                     }
-                    _ => {
-                        let lo = self.binary(36, a, bb);
-                        let hi = self.binary(35, a, bb);
-                        let t = self.binary(36, hi, cc);
-                        self.binary(35, lo, t)
-                    }
+                    _ => self.med3(a, bb, cc),
                 };
                 float(self, r)
             }
@@ -3662,7 +3688,11 @@ impl<'a> Lower<'a> {
                     let k = self.s.m.f32(TWO_PI.to_bits());
                     a = self.s.m.bin(c, Bin::Mul, a, k);
                 }
-                let r = self.unary(op, a);
+                let r = if op == 22 {
+                    self.fract(a, f32::from_bits(0x3F7F_FFFF))
+                } else {
+                    self.unary(op, a)
+                };
                 float(self, r)
             }
             "v_cvt_i32_f32" | "v_cvt_u32_f32" | "v_cvt_flr_i32_f32" | "v_cvt_rpi_i32_f32" => {
@@ -4221,12 +4251,7 @@ impl<'a> Lower<'a> {
                     let t = self.binary(36, a, b);
                     self.binary(36, t, acc)
                 }
-                _ => {
-                    let lo = self.binary(36, a, b);
-                    let hi = self.binary(35, a, b);
-                    let t = self.binary(36, hi, acc);
-                    self.binary(35, lo, t)
-                }
+                _ => self.med3(a, b, acc),
             };
             return float_result(self, r);
         }
@@ -4268,7 +4293,12 @@ impl<'a> Lower<'a> {
                         let k = self.s.m.f32(TWO_PI.to_bits());
                         a = self.s.m.bin(c, Bin::Mul, a, k);
                     }
-                    self.unary(op, a)
+                    if op == 22 {
+                        // 0x3bff as a half: 1 - 2^-11.
+                        self.fract(a, 1.0 - 1.0 / 2048.0)
+                    } else {
+                        self.unary(op, a)
+                    }
                 }
             };
             return float_result(self, r);