ff-render 0.18.1

GPU compositing pipeline for real-time preview (wgpu-based)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Three-pass GPU bloom / glow render node.
//!
//! Pass 0 extracts pixels at or above a luma `threshold` (others become black),
//! pass 1 Gaussian-blurs those highlights (reusing [`GaussianBlurNode`]), and
//! pass 2 adds the blurred highlights back onto the original frame weighted by
//! `intensity`. A CPU fallback ([`RenderNodeCpu`]) mirrors the same three steps.

use super::RenderNodeCpu;
use super::blur::separable_blur_f32;

#[cfg(feature = "wgpu")]
use super::GaussianBlurNode;
#[cfg(feature = "wgpu")]
use super::blur::{create_uniform, fullscreen_pipeline, run_fullscreen, texture_entry};

// GlowNode

/// Three-pass GPU bloom / glow effect.
///
/// Pass 0: extract pixels whose luma is `>= threshold` (others become black).
/// Pass 1: Gaussian-blur the extracted highlights (sigma = `radius`).
/// Pass 2: additive blend of original + blurred highlights weighted by `intensity`.
pub struct GlowNode {
    /// Luminance threshold, clamped to `[0.0, 1.0]`; pixels below it are
    /// suppressed before the glow blur.
    pub threshold: f32,
    /// Gaussian blur sigma in pixels for the glow spread (shares the blur node's
    /// `[0.5, 20.0]` effective range).
    pub radius: f32,
    /// Additive blend weight of the glow layer (`0.0` = no glow / identity).
    pub intensity: f32,
    /// The blur node reused for pass 1 (its pipeline caches independently).
    #[cfg(feature = "wgpu")]
    blur: GaussianBlurNode,
    #[cfg(feature = "wgpu")]
    pipeline: std::sync::OnceLock<GlowPipeline>,
}

impl GlowNode {
    /// Creates a glow node with the given threshold, blur radius, and intensity.
    #[must_use]
    pub fn new(threshold: f32, radius: f32, intensity: f32) -> Self {
        Self {
            threshold,
            radius,
            intensity,
            #[cfg(feature = "wgpu")]
            blur: GaussianBlurNode::new(radius),
            #[cfg(feature = "wgpu")]
            pipeline: std::sync::OnceLock::new(),
        }
    }
}

impl Default for GlowNode {
    /// Identity node (no glow).
    fn default() -> Self {
        Self::new(0.8, 10.0, 0.0)
    }
}

// CPU path

impl RenderNodeCpu for GlowNode {
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    fn process_cpu(&self, rgba: &mut [u8], w: u32, h: u32) {
        let threshold = self.threshold.clamp(0.0, 1.0);

        // Pass 0: extract highlights into an opaque black scratch buffer.
        let mut highlights = vec![0u8; rgba.len()];
        for (dst, px) in highlights
            .as_chunks_mut::<4>()
            .0
            .iter_mut()
            .zip(rgba.as_chunks::<4>().0)
        {
            let luma =
                (0.299 * f32::from(px[0]) + 0.587 * f32::from(px[1]) + 0.114 * f32::from(px[2]))
                    / 255.0;
            if luma >= threshold {
                dst[0..3].copy_from_slice(&px[0..3]);
            }
            dst[3] = 255;
        }

        // Pass 1: Gaussian-blur the highlights (shared kernel with the blur node).
        let Some(blurred) = separable_blur_f32(&highlights, w, h, self.radius) else {
            return;
        };

        // Pass 2: additive blend of the original with the blurred highlights.
        for (px, glow) in rgba
            .as_chunks_mut::<4>()
            .0
            .iter_mut()
            .zip(blurred.as_chunks::<4>().0)
        {
            for c in 0..3 {
                let base = f32::from(px[c]) / 255.0;
                let out = (base + glow[c] * self.intensity).clamp(0.0, 1.0);
                px[c] = (out * 255.0 + 0.5) as u8;
            }
            // alpha unchanged
        }
    }
}

// GPU path

#[cfg(feature = "wgpu")]
struct GlowPipeline {
    extract: EffectPipeline,
    blend: EffectPipeline,
}

#[cfg(feature = "wgpu")]
struct EffectPipeline {
    render_pipeline: wgpu::RenderPipeline,
    bind_group_layout: wgpu::BindGroupLayout,
    uniform_buf: wgpu::Buffer,
}

#[cfg(feature = "wgpu")]
impl GlowNode {
    fn get_or_create_pipeline(&self, ctx: &crate::context::RenderContext) -> &GlowPipeline {
        self.pipeline.get_or_init(|| GlowPipeline {
            extract: create_extract_pipeline(ctx, self.threshold.clamp(0.0, 1.0)),
            blend: create_blend_pipeline(ctx, self.intensity),
        })
    }
}

#[cfg(feature = "wgpu")]
impl super::RenderNode for GlowNode {
    fn pass_count(&self) -> usize {
        3
    }

