ff-render 0.14.2

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
use super::RenderNodeCpu;

// ── Pipeline cache ────────────────────────────────────────────────────────────

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

// ── ColorGradeNode ────────────────────────────────────────────────────────────

/// Basic colour grading: brightness, contrast, saturation, temperature, tint.
///
/// # Processing order
///
/// brightness → contrast → temperature/tint → saturation
pub struct ColorGradeNode {
    /// Additive brightness offset (−1.0 – +1.0; 0.0 = no change).
    pub brightness: f32,
    /// Contrast multiplier around 0.5 (0.0 – 4.0; 1.0 = no change).
    pub contrast: f32,
    /// Saturation multiplier (0.0 = greyscale; 1.0 = no change; 2.0 = double).
    pub saturation: f32,
    /// Colour temperature offset (−1.0 = cool/blue; +1.0 = warm/orange).
    pub temperature: f32,
    /// Tint offset (−1.0 = magenta; +1.0 = green).
    pub tint: f32,
    #[cfg(feature = "wgpu")]
    pipeline: std::sync::OnceLock<ColorGradePipeline>,
}

impl ColorGradeNode {
    /// Identity node (no colour change).
    #[must_use]
    pub fn new(
        brightness: f32,
        contrast: f32,
        saturation: f32,
        temperature: f32,
        tint: f32,
    ) -> Self {
        Self {
            brightness,
            contrast,
            saturation,
            temperature,
            tint,
            #[cfg(feature = "wgpu")]
            pipeline: std::sync::OnceLock::new(),
        }
    }
}

impl Default for ColorGradeNode {
    fn default() -> Self {
        Self::new(0.0, 1.0, 1.0, 0.0, 0.0)
    }
}

// ── CPU path ──────────────────────────────────────────────────────────────────

impl RenderNodeCpu for ColorGradeNode {
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    fn process_cpu(&self, rgba: &mut [u8], _w: u32, _h: u32) {
        for pixel in rgba.chunks_exact_mut(4) {
            let r = f32::from(pixel[0]) / 255.0;
            let g = f32::from(pixel[1]) / 255.0;
            let b = f32::from(pixel[2]) / 255.0;

            // Brightness
            let r = r + self.brightness;
            let g = g + self.brightness;
            let b = b + self.brightness;

            // Contrast
            let r = (r - 0.5) * self.contrast + 0.5;
            let g = (g - 0.5) * self.contrast + 0.5;
            let b = (b - 0.5) * self.contrast + 0.5;

            // Temperature
            let r = r + self.temperature * 0.1;
            let b = b - self.temperature * 0.1;

            // Tint
            let g = g + self.tint * 0.1;

            // Saturation (BT.709 luma coefficients)
            let luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
            let r = luma + (r - luma) * self.saturation;
            let g = luma + (g - luma) * self.saturation;
            let b = luma + (b - luma) * self.saturation;

            pixel[0] = (r.clamp(0.0, 1.0) * 255.0 + 0.5) as u8;
            pixel[1] = (g.clamp(0.0, 1.0) * 255.0 + 0.5) as u8;
            pixel[2] = (b.clamp(0.0, 1.0) * 255.0 + 0.5) as u8;
            // alpha unchanged
        }
    }
}

// ── GPU path ──────────────────────────────────────────────────────────────────

#[cfg(feature = "wgpu")]
impl ColorGradeNode {
    fn get_or_create_pipeline(&self, ctx: &crate::context::RenderContext) -> &ColorGradePipeline {
        self.pipeline.get_or_init(|| {
            let device = &ctx.device;

            let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
                label: Some("ColorGrade shader"),
                source: wgpu::ShaderSource::Wgsl(
                    include_str!("../shaders/color_grade.wgsl").into(),
                ),
            });

            let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("ColorGrade BGL"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Texture {
                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
                            view_dimension: wgpu::TextureViewDimension::D2,
                            multisampled: false,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 2,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Uniform,
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                ],
            });

            let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("ColorGrade layout"),
                bind_group_layouts: &[Some(&bgl)],
                immediate_size: 0,
            });

            let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
                label: Some("ColorGrade pipeline"),
                layout: Some(&pipeline_layout),
                vertex: wgpu::VertexState {
                    module: &shader,
                    entry_point: Some("vs_main"),
                    buffers: &[],
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                },
                fragment: Some(wgpu::FragmentState {
                    module: &shader,
                    entry_point: Some("fs_main"),
                    targets: &[Some(wgpu::ColorTargetState {
                        format: wgpu::TextureFormat::Rgba8Unorm,
                        blend: None,
                        write_mask: wgpu::ColorWrites::ALL,
                    })],
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                }),
                primitive: wgpu::PrimitiveState::default(),
                depth_stencil: None,
                multisample: wgpu::MultisampleState::default(),
                multiview_mask: None,
                cache: None,
            });

            let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
                label: Some("ColorGrade sampler"),
                address_mode_u: wgpu::AddressMode::ClampToEdge,
                address_mode_v: wgpu::AddressMode::ClampToEdge,
                mag_filter: wgpu::FilterMode::Linear,
                min_filter: wgpu::FilterMode::Linear,
                ..Default::default()
            });

            // 8 × f32 = 32 bytes — matches ColorGradeUniforms in the shader.
            let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("ColorGrade uniforms"),
                size: 32,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });

            ColorGradePipeline {
                render_pipeline,
                bind_group_layout: bgl,
                sampler,
                uniform_buf,
            }
        })
    }
}

