diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 4bb0f8db5c7712..b52df6c08cc0f1 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -4,7 +4,7 @@ use crate::{ query::{Access, FilteredAccessSet}, system::{ check_system_change_tick, ReadOnlySystemParamFetch, System, SystemParam, SystemParamFetch, - SystemParamItem, SystemParamState, + SystemParamState, }, world::{World, WorldId}, }; @@ -46,11 +46,6 @@ impl SystemMeta { pub fn set_non_send(&mut self) { self.is_send = false; } - - #[inline] - pub(crate) fn check_change_tick(&mut self, change_tick: u32) { - check_system_change_tick(&mut self.last_change_tick, change_tick, self.name.as_ref()); - } } // TODO: Actually use this in FunctionSystem. We should probably only do this once Systems are constructed using a World reference @@ -126,10 +121,6 @@ impl SystemState { self.world_id == world.id() } - pub(crate) fn new_archetype(&mut self, archetype: &Archetype) { - self.param_state.new_archetype(archetype, &mut self.meta); - } - fn validate_world_and_update_archetypes(&mut self, world: &World) { assert!(self.matches_world(world), "Encountered a mismatched World. A SystemState cannot be used with Worlds other than the one it was created with."); let archetypes = world.archetypes(); @@ -168,74 +159,6 @@ impl SystemState { } } -/// A trait for defining systems with a [`SystemParam`] associated type. -/// -/// This facilitates the creation of systems that are generic over some trait -/// and that use that trait's associated types as `SystemParam`s. -pub trait RunSystem: Send + Sync + 'static { - /// The `SystemParam` type passed to the system when it runs. - type Param: SystemParam; - - /// Runs the system. - fn run(param: SystemParamItem); - - /// Creates a concrete instance of the system for the specified `World`. - fn system(world: &mut World) -> ParamSystem { - ParamSystem { - run: Self::run, - state: SystemState::new(world), - } - } -} - -pub struct ParamSystem { - state: SystemState

, - run: fn(SystemParamItem

), -} - -impl System for ParamSystem

