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,