diff --git a/crates/victory-gpu/src/shader_cache.rs b/crates/victory-gpu/src/shader_cache.rs index d5cf03f..35b5446 100644 --- a/crates/victory-gpu/src/shader_cache.rs +++ b/crates/victory-gpu/src/shader_cache.rs @@ -73,6 +73,7 @@ fn build() -> [u8; 32] { "VICTORY_FIRST_HALF", "VICTORY_STRICT_JOINS", "VICTORY_MESH_OUTPUT_STRICT", + "VICTORY_BVH_FENCES", ] { 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 d8387d5..ecad742 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -5230,6 +5230,17 @@ impl<'a> Lower<'a> { let void = self.s.t.void; self.s.op(self.cur, "barrier", Attr::NoDuplicate, void, 80, &[mode]); } + // The structure builder's threads take turns through atomics (a tree + // built from its leaves up): what one wrote before its atomic is + // seen by the thread that comes after it. The console's program + // waits for its writes there, and those waits are not in the code + // here (after beloooo's 72e748ea, VIctory-Belobug-AMD). + let ordered = (name.starts_with("buffer_atomic") || name.starts_with("global_atomic")) + && crate::spill::is_spilled(self.x.native) + && super::builder_fences(); + if ordered { + self.memory_fence(); + } let r = match &inst.fields { Fields::Sop { .. } => self.sop(inst), Fields::Sopp { .. } if name == "s_barrier" && self.no_barrier.contains(&b) => Ok(()), @@ -5275,6 +5286,9 @@ impl<'a> Lower<'a> { Fields::Flat { .. } if name.starts_with("global_") => self.flat(inst, b, i), Fields::Flat { .. } => Err("flat memory"), }; + if ordered && r.is_ok() { + self.memory_fence(); + } match r { Ok(()) => Ok(()), Err(why) if effect => Err(unsupported(offset, why)), @@ -5287,6 +5301,12 @@ impl<'a> Lower<'a> { } } + /// Writes to buffers made before are seen, by every group, before those made after. + fn memory_fence(&mut self) { + let (mode, void) = (self.s.m.i32(2), self.s.t.void); + self.s.op(self.cur, "barrier", Attr::NoDuplicate, void, 80, &[mode]); + } + /// The two host waves of a console wave meet (a group barrier with a shared-memory /// fence): LDS writes before are seen by reads after. fn lds_meeting(&mut self) { diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index f98517d..b714dc6 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -180,6 +180,14 @@ pub fn group_sync() -> u8 { }) } +/// Whether buffer atomics of the ray-tracing structure builder stand between +/// two fences (`VICTORY_BVH_FENCES=0`: they do not). Part of the +/// translation: the shader cache keys on it. +pub fn builder_fences() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("VICTORY_BVH_FENCES").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