diff --git a/crates/victory-gpu/src/shader_cache.rs b/crates/victory-gpu/src/shader_cache.rs index 35b5446..016ab4e 100644 --- a/crates/victory-gpu/src/shader_cache.rs +++ b/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'='); diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index ecad742..09db9d1 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/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(()); diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index b714dc6..eed0fac 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/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 = 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