Skip to content

Commit

Permalink
Support choosing wgpu backend using env var
Browse files Browse the repository at this point in the history
  • Loading branch information
Cupnfish authored and hecrj committed Mar 25, 2021
1 parent 2b520ca commit ab8dcf9
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,53 @@ pub struct Compositor {
local_pool: futures::executor::LocalPool,
}

#[derive(Clone)]
pub enum WgpuBackend {
Auto,
Vulkan,
Metal,
Dx12,
Dx11,
Gl,
BrowserWgpu,
}

impl WgpuBackend {
fn from_env() -> Self {
if let Ok(backend) = std::env::var("WGPU_BACKEND") {
match backend.to_lowercase().as_str() {
"vulkan" => WgpuBackend::Vulkan,
"metal" => WgpuBackend::Metal,
"dx12" => WgpuBackend::Dx12,
"dx11" => WgpuBackend::Dx11,
"gl" => WgpuBackend::Gl,
"webgpu" => WgpuBackend::BrowserWgpu,
other => panic!("Unknown backend: {}", other),
}
} else {
WgpuBackend::Auto
}
}
}

impl Compositor {
const CHUNK_SIZE: u64 = 10 * 1024;

/// Requests a new [`Compositor`] with the given [`Settings`].
///
/// Returns `None` if no compatible graphics adapter could be found.
pub async fn request(settings: Settings) -> Option<Self> {
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let backend = match WgpuBackend::from_env() {
WgpuBackend::Auto => wgpu::BackendBit::PRIMARY,
WgpuBackend::Vulkan => wgpu::BackendBit::VULKAN,
WgpuBackend::Metal => wgpu::BackendBit::METAL,
WgpuBackend::Dx12 => wgpu::BackendBit::DX12,
WgpuBackend::Dx11 => wgpu::BackendBit::DX11,
WgpuBackend::Gl => wgpu::BackendBit::GL,
WgpuBackend::BrowserWgpu => wgpu::BackendBit::BROWSER_WEBGPU,
};

let instance = wgpu::Instance::new(backend);

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand Down

0 comments on commit ab8dcf9

Please sign in to comment.