1use wgpu::util::DeviceExt;
2
3use super::gpu::GpuContext;
4
5const MAX_EFFECT_PARAMS: usize = 4;
7const PARAM_FLOATS: usize = 20;
9
10#[derive(Clone, Copy, Debug)]
11pub enum EffectType {
12 Bloom,
13 Blur,
14 Vignette,
15 Crt,
16}
17
18impl EffectType {
19 pub fn from_str(s: &str) -> Option<Self> {
20 match s {
21 "bloom" => Some(EffectType::Bloom),
22 "blur" => Some(EffectType::Blur),
23 "vignette" => Some(EffectType::Vignette),
24 "crt" => Some(EffectType::Crt),
25 _ => None,
26 }
27 }
28
29 fn fragment_source(&self) -> &'static str {
30 match self {
31 EffectType::Bloom => BLOOM_FRAGMENT,
32 EffectType::Blur => BLUR_FRAGMENT,
33 EffectType::Vignette => VIGNETTE_FRAGMENT,
34 EffectType::Crt => CRT_FRAGMENT,
35 }
36 }
37
38 fn defaults(&self) -> [f32; PARAM_FLOATS] {
40 let mut d = [0.0f32; PARAM_FLOATS];
41 match self {
42 EffectType::Bloom => {
43 d[4] = 0.7;
45 d[5] = 0.5;
46 d[6] = 3.0;
47 }
48 EffectType::Blur => {
49 d[4] = 1.0;
51 }
52 EffectType::Vignette => {
53 d[4] = 0.5;
55 d[5] = 0.8;
56 }
57 EffectType::Crt => {
58 d[4] = 800.0;
60 d[5] = 0.1;
61 d[6] = 1.1;
62 }
63 }
64 d
65 }
66}
67
68struct EffectEntry {
69 #[allow(dead_code)]
70 effect_type: EffectType,
71 pipeline: wgpu::RenderPipeline,
72 param_buffer: wgpu::Buffer,
73 param_bind_group: wgpu::BindGroup,
74 param_data: [f32; PARAM_FLOATS],
75}
76
77struct OffscreenTarget {
78 #[allow(dead_code)]
79 texture: wgpu::Texture,
80 view: wgpu::TextureView,
81 bind_group: wgpu::BindGroup,
82 width: u32,
83 height: u32,
84}
85
86pub struct PostProcessPipeline {
89 effects: Vec<(u32, EffectEntry)>,
91 target_a: Option<OffscreenTarget>,
93 target_b: Option<OffscreenTarget>,
94 texture_bind_group_layout: wgpu::BindGroupLayout,
96 params_bind_group_layout: wgpu::BindGroupLayout,
97 pipeline_layout: wgpu::PipelineLayout,
98 sampler: wgpu::Sampler,
99 surface_format: wgpu::TextureFormat,
100}
101
102impl PostProcessPipeline {
103 pub fn new_headless(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
105 Self::new_internal(device, format)
106 }
107
108 pub fn new(gpu: &GpuContext) -> Self {
109 Self::new_internal(&gpu.device, gpu.config.format)
110 }
111
112 fn new_internal(device: &wgpu::Device, surface_format: wgpu::TextureFormat) -> Self {
113 let texture_bind_group_layout =
115 device
116 .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
117 label: Some("postprocess_texture_layout"),
118 entries: &[
119 wgpu::BindGroupLayoutEntry {
120 binding: 0,
121 visibility: wgpu::ShaderStages::FRAGMENT,
122 ty: wgpu::BindingType::Texture {
123 multisampled: false,
124 view_dimension: wgpu::TextureViewDimension::D2,
125 sample_type: wgpu::TextureSampleType::Float {
126 filterable: true,
127 },
128 },
129 count: None,
130 },
131 wgpu::BindGroupLayoutEntry {
132 binding: 1,
133 visibility: wgpu::ShaderStages::FRAGMENT,
134 ty: wgpu::BindingType::Sampler(
135 wgpu::SamplerBindingType::Filtering,
136 ),
137 count: None,
138 },
139 ],
140 });
141
142 let params_bind_group_layout =
144 device
145 .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
146 label: Some("postprocess_params_layout"),
147 entries: &[wgpu::BindGroupLayoutEntry {
148 binding: 0,
149 visibility: wgpu::ShaderStages::FRAGMENT,
150 ty: wgpu::BindingType::Buffer {
151 ty: wgpu::BufferBindingType::Uniform,
152 has_dynamic_offset: false,
153 min_binding_size: None,
154 },
155 count: None,
156 }],
157 });
158
159 let pipeline_layout =
160 device
161 .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
162 label: Some("postprocess_pipeline_layout"),
163 bind_group_layouts: &[
164 &texture_bind_group_layout,
165 ¶ms_bind_group_layout,
166 ],
167 push_constant_ranges: &[],
168 });
169
170 let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
171 label: Some("postprocess_sampler"),
172 address_mode_u: wgpu::AddressMode::ClampToEdge,
173 address_mode_v: wgpu::AddressMode::ClampToEdge,
174 mag_filter: wgpu::FilterMode::Linear,
175 min_filter: wgpu::FilterMode::Linear,
176 ..Default::default()
177 });
178
179 Self {
180 effects: Vec::new(),
181 target_a: None,
182 target_b: None,
183 texture_bind_group_layout,
184 params_bind_group_layout,
185 pipeline_layout,
186 sampler,
187 surface_format,
188 }
189 }
190
191 pub fn has_effects(&self) -> bool {
193 !self.effects.is_empty()
194 }
195
196 pub fn add(&mut self, device: &wgpu::Device, id: u32, effect_type: EffectType) {
198 let wgsl = build_effect_wgsl(effect_type.fragment_source());
199
200 let shader_module =
201 device
202 .create_shader_module(wgpu::ShaderModuleDescriptor {
203 label: Some("postprocess_shader"),
204 source: wgpu::ShaderSource::Wgsl(wgsl.into()),
205 });
206
207 let pipeline =
208 device
209 .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
210 label: Some("postprocess_pipeline"),
211 layout: Some(&self.pipeline_layout),
212 vertex: wgpu::VertexState {
213 module: &shader_module,
214 entry_point: Some("vs_main"),
215 buffers: &[], compilation_options: Default::default(),
217 },
218 fragment: Some(wgpu::FragmentState {
219 module: &shader_module,
220 entry_point: Some("fs_main"),
221 targets: &[Some(wgpu::ColorTargetState {
222 format: self.surface_format,
223 blend: None,
224 write_mask: wgpu::ColorWrites::ALL,
225 })],
226 compilation_options: Default::default(),
227 }),
228 primitive: wgpu::PrimitiveState {
229 topology: wgpu::PrimitiveTopology::TriangleList,
230 ..Default::default()
231 },
232 depth_stencil: None,
233 multisample: wgpu::MultisampleState::default(),
234 multiview: None,
235 cache: None,
236 });
237
238 let param_data = effect_type.defaults();
239
240 let param_buffer =
241 device
242 .create_buffer_init(&wgpu::util::BufferInitDescriptor {
243 label: Some("postprocess_param_buffer"),
244 contents: bytemuck::cast_slice(¶m_data),
245 usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
246 });
247
248 let param_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
249 label: Some("postprocess_param_bind_group"),
250 layout: &self.params_bind_group_layout,
251 entries: &[wgpu::BindGroupEntry {
252 binding: 0,
253 resource: param_buffer.as_entire_binding(),
254 }],
255 });
256
257 self.effects.push((
258 id,
259 EffectEntry {
260 effect_type,
261 pipeline,
262 param_buffer,
263 param_bind_group,
264 param_data,
265 },
266 ));
267 }
268
269 pub fn set_param(&mut self, id: u32, index: u32, x: f32, y: f32, z: f32, w: f32) {
271 if let Some((_, entry)) = self.effects.iter_mut().find(|(eid, _)| *eid == id) {
272 let base = 4 + (index as usize).min(MAX_EFFECT_PARAMS - 1) * 4;
273 entry.param_data[base] = x;
274 entry.param_data[base + 1] = y;
275 entry.param_data[base + 2] = z;
276 entry.param_data[base + 3] = w;
277 }
278 }
279
280 pub fn remove(&mut self, id: u32) {
282 self.effects.retain(|(eid, _)| *eid != id);
283 }
284
285 pub fn clear(&mut self) {
287 self.effects.clear();
288 }
289
290 fn ensure_targets(&mut self, gpu: &GpuContext) {
292 let w = gpu.config.width;
293 let h = gpu.config.height;
294
295 let needs_recreate = self
296 .target_a
297 .as_ref()
298 .map(|t| t.width != w || t.height != h)
299 .unwrap_or(true);
300
301 if needs_recreate {
302 self.target_a = Some(self.create_target(gpu, w, h, "postprocess_a"));
303 self.target_b = Some(self.create_target(gpu, w, h, "postprocess_b"));
304 }
305 }
306
307 fn create_target(
308 &self,
309 gpu: &GpuContext,
310 width: u32,
311 height: u32,
312 label: &str,
313 ) -> OffscreenTarget {
314 let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {
315 label: Some(label),
316 size: wgpu::Extent3d {
317 width,
318 height,
319 depth_or_array_layers: 1,
320 },
321 mip_level_count: 1,
322 sample_count: 1,
323 dimension: wgpu::TextureDimension::D2,
324 format: self.surface_format,
325 usage: wgpu::TextureUsages::RENDER_ATTACHMENT
326 | wgpu::TextureUsages::TEXTURE_BINDING,
327 view_formats: &[],
328 });
329
330 let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
331
332 let bind_group = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
333 label: Some(&format!("{label}_bind_group")),
334 layout: &self.texture_bind_group_layout,
335 entries: &[
336 wgpu::BindGroupEntry {
337 binding: 0,
338 resource: wgpu::BindingResource::TextureView(&view),
339 },
340 wgpu::BindGroupEntry {
341 binding: 1,
342 resource: wgpu::BindingResource::Sampler(&self.sampler),
343 },
344 ],
345 });
346
347 OffscreenTarget {
348 texture,
349 view,
350 bind_group,
351 width,
352 height,
353 }
354 }
355
356 pub fn sprite_target(&mut self, gpu: &GpuContext) -> &wgpu::TextureView {
359 self.ensure_targets(gpu);
360 &self.target_a.as_ref().unwrap().view
361 }
362
363 pub fn apply(
366 &mut self,
367 gpu: &GpuContext,
368 encoder: &mut wgpu::CommandEncoder,
369 surface_view: &wgpu::TextureView,
370 ) {
371 let n = self.effects.len();
372 if n == 0 {
373 return;
374 }
375
376 let resolution = [gpu.config.width as f32, gpu.config.height as f32];
377
378 for (_, entry) in self.effects.iter_mut() {
380 entry.param_data[0] = resolution[0];
381 entry.param_data[1] = resolution[1];
382 gpu.queue.write_buffer(
383 &entry.param_buffer,
384 0,
385 bytemuck::cast_slice(&entry.param_data),
386 );
387 }
388
389 for i in 0..n {
394 let is_last = i == n - 1;
395
396 let source_bg = if i % 2 == 0 {
398 &self.target_a.as_ref().unwrap().bind_group
399 } else {
400 &self.target_b.as_ref().unwrap().bind_group
401 };
402
403 let dest_view = if is_last {
405 surface_view
406 } else if i % 2 == 0 {
407 &self.target_b.as_ref().unwrap().view
408 } else {
409 &self.target_a.as_ref().unwrap().view
410 };
411
412 let (_, entry) = &self.effects[i];
413
414 let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
415 label: Some("postprocess_pass"),
416 color_attachments: &[Some(wgpu::RenderPassColorAttachment {
417 view: dest_view,
418 resolve_target: None,
419 ops: wgpu::Operations {
420 load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
421 store: wgpu::StoreOp::Store,
422 },
423 })],
424 depth_stencil_attachment: None,
425 timestamp_writes: None,
426 occlusion_query_set: None,
427 });
428
429 pass.set_pipeline(&entry.pipeline);
430 pass.set_bind_group(0, source_bg, &[]);
431 pass.set_bind_group(1, &entry.param_bind_group, &[]);
432 pass.draw(0..3, 0..1); }
434 }
435}
436
437fn build_effect_wgsl(fragment_source: &str) -> String {
439 format!("{}\n{}\n", EFFECT_PREAMBLE, fragment_source)
440}
441
442const EFFECT_PREAMBLE: &str = r#"
444@group(0) @binding(0)
445var t_input: texture_2d<f32>;
446
447@group(0) @binding(1)
448var s_input: sampler;
449
450struct EffectParams {
451 resolution: vec4<f32>,
452 values: array<vec4<f32>, 4>,
453};
454
455@group(1) @binding(0)
456var<uniform> params: EffectParams;
457
458struct VertexOutput {
459 @builtin(position) position: vec4<f32>,
460 @location(0) uv: vec2<f32>,
461};
462
463@vertex
464fn vs_main(@builtin(vertex_index) idx: u32) -> VertexOutput {
465 // Fullscreen triangle: 3 vertices cover clip space [-1,1]
466 var out: VertexOutput;
467 let uv = vec2<f32>(f32((idx << 1u) & 2u), f32(idx & 2u));
468 out.position = vec4<f32>(uv * 2.0 - 1.0, 0.0, 1.0);
469 out.uv = vec2<f32>(uv.x, 1.0 - uv.y);
470 return out;
471}
472"#;
473
474const BLOOM_FRAGMENT: &str = r#"
477@fragment
478fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
479 let resolution = params.resolution.xy;
480 let threshold = params.values[0].x;
481 let intensity = params.values[0].y;
482 let radius = params.values[0].z;
483
484 let texel = 1.0 / resolution;
485 let original = textureSample(t_input, s_input, in.uv);
486
487 // 3x3 Gaussian-weighted bright-pass blur
488 var bloom = vec3<f32>(0.0);
489
490 let s00 = textureSample(t_input, s_input, in.uv + vec2<f32>(-1.0, -1.0) * texel * radius).rgb;
491 let s10 = textureSample(t_input, s_input, in.uv + vec2<f32>( 0.0, -1.0) * texel * radius).rgb;
492 let s20 = textureSample(t_input, s_input, in.uv + vec2<f32>( 1.0, -1.0) * texel * radius).rgb;
493 let s01 = textureSample(t_input, s_input, in.uv + vec2<f32>(-1.0, 0.0) * texel * radius).rgb;
494 let s11 = textureSample(t_input, s_input, in.uv).rgb;
495 let s21 = textureSample(t_input, s_input, in.uv + vec2<f32>( 1.0, 0.0) * texel * radius).rgb;
496 let s02 = textureSample(t_input, s_input, in.uv + vec2<f32>(-1.0, 1.0) * texel * radius).rgb;
497 let s12 = textureSample(t_input, s_input, in.uv + vec2<f32>( 0.0, 1.0) * texel * radius).rgb;
498 let s22 = textureSample(t_input, s_input, in.uv + vec2<f32>( 1.0, 1.0) * texel * radius).rgb;
499
500 let lum = vec3<f32>(0.2126, 0.7152, 0.0722);
501 bloom += max(dot(s00, lum) - threshold, 0.0) * s00 * 0.0625;
502 bloom += max(dot(s10, lum) - threshold, 0.0) * s10 * 0.125;
503 bloom += max(dot(s20, lum) - threshold, 0.0) * s20 * 0.0625;
504 bloom += max(dot(s01, lum) - threshold, 0.0) * s01 * 0.125;
505 bloom += max(dot(s11, lum) - threshold, 0.0) * s11 * 0.25;
506 bloom += max(dot(s21, lum) - threshold, 0.0) * s21 * 0.125;
507 bloom += max(dot(s02, lum) - threshold, 0.0) * s02 * 0.0625;
508 bloom += max(dot(s12, lum) - threshold, 0.0) * s12 * 0.125;
509 bloom += max(dot(s22, lum) - threshold, 0.0) * s22 * 0.0625;
510
511 return vec4<f32>(original.rgb + bloom * intensity, original.a);
512}
513"#;
514
515const BLUR_FRAGMENT: &str = r#"
518@fragment
519fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
520 let resolution = params.resolution.xy;
521 let strength = params.values[0].x;
522
523 let texel = 1.0 / resolution * strength;
524
525 var color = vec4<f32>(0.0);
526 color += textureSample(t_input, s_input, in.uv + vec2<f32>(-1.0, -1.0) * texel) * 0.0625;
527 color += textureSample(t_input, s_input, in.uv + vec2<f32>( 0.0, -1.0) * texel) * 0.125;
528 color += textureSample(t_input, s_input, in.uv + vec2<f32>( 1.0, -1.0) * texel) * 0.0625;
529 color += textureSample(t_input, s_input, in.uv + vec2<f32>(-1.0, 0.0) * texel) * 0.125;
530 color += textureSample(t_input, s_input, in.uv) * 0.25;
531 color += textureSample(t_input, s_input, in.uv + vec2<f32>( 1.0, 0.0) * texel) * 0.125;
532 color += textureSample(t_input, s_input, in.uv + vec2<f32>(-1.0, 1.0) * texel) * 0.0625;
533 color += textureSample(t_input, s_input, in.uv + vec2<f32>( 0.0, 1.0) * texel) * 0.125;
534 color += textureSample(t_input, s_input, in.uv + vec2<f32>( 1.0, 1.0) * texel) * 0.0625;
535
536 return color;
537}
538"#;
539
540const VIGNETTE_FRAGMENT: &str = r#"
543@fragment
544fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
545 let intensity = params.values[0].x;
546 let radius = params.values[0].y;
547
548 let original = textureSample(t_input, s_input, in.uv);
549
550 let center = in.uv - vec2<f32>(0.5);
551 let dist = length(center) * 1.414;
552 let vignette = smoothstep(radius, radius - 0.3, dist);
553 let factor = mix(1.0, vignette, intensity);
554
555 return vec4<f32>(original.rgb * factor, original.a);
556}
557"#;
558
559const CRT_FRAGMENT: &str = r#"
562@fragment
563fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
564 let scanline_freq = params.values[0].x;
565 let distortion = params.values[0].y;
566 let brightness = params.values[0].z;
567
568 // Barrel distortion
569 let center = in.uv - vec2<f32>(0.5);
570 let dist2 = dot(center, center);
571 let distorted_uv = in.uv + center * dist2 * distortion;
572
573 // Black outside screen bounds
574 if distorted_uv.x < 0.0 || distorted_uv.x > 1.0 || distorted_uv.y < 0.0 || distorted_uv.y > 1.0 {
575 return vec4<f32>(0.0, 0.0, 0.0, 1.0);
576 }
577
578 let original = textureSample(t_input, s_input, distorted_uv);
579
580 // Scanlines
581 let scanline = sin(distorted_uv.y * scanline_freq) * 0.5 + 0.5;
582 let scanline_effect = mix(0.8, 1.0, scanline);
583
584 // Chromatic aberration (subtle RGB offset at edges)
585 let ca_offset = center * dist2 * 0.003;
586 let r = textureSample(t_input, s_input, distorted_uv + ca_offset).r;
587 let g = original.g;
588 let b = textureSample(t_input, s_input, distorted_uv - ca_offset).b;
589
590 return vec4<f32>(vec3<f32>(r, g, b) * scanline_effect * brightness, original.a);
591}
592"#;
593
594#[cfg(test)]
595mod tests {
596 use super::*;
597
598 #[test]
599 fn test_effect_type_from_str_bloom() {
600 assert!(matches!(EffectType::from_str("bloom"), Some(EffectType::Bloom)));
601 }
602
603 #[test]
604 fn test_effect_type_from_str_blur() {
605 assert!(matches!(EffectType::from_str("blur"), Some(EffectType::Blur)));
606 }
607
608 #[test]
609 fn test_effect_type_from_str_vignette() {
610 assert!(matches!(EffectType::from_str("vignette"), Some(EffectType::Vignette)));
611 }
612
613 #[test]
614 fn test_effect_type_from_str_crt() {
615 assert!(matches!(EffectType::from_str("crt"), Some(EffectType::Crt)));
616 }
617
618 #[test]
619 fn test_effect_type_from_str_unknown() {
620 assert!(EffectType::from_str("unknown").is_none());
621 assert!(EffectType::from_str("").is_none());
622 assert!(EffectType::from_str("Bloom").is_none()); }
624
625 #[test]
626 fn test_bloom_defaults() {
627 let d = EffectType::Bloom.defaults();
628 assert_eq!(d[0], 0.0);
630 assert_eq!(d[4], 0.7);
632 assert_eq!(d[5], 0.5);
633 assert_eq!(d[6], 3.0);
634 }
635
636 #[test]
637 fn test_blur_defaults() {
638 let d = EffectType::Blur.defaults();
639 assert_eq!(d[4], 1.0); assert_eq!(d[5], 0.0); }
642
643 #[test]
644 fn test_vignette_defaults() {
645 let d = EffectType::Vignette.defaults();
646 assert_eq!(d[4], 0.5); assert_eq!(d[5], 0.8); }
649
650 #[test]
651 fn test_crt_defaults() {
652 let d = EffectType::Crt.defaults();
653 assert_eq!(d[4], 800.0); assert_eq!(d[5], 0.1); assert_eq!(d[6], 1.1); }
657
658 #[test]
659 fn test_defaults_array_size() {
660 let d = EffectType::Bloom.defaults();
661 assert_eq!(d.len(), PARAM_FLOATS);
662 }
663}