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.
This commit is contained in:
targrub 2023-01-05 11:42:35 +00:00
parent 85743ce49e
commit 329b71fa62
5 changed files with 58 additions and 28 deletions

View File

@ -493,7 +493,8 @@ mod tests {
#[uuid = "44115972-f31b-46e5-be5c-2b9aece6a52f"] #[uuid = "44115972-f31b-46e5-be5c-2b9aece6a52f"]
struct MyAsset; struct MyAsset;
let mut app = App::new(); let mut app = App::new();
app.add_plugin(bevy_core::CorePlugin::default()) app.add_plugin(bevy_core::TaskPoolPlugin::default())
.add_plugin(bevy_core::TypeRegistrationPlugin::default())
.add_plugin(crate::AssetPlugin::default()); .add_plugin(crate::AssetPlugin::default());
app.add_asset::<MyAsset>(); app.add_asset::<MyAsset>();
let mut assets_before = app.world.resource_mut::<Assets<MyAsset>>(); let mut assets_before = app.world.resource_mut::<Assets<MyAsset>>();

View File

@ -14,7 +14,9 @@ pub use task_pool_options::*;
pub mod prelude { pub mod prelude {
//! The Bevy Core Prelude. //! The Bevy Core Prelude.
#[doc(hidden)] #[doc(hidden)]
pub use crate::{CorePlugin, Name, TaskPoolOptions}; pub use crate::{
FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin,
};
} }
use bevy_app::prelude::*; use bevy_app::prelude::*;
@ -31,31 +33,16 @@ use bevy_ecs::schedule::IntoSystemDescriptor;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
use bevy_tasks::tick_global_task_pools_on_main_thread; use bevy_tasks::tick_global_task_pools_on_main_thread;
/// Adds core functionality to Apps. /// Registration of default types to the `TypeRegistry` reesource.
#[derive(Default)] #[derive(Default)]
pub struct CorePlugin { pub struct TypeRegistrationPlugin;
/// Options for the [`TaskPool`](bevy_tasks::TaskPool) created at application start.
pub task_pool_options: TaskPoolOptions,
}
impl Plugin for CorePlugin { impl Plugin for TypeRegistrationPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
// Setup the default bevy task pools
self.task_pool_options.create_default_pools();
#[cfg(not(target_arch = "wasm32"))]
app.add_system_to_stage(
bevy_app::CoreStage::Last,
tick_global_task_pools_on_main_thread.at_end(),
);
app.register_type::<Entity>().register_type::<Name>(); app.register_type::<Entity>().register_type::<Name>();
register_rust_types(app); register_rust_types(app);
register_math_types(app); register_math_types(app);
app.init_resource::<FrameCount>();
app.add_system(update_frame_count);
} }
} }
@ -107,12 +94,43 @@ fn register_math_types(app: &mut App) {
.register_type::<bevy_math::Quat>(); .register_type::<bevy_math::Quat>();
} }
/// Setup of default task pools: `AsyncComputeTaskPool`, `ComputeTaskPool`, `IoTaskPool`.
#[derive(Default)]
pub struct TaskPoolPlugin {
/// Options for the [`TaskPool`](bevy_tasks::TaskPool) created at application start.
pub task_pool_options: TaskPoolOptions,
}
impl Plugin for TaskPoolPlugin {
fn build(&self, app: &mut App) {
// Setup the default bevy task pools
self.task_pool_options.create_default_pools();
#[cfg(not(target_arch = "wasm32"))]
app.add_system_to_stage(
bevy_app::CoreStage::Last,
tick_global_task_pools_on_main_thread.at_end(),
);
}
}
/// Keeps a count of rendered frames since the start of the app /// Keeps a count of rendered frames since the start of the app
/// ///
/// Wraps to 0 when it reaches the maximum u32 value /// Wraps to 0 when it reaches the maximum u32 value
#[derive(Default, Resource, Clone, Copy)] #[derive(Default, Resource, Clone, Copy)]
pub struct FrameCount(pub u32); pub struct FrameCount(pub u32);
/// Adds frame counting functionality to Apps.
#[derive(Default)]
pub struct FrameCountPlugin;
impl Plugin for FrameCountPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<FrameCount>();
app.add_system(update_frame_count);
}
}
fn update_frame_count(mut frame_count: ResMut<FrameCount>) { fn update_frame_count(mut frame_count: ResMut<FrameCount>) {
frame_count.0 = frame_count.0.wrapping_add(1); frame_count.0 = frame_count.0.wrapping_add(1);
} }
@ -125,7 +143,8 @@ mod tests {
#[test] #[test]
fn runs_spawn_local_tasks() { fn runs_spawn_local_tasks() {
let mut app = App::new(); let mut app = App::new();
app.add_plugin(CorePlugin::default()); app.add_plugin(TaskPoolPlugin::default());
app.add_plugin(TypeRegistrationPlugin::default());
let (async_tx, async_rx) = crossbeam_channel::unbounded(); let (async_tx, async_rx) = crossbeam_channel::unbounded();
AsyncComputeTaskPool::get() AsyncComputeTaskPool::get()
@ -158,7 +177,9 @@ mod tests {
#[test] #[test]
fn frame_counter_update() { fn frame_counter_update() {
let mut app = App::new(); let mut app = App::new();
app.add_plugin(CorePlugin::default()); app.add_plugin(TaskPoolPlugin::default());
app.add_plugin(TypeRegistrationPlugin::default());
app.add_plugin(FrameCountPlugin::default());
app.update(); app.update();
let frame_count = app.world.resource::<FrameCount>(); let frame_count = app.world.resource::<FrameCount>();

View File

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

View File

@ -2,7 +2,9 @@ use bevy_app::{PluginGroup, PluginGroupBuilder};
/// This plugin group will add all the default plugins: /// This plugin group will add all the default plugins:
/// * [`LogPlugin`](crate::log::LogPlugin) /// * [`LogPlugin`](crate::log::LogPlugin)
/// * [`CorePlugin`](crate::core::CorePlugin) /// * [`TaskPoolPlugin`](crate::core::TaskPoolPlugin)
/// * [`TypeRegistrationPlugin`](crate::core::TypeRegistrationPlugin)
/// * [`FrameCountPlugin`](crate::core::FrameCountPlugin)
/// * [`TimePlugin`](crate::time::TimePlugin) /// * [`TimePlugin`](crate::time::TimePlugin)
/// * [`TransformPlugin`](crate::transform::TransformPlugin) /// * [`TransformPlugin`](crate::transform::TransformPlugin)
/// * [`HierarchyPlugin`](crate::hierarchy::HierarchyPlugin) /// * [`HierarchyPlugin`](crate::hierarchy::HierarchyPlugin)
@ -29,7 +31,9 @@ impl PluginGroup for DefaultPlugins {
let mut group = PluginGroupBuilder::start::<Self>(); let mut group = PluginGroupBuilder::start::<Self>();
group = group group = group
.add(bevy_log::LogPlugin::default()) .add(bevy_log::LogPlugin::default())
.add(bevy_core::CorePlugin::default()) .add(bevy_core::TaskPoolPlugin::default())
.add(bevy_core::TypeRegistrationPlugin::default())
.add(bevy_core::FrameCountPlugin::default())
.add(bevy_time::TimePlugin::default()) .add(bevy_time::TimePlugin::default())
.add(bevy_transform::TransformPlugin::default()) .add(bevy_transform::TransformPlugin::default())
.add(bevy_hierarchy::HierarchyPlugin::default()) .add(bevy_hierarchy::HierarchyPlugin::default())
@ -118,7 +122,9 @@ impl PluginGroup for DefaultPlugins {
} }
/// Minimal plugin group that will add the following plugins: /// Minimal plugin group that will add the following plugins:
/// * [`CorePlugin`](crate::core::CorePlugin) /// * [`TaskPoolPlugin`](crate::core::TaskPoolPlugin)
/// * [`TypeRegistrationPlugin`](crate::core::TypeRegistrationPlugin)
/// * [`FrameCountPlugin`](crate::core::FrameCountPlugin)
/// * [`TimePlugin`](crate::time::TimePlugin) /// * [`TimePlugin`](crate::time::TimePlugin)
/// * [`ScheduleRunnerPlugin`](crate::app::ScheduleRunnerPlugin) /// * [`ScheduleRunnerPlugin`](crate::app::ScheduleRunnerPlugin)
/// ///
@ -128,7 +134,9 @@ pub struct MinimalPlugins;
impl PluginGroup for MinimalPlugins { impl PluginGroup for MinimalPlugins {
fn build(self) -> PluginGroupBuilder { fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>() PluginGroupBuilder::start::<Self>()
.add(bevy_core::CorePlugin::default()) .add(bevy_core::TaskPoolPlugin::default())
.add(bevy_core::TypeRegistrationPlugin::default())
.add(bevy_core::FrameCountPlugin::default())
.add(bevy_time::TimePlugin::default()) .add(bevy_time::TimePlugin::default())
.add(bevy_app::ScheduleRunnerPlugin::default()) .add(bevy_app::ScheduleRunnerPlugin::default())
} }

View File

@ -5,7 +5,7 @@ use bevy::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins.set(CorePlugin { .add_plugins(DefaultPlugins.set(TaskPoolPlugin {
task_pool_options: TaskPoolOptions::with_num_threads(4), task_pool_options: TaskPoolOptions::with_num_threads(4),
})) }))
.run(); .run();