Skip to content

Run RenderStartup in/before extract instead of after it. #19926

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

Merged
merged 5 commits into from
Jul 7, 2025
Merged
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
8 changes: 4 additions & 4 deletions crates/bevy_app/src/sub_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::fmt::Debug;
#[cfg(feature = "trace")]
use tracing::info_span;

type ExtractFn = Box<dyn Fn(&mut World, &mut World) + Send>;
type ExtractFn = Box<dyn FnMut(&mut World, &mut World) + Send>;

/// A secondary application with its own [`World`]. These can run independently of each other.
///
Expand Down Expand Up @@ -160,7 +160,7 @@ impl SubApp {
/// The first argument is the `World` to extract data from, the second argument is the app `World`.
pub fn set_extract<F>(&mut self, extract: F) -> &mut Self
where
F: Fn(&mut World, &mut World) + Send + 'static,
F: FnMut(&mut World, &mut World) + Send + 'static,
{
self.extract = Some(Box::new(extract));
self
Expand All @@ -177,13 +177,13 @@ impl SubApp {
/// ```
/// # use bevy_app::SubApp;
/// # let mut app = SubApp::new();
/// let default_fn = app.take_extract();
/// let mut default_fn = app.take_extract();
/// app.set_extract(move |main, render| {
/// // Do pre-extract custom logic
/// // [...]
///
/// // Call Bevy's default, which executes the Extract phase
/// if let Some(f) = default_fn.as_ref() {
/// if let Some(f) = default_fn.as_mut() {
/// f(main, render);
/// }
///
Expand Down
33 changes: 14 additions & 19 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,22 +217,6 @@ pub enum RenderSystems {
PostCleanup,
}

/// The schedule that contains the app logic that is evaluated each tick
///
/// This is highly inspired by [`bevy_app::Main`]
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash, Default)]
pub struct MainRender;
impl MainRender {
pub fn run(world: &mut World, mut run_at_least_once: Local<bool>) {
if !*run_at_least_once {
let _ = world.try_run_schedule(RenderStartup);
*run_at_least_once = true;
}

let _ = world.try_run_schedule(Render);
}
}

/// Deprecated alias for [`RenderSystems`].
#[deprecated(since = "0.17.0", note = "Renamed to `RenderSystems`.")]
pub type RenderSet = RenderSystems;
Expand Down Expand Up @@ -551,7 +535,7 @@ unsafe fn initialize_render_app(app: &mut App) {
app.init_resource::<ScratchMainWorld>();

let mut render_app = SubApp::new();
render_app.update_schedule = Some(MainRender.intern());
render_app.update_schedule = Some(Render.intern());

let mut extract_schedule = Schedule::new(ExtractSchedule);
// We skip applying any commands during the ExtractSchedule
Expand All @@ -566,7 +550,6 @@ unsafe fn initialize_render_app(app: &mut App) {
.add_schedule(extract_schedule)
.add_schedule(Render::base_schedule())
.init_resource::<render_graph::RenderGraph>()
.add_systems(MainRender, MainRender::run)
.insert_resource(app.world().resource::<AssetServer>().clone())
.add_systems(ExtractSchedule, PipelineCache::extract_shaders)
.add_systems(
Expand All @@ -582,7 +565,19 @@ unsafe fn initialize_render_app(app: &mut App) {
),
);

render_app.set_extract(|main_world, render_world| {
// We want the closure to have a flag to only run the RenderStartup schedule once, but the only
// way to have the closure store this flag is by capturing it. This variable is otherwise
// unused.
let mut should_run_startup = true;
render_app.set_extract(move |main_world, render_world| {
if should_run_startup {
// Run the `RenderStartup` if it hasn't run yet. This does mean `RenderStartup` blocks
// the rest of the app extraction, but this is necessary since extraction itself can
// depend on resources initialized in `RenderStartup`.
render_world.run_schedule(RenderStartup);
should_run_startup = false;
}

{
#[cfg(feature = "trace")]
let _stage_span = tracing::info_span!("entity_sync").entered();
Expand Down
10 changes: 10 additions & 0 deletions release-content/migration-guides/extract_fn_is_mut.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: `take_extract` now returns `dyn FnMut` instead of `dyn Fn`.
pull_requests: [19926]
---

Previously, `set_extract` accepted any `Fn`. Now we accept any `FnMut`. For callers of
`set_extract`, there is no difference since `Fn: FnMut`.

However, callers of `take_extract` will now be returned
`Option<Box<dyn FnMut(&mut World, &mut World) + Send>>` instead.