Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow wgpu backend to be configured from the environment #1042

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions crates/bevy_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,45 @@ pub fn get_wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut Worl

#[derive(Default, Clone)]
pub struct WgpuOptions {
backend: WgpuBackend,
power_pref: WgpuPowerOptions,
}

#[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("BEVY_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 Default for WgpuBackend {
fn default() -> Self {
Self::from_env()
}
}

#[derive(Clone)]
pub enum WgpuPowerOptions {
HighPerformance,
Expand Down
13 changes: 11 additions & 2 deletions crates/bevy_wgpu/src/wgpu_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext},
WgpuOptions, WgpuPowerOptions,
WgpuBackend, WgpuOptions, WgpuPowerOptions,
};
use bevy_app::prelude::*;
use bevy_ecs::{Resources, World};
Expand All @@ -22,7 +22,16 @@ pub struct WgpuRenderer {

impl WgpuRenderer {
pub async fn new(options: WgpuOptions) -> Self {
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let backend = match options.backend {
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