Skip to content

Commit

Permalink
add flag for low power gpu as default (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-orr authored Oct 20, 2020
1 parent 0dbba3e commit 0dba0fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
27 changes: 24 additions & 3 deletions crates/bevy_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -32,12 +32,33 @@ 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::<WgpuOptions>()
.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<dyn RenderResourceContext>>(Box::new(resource_context.clone()));
resources.insert(SharedBuffers::new(Box::new(resource_context)));
move |world, resources| {
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
}
}
14 changes: 11 additions & 3 deletions crates/bevy_wgpu/src/wgpu_renderer.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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
Expand Down

0 comments on commit 0dba0fe

Please sign in to comment.