oxigaf-render 0.1.2

Differentiable 3D Gaussian Splatting rasterizer (wgpu)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Local neighbourhood statistics and variance clipping.
//!
//! [`local_color_stats`] is the reference formulation; [`RowWindowStats`] is
//! the row-sliding running-sum form used by
//! [`crate::temporal_aa::accumulate_taa`] for the same windows at `O(1)` per
//! pixel.

// ---------------------------------------------------------------------------
// Local color statistics for variance clipping
// ---------------------------------------------------------------------------

/// Compute per-pixel mean and standard deviation over a local window.
///
/// Gathers all pixels in a `(2*radius+1)²` window centered at `(cx, cy)`,
/// clamped to the image edges, and computes per-channel statistics.
///
/// # Parameters
///
/// - `image`: flat RGB image buffer, `len == width * height * 3`.
/// - `width`, `height`: image dimensions.
/// - `cx`, `cy`: center pixel coordinates.
/// - `radius`: half-width of the window. `radius=1` → 3×3 window.
///
/// # Returns
///
/// `(mean, std)` as `[f32; 3]` per channel. `std` is the population standard deviation.
pub fn local_color_stats(
    image: &[f32],
    width: usize,
    height: usize,
    cx: usize,
    cy: usize,
    radius: usize,
) -> ([f32; 3], [f32; 3]) {
    let y_min = cy.saturating_sub(radius);
    let y_max = (cy + radius + 1).min(height);
    let x_min = cx.saturating_sub(radius);
    let x_max = (cx + radius + 1).min(width);

    let mut sum = [0.0_f32; 3];
    let mut sum_sq = [0.0_f32; 3];
    let mut count = 0_usize;

    for ny in y_min..y_max {
        for nx in x_min..x_max {
            let base = (ny * width + nx) * 3;
            for c in 0..3 {
                let v = image.get(base + c).copied().unwrap_or(0.0);
                sum[c] += v;
                sum_sq[c] += v * v;
            }
            count += 1;
        }
    }

    if count == 0 {
        return ([0.0; 3], [0.0; 3]);
    }

    let n = count as f32;
    let mut mean = [0.0_f32; 3];
    let mut std = [0.0_f32; 3];
    for c in 0..3 {
        mean[c] = sum[c] / n;
        let variance = (sum_sq[c] / n) - (mean[c] * mean[c]);
        std[c] = variance.max(0.0).sqrt();
    }

    (mean, std)
}

/// Row-sliding provider of the same local mean/σ that [`local_color_stats`]
/// computes, at `O(1)` per pixel instead of `O((2r+1)²)`.
///
/// [`local_color_stats`] re-sums the whole window from scratch for every
/// pixel, so a full-frame variance-clipping pass costs `O(w · h · r²)`. This
/// walks the image in row-major order and keeps, for each column, the running
/// sums of `v` and `v²` over the current *vertical* window; advancing one row
/// adds the row entering the window and subtracts the one leaving it. A
/// second running sum slides horizontally across the columns of one row.
/// Total cost is `O(w · h)` per frame with `O(w)` extra memory.
///
/// The window bounds are identical to [`local_color_stats`]: the half-open
/// ranges `[cy - r, cy + r + 1) ∩ [0, height)` and
/// `[cx - r, cx + r + 1) ∩ [0, width)`, i.e. clipped (not clamp-replicated)
/// at the borders, with `count` equal to the number of pixels actually inside.
///
/// Sums are accumulated in `f64` so that the incremental add/subtract does
/// not drift across a full image sweep; the published values are `f32`. The
/// result therefore agrees with [`local_color_stats`] to within `f32`
/// summation-order noise rather than bit-exactly.
pub(super) struct RowWindowStats {
    width: usize,
    height: usize,
    radius: usize,
    /// Per-column running sums of `v` over the current vertical window,
    /// `width * 3` entries laid out as `[x * 3 + channel]`.
    col_sum: Vec<f64>,
    /// Per-column running sums of `v²`, same layout as `col_sum`.
    col_sum_sq: Vec<f64>,
    /// Half-open vertical window `[y_min, y_max)` currently held in the
    /// column sums. Equal when nothing has been accumulated yet.
    y_min: usize,
    y_max: usize,
    /// Per-channel means for the row most recently produced (`width * 3`).
    mean_row: Vec<f32>,
    /// Per-channel population σ for that row (`width * 3`).
    std_row: Vec<f32>,
}

