1use bevy::{
2 ecs::system::{
3 SystemParamItem,
4 lifetimeless::{SRes, SResMut}
5 },
6 prelude::*
7};
8use wde_renderer::{prelude::*, wgpu_utils::BindGroup};
9
10use super::data::{
11 AntiAliasingBinding, PostProcessCompositeBinding, PostProcessTextures, SharpeningBinding
12};
13use super::settings::{AntiAliasing, PostProcessUniform};
14use super::smaa::{SmaaBlendBinding, SmaaBlendPipeline};
15
16pub struct RenderPassComposite;
21impl RenderPass for RenderPassComposite {
22 type Params = SRenderData<PostProcessTextures>;
23
24 fn describe(textures: &SystemParamItem<Self::Params>) -> RenderPassDesc {
25 RenderPassDesc {
26 attachments_colors: Some(vec![RenderPassDescColorAttachment {
27 texture: textures
28 .iter()
29 .next()
30 .and_then(|(_, t)| t.get_texture(PostProcessTextures::LDR_IDX))
31 .map(|t| t.id()),
32 ..Default::default()
33 }]),
34 ..Default::default()
35 }
36 }
37 fn id() -> RenderPassId {
38 95
39 }
40 fn label() -> &'static str {
41 "post-process-composite"
42 }
43}
44
45pub struct RenderPassAntiAliasing;
49impl RenderPass for RenderPassAntiAliasing {
50 type Params = SRenderData<PostProcessTextures>;
51
52 fn describe(textures: &SystemParamItem<Self::Params>) -> RenderPassDesc {
53 RenderPassDesc {
54 attachments_colors: Some(vec![RenderPassDescColorAttachment {
55 texture: textures
56 .iter()
57 .next()
58 .and_then(|(_, t)| t.get_texture(PostProcessTextures::ANTI_ALIASED_IDX))
59 .map(|t| t.id()),
60 ..Default::default()
61 }]),
62 ..Default::default()
63 }
64 }
65 fn id() -> RenderPassId {
66 97
67 }
68 fn label() -> &'static str {
69 "post-process-anti-aliasing"
70 }
71}
72
73pub struct RenderPassPostProcess;
80impl RenderPass for RenderPassPostProcess {
81 type Params = ();
82
83 fn describe(_: &SystemParamItem<Self::Params>) -> RenderPassDesc {
84 RenderPassDesc::default()
85 }
86 fn id() -> RenderPassId {
87 100
88 }
89 fn label() -> &'static str {
90 "post-process"
91 }
92}
93
94#[derive(TypePath, Default, Clone, Debug)]
95pub(crate) struct CompositePipeline(pub CachedPipelineIndex);
96impl RenderAsset for CompositePipeline {
97 type SourceAsset = RenderPipelineAsset<CompositePipeline>;
98 type Params = (
99 SRes<AssetServer>,
100 SResMut<PipelineManager>,
101 SBinding<PostProcessCompositeBinding>
102 );
103
104 fn prepare(
105 _id: AssetId<Self::SourceAsset>,
106 asset: Self::SourceAsset,
107 (assets_server, pipeline_manager, binding): &mut SystemParamItem<Self::Params>
108 ) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
109 Ok(CompositePipeline(pipeline_manager.create_render_pipeline(
110 RenderPipelineDescriptor {
111 label: "post-process-composite",
112 vert: Some(assets_server.load("core/render/post_process/fullscreen.vert.wgsl")),
113 frag: Some(assets_server.load("core/render/post_process/composite.frag.wgsl")),
114 bind_group_layouts: vec![binding.iter().next().map(|(_, b)| b.layout.clone())],
115 depth: DepthDescriptor {
116 enabled: false,
117 ..default()
118 },
119 render_targets: Some(vec![PostProcessTextures::LDR_FORMAT]),
120 ..default()
121 },
122 asset
123 )?))
124 }
125}
126
127#[derive(TypePath, Default, Clone, Debug)]
128pub(crate) struct AntiAliasingPipeline(pub CachedPipelineIndex);
129impl RenderAsset for AntiAliasingPipeline {
130 type SourceAsset = RenderPipelineAsset<AntiAliasingPipeline>;
131 type Params = (
132 SRes<AssetServer>,
133 SResMut<PipelineManager>,
134 SBinding<AntiAliasingBinding>
135 );
136
137 fn prepare(
138 _id: AssetId<Self::SourceAsset>,
139 asset: Self::SourceAsset,
140 (assets_server, pipeline_manager, binding): &mut SystemParamItem<Self::Params>
141 ) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
142 Ok(AntiAliasingPipeline(
143 pipeline_manager.create_render_pipeline(
144 RenderPipelineDescriptor {
145 label: "post-process-anti-aliasing",
146 vert: Some(assets_server.load("core/render/post_process/fullscreen.vert.wgsl")),
147 frag: Some(assets_server.load("core/render/post_process/fxaa.frag.wgsl")),
148 bind_group_layouts: vec![binding.iter().next().map(|(_, b)| b.layout.clone())],
149 depth: DepthDescriptor {
150 enabled: false,
151 ..default()
152 },
153 render_targets: Some(vec![PostProcessTextures::LDR_FORMAT]),
154 ..default()
155 },
156 asset
157 )?
158 ))
159 }
160}
161
162#[derive(TypePath, Default, Clone, Debug)]
163pub(crate) struct SharpeningPipeline(pub CachedPipelineIndex);
164impl RenderAsset for SharpeningPipeline {
165 type SourceAsset = RenderPipelineAsset<SharpeningPipeline>;
166 type Params = (
167 SRes<AssetServer>,
168 SResMut<PipelineManager>,
169 SBinding<SharpeningBinding>
170 );
171
172 fn prepare(
173 _id: AssetId<Self::SourceAsset>,
174 asset: Self::SourceAsset,
175 (assets_server, pipeline_manager, binding): &mut SystemParamItem<Self::Params>
176 ) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
177 Ok(SharpeningPipeline(
178 pipeline_manager.create_render_pipeline(
179 RenderPipelineDescriptor {
180 label: "post-process-sharpening",
181 vert: Some(assets_server.load("core/render/post_process/fullscreen.vert.wgsl")),
182 frag: Some(assets_server.load("core/render/post_process/cas.frag.wgsl")),
183 bind_group_layouts: vec![binding.iter().next().map(|(_, b)| b.layout.clone())],
184 depth: DepthDescriptor {
185 enabled: false,
186 ..default()
187 },
188 ..default()
189 },
190 asset
191 )?
192 ))
193 }
194}
195
196fn fullscreen_draw(
197 pipeline: Option<CachedPipelineIndex>,
198 mesh: &PostProcessingMesh,
199 bind_group: Option<BindGroup>
200) -> RenderSubPassDesc {
201 RenderSubPassDesc(vec![
202 SubPassCommand::Pipeline(pipeline),
203 SubPassCommand::Mesh(mesh.0.as_ref().map(|h| h.id())),
204 SubPassCommand::BindGroup(0, bind_group),
205 SubPassCommand::DrawBatches(vec![DrawCommandsBatch {
206 index_range: 0..6,
207 ..Default::default()
208 }]),
209 ])
210}
211
212pub(crate) struct SubRenderPassComposite;
213impl RenderSubPass for SubRenderPassComposite {
214 type Params = (
215 SRes<RenderAssets<CompositePipeline>>,
216 SRes<PostProcessingMesh>,
217 SBinding<PostProcessCompositeBinding>
218 );
219
220 fn describe((pipeline, mesh, binding): &SystemParamItem<Self::Params>) -> RenderSubPassDesc {
221 fullscreen_draw(
222 pipeline.iter().next().map(|(_, p)| p.0),
223 mesh,
224 binding.iter().next().map(|(_, b)| b.bind_group.clone())
225 )
226 }
227
228 fn label() -> &'static str {
229 "post-process-composite"
230 }
231}
232
233pub(crate) struct SubRenderPassAntiAliasing;
234impl RenderSubPass for SubRenderPassAntiAliasing {
235 type Params = (
236 SRes<PostProcessUniform>,
237 SRes<PostProcessingMesh>,
238 SRes<RenderAssets<AntiAliasingPipeline>>,
239 SBinding<AntiAliasingBinding>,
240 SRes<RenderAssets<SmaaBlendPipeline>>,
241 SBinding<SmaaBlendBinding>
242 );
243
244 fn describe(
245 (params, mesh, pipeline, binding, smaa_pipeline, smaa_binding): &SystemParamItem<
246 Self::Params
247 >
248 ) -> RenderSubPassDesc {
249 if params.anti_aliasing() == AntiAliasing::Smaa
251 && let (Some((_, p)), Some((_, b))) =
252 (smaa_pipeline.iter().next(), smaa_binding.iter().next())
253 {
254 return fullscreen_draw(Some(p.0), mesh, Some(b.bind_group.clone()));
255 }
256 fullscreen_draw(
257 pipeline.iter().next().map(|(_, p)| p.0),
258 mesh,
259 binding.iter().next().map(|(_, b)| b.bind_group.clone())
260 )
261 }
262
263 fn label() -> &'static str {
264 "post-process-anti-aliasing"
265 }
266}
267
268pub(crate) struct SubRenderPassSharpening;
269impl RenderSubPass for SubRenderPassSharpening {
270 type Params = (
271 SRes<RenderAssets<SharpeningPipeline>>,
272 SRes<PostProcessingMesh>,
273 SBinding<SharpeningBinding>
274 );
275
276 fn describe((pipeline, mesh, binding): &SystemParamItem<Self::Params>) -> RenderSubPassDesc {
277 fullscreen_draw(
278 pipeline.iter().next().map(|(_, p)| p.0),
279 mesh,
280 binding.iter().next().map(|(_, b)| b.bind_group.clone())
281 )
282 }
283
284 fn label() -> &'static str {
285 "post-process-sharpening"
286 }
287}