Skip to content

Commit 4000a36

Browse files
committed
Ensure depth texture is shared between passes
1 parent 7cdfa33 commit 4000a36

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

crates/bevy_core_pipeline/src/lib.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use bevy_app::{App, Plugin};
2323
use bevy_core::FloatOrd;
2424
use bevy_ecs::prelude::*;
2525
use bevy_render::{
26-
camera::{ActiveCameras, CameraPlugin, RenderTarget},
26+
camera::{ActiveCameras, CameraPlugin, ExtractedCamera, RenderTarget},
2727
color::Color,
2828
render_graph::{EmptyNode, RenderGraph, SlotInfo, SlotType},
2929
render_phase::{
@@ -393,32 +393,43 @@ pub fn prepare_core_views_system(
393393
msaa: Res<Msaa>,
394394
render_device: Res<RenderDevice>,
395395
views_3d: Query<
396-
(Entity, &ExtractedView),
396+
(Entity, &ExtractedView, Option<&ExtractedCamera>),
397397
(
398398
With<RenderPhase<Opaque3d>>,
399399
With<RenderPhase<AlphaMask3d>>,
400400
With<RenderPhase<Transparent3d>>,
401401
),
402402
>,
403403
) {
404-
for (entity, view) in views_3d.iter() {
405-
let cached_texture = texture_cache.get(
406-
&render_device,
407-
TextureDescriptor {
408-
label: Some("view_depth_texture"),
409-
size: Extent3d {
410-
depth_or_array_layers: 1,
411-
width: view.width as u32,
412-
height: view.height as u32,
404+
let mut textures = HashMap::default();
405+
for (entity, view, camera) in views_3d.iter() {
406+
let mut get_cached_texture = || {
407+
texture_cache.get(
408+
&render_device,
409+
TextureDescriptor {
410+
label: Some("view_depth_texture"),
411+
size: Extent3d {
412+
depth_or_array_layers: 1,
413+
width: view.width as u32,
414+
height: view.height as u32,
415+
},
416+
mip_level_count: 1,
417+
sample_count: msaa.samples,
418+
dimension: TextureDimension::D2,
419+
format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24
420+
* bit depth for better performance */
421+
usage: TextureUsages::RENDER_ATTACHMENT,
413422
},
414-
mip_level_count: 1,
415-
sample_count: msaa.samples,
416-
dimension: TextureDimension::D2,
417-
format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24
418-
* bit depth for better performance */
419-
usage: TextureUsages::RENDER_ATTACHMENT,
420-
},
421-
);
423+
)
424+
};
425+
let cached_texture = if let Some(camera) = camera {
426+
textures
427+
.entry(camera.target.clone())
428+
.or_insert_with(get_cached_texture)
429+
.clone()
430+
} else {
431+
get_cached_texture()
432+
};
422433
commands.entity(entity).insert(ViewDepthTexture {
423434
texture: cached_texture.texture,
424435
view: cached_texture.default_view,

crates/bevy_core_pipeline/src/main_pass_3d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Node for MainPass3dNode {
139139
// transparent ones.
140140
depth_ops: Some(Operations {
141141
load: LoadOp::Load,
142-
store: false,
142+
store: true,
143143
}),
144144
stencil_ops: None,
145145
}),

crates/bevy_render/src/texture/texture_cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct CachedTextureMeta {
1818
/// A cached GPU [`Texture`] with corresponding [`TextureView`].
1919
/// This is useful for textures that are created repeatedly (each frame) in the rendering process
2020
/// to reduce the amount of GPU memory allocations.
21+
#[derive(Clone)]
2122
pub struct CachedTexture {
2223
pub texture: Texture,
2324
pub default_view: TextureView,

examples/3d/two_passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn setup(
104104
..Default::default()
105105
});
106106

107-
let split = 1.0;
107+
let split = 2.0;
108108

109109
// This specifies the layer used for the first pass, which will be attached to the first pass camera and cube.
110110
let first_pass_layer = RenderLayers::layer(1);
@@ -158,7 +158,7 @@ fn setup(
158158
mesh: cube_handle,
159159
material: material_handle,
160160
transform: Transform {
161-
translation: Vec3::new(split, 0.0, 1.5),
161+
translation: Vec3::new(split, 0.0, -4.5),
162162
rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0),
163163
..Default::default()
164164
},

0 commit comments

Comments
 (0)