Просмотр исходного кода

Structure builder: accesses stay near the root views, a size that cannot be one counts as none (VICTORY_BVH_BOUNDS=0: neither)

A structure that was written over names gigabytes; the builder's copy of it
(CopyForBVHUpdate, 63 a frame in the dorm, 13.3 of 84 ms in the timing run
light-13) went through all other structures' memory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
local 1 день назад
Родитель
Сommit
adf5706983

+ 1 - 0
crates/victory-gpu/src/shader_cache.rs

@@ -74,6 +74,7 @@ fn build() -> [u8; 32] {
             "VICTORY_STRICT_JOINS",
             "VICTORY_MESH_OUTPUT_STRICT",
             "VICTORY_BVH_FENCES",
+            "VICTORY_BVH_BOUNDS",
         ] {
             bytes.extend_from_slice(name.as_bytes());
             bytes.push(b'=');

+ 28 - 2
crates/victory-shader/src/emit/lower.rs

@@ -3184,7 +3184,19 @@ impl<'a> Lower<'a> {
         let k31 = self.s.m.i32(31);
         let sign = self.s.m.bin(c, Bin::AShr, off, k31);
         let hi = self.s.m.bin(c, Bin::Add, hi, carry);
-        let hi = self.s.m.bin(c, Bin::Add, hi, sign);
+        let mut hi = self.s.m.bin(c, Bin::Add, hi, sign);
+        let mut sum = sum;
+        if super::builder_bounds() {
+            // No structure is near 64 MiB (the greatest seen has 14). One
+            // whose memory was written over says it has gigabytes, and the
+            // builder's copy of it went through all other structures' memory
+            // after it and, past 2 GiB, before it. Nothing is there instead.
+            let (below, all, zero) = (self.s.m.i32(16 << 20), self.s.m.i32(80 << 20), self.s.m.i32(0));
+            let at = self.s.m.bin(c, Bin::Add, off, below);
+            let near = self.s.m.cmp(c, pred::IULT, at, all);
+            sum = self.s.m.select(c, near, sum, zero);
+            hi = self.s.m.select(c, near, hi, zero);
+        }
         let (h, byte) = self.memory(sum, hi, guard, store);
         self.root_copy = Some(h);
         Some((h, byte))
@@ -6086,9 +6098,18 @@ impl<'a> Lower<'a> {
             return Ok(());
         }
         if matches!(kind, Some(u) if u.kind == ResourceType::Srv) {
+            // The size in a structure's header (its second dword), which a
+            // copy of it loops over.
+            let size = *offset == 4
+                && count == 1
+                && extra.is_none()
+                && matches!(soffset, Operand::Scalar { code: NULL, .. })
+                && super::builder_bounds();
+            let mut root = false;
             let h = match self.root_memory(src, addr, None, false) {
                 Some((h, byte)) => {
                     addr = byte;
+                    root = true;
                     h
                 }
                 None => self.handle(site, src, false, (11, 0))?,
@@ -6108,7 +6129,12 @@ impl<'a> Lower<'a> {
                     &[h, a, u, mask, al],
                 );
                 let x = self.s.m.extract(c, r, 0);
-                let x = self.as_int(x);
+                let mut x = self.as_int(x);
+                if size && root {
+                    let (most, zero) = (self.s.m.i32(1 << 20), self.s.m.i32(0));
+                    let sane = self.s.m.cmp(c, pred::IULT, x, most);
+                    x = self.s.m.select(c, sane, x, zero);
+                }
                 self.write(inst, Reg::S(code + k as u8), x, None);
             }
             return Ok(());

+ 9 - 0
crates/victory-shader/src/emit/mod.rs

@@ -188,6 +188,15 @@ pub fn builder_fences() -> bool {
     *ON.get_or_init(|| std::env::var_os("VICTORY_BVH_FENCES").is_none_or(|v| v != "0"))
 }
 
+/// Whether the structure builder's accesses through its root views stay near
+/// them, and a size in a structure's header that cannot be one counts as none
+/// (`VICTORY_BVH_BOUNDS=0`: neither). Part of the translation: the shader
+/// cache keys on it.
+pub fn builder_bounds() -> bool {
+    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
+    *ON.get_or_init(|| std::env::var_os("VICTORY_BVH_BOUNDS").is_none_or(|v| v != "0"))
+}
+
 /// The clean-up passes that run over a translated program before it is
 /// written (`bitcode::opt`): a set of that module's pass bits. Every pass
 /// is exact, so the switch is for telling a translator fault from a