Skip to content

Commit 2ea5e9b

Browse files
tychedeliapcwalton
andauthored
Cold Specialization (#17567)
# Cold Specialization ## Objective An ongoing part of our quest to retain everything in the render world, cold-specialization aims to cache pipeline specialization so that pipeline IDs can be recomputed only when necessary, rather than every frame. This approach reduces redundant work in stable scenes, while still accommodating scenarios in which materials, views, or visibility might change, as well as unlocking future optimization work like retaining render bins. ## Solution Queue systems are split into a specialization system and queue system, the former of which only runs when necessary to compute a new pipeline id. Pipelines are invalidated using a combination of change detection and ECS ticks. ### The difficulty with change detection Detecting “what changed” can be tricky because pipeline specialization depends not only on the entity’s components (e.g., mesh, material, etc.) but also on which view (camera) it is rendering in. In other words, the cache key for a given pipeline id is a view entity/render entity pair. As such, it's not sufficient simply to react to change detection in order to specialize -- an entity could currently be out of view or could be rendered in the future in camera that is currently disabled or hasn't spawned yet. ### Why ticks? Ticks allow us to ensure correctness by allowing us to compare the last time a view or entity was updated compared to the cached pipeline id. This ensures that even if an entity was out of view or has never been seen in a given camera before we can still correctly determine whether it needs to be re-specialized or not. ## Testing TODO: Tested a bunch of different examples, need to test more. ## Migration Guide TODO - `AssetEvents` has been moved into the `PostUpdate` schedule. --------- Co-authored-by: Patrick Walton <[email protected]>
1 parent be9b38e commit 2ea5e9b

File tree

17 files changed

+1362
-482
lines changed

17 files changed

+1362
-482
lines changed

crates/bevy_animation/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{
3232
};
3333

3434
use bevy_app::{Animation, App, Plugin, PostUpdate};
35-
use bevy_asset::{Asset, AssetApp, Assets};
35+
use bevy_asset::{Asset, AssetApp, AssetEvents, Assets};
3636
use bevy_ecs::{
3737
entity::{VisitEntities, VisitEntitiesMut},
3838
prelude::*,
@@ -1244,7 +1244,7 @@ impl Plugin for AnimationPlugin {
12441244
.add_systems(
12451245
PostUpdate,
12461246
(
1247-
graph::thread_animation_graphs,
1247+
graph::thread_animation_graphs.before(AssetEvents),
12481248
advance_transitions,
12491249
advance_animations,
12501250
// TODO: `animate_targets` can animate anything, so

crates/bevy_asset/src/asset_changed.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ mod tests {
293293
use std::println;
294294

295295
use crate::{AssetApp, Assets};
296-
use bevy_app::{App, AppExit, Last, Startup, TaskPoolPlugin, Update};
296+
use bevy_app::{App, AppExit, PostUpdate, Startup, TaskPoolPlugin, Update};
297297
use bevy_ecs::schedule::IntoSystemConfigs;
298298
use bevy_ecs::{
299299
component::Component,
@@ -410,7 +410,7 @@ mod tests {
410410
.init_asset::<MyAsset>()
411411
.insert_resource(Counter(vec![0, 0, 0, 0]))
412412
.add_systems(Update, add_some)
413-
.add_systems(Last, count_update.after(AssetEvents));
413+
.add_systems(PostUpdate, count_update.after(AssetEvents));
414414

415415
// First run of the app, `add_systems(Startup…)` runs.
416416
app.update(); // run_count == 0
@@ -445,7 +445,7 @@ mod tests {
445445
},
446446
)
447447
.add_systems(Update, update_some)
448-
.add_systems(Last, count_update.after(AssetEvents));
448+
.add_systems(PostUpdate, count_update.after(AssetEvents));
449449

450450
// First run of the app, `add_systems(Startup…)` runs.
451451
app.update(); // run_count == 0

crates/bevy_asset/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ use alloc::{
212212
sync::Arc,
213213
vec::Vec,
214214
};
215-
use bevy_app::{App, Last, Plugin, PreUpdate};
215+
use bevy_app::{App, Plugin, PostUpdate, PreUpdate};
216216
use bevy_ecs::prelude::Component;
217217
use bevy_ecs::{
218218
reflect::AppTypeRegistry,
@@ -580,7 +580,7 @@ impl AssetApp for App {
580580
.add_event::<AssetLoadFailedEvent<A>>()
581581
.register_type::<Handle<A>>()
582582
.add_systems(
583-
Last,
583+
PostUpdate,
584584
Assets::<A>::asset_events
585585
.run_if(Assets::<A>::asset_events_condition)
586586
.in_set(AssetEvents),

0 commit comments

Comments
 (0)