Skip to content

Commit

Permalink
expose stages and system containers (bevyengine#1647)
Browse files Browse the repository at this point in the history
This allows third-party plugins to analyze the schedule, e.g. `bevy_mod_picking` can [display a schedule graph](https://github.com/jakobhellermann/bevy_mod_debugdump/tree/schedule-graph#schedule-graph):

![schedule graph](https://raw.githubusercontent.com/jakobhellermann/bevy_mod_debugdump/schedule-graph/docs/schedule_graph.svg)
  • Loading branch information
jakobhellermann committed Mar 14, 2021
1 parent de55e05 commit 48ee167
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
7 changes: 7 additions & 0 deletions crates/bevy_ecs/src/schedule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ impl Schedule {
stage.run(world);
}
}

/// Iterates over all of schedule's stages and their labels, in execution order.
pub fn iter_stages(&self) -> impl Iterator<Item = (&dyn StageLabel, &dyn Stage)> {
self.stage_order
.iter()
.map(move |label| (&**label, &*self.stages[label]))
}
}

impl Stage for Schedule {
Expand Down
32 changes: 29 additions & 3 deletions crates/bevy_ecs/src/schedule/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ pub struct SystemStage {
executor: Box<dyn ParallelSystemExecutor>,
/// Groups of systems; each set has its own run criterion.
system_sets: Vec<VirtualSystemSet>,
/// Topologically sorted exclusive systems that want to be ran at the start of the stage.
/// Topologically sorted exclusive systems that want to be run at the start of the stage.
exclusive_at_start: Vec<ExclusiveSystemContainer>,
/// Topologically sorted exclusive systems that want to be ran after parallel systems but
/// Topologically sorted exclusive systems that want to be run after parallel systems but
/// before the application of their command buffers.
exclusive_before_commands: Vec<ExclusiveSystemContainer>,
/// Topologically sorted exclusive systems that want to be ran at the end of the stage.
/// Topologically sorted exclusive systems that want to be run at the end of the stage.
exclusive_at_end: Vec<ExclusiveSystemContainer>,
/// Topologically sorted parallel systems.
parallel: Vec<ParallelSystemContainer>,
Expand Down Expand Up @@ -167,6 +167,32 @@ impl SystemStage {
self.add_system_to_set(system, 0)
}

/// Topologically sorted parallel systems.
///
/// Note that systems won't be fully-formed until the stage has been run at least once.
pub fn parallel_systems(&self) -> &[impl SystemContainer] {
&self.parallel
}
/// Topologically sorted exclusive systems that want to be run at the start of the stage.
///
/// Note that systems won't be fully-formed until the stage has been run at least once.
pub fn exclusive_at_start_systems(&self) -> &[impl SystemContainer] {
&self.exclusive_at_start
}
/// Topologically sorted exclusive systems that want to be run at the end of the stage.
///
/// Note that systems won't be fully-formed until the stage has been run at least once.
pub fn exclusive_at_end_systems(&self) -> &[impl SystemContainer] {
&self.exclusive_at_end
}
/// Topologically sorted exclusive systems that want to be run after parallel systems but
/// before the application of their command buffers.
///
/// Note that systems won't be fully-formed until the stage has been run at least once.
pub fn exclusive_before_commands_systems(&self) -> &[impl SystemContainer] {
&self.exclusive_before_commands
}

// TODO: consider exposing
fn add_system_to_set(&mut self, system: impl Into<SystemDescriptor>, set: usize) -> &mut Self {
self.systems_modified = true;
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_ecs/src/schedule/system_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ use crate::{
};
use std::{borrow::Cow, ptr::NonNull};

pub(super) trait SystemContainer {
/// System metadata like its name, labels, order requirements and component access.
pub trait SystemContainer {
fn name(&self) -> Cow<'static, str>;
#[doc(hidden)]
fn dependencies(&self) -> &[usize];
#[doc(hidden)]
fn set_dependencies(&mut self, dependencies: impl IntoIterator<Item = usize>);
fn system_set(&self) -> usize;
fn labels(&self) -> &[BoxedSystemLabel];
Expand Down

0 comments on commit 48ee167

Please sign in to comment.