Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System & frame stepping support #8063

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ impl Plugin for AnimationPlugin {
.register_type::<AnimationPlayer>()
.add_systems(
PostUpdate,
animation_player.before(TransformSystem::TransformPropagate),
animation_player
.before(TransformSystem::TransformPropagate)
.ignore_stepping(),
);
}
}
31 changes: 30 additions & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use crate::{
};
pub use bevy_derive::AppLabel;
use bevy_ecs::{
event::ManualEventReader,
prelude::*,
schedule::{
apply_state_transition, common_conditions::run_once as run_once_condition,
run_enter_schedule, BoxedScheduleLabel, IntoSystemConfigs, IntoSystemSetConfigs,
ScheduleLabel,
ScheduleEvent, ScheduleLabel,
},
};
use bevy_utils::{tracing::debug, HashMap, HashSet};
Expand Down Expand Up @@ -82,6 +83,9 @@ pub struct App {
plugin_name_added: HashSet<String>,
/// A private counter to prevent incorrect calls to `App::run()` from `Plugin::build()`
building_plugin_depth: usize,

/// Tracks which system stepping events have been processed
schedule_event_reader: ManualEventReader<ScheduleEvent>,
}

impl Debug for App {
Expand Down Expand Up @@ -192,6 +196,7 @@ impl Default for App {

app.add_plugin(MainSchedulePlugin);
app.add_event::<AppExit>();
app.add_event::<ScheduleEvent>();

#[cfg(feature = "bevy_ci_testing")]
{
Expand Down Expand Up @@ -223,6 +228,7 @@ impl App {
plugin_name_added: Default::default(),
main_schedule_label: Box::new(Main),
building_plugin_depth: 0,
schedule_event_reader: ManualEventReader::<ScheduleEvent>::default(),
}
}

Expand Down Expand Up @@ -250,6 +256,29 @@ impl App {
sub_app.run();
}

// move schedules out of the world so we can modify it as we iterate
// through the schedule events
let mut schedules = self.world.remove_resource::<Schedules>().unwrap();

if let Some(mut schedule_events) = self.world.get_resource_mut::<Events<ScheduleEvent>>() {
use ScheduleEvent::*;

for event in self.schedule_event_reader.iter(&schedule_events) {
match event {
EnableStepping(label)
| DisableStepping(label)
| ToggleStepping(label)
| StepFrame(label)
| StepSystem(label) => {
schedules.get_mut(&**label).unwrap().handle_event(event);
}
}
}
schedule_events.clear();
}

// Reinsert the Schedules resource, as we're now done with it
self.world.insert_resource(schedules);
self.world.clear_trackers();
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_app/src/main_schedule.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{App, Plugin};
use bevy_ecs::{
schedule::{ExecutorKind, Schedule, ScheduleLabel},
schedule::{ExecutorKind, IntoSystemConfigs, Schedule, ScheduleLabel},
system::{Local, Resource},
world::{Mut, World},
};
Expand Down Expand Up @@ -163,6 +163,6 @@ impl Plugin for MainSchedulePlugin {
app.add_schedule(Main, main_schedule)
.add_schedule(RunFixedUpdateLoop, fixed_update_loop_schedule)
.init_resource::<MainScheduleOrder>()
.add_systems(Main, Main::run_main);
.add_systems(Main, Main::run_main.ignore_stepping());
}
}
32 changes: 32 additions & 0 deletions crates/bevy_app/tests/stepping_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
mod stepping {
use bevy_app::prelude::*;
use bevy_app::App;
use bevy_ecs::prelude::*;
use bevy_ecs::schedule::ScheduleEvent;

// verify App::update() ScheduleEvents behavior
#[test]
fn app_update_schedule_events() {
let mut app = App::new();

// add a system to write a ScheduleEvent
app.add_systems(Update, |mut schedule_events: EventWriter<ScheduleEvent>| {
schedule_events.send(ScheduleEvent::EnableStepping(Box::new(Main)));
});

// ensure stepping isn't enabled on the schedule
let schedule = app.get_schedule(Main).unwrap();
assert!(!schedule.stepping());

app.update();

// verify the event was sent to the schedule by verifing stepping has
// been turned on
let schedule = app.get_schedule(Main).unwrap();
assert!(schedule.stepping());

// verify the ScheduleEvent list was cleared
let schedule_events = app.world.get_resource::<Events<ScheduleEvent>>().unwrap();
assert!(schedule_events.is_empty());
}
}
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/debug_asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Plugin for DebugAssetServerPlugin {
watch_for_changes: true,
});
app.insert_non_send_resource(DebugAssetApp(debug_asset_app));
app.add_systems(Update, run_debug_asset_app);
app.add_systems(Update, run_debug_asset_app.ignore_stepping());
}
}

