Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS55 committed Nov 10, 2023
1 parent 104742e commit 5c0e9a6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
clear_color::{ClearColor, ClearColorConfig},
core_3d::{Camera3d, Opaque3d},
prepass::{DeferredPrepass, DepthPrepass, MotionVectorPrepass, NormalPrepass},
skybox::{SkyboxBindGroup, SkyboxPipelineId},
};
use bevy_ecs::{prelude::*, query::QueryItem};
Expand Down
14 changes: 9 additions & 5 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ pub const CORE_3D: &str = graph::NAME;
// PERF: vulkan docs recommend using 24 bit depth for better performance
pub const CORE_3D_DEPTH_FORMAT: TextureFormat = TextureFormat::Depth32Float;

use std::{cmp::Reverse, ops::Range};
use std::{
cmp::Reverse,
ops::Range,
sync::{atomic::AtomicBool, Arc},
};

pub use camera_3d::*;
pub use main_opaque_pass_3d_node::*;
Expand Down Expand Up @@ -559,10 +563,9 @@ pub fn prepare_core_3d_depth_textures(
})
.clone();

commands.entity(entity).insert(ViewDepthTexture::new(
cached_texture.texture,
cached_texture.default_view,
));
commands
.entity(entity)
.insert(ViewDepthTexture::new(cached_texture));
}
}

Expand Down Expand Up @@ -829,6 +832,7 @@ pub fn prepare_prepass_textures(
deferred: cached_deferred_texture,
deferred_lighting_pass_id: deferred_lighting_pass_id_texture,
size,
first_write: Arc::new(AtomicBool::new(true)),
});
}
}
16 changes: 15 additions & 1 deletion crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@
pub mod node;

use std::{cmp::Reverse, ops::Range};
use std::{
cmp::Reverse,
ops::Range,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};

use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -81,6 +88,13 @@ pub struct ViewPrepassTextures {
pub deferred_lighting_pass_id: Option<CachedTexture>,
/// The size of the textures.
pub size: Extent3d,
pub(crate) first_write: Arc<AtomicBool>,
}

impl ViewPrepassTextures {
pub fn is_first_write(&self) -> bool {
self.first_write.fetch_and(false, Ordering::SeqCst)
}
}

/// Opaque phase of the 3D prepass.
Expand Down
26 changes: 24 additions & 2 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ use bevy_utils::{
tracing::{error, warn},
HashMap,
};
use std::{hash::Hash, num::NonZeroU64, ops::Range};
use std::{
hash::Hash,
num::NonZeroU64,
ops::Range,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};

use crate::*;

Expand Down Expand Up @@ -514,6 +522,13 @@ fn face_index_to_name(face_index: usize) -> &'static str {
pub struct ShadowView {
pub depth_texture_view: TextureView,
pub pass_name: String,
first_write: Arc<AtomicBool>,
}

impl ShadowView {
pub fn is_first_write(&self) -> bool {
self.first_write.fetch_and(false, Ordering::SeqCst)
}
}

#[derive(Component)]
Expand Down Expand Up @@ -1004,6 +1019,7 @@ pub fn prepare_lights(
light_index,
face_index_to_name(face_index)
),
first_write: Arc::new(AtomicBool::new(true)),
},
ExtractedView {
viewport: UVec4::new(
Expand Down Expand Up @@ -1062,6 +1078,7 @@ pub fn prepare_lights(
ShadowView {
depth_texture_view,
pass_name: format!("shadow pass spot light {light_index}"),
first_write: Arc::new(AtomicBool::new(true)),
},
ExtractedView {
viewport: UVec4::new(
Expand Down Expand Up @@ -1128,6 +1145,7 @@ pub fn prepare_lights(
depth_texture_view,
pass_name: format!(
"shadow pass directional light {light_index} cascade {cascade_index}"),
first_write: Arc::new(AtomicBool::new(true)),
},
ExtractedView {
viewport: UVec4::new(
Expand Down Expand Up @@ -1760,7 +1778,11 @@ impl Node for ShadowPassNode {
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &view_light.depth_texture_view,
depth_ops: Some(Operations {
load: LoadOp::Clear(0.0),
load: if view_light.is_first_write() {
LoadOp::Clear(0.0)
} else {
LoadOp::Load
},
store: true,
}),
stencil_ops: None,
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_render/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ pub struct ViewDepthTexture {
}

impl ViewDepthTexture {
pub fn new(texture: Texture, view: TextureView) -> Self {
pub fn new(texture: CachedTexture) -> Self {
Self {
texture,
view,
texture: texture.texture,
view: texture.default_view,
first_write: Arc::new(AtomicBool::new(true)),
}
}
Expand Down

0 comments on commit 5c0e9a6

Please sign in to comment.