Skip to content

Commit

Permalink
Cache depth texture based on usage (bevyengine#9565)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
IceSentry authored Sep 4, 2023
1 parent 9309d89 commit d516a27
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,29 +340,40 @@ 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;
};

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,
width: physical_target_size.x,
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,
Expand Down

0 comments on commit d516a27

Please sign in to comment.