mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 14:40:19 +00:00
add flag for low power gpu as default (#397)
This commit is contained in:
parent
0dbba3efff
commit
0dba0fe45f
2 changed files with 35 additions and 6 deletions
|
@ -20,7 +20,7 @@ pub struct WgpuPlugin;
|
||||||
|
|
||||||
impl Plugin for WgpuPlugin {
|
impl Plugin for WgpuPlugin {
|
||||||
fn build(&self, app: &mut AppBuilder) {
|
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(
|
app.add_system_to_stage(
|
||||||
bevy_render::stage::RENDER,
|
bevy_render::stage::RENDER,
|
||||||
render_system.thread_local_system(),
|
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) {
|
pub fn get_wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut World, &mut Resources) {
|
||||||
let mut wgpu_renderer = future::block_on(WgpuRenderer::new());
|
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());
|
let resource_context = WgpuRenderResourceContext::new(wgpu_renderer.device.clone());
|
||||||
resources.insert::<Box<dyn RenderResourceContext>>(Box::new(resource_context.clone()));
|
resources.insert::<Box<dyn RenderResourceContext>>(Box::new(resource_context.clone()));
|
||||||
resources.insert(SharedBuffers::new(Box::new(resource_context)));
|
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);
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
use crate::renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext};
|
use crate::{
|
||||||
|
renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext},
|
||||||
|
WgpuOptions, WgpuPowerOptions,
|
||||||
|
};
|
||||||
use bevy_app::prelude::*;
|
use bevy_app::prelude::*;
|
||||||
use bevy_ecs::{Resources, World};
|
use bevy_ecs::{Resources, World};
|
||||||
use bevy_render::{
|
use bevy_render::{
|
||||||
|
@ -18,11 +21,16 @@ pub struct WgpuRenderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WgpuRenderer {
|
impl WgpuRenderer {
|
||||||
pub async fn new() -> Self {
|
pub async fn new(options: WgpuOptions) -> Self {
|
||||||
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
|
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
|
||||||
|
|
||||||
let adapter = instance
|
let adapter = instance
|
||||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
.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,
|
compatible_surface: None,
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
|
|
Loading…
Reference in a new issue