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 7 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
2 changes: 2 additions & 0 deletions crates/bevy_core_pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ bevy_core = { path = "../bevy_core", version = "0.7.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.7.0-dev" }
bevy_render = { path = "../bevy_render", version = "0.7.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.7.0-dev" }
bevy_reflect = { path = "../bevy_reflect", version = "0.7.0-dev" }

bitflags = "1.2"
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(),
}
}
130 changes: 129 additions & 1 deletion crates/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod clear_pass;
mod clear_pass_driver;
pub mod fullscreen_vertex_shader;
mod main_pass_2d;
mod main_pass_3d;
mod main_pass_driver;
mod tonemapping;
mod upscaling;

pub mod prelude {
#[doc(hidden)]
Expand All @@ -16,10 +19,12 @@ pub use clear_pass_driver::*;
pub use main_pass_2d::*;
pub use main_pass_3d::*;
pub use main_pass_driver::*;
pub use upscaling::*;

use std::ops::Range;

use bevy_app::{App, Plugin};
use bevy_asset::load_internal_asset;
use bevy_core::FloatOrd;
use bevy_ecs::prelude::*;
use bevy_render::{
Expand All @@ -36,6 +41,11 @@ use bevy_render::{
view::{ExtractedView, Msaa, ViewDepthTexture},
RenderApp, RenderStage, RenderWorld,
};
use tonemapping::TonemappingNode;
use tonemapping::TonemappingPlugin;
use upscaling::UpscalingNode;

use crate::fullscreen_vertex_shader::FULLSCREEN_SHADER_HANDLE;

/// When used as a resource, sets the color that is used to clear the screen between frames.
///
Expand Down Expand Up @@ -82,6 +92,8 @@ pub mod draw_2d_graph {
}
pub mod node {
pub const MAIN_PASS: &str = "main_pass";
pub const TONEMAPPING: &str = "tonemapping";
pub const UPSCALING: &str = "upscaling";
}
}

Expand All @@ -92,6 +104,8 @@ pub mod draw_3d_graph {
}
pub mod node {
pub const MAIN_PASS: &str = "main_pass";
pub const TONEMAPPING: &str = "tonemapping";
pub const UPSCALING: &str = "upscaling";
}
}