    fn process(
        &self,
        inputs: &[&wgpu::Texture],
        outputs: &[&wgpu::Texture],
        ctx: &crate::context::RenderContext,
    ) {
        let Some(input) = inputs.first() else {
            log::warn!("GlowNode::process called with no inputs");
            return;
        };
        if outputs.len() < 3 {
            log::warn!("GlowNode::process needs 3 output targets");
            return;
        }
        let pd = self.get_or_create_pipeline(ctx);

        // Pass 0: extract highlights (input -> outputs[0]).
        encode_uniform_texture_pass(ctx, &pd.extract, &[input], outputs[0], "Glow extract");

        // Pass 1: blur the highlights. Delegating to GaussianBlurNode runs the two
        // separable passes as outputs[0] -> outputs[1] -> outputs[0], leaving the
        // blurred highlights in outputs[0]. The extract in outputs[0] is consumed by
        // the horizontal pass before the vertical pass overwrites it.
        self.blur
            .process(&[outputs[0]], &[outputs[1], outputs[0]], ctx);

        // Pass 2: additive blend of the original with the blurred highlights
        // (input + outputs[0] -> outputs[2], the final target).
        encode_uniform_texture_pass(
            ctx,
            &pd.blend,
            &[input, outputs[0]],
            outputs[2],
            "Glow blend",
        );
    }
}

/// Builds the single-pass highlight-extraction pipeline (texture + `threshold`).
#[cfg(feature = "wgpu")]
fn create_extract_pipeline(ctx: &crate::context::RenderContext, threshold: f32) -> EffectPipeline {
    let device = &ctx.device;
    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("Glow extract shader"),
        source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/glow_extract.wgsl").into()),
    });
    let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some("Glow extract BGL"),
        entries: &[texture_entry(0), uniform_entry(1)],
    });
    let render_pipeline = fullscreen_pipeline(device, &shader, &bgl, "Glow extract");
    let uniform_buf = create_uniform(device, "Glow extract uniforms", 16);
    ctx.queue
        .write_buffer(&uniform_buf, 0, &pack_scalar(threshold));
    EffectPipeline {
        render_pipeline,
        bind_group_layout: bgl,
        uniform_buf,
    }
}

/// Builds the two-input additive-blend pipeline (original + blurred + `intensity`).
#[cfg(feature = "wgpu")]
fn create_blend_pipeline(ctx: &crate::context::RenderContext, intensity: f32) -> EffectPipeline {
    let device = &ctx.device;
    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("Glow blend shader"),
        source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/glow_blend.wgsl").into()),
    });
    let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some("Glow blend BGL"),
        entries: &[texture_entry(0), texture_entry(1), uniform_entry(2)],
    });
    let render_pipeline = fullscreen_pipeline(device, &shader, &bgl, "Glow blend");
    let uniform_buf = create_uniform(device, "Glow blend uniforms", 16);
    ctx.queue
        .write_buffer(&uniform_buf, 0, &pack_scalar(intensity));
    EffectPipeline {
        render_pipeline,
        bind_group_layout: bgl,
        uniform_buf,
    }
}

#[cfg(feature = "wgpu")]
fn uniform_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
    wgpu::BindGroupLayoutEntry {
        binding,
        visibility: wgpu::ShaderStages::FRAGMENT,
        ty: wgpu::BindingType::Buffer {
            ty: wgpu::BufferBindingType::Uniform,
            has_dynamic_offset: false,
            min_binding_size: None,
        },
        count: None,
    }
}

/// Packs one f32 into a 16-byte uniform (the shader pads it to a vec4).
#[cfg(feature = "wgpu")]
fn pack_scalar(v: f32) -> [u8; 16] {
    let mut b = [0u8; 16];
    b[0..4].copy_from_slice(&v.to_le_bytes());
    b
}

