-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Refactor the render instance logic in #9903 so that it's easier for other components to adopt. #10002
Merged
Merged
Refactor the render instance logic in #9903 so that it's easier for other components to adopt. #10002
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ce8d88d
Refactor the render instance logic in #9903 so that it's easier for
pcwalton bb6acf0
Merge remote-tracking branch 'origin/main' into generic-render-instances
pcwalton 84cb2d7
Fix symlink issue
pcwalton 205c364
Fix doc test
pcwalton aa49ce3
Implement on the target, not on the component
pcwalton 6868e86
Fix some naming
pcwalton 079bdcb
Fix doc failure
pcwalton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
//! Convenience logic for turning components from the main world into render | ||
//! instances in the render world. | ||
//! | ||
//! This is essentially the same as the `extract_component` module, but | ||
//! higher-performance because it avoids the ECS overhead. | ||
|
||
use std::marker::PhantomData; | ||
|
||
use bevy_app::{App, Plugin}; | ||
use bevy_asset::{Asset, AssetId, Handle}; | ||
use bevy_derive::{Deref, DerefMut}; | ||
use bevy_ecs::{ | ||
component::Component, | ||
prelude::Entity, | ||
query::{QueryItem, ReadOnlyWorldQuery, WorldQuery}, | ||
system::{lifetimeless::Read, Query, ResMut, Resource}, | ||
}; | ||
use bevy_utils::EntityHashMap; | ||
|
||
use crate::{prelude::ViewVisibility, Extract, ExtractSchedule, RenderApp}; | ||
|
||
/// Describes how a component gets turned into a render instance for rendering. | ||
/// | ||
/// Before rendering, a component will be transferred from the main world to the | ||
/// render world in the [`ExtractSchedule`] step. | ||
/// | ||
/// This is essentially the same as | ||
/// [`ExtractComponent`](crate::extract_component::ExtractComponent), but | ||
/// higher-performance because it avoids the ECS overhead. | ||
pub trait ExtractToRenderInstance: Component { | ||
/// ECS [`WorldQuery`] to fetch the components to extract. | ||
type Query: WorldQuery + ReadOnlyWorldQuery; | ||
/// Filters the entities with additional constraints. | ||
type Filter: WorldQuery + ReadOnlyWorldQuery; | ||
|
||
type Instance: Send + Sync; | ||
|
||
/// Defines how the component is transferred into the "render world". | ||
fn extract_to_render_instance(item: QueryItem<'_, Self::Query>) -> Option<Self::Instance>; | ||
} | ||
|
||
/// This plugin extracts the components into the "render world" as render | ||
/// instances. | ||
/// | ||
/// Therefore it sets up the [`ExtractSchedule`](crate::ExtractSchedule) step | ||
/// for the specified [`RenderInstances`]. | ||
#[derive(Default)] | ||
pub struct ExtractToRenderInstancePlugin<C> | ||
where | ||
C: ExtractToRenderInstance, | ||
{ | ||
only_extract_visible: bool, | ||
marker: PhantomData<fn() -> C>, | ||
} | ||
|
||
/// Stores all render instances corresponding to the given component in the render world. | ||
#[derive(Resource, Deref, DerefMut)] | ||
pub struct RenderInstances<C>(EntityHashMap<Entity, C::Instance>) | ||
where | ||
C: ExtractToRenderInstance; | ||
|
||
impl<C> Default for RenderInstances<C> | ||
where | ||
C: ExtractToRenderInstance, | ||
{ | ||
fn default() -> Self { | ||
Self(Default::default()) | ||
} | ||
} | ||
|
||
impl<C> ExtractToRenderInstancePlugin<C> | ||
where | ||
C: ExtractToRenderInstance, | ||
{ | ||
/// Creates a new [`ExtractToRenderInstancePlugin`] that unconditionally | ||
/// extracts the component to the render world, whether visible or not. | ||
pub fn new() -> Self { | ||
Self { | ||
only_extract_visible: false, | ||
marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<C> ExtractToRenderInstancePlugin<C> | ||
where | ||
C: ExtractToRenderInstance, | ||
{ | ||
/// Creates a new [`ExtractToRenderInstancePlugin`] that extracts the | ||
/// component to the render world if and only if the entity it's attached to | ||
/// is visible. | ||
pub fn extract_visible() -> Self { | ||
Self { | ||
only_extract_visible: true, | ||
marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<C> Plugin for ExtractToRenderInstancePlugin<C> | ||
where | ||
C: ExtractToRenderInstance, | ||
{ | ||
fn build(&self, app: &mut App) { | ||
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { | ||
render_app.init_resource::<RenderInstances<C>>(); | ||
if self.only_extract_visible { | ||
render_app.add_systems(ExtractSchedule, extract_visible_to_render_instances::<C>); | ||
} else { | ||
render_app.add_systems(ExtractSchedule, extract_to_render_instances::<C>); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn extract_to_render_instances<C>( | ||
mut instances: ResMut<RenderInstances<C>>, | ||
query: Extract<Query<(Entity, C::Query), C::Filter>>, | ||
) where | ||
C: ExtractToRenderInstance, | ||
{ | ||
instances.clear(); | ||
for (entity, other) in &query { | ||
if let Some(render_instance) = C::extract_to_render_instance(other) { | ||
instances.insert(entity, render_instance); | ||
} | ||
} | ||
} | ||
|
||
fn extract_visible_to_render_instances<C>( | ||
mut instances: ResMut<RenderInstances<C>>, | ||
query: Extract<Query<(Entity, &ViewVisibility, C::Query), C::Filter>>, | ||
) where | ||
C: ExtractToRenderInstance, | ||
{ | ||
instances.clear(); | ||
for (entity, view_visibility, other) in &query { | ||
if view_visibility.get() { | ||
if let Some(render_instance) = C::extract_to_render_instance(other) { | ||
instances.insert(entity, render_instance); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<A> ExtractToRenderInstance for Handle<A> | ||
where | ||
A: Asset, | ||
{ | ||
type Query = Read<Handle<A>>; | ||
type Filter = (); | ||
type Instance = AssetId<A>; | ||
|
||
fn extract_to_render_instance(item: QueryItem<'_, Self::Query>) -> Option<Self::Instance> { | ||
Some(item.id()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m wondering if there is a case for a more flexible version where you give it a query, filter, and instance, and a method as a way of mapping from the query to the instance but it isn’t implemented for a single Component. I’m a bit concerned that doing this per component one wants to extract will be detrimental to performance because it will result in more, independent lookups.