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"
This commit is contained in:
Benjamin Frye authored and GitHub committed 2024-12-12 19:58:23 -05:00
1 parent 8f82992b9f
commit 3918a09a4b
3 files changed
+18 -11

No files matched your search

+1 -10
View File
@@ -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(
+1 -1
View File
@@ -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
+16
View File
@@ -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.