Skip to content

update time in render stage #4735

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

Closed
wants to merge 1 commit 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
15 changes: 8 additions & 7 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ impl Plugin for CorePlugin {
.register_type::<Entity>()
.register_type::<Name>()
.register_type::<Range<f32>>()
.register_type::<Timer>()
// time system is added as an "exclusive system" to ensure it runs before other systems
// in CoreStage::First
.add_system_to_stage(
CoreStage::First,
time_system.exclusive_system().label(CoreSystem::Time),
);
.register_type::<Timer>();
// TODO: figure out how to add this when we don't have a render sub app
// time system is added as an "exclusive system" to ensure it runs before other systems
// in CoreStage::First
// .add_system_to_stage(
// CoreStage::First,
// time_system.exclusive_system().label(CoreSystem::Time),
// );

register_rust_types(app);
register_math_types(app);
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ trace = []
# bevy
bevy_app = { path = "../bevy_app", version = "0.8.0-dev" }
bevy_asset = { path = "../bevy_asset", version = "0.8.0-dev" }
bevy_core = { path = "../bevy_core", version = "0.8.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" }
bevy_render = { path = "../bevy_render", version = "0.8.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
Expand Down
11 changes: 10 additions & 1 deletion crates/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use main_pass_driver::*;
use std::ops::Range;

use bevy_app::{App, Plugin};
use bevy_core::prelude::Time;
use bevy_ecs::prelude::*;
use bevy_render::{
camera::{ActiveCamera, Camera2d, Camera3d, ExtractedCamera, RenderTarget},
Expand Down Expand Up @@ -121,6 +122,7 @@ impl Plugin for CorePipelinePlugin {
};

render_app
.init_resource::<Time>()
.init_resource::<DrawFunctions<Transparent2d>>()
.init_resource::<DrawFunctions<Opaque3d>>()
.init_resource::<DrawFunctions<AlphaMask3d>>()
Expand All @@ -140,7 +142,8 @@ impl Plugin for CorePipelinePlugin {
)
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<Opaque3d>)
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<AlphaMask3d>)
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<Transparent3d>);
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<Transparent3d>)
.add_system_to_stage(RenderStage::ExtractToApp, extract_time_to_app);

let clear_pass_node = ClearPassNode::new(&mut render_app.world);
let pass_node_2d = MainPass2dNode::new(&mut render_app.world);
Expand Down Expand Up @@ -384,6 +387,12 @@ pub fn extract_core_pipeline_camera_phases(
}
}

pub fn extract_time_to_app(mut time: ResMut<Time>, render_world: Res<RenderWorld>) {
if let Some(render_time) = render_world.get_resource::<Time>() {
std::mem::swap(&mut *time, &mut render_time.clone());
}
}

pub fn prepare_core_views_system(
mut commands: Commands,
mut texture_cache: ResMut<TextureCache>,
Expand Down
39 changes: 39 additions & 0 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ pub enum RenderStage {

/// Cleanup render resources here.
Cleanup,

/// Extract data from the "render world" and insert it into the "app world".
/// This should be used very sparingly and will be combined with the Extract stage
/// when pipelined rendering is implemented. You will only be able to access resources here
/// entities are cleared in the cleanup stage that runs before this.
ExtractToApp,
}

/// The Render App World. This is only available as a resource during the Extract step.
Expand Down Expand Up @@ -170,6 +176,7 @@ impl Plugin for RenderPlugin {
.with_system(render_system.exclusive_system().at_end()),
)
.add_stage(RenderStage::Cleanup, SystemStage::parallel())
.add_stage(RenderStage::ExtractToApp, SystemStage::parallel())
.insert_resource(instance)
.insert_resource(device)
.insert_resource(queue)
Expand Down Expand Up @@ -276,6 +283,15 @@ impl Plugin for RenderPlugin {

render_app.world.clear_entities();
}

{
#[cfg(feature = "trace")]
let _stage_span =
bevy_utils::tracing::info_span!("stage", name = "extract to app").entered();

// extract to app
extract_to_app(app_world, render_app);
}
});
}

Expand Down Expand Up @@ -311,3 +327,26 @@ fn extract(app_world: &mut World, render_app: &mut App) {

extract.apply_buffers(&mut render_app.world);
}

/// Executes the [`ExtractToApp`](RenderStage::Extract) stage of the renderer.
/// This updates the render world with the extracted ECS data of the current frame.
fn extract_to_app(app_world: &mut World, render_app: &mut App) {
let extract_to_app = render_app
.schedule
.get_stage_mut::<SystemStage>(&RenderStage::ExtractToApp)
.unwrap();

// temporarily add the render world to the app world as a resource
let scratch_world = app_world.remove_resource::<ScratchRenderWorld>().unwrap();
let render_world = std::mem::replace(&mut render_app.world, scratch_world.0);
app_world.insert_resource(RenderWorld(render_world));

extract_to_app.run(app_world);

// add the render world back to the render app
let render_world = app_world.remove_resource::<RenderWorld>().unwrap();
let scratch_world = std::mem::replace(&mut render_app.world, render_world.0);
app_world.insert_resource(ScratchRenderWorld(scratch_world));

extract_to_app.apply_buffers(&mut render_app.world);
}
6 changes: 6 additions & 0 deletions crates/bevy_render/src/renderer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod graph_runner;
mod render_device;

use bevy_core::Time;
use bevy_utils::tracing::{error, info, info_span};
pub use graph_runner::*;
pub use render_device::*;
Expand Down Expand Up @@ -73,6 +74,11 @@ pub fn render_system(world: &mut World) {
tracy.frame_mark = true
);
}

// update time after presenting frames
if let Some(mut time) = world.get_resource_mut::<Time>() {
time.update();
}
}

/// This queue is used to enqueue tasks for the GPU to execute asynchronously.
Expand Down