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

Type safe retained render world #15756

Merged
merged 18 commits into from
Oct 10, 2024
34 changes: 24 additions & 10 deletions crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ pub use camera_2d::*;
pub use main_opaque_pass_2d_node::*;
pub use main_transparent_pass_2d_node::*;

use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode};
use bevy_app::{App, Plugin};
use bevy_ecs::{entity::EntityHashSet, prelude::*};
use bevy_math::FloatOrd;
use bevy_render::sync_world::MainEntity;
use bevy_render::{
camera::{Camera, ExtractedCamera},
extract_component::ExtractComponentPlugin,
Expand All @@ -61,8 +63,6 @@ use bevy_render::{
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};

use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode};

use self::graph::{Core2d, Node2d};

pub const CORE_2D_DEPTH_FORMAT: TextureFormat = TextureFormat::Depth32Float;
Expand Down Expand Up @@ -129,7 +129,7 @@ pub struct Opaque2d {
pub key: Opaque2dBinKey,
/// An entity from which data will be fetched, including the mesh if
/// applicable.
pub representative_entity: Entity,
pub representative_entity: (Entity, MainEntity),
/// The ranges of instances.
pub batch_range: Range<u32>,
/// An extra index, which is either a dynamic offset or an index in the
Expand All @@ -156,7 +156,11 @@ pub struct Opaque2dBinKey {
impl PhaseItem for Opaque2d {
#[inline]
fn entity(&self) -> Entity {
self.representative_entity
self.representative_entity.0
}

fn main_entity(&self) -> MainEntity {
self.representative_entity.1
}

#[inline]
Expand Down Expand Up @@ -188,7 +192,7 @@ impl BinnedPhaseItem for Opaque2d {

fn new(
key: Self::BinKey,
representative_entity: Entity,
representative_entity: (Entity, MainEntity),
batch_range: Range<u32>,
extra_index: PhaseItemExtraIndex,
) -> Self {
Expand All @@ -214,7 +218,7 @@ pub struct AlphaMask2d {
pub key: AlphaMask2dBinKey,
/// An entity from which data will be fetched, including the mesh if
/// applicable.
pub representative_entity: Entity,
pub representative_entity: (Entity, MainEntity),
/// The ranges of instances.
pub batch_range: Range<u32>,
/// An extra index, which is either a dynamic offset or an index in the
Expand All @@ -241,7 +245,12 @@ pub struct AlphaMask2dBinKey {
impl PhaseItem for AlphaMask2d {
#[inline]
fn entity(&self) -> Entity {
self.representative_entity
self.representative_entity.0
}

#[inline]
fn main_entity(&self) -> MainEntity {
self.representative_entity.1
}

#[inline]
Expand Down Expand Up @@ -273,7 +282,7 @@ impl BinnedPhaseItem for AlphaMask2d {

fn new(
key: Self::BinKey,
representative_entity: Entity,
representative_entity: (Entity, MainEntity),
batch_range: Range<u32>,
extra_index: PhaseItemExtraIndex,
) -> Self {
Expand All @@ -296,7 +305,7 @@ impl CachedRenderPipelinePhaseItem for AlphaMask2d {
/// Transparent 2D [`SortedPhaseItem`]s.
pub struct Transparent2d {
pub sort_key: FloatOrd,
pub entity: Entity,
pub entity: (Entity, MainEntity),
pub pipeline: CachedRenderPipelineId,
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
Expand All @@ -306,7 +315,12 @@ pub struct Transparent2d {
impl PhaseItem for Transparent2d {
#[inline]
fn entity(&self) -> Entity {
self.entity
self.entity.0
}

#[inline]
fn main_entity(&self) -> MainEntity {
self.entity.1
}

#[inline]
Expand Down
39 changes: 29 additions & 10 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub use main_transparent_pass_3d_node::*;
use bevy_app::{App, Plugin, PostUpdate};
use bevy_ecs::{entity::EntityHashSet, prelude::*};
use bevy_math::FloatOrd;
use bevy_render::sync_world::MainEntity;
use bevy_render::{
camera::{Camera, ExtractedCamera},
extract_component::ExtractComponentPlugin,
Expand Down Expand Up @@ -214,7 +215,7 @@ pub struct Opaque3d {
pub key: Opaque3dBinKey,
/// An entity from which data will be fetched, including the mesh if
/// applicable.
pub representative_entity: Entity,
pub representative_entity: (Entity, MainEntity),
/// The ranges of instances.
pub batch_range: Range<u32>,
/// An extra index, which is either a dynamic offset or an index in the
Expand Down Expand Up @@ -249,7 +250,12 @@ pub struct Opaque3dBinKey {
impl PhaseItem for Opaque3d {
#[inline]
fn entity(&self) -> Entity {
self.representative_entity
self.representative_entity.0
}

#[inline]
fn main_entity(&self) -> MainEntity {
self.representative_entity.1
}

#[inline]
Expand Down Expand Up @@ -282,7 +288,7 @@ impl BinnedPhaseItem for Opaque3d {
#[inline]
fn new(
key: Self::BinKey,
representative_entity: Entity,
representative_entity: (Entity, MainEntity),
batch_range: Range<u32>,
extra_index: PhaseItemExtraIndex,
) -> Self {
Expand All @@ -304,15 +310,19 @@ impl CachedRenderPipelinePhaseItem for Opaque3d {

pub struct AlphaMask3d {
pub key: OpaqueNoLightmap3dBinKey,
pub representative_entity: Entity,
pub representative_entity: (Entity, MainEntity),
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
}

impl PhaseItem for AlphaMask3d {
#[inline]
fn entity(&self) -> Entity {
self.representative_entity
self.representative_entity.0
}

fn main_entity(&self) -> MainEntity {
self.representative_entity.1
}

#[inline]
Expand Down Expand Up @@ -347,7 +357,7 @@ impl BinnedPhaseItem for AlphaMask3d {
#[inline]
fn new(
key: Self::BinKey,
representative_entity: Entity,
representative_entity: (Entity, MainEntity),
batch_range: Range<u32>,
extra_index: PhaseItemExtraIndex,
) -> Self {
Expand All @@ -370,7 +380,7 @@ impl CachedRenderPipelinePhaseItem for AlphaMask3d {
pub struct Transmissive3d {
pub distance: f32,
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub entity: (Entity, MainEntity),
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
Expand All @@ -390,7 +400,12 @@ impl PhaseItem for Transmissive3d {

#[inline]
fn entity(&self) -> Entity {
self.entity
self.entity.0
}

#[inline]
fn main_entity(&self) -> MainEntity {
self.entity.1
}

#[inline]
Expand Down Expand Up @@ -444,7 +459,7 @@ impl CachedRenderPipelinePhaseItem for Transmissive3d {
pub struct Transparent3d {
pub distance: f32,
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub entity: (Entity, MainEntity),
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
Expand All @@ -453,7 +468,11 @@ pub struct Transparent3d {
impl PhaseItem for Transparent3d {
#[inline]
fn entity(&self) -> Entity {
self.entity
self.entity.0
}

fn main_entity(&self) -> MainEntity {
self.entity.1
}

#[inline]
Expand Down
25 changes: 17 additions & 8 deletions crates/bevy_core_pipeline/src/deferred/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ pub mod node;

use core::ops::Range;

use crate::prepass::OpaqueNoLightmap3dBinKey;
use bevy_ecs::prelude::*;
use bevy_render::sync_world::MainEntity;
use bevy_render::{
render_phase::{
BinnedPhaseItem, CachedRenderPipelinePhaseItem, DrawFunctionId, PhaseItem,
Expand All @@ -12,8 +14,6 @@ use bevy_render::{
render_resource::{CachedRenderPipelineId, TextureFormat},
};

use crate::prepass::OpaqueNoLightmap3dBinKey;

pub const DEFERRED_PREPASS_FORMAT: TextureFormat = TextureFormat::Rgba32Uint;
pub const DEFERRED_LIGHTING_PASS_ID_FORMAT: TextureFormat = TextureFormat::R8Uint;
pub const DEFERRED_LIGHTING_PASS_ID_DEPTH_FORMAT: TextureFormat = TextureFormat::Depth16Unorm;
Expand All @@ -26,15 +26,19 @@ pub const DEFERRED_LIGHTING_PASS_ID_DEPTH_FORMAT: TextureFormat = TextureFormat:
#[derive(PartialEq, Eq, Hash)]
pub struct Opaque3dDeferred {
pub key: OpaqueNoLightmap3dBinKey,
pub representative_entity: Entity,
pub representative_entity: (Entity, MainEntity),
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
}

impl PhaseItem for Opaque3dDeferred {
#[inline]
fn entity(&self) -> Entity {
self.representative_entity
self.representative_entity.0
}

fn main_entity(&self) -> MainEntity {
self.representative_entity.1
}

#[inline]
Expand Down Expand Up @@ -69,7 +73,7 @@ impl BinnedPhaseItem for Opaque3dDeferred {
#[inline]
fn new(
key: Self::BinKey,
representative_entity: Entity,
representative_entity: (Entity, MainEntity),
batch_range: Range<u32>,
extra_index: PhaseItemExtraIndex,
) -> Self {
Expand All @@ -96,15 +100,20 @@ impl CachedRenderPipelinePhaseItem for Opaque3dDeferred {
/// Used to render all meshes with a material with an alpha mask.
pub struct AlphaMask3dDeferred {
pub key: OpaqueNoLightmap3dBinKey,
pub representative_entity: Entity,
pub representative_entity: (Entity, MainEntity),
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
}

impl PhaseItem for AlphaMask3dDeferred {
#[inline]
fn entity(&self) -> Entity {
self.representative_entity
self.representative_entity.0
}

#[inline]
fn main_entity(&self) -> MainEntity {
self.representative_entity.1
}

#[inline]
Expand Down Expand Up @@ -138,7 +147,7 @@ impl BinnedPhaseItem for AlphaMask3dDeferred {

fn new(
key: Self::BinKey,
representative_entity: Entity,
representative_entity: (Entity, MainEntity),
batch_range: Range<u32>,
extra_index: PhaseItemExtraIndex,
) -> Self {
Expand Down
25 changes: 16 additions & 9 deletions crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ pub mod node;

use core::ops::Range;

use crate::deferred::{DEFERRED_LIGHTING_PASS_ID_FORMAT, DEFERRED_PREPASS_FORMAT};
use bevy_asset::UntypedAssetId;
use bevy_ecs::prelude::*;
use bevy_math::Mat4;
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::sync_world::MainEntity;
use bevy_render::{
render_phase::{
BinnedPhaseItem, CachedRenderPipelinePhaseItem, DrawFunctionId, PhaseItem,
Expand All @@ -45,8 +47,6 @@ use bevy_render::{
texture::ColorAttachment,
};

use crate::deferred::{DEFERRED_LIGHTING_PASS_ID_FORMAT, DEFERRED_PREPASS_FORMAT};

pub const NORMAL_PREPASS_FORMAT: TextureFormat = TextureFormat::Rgb10a2Unorm;
pub const MOTION_VECTOR_PREPASS_FORMAT: TextureFormat = TextureFormat::Rg16Float;

Expand Down Expand Up @@ -143,8 +143,7 @@ pub struct Opaque3dPrepass {

/// An entity from which Bevy fetches data common to all instances in this
/// batch, such as the mesh.
pub representative_entity: Entity,

pub representative_entity: (Entity, MainEntity),
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
}
Expand All @@ -171,7 +170,11 @@ pub struct OpaqueNoLightmap3dBinKey {
impl PhaseItem for Opaque3dPrepass {
#[inline]
fn entity(&self) -> Entity {
self.representative_entity
self.representative_entity.0
}

fn main_entity(&self) -> MainEntity {
self.representative_entity.1
}

#[inline]
Expand Down Expand Up @@ -206,7 +209,7 @@ impl BinnedPhaseItem for Opaque3dPrepass {
#[inline]
fn new(
key: Self::BinKey,
representative_entity: Entity,
representative_entity: (Entity, MainEntity),
batch_range: Range<u32>,
extra_index: PhaseItemExtraIndex,
) -> Self {
Expand All @@ -233,15 +236,19 @@ impl CachedRenderPipelinePhaseItem for Opaque3dPrepass {
/// Used to render all meshes with a material with an alpha mask.
pub struct AlphaMask3dPrepass {
pub key: OpaqueNoLightmap3dBinKey,
pub representative_entity: Entity,
pub representative_entity: (Entity, MainEntity),
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
}

impl PhaseItem for AlphaMask3dPrepass {
#[inline]
fn entity(&self) -> Entity {
self.representative_entity
self.representative_entity.0
}

fn main_entity(&self) -> MainEntity {
self.representative_entity.1
}

#[inline]
Expand Down Expand Up @@ -276,7 +283,7 @@ impl BinnedPhaseItem for AlphaMask3dPrepass {
#[inline]
fn new(
key: Self::BinKey,
representative_entity: Entity,
representative_entity: (Entity, MainEntity),
batch_range: Range<u32>,
extra_index: PhaseItemExtraIndex,
) -> Self {
Expand Down
Loading
Loading