Skip to main content

wde_pbr/post_process/
mod.rs

1//! Post-processing of the HDR [`RenderTexture`](crate::textures::RenderTexture), configured by the [`PostProcessSettings`] resource.
2//!
3//! The effects run in these render passes:
4//! - [`RenderPassFog`] (ID 30): exponential height fog, blended over the opaque geometry and the sky, before the transparent objects.
5//! - [`RenderPassTaa`] (ID 90): temporal anti-aliasing of the HDR render texture, when it is the [`AntiAliasing`].
6//! - [`RenderPassBloom`] (ID 91): downsamples then upsamples the render texture through the bloom mips.
7//! - [`RenderPassExposure`] (ID 92): eye adaptation, from a luminance histogram of the render texture (see [`ExposureSettings`]).
8//! - [`RenderPassComposite`] (ID 95): composites the chromatic aberration, the bloom, the exposure, the color grading, the tonemapping,
9//!   the vignette, the film grain and the dithering into the LDR texture of the [`PostProcessTextures`].
10//! - [`RenderPassSmaa`] (ID 96): edge detection and blending weights of the SMAA, when it is the [`AntiAliasing`].
11//! - [`RenderPassAntiAliasing`] (ID 97): anti-aliases the LDR texture (see [`AntiAliasing`]).
12//! - [`RenderPassPostProcess`] (ID 100): copies the anti-aliased image to the swapchain, sharpening it (see [`SharpeningSettings`]).
13//!
14//! To add a full-screen effect over the final image, add a sub-pass to [`RenderPassPostProcess`]. To add an HDR effect,
15//! add a pass between 50 and 90 that reads and writes the render texture.
16use bevy::prelude::*;
17use wde_renderer::prelude::*;
18
19mod data;
20#[cfg(debug_assertions)]
21mod editor;
22mod pass_bloom;
23mod pass_composite;
24mod pass_exposure;
25mod pass_fog;
26mod settings;
27mod smaa;
28mod taa;
29
30pub use data::{BLOOM_MIP_COUNT, PostProcessTextures};
31pub use pass_bloom::RenderPassBloom;
32pub use pass_composite::{RenderPassAntiAliasing, RenderPassComposite, RenderPassPostProcess};
33pub use pass_exposure::RenderPassExposure;
34pub use pass_fog::RenderPassFog;
35pub use settings::*;
36pub use smaa::{RenderPassSmaa, SmaaTextures};
37pub use taa::{RenderPassTaa, TaaTextures};
38
39use data::*;
40use pass_bloom::*;
41use pass_composite::*;
42use pass_exposure::*;
43use pass_fog::*;
44use smaa::*;
45use taa::*;
46
47pub(crate) struct PostProcessPlugin;
48impl Plugin for PostProcessPlugin {
49    fn build(&self, app: &mut App) {
50        app.init_resource::<PostProcessSettings>()
51            .register_type::<PostProcessSettings>()
52            .add_plugins((
53                RenderDataRegisterPlugin::<PostProcessTextures>::default(),
54                RenderDataRegisterPlugin::<PostProcessBuffers>::default(),
55                RenderBindingRegisterPlugin::<PostProcessParamsBinding>::default(),
56                RenderBindingRegisterPlugin::<PostProcessCompositeBinding>::default(),
57                RenderBindingRegisterPlugin::<AntiAliasingBinding>::default(),
58                RenderBindingRegisterPlugin::<SharpeningBinding>::default(),
59                RenderBindingRegisterPlugin::<FogBinding>::default(),
60                RenderPipelineRegisterPlugin::<FogPipeline>::default(),
61                RenderPipelineRegisterPlugin::<BloomDownsamplePipeline>::default(),
62                RenderPipelineRegisterPlugin::<BloomUpsamplePipeline>::default(),
63                RenderPipelineRegisterPlugin::<ExposureHistogramPipeline>::default(),
64                RenderPipelineRegisterPlugin::<ExposureAveragePipeline>::default(),
65                RenderPipelineRegisterPlugin::<CompositePipeline>::default(),
66                RenderPipelineRegisterPlugin::<AntiAliasingPipeline>::default(),
67                RenderPipelineRegisterPlugin::<SharpeningPipeline>::default()
68            ))
69            .add_plugins((
70                RenderDataRegisterPlugin::<SmaaTextures>::default(),
71                RenderBindingRegisterPlugin::<SmaaEdgesBinding>::default(),
72                RenderBindingRegisterPlugin::<SmaaWeightsBinding>::default(),
73                RenderBindingRegisterPlugin::<SmaaBlendBinding>::default(),
74                RenderPipelineRegisterPlugin::<SmaaEdgesPipeline>::default(),
75                RenderPipelineRegisterPlugin::<SmaaWeightsPipeline>::default(),
76                RenderPipelineRegisterPlugin::<SmaaBlendPipeline>::default(),
77                RenderDataRegisterPlugin::<TaaTextures>::default(),
78                RenderBindingRegisterPlugin::<TaaBinding>::default(),
79                RenderPipelineRegisterPlugin::<TaaPipeline>::default()
80            ))
81            .add_systems(PostUpdate, sync_projection_jitter);
82        #[cfg(debug_assertions)]
83        app.add_plugins(editor::PostProcessEditorPlugin);
84
85        let smaa_luts = SmaaLuts::load(app.world().resource::<AssetServer>());
86        let render_app = app.get_sub_app_mut(RenderApp).unwrap();
87        render_app
88            .insert_resource(smaa_luts)
89            .init_resource::<PostProcessUniform>()
90            .init_resource::<FogUniform>()
91            .init_resource::<BloomBindGroups>()
92            .init_resource::<ExposureBindGroup>()
93            .init_resource::<TaaState>()
94            .add_systems(ExtractSchedule, extract_post_process_params)
95            .add_systems(
96                Render,
97                update_post_process_buffers.in_set(RenderSet::Prepare)
98            );
99        render_app
100            .world_mut()
101            .resource_mut::<RenderGraph>()
102            .add_pass::<RenderPassFog>()
103            .add_sub_pass::<SubRenderPassFog, RenderPassFog>()
104            .add_pass::<RenderPassTaa>()
105            .add_pass::<RenderPassBloom>()
106            .add_pass::<RenderPassExposure>()
107            .add_pass::<RenderPassComposite>()
108            .add_sub_pass::<SubRenderPassComposite, RenderPassComposite>()
109            .add_pass::<RenderPassSmaa>()
110            .add_pass::<RenderPassAntiAliasing>()
111            .add_sub_pass::<SubRenderPassAntiAliasing, RenderPassAntiAliasing>()
112            .add_pass::<RenderPassPostProcess>()
113            .add_sub_pass::<SubRenderPassSharpening, RenderPassPostProcess>();
114    }
115}