impl RowWindowStats {
    /// Allocate the running-sum scratch for a `width × height` image.
    pub(super) fn new(width: usize, height: usize, radius: usize) -> Self {
        let stride = width.saturating_mul(3);
        Self {
            width,
            height,
            radius,
            col_sum: vec![0.0_f64; stride],
            col_sum_sq: vec![0.0_f64; stride],
            y_min: 0,
            y_max: 0,
            mean_row: vec![0.0_f32; stride],
            std_row: vec![0.0_f32; stride],
        }
    }

    /// Add (`sign = 1.0`) or remove (`sign = -1.0`) image row `y` from the
    /// per-column sums.
    fn accumulate_row(&mut self, image: &[f32], y: usize, sign: f64) {
        let row_base = y * self.width * 3;
        for idx in 0..(self.width * 3) {
            let v = image.get(row_base + idx).copied().unwrap_or(0.0) as f64;
            if let Some(slot) = self.col_sum.get_mut(idx) {
                *slot += sign * v;
            }
            if let Some(slot) = self.col_sum_sq.get_mut(idx) {
                *slot += sign * v * v;
            }
        }
    }

    /// Move the vertical window to the one centred on row `cy`, then run the
    /// horizontal sliding pass that fills `mean_row` / `std_row`.
    ///
    /// Rows are expected in increasing order (the natural `accumulate_taa`
    /// order); any other order falls back to rebuilding the column sums,
    /// which is still correct, just not incremental.
    pub(super) fn advance_to_row(&mut self, image: &[f32], cy: usize) {
        if self.width == 0 || self.height == 0 {
            return;
        }

        let new_y_min = cy.saturating_sub(self.radius);
        let new_y_max = (cy + self.radius + 1).min(self.height);

        if new_y_min < self.y_min || new_y_max < self.y_max {
            // Non-monotonic call order: rebuild from scratch.
            self.col_sum.iter_mut().for_each(|v| *v = 0.0);
            self.col_sum_sq.iter_mut().for_each(|v| *v = 0.0);
            self.y_min = new_y_min;
            self.y_max = new_y_min;
        }
        for y in self.y_max..new_y_max {
            self.accumulate_row(image, y, 1.0);
        }
        for y in self.y_min..new_y_min {
            self.accumulate_row(image, y, -1.0);
        }
        self.y_min = new_y_min;
        self.y_max = new_y_max;

        self.fill_row();
    }

    /// Horizontal sliding pass over the current column sums.
    fn fill_row(&mut self) {
        let v_count = self.y_max - self.y_min;
        let mut run_sum = [0.0_f64; 3];
        let mut run_sum_sq = [0.0_f64; 3];
        let mut cur_min = 0_usize;
        let mut cur_max = 0_usize;

        for cx in 0..self.width {
            let x_min = cx.saturating_sub(self.radius);
            let x_max = (cx + self.radius + 1).min(self.width);

            while cur_max < x_max {
                for (c, (sum, sum_sq)) in run_sum.iter_mut().zip(run_sum_sq.iter_mut()).enumerate()
                {
                    let idx = cur_max * 3 + c;
                    *sum += self.col_sum.get(idx).copied().unwrap_or(0.0);
                    *sum_sq += self.col_sum_sq.get(idx).copied().unwrap_or(0.0);
                }
                cur_max += 1;
            }
            while cur_min < x_min {
                for (c, (sum, sum_sq)) in run_sum.iter_mut().zip(run_sum_sq.iter_mut()).enumerate()
                {
                    let idx = cur_min * 3 + c;
                    *sum -= self.col_sum.get(idx).copied().unwrap_or(0.0);
                    *sum_sq -= self.col_sum_sq.get(idx).copied().unwrap_or(0.0);
                }
                cur_min += 1;
            }

            let count = v_count * (x_max - x_min);
            let out_base = cx * 3;
            if count == 0 {
                for c in 0..3 {
                    if let Some(slot) = self.mean_row.get_mut(out_base + c) {
                        *slot = 0.0;
                    }
                    if let Some(slot) = self.std_row.get_mut(out_base + c) {
                        *slot = 0.0;
                    }
                }
                continue;
            }

            let n = count as f64;
            for c in 0..3 {
                let mean = run_sum[c] / n;
                let variance = (run_sum_sq[c] / n) - (mean * mean);
                if let Some(slot) = self.mean_row.get_mut(out_base + c) {
                    *slot = mean as f32;
                }
                if let Some(slot) = self.std_row.get_mut(out_base + c) {
                    *slot = variance.max(0.0).sqrt() as f32;
                }
            }
        }
    }

