diff --git a/README.md b/README.md index 4672921..49c4f92 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,8 @@ A release build has no debugging aids. Build them in with cargo build --profile diag -p victory --features diagnostics # target\diag\victory.exe ``` +The on/off switches below (`...=1`) are off when unset or set to `0`. + | Variable | Effect | |----------|--------| | `VICTORY_LOG==debug,...` | debug and trace output | diff --git a/crates/victory-gpu/src/capture.rs b/crates/victory-gpu/src/capture.rs index b4010f8..937f560 100644 --- a/crates/victory-gpu/src/capture.rs +++ b/crates/victory-gpu/src/capture.rs @@ -358,7 +358,7 @@ static TARGETS: std::sync::Mutex bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); - *ON.get_or_init(|| folder().is_some() && std::env::var_os("VICTORY_DUMP_TARGETS").is_some()) + *ON.get_or_init(|| folder().is_some() && crate::switch::on("VICTORY_DUMP_TARGETS")) } /// Notes a title texture the frame being built renders to or writes. diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 9c70c3f..edca8d4 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -553,8 +553,7 @@ fn create() -> windows::core::Result { // SAFETY: plain D3D12 object creation with valid descriptions. unsafe { // The debug layer only in builds with diagnostics. - let debug = - cfg!(feature = "diagnostics") && std::env::var_os("VICTORY_D3D12_DEBUG").is_some(); + let debug = cfg!(feature = "diagnostics") && crate::switch::on("VICTORY_D3D12_DEBUG"); if debug { let mut layer: Option = None; match D3D12GetDebugInterface(&mut layer) { @@ -565,7 +564,7 @@ fn create() -> windows::core::Result { // descriptor heap indices and the like become messages naming // the command instead of page faults. Without state tracking // (directly indexed textures are not transitioned). - if std::env::var_os("VICTORY_D3D12_GBV").is_some() + if crate::switch::on("VICTORY_D3D12_GBV") && let Some(l) = &layer { if let Ok(d1) = l.cast::() { @@ -594,7 +593,7 @@ fn create() -> windows::core::Result { // Diagnostics: the software adapter, on which a shader's wild access // is an access violation in this process (a crash report with the // address and instruction) instead of a GPU fault and driver reset. - if cfg!(feature = "diagnostics") && std::env::var_os("VICTORY_D3D12_WARP").is_some() { + if cfg!(feature = "diagnostics") && crate::switch::on("VICTORY_D3D12_WARP") { let adapter = warp_adapter()?; D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_12_0, &mut device)?; tracing::info!("host device on the software adapter (WARP)"); @@ -1326,9 +1325,7 @@ fn record_twin_copy( /// builds). pub(crate) fn dred_on() -> bool { static ON: OnceLock = OnceLock::new(); - *ON.get_or_init(|| { - cfg!(feature = "diagnostics") && std::env::var_os("VICTORY_D3D12_DRED").is_some() - }) + *ON.get_or_init(|| cfg!(feature = "diagnostics") && crate::switch::on("VICTORY_D3D12_DRED")) } /// Indirect mesh dispatches since the last frame line: the largest group @@ -1352,7 +1349,7 @@ fn card_budget() -> bool { #[cfg(feature = "diagnostics")] fn timing() -> bool { static ON: OnceLock = OnceLock::new(); - *ON.get_or_init(|| std::env::var_os("VICTORY_GPU_TIMING").is_some()) + *ON.get_or_init(|| crate::switch::on("VICTORY_GPU_TIMING")) } /// Names a host object for the device-removed report, which names the diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs index 420682d..e9a0cb2 100644 --- a/crates/victory-gpu/src/lib.rs +++ b/crates/victory-gpu/src/lib.rs @@ -63,6 +63,7 @@ mod shadow; #[cfg(windows)] mod tiles; pub mod signature; +mod switch; #[cfg(windows)] mod texture; #[cfg(windows)] diff --git a/crates/victory-gpu/src/switch.rs b/crates/victory-gpu/src/switch.rs new file mode 100644 index 0000000..f3ea9b9 --- /dev/null +++ b/crates/victory-gpu/src/switch.rs @@ -0,0 +1,45 @@ +//! The `VICTORY_*` on/off switches (`VICTORY_D3D12_DEBUG=1` and its siblings). + +use std::ffi::OsStr; + +/// Whether a switch's value turns it on: it is set, and is not `0`. +/// +/// A switch used to be on whenever its variable existed, so `VICTORY_D3D12_WARP=0` (typed to switch it off, or left +/// over from comparing two runs) still selected the software adapter. Any other value keeps the old meaning. +fn value_is_on(value: Option<&OsStr>) -> bool { + value.is_some_and(|v| v != "0") +} + +/// Whether the environment variable `name` switches something on. +pub(crate) fn on(name: &str) -> bool { + value_is_on(std::env::var_os(name).as_deref()) +} + +#[cfg(test)] +mod tests { + use super::value_is_on; + use std::ffi::OsStr; + + #[test] + fn unset_is_off() { + assert!(!value_is_on(None)); + } + + #[test] + fn zero_is_off() { + assert!(!value_is_on(Some(OsStr::new("0")))); + } + + #[test] + fn one_is_on() { + assert!(value_is_on(Some(OsStr::new("1")))); + } + + #[test] + fn other_values_keep_their_old_meaning() { + // Documented as `=1`; anything else that was on before stays on: only `0` is new. + for value in ["", "true", "yes", "2", "00", " 0", "0 "] { + assert!(value_is_on(Some(OsStr::new(value))), "{value:?}"); + } + } +}