Expose wgpu backend in WgpuOptions and allow it to be configured from the environment (#1042)

This commit is contained in:
Agorgianitis Loukas 2020-12-22 21:31:01 +02:00 committed by GitHub
parent 3b2c6ce49b
commit dd668c1970
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 2 deletions

View file

@ -44,9 +44,45 @@ pub fn get_wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut Worl
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct WgpuOptions { pub struct WgpuOptions {
backend: WgpuBackend,
power_pref: WgpuPowerOptions, 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)] #[derive(Clone)]
pub enum WgpuPowerOptions { pub enum WgpuPowerOptions {
HighPerformance, HighPerformance,

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext}, renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext},
WgpuOptions, WgpuPowerOptions, WgpuBackend, WgpuOptions, WgpuPowerOptions,
}; };
use bevy_app::prelude::*; use bevy_app::prelude::*;
use bevy_ecs::{Resources, World}; use bevy_ecs::{Resources, World};
@ -22,7 +22,16 @@ pub struct WgpuRenderer {
impl WgpuRenderer { impl WgpuRenderer {
pub async fn new(options: WgpuOptions) -> Self { 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 let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions { .request_adapter(&wgpu::RequestAdapterOptions {