Skip to content

Commit

Permalink
chore: Rename RenderInstance trait to Extractable
Browse files Browse the repository at this point in the history
  • Loading branch information
jancespivo committed Oct 10, 2023
1 parent a52ca17 commit 85d0fcd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bevy_render::{
mesh::{Mesh, MeshVertexBufferLayout},
prelude::Image,
render_asset::{prepare_assets, RenderAssets},
render_instances::{RenderInstancePlugin, RenderInstances},
extract_instances::{ExtractInstancePlugin, ExtractedInstances},
render_phase::{
AddRenderCommand, DrawFunctions, PhaseItem, RenderCommand, RenderCommandResult,
RenderPhase, SetItemPipeline, TrackedRenderPass,
Expand Down Expand Up @@ -178,7 +178,7 @@ where
{
fn build(&self, app: &mut App) {
app.init_asset::<M>()
.add_plugins(RenderInstancePlugin::<AssetId<M>>::extract_visible());
.add_plugins(ExtractInstancePlugin::<AssetId<M>>::extract_visible());

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app
Expand Down Expand Up @@ -370,7 +370,7 @@ impl<P: PhaseItem, M: Material, const I: usize> RenderCommand<P> for SetMaterial
}
}

pub type RenderMaterialInstances<M> = RenderInstances<AssetId<M>>;
pub type RenderMaterialInstances<M> = ExtractedInstances<AssetId<M>>;

const fn alpha_mode_pipeline_key(alpha_mode: AlphaMode) -> MeshPipelineKey {
match alpha_mode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Convenience logic for turning components from the main world into render
//! Convenience logic for turning components from the main world into extracted
//! instances in the render world.
//!
//! This is essentially the same as the `extract_component` module, but
Expand Down Expand Up @@ -27,50 +27,50 @@ use crate::{prelude::ViewVisibility, Extract, ExtractSchedule, RenderApp};
/// This is essentially the same as
/// [`ExtractComponent`](crate::extract_component::ExtractComponent), but
/// higher-performance because it avoids the ECS overhead.
pub trait RenderInstance: Send + Sync + Sized + 'static {
pub trait ExtractInstance: Send + Sync + Sized + 'static {
/// ECS [`WorldQuery`] to fetch the components to extract.
type Query: WorldQuery + ReadOnlyWorldQuery;
/// Filters the entities with additional constraints.
type Filter: WorldQuery + ReadOnlyWorldQuery;

/// Defines how the component is transferred into the "render world".
fn extract_to_render_instance(item: QueryItem<'_, Self::Query>) -> Option<Self>;
fn extract(item: QueryItem<'_, Self::Query>) -> Option<Self>;
}

/// This plugin extracts one or more components into the "render world" as
/// render instances.
/// extracted instances.
///
/// Therefore it sets up the [`ExtractSchedule`] step for the specified
/// [`RenderInstances`].
/// [`ExtractedInstances`].
#[derive(Default)]
pub struct RenderInstancePlugin<RI>
pub struct ExtractInstancePlugin<RI>
where
RI: RenderInstance,
RI: ExtractInstance,
{
only_extract_visible: bool,
marker: PhantomData<fn() -> RI>,
}

/// Stores all render instances of a type in the render world.
#[derive(Resource, Deref, DerefMut)]
pub struct RenderInstances<RI>(EntityHashMap<Entity, RI>)
pub struct ExtractedInstances<RI>(EntityHashMap<Entity, RI>)
where
RI: RenderInstance;
RI: ExtractInstance;

impl<RI> Default for RenderInstances<RI>
impl<RI> Default for ExtractedInstances<RI>
where
RI: RenderInstance,
RI: ExtractInstance,
{
fn default() -> Self {
Self(Default::default())
}
}

impl<RI> RenderInstancePlugin<RI>
impl<RI> ExtractInstancePlugin<RI>
where
RI: RenderInstance,
RI: ExtractInstance,
{
/// Creates a new [`RenderInstancePlugin`] that unconditionally extracts to
/// Creates a new [`ExtractInstancePlugin`] that unconditionally extracts to
/// the render world, whether the entity is visible or not.
pub fn new() -> Self {
Self {
Expand All @@ -80,11 +80,11 @@ where
}
}

impl<RI> RenderInstancePlugin<RI>
impl<RI> ExtractInstancePlugin<RI>
where
RI: RenderInstance,
RI: ExtractInstance,
{
/// Creates a new [`RenderInstancePlugin`] that extracts to the render world
/// Creates a new [`ExtractInstancePlugin`] that extracts to the render world
/// if and only if the entity it's attached to is visible.
pub fn extract_visible() -> Self {
Self {
Expand All @@ -94,60 +94,60 @@ where
}
}

impl<RI> Plugin for RenderInstancePlugin<RI>
impl<RI> Plugin for ExtractInstancePlugin<RI>
where
RI: RenderInstance,
RI: ExtractInstance,
{
fn build(&self, app: &mut App) {
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app.init_resource::<RenderInstances<RI>>();
render_app.init_resource::<ExtractedInstances<RI>>();
if self.only_extract_visible {
render_app.add_systems(ExtractSchedule, extract_visible_to_render_instances::<RI>);
render_app.add_systems(ExtractSchedule, extract_visible::<RI>);
} else {
render_app.add_systems(ExtractSchedule, extract_to_render_instances::<RI>);
render_app.add_systems(ExtractSchedule, extract_all::<RI>);
}
}
}
}

fn extract_to_render_instances<RI>(
mut instances: ResMut<RenderInstances<RI>>,
fn extract_all<RI>(
mut instances: ResMut<ExtractedInstances<RI>>,
query: Extract<Query<(Entity, RI::Query), RI::Filter>>,
) where
RI: RenderInstance,
RI: ExtractInstance,
{
instances.clear();
for (entity, other) in &query {
if let Some(render_instance) = RI::extract_to_render_instance(other) {
instances.insert(entity, render_instance);
if let Some(instance) = RI::extract(other) {
instances.insert(entity, instance);
}
}
}

fn extract_visible_to_render_instances<RI>(
mut instances: ResMut<RenderInstances<RI>>,
fn extract_visible<RI>(
mut instances: ResMut<ExtractedInstances<RI>>,
query: Extract<Query<(Entity, &ViewVisibility, RI::Query), RI::Filter>>,
) where
RI: RenderInstance,
RI: ExtractInstance,
{
instances.clear();
for (entity, view_visibility, other) in &query {
if view_visibility.get() {
if let Some(render_instance) = RI::extract_to_render_instance(other) {
instances.insert(entity, render_instance);
if let Some(instance) = RI::extract(other) {
instances.insert(entity, instance);
}
}
}
}

impl<A> RenderInstance for AssetId<A>
impl<A> ExtractInstance for AssetId<A>
where
A: Asset,
{
type Query = Read<Handle<A>>;
type Filter = ();

fn extract_to_render_instance(item: QueryItem<'_, Self::Query>) -> Option<Self> {
fn extract(item: QueryItem<'_, Self::Query>) -> Option<Self> {
Some(item.id())
}
}
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod pipelined_rendering;
pub mod primitives;
pub mod render_asset;
pub mod render_graph;
pub mod render_instances;
pub mod extract_instances;
pub mod render_phase;
pub mod render_resource;
pub mod renderer;
Expand Down

0 comments on commit 85d0fcd

Please sign in to comment.