Expand Down
8 changes: 5 additions & 3 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub use path::*;
pub use reflect::*;

use bevy_app::{prelude::*, MainScheduleOrder};
use bevy_ecs::schedule::ScheduleLabel;
use bevy_ecs::schedule::{IntoSystemConfigs, ScheduleLabel};

/// Asset storages are updated.
#[derive(Debug, Hash, PartialEq, Eq, Clone, ScheduleLabel)]
Expand Down Expand Up @@ -103,8 +103,10 @@ impl Plugin for AssetPlugin {
app.insert_resource(asset_server);
}

app.register_type::<HandleId>()
.add_systems(PreUpdate, asset_server::free_unused_assets_system);
app.register_type::<HandleId>().add_systems(
PreUpdate,
asset_server::free_unused_assets_system.ignore_stepping(),
);
app.init_schedule(LoadAssets);
app.init_schedule(AssetEvents);

Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub use sinks::*;

use bevy_app::prelude::*;
use bevy_asset::{AddAsset, Asset};
use bevy_ecs::schedule::IntoSystemConfigs;

/// Adds support for audio playback to a Bevy Application
///
Expand All @@ -61,7 +62,10 @@ impl Plugin for AudioPlugin {
.add_asset::<AudioSink>()
.add_asset::<SpatialAudioSink>()
.init_resource::<Audio<AudioSource>>()
.add_systems(PostUpdate, play_queued_audio_system::<AudioSource>);
.add_systems(
PostUpdate,
play_queued_audio_system::<AudioSource>.ignore_stepping(),
);

#[cfg(any(feature = "mp3", feature = "flac", feature = "wav", feature = "vorbis"))]
app.init_asset_loader::<AudioLoader>();
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Plugin for TaskPoolPlugin {
self.task_pool_options.create_default_pools();

#[cfg(not(target_arch = "wasm32"))]
_app.add_systems(Last, tick_global_task_pools);
_app.add_systems(Last, tick_global_task_pools.ignore_stepping());
}
}
/// A dummy type that is [`!Send`](Send), to force systems to run on the main thread.
Expand Down Expand Up @@ -142,7 +142,7 @@ pub struct FrameCountPlugin;
impl Plugin for FrameCountPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<FrameCount>();
app.add_systems(Last, update_frame_count);
app.add_systems(Last, update_frame_count.ignore_stepping());
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ impl Plugin for BloomPlugin {
prepare_downsampling_pipeline.in_set(RenderSet::Prepare),
prepare_upsampling_pipeline.in_set(RenderSet::Prepare),
queue_bloom_bind_groups.in_set(RenderSet::Queue),
),
)
.ignore_stepping(),
);

// Add bloom to the 3d render graph
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ impl Plugin for Core2dPlugin {

render_app
.init_resource::<DrawFunctions<Transparent2d>>()
.add_systems(ExtractSchedule, extract_core_2d_camera_phases)
.add_systems(
ExtractSchedule,
extract_core_2d_camera_phases.ignore_stepping(),
)
.add_systems(
Render,
(
sort_phase_system::<Transparent2d>.in_set(RenderSet::PhaseSort),
batch_phase_system::<Transparent2d>
.after(sort_phase_system::<Transparent2d>)
.in_set(RenderSet::PhaseSort),
),
)
.ignore_stepping(),
);

