-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
306 additions
and
288 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
pub mod node; | ||
|
||
use bevy_ecs::prelude::*; | ||
use bevy_render::{ | ||
render_phase::{CachedRenderPipelinePhaseItem, DrawFunctionId, EntityPhaseItem, PhaseItem}, | ||
render_resource::{CachedRenderPipelineId, Extent3d}, | ||
texture::CachedTexture, | ||
}; | ||
use bevy_utils::FloatOrd; | ||
|
||
/// Add a `PrepassSettings` component to a view to perform a depth and/or normal prepass. | ||
/// These textures are useful for reducing overdraw in the main pass, and screen-space effects. | ||
#[derive(Clone, Component)] | ||
pub struct PrepassSettings { | ||
/// If true then depth values will be copied to a separate texture available to the main pass. | ||
pub output_depth: bool, | ||
/// If true then vertex world normals will be copied to a separate texture available to the main pass. | ||
pub output_normals: bool, | ||
} | ||
|
||
impl Default for PrepassSettings { | ||
fn default() -> Self { | ||
Self { | ||
output_depth: true, | ||
output_normals: true, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Component)] | ||
pub struct ViewPrepassTextures { | ||
pub depth: Option<CachedTexture>, | ||
pub normals: Option<CachedTexture>, | ||
pub size: Extent3d, | ||
} | ||
|
||
pub struct OpaquePrepass { | ||
pub distance: f32, | ||
pub entity: Entity, | ||
pub pipeline_id: CachedRenderPipelineId, | ||
pub draw_function: DrawFunctionId, | ||
} | ||
|
||
impl PhaseItem for OpaquePrepass { | ||
type SortKey = FloatOrd; | ||
|
||
#[inline] | ||
fn sort_key(&self) -> Self::SortKey { | ||
FloatOrd(self.distance) | ||
} | ||
|
||
#[inline] | ||
fn draw_function(&self) -> DrawFunctionId { | ||
self.draw_function | ||
} | ||
|
||
#[inline] | ||
fn sort(items: &mut [Self]) { | ||
radsort::sort_by_key(items, |item| item.distance); | ||
} | ||
} | ||
|
||
impl EntityPhaseItem for OpaquePrepass { | ||
fn entity(&self) -> Entity { | ||
self.entity | ||
} | ||
} | ||
|
||
impl CachedRenderPipelinePhaseItem for OpaquePrepass { | ||
#[inline] | ||
fn cached_pipeline(&self) -> CachedRenderPipelineId { | ||
self.pipeline_id | ||
} | ||
} | ||
|
||
pub struct AlphaMaskPrepass { | ||
pub distance: f32, | ||
pub entity: Entity, | ||
pub pipeline_id: CachedRenderPipelineId, | ||
pub draw_function: DrawFunctionId, | ||
} | ||
|
||
impl PhaseItem for AlphaMaskPrepass { | ||
type SortKey = FloatOrd; | ||
|
||
#[inline] | ||
fn sort_key(&self) -> Self::SortKey { | ||
FloatOrd(self.distance) | ||
} | ||
|
||
#[inline] | ||
fn draw_function(&self) -> DrawFunctionId { | ||
self.draw_function | ||
} | ||
|
||
#[inline] | ||
fn sort(items: &mut [Self]) { | ||
radsort::sort_by_key(items, |item| item.distance); | ||
} | ||
} | ||
|
||
impl EntityPhaseItem for AlphaMaskPrepass { | ||
fn entity(&self) -> Entity { | ||
self.entity | ||
} | ||
} | ||
|
||
impl CachedRenderPipelinePhaseItem for AlphaMaskPrepass { | ||
#[inline] | ||
fn cached_pipeline(&self) -> CachedRenderPipelineId { | ||
self.pipeline_id | ||
} | ||
} |
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,145 @@ | ||
use bevy_ecs::prelude::*; | ||
use bevy_ecs::query::QueryState; | ||
use bevy_render::{ | ||
camera::ExtractedCamera, | ||
prelude::Color, | ||
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType}, | ||
render_phase::{DrawFunctions, RenderPhase, TrackedRenderPass}, | ||
render_resource::{ | ||
LoadOp, Operations, RenderPassColorAttachment, RenderPassDepthStencilAttachment, | ||
RenderPassDescriptor, | ||
}, | ||
renderer::RenderContext, | ||
view::{ExtractedView, ViewDepthTexture}, | ||
}; | ||
#[cfg(feature = "trace")] | ||
use bevy_utils::tracing::info_span; | ||
|
||
use super::{AlphaMaskPrepass, OpaquePrepass, ViewPrepassTextures}; | ||
|
||
pub struct PrepassNode { | ||
main_view_query: QueryState< | ||
( | ||
&'static ExtractedCamera, | ||
&'static RenderPhase<OpaquePrepass>, | ||
&'static RenderPhase<AlphaMaskPrepass>, | ||
&'static ViewDepthTexture, | ||
&'static ViewPrepassTextures, | ||
), | ||
With<ExtractedView>, | ||
>, | ||
} | ||
|
||
impl PrepassNode { | ||
pub const IN_VIEW: &'static str = "view"; | ||
|
||
pub fn new(world: &mut World) -> Self { | ||
Self { | ||
main_view_query: QueryState::new(world), | ||
} | ||
} | ||
} | ||
|
||
impl Node for PrepassNode { | ||
fn input(&self) -> Vec<SlotInfo> { | ||
vec![SlotInfo::new(PrepassNode::IN_VIEW, SlotType::Entity)] | ||
} | ||
|
||
fn update(&mut self, world: &mut World) { | ||
self.main_view_query.update_archetypes(world); | ||
} | ||
|
||
fn run( | ||
&self, | ||
graph: &mut RenderGraphContext, | ||
render_context: &mut RenderContext, | ||
world: &World, | ||
) -> Result<(), NodeRunError> { | ||
let view_entity = graph.get_input_entity(Self::IN_VIEW)?; | ||
if let Ok(( | ||
camera, | ||
opaque_prepass_phase, | ||
alpha_mask_prepass_phase, | ||
view_depth_texture, | ||
view_prepass_textures, | ||
)) = self.main_view_query.get_manual(world, view_entity) | ||
{ | ||
if opaque_prepass_phase.items.is_empty() && alpha_mask_prepass_phase.items.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
let mut color_attachments = vec![]; | ||
if let Some(view_normals_texture) = &view_prepass_textures.normals { | ||
color_attachments.push(Some(RenderPassColorAttachment { | ||
view: &view_normals_texture.default_view, | ||
resolve_target: None, | ||
ops: Operations { | ||
load: LoadOp::Clear(Color::BLACK.into()), | ||
store: true, | ||
}, | ||
})); | ||
} | ||
|
||
{ | ||
// Set up the pass descriptor with the depth attachment and maybe colour attachment | ||
let pass_descriptor = RenderPassDescriptor { | ||
label: Some("prepass"), | ||
color_attachments: &color_attachments, | ||
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment { | ||
view: &view_depth_texture.view, | ||
depth_ops: Some(Operations { | ||
load: LoadOp::Clear(0.0), | ||
store: true, | ||
}), | ||
stencil_ops: None, | ||
}), | ||
}; | ||
|
||
let render_pass = render_context | ||
.command_encoder | ||
.begin_render_pass(&pass_descriptor); | ||
let mut tracked_pass = TrackedRenderPass::new(render_pass); | ||
if let Some(viewport) = camera.viewport.as_ref() { | ||
tracked_pass.set_camera_viewport(viewport); | ||
} | ||
|
||
{ | ||
// Run the depth prepass, sorted front-to-back | ||
#[cfg(feature = "trace")] | ||
let _opaque_prepass_span = info_span!("opaque_prepass").entered(); | ||
let draw_functions = world.resource::<DrawFunctions<OpaquePrepass>>(); | ||
|
||
let mut draw_functions = draw_functions.write(); | ||
for item in &opaque_prepass_phase.items { | ||
let draw_function = draw_functions.get_mut(item.draw_function).unwrap(); | ||
draw_function.draw(world, &mut tracked_pass, view_entity, item); | ||
} | ||
} | ||
|
||
{ | ||
// Run the depth prepass, sorted front-to-back | ||
#[cfg(feature = "trace")] | ||
let _alpha_mask_prepass_span = info_span!("alpha_mask_prepass").entered(); | ||
let draw_functions = world.resource::<DrawFunctions<AlphaMaskPrepass>>(); | ||
|
||
let mut draw_functions = draw_functions.write(); | ||
for item in &alpha_mask_prepass_phase.items { | ||
let draw_function = draw_functions.get_mut(item.draw_function).unwrap(); | ||
draw_function.draw(world, &mut tracked_pass, view_entity, item); | ||
} | ||
} | ||
} | ||
|
||
if let Some(prepass_depth_texture) = &view_prepass_textures.depth { | ||
// copy depth buffer to texture | ||
render_context.command_encoder.copy_texture_to_texture( | ||
view_depth_texture.texture.as_image_copy(), | ||
prepass_depth_texture.texture.as_image_copy(), | ||
view_prepass_textures.size, | ||
); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
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.