-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Add FullscreenPassNode for postprocessing #1988
Closed
Closed
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
344360f
Make WindowTextureNode a wrapper around TextureNode
mtsr f268573
Fix WindowTextureNode
mtsr 8dc4aa4
Fix clippy lint
mtsr aa2bc75
Intermediate commit
mtsr 7a2d3c6
Move to drawing a single triangle
mtsr 633eb0f
Use new WindowTextureNode
mtsr 6116b74
Pass sampler directly into FullscreenPassNode
mtsr f45d230
Start adding separate resolve pass
mtsr dd87782
Resolve and post pass work
mtsr 80f9fa8
Work on some permutations of enabled/disabled passes
mtsr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
use super::{ | ||
CameraNode, PassNode, RenderGraph, SharedBuffersNode, TextureCopyNode, WindowSwapChainNode, | ||
WindowTextureNode, | ||
fullscreen_pass_node, CameraNode, FullscreenPassNode, PassNode, RenderGraph, SharedBuffersNode, | ||
TextureCopyNode, WindowSwapChainNode, WindowTextureNode, | ||
}; | ||
use crate::{ | ||
pass::{ | ||
LoadOp, Operations, PassDescriptor, RenderPassColorAttachmentDescriptor, | ||
RenderPassDepthStencilAttachmentDescriptor, TextureAttachment, | ||
}, | ||
texture::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage}, | ||
pipeline::{ | ||
BlendFactor, BlendOperation, BlendState, ColorTargetState, ColorWrite, PipelineDescriptor, | ||
}, | ||
shader::{Shader, ShaderStage, ShaderStages}, | ||
texture::{ | ||
Extent3d, SamplerDescriptor, TextureDescriptor, TextureDimension, TextureFormat, | ||
TextureUsage, | ||
}, | ||
Color, | ||
}; | ||
use bevy_asset::Assets; | ||
use bevy_ecs::{reflect::ReflectComponent, world::World}; | ||
use bevy_reflect::Reflect; | ||
use bevy_window::WindowId; | ||
|
@@ -61,6 +69,7 @@ pub struct BaseRenderGraphConfig { | |
pub add_main_pass: bool, | ||
pub connect_main_pass_to_swapchain: bool, | ||
pub connect_main_pass_to_main_depth_texture: bool, | ||
pub add_post_pass: bool, | ||
} | ||
|
||
pub mod node { | ||
|
@@ -69,16 +78,28 @@ pub mod node { | |
pub const CAMERA_2D: &str = "camera_2d"; | ||
pub const TEXTURE_COPY: &str = "texture_copy"; | ||
pub const MAIN_DEPTH_TEXTURE: &str = "main_pass_depth_texture"; | ||
pub const MAIN_RENDER_TEXTURE: &str = "main_pass_render_texture"; | ||
pub const MAIN_SAMPLED_COLOR_ATTACHMENT: &str = "main_pass_sampled_color_attachment"; | ||
pub const MAIN_PASS: &str = "main_pass"; | ||
pub const SHARED_BUFFERS: &str = "shared_buffers"; | ||
pub const POST_PASS: &str = "post_pass"; | ||
} | ||
|
||
pub mod camera { | ||
pub const CAMERA_3D: &str = "Camera3d"; | ||
pub const CAMERA_2D: &str = "Camera2d"; | ||
} | ||
|
||
pub mod texture { | ||
use crate::Texture; | ||
use bevy_asset::HandleUntyped; | ||
use bevy_reflect::TypeUuid; | ||
|
||
pub const MAIN_RENDER_TEXTURE_HANDLE: HandleUntyped = | ||
HandleUntyped::weak_from_u64(Texture::TYPE_UUID, 13378939762009864029); | ||
pub const MAIN_DEPTH_TEXTURE_HANDLE: HandleUntyped = | ||
HandleUntyped::weak_from_u64(Texture::TYPE_UUID, 13378939762009864027); | ||
} | ||
|
||
impl Default for BaseRenderGraphConfig { | ||
fn default() -> Self { | ||
BaseRenderGraphConfig { | ||
|
@@ -88,6 +109,7 @@ impl Default for BaseRenderGraphConfig { | |
add_main_depth_texture: true, | ||
connect_main_pass_to_swapchain: true, | ||
connect_main_pass_to_main_depth_texture: true, | ||
add_post_pass: true, | ||
} | ||
} | ||
} | ||
|
@@ -110,7 +132,8 @@ pub(crate) fn add_base_graph(config: &BaseRenderGraphConfig, world: &mut World) | |
} | ||
|
||
graph.add_node(node::SHARED_BUFFERS, SharedBuffersNode::default()); | ||
if config.add_main_depth_texture { | ||
|
||
if config.add_main_depth_texture && !config.add_post_pass { | ||
graph.add_node( | ||
node::MAIN_DEPTH_TEXTURE, | ||
WindowTextureNode::new( | ||
|
@@ -128,20 +151,35 @@ pub(crate) fn add_base_graph(config: &BaseRenderGraphConfig, world: &mut World) | |
* bit depth for better performance */ | ||
usage: TextureUsage::OUTPUT_ATTACHMENT, | ||
}, | ||
None, | ||
None, | ||
), | ||
); | ||
} | ||
|
||
if config.add_main_pass { | ||
let mut main_pass_node = PassNode::<&MainPass>::new(PassDescriptor { | ||
color_attachments: vec![msaa.color_attachment_descriptor( | ||
let color_attachments = if config.add_post_pass { | ||
vec![RenderPassColorAttachmentDescriptor { | ||
attachment: TextureAttachment::Input("color_attachment".to_string()), | ||
resolve_target: None, | ||
ops: Operations { | ||
load: LoadOp::Clear(Color::rgb(0.1, 0.2, 0.3)), | ||
store: true, | ||
}, | ||
}] | ||
} else { | ||
vec![msaa.color_attachment_descriptor( | ||
TextureAttachment::Input("color_attachment".to_string()), | ||
TextureAttachment::Input("color_resolve_target".to_string()), | ||
Operations { | ||
load: LoadOp::Clear(Color::rgb(0.1, 0.1, 0.1)), | ||
store: true, | ||
}, | ||
)], | ||
)] | ||
}; | ||
|
||
let mut main_pass_node = PassNode::<&MainPass>::new(PassDescriptor { | ||
color_attachments, | ||
depth_stencil_attachment: Some(RenderPassDepthStencilAttachmentDescriptor { | ||
attachment: TextureAttachment::Input("depth".to_string()), | ||
depth_ops: Some(Operations { | ||
|
@@ -185,12 +223,145 @@ pub(crate) fn add_base_graph(config: &BaseRenderGraphConfig, world: &mut World) | |
} | ||
} | ||
|
||
if config.add_post_pass { | ||
let main_render_texture_node = WindowTextureNode::new( | ||
WindowId::primary(), | ||
TextureDescriptor { | ||
size: Extent3d::new(1, 1, 1), | ||
mip_level_count: 1, | ||
sample_count: msaa.samples, | ||
dimension: TextureDimension::D2, | ||
format: TextureFormat::Bgra8Unorm, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when a user wants a different buffer format? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair question, that's one for the how do we compose a rendergraph discussion. |
||
usage: TextureUsage::OUTPUT_ATTACHMENT | TextureUsage::SAMPLED, | ||
}, | ||
Some(SamplerDescriptor::default()), | ||
Some(texture::MAIN_RENDER_TEXTURE_HANDLE), | ||
); | ||
|
||
graph.add_node(node::MAIN_RENDER_TEXTURE, main_render_texture_node); | ||
|
||
let main_depth_texture_node = WindowTextureNode::new( | ||
WindowId::primary(), | ||
TextureDescriptor { | ||
size: Extent3d::new(1, 1, 1), | ||
mip_level_count: 1, | ||
sample_count: msaa.samples, | ||
dimension: TextureDimension::D2, | ||
format: TextureFormat::Depth32Float, | ||
usage: TextureUsage::OUTPUT_ATTACHMENT | TextureUsage::SAMPLED, | ||
}, | ||
Some(SamplerDescriptor::default()), | ||
Some(texture::MAIN_DEPTH_TEXTURE_HANDLE), | ||
); | ||
|
||
graph.add_node(node::MAIN_DEPTH_TEXTURE, main_depth_texture_node); | ||
|
||
let mut shaders = world.get_resource_mut::<Assets<Shader>>().unwrap(); | ||
let mut pipelines = world | ||
.get_resource_mut::<Assets<PipelineDescriptor>>() | ||
.unwrap(); | ||
|
||
let pipeline_descriptor = PipelineDescriptor { | ||
depth_stencil: None, | ||
color_target_states: vec![ColorTargetState { | ||
format: TextureFormat::Bgra8UnormSrgb, | ||
color_blend: BlendState { | ||
src_factor: BlendFactor::SrcAlpha, | ||
dst_factor: BlendFactor::OneMinusSrcAlpha, | ||
operation: BlendOperation::Add, | ||
}, | ||
alpha_blend: BlendState { | ||
src_factor: BlendFactor::One, | ||
dst_factor: BlendFactor::One, | ||
operation: BlendOperation::Add, | ||
}, | ||
write_mask: ColorWrite::ALL, | ||
}], | ||
..PipelineDescriptor::new(ShaderStages { | ||
vertex: shaders.add(Shader::from_glsl( | ||
ShaderStage::Vertex, | ||
fullscreen_pass_node::shaders::VERTEX_SHADER, | ||
)), | ||
fragment: Some(shaders.add(Shader::from_glsl( | ||
ShaderStage::Fragment, | ||
fullscreen_pass_node::shaders::REINHARD_FRAGMENT_SHADER, | ||
))), | ||
}) | ||
}; | ||
|
||
let pipeline_handle = pipelines.add(pipeline_descriptor); | ||
|
||
let pass_descriptor = PassDescriptor { | ||
color_attachments: vec![msaa.color_attachment_descriptor( | ||
TextureAttachment::Input("color_attachment".to_string()), | ||
TextureAttachment::Input("color_resolve_target".to_string()), | ||
Operations { | ||
load: LoadOp::Load, | ||
store: true, | ||
}, | ||
)], | ||
depth_stencil_attachment: None, | ||
sample_count: msaa.samples, | ||
}; | ||
|
||
let post_pass_node = FullscreenPassNode::new( | ||
pass_descriptor, | ||
pipeline_handle, | ||
vec!["color_texture".into()], | ||
); | ||
|
||
graph.add_node(node::POST_PASS, post_pass_node); | ||
|
||
graph | ||
.add_node_edge(node::MAIN_PASS, node::POST_PASS) | ||
.unwrap(); | ||
|
||
graph | ||
.add_slot_edge( | ||
node::MAIN_RENDER_TEXTURE, | ||
WindowTextureNode::OUT_TEXTURE, | ||
node::POST_PASS, | ||
"color_texture", | ||
) | ||
.unwrap(); | ||
graph | ||
.add_slot_edge( | ||
node::MAIN_RENDER_TEXTURE, | ||
WindowTextureNode::OUT_SAMPLER, | ||
node::POST_PASS, | ||
"color_texture_sampler", | ||
) | ||
.unwrap(); | ||
} | ||
|
||
graph.add_node( | ||
node::PRIMARY_SWAP_CHAIN, | ||
WindowSwapChainNode::new(WindowId::primary()), | ||
); | ||
|
||
if config.connect_main_pass_to_swapchain { | ||
if config.add_post_pass { | ||
graph | ||
.add_slot_edge( | ||
node::MAIN_RENDER_TEXTURE, | ||
WindowTextureNode::OUT_TEXTURE, | ||
node::MAIN_PASS, | ||
"color_attachment", | ||
) | ||
.unwrap(); | ||
|
||
graph | ||
.add_slot_edge( | ||
node::PRIMARY_SWAP_CHAIN, | ||
WindowSwapChainNode::OUT_TEXTURE, | ||
node::POST_PASS, | ||
if msaa.samples > 1 { | ||
"color_resolve_target" | ||
} else { | ||
"color_attachment" | ||
}, | ||
) | ||
.unwrap(); | ||
} else if config.connect_main_pass_to_swapchain { | ||
graph | ||
.add_slot_edge( | ||
node::PRIMARY_SWAP_CHAIN, | ||
|
@@ -213,29 +384,42 @@ pub(crate) fn add_base_graph(config: &BaseRenderGraphConfig, world: &mut World) | |
TextureDescriptor { | ||
size: Extent3d { | ||
depth: 1, | ||
width: 1, | ||
height: 1, | ||
width: 2560, | ||
height: 1440, | ||
mtsr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
mip_level_count: 1, | ||
sample_count: msaa.samples, | ||
dimension: TextureDimension::D2, | ||
format: TextureFormat::default(), | ||
usage: TextureUsage::OUTPUT_ATTACHMENT, | ||
format: TextureFormat::Bgra8UnormSrgb, | ||
usage: TextureUsage::OUTPUT_ATTACHMENT | TextureUsage::SAMPLED, | ||
}, | ||
Some(SamplerDescriptor::default()), | ||
Some(texture::MAIN_RENDER_TEXTURE_HANDLE), | ||
), | ||
); | ||
|
||
graph | ||
.add_slot_edge( | ||
node::MAIN_SAMPLED_COLOR_ATTACHMENT, | ||
WindowSwapChainNode::OUT_TEXTURE, | ||
node::MAIN_PASS, | ||
"color_attachment", | ||
) | ||
.unwrap(); | ||
if config.add_post_pass { | ||
graph | ||
.add_slot_edge( | ||
node::MAIN_SAMPLED_COLOR_ATTACHMENT, | ||
WindowTextureNode::OUT_TEXTURE, | ||
node::POST_PASS, | ||
"color_attachment", | ||
) | ||
.unwrap(); | ||
} else if config.connect_main_pass_to_swapchain { | ||
graph | ||
.add_slot_edge( | ||
node::MAIN_SAMPLED_COLOR_ATTACHMENT, | ||
WindowSwapChainNode::OUT_TEXTURE, | ||
node::MAIN_PASS, | ||
"color_attachment", | ||
) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
if config.connect_main_pass_to_main_depth_texture { | ||
if config.add_post_pass || config.connect_main_pass_to_main_depth_texture { | ||
graph | ||
.add_slot_edge( | ||
node::MAIN_DEPTH_TEXTURE, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't there a resource for clear color we could use here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Good point.