Skip to content

Commit decf996

Browse files
committed
Use plugin setup for resource only used at setup time (#6360)
# Objective - Build on #6336 for more plugin configurations ## Solution - `LogSettings`, `ImageSettings` and `DefaultTaskPoolOptions` are now plugins settings rather than resources --- ## Changelog - `LogSettings` plugin settings have been move to `LogPlugin`, `ImageSettings` to `ImagePlugin` and `DefaultTaskPoolOptions` to `CorePlugin` ## Migration Guide The `LogSettings` settings have been moved from a resource to `LogPlugin` configuration: ```rust // Old (Bevy 0.8) app .insert_resource(LogSettings { level: Level::DEBUG, filter: "wgpu=error,bevy_render=info,bevy_ecs=trace".to_string(), }) .add_plugins(DefaultPlugins) // New (Bevy 0.9) app.add_plugins(DefaultPlugins.set(LogPlugin { level: Level::DEBUG, filter: "wgpu=error,bevy_render=info,bevy_ecs=trace".to_string(), })) ``` The `ImageSettings` settings have been moved from a resource to `ImagePlugin` configuration: ```rust // Old (Bevy 0.8) app .insert_resource(ImageSettings::default_nearest()) .add_plugins(DefaultPlugins) // New (Bevy 0.9) app.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) ``` The `DefaultTaskPoolOptions` settings have been moved from a resource to `CorePlugin::task_pool_options`: ```rust // Old (Bevy 0.8) app .insert_resource(DefaultTaskPoolOptions::with_num_threads(4)) .add_plugins(DefaultPlugins) // New (Bevy 0.9) app.add_plugins(DefaultPlugins.set(CorePlugin { task_pool_options: TaskPoolOptions::with_num_threads(4), })) ```
1 parent ee2c5a7 commit decf996

File tree

14 files changed

+79
-102
lines changed

14 files changed

+79
-102
lines changed

crates/bevy_asset/src/assets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ mod tests {
429429
#[uuid = "44115972-f31b-46e5-be5c-2b9aece6a52f"]
430430
struct MyAsset;
431431
let mut app = App::new();
432-
app.add_plugin(bevy_core::CorePlugin)
432+
app.add_plugin(bevy_core::CorePlugin::default())
433433
.add_plugin(crate::AssetPlugin::default());
434434
app.add_asset::<MyAsset>();
435435
let mut assets_before = app.world.resource_mut::<Assets<MyAsset>>();

crates/bevy_core/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use task_pool_options::*;
1212
pub mod prelude {
1313
//! The Bevy Core Prelude.
1414
#[doc(hidden)]
15-
pub use crate::{DefaultTaskPoolOptions, Name};
15+
pub use crate::{CorePlugin, Name, TaskPoolOptions};
1616
}
1717

1818
use bevy_app::prelude::*;
@@ -29,16 +29,15 @@ use bevy_tasks::tick_global_task_pools_on_main_thread;
2929

3030
/// Adds core functionality to Apps.
3131
#[derive(Default)]
32-
pub struct CorePlugin;
32+
pub struct CorePlugin {
33+
/// Options for the [`TaskPool`](bevy_tasks::TaskPool) created at application start.
34+
pub task_pool_options: TaskPoolOptions,
35+
}
3336

3437
impl Plugin for CorePlugin {
3538
fn build(&self, app: &mut App) {
3639
// Setup the default bevy task pools
37-
app.world
38-
.get_resource::<DefaultTaskPoolOptions>()
39-
.cloned()
40-
.unwrap_or_default()
41-
.create_default_pools();
40+
self.task_pool_options.create_default_pools();
4241

4342
#[cfg(not(target_arch = "wasm32"))]
4443
app.add_system_to_stage(
@@ -118,7 +117,7 @@ mod tests {
118117
#[test]
119118
fn runs_spawn_local_tasks() {
120119
let mut app = App::new();
121-
app.add_plugin(CorePlugin);
120+
app.add_plugin(CorePlugin::default());
122121

123122
let (async_tx, async_rx) = crossbeam_channel::unbounded();
124123
AsyncComputeTaskPool::get()

crates/bevy_core/src/task_pool_options.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ impl TaskPoolThreadAssignmentPolicy {
3232
}
3333

3434
/// Helper for configuring and creating the default task pools. For end-users who want full control,
35-
/// insert the default task pools into the resource map manually. If the pools are already inserted,
36-
/// this helper will do nothing.
35+
/// set up [`CorePlugin`](super::CorePlugin)
3736
#[derive(Clone, Resource)]
38-
pub struct DefaultTaskPoolOptions {
37+
pub struct TaskPoolOptions {
3938
/// If the number of physical cores is less than min_total_threads, force using
4039
/// min_total_threads
4140
pub min_total_threads: usize,
@@ -51,9 +50,9 @@ pub struct DefaultTaskPoolOptions {
5150
pub compute: TaskPoolThreadAssignmentPolicy,
5251
}
5352

54-
impl Default for DefaultTaskPoolOptions {
53+
impl Default for TaskPoolOptions {
5554
fn default() -> Self {
56-
DefaultTaskPoolOptions {
55+
TaskPoolOptions {
5756
// By default, use however many cores are available on the system
5857
min_total_threads: 1,
5958
max_total_threads: std::usize::MAX,
@@ -82,10 +81,10 @@ impl Default for DefaultTaskPoolOptions {
8281
}
8382
}
8483

85-
impl DefaultTaskPoolOptions {
84+
impl TaskPoolOptions {
8685
/// Create a configuration that forces using the given number of threads.
8786
pub fn with_num_threads(thread_count: usize) -> Self {
88-
DefaultTaskPoolOptions {
87+
TaskPoolOptions {
8988
min_total_threads: thread_count,
9089
max_total_threads: thread_count,
9190
..Default::default()

crates/bevy_internal/src/default_plugins.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bevy_app::{PluginGroup, PluginGroupBuilder};
2+
use bevy_render::texture::ImagePlugin;
23

34
/// This plugin group will add all the default plugins:
45
/// * [`LogPlugin`](bevy_log::LogPlugin)
@@ -59,7 +60,11 @@ impl PluginGroup for DefaultPlugins {
5960

6061
#[cfg(feature = "bevy_render")]
6162
{
62-
group = group.add(bevy_render::RenderPlugin::default());
63+
group = group
64+
.add(bevy_render::RenderPlugin::default())
65+
// NOTE: Load this after renderer initialization so that it knows about the supported
66+
// compressed texture formats
67+
.add(ImagePlugin::default());
6368
}
6469

6570
#[cfg(feature = "bevy_core_pipeline")]

crates/bevy_log/src/lib.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//! By default, the [`LogPlugin`] from this crate is included in Bevy's `DefaultPlugins`
99
//! and the logging macros can be used out of the box, if used.
1010
//!
11-
//! For more fine-tuned control over logging behavior, insert a [`LogSettings`] resource before
12-
//! adding [`LogPlugin`] or `DefaultPlugins` during app initialization.
11+
//! For more fine-tuned control over logging behavior, set up the [`LogPlugin`] or
12+
//! `DefaultPlugins` during app initialization.
1313
1414
#[cfg(feature = "trace")]
1515
use std::panic;
@@ -30,8 +30,6 @@ pub use bevy_utils::tracing::{
3030
Level,
3131
};
3232

33-
use bevy_ecs::prelude::Resource;
34-
3533
use bevy_app::{App, Plugin};
3634
use tracing_log::LogTracer;
3735
#[cfg(feature = "tracing-chrome")]
@@ -47,27 +45,26 @@ use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter};
4745
/// * Using [`tracing-wasm`](https://crates.io/crates/tracing-wasm) in WASM, logging
4846
/// to the browser console.
4947
///
50-
/// You can configure this plugin using the resource [`LogSettings`].
48+
/// You can configure this plugin.
5149
/// ```no_run
52-
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins};
53-
/// # use bevy_log::LogSettings;
50+
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup};
51+
/// # use bevy_log::LogPlugin;
5452
/// # use bevy_utils::tracing::Level;
5553
/// fn main() {
5654
/// App::new()
57-
/// .insert_resource(LogSettings {
55+
/// .add_plugins(DefaultPlugins.set(LogPlugin {
5856
/// level: Level::DEBUG,
5957
/// filter: "wgpu=error,bevy_render=info,bevy_ecs=trace".to_string(),
60-
/// })
61-
/// .add_plugins(DefaultPlugins)
58+
/// }))
6259
/// .run();
6360
/// }
6461
/// ```
6562
///
6663
/// Log level can also be changed using the `RUST_LOG` environment variable.
6764
/// For example, using `RUST_LOG=wgpu=error,bevy_render=info,bevy_ecs=trace cargo run ..`
6865
///
69-
/// It has the same syntax as the field [`LogSettings::filter`], see [`EnvFilter`].
70-
/// If you define the `RUST_LOG` environment variable, the [`LogSettings`] resource
66+
/// It has the same syntax as the field [`LogPlugin::filter`], see [`EnvFilter`].
67+
/// If you define the `RUST_LOG` environment variable, the [`LogPlugin`] settings
7168
/// will be ignored.
7269
///
7370
/// If you want to setup your own tracing collector, you should disable this
@@ -87,12 +84,7 @@ use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter};
8784
/// This plugin should not be added multiple times in the same process. This plugin
8885
/// sets up global logging configuration for **all** Apps in a given process, and
8986
/// rerunning the same initialization multiple times will lead to a panic.
90-
#[derive(Default)]
91-
pub struct LogPlugin;
92-
93-
/// `LogPlugin` settings
94-
#[derive(Resource)]
95-
pub struct LogSettings {
87+
pub struct LogPlugin {
9688
/// Filters logs using the [`EnvFilter`] format
9789
pub filter: String,
9890

@@ -101,7 +93,7 @@ pub struct LogSettings {
10193
pub level: Level,
10294
}
10395

104-
impl Default for LogSettings {
96+
impl Default for LogPlugin {
10597
fn default() -> Self {
10698
Self {
10799
filter: "wgpu=error".to_string(),
@@ -111,6 +103,7 @@ impl Default for LogSettings {
111103
}
112104

113105
impl Plugin for LogPlugin {
106+
#[cfg_attr(not(feature = "tracing-chrome"), allow(unused_variables))]
114107
fn build(&self, app: &mut App) {
115108
#[cfg(feature = "trace")]
116109
{
@@ -121,10 +114,7 @@ impl Plugin for LogPlugin {
121114
}));
122115
}
123116

124-
let default_filter = {
125-
let settings = app.world.get_resource_or_insert_with(LogSettings::default);
126-
format!("{},{}", settings.level, settings.filter)
127-
};
117+
let default_filter = { format!("{},{}", self.level, self.filter) };
128118
LogTracer::init().unwrap();
129119
let filter_layer = EnvFilter::try_from_default_env()
130120
.or_else(|_| EnvFilter::try_new(&default_filter))

crates/bevy_render/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub mod prelude {
3131
mesh::{shape, Mesh},
3232
render_resource::Shader,
3333
spatial_bundle::SpatialBundle,
34-
texture::{Image, ImageSettings},
34+
texture::{Image, ImagePlugin},
3535
view::{ComputedVisibility, Msaa, Visibility, VisibilityBundle},
3636
};
3737
}
@@ -49,7 +49,7 @@ use crate::{
4949
render_graph::RenderGraph,
5050
render_resource::{PipelineCache, Shader, ShaderLoader},
5151
renderer::{render_system, RenderInstance, RenderTextureFormat},
52-
texture::{BevyDefault, ImagePlugin},
52+
texture::BevyDefault,
5353
view::{ViewPlugin, WindowRenderPlugin},
5454
};
5555
use bevy_app::{App, AppLabel, Plugin};
@@ -348,9 +348,6 @@ impl Plugin for RenderPlugin {
348348
.add_plugin(CameraPlugin)
349349
.add_plugin(ViewPlugin)
350350
.add_plugin(MeshPlugin)
351-
// NOTE: Load this after renderer initialization so that it knows about the supported
352-
// compressed texture formats
353-
.add_plugin(ImagePlugin)
354351
.add_plugin(GlobalsPlugin)
355352
.add_plugin(FrameCountPlugin);
356353
}

crates/bevy_render/src/texture/image.rs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ pub struct Image {
113113
}
114114

115115
/// Used in [`Image`], this determines what image sampler to use when rendering. The default setting,
116-
/// [`ImageSampler::Default`], will read the sampler from the [`ImageSettings`] resource at runtime.
116+
/// [`ImageSampler::Default`], will read the sampler from the [`ImagePlugin`](super::ImagePlugin) at setup.
117117
/// Setting this to [`ImageSampler::Descriptor`] will override the global default descriptor for this [`Image`].
118118
#[derive(Debug, Default, Clone)]
119119
pub enum ImageSampler {
120-
/// Default image sampler, derived from the [`ImageSettings`] resource.
120+
/// Default image sampler, derived from the [`ImagePlugin`](super::ImagePlugin) setup.
121121
#[default]
122122
Default,
123123
/// Custom sampler for this image which will override global default.
@@ -158,41 +158,10 @@ impl ImageSampler {
158158
}
159159
}
160160

161-
/// Global resource for [`Image`] settings.
162-
///
163-
/// Can be set via `insert_resource` during app initialization to change the default settings.
164-
#[derive(Resource)]
165-
pub struct ImageSettings {
166-
/// The default image sampler to use when [`ImageSampler`] is set to `Default`.
167-
pub default_sampler: wgpu::SamplerDescriptor<'static>,
168-
}
169-
170-
impl Default for ImageSettings {
171-
fn default() -> Self {
172-
ImageSettings::default_linear()
173-
}
174-
}
175-
176-
impl ImageSettings {
177-
/// Creates image settings with linear sampling by default.
178-
pub fn default_linear() -> ImageSettings {
179-
ImageSettings {
180-
default_sampler: ImageSampler::linear_descriptor(),
181-
}
182-
}
183-
184-
/// Creates image settings with nearest sampling by default.
185-
pub fn default_nearest() -> ImageSettings {
186-
ImageSettings {
187-
default_sampler: ImageSampler::nearest_descriptor(),
188-
}
189-
}
190-
}
191-
192161
/// A rendering resource for the default image sampler which is set during renderer
193162
/// initialization.
194163
///
195-
/// The [`ImageSettings`] resource can be set during app initialization to change the default
164+
/// The [`ImagePlugin`](super::ImagePlugin) can be set during app initialization to change the default
196165
/// image sampler.
197166
#[derive(Resource, Debug, Clone, Deref, DerefMut)]
198167
pub struct DefaultImageSampler(pub(crate) Sampler);

crates/bevy_render/src/texture/mod.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,32 @@ use bevy_asset::{AddAsset, Assets};
3636

3737
// TODO: replace Texture names with Image names?
3838
/// Adds the [`Image`] as an asset and makes sure that they are extracted and prepared for the GPU.
39-
pub struct ImagePlugin;
39+
pub struct ImagePlugin {
40+
/// The default image sampler to use when [`ImageSampler`] is set to `Default`.
41+
pub default_sampler: wgpu::SamplerDescriptor<'static>,
42+
}
43+
44+
impl Default for ImagePlugin {
45+
fn default() -> Self {
46+
ImagePlugin::default_linear()
47+
}
48+
}
49+
50+
impl ImagePlugin {
51+
/// Creates image settings with linear sampling by default.
52+
pub fn default_linear() -> ImagePlugin {
53+
ImagePlugin {
54+
default_sampler: ImageSampler::linear_descriptor(),
55+
}
56+
}
57+
58+
/// Creates image settings with nearest sampling by default.
59+
pub fn default_nearest() -> ImagePlugin {
60+
ImagePlugin {
61+
default_sampler: ImageSampler::nearest_descriptor(),
62+
}
63+
}
64+
}
4065

4166
impl Plugin for ImagePlugin {
4267
fn build(&self, app: &mut App) {
@@ -66,15 +91,10 @@ impl Plugin for ImagePlugin {
6691
.resource_mut::<Assets<Image>>()
6792
.set_untracked(DEFAULT_IMAGE_HANDLE, Image::default());
6893

69-
let default_sampler = app
70-
.world
71-
.get_resource_or_insert_with(ImageSettings::default)
72-
.default_sampler
73-
.clone();
7494
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
7595
let default_sampler = {
7696
let device = render_app.world.resource::<RenderDevice>();
77-
device.create_sampler(&default_sampler)
97+
device.create_sampler(&self.default_sampler.clone())
7898
};
7999
render_app
80100
.insert_resource(DefaultImageSampler(default_sampler))

examples/2d/sprite_sheet.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use bevy::prelude::*;
55

66
fn main() {
77
App::new()
8-
.insert_resource(ImageSettings::default_nearest()) // prevents blurry sprites
9-
.add_plugins(DefaultPlugins)
8+
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) // prevents blurry sprites
109
.add_startup_system(setup)
1110
.add_system(animate_sprite)
1211
.run();

examples/2d/texture_atlas.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use bevy::{asset::LoadState, prelude::*};
66
fn main() {
77
App::new()
88
.init_resource::<RpgSpriteHandles>()
9-
.insert_resource(ImageSettings::default_nearest()) // prevents blurry sprites
10-
.add_plugins(DefaultPlugins)
9+
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) // prevents blurry sprites
1110
.add_state(AppState::Setup)
1211
.add_system_set(SystemSet::on_enter(AppState::Setup).with_system(load_textures))
1312
.add_system_set(SystemSet::on_update(AppState::Setup).with_system(check_textures))

examples/3d/3d_shapes.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use bevy::{
1010

1111
fn main() {
1212
App::new()
13-
.insert_resource(ImageSettings::default_nearest())
14-
.add_plugins(DefaultPlugins)
13+
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
1514
.add_startup_system(setup)
1615
.add_system(rotate)
1716
.run();

examples/app/logs.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use bevy::prelude::*;
44

55
fn main() {
66
App::new()
7-
// Uncomment this to override the default log settings:
8-
// .insert_resource(bevy::log::LogSettings {
9-
// level: bevy::log::Level::TRACE,
10-
// filter: "wgpu=warn,bevy_ecs=info".to_string(),
11-
// })
12-
.add_plugins(DefaultPlugins)
7+
.add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
8+
// Uncomment this to override the default log settings:
9+
// level: bevy::log::Level::TRACE,
10+
// filter: "wgpu=warn,bevy_ecs=info".to_string(),
11+
..default()
12+
}))
1313
.add_system(log_system)
1414
.run();
1515
}
@@ -24,7 +24,7 @@ fn log_system() {
2424
error!("something failed");
2525

2626
// by default, trace and debug logs are ignored because they are "noisy"
27-
// you can control what level is logged by adding the LogSettings resource
27+
// you can control what level is logged by setting up the LogPlugin
2828
// alternatively you can set the log level via the RUST_LOG=LEVEL environment variable
2929
// ex: RUST_LOG=trace, RUST_LOG=info,bevy_ecs=warn
3030
// the format used here is super flexible. check out this documentation for more info:

0 commit comments

Comments
 (0)