Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - separate tonemapping and upscaling passes #3425

Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3a86a0e
add separate tonemapping and upscaling pass
jakobhellermann Dec 23, 2021
315ef6f
return rendered-to texture view as output
jakobhellermann Dec 24, 2021
cf014e8
create bind group in tonemapping/upscaling node to enable customization
jakobhellermann Dec 29, 2021
babcbb2
make hdr configurable
jakobhellermann Dec 30, 2021
21ba00d
add UpscalingPipelineKey
jakobhellermann Mar 20, 2022
3f4b10f
address pr feedback
jakobhellermann Mar 21, 2022
4cf1966
extract full screen vertex shader into a shared internal asset
jakobhellermann Mar 21, 2022
eac57bb
Merge remote-tracking branch 'origin/main' into pr/jakobhellermann/34…
cart Jun 10, 2022
ff3bfc3
initial (broken) port
cart Jun 10, 2022
aeb5190
Fix upscaling
cart Jun 11, 2022
e8d01d6
fix multiple runs on the same target
cart Jun 11, 2022
8b9c5cf
fix tonemapping
cart Jun 12, 2022
60d6a17
Merge remote-tracking branch 'origin/main' into pr/jakobhellermann/34…
cart Jun 26, 2022
2cae409
add tonemapping and upscaling to 2d
cart Jun 27, 2022
723f692
Separate tonemapping config from hdr. Disable tonemapping for sprites…
cart Jun 27, 2022
823dc8d
Merge remote-tracking branch 'origin/main' into pr/jakobhellermann/34…
cart Jul 16, 2022
ac49b15
Fix 2d texture formats
cart Jul 16, 2022
a8cdf85
Merge remote-tracking branch 'origin/main' into pr/jakobhellermann/34…
cart Jul 16, 2022
e0c3aff
Merge remote-tracking branch 'origin/main' into pr/jakobhellermann/34…
cart Jul 16, 2022
f2c23a9
fixe mesh2d_manual example
cart Jul 16, 2022
4efdb52
hdr->ldr
cart Jul 16, 2022
ed14c98
Merge remote-tracking branch 'origin/main' into pr/jakobhellermann/34…
cart Oct 24, 2022
4cf7452
use surface texture format in upscaling node
cart Oct 25, 2022
884e1a8
Default to Rgba8UnormSrgb unconditionally on all platforms
cart Oct 25, 2022
294a9e0
Disable hdr by default
cart Oct 25, 2022
5dc45e1
Render UI to intermediate target texture, post tonemapping, pre upsca…
cart Oct 25, 2022
5b9cdf1
adjust tonemapping and upscaling plugins to not panic when renderer i…
cart Oct 25, 2022
efee4bc
Merge remote-tracking branch 'origin/main' into pr/jakobhellermann/34…
cart Oct 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }

serde = { version = "1", features = ["derive"] }
bitflags = "1.2"
radsort = "0.1"
4 changes: 3 additions & 1 deletion crates/bevy_core_pipeline/src/core_2d/camera_2d.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::clear_color::ClearColorConfig;
use crate::{clear_color::ClearColorConfig, tonemapping::Tonemapping};
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_reflect::Reflect;
use bevy_render::{
Expand Down Expand Up @@ -36,6 +36,7 @@ pub struct Camera2dBundle {
pub transform: Transform,
pub global_transform: GlobalTransform,
pub camera_2d: Camera2d,
pub tonemapping: Tonemapping,
}

impl Default for Camera2dBundle {
Expand Down Expand Up @@ -77,6 +78,7 @@ impl Camera2dBundle {
global_transform: Default::default(),
camera: Camera::default(),
camera_2d: Camera2d::default(),
tonemapping: Tonemapping { is_enabled: false },
}
}
}
30 changes: 30 additions & 0 deletions crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod graph {
}
pub mod node {
pub const MAIN_PASS: &str = "main_pass";
pub const TONEMAPPING: &str = "tonemapping";
pub const UPSCALING: &str = "upscaling";
}
}

Expand All @@ -30,6 +32,8 @@ use bevy_render::{
use bevy_utils::FloatOrd;
use std::ops::Range;

use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode};

pub struct Core2dPlugin;

