From d516a27dc6a3a8ddf09c2c259dbe99d6235ce3a9 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Mon, 4 Sep 2023 11:26:05 -0400 Subject: [PATCH] Cache depth texture based on usage (#9565) # Objective - Currently, the depth textures are cached based on the target. If multiple camera have the same target but a different `depth_texture_usage` bevy will just use the same texture and ignore that setting. ## Solution - Add the usage as a cache key --- crates/bevy_core_pipeline/src/core_3d/mod.rs | 27 ++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index f415751cde719..04f4f0973998f 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -340,8 +340,22 @@ pub fn prepare_core_3d_depth_textures( ), >, ) { + let mut render_target_usage = HashMap::default(); + for (_, camera, depth_prepass, camera_3d) in &views_3d { + // Default usage required to write to the depth texture + let mut usage: TextureUsages = camera_3d.depth_texture_usages.into(); + if depth_prepass.is_some() { + // Required to read the output of the prepass + usage |= TextureUsages::COPY_SRC; + } + render_target_usage + .entry(camera.target.clone()) + .and_modify(|u| *u |= usage) + .or_insert_with(|| usage); + } + let mut textures = HashMap::default(); - for (entity, camera, depth_prepass, camera_3d) in &views_3d { + for (entity, camera, _, _) in &views_3d { let Some(physical_target_size) = camera.physical_target_size else { continue; }; @@ -349,13 +363,6 @@ pub fn prepare_core_3d_depth_textures( let cached_texture = textures .entry(camera.target.clone()) .or_insert_with(|| { - // Default usage required to write to the depth texture - let mut usage = camera_3d.depth_texture_usages.into(); - if depth_prepass.is_some() { - // Required to read the output of the prepass - usage |= TextureUsages::COPY_SRC; - } - // The size of the depth texture let size = Extent3d { depth_or_array_layers: 1, @@ -363,6 +370,10 @@ pub fn prepare_core_3d_depth_textures( height: physical_target_size.y, }; + let usage = *render_target_usage + .get(&camera.target.clone()) + .expect("The depth texture usage should already exist for this target"); + let descriptor = TextureDescriptor { label: Some("view_depth_texture"), size,