Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Introduce SystemLabel's for RenderAssetPlugin, and change Image preparation system to run before others #3917

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions crates/bevy_render/src/render_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,63 @@ pub trait RenderAsset: Asset {
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>>;
}

#[derive(Clone, Hash, Debug, PartialEq, Eq, SystemLabel)]
pub enum PrepareAssetLabel {
blaind marked this conversation as resolved.
Show resolved Hide resolved
PreAssetPrepare,
AssetPrepare,
PostAssetPrepare,
}

impl Default for PrepareAssetLabel {
fn default() -> Self {
Self::AssetPrepare
}
}

/// 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<A: RenderAsset>(PhantomData<fn() -> A>);
pub struct RenderAssetPlugin<A: RenderAsset> {
prepare_asset_label: PrepareAssetLabel,
phantom: PhantomData<fn() -> A>,
}

impl<A: RenderAsset> RenderAssetPlugin<A> {
pub fn with_prepare_asset_label(prepare_asset_label: PrepareAssetLabel) -> Self {
blaind marked this conversation as resolved.
Show resolved Hide resolved
Self {
prepare_asset_label,
phantom: PhantomData,
}
}
}

impl<A: RenderAsset> Default for RenderAssetPlugin<A> {
fn default() -> Self {
Self(PhantomData)
Self {
prepare_asset_label: Default::default(),
phantom: PhantomData,
}
}
}

impl<A: RenderAsset> Plugin for RenderAssetPlugin<A> {
fn build(&self, app: &mut App) {
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
let prepare_asset_system = PrepareAssetSystem::<A>::system(&mut render_app.world);
let prepare_asset_system = PrepareAssetSystem::<A>::system(&mut render_app.world)
.label(self.prepare_asset_label.clone());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any problem with using the same labels for many systems?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope this is fine / a pattern we use in a number of places.


let prepare_asset_system = match self.prepare_asset_label {
blaind marked this conversation as resolved.
Show resolved Hide resolved
PrepareAssetLabel::PreAssetPrepare => prepare_asset_system,
PrepareAssetLabel::AssetPrepare => {
prepare_asset_system.after(PrepareAssetLabel::PreAssetPrepare)
}
PrepareAssetLabel::PostAssetPrepare => {
prepare_asset_system.after(PrepareAssetLabel::AssetPrepare)
}
};

render_app
.init_resource::<ExtractedAssets<A>>()
.init_resource::<RenderAssets<A>>()
Expand Down
11 changes: 8 additions & 3 deletions crates/bevy_render/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -40,8 +43,10 @@ impl Plugin for ImagePlugin {
app.init_asset_loader::<HdrTextureLoader>();
}

app.add_plugin(RenderAssetPlugin::<Image>::default())
.add_asset::<Image>();
app.add_plugin(RenderAssetPlugin::<Image>::with_prepare_asset_label(
PrepareAssetLabel::PreAssetPrepare,
))
.add_asset::<Image>();
app.world
.get_resource_mut::<Assets<Image>>()
.unwrap()
Expand Down