oxihuman-viewer 0.2.1

wgpu/WebGPU rendering adapter for OxiHuman
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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Post-processing effect pipeline descriptors (pure data, no GPU calls).

// ── Types ──────────────────────────────────────────────────────────────────

/// Screen-space bloom effect configuration.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct BloomConfig {
    pub enabled: bool,
    /// Luminance threshold above which bloom is applied.
    pub threshold: f32,
    /// Bloom intensity multiplier.
    pub intensity: f32,
    /// Blur radius factor.
    pub radius: f32,
}

impl Default for BloomConfig {
    fn default() -> Self {
        BloomConfig {
            enabled: false,
            threshold: 1.0,
            intensity: 0.3,
            radius: 1.0,
        }
    }
}

/// Screen-Space Ambient Occlusion configuration.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct SsaoConfig {
    pub enabled: bool,
    /// World-space hemisphere radius for occlusion sampling.
    pub radius: f32,
    /// Depth bias to avoid self-shadowing artifacts.
    pub bias: f32,
    /// Occlusion exponent (higher = darker shadows).
    pub power: f32,
    /// Number of SSAO sample kernel taps.
    pub sample_count: u32,
}

impl Default for SsaoConfig {
    fn default() -> Self {
        SsaoConfig {
            enabled: false,
            radius: 0.5,
            bias: 0.025,
            power: 1.0,
            sample_count: 16,
        }
    }
}

/// Supported tone-mapping operators.
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq)]
pub enum ToneMapMethod {
    Linear,
    Reinhard,
    AcesFilm,
    Filmic,
    Uncharted2,
}

/// Tone-mapping and gamma configuration.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ToneMappingConfig {
    pub method: ToneMapMethod,
    /// Linear exposure multiplier applied before tone mapping.
    pub exposure: f32,
    /// Output gamma (sRGB default is 2.2).
    pub gamma: f32,
}

impl Default for ToneMappingConfig {
    fn default() -> Self {
        ToneMappingConfig {
            method: ToneMapMethod::Reinhard,
            exposure: 1.0,
            gamma: 2.2,
        }
    }
}

/// Fast-approximate anti-aliasing configuration.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct FxaaConfig {
    pub enabled: bool,
    /// Sub-pixel quality dithering (0.0 = off, 0.75 = default).
    pub quality_subpix: f32,
    /// Minimum local contrast required to trigger FXAA (lower = more AA).
    pub quality_edge_threshold: f32,
}

impl Default for FxaaConfig {
    fn default() -> Self {
        FxaaConfig {
            enabled: false,
            quality_subpix: 0.75,
            quality_edge_threshold: 0.166,
        }
    }
}

/// Complete post-processing pipeline descriptor.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct PostProcessPipeline {
    pub bloom: BloomConfig,
    pub ssao: SsaoConfig,
    pub tone_mapping: ToneMappingConfig,
    pub fxaa: FxaaConfig,
    /// Screen-edge darkening strength: 0 = none, 1 = full black edges.
    pub vignette_strength: f32,
    /// Lateral chromatic aberration offset (0 = none).
    pub chromatic_aberration: f32,
}

impl Default for PostProcessPipeline {
    fn default() -> Self {
        PostProcessPipeline {
            bloom: BloomConfig::default(),
            ssao: SsaoConfig::default(),
            tone_mapping: ToneMappingConfig::default(),
            fxaa: FxaaConfig::default(),
            vignette_strength: 0.0,
            chromatic_aberration: 0.0,
        }
    }
}

impl PostProcessPipeline {
    /// High-quality preset: SSAO + bloom + FXAA enabled, ACES tone mapping.
    pub fn high_quality() -> Self {
        PostProcessPipeline {
            bloom: BloomConfig {
                enabled: true,
                threshold: 0.9,
                intensity: 0.4,
                radius: 1.2,
            },
            ssao: SsaoConfig {
                enabled: true,
                radius: 0.4,
                bias: 0.02,
                power: 1.5,
                sample_count: 32,
            },
            tone_mapping: ToneMappingConfig {
                method: ToneMapMethod::AcesFilm,
                exposure: 1.0,
                gamma: 2.2,
            },
            fxaa: FxaaConfig {
                enabled: true,
                quality_subpix: 0.75,
                quality_edge_threshold: 0.125,
            },
            vignette_strength: 0.0,
            chromatic_aberration: 0.0,
        }
    }

