Move 'startup' Resource WgpuSettings into the RenderPlugin (#6946)
# Objective
The `WgpuSettings` resource is only used during plugin build. Move it into the `RenderPlugin` struct.
Changing these settings requires re-initializing the render context, which is currently not supported.
If it is supported in the future it should probably be more explicit than changing a field on a resource, maybe something similar to the `CreateWindow` event.
## Migration Guide
```rust
// Before (0.9)
App::new()
.insert_resource(WgpuSettings { .. })
.add_plugins(DefaultPlugins)
// After (0.10)
App::new()
.add_plugins(DefaultPlugins.set(RenderPlugin {
wgpu_settings: WgpuSettings { .. },
}))
```
Co-authored-by: devil-ira <justthecooldude@gmail.com>
This commit is contained in:
parent
0761594dd8
commit
15b19b930c
@ -46,6 +46,7 @@ use crate::{
|
|||||||
mesh::MeshPlugin,
|
mesh::MeshPlugin,
|
||||||
render_resource::{PipelineCache, Shader, ShaderLoader},
|
render_resource::{PipelineCache, Shader, ShaderLoader},
|
||||||
renderer::{render_system, RenderInstance},
|
renderer::{render_system, RenderInstance},
|
||||||
|
settings::WgpuSettings,
|
||||||
view::{ViewPlugin, WindowRenderPlugin},
|
view::{ViewPlugin, WindowRenderPlugin},
|
||||||
};
|
};
|
||||||
use bevy_app::{App, AppLabel, Plugin};
|
use bevy_app::{App, AppLabel, Plugin};
|
||||||
@ -59,7 +60,9 @@ use std::{
|
|||||||
|
|
||||||
/// Contains the default Bevy rendering backend based on wgpu.
|
/// Contains the default Bevy rendering backend based on wgpu.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct RenderPlugin;
|
pub struct RenderPlugin {
|
||||||
|
pub wgpu_settings: WgpuSettings,
|
||||||
|
}
|
||||||
|
|
||||||
/// The labels of the default App rendering stages.
|
/// The labels of the default App rendering stages.
|
||||||
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
|
||||||
@ -127,18 +130,12 @@ pub struct RenderApp;
|
|||||||
impl Plugin for RenderPlugin {
|
impl Plugin for RenderPlugin {
|
||||||
/// Initializes the renderer, sets up the [`RenderStage`](RenderStage) and creates the rendering sub-app.
|
/// Initializes the renderer, sets up the [`RenderStage`](RenderStage) and creates the rendering sub-app.
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
let options = app
|
|
||||||
.world
|
|
||||||
.get_resource::<settings::WgpuSettings>()
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
app.add_asset::<Shader>()
|
app.add_asset::<Shader>()
|
||||||
.add_debug_asset::<Shader>()
|
.add_debug_asset::<Shader>()
|
||||||
.init_asset_loader::<ShaderLoader>()
|
.init_asset_loader::<ShaderLoader>()
|
||||||
.init_debug_asset_loader::<ShaderLoader>();
|
.init_debug_asset_loader::<ShaderLoader>();
|
||||||
|
|
||||||
if let Some(backends) = options.backends {
|
if let Some(backends) = self.wgpu_settings.backends {
|
||||||
let windows = app.world.resource_mut::<bevy_window::Windows>();
|
let windows = app.world.resource_mut::<bevy_window::Windows>();
|
||||||
let instance = wgpu::Instance::new(backends);
|
let instance = wgpu::Instance::new(backends);
|
||||||
|
|
||||||
@ -151,13 +148,16 @@ impl Plugin for RenderPlugin {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let request_adapter_options = wgpu::RequestAdapterOptions {
|
let request_adapter_options = wgpu::RequestAdapterOptions {
|
||||||
power_preference: options.power_preference,
|
power_preference: self.wgpu_settings.power_preference,
|
||||||
compatible_surface: surface.as_ref(),
|
compatible_surface: surface.as_ref(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let (device, queue, adapter_info, render_adapter) = futures_lite::future::block_on(
|
let (device, queue, adapter_info, render_adapter) =
|
||||||
renderer::initialize_renderer(&instance, &options, &request_adapter_options),
|
futures_lite::future::block_on(renderer::initialize_renderer(
|
||||||
);
|
&instance,
|
||||||
|
&self.wgpu_settings,
|
||||||
|
&request_adapter_options,
|
||||||
|
));
|
||||||
debug!("Configured wgpu adapter Limits: {:#?}", device.limits());
|
debug!("Configured wgpu adapter Limits: {:#?}", device.limits());
|
||||||
debug!("Configured wgpu adapter Features: {:#?}", device.features());
|
debug!("Configured wgpu adapter Features: {:#?}", device.features());
|
||||||
app.insert_resource(device.clone())
|
app.insert_resource(device.clone())
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use bevy_ecs::system::Resource;
|
|
||||||
pub use wgpu::{Backends, Features as WgpuFeatures, Limits as WgpuLimits, PowerPreference};
|
pub use wgpu::{Backends, Features as WgpuFeatures, Limits as WgpuLimits, PowerPreference};
|
||||||
|
|
||||||
/// Configures the priority used when automatically configuring the features/limits of `wgpu`.
|
/// Configures the priority used when automatically configuring the features/limits of `wgpu`.
|
||||||
@ -23,7 +22,7 @@ pub enum WgpuSettingsPriority {
|
|||||||
/// NOTE: If you want to use [`Backends::GL`](Backends::GL) in a native app on Windows, you must
|
/// NOTE: If you want to use [`Backends::GL`](Backends::GL) in a native app on Windows, you must
|
||||||
/// use [`ANGLE`](https://github.com/gfx-rs/wgpu#angle). This is because wgpu requires EGL to
|
/// use [`ANGLE`](https://github.com/gfx-rs/wgpu#angle). This is because wgpu requires EGL to
|
||||||
/// create a GL context without a window and only ANGLE supports that.
|
/// create a GL context without a window and only ANGLE supports that.
|
||||||
#[derive(Resource, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct WgpuSettings {
|
pub struct WgpuSettings {
|
||||||
pub device_label: Option<Cow<'static, str>>,
|
pub device_label: Option<Cow<'static, str>>,
|
||||||
pub backends: Option<Backends>,
|
pub backends: Option<Backends>,
|
||||||
|
|||||||
@ -3,16 +3,17 @@
|
|||||||
use bevy::{
|
use bevy::{
|
||||||
pbr::wireframe::{Wireframe, WireframeConfig, WireframePlugin},
|
pbr::wireframe::{Wireframe, WireframeConfig, WireframePlugin},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
render::{render_resource::WgpuFeatures, settings::WgpuSettings},
|
render::{render_resource::WgpuFeatures, settings::WgpuSettings, RenderPlugin},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.insert_resource(WgpuSettings {
|
.add_plugins(DefaultPlugins.set(RenderPlugin {
|
||||||
|
wgpu_settings: WgpuSettings {
|
||||||
features: WgpuFeatures::POLYGON_MODE_LINE,
|
features: WgpuFeatures::POLYGON_MODE_LINE,
|
||||||
..default()
|
..default()
|
||||||
})
|
},
|
||||||
.add_plugins(DefaultPlugins)
|
}))
|
||||||
.add_plugin(WireframePlugin)
|
.add_plugin(WireframePlugin)
|
||||||
.add_startup_system(setup)
|
.add_startup_system(setup)
|
||||||
.run();
|
.run();
|
||||||
|
|||||||
@ -1,19 +1,23 @@
|
|||||||
use bevy::{
|
use bevy::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
render::settings::{WgpuSettings, WgpuSettingsPriority},
|
render::{
|
||||||
|
settings::{WgpuSettings, WgpuSettingsPriority},
|
||||||
|
RenderPlugin,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// the `bevy_main` proc_macro generates the required android boilerplate
|
// the `bevy_main` proc_macro generates the required android boilerplate
|
||||||
#[bevy_main]
|
#[bevy_main]
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
|
.add_plugins(DefaultPlugins.set(RenderPlugin {
|
||||||
// This configures the app to use the most compatible rendering settings.
|
// This configures the app to use the most compatible rendering settings.
|
||||||
// They help with compatibility with as many devices as possible.
|
// They help with compatibility with as many devices as possible.
|
||||||
.insert_resource(WgpuSettings {
|
wgpu_settings: WgpuSettings {
|
||||||
priority: WgpuSettingsPriority::Compatibility,
|
priority: WgpuSettingsPriority::Compatibility,
|
||||||
..default()
|
..default()
|
||||||
})
|
},
|
||||||
.add_plugins(DefaultPlugins)
|
}))
|
||||||
.add_startup_system(setup)
|
.add_startup_system(setup)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,14 +4,18 @@
|
|||||||
//!
|
//!
|
||||||
//! See also the `headless` example which does not display a window.
|
//! See also the `headless` example which does not display a window.
|
||||||
|
|
||||||
use bevy::{prelude::*, render::settings::WgpuSettings};
|
use bevy::{
|
||||||
|
prelude::*,
|
||||||
|
render::{settings::WgpuSettings, RenderPlugin},
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.insert_resource(WgpuSettings {
|
.add_plugins(DefaultPlugins.set(RenderPlugin {
|
||||||
|
wgpu_settings: WgpuSettings {
|
||||||
backends: None,
|
backends: None,
|
||||||
..default()
|
..default()
|
||||||
})
|
},
|
||||||
.add_plugins(DefaultPlugins)
|
}))
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user