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"]
struct MyAsset;
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());
app.add_asset::<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 {
//! The Bevy Core Prelude.
#[doc(hidden)]
pub use crate::{CorePlugin, Name, TaskPoolOptions};
pub use crate::{
FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin,
};
}
use bevy_app::prelude::*;
@ -31,31 +33,16 @@ use bevy_ecs::schedule::IntoSystemDescriptor;
#[cfg(not(target_arch = "wasm32"))]
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)]
pub struct CorePlugin {
/// Options for the [`TaskPool`](bevy_tasks::TaskPool) created at application start.
pub task_pool_options: TaskPoolOptions,
}
pub struct TypeRegistrationPlugin;
impl Plugin for CorePlugin {
impl Plugin for TypeRegistrationPlugin {
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>();
register_rust_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>();
}
/// 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
///
/// Wraps to 0 when it reaches the maximum u32 value
#[derive(Default, Resource, Clone, Copy)]
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>) {
frame_count.0 = frame_count.0.wrapping_add(1);
}
@ -125,7 +143,8 @@ mod tests {
#[test]
fn runs_spawn_local_tasks() {
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();
AsyncComputeTaskPool::get()
@ -158,7 +177,9 @@ mod tests {
#[test]
fn frame_counter_update() {
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();
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,
/// set up [`CorePlugin`](super::CorePlugin)
/// set up [`TaskPoolPlugin`](super::TaskPoolPlugin)
#[derive(Clone, Resource)]
pub struct TaskPoolOptions {
/// 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:
/// * [`LogPlugin`](crate::log::LogPlugin)
/// * [`CorePlugin`](crate::core::CorePlugin)
/// * [`TaskPoolPlugin`](crate::core::TaskPoolPlugin)
/// * [`TypeRegistrationPlugin`](crate::core::TypeRegistrationPlugin)
/// * [`FrameCountPlugin`](crate::core::FrameCountPlugin)
/// * [`TimePlugin`](crate::time::TimePlugin)
/// * [`TransformPlugin`](crate::transform::TransformPlugin)
/// * [`HierarchyPlugin`](crate::hierarchy::HierarchyPlugin)
@ -29,7 +31,9 @@ impl PluginGroup for DefaultPlugins {
let mut group = PluginGroupBuilder::start::<Self>();
group = group
.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_transform::TransformPlugin::default())
.add(bevy_hierarchy::HierarchyPlugin::default())
@ -118,7 +122,9 @@ impl PluginGroup for DefaultPlugins {
}
/// 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)
/// * [`ScheduleRunnerPlugin`](crate::app::ScheduleRunnerPlugin)
///
@ -128,7 +134,9 @@ pub struct MinimalPlugins;
impl PluginGroup for MinimalPlugins {
fn build(self) -> PluginGroupBuilder {
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_app::ScheduleRunnerPlugin::default())
}

View file

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