    /// Performance preset: minimal effects, no SSAO, FXAA only, Reinhard tone mapping.
    pub fn performance() -> Self {
        PostProcessPipeline {
            bloom: BloomConfig {
                enabled: false,
                ..BloomConfig::default()
            },
            ssao: SsaoConfig {
                enabled: false,
                ..SsaoConfig::default()
            },
            tone_mapping: ToneMappingConfig {
                method: ToneMapMethod::Reinhard,
                exposure: 1.0,
                gamma: 2.2,
            },
            fxaa: FxaaConfig {
                enabled: true,
                quality_subpix: 0.5,
                quality_edge_threshold: 0.25,
            },
            vignette_strength: 0.0,
            chromatic_aberration: 0.0,
        }
    }

    /// Cinematic preset: ACES + bloom + vignette + chromatic aberration.
    pub fn cinematic() -> Self {
        PostProcessPipeline {
            bloom: BloomConfig {
                enabled: true,
                threshold: 0.8,
                intensity: 0.6,
                radius: 1.5,
            },
            ssao: SsaoConfig {
                enabled: true,
                radius: 0.5,
                bias: 0.025,
                power: 2.0,
                sample_count: 64,
            },
            tone_mapping: ToneMappingConfig {
                method: ToneMapMethod::AcesFilm,
                exposure: 1.2,
                gamma: 2.2,
            },
            fxaa: FxaaConfig {
                enabled: true,
                quality_subpix: 0.75,
                quality_edge_threshold: 0.125,
            },
            vignette_strength: 0.35,
            chromatic_aberration: 0.003,
        }
    }

    /// Serialize to a compact JSON string.
    pub fn to_json(&self) -> String {
        let method_str = match self.tone_mapping.method {
            ToneMapMethod::Linear => "Linear",
            ToneMapMethod::Reinhard => "Reinhard",
            ToneMapMethod::AcesFilm => "AcesFilm",
            ToneMapMethod::Filmic => "Filmic",
            ToneMapMethod::Uncharted2 => "Uncharted2",
        };
        format!(
            r#"{{"bloom":{{"enabled":{},"threshold":{:.4},"intensity":{:.4},"radius":{:.4}}},"ssao":{{"enabled":{},"radius":{:.4},"bias":{:.4},"power":{:.4},"sample_count":{}}},"tone_mapping":{{"method":"{}","exposure":{:.4},"gamma":{:.4}}},"fxaa":{{"enabled":{},"quality_subpix":{:.4},"quality_edge_threshold":{:.4}}},"vignette_strength":{:.4},"chromatic_aberration":{:.6}}}"#,
            self.bloom.enabled,
            self.bloom.threshold,
            self.bloom.intensity,
            self.bloom.radius,
            self.ssao.enabled,
            self.ssao.radius,
            self.ssao.bias,
            self.ssao.power,
            self.ssao.sample_count,
            method_str,
            self.tone_mapping.exposure,
            self.tone_mapping.gamma,
            self.fxaa.enabled,
            self.fxaa.quality_subpix,
            self.fxaa.quality_edge_threshold,
            self.vignette_strength,
            self.chromatic_aberration,
        )
    }
}

// ── Tone-mapping functions ─────────────────────────────────────────────────

/// Reinhard tone operator: maps [0, ∞) → [0, 1).
#[inline]
pub fn tone_map_reinhard(x: f32) -> f32 {
    x / (1.0 + x)
}

/// Approximate ACES filmic tone mapping (Narkowicz 2015).
///
/// Numerically stable for very bright inputs.
#[inline]
pub fn tone_map_aces_approx(x: f32) -> f32 {
    let a = 2.51_f32;
    let b = 0.03_f32;
    let c = 2.43_f32;
    let d = 0.59_f32;
    let e = 0.14_f32;
    ((x * (a * x + b)) / (x * (c * x + d) + e)).clamp(0.0, 1.0)
}

/// Linear tone mapping with exposure and gamma correction.
///
/// `pow(x * exposure, 1 / gamma)`.  Returns 0 for negative inputs.
#[inline]
pub fn tone_map_linear(x: f32, exposure: f32, gamma: f32) -> f32 {
    let v = (x * exposure).max(0.0);
    if gamma <= 0.0 {
        return v;
    }
    v.powf(1.0 / gamma)
}