impl Plugin for Core2dPlugin {
Expand All @@ -49,10 +53,14 @@ impl Plugin for Core2dPlugin {
.add_system_to_stage(RenderStage::PhaseSort, batch_phase_system::<Transparent2d>);

let pass_node_2d = MainPass2dNode::new(&mut render_app.world);
let tonemapping = TonemappingNode::new(&mut render_app.world);
let upscaling = UpscalingNode::new(&mut render_app.world);
let mut graph = render_app.world.resource_mut::<RenderGraph>();

let mut draw_2d_graph = RenderGraph::default();
draw_2d_graph.add_node(graph::node::MAIN_PASS, pass_node_2d);
draw_2d_graph.add_node(graph::node::TONEMAPPING, tonemapping);
draw_2d_graph.add_node(graph::node::UPSCALING, upscaling);
let input_node_id = draw_2d_graph.set_input(vec![SlotInfo::new(
graph::input::VIEW_ENTITY,
SlotType::Entity,
Expand All @@ -65,6 +73,28 @@ impl Plugin for Core2dPlugin {
MainPass2dNode::IN_VIEW,
)
.unwrap();
draw_2d_graph
.add_slot_edge(
input_node_id,
graph::input::VIEW_ENTITY,
graph::node::TONEMAPPING,
TonemappingNode::IN_VIEW,
)
.unwrap();
draw_2d_graph
.add_slot_edge(
input_node_id,
graph::input::VIEW_ENTITY,
graph::node::UPSCALING,
UpscalingNode::IN_VIEW,
)
.unwrap();
draw_2d_graph
.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING)
.unwrap();
draw_2d_graph
.add_node_edge(graph::node::TONEMAPPING, graph::node::UPSCALING)
.unwrap();
graph.add_sub_graph(graph::NAME, draw_2d_graph);
}
}
Expand Down
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
@@ -1,4 +1,4 @@
use crate::clear_color::ClearColorConfig;
use crate::{clear_color::ClearColorConfig, tonemapping::Tonemapping};
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::{
Expand Down Expand Up @@ -66,13 +66,15 @@ pub struct Camera3dBundle {
pub transform: Transform,
pub global_transform: GlobalTransform,
pub camera_3d: Camera3d,
pub tonemapping: Tonemapping,
}

// NOTE: ideally Perspective and Orthographic defaults can share the same impl, but sadly it breaks rust's type inference
impl Default for Camera3dBundle {
fn default() -> Self {
Self {
camera_render_graph: CameraRenderGraph::new(crate::core_3d::graph::NAME),
tonemapping: Tonemapping { is_enabled: true },
camera: Default::default(),
projection: Default::default(),
visible_entities: Default::default(),
Expand Down
30 changes: 30 additions & 0 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod graph {
}
pub mod node {
pub const MAIN_PASS: &str = "main_pass";
pub const TONEMAPPING: &str = "tonemapping";
pub const UPSCALING: &str = "upscaling";
}
}

Expand Down Expand Up @@ -36,6 +38,8 @@ use bevy_render::{
};
use bevy_utils::{FloatOrd, HashMap};

use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode};

pub struct Core3dPlugin;

impl Plugin for Core3dPlugin {
Expand All @@ -59,10 +63,14 @@ impl Plugin for Core3dPlugin {
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<Transparent3d>);

let pass_node_3d = MainPass3dNode::new(&mut render_app.world);
let tonemapping = TonemappingNode::new(&mut render_app.world);
let upscaling = UpscalingNode::new(&mut render_app.world);
let mut graph = render_app.world.resource_mut::<RenderGraph>();

let mut draw_3d_graph = RenderGraph::default();
draw_3d_graph.add_node(graph::node::MAIN_PASS, pass_node_3d);
draw_3d_graph.add_node(graph::node::TONEMAPPING, tonemapping);
draw_3d_graph.add_node(graph::node::UPSCALING, upscaling);
let input_node_id = draw_3d_graph.set_input(vec![SlotInfo::new(
graph::input::VIEW_ENTITY,
SlotType::Entity,
Expand All @@ -75,6 +83,28 @@ impl Plugin for Core3dPlugin {
MainPass3dNode::IN_VIEW,
)
.unwrap();
draw_3d_graph
.add_slot_edge(
input_node_id,
graph::input::VIEW_ENTITY,
graph::node::TONEMAPPING,
TonemappingNode::IN_VIEW,
)
.unwrap();
draw_3d_graph
.add_slot_edge(
input_node_id,
graph::input::VIEW_ENTITY,
graph::node::UPSCALING,
UpscalingNode::IN_VIEW,
)
.unwrap();
draw_3d_graph
.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING)
.unwrap();
draw_3d_graph
.add_node_edge(graph::node::TONEMAPPING, graph::node::UPSCALING)
.unwrap();
graph.add_sub_graph(graph::NAME, draw_3d_graph);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#define_import_path bevy_core_pipeline::fullscreen_vertex_shader

struct FullscreenVertexOutput {
[[builtin(position)]]
position: vec4<f32>;
[[location(0)]]
uv: vec2<f32>;
};

[[stage(vertex)]]
fn fullscreen_vertex_shader([[builtin(vertex_index)]] vertex_index: u32) -> FullscreenVertexOutput {
let uv = vec2<f32>(f32(vertex_index >> 1u), f32(vertex_index & 1u)) * 2.0;
let clip_position = vec4<f32>(uv * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0), 0.0, 1.0);

return FullscreenVertexOutput(clip_position, uv);
}
26 changes: 26 additions & 0 deletions crates/bevy_core_pipeline/src/fullscreen_vertex_shader/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use bevy_asset::HandleUntyped;
use bevy_reflect::TypeUuid;
use bevy_render::{prelude::Shader, render_resource::VertexState};

pub const FULLSCREEN_SHADER_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 7837534426033940724);

/// uses the [`FULLSCREEN_SHADER_HANDLE`] to output a
/// ```wgsl
/// struct FullscreenVertexOutput {
/// [[builtin(position)]]
/// position: vec4<f32>;
/// [[location(0)]]
/// uv: vec2<f32>;
/// };
/// ```
/// from the vertex shader.
/// The draw call should render one triangle: `render_pass.draw(0..3, 0..1);`
pub fn fullscreen_shader_vertex_state() -> VertexState {
VertexState {
shader: FULLSCREEN_SHADER_HANDLE.typed(),
shader_defs: Vec::new(),
entry_point: "fullscreen_vertex_shader".into(),
buffers: Vec::new(),
}
}
21 changes: 19 additions & 2 deletions crates/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub mod clear_color;
pub mod core_2d;
pub mod core_3d;
pub mod fullscreen_vertex_shader;
pub mod tonemapping;
pub mod upscaling;

pub mod prelude {
#[doc(hidden)]
Expand All @@ -11,17 +14,31 @@ pub mod prelude {
};
}

use crate::{clear_color::ClearColor, core_2d::Core2dPlugin, core_3d::Core3dPlugin};
use crate::{
clear_color::ClearColor, core_2d::Core2dPlugin, core_3d::Core3dPlugin,
fullscreen_vertex_shader::FULLSCREEN_SHADER_HANDLE, tonemapping::TonemappingPlugin,
upscaling::UpscalingPlugin,
};
use bevy_app::{App, Plugin};
use bevy_render::extract_resource::ExtractResourcePlugin;
use bevy_asset::load_internal_asset;
use bevy_render::{extract_resource::ExtractResourcePlugin, prelude::Shader};

#[derive(Default)]
pub struct CorePipelinePlugin;

impl Plugin for CorePipelinePlugin {
fn build(&self, app: &mut App) {
load_internal_asset!(
app,
FULLSCREEN_SHADER_HANDLE,
"fullscreen_vertex_shader/fullscreen.wgsl",
Shader::from_wgsl
);

app.init_resource::<ClearColor>()
.add_plugin(ExtractResourcePlugin::<ClearColor>::default())
.add_plugin(TonemappingPlugin)
.add_plugin(UpscalingPlugin)
.add_plugin(Core2dPlugin)
.add_plugin(Core3dPlugin);
}
Expand Down
11 changes: 11 additions & 0 deletions crates/bevy_core_pipeline/src/tonemapping/blit.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import bevy_core_pipeline::fullscreen_vertex_shader

[[group(0), binding(0)]]
var texture: texture_2d<f32>;
[[group(0), binding(1)]]
var texture_sampler: sampler;

[[stage(fragment)]]
fn fs_main(in: FullscreenVertexOutput) -> [[location(0)]] vec4<f32> {
return textureSample(texture, texture_sampler, in.uv);
}
Loading