Allow custom depth texture usage (#6815)
# Objective Sometimes we might want to read from the depth texture in some custom rendering features. We must then add `STORAGE_BINDING` or `TEXTURE_BINDING` to the texture usage flags when creating them. ## Solution This PR allows one to customize the usage flags in the `Camera3d` component.
This commit is contained in:
parent
8930cfcdd4
commit
44a365d540
@ -8,14 +8,14 @@ use bevy_render::{
|
|||||||
camera::{Camera, CameraRenderGraph, Projection},
|
camera::{Camera, CameraRenderGraph, Projection},
|
||||||
extract_component::ExtractComponent,
|
extract_component::ExtractComponent,
|
||||||
primitives::Frustum,
|
primitives::Frustum,
|
||||||
render_resource::LoadOp,
|
render_resource::{LoadOp, TextureUsages},
|
||||||
view::{ColorGrading, VisibleEntities},
|
view::{ColorGrading, VisibleEntities},
|
||||||
};
|
};
|
||||||
use bevy_transform::prelude::{GlobalTransform, Transform};
|
use bevy_transform::prelude::{GlobalTransform, Transform};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Configuration for the "main 3d render graph".
|
/// Configuration for the "main 3d render graph".
|
||||||
#[derive(Component, Reflect, Clone, Default, ExtractComponent)]
|
#[derive(Component, Reflect, Clone, ExtractComponent)]
|
||||||
#[extract_component_filter(With<Camera>)]
|
#[extract_component_filter(With<Camera>)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Camera3d {
|
pub struct Camera3d {
|
||||||
@ -23,6 +23,32 @@ pub struct Camera3d {
|
|||||||
pub clear_color: ClearColorConfig,
|
pub clear_color: ClearColorConfig,
|
||||||
/// The depth clear operation to perform for the main 3d pass.
|
/// The depth clear operation to perform for the main 3d pass.
|
||||||
pub depth_load_op: Camera3dDepthLoadOp,
|
pub depth_load_op: Camera3dDepthLoadOp,
|
||||||
|
/// The texture usages for the depth texture created for the main 3d pass.
|
||||||
|
pub depth_texture_usages: Camera3dDepthTextureUsage,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Camera3d {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
clear_color: ClearColorConfig::Default,
|
||||||
|
depth_load_op: Default::default(),
|
||||||
|
depth_texture_usages: TextureUsages::RENDER_ATTACHMENT.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Reflect)]
|
||||||
|
pub struct Camera3dDepthTextureUsage(u32);
|
||||||
|
|
||||||
|
impl From<TextureUsages> for Camera3dDepthTextureUsage {
|
||||||
|
fn from(value: TextureUsages) -> Self {
|
||||||
|
Self(value.bits())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<Camera3dDepthTextureUsage> for TextureUsages {
|
||||||
|
fn from(value: Camera3dDepthTextureUsage) -> Self {
|
||||||
|
Self::from_bits_truncate(value.0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The depth clear operation to perform for the main 3d pass.
|
/// The depth clear operation to perform for the main 3d pass.
|
||||||
|
|||||||
@ -257,7 +257,7 @@ pub fn prepare_core_3d_depth_textures(
|
|||||||
msaa: Res<Msaa>,
|
msaa: Res<Msaa>,
|
||||||
render_device: Res<RenderDevice>,
|
render_device: Res<RenderDevice>,
|
||||||
views_3d: Query<
|
views_3d: Query<
|
||||||
(Entity, &ExtractedCamera, Option<&DepthPrepass>),
|
(Entity, &ExtractedCamera, Option<&DepthPrepass>, &Camera3d),
|
||||||
(
|
(
|
||||||
With<RenderPhase<Opaque3d>>,
|
With<RenderPhase<Opaque3d>>,
|
||||||
With<RenderPhase<AlphaMask3d>>,
|
With<RenderPhase<AlphaMask3d>>,
|
||||||
@ -266,7 +266,7 @@ pub fn prepare_core_3d_depth_textures(
|
|||||||
>,
|
>,
|
||||||
) {
|
) {
|
||||||
let mut textures = HashMap::default();
|
let mut textures = HashMap::default();
|
||||||
for (entity, camera, depth_prepass) in &views_3d {
|
for (entity, camera, depth_prepass, camera_3d) in &views_3d {
|
||||||
let Some(physical_target_size) = camera.physical_target_size else {
|
let Some(physical_target_size) = camera.physical_target_size else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@ -275,7 +275,7 @@ pub fn prepare_core_3d_depth_textures(
|
|||||||
.entry(camera.target.clone())
|
.entry(camera.target.clone())
|
||||||
.or_insert_with(|| {
|
.or_insert_with(|| {
|
||||||
// Default usage required to write to the depth texture
|
// Default usage required to write to the depth texture
|
||||||
let mut usage = TextureUsages::RENDER_ATTACHMENT;
|
let mut usage = camera_3d.depth_texture_usages.into();
|
||||||
if depth_prepass.is_some() {
|
if depth_prepass.is_some() {
|
||||||
// Required to read the output of the prepass
|
// Required to read the output of the prepass
|
||||||
usage |= TextureUsages::COPY_SRC;
|
usage |= TextureUsages::COPY_SRC;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user