#[cfg(feature = "wgpu")]
impl super::RenderNode for ColorGradeNode {
    fn process(
        &self,
        inputs: &[&wgpu::Texture],
        outputs: &[&wgpu::Texture],
        ctx: &crate::context::RenderContext,
    ) {
        let Some(input) = inputs.first() else {
            log::warn!("ColorGradeNode::process called with no inputs");
            return;
        };
        let Some(output) = outputs.first() else {
            log::warn!("ColorGradeNode::process called with no outputs");
            return;
        };

        let pd = self.get_or_create_pipeline(ctx);

        // Update uniforms for this frame.
        let uniform_bytes = pack_f32(&[
            self.brightness,
            self.contrast,
            self.saturation,
            self.temperature,
            self.tint,
            0.0,
            0.0,
            0.0,
        ]);
        ctx.queue.write_buffer(&pd.uniform_buf, 0, &uniform_bytes);

        let input_view = input.create_view(&wgpu::TextureViewDescriptor::default());
        let output_view = output.create_view(&wgpu::TextureViewDescriptor::default());

        let bind_group = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("ColorGrade BG"),
            layout: &pd.bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&input_view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&pd.sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: pd.uniform_buf.as_entire_binding(),
                },
            ],
        });

        let mut encoder = ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("ColorGrade pass"),
            });
        {
            let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("ColorGrade pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &output_view,
                    resolve_target: None,
                    depth_slice: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                timestamp_writes: None,
                occlusion_query_set: None,
                multiview_mask: None,
            });
            pass.set_pipeline(&pd.render_pipeline);
            pass.set_bind_group(0, &bind_group, &[]);
            pass.draw(0..6, 0..1);
        }
        ctx.queue.submit(std::iter::once(encoder.finish()));
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn color_grade_node_default_should_be_identity() {
        let node = ColorGradeNode::default();
        let original = vec![100u8, 150, 200, 255];
        let mut rgba = original.clone();
        node.process_cpu(&mut rgba, 1, 1);
        // Identity: brightness=0, contrast=1, saturation=1, temperature=0, tint=0.
        // Allow ±1 rounding error from f32 round-trip.
        for i in 0..3 {
            let diff = (rgba[i] as i32 - original[i] as i32).abs();
            assert!(
                diff <= 1,
                "identity must preserve pixel at channel {i}: expected ~{} got {}",
                original[i],
                rgba[i]
            );
        }
        assert_eq!(rgba[3], 255, "alpha must not change");
    }

    #[test]
    fn color_grade_node_brightness_positive_should_increase_mid_grey() {
        let node = ColorGradeNode {
            brightness: 0.5,
            ..Default::default()
        };
        let mut rgba = vec![128u8, 128, 128, 255]; // mid-grey
        node.process_cpu(&mut rgba, 1, 1);
        assert!(
            rgba[0] > 128,
            "brightness +0.5 must increase R; got {}",
            rgba[0]
        );
        assert!(
            rgba[1] > 128,
            "brightness +0.5 must increase G; got {}",
            rgba[1]
        );
        assert!(
            rgba[2] > 128,
            "brightness +0.5 must increase B; got {}",
            rgba[2]
        );
        assert_eq!(rgba[3], 255, "alpha must not change");
    }

    #[test]
    fn color_grade_node_brightness_negative_should_decrease_mid_grey() {
        let node = ColorGradeNode {
            brightness: -0.5,
            ..Default::default()
        };
        let mut rgba = vec![128u8, 128, 128, 255];
        node.process_cpu(&mut rgba, 1, 1);
        assert!(
            rgba[0] < 128,
            "brightness −0.5 must decrease R; got {}",
            rgba[0]
        );
    }

    #[test]
    fn color_grade_node_saturation_zero_should_produce_greyscale() {
        let node = ColorGradeNode {
            saturation: 0.0,
            ..Default::default()
        };
        let mut rgba = vec![200u8, 100, 50, 255]; // colourful pixel
        node.process_cpu(&mut rgba, 1, 1);
        // All channels must be equal (greyscale) — allow ±1 rounding.
        let diff_rg = (rgba[0] as i32 - rgba[1] as i32).abs();
        let diff_rb = (rgba[0] as i32 - rgba[2] as i32).abs();
        assert!(
            diff_rg <= 1,
            "saturation=0 must equalise R and G; got R={} G={}",
            rgba[0],
            rgba[1]
        );
        assert!(
            diff_rb <= 1,
            "saturation=0 must equalise R and B; got R={} B={}",
            rgba[0],
            rgba[2]
        );
    }

    #[test]
    fn color_grade_node_clamp_should_not_exceed_255() {
        let node = ColorGradeNode {
            brightness: 2.0,
            ..Default::default()
        };
        let mut rgba = vec![200u8, 200, 200, 255];
        node.process_cpu(&mut rgba, 1, 1);
        assert_eq!(rgba[0], 255, "clamped R must be 255");
        assert_eq!(rgba[1], 255, "clamped G must be 255");
        assert_eq!(rgba[2], 255, "clamped B must be 255");
    }

    #[test]
    fn color_grade_node_variants_should_construct_via_default() {
        let _ = ColorGradeNode {
            brightness: 0.1,
            contrast: 1.2,
            saturation: 0.9,
            ..Default::default()
        };
    }
}

// ── helpers ───────────────────────────────────────────────────────────────────

#[cfg(feature = "wgpu")]
fn pack_f32(values: &[f32]) -> Vec<u8> {
    values.iter().flat_map(|f| f.to_le_bytes()).collect()
}