From ef9bb9edc6f809d179961a9cd8fd6a2987db8e8d Mon Sep 17 00:00:00 2001 From: leeorr Date: Sun, 30 Aug 2020 15:14:26 -0400 Subject: [PATCH] add flag for low power gpu as default --- crates/bevy_wgpu/src/lib.rs | 27 ++++++++++++++++++++++++--- crates/bevy_wgpu/src/wgpu_renderer.rs | 14 +++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/crates/bevy_wgpu/src/lib.rs b/crates/bevy_wgpu/src/lib.rs index 430ab6095945d..4c38dcdeda4ae 100644 --- a/crates/bevy_wgpu/src/lib.rs +++ b/crates/bevy_wgpu/src/lib.rs @@ -20,7 +20,7 @@ pub struct WgpuPlugin; impl Plugin for WgpuPlugin { fn build(&self, app: &mut AppBuilder) { - let render_system = wgpu_render_system(app.resources_mut()); + let render_system = get_wgpu_render_system(app.resources_mut()); app.add_system_to_stage( bevy_render::stage::RENDER, render_system.thread_local_system(), @@ -32,8 +32,11 @@ impl Plugin for WgpuPlugin { } } -pub fn wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut World, &mut Resources) { - let mut wgpu_renderer = future::block_on(WgpuRenderer::new()); +pub fn get_wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut World, &mut Resources) { + let options = resources + .get_cloned::() + .unwrap_or_else(WgpuOptions::default); + let mut wgpu_renderer = future::block_on(WgpuRenderer::new(options)); let resource_context = WgpuRenderResourceContext::new(wgpu_renderer.device.clone()); resources.insert::>(Box::new(resource_context.clone())); resources.insert(SharedBuffers::new(Box::new(resource_context))); @@ -41,3 +44,21 @@ pub fn wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut World, & wgpu_renderer.update(world, resources); } } + +#[derive(Default, Clone)] +pub struct WgpuOptions { + power_pref: WgpuPowerOptions, +} + +#[derive(Clone)] +pub enum WgpuPowerOptions { + HighPerformance, + Adaptive, + LowPower, +} + +impl Default for WgpuPowerOptions { + fn default() -> Self { + WgpuPowerOptions::HighPerformance + } +} diff --git a/crates/bevy_wgpu/src/wgpu_renderer.rs b/crates/bevy_wgpu/src/wgpu_renderer.rs index 31803de4b21f5..964cf2028f933 100644 --- a/crates/bevy_wgpu/src/wgpu_renderer.rs +++ b/crates/bevy_wgpu/src/wgpu_renderer.rs @@ -1,4 +1,7 @@ -use crate::renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext}; +use crate::{ + renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext}, + WgpuOptions, WgpuPowerOptions, +}; use bevy_app::prelude::*; use bevy_ecs::{Resources, World}; use bevy_render::{ @@ -18,11 +21,16 @@ pub struct WgpuRenderer { } impl WgpuRenderer { - pub async fn new() -> Self { + pub async fn new(options: WgpuOptions) -> Self { let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY); + let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::HighPerformance, + power_preference: match options.power_pref { + WgpuPowerOptions::HighPerformance => wgpu::PowerPreference::HighPerformance, + WgpuPowerOptions::Adaptive => wgpu::PowerPreference::Default, + WgpuPowerOptions::LowPower => wgpu::PowerPreference::LowPower, + }, compatible_surface: None, }) .await