{ - type In = (); - - type Out = (); - - fn name(&self) -> Cow<'static, str> { - self.state.meta().name.clone() - } - - fn new_archetype(&mut self, archetype: &Archetype) { - self.state.new_archetype(archetype); - } - - fn component_access(&self) -> &Access { - self.state.meta().component_access_set.combined_access() - } - - fn archetype_component_access(&self) -> &Access { - &self.state.meta().archetype_component_access - } - - fn is_send(&self) -> bool { - self.state.meta().is_send() - } - - unsafe fn run_unsafe(&mut self, _input: Self::In, world: &World) -> Self::Out { - let param = self.state.get_unchecked_manual(world); - (self.run)(param); - } - - fn apply_buffers(&mut self, world: &mut World) { - self.state.apply(world); - } - - fn initialize(&mut self, _world: &mut World) { - // already initialized by nature of the SystemState being constructed - } - - fn check_change_tick(&mut self, change_tick: u32) { - self.state.meta.check_change_tick(change_tick); - } -} - /// Conversion trait to turn something into a [`System`]. /// /// Use this to get a system from a function. Also note that every system implements this trait as diff --git a/crates/bevy_render/src/render_asset.rs b/crates/bevy_render/src/render_asset.rs index 214036520766c1..4efb2a95117907 100644 --- a/crates/bevy_render/src/render_asset.rs +++ b/crates/bevy_render/src/render_asset.rs @@ -3,7 +3,7 @@ use bevy_app::{App, Plugin}; use bevy_asset::{Asset, AssetEvent, Assets, Handle}; use bevy_ecs::{ prelude::*, - system::{lifetimeless::*, RunSystem, SystemParam, SystemParamItem}, + system::{StaticSystemParam, SystemParam, SystemParamItem}, }; use bevy_utils::{HashMap, HashSet}; use std::marker::PhantomData; @@ -55,13 +55,12 @@ impl Default for RenderAssetPlugin { impl Plugin for RenderAssetPlugin { fn build(&self, app: &mut App) { if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { - let prepare_asset_system = PrepareAssetSystem::::system(&mut render_app.world); render_app .init_resource::>() .init_resource::>() .init_resource::>() .add_system_to_stage(RenderStage::Extract, extract_render_asset::) - .add_system_to_stage(RenderStage::Prepare, prepare_asset_system); + .add_system_to_stage(RenderStage::Prepare, prepare_assets::); } } } @@ -122,14 +121,6 @@ fn extract_render_asset( }) } -/// Specifies all ECS data required by [`PrepareAssetSystem`]. -pub type RenderAssetParams = ( - SResMut>, - SResMut>, - SResMut>, - ::Param, -); - // TODO: consider storing inside system? /// All assets that should be prepared next frame. pub struct PrepareNextFrameAssets { @@ -146,38 +137,36 @@ impl Default for PrepareNextFrameAssets { /// This system prepares all assets of the corresponding [`RenderAsset`] type /// which where extracted this frame for the GPU. -pub struct PrepareAssetSystem(PhantomData); - -impl RunSystem for PrepareAssetSystem { - type Param = RenderAssetParams; - - fn run( - (mut extracted_assets, mut render_assets, mut prepare_next_frame, mut param): SystemParamItem, - ) { - let mut queued_assets = std::mem::take(&mut prepare_next_frame.assets); - for (handle, extracted_asset) in queued_assets.drain(..) { - match R::prepare_asset(extracted_asset, &mut param) { - Ok(prepared_asset) => { - render_assets.insert(handle, prepared_asset); - } - Err(PrepareAssetError::RetryNextUpdate(extracted_asset)) => { - prepare_next_frame.assets.push((handle, extracted_asset)); - } +fn prepare_assets( + mut extracted_assets: ResMut>, + mut render_assets: ResMut>, + mut prepare_next_frame: ResMut>, + param: StaticSystemParam<::Param>, +) { + let mut param = param.inner(); + let mut queued_assets = std::mem::take(&mut prepare_next_frame.assets); + for (handle, extracted_asset) in queued_assets.drain(..) { + match R::prepare_asset(extracted_asset, &mut param) { + Ok(prepared_asset) => { + render_assets.insert(handle, prepared_asset); + } + Err(PrepareAssetError::RetryNextUpdate(extracted_asset)) => { + prepare_next_frame.assets.push((handle, extracted_asset)); } } + } - for removed in std::mem::take(&mut extracted_assets.removed) { - render_assets.remove(&removed); - } + for removed in std::mem::take(&mut extracted_assets.removed) { + render_assets.remove(&removed); + } - for (handle, extracted_asset) in std::mem::take(&mut extracted_assets.extracted) { - match R::prepare_asset(extracted_asset, &mut param) { - Ok(prepared_asset) => { - render_assets.insert(handle, prepared_asset); - } - Err(PrepareAssetError::RetryNextUpdate(extracted_asset)) => { - prepare_next_frame.assets.push((handle, extracted_asset)); - } + for (handle, extracted_asset) in std::mem::take(&mut extracted_assets.extracted) { + match R::prepare_asset(extracted_asset, &mut param) { + Ok(prepared_asset) => { + render_assets.insert(handle, prepared_asset); + } + Err(PrepareAssetError::RetryNextUpdate(extracted_asset)) => { + prepare_next_frame.assets.push((handle, extracted_asset)); } } } diff --git a/crates/bevy_render/src/render_component.rs b/crates/bevy_render/src/render_component.rs index 1c72786f89f160..1f348e38a7ce36 100644 --- a/crates/bevy_render/src/render_component.rs +++ b/crates/bevy_render/src/render_component.rs @@ -9,10 +9,7 @@ use bevy_ecs::{ component::Component, prelude::*, query::{FilterFetch, QueryItem, WorldQuery}, - system::{ - lifetimeless::{Read, SCommands, SQuery}, - RunSystem, SystemParamItem, - }, + system::{lifetimeless::Read, StaticSystemParam}, }; use std::{marker::PhantomData, ops::Deref}; @@ -142,9 +139,8 @@ where ::Fetch: FilterFetch, { fn build(&self, app: &mut App) { - let system = ExtractComponentSystem::::system(&mut app.world); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { - render_app.add_system_to_stage(RenderStage::Extract, system); + render_app.add_system_to_stage(RenderStage::Extract, extract_components::); } } } @@ -160,25 +156,17 @@ impl ExtractComponent for Handle { } /// This system extracts all components of the corresponding [`ExtractComponent`] type. -pub struct ExtractComponentSystem(PhantomData); - -impl RunSystem for ExtractComponentSystem -where +fn extract_components( + mut commands: Commands, + mut previous_len: Local, + mut query: StaticSystemParam>, +) where ::Fetch: FilterFetch, { - type Param = ( - SCommands, - // the previous amount of extracted components - Local<'static, usize>, - SQuery<(Entity, C::Query), C::Filter>, - ); - - fn run((mut commands, mut previous_len, mut query): SystemParamItem) { - let mut values = Vec::with_capacity(*previous_len); - for (entity, query_item) in query.iter_mut() { - values.push((entity, (C::extract_component(query_item),))); - } - *previous_len = values.len(); - commands.insert_or_spawn_batch(values); + let mut values = Vec::with_capacity(*previous_len); + for (entity, query_item) in query.iter_mut() { + values.push((entity, (C::extract_component(query_item),))); } + *previous_len = values.len(); + commands.insert_or_spawn_batch(values); }