Skip to content

Commit 329b71f

Browse files
committed
Break CorePlugin into TaskPoolPlugin, TypeRegistrationPlugin, FrameCountPlugin. (#7083)
# Objective - Fixes #7081. ## Solution - Moved functionality from kitchen sink plugin `CorePlugin` to separate plugins, `TaskPoolPlugin`, `TypeRegistrationPlugin`, `FrameCountPlugin`. `TaskPoolOptions` resource should now be used with `TaskPoolPlugin`. ## Changelog Minimal changes made (code kept in `bevy_core/lib.rs`). ## Migration Guide - `CorePlugin` broken into separate plugins. If not using `DefaultPlugins` or `MinimalPlugins` `PluginGroup`s, the replacement for `CorePlugin` is now to add `TaskPoolPlugin`, `TypeRegistrationPlugin`, and `FrameCountPlugin` to the app. ## Notes - Consistent with Bevy goal "modularity over deep integration" but the functionality of `TypeRegistrationPlugin` and `FrameCountPlugin` is weak (the code has to go somewhere, though!). - No additional tests written.
1 parent 85743ce commit 329b71f

File tree

5 files changed

+58
-28
lines changed

5 files changed

+58
-28
lines changed

crates/bevy_asset/src/assets.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ mod tests {
493493
#[uuid = "44115972-f31b-46e5-be5c-2b9aece6a52f"]
494494
struct MyAsset;
495495
let mut app = App::new();
496-
app.add_plugin(bevy_core::CorePlugin::default())
496+
app.add_plugin(bevy_core::TaskPoolPlugin::default())
497+
.add_plugin(bevy_core::TypeRegistrationPlugin::default())
497498
.add_plugin(crate::AssetPlugin::default());
498499
app.add_asset::<MyAsset>();
499500
let mut assets_before = app.world.resource_mut::<Assets<MyAsset>>();

crates/bevy_core/src/lib.rs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub use task_pool_options::*;
1414
pub mod prelude {
1515
//! The Bevy Core Prelude.
1616
#[doc(hidden)]
17-
pub use crate::{CorePlugin, Name, TaskPoolOptions};
17+
pub use crate::{
18+
FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin,
19+
};
1820
}
1921

2022
use bevy_app::prelude::*;
@@ -31,31 +33,16 @@ use bevy_ecs::schedule::IntoSystemDescriptor;
3133
#[cfg(not(target_arch = "wasm32"))]
3234
use bevy_tasks::tick_global_task_pools_on_main_thread;
3335

34-
/// Adds core functionality to Apps.
36+
/// Registration of default types to the `TypeRegistry` reesource.
3537
#[derive(Default)]
36-
pub struct CorePlugin {
37-
/// Options for the [`TaskPool`](bevy_tasks::TaskPool) created at application start.
38-
pub task_pool_options: TaskPoolOptions,
39-
}
38+
pub struct TypeRegistrationPlugin;
4039

41-
impl Plugin for CorePlugin {
40+
impl Plugin for TypeRegistrationPlugin {
4241
fn build(&self, app: &mut App) {
43-
// Setup the default bevy task pools
44-
self.task_pool_options.create_default_pools();
45-
46-
#[cfg(not(target_arch = "wasm32"))]
47-
app.add_system_to_stage(
48-
bevy_app::CoreStage::Last,
49-
tick_global_task_pools_on_main_thread.at_end(),
50-
);
51-
5242
app.register_type::<Entity>().register_type::<Name>();
5343

5444
register_rust_types(app);
5545
register_math_types(app);
56-
57-
app.init_resource::<FrameCount>();
58-
app.add_system(update_frame_count);
5946
}
6047
}
6148

@@ -107,12 +94,43 @@ fn register_math_types(app: &mut App) {
10794
.register_type::<bevy_math::Quat>();
10895
}
10996

97+
/// Setup of default task pools: `AsyncComputeTaskPool`, `ComputeTaskPool`, `IoTaskPool`.
98+
#[derive(Default)]
99+
pub struct TaskPoolPlugin {
100+
/// Options for the [`TaskPool`](bevy_tasks::TaskPool) created at application start.
101+
pub task_pool_options: TaskPoolOptions,
102+
}
103+
104+
impl Plugin for TaskPoolPlugin {
105+
fn build(&self, app: &mut App) {
106+
// Setup the default bevy task pools
107+
self.task_pool_options.create_default_pools();
108+
109+
#[cfg(not(target_arch = "wasm32"))]
110+
app.add_system_to_stage(
111+
bevy_app::CoreStage::Last,
112+
tick_global_task_pools_on_main_thread.at_end(),
113+
);
114+
}
115+
}
116+
110117
/// Keeps a count of rendered frames since the start of the app
111118
///
112119
/// Wraps to 0 when it reaches the maximum u32 value
113120
#[derive(Default, Resource, Clone, Copy)]
114121
pub struct FrameCount(pub u32);
115122

123+
/// Adds frame counting functionality to Apps.
124+
#[derive(Default)]
125+
pub struct FrameCountPlugin;
126+
127+
impl Plugin for FrameCountPlugin {
128+
fn build(&self, app: &mut App) {
129+
app.init_resource::<FrameCount>();
130+
app.add_system(update_frame_count);
131+
}
132+
}
133+
116134
fn update_frame_count(mut frame_count: ResMut<FrameCount>) {
117135
frame_count.0 = frame_count.0.wrapping_add(1);
118136
}
@@ -125,7 +143,8 @@ mod tests {
125143
#[test]
126144
fn runs_spawn_local_tasks() {
127145
let mut app = App::new();
128-
app.add_plugin(CorePlugin::default());
146+
app.add_plugin(TaskPoolPlugin::default());
147+
app.add_plugin(TypeRegistrationPlugin::default());
129148

130149
let (async_tx, async_rx) = crossbeam_channel::unbounded();
131150
AsyncComputeTaskPool::get()
@@ -158,7 +177,9 @@ mod tests {
158177
#[test]
159178
fn frame_counter_update() {
160179
let mut app = App::new();
161-
app.add_plugin(CorePlugin::default());
180+
app.add_plugin(TaskPoolPlugin::default());
181+
app.add_plugin(TypeRegistrationPlugin::default());
182+
app.add_plugin(FrameCountPlugin::default());
162183
app.update();
163184

164185
let frame_count = app.world.resource::<FrameCount>();

crates/bevy_core/src/task_pool_options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl TaskPoolThreadAssignmentPolicy {
3232
}
3333

3434
/// Helper for configuring and creating the default task pools. For end-users who want full control,
35-
/// set up [`CorePlugin`](super::CorePlugin)
35+
/// set up [`TaskPoolPlugin`](super::TaskPoolPlugin)
3636
#[derive(Clone, Resource)]
3737
pub struct TaskPoolOptions {
3838
/// If the number of physical cores is less than min_total_threads, force using

crates/bevy_internal/src/default_plugins.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use bevy_app::{PluginGroup, PluginGroupBuilder};
22

33
/// This plugin group will add all the default plugins:
44
/// * [`LogPlugin`](crate::log::LogPlugin)
5-
/// * [`CorePlugin`](crate::core::CorePlugin)
5+
/// * [`TaskPoolPlugin`](crate::core::TaskPoolPlugin)
6+
/// * [`TypeRegistrationPlugin`](crate::core::TypeRegistrationPlugin)
7+
/// * [`FrameCountPlugin`](crate::core::FrameCountPlugin)
68
/// * [`TimePlugin`](crate::time::TimePlugin)
79
/// * [`TransformPlugin`](crate::transform::TransformPlugin)
810
/// * [`HierarchyPlugin`](crate::hierarchy::HierarchyPlugin)
@@ -29,7 +31,9 @@ impl PluginGroup for DefaultPlugins {
2931
let mut group = PluginGroupBuilder::start::<Self>();
3032
group = group
3133
.add(bevy_log::LogPlugin::default())
32-
.add(bevy_core::CorePlugin::default())
34+
.add(bevy_core::TaskPoolPlugin::default())
35+
.add(bevy_core::TypeRegistrationPlugin::default())
36+
.add(bevy_core::FrameCountPlugin::default())
3337
.add(bevy_time::TimePlugin::default())
3438
.add(bevy_transform::TransformPlugin::default())
3539
.add(bevy_hierarchy::HierarchyPlugin::default())
@@ -118,7 +122,9 @@ impl PluginGroup for DefaultPlugins {
118122
}
119123

120124
/// Minimal plugin group that will add the following plugins:
121-
/// * [`CorePlugin`](crate::core::CorePlugin)
125+
/// * [`TaskPoolPlugin`](crate::core::TaskPoolPlugin)
126+
/// * [`TypeRegistrationPlugin`](crate::core::TypeRegistrationPlugin)
127+
/// * [`FrameCountPlugin`](crate::core::FrameCountPlugin)
122128
/// * [`TimePlugin`](crate::time::TimePlugin)
123129
/// * [`ScheduleRunnerPlugin`](crate::app::ScheduleRunnerPlugin)
124130
///
@@ -128,7 +134,9 @@ pub struct MinimalPlugins;
128134
impl PluginGroup for MinimalPlugins {
129135
fn build(self) -> PluginGroupBuilder {
130136
PluginGroupBuilder::start::<Self>()
131-
.add(bevy_core::CorePlugin::default())
137+
.add(bevy_core::TaskPoolPlugin::default())
138+
.add(bevy_core::TypeRegistrationPlugin::default())
139+
.add(bevy_core::FrameCountPlugin::default())
132140
.add(bevy_time::TimePlugin::default())
133141
.add(bevy_app::ScheduleRunnerPlugin::default())
134142
}

examples/app/thread_pool_resources.rs

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

66
fn main() {
77
App::new()
8-
.add_plugins(DefaultPlugins.set(CorePlugin {
8+
.add_plugins(DefaultPlugins.set(TaskPoolPlugin {
99
task_pool_options: TaskPoolOptions::with_num_threads(4),
1010
}))
1111
.run();

0 commit comments

Comments
 (0)