Skip to content

Commit

Permalink
Customizable camera main texture usage (#11412)
Browse files Browse the repository at this point in the history
# Objective

- Some users want to change the default texture usage of the main camera
but they are currently hardcoded

## Solution

- Add a component that is used to configure the main texture usage field

---

## Changelog

Added `CameraMainTextureUsage`
Added `CameraMainTextureUsage` to `Camera3dBundle` and `Camera2dBundle`

## Migration Guide

Add `main_texture_usages: Default::default()` to your camera bundle.

# Notes

Inspired by: #6815
  • Loading branch information
IceSentry authored Jan 18, 2024
1 parent 03ee959 commit 7125dcb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
8 changes: 7 additions & 1 deletion crates/bevy_core_pipeline/src/core_2d/camera_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::tonemapping::{DebandDither, Tonemapping};
use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
use bevy_render::{
camera::{Camera, CameraProjection, CameraRenderGraph, OrthographicProjection},
camera::{
Camera, CameraMainTextureUsages, CameraProjection, CameraRenderGraph,
OrthographicProjection,
},
extract_component::ExtractComponent,
primitives::Frustum,
view::VisibleEntities,
Expand All @@ -26,6 +29,7 @@ pub struct Camera2dBundle {
pub camera_2d: Camera2d,
pub tonemapping: Tonemapping,
pub deband_dither: DebandDither,
pub main_texture_usages: CameraMainTextureUsages,
}

impl Default for Camera2dBundle {
Expand Down Expand Up @@ -55,6 +59,7 @@ impl Default for Camera2dBundle {
camera_2d: Camera2d,
tonemapping: Tonemapping::None,
deband_dither: DebandDither::Disabled,
main_texture_usages: Default::default(),
}
}
}
Expand Down Expand Up @@ -93,6 +98,7 @@ impl Camera2dBundle {
camera_2d: Camera2d,
tonemapping: Tonemapping::None,
deband_dither: DebandDither::Disabled,
main_texture_usages: Default::default(),
}
}
}
4 changes: 3 additions & 1 deletion crates/bevy_core_pipeline/src/core_3d/camera_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::tonemapping::{DebandDither, Tonemapping};
use bevy_ecs::prelude::*;
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::{
camera::{Camera, CameraRenderGraph, Projection},
camera::{Camera, CameraMainTextureUsages, CameraRenderGraph, Projection},
extract_component::ExtractComponent,
primitives::Frustum,
render_resource::{LoadOp, TextureUsages},
Expand Down Expand Up @@ -143,6 +143,7 @@ pub struct Camera3dBundle {
pub tonemapping: Tonemapping,
pub dither: DebandDither,
pub color_grading: ColorGrading,
pub main_texture_usages: CameraMainTextureUsages,
}

// NOTE: ideally Perspective and Orthographic defaults can share the same impl, but sadly it breaks rust's type inference
Expand All @@ -160,6 +161,7 @@ impl Default for Camera3dBundle {
tonemapping: Default::default(),
dither: DebandDither::Enabled,
color_grading: ColorGrading::default(),
main_texture_usages: Default::default(),
}
}
}
16 changes: 15 additions & 1 deletion crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ use bevy_math::{
primitives::Direction3d, vec2, Mat4, Ray3d, Rect, URect, UVec2, UVec4, Vec2, Vec3,
};
use bevy_reflect::prelude::*;
use bevy_render_macros::ExtractComponent;
use bevy_transform::components::GlobalTransform;
use bevy_utils::{HashMap, HashSet};
use bevy_window::{
NormalizedWindowRef, PrimaryWindow, Window, WindowCreated, WindowRef, WindowResized,
WindowScaleFactorChanged,
};
use std::{borrow::Cow, ops::Range};
use wgpu::{BlendState, LoadOp, TextureFormat};
use wgpu::{BlendState, LoadOp, TextureFormat, TextureUsages};

use super::{ClearColorConfig, Projection};

Expand Down Expand Up @@ -750,6 +751,19 @@ pub fn camera_system<T: CameraProjection + Component>(
}
}

/// This component lets you control the [`TextureUsages`] field of the main texture generated for the camera
#[derive(Component, ExtractComponent, Clone, Copy)]
pub struct CameraMainTextureUsages(pub TextureUsages);
impl Default for CameraMainTextureUsages {
fn default() -> Self {
Self(
TextureUsages::RENDER_ATTACHMENT
| TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_SRC,
)
}
}

#[derive(Component, Debug)]
pub struct ExtractedCamera {
pub target: Option<NormalizedRenderTarget>,
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_render/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub use manual_texture_view::*;
pub use projection::*;

use crate::{
extract_resource::ExtractResourcePlugin, render_graph::RenderGraph, ExtractSchedule, Render,
RenderApp, RenderSet,
extract_component::ExtractComponentPlugin, extract_resource::ExtractResourcePlugin,
render_graph::RenderGraph, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_app::{App, Plugin};
use bevy_ecs::schedule::IntoSystemConfigs;
Expand All @@ -39,6 +39,7 @@ impl Plugin for CameraPlugin {
CameraProjectionPlugin::<PerspectiveProjection>::default(),
ExtractResourcePlugin::<ManualTextureViews>::default(),
ExtractResourcePlugin::<ClearColor>::default(),
ExtractComponentPlugin::<CameraMainTextureUsages>::default(),
));

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
Expand Down
17 changes: 10 additions & 7 deletions crates/bevy_render/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub use window::*;

use crate::{
camera::{
ClearColor, ClearColorConfig, ExposureSettings, ExtractedCamera, ManualTextureViews,
MipBias, TemporalJitter,
CameraMainTextureUsages, ClearColor, ClearColorConfig, ExposureSettings, ExtractedCamera,
ManualTextureViews, MipBias, TemporalJitter,
},
extract_resource::{ExtractResource, ExtractResourcePlugin},
prelude::{Image, Shader},
Expand Down Expand Up @@ -465,11 +465,16 @@ fn prepare_view_targets(
clear_color_global: Res<ClearColor>,
render_device: Res<RenderDevice>,
mut texture_cache: ResMut<TextureCache>,
cameras: Query<(Entity, &ExtractedCamera, &ExtractedView)>,
cameras: Query<(
Entity,
&ExtractedCamera,
&ExtractedView,
&CameraMainTextureUsages,
)>,
manual_texture_views: Res<ManualTextureViews>,
) {
let mut textures = HashMap::default();
for (entity, camera, view) in cameras.iter() {
for (entity, camera, view, texture_usage) in cameras.iter() {
if let (Some(target_size), Some(target)) = (camera.physical_target_size, &camera.target) {
if let (Some(out_texture_view), Some(out_texture_format)) = (
target.get_texture_view(&windows, &images, &manual_texture_views),
Expand Down Expand Up @@ -502,9 +507,7 @@ fn prepare_view_targets(
sample_count: 1,
dimension: TextureDimension::D2,
format: main_texture_format,
usage: TextureUsages::RENDER_ATTACHMENT
| TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_SRC,
usage: texture_usage.0,
view_formats: match main_texture_format {
TextureFormat::Bgra8Unorm => &[TextureFormat::Bgra8UnormSrgb],
TextureFormat::Rgba8Unorm => &[TextureFormat::Rgba8UnormSrgb],
Expand Down

0 comments on commit 7125dcb

Please sign in to comment.