bevy_render/
sync_world.rs

1use bevy_app::Plugin;
2use bevy_derive::{Deref, DerefMut};
3use bevy_ecs::entity::EntityHash;
4use bevy_ecs::{
5    component::Component,
6    entity::Entity,
7    observer::Trigger,
8    query::With,
9    reflect::ReflectComponent,
10    system::{Local, Query, ResMut, Resource, SystemState},
11    world::{Mut, OnAdd, OnRemove, World},
12};
13use bevy_reflect::Reflect;
14use bevy_utils::hashbrown;
15
16/// A plugin that synchronizes entities with [`SyncToRenderWorld`] between the main world and the render world.
17///
18/// All entities with the [`SyncToRenderWorld`] component are kept in sync. It
19/// is automatically added as a required component by [`ExtractComponentPlugin`]
20/// and [`SyncComponentPlugin`], so it doesn't need to be added manually when
21/// spawning or as a required component when either of these plugins are used.
22///
23/// # Implementation
24///
25/// Bevy's renderer is architected independently from the main app.
26/// It operates in its own separate ECS [`World`], so the renderer logic can run in parallel with the main world logic.
27/// This is called "Pipelined Rendering", see [`PipelinedRenderingPlugin`] for more information.
28///
29/// [`SyncWorldPlugin`] is the first thing that runs every frame and it maintains an entity-to-entity mapping
30/// between the main world and the render world.
31/// It does so by spawning and despawning entities in the render world, to match spawned and despawned entities in the main world.
32/// The link between synced entities is maintained by the [`RenderEntity`] and [`MainEntity`] components.
33///
34/// The [`RenderEntity`] contains the corresponding render world entity of a main world entity, while [`MainEntity`] contains
35/// the corresponding main world entity of a render world entity.
36/// For convenience, [`QueryData`](bevy_ecs::query::QueryData) implementations are provided for both components:
37/// adding [`MainEntity`] to a query (without a `&`) will return the corresponding main world [`Entity`],
38/// and adding [`RenderEntity`] will return the corresponding render world [`Entity`].
39/// If you have access to the component itself, the underlying entities can be accessed by calling `.id()`.
40///
41/// Synchronization is necessary preparation for extraction ([`ExtractSchedule`](crate::ExtractSchedule)), which copies over component data from the main
42/// to the render world for these entities.
43///
44/// ```text
45/// |--------------------------------------------------------------------|
46/// |      |         |          Main world update                        |
47/// | sync | extract |---------------------------------------------------|
48/// |      |         |         Render world update                       |
49/// |--------------------------------------------------------------------|
50/// ```
51///
52/// An example for synchronized main entities 1v1 and 18v1
53///
54/// ```text
55/// |---------------------------Main World------------------------------|
56/// |  Entity  |                    Component                           |
57/// |-------------------------------------------------------------------|
58/// | ID: 1v1  | PointLight | RenderEntity(ID: 3V1) | SyncToRenderWorld |
59/// | ID: 18v1 | PointLight | RenderEntity(ID: 5V1) | SyncToRenderWorld |
60/// |-------------------------------------------------------------------|
61///
62/// |----------Render World-----------|
63/// |  Entity  |       Component      |
64/// |---------------------------------|
65/// | ID: 3v1  | MainEntity(ID: 1V1)  |
66/// | ID: 5v1  | MainEntity(ID: 18V1) |
67/// |---------------------------------|
68///
69/// ```
70///
71/// Note that this effectively establishes a link between the main world entity and the render world entity.
72/// Not every entity needs to be synchronized, however; only entities with the [`SyncToRenderWorld`] component are synced.
73/// Adding [`SyncToRenderWorld`] to a main world component will establish such a link.
74/// Once a synchronized main entity is despawned, its corresponding render entity will be automatically
75/// despawned in the next `sync`.
76///
77/// The sync step does not copy any of component data between worlds, since its often not necessary to transfer over all
78/// the components of a main world entity.
79/// The render world probably cares about a `Position` component, but not a `Velocity` component.
80/// The extraction happens in its own step, independently from, and after synchronization.
81///
82/// Moreover, [`SyncWorldPlugin`] only synchronizes *entities*. [`RenderAsset`](crate::render_asset::RenderAsset)s like meshes and textures are handled
83/// differently.
84///
85/// [`PipelinedRenderingPlugin`]: crate::pipelined_rendering::PipelinedRenderingPlugin
86/// [`ExtractComponentPlugin`]: crate::extract_component::ExtractComponentPlugin
87/// [`SyncComponentPlugin`]: crate::sync_component::SyncComponentPlugin
88#[derive(Default)]
89pub struct SyncWorldPlugin;
90
91impl Plugin for SyncWorldPlugin {
92    fn build(&self, app: &mut bevy_app::App) {
93        app.init_resource::<PendingSyncEntity>();
94        app.add_observer(
95            |trigger: Trigger<OnAdd, SyncToRenderWorld>, mut pending: ResMut<PendingSyncEntity>| {
96                pending.push(EntityRecord::Added(trigger.entity()));
97            },
98        );
99        app.add_observer(
100            |trigger: Trigger<OnRemove, SyncToRenderWorld>,
101             mut pending: ResMut<PendingSyncEntity>,
102             query: Query<&RenderEntity>| {
103                if let Ok(e) = query.get(trigger.entity()) {
104                    pending.push(EntityRecord::Removed(*e));
105                };
106            },
107        );
108    }
109}
110/// Marker component that indicates that its entity needs to be synchronized to the render world.
111///
112/// This component is automatically added as a required component by [`ExtractComponentPlugin`] and [`SyncComponentPlugin`].
113/// For more information see [`SyncWorldPlugin`].
114///
115/// NOTE: This component should persist throughout the entity's entire lifecycle.
116/// If this component is removed from its entity, the entity will be despawned.
117///
118/// [`ExtractComponentPlugin`]: crate::extract_component::ExtractComponentPlugin
119/// [`SyncComponentPlugin`]: crate::sync_component::SyncComponentPlugin
120#[derive(Component, Copy, Clone, Debug, Default, Reflect)]
121#[reflect[Component]]
122#[component(storage = "SparseSet")]
123pub struct SyncToRenderWorld;
124
125/// Component added on the main world entities that are synced to the Render World in order to keep track of the corresponding render world entity.
126///
127/// Can also be used as a newtype wrapper for render world entities.
128#[derive(Component, Deref, Copy, Clone, Debug, Eq, Hash, PartialEq)]
129pub struct RenderEntity(Entity);
130impl RenderEntity {
131    #[inline]
132    pub fn id(&self) -> Entity {
133        self.0
134    }
135}
136
137impl From<Entity> for RenderEntity {
138    fn from(entity: Entity) -> Self {
139        RenderEntity(entity)
140    }
141}
142
143/// Component added on the render world entities to keep track of the corresponding main world entity.
144///
145/// Can also be used as a newtype wrapper for main world entities.
146#[derive(Component, Deref, Copy, Clone, Debug, Eq, Hash, PartialEq)]
147pub struct MainEntity(Entity);
148impl MainEntity {
149    #[inline]
150    pub fn id(&self) -> Entity {
151        self.0
152    }
153}
154
155impl From<Entity> for MainEntity {
156    fn from(entity: Entity) -> Self {
157        MainEntity(entity)
158    }
159}
160
161/// A [`HashMap`](hashbrown::HashMap) pre-configured to use [`EntityHash`] hashing with a [`MainEntity`].
162pub type MainEntityHashMap<V> = hashbrown::HashMap<MainEntity, V, EntityHash>;
163
164/// A [`HashSet`](hashbrown::HashSet) pre-configured to use [`EntityHash`] hashing with a [`MainEntity`]..
165pub type MainEntityHashSet = hashbrown::HashSet<MainEntity, EntityHash>;
166
167/// Marker component that indicates that its entity needs to be despawned at the end of the frame.
168#[derive(Component, Copy, Clone, Debug, Default, Reflect)]
169pub struct TemporaryRenderEntity;
170
171/// A record enum to what entities with [`SyncToRenderWorld`] have been added or removed.
172#[derive(Debug)]
173pub(crate) enum EntityRecord {
174    /// When an entity is spawned on the main world, notify the render world so that it can spawn a corresponding
175    /// entity. This contains the main world entity.
176    Added(Entity),
177    /// When an entity is despawned on the main world, notify the render world so that the corresponding entity can be
178    /// despawned. This contains the render world entity.
179    Removed(RenderEntity),
180    /// When a component is removed from an entity, notify the render world so that the corresponding component can be
181    /// removed. This contains the main world entity.
182    ComponentRemoved(Entity),
183}
184
185// Entity Record in MainWorld pending to Sync
186#[derive(Resource, Default, Deref, DerefMut)]
187pub(crate) struct PendingSyncEntity {
188    records: Vec<EntityRecord>,
189}
190
191pub(crate) fn entity_sync_system(main_world: &mut World, render_world: &mut World) {
192    main_world.resource_scope(|world, mut pending: Mut<PendingSyncEntity>| {
193        // TODO : batching record
194        for record in pending.drain(..) {
195            match record {
196                EntityRecord::Added(e) => {
197                    if let Ok(mut main_entity) = world.get_entity_mut(e) {
198                        match main_entity.entry::<RenderEntity>() {
199                            bevy_ecs::world::Entry::Occupied(_) => {
200                                panic!("Attempting to synchronize an entity that has already been synchronized!");
201                            }
202                            bevy_ecs::world::Entry::Vacant(entry) => {
203                                let id = render_world.spawn(MainEntity(e)).id();
204
205                                entry.insert(RenderEntity(id));
206                            }
207                        };
208                    }
209                }
210                EntityRecord::Removed(render_entity) => {
211                    if let Ok(ec) = render_world.get_entity_mut(render_entity.id()) {
212                        ec.despawn();
213                    };
214                }
215                EntityRecord::ComponentRemoved(main_entity) => {
216                    let Some(mut render_entity) = world.get_mut::<RenderEntity>(main_entity) else {
217                        continue;
218                    };
219                    if let Ok(render_world_entity) = render_world.get_entity_mut(render_entity.id()) {
220                        // In order to handle components that extract to derived components, we clear the entity
221                        // and let the extraction system re-add the components.
222                        render_world_entity.despawn();
223
224                        let id = render_world.spawn(MainEntity(main_entity)).id();
225                        render_entity.0 = id;
226                    }
227                },
228            }
229        }
230    });
231}
232
233pub(crate) fn despawn_temporary_render_entities(
234    world: &mut World,
235    state: &mut SystemState<Query<Entity, With<TemporaryRenderEntity>>>,
236    mut local: Local<Vec<Entity>>,
237) {
238    let query = state.get(world);
239
240    local.extend(query.iter());
241
242    // Ensure next frame allocation keeps order
243    local.sort_unstable_by_key(|e| e.index());
244    for e in local.drain(..).rev() {
245        world.despawn(e);
246    }
247}
248
249/// This module exists to keep the complex unsafe code out of the main module.
250///
251/// The implementations for both [`MainEntity`] and [`RenderEntity`] should stay in sync,
252/// and are based off of the `&T` implementation in `bevy_ecs`.
253mod render_entities_world_query_impls {
254    use super::{MainEntity, RenderEntity};
255
256    use bevy_ecs::{
257        archetype::Archetype,
258        component::{ComponentId, Components, Tick},
259        entity::Entity,
260        query::{FilteredAccess, QueryData, ReadOnlyQueryData, WorldQuery},
261        storage::{Table, TableRow},
262        world::{unsafe_world_cell::UnsafeWorldCell, World},
263    };
264
265    /// SAFETY: defers completely to `&RenderEntity` implementation,
266    /// and then only modifies the output safely.
267    unsafe impl WorldQuery for RenderEntity {
268        type Item<'w> = Entity;
269        type Fetch<'w> = <&'static RenderEntity as WorldQuery>::Fetch<'w>;
270        type State = <&'static RenderEntity as WorldQuery>::State;
271
272        fn shrink<'wlong: 'wshort, 'wshort>(item: Entity) -> Entity {
273            item
274        }
275
276        fn shrink_fetch<'wlong: 'wshort, 'wshort>(
277            fetch: Self::Fetch<'wlong>,
278        ) -> Self::Fetch<'wshort> {
279            fetch
280        }
281
282        #[inline]
283        unsafe fn init_fetch<'w>(
284            world: UnsafeWorldCell<'w>,
285            component_id: &ComponentId,
286            last_run: Tick,
287            this_run: Tick,
288        ) -> Self::Fetch<'w> {
289            // SAFETY: defers to the `&T` implementation, with T set to `RenderEntity`.
290            unsafe {
291                <&RenderEntity as WorldQuery>::init_fetch(world, component_id, last_run, this_run)
292            }
293        }
294
295        const IS_DENSE: bool = <&'static RenderEntity as WorldQuery>::IS_DENSE;
296
297        #[inline]
298        unsafe fn set_archetype<'w>(
299            fetch: &mut Self::Fetch<'w>,
300            component_id: &ComponentId,
301            archetype: &'w Archetype,
302            table: &'w Table,
303        ) {
304            // SAFETY: defers to the `&T` implementation, with T set to `RenderEntity`.
305            unsafe {
306                <&RenderEntity as WorldQuery>::set_archetype(fetch, component_id, archetype, table);
307            }
308        }
309
310        #[inline]
311        unsafe fn set_table<'w>(
312            fetch: &mut Self::Fetch<'w>,
313            &component_id: &ComponentId,
314            table: &'w Table,
315        ) {
316            // SAFETY: defers to the `&T` implementation, with T set to `RenderEntity`.
317            unsafe { <&RenderEntity as WorldQuery>::set_table(fetch, &component_id, table) }
318        }
319
320        #[inline(always)]
321        unsafe fn fetch<'w>(
322            fetch: &mut Self::Fetch<'w>,
323            entity: Entity,
324            table_row: TableRow,
325        ) -> Self::Item<'w> {
326            // SAFETY: defers to the `&T` implementation, with T set to `RenderEntity`.
327            let component =
328                unsafe { <&RenderEntity as WorldQuery>::fetch(fetch, entity, table_row) };
329            component.id()
330        }
331
332        fn update_component_access(
333            &component_id: &ComponentId,
334            access: &mut FilteredAccess<ComponentId>,
335        ) {
336            <&RenderEntity as WorldQuery>::update_component_access(&component_id, access);
337        }
338
339        fn init_state(world: &mut World) -> ComponentId {
340            <&RenderEntity as WorldQuery>::init_state(world)
341        }
342
343        fn get_state(components: &Components) -> Option<Self::State> {
344            <&RenderEntity as WorldQuery>::get_state(components)
345        }
346
347        fn matches_component_set(
348            &state: &ComponentId,
349            set_contains_id: &impl Fn(ComponentId) -> bool,
350        ) -> bool {
351            <&RenderEntity as WorldQuery>::matches_component_set(&state, set_contains_id)
352        }
353    }
354
355    // SAFETY: Component access of Self::ReadOnly is a subset of Self.
356    // Self::ReadOnly matches exactly the same archetypes/tables as Self.
357    unsafe impl QueryData for RenderEntity {
358        type ReadOnly = RenderEntity;
359    }
360
361    // SAFETY: the underlying `Entity` is copied, and no mutable access is provided.
362    unsafe impl ReadOnlyQueryData for RenderEntity {}
363
364    /// SAFETY: defers completely to `&RenderEntity` implementation,
365    /// and then only modifies the output safely.
366    unsafe impl WorldQuery for MainEntity {
367        type Item<'w> = Entity;
368        type Fetch<'w> = <&'static MainEntity as WorldQuery>::Fetch<'w>;
369        type State = <&'static MainEntity as WorldQuery>::State;
370
371        fn shrink<'wlong: 'wshort, 'wshort>(item: Entity) -> Entity {
372            item
373        }
374
375        fn shrink_fetch<'wlong: 'wshort, 'wshort>(
376            fetch: Self::Fetch<'wlong>,
377        ) -> Self::Fetch<'wshort> {
378            fetch
379        }
380
381        #[inline]
382        unsafe fn init_fetch<'w>(
383            world: UnsafeWorldCell<'w>,
384            component_id: &ComponentId,
385            last_run: Tick,
386            this_run: Tick,
387        ) -> Self::Fetch<'w> {
388            // SAFETY: defers to the `&T` implementation, with T set to `MainEntity`.
389            unsafe {
390                <&MainEntity as WorldQuery>::init_fetch(world, component_id, last_run, this_run)
391            }
392        }
393
394        const IS_DENSE: bool = <&'static MainEntity as WorldQuery>::IS_DENSE;
395
396        #[inline]
397        unsafe fn set_archetype<'w>(
398            fetch: &mut Self::Fetch<'w>,
399            component_id: &ComponentId,
400            archetype: &'w Archetype,
401            table: &'w Table,
402        ) {
403            // SAFETY: defers to the `&T` implementation, with T set to `MainEntity`.
404            unsafe {
405                <&MainEntity as WorldQuery>::set_archetype(fetch, component_id, archetype, table);
406            }
407        }
408
409        #[inline]
410        unsafe fn set_table<'w>(
411            fetch: &mut Self::Fetch<'w>,
412            &component_id: &ComponentId,
413            table: &'w Table,
414        ) {
415            // SAFETY: defers to the `&T` implementation, with T set to `MainEntity`.
416            unsafe { <&MainEntity as WorldQuery>::set_table(fetch, &component_id, table) }
417        }
418
419        #[inline(always)]
420        unsafe fn fetch<'w>(
421            fetch: &mut Self::Fetch<'w>,
422            entity: Entity,
423            table_row: TableRow,
424        ) -> Self::Item<'w> {
425            // SAFETY: defers to the `&T` implementation, with T set to `MainEntity`.
426            let component = unsafe { <&MainEntity as WorldQuery>::fetch(fetch, entity, table_row) };
427            component.id()
428        }
429
430        fn update_component_access(
431            &component_id: &ComponentId,
432            access: &mut FilteredAccess<ComponentId>,
433        ) {
434            <&MainEntity as WorldQuery>::update_component_access(&component_id, access);
435        }
436
437        fn init_state(world: &mut World) -> ComponentId {
438            <&MainEntity as WorldQuery>::init_state(world)
439        }
440
441        fn get_state(components: &Components) -> Option<Self::State> {
442            <&MainEntity as WorldQuery>::get_state(components)
443        }
444
445        fn matches_component_set(
446            &state: &ComponentId,
447            set_contains_id: &impl Fn(ComponentId) -> bool,
448        ) -> bool {
449            <&MainEntity as WorldQuery>::matches_component_set(&state, set_contains_id)
450        }
451    }
452
453    // SAFETY: Component access of Self::ReadOnly is a subset of Self.
454    // Self::ReadOnly matches exactly the same archetypes/tables as Self.
455    unsafe impl QueryData for MainEntity {
456        type ReadOnly = MainEntity;
457    }
458
459    // SAFETY: the underlying `Entity` is copied, and no mutable access is provided.
460    unsafe impl ReadOnlyQueryData for MainEntity {}
461}
462
463#[cfg(test)]
464mod tests {
465    use bevy_ecs::{
466        component::Component,
467        entity::Entity,
468        observer::Trigger,
469        query::With,
470        system::{Query, ResMut},
471        world::{OnAdd, OnRemove, World},
472    };
473
474    use super::{
475        entity_sync_system, EntityRecord, MainEntity, PendingSyncEntity, RenderEntity,
476        SyncToRenderWorld,
477    };
478
479    #[derive(Component)]
480    struct RenderDataComponent;
481
482    #[test]
483    fn sync_world() {
484        let mut main_world = World::new();
485        let mut render_world = World::new();
486        main_world.init_resource::<PendingSyncEntity>();
487
488        main_world.add_observer(
489            |trigger: Trigger<OnAdd, SyncToRenderWorld>, mut pending: ResMut<PendingSyncEntity>| {
490                pending.push(EntityRecord::Added(trigger.entity()));
491            },
492        );
493        main_world.add_observer(
494            |trigger: Trigger<OnRemove, SyncToRenderWorld>,
495             mut pending: ResMut<PendingSyncEntity>,
496             query: Query<&RenderEntity>| {
497                if let Ok(e) = query.get(trigger.entity()) {
498                    pending.push(EntityRecord::Removed(*e));
499                };
500            },
501        );
502
503        // spawn some empty entities for test
504        for _ in 0..99 {
505            main_world.spawn_empty();
506        }
507
508        // spawn
509        let main_entity = main_world
510            .spawn(RenderDataComponent)
511            // indicates that its entity needs to be synchronized to the render world
512            .insert(SyncToRenderWorld)
513            .id();
514
515        entity_sync_system(&mut main_world, &mut render_world);
516
517        let mut q = render_world.query_filtered::<Entity, With<MainEntity>>();
518
519        // Only one synchronized entity
520        assert!(q.iter(&render_world).count() == 1);
521
522        let render_entity = q.get_single(&render_world).unwrap();
523        let render_entity_component = main_world.get::<RenderEntity>(main_entity).unwrap();
524
525        assert!(render_entity_component.id() == render_entity);
526
527        let main_entity_component = render_world
528            .get::<MainEntity>(render_entity_component.id())
529            .unwrap();
530
531        assert!(main_entity_component.id() == main_entity);
532
533        // despawn
534        main_world.despawn(main_entity);
535
536        entity_sync_system(&mut main_world, &mut render_world);
537
538        // Only one synchronized entity
539        assert!(q.iter(&render_world).count() == 0);
540    }
541}