From 3918a09a4b92d7b8570951fb3329c7051e6f316e Mon Sep 17 00:00:00 2001 From: Benjamin Frye Date: Fri, 13 Dec 2024 11:58:23 +1100 Subject: [PATCH] fix: Read environment variable configs in hello_triangle example (#6698) * 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" --- examples/src/framework.rs | 11 +---------- examples/src/hello_triangle/mod.rs | 2 +- wgpu/src/util/init.rs | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/examples/src/framework.rs b/examples/src/framework.rs index 77eaf07712..9b7c4368e1 100644 --- a/examples/src/framework.rs +++ b/examples/src/framework.rs @@ -268,16 +268,7 @@ impl ExampleContext { async fn init_async(surface: &mut SurfaceWrapper, window: Arc) -> 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( diff --git a/examples/src/hello_triangle/mod.rs b/examples/src/hello_triangle/mod.rs index 7c82d49cf0..27903795aa 100644 --- a/examples/src/hello_triangle/mod.rs +++ b/examples/src/hello_triangle/mod.rs @@ -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 diff --git a/wgpu/src/util/init.rs b/wgpu/src/util/init.rs index 87f787bcb8..b4ff99ef87 100644 --- a/wgpu/src/util/init.rs +++ b/wgpu/src/util/init.rs @@ -138,6 +138,22 @@ pub fn gles_minor_version_from_env() -> Option { ) } +/// 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.