Expand All @@ -112,8 +126,17 @@ pub enum CorePipelineRenderSystems {

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>()
.init_resource::<RenderTargetClearColors>();
.init_resource::<RenderTargetClearColors>()
.add_plugin(TonemappingPlugin)
.add_plugin(UpscalingPlugin);

let render_app = match app.get_sub_app_mut(RenderApp) {
Ok(render_app) => render_app,
Expand Down Expand Up @@ -144,10 +167,17 @@ impl Plugin for CorePipelinePlugin {

let clear_pass_node = ClearPassNode::new(&mut render_app.world);
let pass_node_2d = MainPass2dNode::new(&mut render_app.world);
let tonemapping_2d = TonemappingNode::new(&mut render_app.world);
let upscaling_2d = UpscalingNode::new(&mut render_app.world);

let pass_node_3d = MainPass3dNode::new(&mut render_app.world);
let tonemapping_3d = TonemappingNode::new(&mut render_app.world);
let upscaling_3d = 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(draw_2d_graph::node::TONEMAPPING, tonemapping_2d);
draw_2d_graph.add_node(draw_2d_graph::node::UPSCALING, upscaling_2d);
draw_2d_graph.add_node(draw_2d_graph::node::MAIN_PASS, pass_node_2d);
let input_node_id = draw_2d_graph.set_input(vec![SlotInfo::new(
draw_2d_graph::input::VIEW_ENTITY,
Expand All @@ -161,14 +191,65 @@ impl Plugin for CorePipelinePlugin {
MainPass2dNode::IN_VIEW,
)
.unwrap();

draw_2d_graph
.add_node_edge(
draw_2d_graph::node::MAIN_PASS,
draw_2d_graph::node::TONEMAPPING,
)
.unwrap();
draw_2d_graph
.add_slot_edge(
input_node_id,
draw_2d_graph::input::VIEW_ENTITY,
draw_2d_graph::node::TONEMAPPING,
TonemappingNode::IN_VIEW,
)
.unwrap();
draw_2d_graph
.add_slot_edge(
draw_2d_graph::node::MAIN_PASS,
MainPass2dNode::OUT_TEXTURE,
draw_2d_graph::node::TONEMAPPING,
TonemappingNode::IN_TEXTURE,
)
.unwrap();

draw_2d_graph
.add_node_edge(
draw_2d_graph::node::TONEMAPPING,
draw_2d_graph::node::UPSCALING,
)
.unwrap();
draw_2d_graph
.add_slot_edge(
input_node_id,
draw_2d_graph::input::VIEW_ENTITY,
draw_2d_graph::node::UPSCALING,
UpscalingNode::IN_VIEW,
)
.unwrap();
draw_2d_graph
.add_slot_edge(
draw_2d_graph::node::TONEMAPPING,
TonemappingNode::OUT_TEXTURE,
draw_2d_graph::node::UPSCALING,
UpscalingNode::IN_TEXTURE,
)
.unwrap();

graph.add_sub_graph(draw_2d_graph::NAME, draw_2d_graph);

let mut draw_3d_graph = RenderGraph::default();
draw_3d_graph.add_node(draw_3d_graph::node::MAIN_PASS, pass_node_3d);
draw_3d_graph.add_node(draw_3d_graph::node::TONEMAPPING, tonemapping_3d);
draw_3d_graph.add_node(draw_3d_graph::node::UPSCALING, upscaling_3d);

let input_node_id = draw_3d_graph.set_input(vec![SlotInfo::new(
draw_3d_graph::input::VIEW_ENTITY,
SlotType::Entity,
)]);

draw_3d_graph
.add_slot_edge(
input_node_id,
Expand All @@ -177,6 +258,53 @@ impl Plugin for CorePipelinePlugin {
MainPass3dNode::IN_VIEW,
)
.unwrap();

draw_3d_graph
.add_node_edge(
draw_3d_graph::node::MAIN_PASS,
draw_3d_graph::node::TONEMAPPING,
)
.unwrap();
draw_3d_graph
.add_slot_edge(
input_node_id,
draw_3d_graph::input::VIEW_ENTITY,
draw_3d_graph::node::TONEMAPPING,
TonemappingNode::IN_VIEW,
)
.unwrap();
draw_3d_graph
.add_slot_edge(
draw_3d_graph::node::MAIN_PASS,
MainPass3dNode::OUT_TEXTURE,
draw_3d_graph::node::TONEMAPPING,
TonemappingNode::IN_TEXTURE,
)
.unwrap();

draw_3d_graph
.add_node_edge(
draw_3d_graph::node::TONEMAPPING,
draw_3d_graph::node::UPSCALING,
)
.unwrap();
draw_3d_graph
.add_slot_edge(
input_node_id,
draw_3d_graph::input::VIEW_ENTITY,
draw_3d_graph::node::UPSCALING,
UpscalingNode::IN_VIEW,
)
.unwrap();
draw_3d_graph
.add_slot_edge(
draw_3d_graph::node::TONEMAPPING,
TonemappingNode::OUT_TEXTURE,
draw_3d_graph::node::UPSCALING,
UpscalingNode::IN_TEXTURE,
)
.unwrap();

graph.add_sub_graph(draw_3d_graph::NAME, draw_3d_graph);

let mut clear_graph = RenderGraph::default();
Expand Down
16 changes: 16 additions & 0 deletions crates/bevy_core_pipeline/src/main_pass_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct MainPass2dNode {

impl MainPass2dNode {
pub const IN_VIEW: &'static str = "view";
pub const OUT_TEXTURE: &'static str = "output";

pub fn new(world: &mut World) -> Self {
Self {
Expand All @@ -28,6 +29,13 @@ impl Node for MainPass2dNode {
vec![SlotInfo::new(MainPass2dNode::IN_VIEW, SlotType::Entity)]
}

fn output(&self) -> Vec<SlotInfo> {
vec![SlotInfo::new(
MainPass2dNode::OUT_TEXTURE,
SlotType::TextureView,
)]
}

fn update(&mut self, world: &mut World) {
self.query.update_archetypes(world);
}
Expand Down Expand Up @@ -65,6 +73,14 @@ impl Node for MainPass2dNode {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}

graph
.set_output(
MainPass2dNode::OUT_TEXTURE,
target.main_texture.texture().clone(),
)
.unwrap();

Ok(())
}
}
15 changes: 15 additions & 0 deletions crates/bevy_core_pipeline/src/main_pass_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct MainPass3dNode {

impl MainPass3dNode {
pub const IN_VIEW: &'static str = "view";
pub const OUT_TEXTURE: &'static str = "output";

pub fn new(world: &mut World) -> Self {
Self {
Expand All @@ -38,6 +39,13 @@ impl Node for MainPass3dNode {
vec![SlotInfo::new(MainPass3dNode::IN_VIEW, SlotType::Entity)]
}

fn output(&self) -> Vec<SlotInfo> {
vec![SlotInfo::new(
MainPass3dNode::OUT_TEXTURE,
SlotType::TextureView,
)]
}

fn update(&mut self, world: &mut World) {
self.query.update_archetypes(world);
}
Expand Down Expand Up @@ -166,6 +174,13 @@ impl Node for MainPass3dNode {
}
}

graph
.set_output(
MainPass3dNode::OUT_TEXTURE,
target.main_texture.texture().clone(),
)
.unwrap();

Ok(())
}
}
Loading