Skip to content
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

Cold Specialization #17567

Merged
merged 35 commits into from
Feb 5, 2025
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1634310
Start cold-specialization.
tychedelia Jan 25, 2025
62e01ac
Use ticks in render world.
tychedelia Jan 26, 2025
a8331cb
Forward working.
tychedelia Jan 26, 2025
47aa01d
Simplify extraction.
tychedelia Jan 26, 2025
14fdd1f
Move asset events to PostUpdate.
tychedelia Jan 26, 2025
1e06fd0
Clean-up.
tychedelia Jan 27, 2025
a4010b6
Clean-up.
tychedelia Jan 27, 2025
dae15a8
Start deferred.
tychedelia Jan 27, 2025
54a530c
Fix prepass.
tychedelia Jan 27, 2025
f296a7b
Start shadows.
tychedelia Jan 27, 2025
c251c04
Shadows.
tychedelia Jan 27, 2025
6757df7
Fix shadows.
tychedelia Jan 27, 2025
7055630
Remove plugin.
tychedelia Jan 27, 2025
f83ee15
Cargo fmt.
tychedelia Jan 27, 2025
9da87b8
Fix prepass.
tychedelia Jan 27, 2025
68fa3f0
2d.
tychedelia Jan 27, 2025
7129629
Cleanup.
tychedelia Jan 27, 2025
f46864d
Remove dep.
tychedelia Jan 27, 2025
da672a9
Updates from rebase.
tychedelia Jan 27, 2025
ba3f95a
Fix SSR.
tychedelia Jan 30, 2025
987688d
Update crates/bevy_sprite/src/mesh2d/mesh.rs
tychedelia Jan 30, 2025
6e02ac4
Update crates/bevy_pbr/src/material.rs
tychedelia Jan 30, 2025
1f06ac6
Update crates/bevy_pbr/src/render/mesh.rs
tychedelia Jan 30, 2025
fee692b
Respond to review.
tychedelia Jan 30, 2025
640dda9
Clippy.
tychedelia Jan 30, 2025
9ca6029
Clippy.
tychedelia Jan 30, 2025
e291115
Fmt.
tychedelia Jan 30, 2025
73c6b1a
Cleanup for ambiguity.
tychedelia Jan 30, 2025
278cf65
Clippy.
tychedelia Jan 30, 2025
86bdb26
Clippy.
tychedelia Jan 30, 2025
ad20a91
Ambiguity.
tychedelia Jan 30, 2025
0be3cdd
Ambiguity.
tychedelia Jan 30, 2025
5a00374
Ambiguity.
tychedelia Jan 30, 2025
f2e6ebb
Temp fix ambiguity.
tychedelia Jan 31, 2025
e3aff84
Fix ambiguity.
tychedelia Feb 4, 2025
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
Prev Previous commit
Next Next commit
Simplify extraction.
  • Loading branch information
tychedelia committed Jan 27, 2025
commit 47aa01d8e29535ce8dac4dcea99f5d1528070a81
87 changes: 57 additions & 30 deletions crates/bevy_render/src/specialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use crate::extract_resource::ExtractResource;
use crate::render_resource::CachedRenderPipelineId;
use crate::specialization::view::{SpecializeViewKey, ViewSpecializationTicks};
use crate::sync_world::{MainEntity, MainEntityHashMap};
use crate::view::VisibilitySystems;
use crate::{Extract, ExtractSchedule, RenderApp};
use bevy_app::{App, Plugin};
use bevy_app::{App, Last, Plugin, PostUpdate};
use bevy_asset::AssetEvents;
use bevy_ecs::component::{Component, Tick};
use bevy_ecs::entity::{Entity, EntityBorrow, EntityHash};
use bevy_ecs::prelude::SystemSet;
Expand All @@ -32,14 +34,55 @@ impl<M> Plugin for CheckSpecializationPlugin<M>
where
M: NeedsSpecialization,
{
fn build(&self, _app: &mut App) {}
fn build(&self, app: &mut App) {
app.add_systems(
Last,
check_entities_needing_specialization::<M>.after(AssetEvents),
)
.init_resource::<EntitiesNeedingSpecialization<M>>();
}

fn finish(&self, app: &mut App) {
if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
render_app
.add_systems(ExtractSchedule, extract_entities_needs_specialization::<M>)
.init_resource::<EntitySpecializationTicks<M>>()
.init_resource::<SpecializedMaterialPipelineCache::<M>>();
.init_resource::<SpecializedMaterialPipelineCache<M>>();
}
}
}

