Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 0 additions & 95 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ impl Plugin for Core3dPlugin {
render_app
.init_resource::<DrawFunctions<Opaque3d>>()
.init_resource::<DrawFunctions<AlphaMask3d>>()
.init_resource::<DrawFunctions<Transmissive3d>>()
.init_resource::<DrawFunctions<Transparent3d>>()
.init_resource::<DrawFunctions<Opaque3dPrepass>>()
.init_resource::<DrawFunctions<AlphaMask3dPrepass>>()
Expand All @@ -165,14 +164,12 @@ impl Plugin for Core3dPlugin {
.init_resource::<ViewBinnedRenderPhases<AlphaMask3dPrepass>>()
.init_resource::<ViewBinnedRenderPhases<Opaque3dDeferred>>()
.init_resource::<ViewBinnedRenderPhases<AlphaMask3dDeferred>>()
.init_resource::<ViewSortedRenderPhases<Transmissive3d>>()
.init_resource::<ViewSortedRenderPhases<Transparent3d>>()
.add_systems(ExtractSchedule, extract_core_3d_camera_phases)
.add_systems(ExtractSchedule, extract_camera_prepass_phase)
.add_systems(
Render,
(
sort_phase_system::<Transmissive3d>.in_set(RenderSystems::PhaseSort),
sort_phase_system::<Transparent3d>.in_set(RenderSystems::PhaseSort),
configure_occlusion_culling_view_targets
.after(prepare_view_targets)
Expand Down Expand Up @@ -448,93 +445,6 @@ impl CachedRenderPipelinePhaseItem for AlphaMask3d {
}
}

pub struct Transmissive3d {
pub distance: f32,
pub pipeline: CachedRenderPipelineId,
pub entity: (Entity, MainEntity),
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
/// Whether the mesh in question is indexed (uses an index buffer in
/// addition to its vertex buffer).
pub indexed: bool,
}

impl PhaseItem for Transmissive3d {
/// For now, automatic batching is disabled for transmissive items because their rendering is
/// split into multiple steps depending on `ScreenSpaceTransmission::screen_space_specular_transmission_steps`,
/// which the batching system doesn't currently know about.
///
/// Having batching enabled would cause the same item to be drawn multiple times across different
/// steps, whenever the batching range crossed a step boundary.
///
/// Eventually, we could add support for this by having the batching system break up the batch ranges
/// using the same logic as the transmissive pass, but for now it's simpler to just disable batching.
const AUTOMATIC_BATCHING: bool = false;

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

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

#[inline]
fn draw_function(&self) -> DrawFunctionId {
self.draw_function
}

#[inline]
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}

#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}

#[inline]
fn extra_index(&self) -> PhaseItemExtraIndex {
self.extra_index.clone()
}

#[inline]
fn batch_range_and_extra_index_mut(&mut self) -> (&mut Range<u32>, &mut PhaseItemExtraIndex) {
(&mut self.batch_range, &mut self.extra_index)
}
}

impl SortedPhaseItem for Transmissive3d {
// NOTE: Values increase towards the camera. Back-to-front ordering for transmissive means we need an ascending sort.
type SortKey = FloatOrd;

#[inline]
fn sort_key(&self) -> Self::SortKey {
FloatOrd(self.distance)
}

#[inline]
fn sort(items: &mut [Self]) {
radsort::sort_by_key(items, |item| item.distance);
}

#[inline]
fn indexed(&self) -> bool {
self.indexed
}
}

impl CachedRenderPipelinePhaseItem for Transmissive3d {
#[inline]
fn cached_pipeline(&self) -> CachedRenderPipelineId {
self.pipeline
}
}

pub struct Transparent3d {
pub distance: f32,
pub pipeline: CachedRenderPipelineId,
Expand Down Expand Up @@ -613,7 +523,6 @@ impl CachedRenderPipelinePhaseItem for Transparent3d {
pub fn extract_core_3d_camera_phases(
mut opaque_3d_phases: ResMut<ViewBinnedRenderPhases<Opaque3d>>,
mut alpha_mask_3d_phases: ResMut<ViewBinnedRenderPhases<AlphaMask3d>>,
mut transmissive_3d_phases: ResMut<ViewSortedRenderPhases<Transmissive3d>>,
mut transparent_3d_phases: ResMut<ViewSortedRenderPhases<Transparent3d>>,
cameras_3d: Extract<Query<(Entity, &Camera, Has<NoIndirectDrawing>), With<Camera3d>>>,
mut live_entities: Local<HashSet<RetainedViewEntity>>,
Expand All @@ -639,15 +548,13 @@ pub fn extract_core_3d_camera_phases(

opaque_3d_phases.prepare_for_new_frame(retained_view_entity, gpu_preprocessing_mode);
alpha_mask_3d_phases.prepare_for_new_frame(retained_view_entity, gpu_preprocessing_mode);
transmissive_3d_phases.insert_or_clear(retained_view_entity);
transparent_3d_phases.insert_or_clear(retained_view_entity);

live_entities.insert(retained_view_entity);
}

opaque_3d_phases.retain(|view_entity, _| live_entities.contains(view_entity));
alpha_mask_3d_phases.retain(|view_entity, _| live_entities.contains(view_entity));
transmissive_3d_phases.retain(|view_entity, _| live_entities.contains(view_entity));
transparent_3d_phases.retain(|view_entity, _| live_entities.contains(view_entity));
}

Expand Down Expand Up @@ -785,7 +692,6 @@ pub fn prepare_core_3d_depth_textures(
render_device: Res<RenderDevice>,
opaque_3d_phases: Res<ViewBinnedRenderPhases<Opaque3d>>,
alpha_mask_3d_phases: Res<ViewBinnedRenderPhases<AlphaMask3d>>,
transmissive_3d_phases: Res<ViewSortedRenderPhases<Transmissive3d>>,
transparent_3d_phases: Res<ViewSortedRenderPhases<Transparent3d>>,
views_3d: Query<(
Entity,
Expand All @@ -800,7 +706,6 @@ pub fn prepare_core_3d_depth_textures(
for (_, camera, extracted_view, depth_prepass, camera_3d, _msaa) in &views_3d {
if !opaque_3d_phases.contains_key(&extracted_view.retained_view_entity)
|| !alpha_mask_3d_phases.contains_key(&extracted_view.retained_view_entity)
|| !transmissive_3d_phases.contains_key(&extracted_view.retained_view_entity)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: i don't know what this check is here for. All of these contains_key checks always return true and the conditional never runs in my testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking if this even does anything at all in #22711

|| !transparent_3d_phases.contains_key(&extracted_view.retained_view_entity)
{
continue;
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ itertools = { version = "0.14", optional = true }
bitvec = { version = "1", optional = true }
# direct dependency required for derive macro
bytemuck = { version = "1", features = ["derive", "must_cast"] }
radsort = "0.1"
smallvec = { version = "1", default-features = false }
nonmax = "0.5"
static_assertions = "1"
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use bevy_camera::visibility::ViewVisibility;
use bevy_core_pipeline::deferred::{AlphaMask3dDeferred, Opaque3dDeferred};
use bevy_core_pipeline::prepass::{AlphaMask3dPrepass, Opaque3dPrepass};
use bevy_core_pipeline::{
core_3d::{
AlphaMask3d, Opaque3d, Opaque3dBatchSetKey, Opaque3dBinKey, Transmissive3d, Transparent3d,
},
core_3d::{AlphaMask3d, Opaque3d, Opaque3dBatchSetKey, Opaque3dBinKey, Transparent3d},
prepass::{OpaqueNoLightmap3dBatchSetKey, OpaqueNoLightmap3dBinKey},
tonemapping::Tonemapping,
};
Expand Down Expand Up @@ -299,7 +297,6 @@ impl Plugin for MaterialsPlugin {
.init_resource::<RenderMaterialInstances>()
.init_resource::<MaterialBindGroupAllocators>()
.add_render_command::<Shadow, DrawPrepass>()
.add_render_command::<Transmissive3d, DrawMaterial>()
.add_render_command::<Transparent3d, DrawMaterial>()
.add_render_command::<Opaque3d, DrawMaterial>()
.add_render_command::<AlphaMask3d, DrawMaterial>()
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy_camera::{
Camera, Projection,
};
use bevy_core_pipeline::{
core_3d::{AlphaMask3d, Opaque3d, Transmissive3d, Transparent3d, CORE_3D_DEPTH_FORMAT},
core_3d::{AlphaMask3d, Opaque3d, Transparent3d, CORE_3D_DEPTH_FORMAT},
deferred::{AlphaMask3dDeferred, Opaque3dDeferred},
oit::{prepare_oit_buffers, OrderIndependentTransparencySettingsOffset},
prepass::MotionVectorPrepass,
Expand Down
15 changes: 14 additions & 1 deletion crates/bevy_pbr/src/transmission/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod node;
mod phase;
mod texture;

use bevy_app::{App, Plugin};
Expand All @@ -9,14 +10,18 @@ use bevy_reflect::prelude::*;
use bevy_render::{
extract_component::{ExtractComponent, ExtractComponentPlugin},
render_graph::{RenderGraphExt, ViewNodeRunner},
Render, RenderApp, RenderSystems,
render_phase::{sort_phase_system, AddRenderCommand, DrawFunctions, ViewSortedRenderPhases},
ExtractSchedule, Render, RenderApp, RenderSystems,
};
use bevy_shader::load_shader_library;
pub use node::MainTransmissivePass3dNode;
pub use phase::Transmissive3d;
pub use texture::ViewTransmissionTexture;

use texture::prepare_core_3d_transmission_textures;

use crate::DrawMaterial;

/// Enables screen-space transmission for cameras.
pub struct ScreenSpaceTransmissionPlugin;

Expand All @@ -32,6 +37,14 @@ impl Plugin for ScreenSpaceTransmissionPlugin {
};

render_app
.init_resource::<DrawFunctions<Transmissive3d>>()
.init_resource::<ViewSortedRenderPhases<Transmissive3d>>()
.add_render_command::<Transmissive3d, DrawMaterial>()
.add_systems(
Render,
sort_phase_system::<Transmissive3d>.in_set(RenderSystems::PhaseSort),
)
.add_systems(ExtractSchedule, phase::extract_transmissive_camera_phases)
.add_systems(
Render,
prepare_core_3d_transmission_textures.in_set(RenderSystems::PrepareResources),
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_pbr/src/transmission/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{ScreenSpaceTransmission, ViewTransmissionTexture};
use crate::{ScreenSpaceTransmission, Transmissive3d, ViewTransmissionTexture};

use bevy_camera::{MainPassResolutionOverride, Viewport};
use bevy_core_pipeline::core_3d::Transmissive3d;
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_image::ToExtents;
use bevy_render::{
Expand Down
129 changes: 129 additions & 0 deletions crates/bevy_pbr/src/transmission/phase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use core::ops::Range;

use bevy_camera::{Camera, Camera3d};
use bevy_ecs::{
entity::Entity,
query::With,
system::{Local, Query, ResMut},
};
use bevy_material::{descriptor::CachedRenderPipelineId, labels::DrawFunctionId};
use bevy_math::FloatOrd;
use bevy_platform::collections::HashSet;
use bevy_render::{
render_phase::{
CachedRenderPipelinePhaseItem, PhaseItem, PhaseItemExtraIndex, SortedPhaseItem,
ViewSortedRenderPhases,
},
sync_world::MainEntity,
view::RetainedViewEntity,
Extract,
};

pub struct Transmissive3d {
pub distance: f32,
pub pipeline: CachedRenderPipelineId,
pub entity: (Entity, MainEntity),
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
/// Whether the mesh in question is indexed (uses an index buffer in
/// addition to its vertex buffer).
pub indexed: bool,
}

impl PhaseItem for Transmissive3d {
/// For now, automatic batching is disabled for transmissive items because their rendering is
/// split into multiple steps depending on [`crate::ScreenSpaceTransmission::screen_space_specular_transmission_steps`],
/// which the batching system doesn't currently know about.
///
/// Having batching enabled would cause the same item to be drawn multiple times across different
/// steps, whenever the batching range crossed a step boundary.
///
/// Eventually, we could add support for this by having the batching system break up the batch ranges
/// using the same logic as the transmissive pass, but for now it's simpler to just disable batching.
const AUTOMATIC_BATCHING: bool = false;

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

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

#[inline]
fn draw_function(&self) -> DrawFunctionId {
self.draw_function
}

#[inline]
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}

#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}

#[inline]
fn extra_index(&self) -> PhaseItemExtraIndex {
self.extra_index.clone()
}

#[inline]
fn batch_range_and_extra_index_mut(&mut self) -> (&mut Range<u32>, &mut PhaseItemExtraIndex) {
(&mut self.batch_range, &mut self.extra_index)
}
}

impl SortedPhaseItem for Transmissive3d {
// NOTE: Values increase towards the camera. Back-to-front ordering for transmissive means we need an ascending sort.
type SortKey = FloatOrd;

#[inline]
fn sort_key(&self) -> Self::SortKey {
FloatOrd(self.distance)
}

#[inline]
fn sort(items: &mut [Self]) {
radsort::sort_by_key(items, |item| item.distance);
}

#[inline]
fn indexed(&self) -> bool {
self.indexed
}
}

impl CachedRenderPipelinePhaseItem for Transmissive3d {
#[inline]
fn cached_pipeline(&self) -> CachedRenderPipelineId {
self.pipeline
}
}

pub fn extract_transmissive_camera_phases(
mut transmissive_3d_phases: ResMut<ViewSortedRenderPhases<Transmissive3d>>,
cameras: Extract<Query<(Entity, &Camera), With<Camera3d>>>,
mut live_entities: Local<HashSet<RetainedViewEntity>>,
) {
live_entities.clear();

for (main_entity, camera) in &cameras {
if !camera.is_active {
continue;
}

// This is the main camera, so use the first subview index (0).
let retained_view_entity = RetainedViewEntity::new(main_entity.into(), None, 0);

transmissive_3d_phases.insert_or_clear(retained_view_entity);
live_entities.insert(retained_view_entity);
}

transmissive_3d_phases.retain(|view_entity, _| live_entities.contains(view_entity));
}
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/transmission/texture.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_core_pipeline::core_3d::{AlphaMask3d, Opaque3d, Transmissive3d, Transparent3d};
use bevy_core_pipeline::core_3d::{AlphaMask3d, Opaque3d, Transparent3d};
use bevy_ecs::{
component::Component,
entity::Entity,
Expand All @@ -18,7 +18,7 @@ use bevy_render::{
view::{ExtractedView, ViewTarget},
};

use crate::ScreenSpaceTransmission;
use crate::{ScreenSpaceTransmission, Transmissive3d};

#[derive(Component)]
pub struct ViewTransmissionTexture {
Expand Down
Loading
Loading