Skip to content

Commit

Permalink
Allow overriding validation and debug flags via an env var
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed Oct 11, 2023
1 parent e87f859 commit 38a886a
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,13 +841,43 @@ impl InstanceFlags {
/// Infer good defaults from the build type
///
/// Returns the default flags and add debugging flags if the build configuration has `debug_assertions`.
pub fn from_build_type() -> Self {
pub fn from_build_config() -> Self {
if cfg!(debug_assertions) {
return InstanceFlags::debugging();
}

InstanceFlags::default()
}

/// Returns this set of flags, affected by environment variables.
///
/// The presence of an environment variable implies that the corresponding flag should be set
/// unless the value is "0" in which case the flag is unset. If the environment variable is
/// not present, then the flag is unaffected.
///
/// For example `let flags = InstanceFlags::debuggin().with_env();` with `WGPU_VALIDATION=0`
/// does not contain `InstanceFlags::VALIDATION`.
///
/// The environment variables are named after the flags prefixed with "WGPU_". For example:
/// - WGPU_DEBUG
/// - WGPU_VALIDATION
pub fn with_env(mut self) -> Self {
fn env(key: &str) -> Option<bool> {
std::env::var(key).ok().map(|s| match s.as_str() {
"0" => false,
_ => true,
})
}

if let Some(bit) = env("WGPU_VALIDATION") {
self.set(Self::VALIDATION, bit);
}
if let Some(bit) = env("WGPU_DEBUG") {
self.set(Self::DEBUG, bit);
}

self
}
}

/// Represents the sets of limits an adapter/device supports.
Expand Down

0 comments on commit 38a886a

Please sign in to comment.