Skip to content

Commit

Permalink
Sub app label changes (#2717)
Browse files Browse the repository at this point in the history
Makes some tweaks to the SubApp labeling introduced in #2695:

* Ergonomics improvements
* Removes unnecessary allocation when retrieving subapp label
* Removes the newly added "app macros" crate in favor of bevy_derive
* renamed RenderSubApp to RenderApp

@zicklag (for reference)
  • Loading branch information
cart committed Aug 24, 2021
1 parent e290a7e commit 9898469
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 72 deletions.
1 change: 0 additions & 1 deletion crates/bevy_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ bevy_derive = { path = "../bevy_derive", version = "0.5.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.5.0", optional = true }
bevy_utils = { path = "../bevy_utils", version = "0.5.0" }
bevy_app_macros = { path = "./macros", version = "0.5.0" }

# other
serde = { version = "1.0", features = ["derive"], optional = true }
Expand Down
16 changes: 0 additions & 16 deletions crates/bevy_app/macros/Cargo.toml

This file was deleted.

19 changes: 0 additions & 19 deletions crates/bevy_app/macros/src/lib.rs

This file was deleted.

27 changes: 18 additions & 9 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use bevy_derive::AppLabel;

use crate::{CoreStage, Events, Plugin, PluginGroup, PluginGroupBuilder, StartupStage};
use bevy_ecs::{
component::{Component, ComponentDescriptor},
Expand All @@ -10,13 +12,10 @@ use bevy_ecs::{
use bevy_utils::{tracing::debug, HashMap};
use std::{fmt::Debug, hash::Hash};

pub use bevy_app_macros::SubAppLabel;

#[cfg(feature = "trace")]
use bevy_utils::tracing::info_span;

bevy_utils::define_label!(SubAppLabel);
type BoxedSubAppLabel = Box<dyn SubAppLabel>;
bevy_utils::define_label!(AppLabel);

#[allow(clippy::needless_doctest_main)]
/// Containers of app logic and data
Expand Down Expand Up @@ -45,7 +44,7 @@ pub struct App {
pub world: World,
pub runner: Box<dyn Fn(App)>,
pub schedule: Schedule,
sub_apps: HashMap<BoxedSubAppLabel, SubApp>,
sub_apps: HashMap<Box<dyn AppLabel>, SubApp>,
}

struct SubApp {
Expand Down Expand Up @@ -594,7 +593,7 @@ impl App {

pub fn add_sub_app(
&mut self,
label: impl SubAppLabel,
label: impl AppLabel,
app: App,
f: impl Fn(&mut World, &mut App) + 'static,
) -> &mut Self {
Expand All @@ -608,11 +607,21 @@ impl App {
self
}

pub fn sub_app_mut(&mut self, label: impl SubAppLabel) -> Option<&mut App> {
let label = Box::new(label) as BoxedSubAppLabel;
/// Retrieves a "sub app" stored inside this [App]. This will panic if the sub app does not exist.
pub fn sub_app(&mut self, label: impl AppLabel) -> &mut App {
match self.get_sub_app(label) {
Ok(app) => app,
Err(label) => panic!("Sub-App with label '{:?}' does not exist", label),
}
}

/// Retrieves a "sub app" inside this [App] with the given label, if it exists. Otherwise returns
/// an [Err] containing the given label.
pub fn get_sub_app(&mut self, label: impl AppLabel) -> Result<&mut App, impl AppLabel> {
self.sub_apps
.get_mut(&label)
.get_mut((&label) as &dyn AppLabel)
.map(|sub_app| &mut sub_app.app)
.ok_or(label)
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ mod render_resources;
mod resource;
mod shader_defs;

use bevy_macro_utils::{derive_label, BevyManifest};
use proc_macro::TokenStream;
use quote::format_ident;

/// Derives the FromResources trait. Each field must also implement the FromResources trait or this
/// will fail. FromResources is automatically implemented for types that implement Default.
Expand Down Expand Up @@ -60,3 +62,11 @@ pub fn bevy_main(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream {
enum_variant_meta::derive_enum_variant_meta(input)
}

#[proc_macro_derive(AppLabel)]
pub fn derive_app_label(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
let mut trait_path = BevyManifest::default().get_path("bevy_app");
trait_path.segments.push(format_ident!("AppLabel").into());
derive_label(input, trait_path)
}
4 changes: 2 additions & 2 deletions pipelined/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bevy_render2::{
renderer::RenderDevice,
texture::TextureCache,
view::{ExtractedView, ViewPlugin},
RenderStage, RenderSubApp, RenderWorld,
RenderApp, RenderStage, RenderWorld,
};

/// Resource that configures the clear color
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Plugin for CorePipelinePlugin {
fn build(&self, app: &mut App) {
app.init_resource::<ClearColor>();

let render_app = app.sub_app_mut(RenderSubApp).unwrap();
let render_app = app.sub_app(RenderApp);
render_app
.add_system_to_stage(RenderStage::Extract, extract_clear_color)
.add_system_to_stage(RenderStage::Extract, extract_core_pipeline_camera_phases)
Expand Down
4 changes: 2 additions & 2 deletions pipelined/bevy_pbr2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy_ecs::prelude::*;
use bevy_render2::{
render_graph::RenderGraph,
render_phase::{sort_phase_system, DrawFunctions},
RenderStage, RenderSubApp,
RenderApp, RenderStage,
};

pub mod draw_3d_graph {
Expand All @@ -30,7 +30,7 @@ impl Plugin for PbrPlugin {
app.add_plugin(StandardMaterialPlugin)
.init_resource::<AmbientLight>();

let render_app = app.sub_app_mut(RenderSubApp).unwrap();
let render_app = app.sub_app(RenderApp);
render_app
.add_system_to_stage(RenderStage::Extract, render::extract_meshes)
.add_system_to_stage(RenderStage::Extract, render::extract_lights)
Expand Down
5 changes: 2 additions & 3 deletions pipelined/bevy_render2/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use bundle::*;
pub use camera::*;
pub use projection::*;

use crate::{view::ExtractedView, RenderStage, RenderSubApp};
use crate::{view::ExtractedView, RenderApp, RenderStage};
use bevy_app::{App, CoreStage, Plugin};
use bevy_ecs::prelude::*;

Expand Down Expand Up @@ -40,8 +40,7 @@ impl Plugin for CameraPlugin {
CoreStage::PostUpdate,
crate::camera::camera_system::<PerspectiveProjection>,
);
let render_app = app.sub_app_mut(RenderSubApp).unwrap();
render_app
app.sub_app(RenderApp)
.init_resource::<ExtractedCameraNames>()
.add_system_to_stage(RenderStage::Extract, extract_cameras);
}
Expand Down
8 changes: 4 additions & 4 deletions pipelined/bevy_render2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
texture::ImagePlugin,
view::{ViewPlugin, WindowRenderPlugin},
};
use bevy_app::{App, Plugin, SubAppLabel};
use bevy_app::{App, AppLabel, Plugin};
use bevy_ecs::prelude::*;

#[derive(Default)]
Expand Down Expand Up @@ -74,8 +74,8 @@ impl DerefMut for RenderWorld {
}

/// Label for the rendering sub-app
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, SubAppLabel)]
pub struct RenderSubApp;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, AppLabel)]
pub struct RenderApp;

/// A "scratch" world used to avoid allocating new worlds every frame when
// swapping out the Render World.
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Plugin for RenderPlugin {
.init_resource::<RenderGraph>()
.init_resource::<DrawFunctions>();

app.add_sub_app(RenderSubApp, render_app, move |app_world, render_app| {
app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
// reserve all existing app entities for use in render_app
// they can only be spawned using `get_or_spawn()`
let meta_len = app_world.entities().meta.len();
Expand Down
5 changes: 2 additions & 3 deletions pipelined/bevy_render2/src/render_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::marker::PhantomData;

use crate::{
renderer::{RenderDevice, RenderQueue},
RenderStage, RenderSubApp,
RenderApp, RenderStage,
};
use bevy_app::{App, Plugin};
use bevy_asset::{Asset, AssetEvent, Assets, Handle};
Expand Down Expand Up @@ -31,8 +31,7 @@ impl<A: RenderAsset> Default for RenderAssetPlugin<A> {

impl<A: RenderAsset> Plugin for RenderAssetPlugin<A> {
fn build(&self, app: &mut App) {
let render_app = app.sub_app_mut(RenderSubApp).unwrap();
render_app
app.sub_app(RenderApp)
.init_resource::<ExtractedAssets<A>>()
.init_resource::<RenderAssets<A>>()
.add_system_to_stage(RenderStage::Extract, extract_render_asset::<A>)
Expand Down
5 changes: 2 additions & 3 deletions pipelined/bevy_render2/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use hdr_texture_loader::*;
pub use image_texture_loader::*;
pub use texture_cache::*;

use crate::{render_asset::RenderAssetPlugin, RenderStage, RenderSubApp};
use crate::{render_asset::RenderAssetPlugin, RenderApp, RenderStage};
use bevy_app::{App, Plugin};
use bevy_asset::AddAsset;

Expand All @@ -30,8 +30,7 @@ impl Plugin for ImagePlugin {
app.add_plugin(RenderAssetPlugin::<Image>::default())
.add_asset::<Image>();

let render_app = app.sub_app_mut(RenderSubApp).unwrap();
render_app
app.sub_app(RenderApp)
.init_resource::<TextureCache>()
.add_system_to_stage(RenderStage::Cleanup, update_texture_cache_system);
}
Expand Down
4 changes: 2 additions & 2 deletions pipelined/bevy_render2/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext},
render_resource::DynamicUniformVec,
renderer::{RenderContext, RenderDevice},
RenderStage, RenderSubApp,
RenderApp, RenderStage,
};
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
Expand All @@ -22,7 +22,7 @@ impl ViewPlugin {

impl Plugin for ViewPlugin {
fn build(&self, app: &mut App) {
let render_app = app.sub_app_mut(RenderSubApp).unwrap();
let render_app = app.sub_app(RenderApp);
render_app
.init_resource::<ViewMeta>()
.add_system_to_stage(RenderStage::Prepare, prepare_views);
Expand Down
5 changes: 2 additions & 3 deletions pipelined/bevy_render2/src/view/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
render_resource::TextureView,
renderer::{RenderDevice, RenderInstance},
texture::BevyDefault,
RenderStage, RenderSubApp,
RenderApp, RenderStage,
};
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
Expand All @@ -19,8 +19,7 @@ pub struct WindowRenderPlugin;

impl Plugin for WindowRenderPlugin {
fn build(&self, app: &mut App) {
let render_app = app.sub_app_mut(RenderSubApp).unwrap();
render_app
app.sub_app(RenderApp)
.init_resource::<WindowSurfaces>()
.init_resource::<NonSendMarker>()
.add_system_to_stage(RenderStage::Extract, extract_windows)
Expand Down
7 changes: 3 additions & 4 deletions pipelined/bevy_sprite2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use texture_atlas_builder::*;

use bevy_app::prelude::*;
use bevy_render2::{
render_graph::RenderGraph, render_phase::DrawFunctions, RenderStage, RenderSubApp,
render_graph::RenderGraph, render_phase::DrawFunctions, RenderApp, RenderStage,
};

#[derive(Default)]
Expand All @@ -28,7 +28,7 @@ impl Plugin for SpritePlugin {
app.add_asset::<TextureAtlas>()
.register_type::<Sprite>()
.add_system_to_stage(CoreStage::PostUpdate, sprite_auto_resize_system);
let render_app = app.sub_app_mut(RenderSubApp).unwrap();
let render_app = app.sub_app(RenderApp);
render_app
.init_resource::<ExtractedSprites>()
.add_system_to_stage(RenderStage::Extract, render::extract_atlases)
Expand All @@ -44,8 +44,7 @@ impl Plugin for SpritePlugin {
.unwrap()
.write()
.add(draw_sprite);
let render_world = app.sub_app_mut(RenderSubApp).unwrap().world.cell();
let mut graph = render_world.get_resource_mut::<RenderGraph>().unwrap();
let mut graph = render_app.world.get_resource_mut::<RenderGraph>().unwrap();
graph.add_node("sprite", SpriteNode);
graph
.add_node_edge("sprite", bevy_core_pipeline::node::MAIN_PASS_DEPENDENCIES)
Expand Down
1 change: 0 additions & 1 deletion tools/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ crates=(
bevy_tasks
bevy_ecs/macros
bevy_ecs
bevy_app/macros
bevy_app
bevy_log
bevy_dynamic_plugin
Expand Down

0 comments on commit 9898469

Please sign in to comment.