Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Low power default option [#366] #397

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
lee-orr marked this conversation as resolved.
Show resolved Hide resolved
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