Skip to content

Commit

Permalink
Expose instance flags in the instance descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed Oct 11, 2023
1 parent 09dd1d5 commit 29562ee
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/common/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ async fn setup<E: Example>(title: &str) -> Setup {

let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
flags: wgpu::InstanceFlags::from_build_type(),
dx12_shader_compiler,
gles_minor_version,
});
Expand Down
1 change: 1 addition & 0 deletions examples/timestamp-queries/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ async fn run() {
let backends = wgpu::util::backend_bits_from_env().unwrap_or_else(wgpu::Backends::all);
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
flags: wgpu::InstanceFlags::from_build_type(),
dx12_shader_compiler: wgpu::Dx12Compiler::default(),
gles_minor_version: wgpu::Gles3MinorVersion::default(),
});
Expand Down
1 change: 1 addition & 0 deletions player/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl Corpus {
IdentityPassThroughFactory,
wgt::InstanceDescriptor {
backends: corpus.backends,
flags: wgt::InstanceFlags::debugging(),
dx12_shader_compiler: wgt::Dx12Compiler::Fxc,
gles_minor_version: wgt::Gles3MinorVersion::default(),
},
Expand Down
1 change: 1 addition & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ pub fn initialize_instance() -> Instance {
let gles_minor_version = wgpu::util::gles_minor_version_from_env().unwrap_or_default();
Instance::new(wgpu::InstanceDescriptor {
backends,
flags: wgpu::InstanceFlags::debugging(),
dx12_shader_compiler,
gles_minor_version,
})
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use wasm_bindgen_test::*;
fn initialize() {
let _ = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::util::backend_bits_from_env().unwrap_or_else(wgpu::Backends::all),
flags: wgpu::InstanceFlags::debugging(),
dx12_shader_compiler: wgpu::util::dx12_shader_compiler_from_env().unwrap_or_default(),
gles_minor_version: wgpu::util::gles_minor_version_from_env().unwrap_or_default(),
});
Expand All @@ -13,6 +14,7 @@ fn initialize() {
fn request_adapter_inner(power: wgt::PowerPreference) {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::util::backend_bits_from_env().unwrap_or_else(wgpu::Backends::all),
flags: wgpu::InstanceFlags::debugging(),
dx12_shader_compiler: wgpu::util::dx12_shader_compiler_from_env().unwrap_or_default(),
gles_minor_version: wgpu::util::gles_minor_version_from_env().unwrap_or_default(),
});
Expand Down
7 changes: 1 addition & 6 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,9 @@ impl Instance {
pub fn new(name: &str, instance_desc: wgt::InstanceDescriptor) -> Self {
fn init<A: HalApi>(_: A, instance_desc: &wgt::InstanceDescriptor) -> Option<A::Instance> {
if instance_desc.backends.contains(A::VARIANT.into()) {
let mut flags = wgt::InstanceFlags::empty();
if cfg!(debug_assertions) {
flags |= wgt::InstanceFlags::VALIDATION;
flags |= wgt::InstanceFlags::DEBUG;
}
let hal_desc = hal::InstanceDescriptor {
name: "wgpu",
flags,
flags: instance_desc.flags,
dx12_shader_compiler: instance_desc.dx12_shader_compiler.clone(),
gles_minor_version: instance_desc.gles_minor_version,
};
Expand Down
21 changes: 21 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,24 @@ bitflags::bitflags! {
}
}

impl InstanceFlags {
/// Enable debugging and validation flags.
pub fn debugging() -> Self {
InstanceFlags::default() | InstanceFlags::DEBUG | InstanceFlags::VALIDATION
}

/// 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 {
if cfg!(debug_assertions) {
return InstanceFlags::debugging();
}

InstanceFlags::default()
}
}

/// Represents the sets of limits an adapter/device supports.
///
/// We provide three different defaults.
Expand Down Expand Up @@ -6510,6 +6528,8 @@ pub enum Gles3MinorVersion {
pub struct InstanceDescriptor {
/// Which `Backends` to enable.
pub backends: Backends,
/// Flags to tune the behavior of the instance.
pub flags: InstanceFlags,
/// Which DX12 shader compiler to use.
pub dx12_shader_compiler: Dx12Compiler,
/// Which OpenGL ES 3 minor version to request.
Expand All @@ -6520,6 +6540,7 @@ impl Default for InstanceDescriptor {
fn default() -> Self {
Self {
backends: Backends::all(),
flags: InstanceFlags::default(),
dx12_shader_compiler: Dx12Compiler::default(),
gles_minor_version: Gles3MinorVersion::default(),
}
Expand Down

0 comments on commit 29562ee

Please sign in to comment.