    /// `(mean, std)` for pixel `cx` of the row last passed to
    /// [`Self::advance_to_row`]. Out-of-range columns yield zeros.
    pub(super) fn pixel(&self, cx: usize) -> ([f32; 3], [f32; 3]) {
        let base = cx * 3;
        let mut mean = [0.0_f32; 3];
        let mut std = [0.0_f32; 3];
        for c in 0..3 {
            mean[c] = self.mean_row.get(base + c).copied().unwrap_or(0.0);
            std[c] = self.std_row.get(base + c).copied().unwrap_or(0.0);
        }
        (mean, std)
    }
}

/// Clip `color` to the `±clip_sigma`-sigma range around `mean`.
///
/// For each channel independently, clamps `color[c]` to
/// `[mean[c] - clip_sigma * std[c], mean[c] + clip_sigma * std[c]]`.
///
/// # Parameters
///
/// - `color`: input pixel color to clip.
/// - `mean`: local neighborhood mean per channel.
/// - `std`: local neighborhood standard deviation per channel.
/// - `clip_sigma`: number of standard deviations to allow. Typical: `1.0` or `1.5`.
pub fn clip_to_variance(
    color: [f32; 3],
    mean: [f32; 3],
    std: [f32; 3],
    clip_sigma: f32,
) -> [f32; 3] {
    let mut out = [0.0_f32; 3];
    for c in 0..3 {
        let lo = mean[c] - clip_sigma * std[c];
        let hi = mean[c] + clip_sigma * std[c];
        out[c] = color[c].clamp(lo, hi);
    }
    out
}

// ---------------------------------------------------------------------------
// Luminance helper
// ---------------------------------------------------------------------------

/// Compute the luminance of an RGB color using standard Rec. 709 weights.
#[inline]
pub(super) fn luminance(rgb: [f32; 3]) -> f32 {
    0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]
}

