diff --git a/README.md b/README.md index 304ea8d..0d20180 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ cargo build --profile diag -p victory --features diagnostics # target\diag\vic | `VICTORY_SKIP_PIPELINES=,...` | omit draws and dispatches, including indirect ones, whose pipeline contains a listed 16-digit shader hash | | `VICTORY_GPU_TIMING=1` | with the above, each list of a submission run and timed on its own; slow lists logged with their slowest commands | | `VICTORY_D3D12_WARP=1` | host device on the software adapter (slow; cannot reset the driver) | +| `VICTORY_SHADER_LOOP_LIMIT=` | translated shader loops stop after `n` iterations per lane instead of 2^20 (own shader cache entries); if a lower `n` makes world frames much faster, loops hitting the limit are what makes them slow | | `VICTORY_AUDIO_CAPTURE=` | records what the title plays (our process only) | | `VICTORY_XMA_DUMP=` | saves the first 32 XMA voices' context and input | diff --git a/crates/victory-gpu/src/shader_cache.rs b/crates/victory-gpu/src/shader_cache.rs index b52d7f0..0cba938 100644 --- a/crates/victory-gpu/src/shader_cache.rs +++ b/crates/victory-gpu/src/shader_cache.rs @@ -36,13 +36,25 @@ pub(crate) fn counts() -> (u64, u64) { (HITS.load(Ordering::Relaxed), MISSES.load(Ordering::Relaxed)) } -/// What identifies this build: the BLAKE3 of our executable file. +/// What identifies this build: the BLAKE3 of our executable file and of +/// the translation settings it runs with (the loop limit, which a +/// diagnostics build can change at run time). fn build() -> &'static [u8; 32] { static BUILD: OnceLock<[u8; 32]> = OnceLock::new(); BUILD.get_or_init(|| { let exe = std::env::current_exe().and_then(std::fs::read); + let limit = victory_shader::emit::loop_limit(); + if limit != victory_shader::emit::DEFAULT_LOOP_LIMIT { + tracing::warn!( + limit, + "VICTORY_SHADER_LOOP_LIMIT: translated loops stop sooner" + ); + } match exe { - Ok(bytes) => *blake3::hash(&bytes).as_bytes(), + Ok(mut bytes) => { + bytes.extend_from_slice(&limit.to_le_bytes()); + *blake3::hash(&bytes).as_bytes() + } Err(e) => { // Without it no entry can be trusted: a fresh key per run. tracing::warn!(error = %e, "cannot read our executable; shader cache unused"); diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index f8a1f5a..ca5e898 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -205,13 +205,14 @@ struct Lower<'a> { abort: Option, } -/// Iterations after which a loop is left (per entry into it). The console -/// runs a loop as long as its data says; data the host has not mirrored -/// yet (a null view reads zeros) can make that four billion iterations, -/// which hangs the host GPU until the driver resets it. A lane that -/// reaches the limit leaves the program as killed (a pixel discards). +/// Iterations after which a loop is left (per entry into it), unless +/// [`super::loop_limit`] says otherwise. The console runs a loop as long as +/// its data says; data the host has not mirrored yet (a null view reads +/// zeros) can make that four billion iterations, which hangs the host GPU +/// until the driver resets it. A lane that reaches the limit leaves the +/// program as killed (a pixel discards). // shortcut: a genuine loop of more iterations than this is cut short too. -const LOOP_LIMIT: u32 = 1 << 20; +pub(super) const LOOP_LIMIT: u32 = 1 << 20; /// Lane masks read as numbers (`s_bcnt1`, `s_ff1`, `v_mbcnt`, scalar /// compares, bit tests) are ballots of the lanes' booleans, taken where the @@ -6462,7 +6463,7 @@ impl<'a> Lower<'a> { let (phi, _) = self.counters[&to]; let one = self.s.m.i32(1); let next = self.s.m.bin(c, Bin::Add, phi, one); - let limit = self.s.m.i32(LOOP_LIMIT); + let limit = self.s.m.i32(super::loop_limit()); self.counters .get_mut(&to) .expect("header") diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index d974a65..0df1233 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -97,6 +97,44 @@ use crate::{ShaderError, io, ir, resource, structure}; pub use sampler::StaticSampler; pub use shader::{ROOT_CBV_SPACE, SAMPLER_SPACE, USER_DATA_SPACE}; +/// The loop limit without an override: 2^20. +pub const DEFAULT_LOOP_LIMIT: u32 = lower::LOOP_LIMIT; + +/// The iteration count after which a translated loop is left, per lane and +/// per entry into the loop: [`DEFAULT_LOOP_LIMIT`]. +/// +/// With the `diagnostics` feature, `VICTORY_SHADER_LOOP_LIMIT` (decimal or +/// `0x` hex, at least 1) sets it instead. A loop that hits the limit in +/// every lane of a full-screen pass still runs for seconds (2^20 times a +/// 4K screen is about 10^13 iterations); a much lower limit shows whether +/// such loops are what makes a frame slow. The value is part of the +/// translation, so the shader cache must key on it (`victory-gpu`'s does). +pub fn loop_limit() -> u32 { + static LIMIT: std::sync::OnceLock = std::sync::OnceLock::new(); + *LIMIT.get_or_init(|| { + if cfg!(feature = "diagnostics") { + parse_loop_limit(std::env::var("VICTORY_SHADER_LOOP_LIMIT").ok().as_deref()) + } else { + lower::LOOP_LIMIT + } + }) +} + +/// `VICTORY_SHADER_LOOP_LIMIT`'s value; the default when it is unset, +/// unreadable or 0. Pure, for testing. +fn parse_loop_limit(value: Option<&str>) -> u32 { + let parsed = value + .map(str::trim) + .and_then(|v| match v.strip_prefix("0x") { + Some(hex) => u32::from_str_radix(hex, 16).ok(), + None => v.parse().ok(), + }); + match parsed { + Some(n) if n > 0 => n, + _ => lower::LOOP_LIMIT, + } +} + /// A translated shader and what the host binds for it. #[derive(Debug, Clone, PartialEq, Eq)] pub struct HostShader { @@ -355,6 +393,19 @@ fn signature(part: &[u8]) -> Result, ShaderError> { #[cfg(test)] mod tests { use super::bitcode::Attr; + use super::lower::LOOP_LIMIT; + use super::parse_loop_limit; + + #[test] + fn the_loop_limit_switch_parses_or_falls_back() { + assert_eq!(parse_loop_limit(None), LOOP_LIMIT); + assert_eq!(parse_loop_limit(Some("4096")), 4096); + assert_eq!(parse_loop_limit(Some(" 0x1000 ")), 4096); + assert_eq!(parse_loop_limit(Some("1")), 1); + for bad in ["", "0", "-5", "lots", "0x", "4294967296"] { + assert_eq!(parse_loop_limit(Some(bad)), LOOP_LIMIT, "{bad:?}"); + } + } use super::container::SigElement; use super::shader::Shader; use super::*;