Skip to content

Commit

Permalink
Add some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Apr 3, 2022
1 parent f258c33 commit 4b92626
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
33 changes: 33 additions & 0 deletions crates/bevy_render/src/extract_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_ecs::{
system::{ReadOnlySystemParamFetch, SystemParam, SystemParamItem, SystemState},
};

/// Implementation detail of [`Extract`]
pub struct MainWorldState<P: SystemParam>(SystemState<P>);

impl<P: SystemParam> FromWorld for MainWorldState<P> {
Expand All @@ -12,6 +13,38 @@ impl<P: SystemParam> FromWorld for MainWorldState<P> {
}
}

/// A helper for accessing [`MainWorld`] content using a system parameter.
///
/// A [`SystemParam`] adapter which applies the contained `SystemParam` to the [`World`]
/// contained in [`MainWorld`]. This parameter only works for systems run
/// during [`RenderStage::Extract`].
///
/// This requires that the contained [`SystemParam`] does not mutate the world, as it
/// uses [`Res<MainWorld>`](Res). To get access to the contained `SystemParam`'s item, you
/// must use [`Extract::value`]. This is required because of lifetime limitations in
/// the `SystemParam` api.
///
/// ## Context
///
/// [`RenderStage::Extract`] is used to extract (move) data from the simulation world ([`MainWorld`]) to the
/// render world. The render world drives rendering each frame (generally to a [Window]).
/// This design is used to allow performing calculations related to rendering a prior frame at the same
/// time as the next frame is simulated, which increases throughput (FPS).
///
/// [`Extract`] is used to get data from the main world during [`RenderStage::Extract`].
///
/// ## Examples
///
/// ```rust
/// fn extract_clouds(mut commands: Commands, mut clouds: Extract<Query<Entity, With<Cloud>>>) {
/// for cloud in clouds.value().iter() {
/// commands.get_or_spawn(cloud).insert(Cloud);
/// }
/// }
/// ```
///
/// [`RenderStage::Extract`]: crate::RenderStage::Extract
/// [Window]: bevy_window::Window
#[derive(SystemParam)]
pub struct Extract<'w, 's, P: SystemParam + 'static>
where
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ pub enum RenderStage {
Cleanup,
}

/// The Render App World. This is only available as a resource during the Extract step.
/// The simulation [World] of the application, stored as a resource.
/// This resource is only available during [`RenderStage::Extract`].
/// See [`Extract`] for more details.
#[derive(Default)]
pub struct MainWorld(World);

Expand All @@ -106,7 +108,7 @@ impl DerefMut for MainWorld {
pub struct RenderApp;

/// A "scratch" world used to avoid allocating new worlds every frame when
/// swapping out the [`RenderWorld`].
/// swapping out the [`MainWorld`] for [`RenderStage::Extract`].
#[derive(Default)]
struct ScratchMainWorld(World);

Expand Down

0 comments on commit 4b92626

Please sign in to comment.