Skip to content

Commit

Permalink
fix: Read environment variable configs in hello_triangle example (#6698)
Browse files Browse the repository at this point in the history
* fix: Read environment variable configs in hello_triangle example

* refactor: Add util method for creating entire instance descriptor from env

* fix: Fix clippy error/warning about "unneeded `return` statement"
  • Loading branch information
fryeb authored Dec 13, 2024
1 parent 8f82992 commit 3918a09
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
11 changes: 1 addition & 10 deletions examples/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,7 @@ impl ExampleContext {
async fn init_async<E: Example>(surface: &mut SurfaceWrapper, window: Arc<Window>) -> Self {
log::info!("Initializing wgpu...");

let backends = wgpu::util::backend_bits_from_env().unwrap_or_default();
let dx12_shader_compiler = wgpu::util::dx12_shader_compiler_from_env().unwrap_or_default();
let gles_minor_version = wgpu::util::gles_minor_version_from_env().unwrap_or_default();

let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
flags: wgpu::InstanceFlags::from_build_config().with_env(),
dx12_shader_compiler,
gles_minor_version,
});
let instance = wgpu::Instance::new(wgpu::util::instance_descriptor_from_env());
surface.pre_adapter(&instance, window);

let adapter = get_adapter_with_capabilities_or_from_env(
Expand Down
2 changes: 1 addition & 1 deletion examples/src/hello_triangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
size.width = size.width.max(1);
size.height = size.height.max(1);

let instance = wgpu::Instance::default();
let instance = wgpu::Instance::new(wgpu::util::instance_descriptor_from_env());

let surface = instance.create_surface(&window).unwrap();
let adapter = instance
Expand Down
16 changes: 16 additions & 0 deletions wgpu/src/util/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ pub fn gles_minor_version_from_env() -> Option<wgt::Gles3MinorVersion> {
)
}

/// Get an instance descriptor from the following environment variables
/// - WGPU_BACKEND
/// - WGPU_DEBUG
/// - WGPU_VALIDATION
/// - WGPU_DX12_COMPILER
/// - WGPU_GLES_MINOR_VERSION
/// If variables are missing, falls back to default or build config values
pub fn instance_descriptor_from_env() -> wgt::InstanceDescriptor {
wgt::InstanceDescriptor {
backends: backend_bits_from_env().unwrap_or_default(),
flags: wgt::InstanceFlags::from_build_config().with_env(),
dx12_shader_compiler: dx12_shader_compiler_from_env().unwrap_or_default(),
gles_minor_version: gles_minor_version_from_env().unwrap_or_default(),
}
}

/// Determines whether the [`Backends::BROWSER_WEBGPU`] backend is supported.
///
/// The result can only be true if this is called from the main thread or a dedicated worker.
Expand Down

0 comments on commit 3918a09

Please sign in to comment.