From 5bed153ee47b8004d2a01a7cc79f6ef08a7eee55 Mon Sep 17 00:00:00 2001 From: Mika Vatanen Date: Fri, 11 Feb 2022 17:01:46 +0200 Subject: [PATCH 1/2] Add ordering system for asset prepare - image asset preparation first --- crates/bevy_render/src/render_asset.rs | 46 ++++++++++++++++++++++++-- crates/bevy_render/src/texture/mod.rs | 11 ++++-- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/crates/bevy_render/src/render_asset.rs b/crates/bevy_render/src/render_asset.rs index 214036520766c..a8057b6d283c5 100644 --- a/crates/bevy_render/src/render_asset.rs +++ b/crates/bevy_render/src/render_asset.rs @@ -39,23 +39,63 @@ pub trait RenderAsset: Asset { ) -> Result>; } +#[derive(Clone, Hash, Debug, PartialEq, Eq, SystemLabel)] +pub enum PrepareAssetLabel { + PreAssetExtract, + AssetExtract, + PostAssetExtract, +} + +impl Default for PrepareAssetLabel { + fn default() -> Self { + Self::AssetExtract + } +} + /// This plugin extracts the changed assets from the "app world" into the "render world" /// and prepares them for the GPU. They can then be accessed from the [`RenderAssets`] resource. /// /// Therefore it sets up the [`RenderStage::Extract`](crate::RenderStage::Extract) and /// [`RenderStage::Prepare`](crate::RenderStage::Prepare) steps for the specified [`RenderAsset`]. -pub struct RenderAssetPlugin(PhantomData A>); +pub struct RenderAssetPlugin { + prepare_asset_label: PrepareAssetLabel, + phantom: PhantomData A>, +} + +impl RenderAssetPlugin { + pub fn with_prepare_asset_label(prepare_asset_label: PrepareAssetLabel) -> Self { + Self { + prepare_asset_label, + phantom: PhantomData, + } + } +} impl Default for RenderAssetPlugin { fn default() -> Self { - Self(PhantomData) + Self { + prepare_asset_label: Default::default(), + phantom: PhantomData, + } } } 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); + let prepare_asset_system = PrepareAssetSystem::::system(&mut render_app.world) + .label(self.prepare_asset_label.clone()); + + let prepare_asset_system = match self.prepare_asset_label { + PrepareAssetLabel::PreAssetExtract => prepare_asset_system, + PrepareAssetLabel::AssetExtract => { + prepare_asset_system.after(PrepareAssetLabel::PreAssetExtract) + } + PrepareAssetLabel::PostAssetExtract => { + prepare_asset_system.after(PrepareAssetLabel::AssetExtract) + } + }; + render_app .init_resource::>() .init_resource::>() diff --git a/crates/bevy_render/src/texture/mod.rs b/crates/bevy_render/src/texture/mod.rs index fe53007baa31b..5615784562518 100644 --- a/crates/bevy_render/src/texture/mod.rs +++ b/crates/bevy_render/src/texture/mod.rs @@ -14,7 +14,10 @@ pub use hdr_texture_loader::*; pub use image_texture_loader::*; pub use texture_cache::*; -use crate::{render_asset::RenderAssetPlugin, RenderApp, RenderStage}; +use crate::{ + render_asset::{PrepareAssetLabel, RenderAssetPlugin}, + RenderApp, RenderStage, +}; use bevy_app::{App, Plugin}; use bevy_asset::{AddAsset, Assets}; @@ -40,8 +43,10 @@ impl Plugin for ImagePlugin { app.init_asset_loader::(); } - app.add_plugin(RenderAssetPlugin::::default()) - .add_asset::(); + app.add_plugin(RenderAssetPlugin::::with_prepare_asset_label( + PrepareAssetLabel::PreAssetExtract, + )) + .add_asset::(); app.world .get_resource_mut::>() .unwrap() From b0dbf41b4669bf5ff8e605d330641b355471be04 Mon Sep 17 00:00:00 2001 From: Mika Vatanen Date: Fri, 11 Feb 2022 17:10:46 +0200 Subject: [PATCH 2/2] Fix naming --- crates/bevy_render/src/render_asset.rs | 18 +++++++++--------- crates/bevy_render/src/texture/mod.rs | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/bevy_render/src/render_asset.rs b/crates/bevy_render/src/render_asset.rs index a8057b6d283c5..d92603364b4d8 100644 --- a/crates/bevy_render/src/render_asset.rs +++ b/crates/bevy_render/src/render_asset.rs @@ -41,14 +41,14 @@ pub trait RenderAsset: Asset { #[derive(Clone, Hash, Debug, PartialEq, Eq, SystemLabel)] pub enum PrepareAssetLabel { - PreAssetExtract, - AssetExtract, - PostAssetExtract, + PreAssetPrepare, + AssetPrepare, + PostAssetPrepare, } impl Default for PrepareAssetLabel { fn default() -> Self { - Self::AssetExtract + Self::AssetPrepare } } @@ -87,12 +87,12 @@ impl Plugin for RenderAssetPlugin { .label(self.prepare_asset_label.clone()); let prepare_asset_system = match self.prepare_asset_label { - PrepareAssetLabel::PreAssetExtract => prepare_asset_system, - PrepareAssetLabel::AssetExtract => { - prepare_asset_system.after(PrepareAssetLabel::PreAssetExtract) + PrepareAssetLabel::PreAssetPrepare => prepare_asset_system, + PrepareAssetLabel::AssetPrepare => { + prepare_asset_system.after(PrepareAssetLabel::PreAssetPrepare) } - PrepareAssetLabel::PostAssetExtract => { - prepare_asset_system.after(PrepareAssetLabel::AssetExtract) + PrepareAssetLabel::PostAssetPrepare => { + prepare_asset_system.after(PrepareAssetLabel::AssetPrepare) } }; diff --git a/crates/bevy_render/src/texture/mod.rs b/crates/bevy_render/src/texture/mod.rs index 5615784562518..0118e56ca91bf 100644 --- a/crates/bevy_render/src/texture/mod.rs +++ b/crates/bevy_render/src/texture/mod.rs @@ -44,7 +44,7 @@ impl Plugin for ImagePlugin { } app.add_plugin(RenderAssetPlugin::::with_prepare_asset_label( - PrepareAssetLabel::PreAssetExtract, + PrepareAssetLabel::PreAssetPrepare, )) .add_asset::(); app.world