fn check_entities_needing_specialization<M>(
mut thread_queues: Local<Parallel<Vec<Entity>>>,
mut needs_specialization: Query<(Entity, M::QueryData), M::QueryFilter>,
mut entities_needing_specialization: ResMut<EntitiesNeedingSpecialization<M>>,
) where
M: NeedsSpecialization,
{
entities_needing_specialization.entities.clear();
needs_specialization.par_iter_mut().for_each_init(
|| thread_queues.borrow_local_mut(),
|queue, (entity, item)| {
if M::needs_specialization(item) {
queue.push(entity.into());
}
},
);

thread_queues.drain_into(&mut entities_needing_specialization.entities);
}

#[derive(Clone, Resource, Debug)]
pub struct EntitiesNeedingSpecialization<M> {
pub entities: Vec<Entity>,
_marker: PhantomData<M>,
}

impl<M> Default for EntitiesNeedingSpecialization<M> {
fn default() -> Self {
Self {
entities: Default::default(),
_marker: Default::default(),
}
}
}
Expand All @@ -50,7 +93,7 @@ pub struct EntitySpecializationTicks<M> {
_marker: PhantomData<M>,
}

impl <M> Default for EntitySpecializationTicks<M> {
impl<M> Default for EntitySpecializationTicks<M> {
fn default() -> Self {
Self {
entities: MainEntityHashMap::default(),
Expand All @@ -65,7 +108,8 @@ where
M: NeedsSpecialization,
{
entity_specialization_ticks: Res<'w, EntitySpecializationTicks<M>>,
view_specialization_ticks: Res<'w, ViewSpecializationTicks<<M as NeedsSpecialization>::ViewKey>>,
view_specialization_ticks:
Res<'w, ViewSpecializationTicks<<M as NeedsSpecialization>::ViewKey>>,
specialized_material_pipeline_cache: ResMut<'w, SpecializedMaterialPipelineCache<M>>,
ticks: SystemChangeTick,
}
Expand Down Expand Up @@ -118,43 +162,26 @@ where
}

pub fn extract_entities_needs_specialization<M>(
mut thread_queues: Local<Parallel<Vec<MainEntity>>>,
mut needs_specialization: Extract<Query<(Entity, M::QueryData), M::QueryFilter>>,
mut entities_needing_specialization: Extract<Res<EntitiesNeedingSpecialization<M>>>,
mut entity_specialization_ticks: ResMut<EntitySpecializationTicks<M>>,
ticks: SystemChangeTick,
) where
M: NeedsSpecialization,
{
needs_specialization.par_iter_mut().for_each_init(
|| thread_queues.borrow_local_mut(),
|queue, (entity, item)| {
if M::needs_specialization(item) {
println!("Entity {:?} needs specialization", entity);
queue.push(entity.into());
}
},
);

let size = thread_queues.iter_mut().map(|queue| queue.len()).sum();
entity_specialization_ticks.entities.reserve(size);
let this_run = ticks.this_run();
for queue in thread_queues.iter_mut() {
for entity in queue.drain(..) {
// Update the entity's specialization tick with this run's tick
entity_specialization_ticks.entities.insert(entity, this_run);
}
for entity in &entities_needing_specialization.entities {
// Update the entity's specialization tick with this run's tick
entity_specialization_ticks
.entities
.insert((*entity).into(), ticks.this_run());
}
}


pub trait NeedsSpecialization: Component {
type ViewKey: SpecializeViewKey;
type QueryData: ReadOnlyQueryData + 'static;
type QueryFilter: QueryFilter + 'static;

fn needs_specialization(
item: ROQueryItem<'_, Self::QueryData>,
) -> bool;
fn needs_specialization(item: ROQueryItem<'_, Self::QueryData>) -> bool;
}

#[derive(Resource)]
Expand Down Expand Up @@ -184,4 +211,4 @@ impl<M> DerefMut for SpecializedMaterialPipelineCache<M> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.map
}
}
}