add flag for low power gpu as default (#397)

This commit is contained in:
lee-orr 2020-10-20 14:47:10 -04:00 committed by GitHub
parent 0dbba3efff
commit 0dba0fe45f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 6 deletions

View file

@ -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::<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)));
@ -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
}
}

View file

@ -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