/// Linear interpolation between `a` and `b` by factor `t` ∈ [0, 1].
#[inline]
pub(super) fn lerp(a: f32, b: f32, t: f32) -> f32 {
    a + t * (b - a)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::temporal_aa::{accumulate_taa, TaaConfig, TaaHistory};

    // ── local_color_stats ──────────────────────────────────────────────────────

    #[test]
    fn test_local_color_stats_uniform_std_zero() {
        // Uniform image: all 0.5, std should be 0.0
        let image = vec![0.5_f32; 4 * 4 * 3];
        let (mean, std) = local_color_stats(&image, 4, 4, 2, 2, 1);
        for c in 0..3 {
            assert!(
                (mean[c] - 0.5).abs() < 1e-5,
                "mean[{c}] expected 0.5, got {}",
                mean[c]
            );
            assert!(std[c] < 1e-5, "std[{c}] expected ~0.0, got {}", std[c]);
        }
    }

    #[test]
    fn test_local_color_stats_single_pixel_image() {
        let image = vec![0.3_f32, 0.6_f32, 0.9_f32];
        let (mean, std) = local_color_stats(&image, 1, 1, 0, 0, 1);
        assert!((mean[0] - 0.3).abs() < 1e-5);
        assert!((mean[1] - 0.6).abs() < 1e-5);
        assert!((mean[2] - 0.9).abs() < 1e-5);
        assert!(std[0] < 1e-5);
        assert!(std[1] < 1e-5);
        assert!(std[2] < 1e-5);
    }

    #[test]
    fn test_local_color_stats_corner_pixel() {
        // Corner pixel: window is smaller but should not panic
        let image = vec![0.5_f32; 6 * 6 * 3];
        let (mean, std) = local_color_stats(&image, 6, 6, 0, 0, 1);
        for c in 0..3 {
            assert!((mean[c] - 0.5).abs() < 1e-5);
            assert!(std[c] < 1e-5);
        }
    }

    // ── clip_to_variance ───────────────────────────────────────────────────────

    #[test]
    fn test_clip_within_range_unchanged() {
        let color = [0.5, 0.5, 0.5];
        let mean = [0.5, 0.5, 0.5];
        let std = [0.2, 0.2, 0.2];
        let out = clip_to_variance(color, mean, std, 1.0);
        for c in 0..3 {
            assert!(
                (out[c] - color[c]).abs() < 1e-6,
                "within range must be unchanged"
            );
        }
    }

    #[test]
    fn test_clip_above_range_clamped() {
        let color = [1.0, 1.0, 1.0];
        let mean = [0.5, 0.5, 0.5];
        let std = [0.1, 0.1, 0.1];
        let out = clip_to_variance(color, mean, std, 1.0);
        // expected upper bound: 0.5 + 1.0 * 0.1 = 0.6
        for &val in &out {
            assert!(
                (val - 0.6).abs() < 1e-5,
                "above range: expected 0.6, got {}",
                val
            );
        }
    }

    #[test]
    fn test_clip_below_range_clamped() {
        let color = [0.0, 0.0, 0.0];
        let mean = [0.5, 0.5, 0.5];
        let std = [0.1, 0.1, 0.1];
        let out = clip_to_variance(color, mean, std, 1.0);
        // expected lower bound: 0.5 - 1.0 * 0.1 = 0.4
        for &val in &out {
            assert!(
                (val - 0.4).abs() < 1e-5,
                "below range: expected 0.4, got {}",
                val
            );
        }
    }

    // ── RowWindowStats (sliding local mean/σ) ─────────────────────────────────

    /// Naive `f64` window reference with exactly the bounds
    /// `local_color_stats` documents. Used as the equivalence oracle for
    /// [`RowWindowStats`], because the public `f32` implementation loses
    /// precision in `E[v²] - E[v]²` on near-uniform patches and would make a
    /// tight comparison meaningless.
    fn local_color_stats_f64_reference(
        image: &[f32],
        width: usize,
        height: usize,
        cx: usize,
        cy: usize,
        radius: usize,
    ) -> ([f64; 3], [f64; 3]) {
        let y_min = cy.saturating_sub(radius);
        let y_max = (cy + radius + 1).min(height);
        let x_min = cx.saturating_sub(radius);
        let x_max = (cx + radius + 1).min(width);

        let mut sum = [0.0_f64; 3];
        let mut sum_sq = [0.0_f64; 3];
        let mut count = 0_usize;
        for ny in y_min..y_max {
            for nx in x_min..x_max {
                let base = (ny * width + nx) * 3;
                for (c, (s, sq)) in sum.iter_mut().zip(sum_sq.iter_mut()).enumerate() {
                    let v = image.get(base + c).copied().unwrap_or(0.0) as f64;
                    *s += v;
                    *sq += v * v;
                }
                count += 1;
            }
        }
        if count == 0 {
            return ([0.0; 3], [0.0; 3]);
        }
        let n = count as f64;
        let mut mean = [0.0_f64; 3];
        let mut std = [0.0_f64; 3];
        for c in 0..3 {
            mean[c] = sum[c] / n;
            std[c] = ((sum_sq[c] / n) - mean[c] * mean[c]).max(0.0).sqrt();
        }
        (mean, std)
    }

    /// Deterministic pseudo-random test image in `[0, 1]`.
    fn pseudo_image(width: usize, height: usize, seed: u64) -> Vec<f32> {
        let mut state = seed | 1;
        (0..(width * height * 3))
            .map(|_| {
                state ^= state << 13;
                state ^= state >> 7;
                state ^= state << 17;
                (state >> 40) as f32 / 16_777_216.0
            })
            .collect()
    }

    #[test]
    fn test_row_window_stats_matches_naive_f64_reference() {
        // Regression (F264): the sliding running-sum optimisation must
        // reproduce the exact windows of `local_color_stats`, including the
        // clipped (not clamp-replicated) borders. Bounds bookkeeping is where
        // sliding windows break, so sweep degenerate dimensions and radii
        // larger than the image.
        for &(w, h) in &[
            (1_usize, 1_usize),
            (1, 5),
            (5, 1),
            (2, 2),
            (7, 4),
            (4, 7),
            (9, 9),
        ] {
            let image = pseudo_image(w, h, 0x5EED_1234 ^ (w as u64) << 8 ^ h as u64);
            for radius in [0_usize, 1, 2, 3, 12] {
                let mut sliding = RowWindowStats::new(w, h, radius);
                for cy in 0..h {
                    sliding.advance_to_row(&image, cy);
                    for cx in 0..w {
                        let (mean, std) = sliding.pixel(cx);
                        let (ref_mean, ref_std) =
                            local_color_stats_f64_reference(&image, w, h, cx, cy, radius);
                        for c in 0..3 {
                            assert!(
                                (mean[c] as f64 - ref_mean[c]).abs() < 1e-6,
                                "mean drift at {w}x{h} r={radius} ({cx},{cy}) c={c}: \
                                 {} vs {}",
                                mean[c],
                                ref_mean[c]
                            );
                            assert!(
                                (std[c] as f64 - ref_std[c]).abs() < 1e-6,
                                "std drift at {w}x{h} r={radius} ({cx},{cy}) c={c}: \
                                 {} vs {}",
                                std[c],
                                ref_std[c]
                            );
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn test_row_window_stats_matches_public_local_color_stats() {
        // Looser tolerance than the f64 oracle above on purpose: the public
        // `local_color_stats` accumulates in f32, so `E[v²] - E[v]²` there
        // carries cancellation error the f64 sliding path does not.
        let (w, h) = (11_usize, 6_usize);
        let image = pseudo_image(w, h, 0xABCD_EF01);
        for radius in [1_usize, 2] {
            let mut sliding = RowWindowStats::new(w, h, radius);
            for cy in 0..h {
                sliding.advance_to_row(&image, cy);
                for cx in 0..w {
                    let (mean, std) = sliding.pixel(cx);
                    let (ref_mean, ref_std) = local_color_stats(&image, w, h, cx, cy, radius);
                    for c in 0..3 {
                        assert!(
                            (mean[c] - ref_mean[c]).abs() < 1e-5,
                            "mean mismatch r={radius} ({cx},{cy}) c={c}"
                        );
                        assert!(
                            (std[c] - ref_std[c]).abs() < 1e-4,
                            "std mismatch r={radius} ({cx},{cy}) c={c}"
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn test_row_window_stats_rebuilds_on_backwards_row() {
        // The incremental path assumes increasing rows; a backwards jump must
        // fall back to a rebuild rather than reporting a stale window.
        let (w, h) = (5_usize, 5_usize);
        let image = pseudo_image(w, h, 0x1357_9BDF);
        let mut sliding = RowWindowStats::new(w, h, 1);
        for cy in 0..h {
            sliding.advance_to_row(&image, cy);
        }
        sliding.advance_to_row(&image, 1);
        for cx in 0..w {
            let (mean, std) = sliding.pixel(cx);
            let (ref_mean, ref_std) = local_color_stats_f64_reference(&image, w, h, cx, 1, 1);
            for c in 0..3 {
                assert!(
                    (mean[c] as f64 - ref_mean[c]).abs() < 1e-6,
                    "stale mean after backwards row at cx={cx} c={c}"
                );
                assert!(
                    (std[c] as f64 - ref_std[c]).abs() < 1e-6,
                    "stale std after backwards row at cx={cx} c={c}"
                );
            }
        }
    }

    #[test]
    fn test_accumulate_taa_clipping_matches_per_pixel_reference() {
        // End-to-end: swapping `local_color_stats` for the sliding window
        // inside `accumulate_taa` must not change the blended image.
        let (w, h) = (9_usize, 5_usize);
        let frame0 = pseudo_image(w, h, 0x2468_ACE0);
        let frame1 = pseudo_image(w, h, 0x1122_3344);
        let config = TaaConfig {
            variance_clipping: true,
            clip_window_radius: 2,
            sharpen_strength: 0.0,
            blend_factor: 0.25,
            ..TaaConfig::default()
        };

        let mut history = TaaHistory::new(w, h);
        accumulate_taa(&frame0, &mut history, &config).expect("bootstrap");
        let got = accumulate_taa(&frame1, &mut history, &config).expect("blend");

        // Reference: the original per-pixel formulation.
        let mut expected = vec![0.0_f32; w * h * 3];
        for py in 0..h {
            for px in 0..w {
                let base = (py * w + px) * 3;
                let cur = [frame1[base], frame1[base + 1], frame1[base + 2]];
                let hist = [frame0[base], frame0[base + 1], frame0[base + 2]];
                let (mean, std) =
                    local_color_stats(&frame1, w, h, px, py, config.clip_window_radius);
                let clipped = clip_to_variance(hist, mean, std, 1.0);
                for c in 0..3 {
                    expected[base + c] =
                        config.blend_factor * cur[c] + (1.0 - config.blend_factor) * clipped[c];
                }
            }
        }

        for (i, (&a, &b)) in got.iter().zip(expected.iter()).enumerate() {
            assert!(
                (a - b).abs() < 1e-4,
                "pixel component {i}: sliding {a} vs per-pixel reference {b}"
            );
        }
    }
}