/// Encodes one fullscreen pass binding `textures` at bindings `0..n` followed by
/// the pipeline's uniform at binding `n`, writing into `output`.
// `textures.len()` is 1 or 2 here, so the binding-index casts cannot truncate.
#[cfg(feature = "wgpu")]
#[allow(clippy::cast_possible_truncation)]
fn encode_uniform_texture_pass(
    ctx: &crate::context::RenderContext,
    pd: &EffectPipeline,
    textures: &[&wgpu::Texture],
    output: &wgpu::Texture,
    label: &str,
) {
    let views: Vec<wgpu::TextureView> = textures
        .iter()
        .map(|t| t.create_view(&wgpu::TextureViewDescriptor::default()))
        .collect();
    let output_view = output.create_view(&wgpu::TextureViewDescriptor::default());

    let mut entries: Vec<wgpu::BindGroupEntry> = views
        .iter()
        .enumerate()
        .map(|(i, view)| wgpu::BindGroupEntry {
            binding: i as u32,
            resource: wgpu::BindingResource::TextureView(view),
        })
        .collect();
    entries.push(wgpu::BindGroupEntry {
        binding: views.len() as u32,
        resource: pd.uniform_buf.as_entire_binding(),
    });

    let bind_group = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some(label),
        layout: &pd.bind_group_layout,
        entries: &entries,
    });
    run_fullscreen(ctx, &pd.render_pipeline, &bind_group, &output_view, label);
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A `w × h` opaque black frame with a filled `[x0, x1) × [y0, y1)` white
    /// rectangle (used as the glow source).
    fn white_rect(
        w: usize,
        h: usize,
        x0: usize,
        x1: usize,
        y0: usize,
        y1: usize,
        v: u8,
    ) -> Vec<u8> {
        let mut buf = vec![0u8; w * h * 4];
        for (i, px) in buf.as_chunks_mut::<4>().0.iter_mut().enumerate() {
            let x = i % w;
            let y = i / w;
            px[3] = 255;
            if x >= x0 && x < x1 && y >= y0 && y < y1 {
                px[0] = v;
                px[1] = v;
                px[2] = v;
            }
        }
        buf
    }

    #[test]
    fn glow_should_produce_halo_beyond_edge() {
        // 16x16 white rectangle centred in a 48x48 black frame; right edge at x=32.
        let (w, h) = (48usize, 48usize);
        let mut frame = white_rect(w, h, 16, 32, 16, 32, 255);
        GlowNode::new(0.8, 10.0, 1.0).process_cpu(&mut frame, w as u32, h as u32);
        // A pixel 5 px beyond the right edge (x=37, y=24) must have gained glow.
        let p = (24 * w + 37) * 4;
        assert!(
            frame[p] > 0,
            "glow halo must extend >= 5 px beyond the edge; got {}",
            frame[p]
        );
    }

    #[test]
    fn glow_intensity_zero_should_be_noop() {
        let (w, h) = (32usize, 32usize);
        let original = white_rect(w, h, 8, 24, 8, 24, 255);
        let mut frame = original.clone();
        GlowNode::new(0.8, 10.0, 0.0).process_cpu(&mut frame, w as u32, h as u32);
        for (a, b) in frame.iter().zip(original.iter()) {
            assert!(
                (i32::from(*a) - i32::from(*b)).abs() <= 1,
                "intensity 0 must be a no-op (within rounding); got {a} vs {b}"
            );
        }
    }

    #[test]
    fn glow_high_threshold_should_suppress() {
        // A bright but sub-white rectangle (luma ~0.78): with threshold 1.1 clamped
        // to 1.0, no pixel qualifies, so the frame is unchanged.
        let (w, h) = (32usize, 32usize);
        let original = white_rect(w, h, 8, 24, 8, 24, 200);
        let mut frame = original.clone();
        GlowNode::new(1.1, 10.0, 1.0).process_cpu(&mut frame, w as u32, h as u32);
        for (a, b) in frame.iter().zip(original.iter()) {
            assert!(
                (i32::from(*a) - i32::from(*b)).abs() <= 1,
                "threshold above the brightest luma must suppress all glow; got {a} vs {b}"
            );
        }
    }
}

#[cfg(all(test, feature = "wgpu"))]
mod gpu_tests {
    use super::*;
    use crate::context::RenderContext;
    use crate::graph::RenderGraph;
    use std::sync::Arc;

    fn ctx() -> Option<Arc<RenderContext>> {
        match futures::executor::block_on(RenderContext::init()) {
            Ok(ctx) => Some(Arc::new(ctx)),
            Err(_) => None,
        }
    }

    fn white_rect(
        w: usize,
        h: usize,
        x0: usize,
        x1: usize,
        y0: usize,
        y1: usize,
        v: u8,
    ) -> Vec<u8> {
        let mut buf = vec![0u8; w * h * 4];
        for (i, px) in buf.as_chunks_mut::<4>().0.iter_mut().enumerate() {
            let x = i % w;
            let y = i / w;
            px[3] = 255;
            if x >= x0 && x < x1 && y >= y0 && y < y1 {
                px[0] = v;
                px[1] = v;
                px[2] = v;
            }
        }
        buf
    }

    #[test]
    fn glow_gpu_should_produce_halo_beyond_edge() {
        let Some(ctx) = ctx() else {
            return;
        };
        let (w, h) = (48u32, 48u32);
        let frame = white_rect(48, 48, 16, 32, 16, 32, 255);
        let gpu = RenderGraph::new(Arc::clone(&ctx))
            .push(GlowNode::new(0.8, 10.0, 1.0))
            .process_gpu(&frame, w, h)
            .expect("gpu glow");
        let p = (24 * 48 + 37) * 4;
        assert!(
            gpu[p] > 0,
            "GPU glow halo must extend >= 5 px beyond the edge; got {}",
            gpu[p]
        );
    }

    #[test]
    fn glow_gpu_intensity_zero_should_preserve_input() {
        let Some(ctx) = ctx() else {
            return;
        };
        let (w, h) = (32u32, 32u32);
        let frame = white_rect(32, 32, 8, 24, 8, 24, 255);
        let gpu = RenderGraph::new(Arc::clone(&ctx))
            .push(GlowNode::new(0.8, 10.0, 0.0))
            .process_gpu(&frame, w, h)
            .expect("gpu glow");
        // Sample the rectangle centre and a background pixel: both must be unchanged.
        for &idx in &[(16 * 32 + 16) * 4, (2 * 32 + 2) * 4] {
            assert!(
                (i32::from(gpu[idx]) - i32::from(frame[idx])).abs() <= 1,
                "intensity 0 must preserve the input on the GPU path"
            );
        }
    }
}