/// Rec. 709 / sRGB luminance: `0.2126 R + 0.7152 G + 0.0722 B`.
#[inline]
pub fn luminance(r: f32, g: f32, b: f32) -> f32 {
    0.2126 * r + 0.7152 * g + 0.0722 * b
}

/// Apply the configured tone-mapping operator per-channel and return the result.
///
/// The output may still exceed 1.0 for `Linear` with high exposure; callers
/// should clamp if writing to an 8-bit target.
pub fn apply_tone_map(color: [f32; 3], cfg: &ToneMappingConfig) -> [f32; 3] {
    match cfg.method {
        ToneMapMethod::Linear => [
            tone_map_linear(color[0], cfg.exposure, cfg.gamma),
            tone_map_linear(color[1], cfg.exposure, cfg.gamma),
            tone_map_linear(color[2], cfg.exposure, cfg.gamma),
        ],
        ToneMapMethod::Reinhard => {
            let ec = [
                color[0] * cfg.exposure,
                color[1] * cfg.exposure,
                color[2] * cfg.exposure,
            ];
            [
                tone_map_reinhard(ec[0]).max(0.0),
                tone_map_reinhard(ec[1]).max(0.0),
                tone_map_reinhard(ec[2]).max(0.0),
            ]
        }
        ToneMapMethod::AcesFilm => {
            let ec = [
                color[0] * cfg.exposure,
                color[1] * cfg.exposure,
                color[2] * cfg.exposure,
            ];
            [
                tone_map_aces_approx(ec[0]),
                tone_map_aces_approx(ec[1]),
                tone_map_aces_approx(ec[2]),
            ]
        }
        ToneMapMethod::Filmic => {
            // Simple filmic S-curve approximation
            let ec = [
                color[0] * cfg.exposure,
                color[1] * cfg.exposure,
                color[2] * cfg.exposure,
            ];
            [
                tone_map_reinhard(ec[0] * 1.6).max(0.0),
                tone_map_reinhard(ec[1] * 1.6).max(0.0),
                tone_map_reinhard(ec[2] * 1.6).max(0.0),
            ]
        }
        ToneMapMethod::Uncharted2 => {
            // Uncharted 2 "Hable" filmic curve
            fn hable(x: f32) -> f32 {
                let a = 0.15_f32;
                let b = 0.50_f32;
                let c = 0.10_f32;
                let d = 0.20_f32;
                let e = 0.02_f32;
                let f = 0.30_f32;
                (x * (a * x + c * b) + d * e) / (x * (a * x + b) + d * f) - e / f
            }
            let white = hable(11.2);
            let ec = [
                color[0] * cfg.exposure * 2.0,
                color[1] * cfg.exposure * 2.0,
                color[2] * cfg.exposure * 2.0,
            ];
            [
                (hable(ec[0]) / white).clamp(0.0, 1.0),
                (hable(ec[1]) / white).clamp(0.0, 1.0),
                (hable(ec[2]) / white).clamp(0.0, 1.0),
            ]
        }
    }
}

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

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

    // ── tone_map_reinhard ────────────────────────────────────────────────

    #[test]
    fn tone_map_reinhard_at_one() {
        let v = tone_map_reinhard(1.0);
        assert!((v - 0.5).abs() < 1e-6, "reinhard(1) should be 0.5, got {v}");
    }

    #[test]
    fn tone_map_reinhard_monotone() {
        assert!(tone_map_reinhard(2.0) > tone_map_reinhard(1.0));
        assert!(tone_map_reinhard(10.0) > tone_map_reinhard(2.0));
    }

    #[test]
    fn tone_map_reinhard_approaches_one() {
        let v = tone_map_reinhard(1_000_000.0);
        assert!(v < 1.0 + 1e-4 && v > 0.999);
    }

    // ── tone_map_aces_approx ─────────────────────────────────────────────

    #[test]
    fn tone_map_aces_stable_for_bright() {
        // Should not panic or overflow for very large inputs
        let v = tone_map_aces_approx(1_000.0);
        assert!(
            (0.0..=1.0).contains(&v),
            "ACES should clamp to [0,1], got {v}"
        );
    }

    #[test]
    fn tone_map_aces_zero_is_zero() {
        assert!(tone_map_aces_approx(0.0).abs() < 1e-4);
    }

    #[test]
    fn tone_map_aces_one_is_reasonable() {
        let v = tone_map_aces_approx(1.0);
        // At exposure=1, ACES(1.0) should be in a reasonable range
        assert!(v > 0.7 && v <= 1.0, "ACES(1.0) should be ~0.8+, got {v}");
    }

    // ── luminance ────────────────────────────────────────────────────────

    #[test]
    fn luminance_white_is_one() {
        let l = luminance(1.0, 1.0, 1.0);
        assert!(
            (l - 1.0).abs() < 1e-5,
            "luminance(1,1,1) should be 1.0, got {l}"
        );
    }

    #[test]
    fn luminance_black_is_zero() {
        assert!(luminance(0.0, 0.0, 0.0).abs() < 1e-6);
    }

    #[test]
    fn luminance_formula() {
        let l = luminance(1.0, 0.0, 0.0);
        assert!(
            (l - 0.2126).abs() < 1e-4,
            "expected 0.2126 for pure red, got {l}"
        );
    }

    #[test]
    fn luminance_green_heaviest() {
        let lr = luminance(1.0, 0.0, 0.0);
        let lg = luminance(0.0, 1.0, 0.0);
        let lb = luminance(0.0, 0.0, 1.0);
        assert!(lg > lr, "green should dominate luminance");
        assert!(lg > lb, "green should dominate luminance over blue");
    }

    // ── apply_tone_map ───────────────────────────────────────────────────

    #[test]
    fn apply_tone_map_non_negative_output() {
        let cfg = ToneMappingConfig::default();
        let out = apply_tone_map([0.5, 1.0, 2.0], &cfg);
        for ch in out {
            assert!(ch >= 0.0, "output channel must be non-negative, got {ch}");
        }
    }

    #[test]
    fn apply_tone_map_aces_clamps() {
        let cfg = ToneMappingConfig {
            method: ToneMapMethod::AcesFilm,
            exposure: 1.0,
            gamma: 2.2,
        };
        let out = apply_tone_map([1000.0, 1000.0, 1000.0], &cfg);
        for ch in out {
            assert!(ch <= 1.0 + 1e-4, "ACES output must be ≤ 1.0, got {ch}");
        }
    }

    // ── PostProcessPipeline presets ──────────────────────────────────────

    #[test]
    fn high_quality_ssao_enabled() {
        assert!(PostProcessPipeline::high_quality().ssao.enabled);
    }

    #[test]
    fn high_quality_fxaa_enabled() {
        assert!(PostProcessPipeline::high_quality().fxaa.enabled);
    }

    #[test]
    fn high_quality_bloom_enabled() {
        assert!(PostProcessPipeline::high_quality().bloom.enabled);
    }

    #[test]
    fn performance_ssao_disabled() {
        assert!(!PostProcessPipeline::performance().ssao.enabled);
    }

    #[test]
    fn performance_bloom_disabled() {
        assert!(!PostProcessPipeline::performance().bloom.enabled);
    }

    #[test]
    fn cinematic_vignette_positive() {
        assert!(
            PostProcessPipeline::cinematic().vignette_strength > 0.0,
            "cinematic preset should have vignette"
        );
    }

    #[test]
    fn cinematic_chromatic_aberration_nonzero() {
        assert!(PostProcessPipeline::cinematic().chromatic_aberration > 0.0);
    }

    // ── Default values ────────────────────────────────────────────────────

    #[test]
    fn default_bloom_threshold_is_one() {
        let cfg = BloomConfig::default();
        assert!((cfg.threshold - 1.0).abs() < 1e-6);
    }

    #[test]
    fn all_presets_have_valid_gamma() {
        let presets = [
            PostProcessPipeline::default(),
            PostProcessPipeline::high_quality(),
            PostProcessPipeline::performance(),
            PostProcessPipeline::cinematic(),
        ];
        for p in &presets {
            assert!(
                p.tone_mapping.gamma > 0.0,
                "gamma must be positive, got {}",
                p.tone_mapping.gamma
            );
        }
    }

    // ── to_json ───────────────────────────────────────────────────────────

    #[test]
    fn to_json_non_empty() {
        let json = PostProcessPipeline::default().to_json();
        assert!(!json.is_empty());
    }

    #[test]
    fn to_json_contains_bloom_key() {
        let json = PostProcessPipeline::high_quality().to_json();
        assert!(json.contains("\"bloom\""), "JSON should contain 'bloom'");
    }

    #[test]
    fn to_json_contains_tone_mapping() {
        let json = PostProcessPipeline::cinematic().to_json();
        assert!(
            json.contains("AcesFilm"),
            "cinematic JSON should mention AcesFilm"
        );
    }
}