let pass_node_2d = MainPass2dNode::new(&mut render_app.world);
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ impl Plugin for Core3dPlugin {
.init_resource::<DrawFunctions<Opaque3d>>()
.init_resource::<DrawFunctions<AlphaMask3d>>()
.init_resource::<DrawFunctions<Transparent3d>>()
.add_systems(ExtractSchedule, extract_core_3d_camera_phases)
.add_systems(
ExtractSchedule,
extract_core_3d_camera_phases.ignore_stepping(),
)
.add_systems(
Render,
(
Expand All @@ -78,7 +81,8 @@ impl Plugin for Core3dPlugin {
sort_phase_system::<Opaque3d>.in_set(RenderSet::PhaseSort),
sort_phase_system::<AlphaMask3d>.in_set(RenderSet::PhaseSort),
sort_phase_system::<Transparent3d>.in_set(RenderSet::PhaseSort),
),
)
.ignore_stepping(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand why rustfmt is indenting this, but it happens everywhere I added ignore_stepping() to a tuple.

);

let prepass_node = PrepassNode::new(&mut render_app.world);
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_core_pipeline/src/fxaa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ impl Plugin for FxaaPlugin {
render_app
.init_resource::<FxaaPipeline>()
.init_resource::<SpecializedRenderPipelines<FxaaPipeline>>()
.add_systems(Render, prepare_fxaa_pipelines.in_set(RenderSet::Prepare));
.add_systems(
Render,
prepare_fxaa_pipelines
.in_set(RenderSet::Prepare)
.ignore_stepping(),
);

{
let fxaa_node = FxaaNode::new(&mut render_app.world);
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_core_pipeline/src/msaa_writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ impl Plugin for MsaaWritebackPlugin {

render_app.add_systems(
Render,
queue_msaa_writeback_pipelines.in_set(RenderSet::Queue),
queue_msaa_writeback_pipelines
.in_set(RenderSet::Queue)
.ignore_stepping(),
);
let msaa_writeback_2d = MsaaWritebackNode::new(&mut render_app.world);
let msaa_writeback_3d = MsaaWritebackNode::new(&mut render_app.world);
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_core_pipeline/src/tonemapping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ impl Plugin for TonemappingPlugin {
.init_resource::<SpecializedRenderPipelines<TonemappingPipeline>>()
.add_systems(
Render,
queue_view_tonemapping_pipelines.in_set(RenderSet::Queue),
queue_view_tonemapping_pipelines
.in_set(RenderSet::Queue)
.ignore_stepping(),
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_core_pipeline/src/upscaling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ impl Plugin for UpscalingPlugin {
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app.add_systems(
Render,
queue_view_upscaling_pipelines.in_set(RenderSet::Queue),
queue_view_upscaling_pipelines
.in_set(RenderSet::Queue)
.ignore_stepping(),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_diagnostic/src/entity_count_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub struct EntityCountDiagnosticsPlugin;

impl Plugin for EntityCountDiagnosticsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, Self::setup_system)
.add_systems(Update, Self::diagnostic_system);
app.add_systems(Startup, Self::setup_system.ignore_stepping())
.add_systems(Update, Self::diagnostic_system.ignore_stepping());
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct FrameTimeDiagnosticsPlugin;

impl Plugin for FrameTimeDiagnosticsPlugin {
fn build(&self, app: &mut bevy_app::App) {
app.add_systems(Startup, Self::setup_system)
.add_systems(Update, Self::diagnostic_system);
app.add_systems(Startup, Self::setup_system.ignore_stepping())
.add_systems(Update, Self::diagnostic_system.ignore_stepping());
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod log_diagnostics_plugin;
mod system_information_diagnostics_plugin;

use bevy_app::prelude::*;
use bevy_ecs::schedule::IntoSystemConfigs;
pub use diagnostic::*;
pub use entity_count_diagnostics_plugin::EntityCountDiagnosticsPlugin;
pub use frame_time_diagnostics_plugin::FrameTimeDiagnosticsPlugin;
Expand All @@ -19,7 +20,7 @@ impl Plugin for DiagnosticsPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Diagnostics>().add_systems(
Startup,
system_information_diagnostics_plugin::internal::log_system_info,
system_information_diagnostics_plugin::internal::log_system_info.ignore_stepping(),
);
}
}
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_diagnostic/src/log_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ impl Plugin for LogDiagnosticsPlugin {
});

if self.debug {
app.add_systems(PostUpdate, Self::log_diagnostics_debug_system);
app.add_systems(
PostUpdate,
Self::log_diagnostics_debug_system.ignore_stepping(),
);
} else {
app.add_systems(PostUpdate, Self::log_diagnostics_system);
app.add_systems(PostUpdate, Self::log_diagnostics_system.ignore_stepping());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::DiagnosticId;
use bevy_app::prelude::*;
use bevy_ecs::schedule::IntoSystemConfigs;

/// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %)
///
Expand All @@ -14,8 +15,8 @@ use bevy_app::prelude::*;
pub struct SystemInformationDiagnosticsPlugin;
impl Plugin for SystemInformationDiagnosticsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, internal::setup_system)
.add_systems(Update, internal::diagnostic_system);
app.add_systems(Startup, internal::setup_system.ignore_stepping())
.add_systems(Update, internal::diagnostic_system.ignore_stepping());
}
}

Expand Down
Loading