forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ExtractResourcePlugin (bevyengine#3745)
# Objective - Add an `ExtractResourcePlugin` for convenience and consistency ## Solution - Add an `ExtractResourcePlugin` similar to `ExtractComponentPlugin` but for ECS `Resource`s. The system that is executed simply clones the main world resource into a render world resource, if and only if the main world resource was either added or changed since the last execution of the system. - Add an `ExtractResource` trait with a `fn extract_resource(res: &Self) -> Self` function. This is used by the `ExtractResourcePlugin` to extract the resource - Add a derive macro for `ExtractResource` on a `Resource` with the `Clone` trait, that simply returns `res.clone()` - Use `ExtractResourcePlugin` wherever both possible and appropriate
- Loading branch information
Showing
21 changed files
with
154 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "bevy_render_macros" | ||
version = "0.8.0-dev" | ||
edition = "2021" | ||
description = "Derive implementations for bevy_render" | ||
homepage = "https://bevyengine.org" | ||
repository = "https://github.com/bevyengine/bevy" | ||
license = "MIT OR Apache-2.0" | ||
keywords = ["bevy"] | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
bevy_macro_utils = { path = "../../bevy_macro_utils", version = "0.8.0-dev" } | ||
|
||
syn = "1.0" | ||
proc-macro2 = "1.0" | ||
quote = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, parse_quote, DeriveInput, Path}; | ||
|
||
pub fn derive_extract_resource(input: TokenStream) -> TokenStream { | ||
let mut ast = parse_macro_input!(input as DeriveInput); | ||
let bevy_render_path: Path = crate::bevy_render_path(); | ||
|
||
ast.generics | ||
.make_where_clause() | ||
.predicates | ||
.push(parse_quote! { Self: Clone }); | ||
|
||
let struct_name = &ast.ident; | ||
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl(); | ||
|
||
TokenStream::from(quote! { | ||
impl #impl_generics #bevy_render_path::extract_resource::ExtractResource for #struct_name #type_generics #where_clause { | ||
type Source = Self; | ||
|
||
fn extract_resource(source: &Self::Source) -> Self { | ||
source.clone() | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
mod extract_resource; | ||
|
||
use bevy_macro_utils::BevyManifest; | ||
use proc_macro::TokenStream; | ||
|
||
pub(crate) fn bevy_render_path() -> syn::Path { | ||
BevyManifest::default() | ||
.maybe_get_path("bevy_render") | ||
// NOTE: If the derivation is within bevy_render, then we need to return 'crate' | ||
.unwrap_or_else(|| BevyManifest::parse_str("crate")) | ||
} | ||
|
||
#[proc_macro_derive(ExtractResource)] | ||
pub fn derive_extract_resource(input: TokenStream) -> TokenStream { | ||
extract_resource::derive_extract_resource(input) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::marker::PhantomData; | ||
|
||
use bevy_app::{App, Plugin}; | ||
use bevy_ecs::system::{Commands, Res, Resource}; | ||
pub use bevy_render_macros::ExtractResource; | ||
|
||
use crate::{RenderApp, RenderStage}; | ||
|
||
/// Describes how a resource gets extracted for rendering. | ||
/// | ||
/// Therefore the resource is transferred from the "main world" into the "render world" | ||
/// in the [`RenderStage::Extract`](crate::RenderStage::Extract) step. | ||
pub trait ExtractResource: Resource { | ||
type Source: Resource; | ||
|
||
/// Defines how the resource is transferred into the "render world". | ||
fn extract_resource(source: &Self::Source) -> Self; | ||
} | ||
|
||
/// This plugin extracts the resources into the "render world". | ||
/// | ||
/// Therefore it sets up the [`RenderStage::Extract`](crate::RenderStage::Extract) step | ||
/// for the specified [`Resource`]. | ||
pub struct ExtractResourcePlugin<R: ExtractResource>(PhantomData<R>); | ||
|
||
impl<R: ExtractResource> Default for ExtractResourcePlugin<R> { | ||
fn default() -> Self { | ||
Self(PhantomData) | ||
} | ||
} | ||
|
||
impl<R: ExtractResource> Plugin for ExtractResourcePlugin<R> { | ||
fn build(&self, app: &mut App) { | ||
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { | ||
render_app.add_system_to_stage(RenderStage::Extract, extract_resource::<R>); | ||
} | ||
} | ||
} | ||
|
||
/// This system extracts the resource of the corresponding [`Resource`] type | ||
/// by cloning it. | ||
pub fn extract_resource<R: ExtractResource>(mut commands: Commands, resource: Res<R::Source>) { | ||
if resource.is_changed() { | ||
commands.insert_resource(R::extract_resource(resource.into_inner())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.