Skip to main content

bevy_render/
extract_param.rs

1use crate::MainWorld;
2use bevy_ecs::{
3    change_detection::Tick,
4    prelude::*,
5    query::FilteredAccessSet,
6    system::{
7        ReadOnlySystemParam, SystemMeta, SystemParam, SystemParamItem, SystemParamValidationError,
8        SystemState,
9    },
10    world::unsafe_world_cell::UnsafeWorldCell,
11};
12use core::ops::{Deref, DerefMut};
13
14/// A helper for accessing [`MainWorld`] content using a system parameter.
15///
16/// A [`SystemParam`] adapter which applies the contained `SystemParam` to the [`World`]
17/// contained in [`MainWorld`]. This parameter only works for systems run
18/// during the [`ExtractSchedule`](crate::ExtractSchedule).
19///
20/// This requires that the contained [`SystemParam`] does not mutate the world, as it
21/// uses a read-only reference to [`MainWorld`] internally.
22///
23/// ## Context
24///
25/// [`ExtractSchedule`] is used to extract (move) data from the simulation world ([`MainWorld`]) to the
26/// render world. The render world drives rendering each frame (generally to a `Window`).
27/// This design is used to allow performing calculations related to rendering a prior frame at the same
28/// time as the next frame is simulated, which increases throughput (FPS).
29///
30/// [`Extract`] is used to get data from the main world during [`ExtractSchedule`].
31///
32/// ## Examples
33///
34/// ```
35/// use bevy_ecs::prelude::*;
36/// use bevy_render::Extract;
37/// use bevy_render::sync_world::RenderEntity;
38/// # #[derive(Component)]
39/// // Do make sure to sync the cloud entities before extracting them.
40/// # struct Cloud;
41/// fn extract_clouds(mut commands: Commands, clouds: Extract<Query<RenderEntity, With<Cloud>>>) {
42///     for cloud in &clouds {
43///         commands.entity(cloud).insert(Cloud);
44///     }
45/// }
46/// ```
47///
48/// [`ExtractSchedule`]: crate::ExtractSchedule
49/// [Window]: bevy_window::Window
50pub struct Extract<'w, 's, P>
51where
52    P: ReadOnlySystemParam + 'static,
53{
54    item: SystemParamItem<'w, 's, P>,
55}
56
57#[doc(hidden)]
58pub struct ExtractState<P: SystemParam + 'static> {
59    state: SystemState<P>,
60    main_world_state: <Res<'static, MainWorld> as SystemParam>::State,
61}
62
63// SAFETY: The only `World` access (`Res<MainWorld>`) is read-only.
64unsafe impl<P> ReadOnlySystemParam for Extract<'_, '_, P> where P: ReadOnlySystemParam {}
65
66// SAFETY: The only `World` access is properly registered by `Res<MainWorld>::init_state`.
67// This call will also ensure that there are no conflicts with prior params.
68unsafe impl<P> SystemParam for Extract<'_, '_, P>
69where
70    P: ReadOnlySystemParam,
71{
72    type State = ExtractState<P>;
73    type Item<'w, 's> = Extract<'w, 's, P>;
74
75    fn init_state(world: &mut World) -> Self::State {
76        let mut main_world = world.resource_mut::<MainWorld>();
77        ExtractState {
78            state: SystemState::new(&mut main_world),
79            main_world_state: Res::<MainWorld>::init_state(world),
80        }
81    }
82
83    fn init_access(
84        state: &Self::State,
85        system_meta: &mut SystemMeta,
86        component_access_set: &mut FilteredAccessSet,
87        world: &mut World,
88    ) {
89        Res::<MainWorld>::init_access(
90            &state.main_world_state,
91            system_meta,
92            component_access_set,
93            world,
94        );
95    }
96
97    #[inline]
98    unsafe fn get_param<'w, 's>(
99        state: &'s mut Self::State,
100        system_meta: &SystemMeta,
101        world: UnsafeWorldCell<'w>,
102        change_tick: Tick,
103    ) -> Result<Self::Item<'w, 's>, SystemParamValidationError> {
104        // SAFETY:
105        // - The caller ensures that `world` is the same one that `init_state` was called with.
106        // - The caller ensures that no other `SystemParam`s will conflict with the accesses we have registered.
107        let main_world = unsafe {
108            Res::<MainWorld>::get_param(
109                &mut state.main_world_state,
110                system_meta,
111                world,
112                change_tick,
113            )?
114        };
115        let item = state.state.get(main_world.into_inner())?;
116        Ok(Extract { item })
117    }
118}
119
120impl<'w, 's, P> Deref for Extract<'w, 's, P>
121where
122    P: ReadOnlySystemParam,
123{
124    type Target = SystemParamItem<'w, 's, P>;
125
126    #[inline]
127    fn deref(&self) -> &Self::Target {
128        &self.item
129    }
130}
131
132impl<'w, 's, P> DerefMut for Extract<'w, 's, P>
133where
134    P: ReadOnlySystemParam,
135{
136    #[inline]
137    fn deref_mut(&mut self) -> &mut Self::Target {
138        &mut self.item
139    }
140}
141
142impl<'a, 'w, 's, P> IntoIterator for &'a Extract<'w, 's, P>
143where
144    P: ReadOnlySystemParam,
145    &'a SystemParamItem<'w, 's, P>: IntoIterator,
146{
147    type Item = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::Item;
148    type IntoIter = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::IntoIter;
149
150    fn into_iter(self) -> Self::IntoIter {
151        (&self.item).into_iter()
152    }
153}