From 18e1bf1c3ddf6b102a416b4886b40c025e800cd6 Mon Sep 17 00:00:00 2001 From: charlotte Date: Mon, 14 Apr 2025 23:44:01 -0700 Subject: [PATCH] Swap order of eviction/extraction when extracting for specialization (#18846) # Objective Fixes #18843 ## Solution We need to account for the material being added and removed in the course of the same frame. We evict the caches first because the entity will be re-added if it was marked as needing specialization, which avoids another check on removed components to see if it was "really" despawned. --- crates/bevy_pbr/src/material.rs | 13 ++++++++----- crates/bevy_sprite/src/mesh2d/material.rs | 12 +++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index b00bb955a1..1814996184 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -822,11 +822,9 @@ pub fn extract_entities_needs_specialization( ) where M: Material, { - for entity in entities_needing_specialization.iter() { - // Update the entity's specialization tick with this run's tick - entity_specialization_ticks.insert((*entity).into(), ticks.this_run()); - } - // Clean up any despawned entities + // Clean up any despawned entities, we do this first in case the removed material was re-added + // the same frame, thus will appear both in the removed components list and have been added to + // the `EntitiesNeedingSpecialization` collection by triggering the `Changed` filter for entity in removed_mesh_material_components.read() { entity_specialization_ticks.remove(&MainEntity::from(entity)); for view in views { @@ -849,6 +847,11 @@ pub fn extract_entities_needs_specialization( } } } + + for entity in entities_needing_specialization.iter() { + // Update the entity's specialization tick with this run's tick + entity_specialization_ticks.insert((*entity).into(), ticks.this_run()); + } } #[derive(Resource, Deref, DerefMut, Clone, Debug)] diff --git a/crates/bevy_sprite/src/mesh2d/material.rs b/crates/bevy_sprite/src/mesh2d/material.rs index ac19697081..e34595f138 100644 --- a/crates/bevy_sprite/src/mesh2d/material.rs +++ b/crates/bevy_sprite/src/mesh2d/material.rs @@ -564,11 +564,9 @@ pub fn extract_entities_needs_specialization( ) where M: Material2d, { - for entity in entities_needing_specialization.iter() { - // Update the entity's specialization tick with this run's tick - entity_specialization_ticks.insert((*entity).into(), ticks.this_run()); - } - // Clean up any despawned entities + // Clean up any despawned entities, we do this first in case the removed material was re-added + // the same frame, thus will appear both in the removed components list and have been added to + // the `EntitiesNeedingSpecialization` collection by triggering the `Changed` filter for entity in removed_mesh_material_components.read() { entity_specialization_ticks.remove(&MainEntity::from(entity)); for view in views { @@ -577,6 +575,10 @@ pub fn extract_entities_needs_specialization( } } } + for entity in entities_needing_specialization.iter() { + // Update the entity's specialization tick with this run's tick + entity_specialization_ticks.insert((*entity).into(), ticks.this_run()); + } } #[derive(Clone, Resource, Deref, DerefMut, Debug)]