Skip to content

Commit

Permalink
Expose wgpu backend in WgpuOptions and allow it to be configured from…
Browse files Browse the repository at this point in the history
… the environment
  • Loading branch information
agorgl committed Dec 17, 2020
1 parent 841755a commit 18a454a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
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,
WgpuOptions, WgpuBackend, 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

0 comments on commit 18a454a

Please sign in to comment.