three-rs 0.3.0

A port of three.js core and its WebGPURenderer to Rust on wgpu, graded against three.js's own end-to-end pixel comparison.
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//! Port of `examples/jsm/tsl/display/ImportanceSampledEnvironment.js`: HDR
//! environment lookups for screen-space effects, with luminance importance
//! sampling (CDF tables + MIS) for the rays SSR could not resolve on screen.
//!
//! [`EnvMapCdfGenerator`] is three's `EnvMapCDFGenerator`: it converts an
//! equirectangular environment map to a half-float copy (`preprocessEnvMap`)
//! and, for importance sampling, builds the marginal and conditional
//! inverse-CDF tables into `r16float` `DataTexture`s on the CPU.
//! [`ImportanceSampledEnvironment`] owns one, plus the `totalSum`, `size` and
//! `intensity` uniforms, and builds the three lookups three exposes:
//! [`sample_reflect`](ImportanceSampledEnvironment::sample_reflect),
//! [`sample_environment_brdf`](ImportanceSampledEnvironment::sample_environment_brdf)
//! and [`sample_environment_mis`](ImportanceSampledEnvironment::sample_environment_mis).
//!
//! The WGSL of all three is gated against three's dump of
//! `tools/dump-pages/specular_helpers.html` in `tests/nodes_display_wgsl.rs`,
//! and the CPU tables by this module's unit test (`docs/nodes.md` ยง85).
//!
//! **Divergence, texture identity.** Three's `updateFrom()` builds new
//! textures every time and points the existing texture nodes at them
//! (`this._mapNode.value = map`). A texture node here holds its [`Texture`]
//! handle and has no `value` to reassign, so a later `update_from` with an
//! environment of the same size writes the new pixels into the textures the
//! graphs already sample; one of a different size allocates new textures,
//! which only graphs built after it read.

use crate::extras::{from_half_float, to_half_float};
use crate::nodes::node::{NodeRef, SettableValue, Type};
use crate::nodes::tsl::{
    d_gtr, dot, equirect_dir_pdf, equirect_uv, f_schlick, float, geometry_term, if_then, luminance,
    max, mis_power_heuristic, smith_g, texture_level, texture_with_uv, to_var, uniform_settable,
    vec2_join, vec4_join,
};
use crate::textures::{MinFilter, Texture, TextureFilter, Wrapping};

/// `colorToLuminance( r, g, b )` โ€” the Rec. 709 weights the CDF uses.
fn color_to_luminance(r: f64, g: f64, b: f64) -> f64 {
    0.2126 * r + 0.7152 * g + 0.0722 * b
}

/// `binarySearchFindClosestIndexOf( array, targetValue, offset, count )` โ€”
/// the first index in `array[ offset .. offset + count ]` whose value is not
/// below `target_value` (the last one if none is), relative to `offset`. The
/// values are `Float32Array` entries compared against a JS number, hence the
/// widening.
fn binary_search_find_closest_index_of(
    array: &[f32],
    target_value: f64,
    offset: usize,
    count: usize,
) -> usize {
    let mut lower = offset;
    let mut upper = offset + count - 1;
    while lower < upper {
        let mid = (lower + upper) >> 1;
        if (array[mid] as f64) < target_value {
            lower = mid + 1;
        } else {
            upper = mid;
        }
    }
    lower - offset
}

/// `preprocessEnvMap( envMap )` โ€” a clone of the map whose data is RGBA
/// half floats with no Y flip: float data is converted as is, normalized
/// 8-bit data divided by 255, and a `flipY` map has its rows reversed.
fn preprocess_env_map(env_map: &Texture) -> Texture {
    let map = env_map.clone_texture();
    let (width, _) = env_map.size();
    let (format, flip_y, bytes) = {
        let inner = env_map.borrow();
        let bytes = inner.data.clone().expect(
            "three-rs: ImportanceSampledEnvironment needs an environment map with CPU \
             pixel data (a DataTexture or a loaded HDR), not a render target",
        );
        (inner.format, inner.flip_y, bytes)
    };
    let mut halves: Vec<u16> = match format {
        wgpu::TextureFormat::Rgba16Float => bytemuck::pod_collect_to_vec(&bytes),
        wgpu::TextureFormat::Rgba32Float => bytemuck::pod_collect_to_vec::<u8, f32>(&bytes)
            .into_iter()
            .map(|v| to_half_float(v as f64))
            .collect(),
        wgpu::TextureFormat::Rgba8Unorm | wgpu::TextureFormat::Rgba8UnormSrgb => bytes
            .iter()
            .map(|&v| to_half_float(v as f64 / 255.0))
            .collect(),
        other => panic!(
            "three-rs: ImportanceSampledEnvironment reads RGBA float, half-float or 8-bit \
             environment maps, not {other:?}"
        ),
    };
    if flip_y {
        let stride = 4 * width as usize;
        let flipped: Vec<u16> = halves
            .chunks_exact(stride)
            .rev()
            .flatten()
            .copied()
            .collect();
        halves = flipped;
    }
    {
        let mut inner = map.borrow_mut_inner();
        inner.format = wgpu::TextureFormat::Rgba16Float;
        inner.flip_y = false;
        inner.data = Some(bytemuck::cast_slice(&halves).to_vec());
        inner.version += 1;
    }
    map
}

/// The CPU half of `EnvMapCDFGenerator.updateFrom()`: the half-float bit
/// patterns of the marginal (`height` texels) and conditional (`width ร—
/// height`) inverse-CDF tables, and the summed luminance.
#[derive(Debug, PartialEq)]
struct CdfTables {
    marginal: Vec<u16>,
    conditional: Vec<u16>,
    total_sum: f64,
}

/// The table arithmetic of `EnvMapCDFGenerator.updateFrom()` over RGBA half
/// floats, `Float32Array` storage included. Three also fills the two PDF
/// arrays, which nothing reads; they are left out.
fn cdf_tables(width: usize, height: usize, data: &[u16]) -> CdfTables {
    let mut cdf_conditional = vec![0.0f32; width * height];
    let mut cdf_marginal = vec![0.0f32; height];
    let mut total_sum = 0.0f64;
    let mut cumulative_weight_marginal = 0.0f64;

    for y in 0..height {
        let mut cumulative_row_weight = 0.0f64;
        for x in 0..width {
            let i = y * width + x;
            let r = from_half_float(data[4 * i]) as f64;
            let g = from_half_float(data[4 * i + 1]) as f64;
            let b = from_half_float(data[4 * i + 2]) as f64;
            let weight = color_to_luminance(r, g, b);
            cumulative_row_weight += weight;
            total_sum += weight;
            cdf_conditional[i] = cumulative_row_weight as f32;
        }
        if cumulative_row_weight != 0.0 {
            for value in &mut cdf_conditional[y * width..(y + 1) * width] {
                *value = (*value as f64 / cumulative_row_weight) as f32;
            }
        }
        cumulative_weight_marginal += cumulative_row_weight;
        cdf_marginal[y] = cumulative_weight_marginal as f32;
    }
    if cumulative_weight_marginal != 0.0 {
        for value in &mut cdf_marginal {
            *value = (*value as f64 / cumulative_weight_marginal) as f32;
        }
    }

    let marginal = (0..height)
        .map(|i| {
            let dist = (i + 1) as f64 / height as f64;
            let row = binary_search_find_closest_index_of(&cdf_marginal, dist, 0, height);
            to_half_float((row as f64 + 0.5) / height as f64)
        })
        .collect();
    let mut conditional = Vec::with_capacity(width * height);
    for y in 0..height {
        for x in 0..width {
            let dist = (x + 1) as f64 / width as f64;
            let col = binary_search_find_closest_index_of(&cdf_conditional, dist, y * width, width);
            conditional.push(to_half_float((col as f64 + 0.5) / width as f64));
        }
    }
    CdfTables {
        marginal,
        conditional,
        total_sum,
    }
}

/// One CDF table as three builds it: a `RedFormat` + `HalfFloatType`
/// `DataTexture` with `LinearFilter` on both sides, clamped on both axes and
/// no mipmaps.
fn cdf_texture(width: u32, height: u32, data: &[u16]) -> Texture {
    let texture = Texture::data_r16float(width, height, data);
    texture.set_min_filter(MinFilter::Linear);
    texture.set_mag_filter(TextureFilter::Linear);
    texture.set_wrapping(Wrapping::ClampToEdge, Wrapping::ClampToEdge);
    texture.set_generate_mipmaps(false);
    texture
}

/// Puts `fresh` in `slot`: written into the texture already there when it has
/// the same size and format (so graphs that sample it see the new pixels, as
/// three's `node.value = texture` makes them), or in place of it otherwise.
fn install(slot: &mut Option<Texture>, fresh: Texture) {
    if let Some(current) = slot {
        if current.size() == fresh.size() && current.format() == fresh.format() {
            let source = fresh.borrow();
            let mut target = current.borrow_mut_inner();
            target.data = source.data.clone();
            target.color_space = source.color_space;
            target.flip_y = source.flip_y;
            target.generate_mipmaps = source.generate_mipmaps;
            target.wrap_s = source.wrap_s;
            target.wrap_t = source.wrap_t;
            target.mag_filter = source.mag_filter;
            target.min_filter = source.min_filter;
            target.anisotropy = source.anisotropy;
            target.version += 1;
            return;
        }
    }
    *slot = Some(fresh);
}

/// `EnvMapCDFGenerator` โ€” the preprocessed environment map and its marginal
/// and conditional inverse-CDF textures for luminance importance sampling.
///
/// The marginal table is `height ร— 1` and holds, for each `( i + 1 ) / height`
/// quantile, the centre `v` of the row the marginal CDF reaches it in; row `y`
/// of the `width ร— height` conditional table holds the same per column within
/// row `y`. Both are `r16float` with linear filtering and clamped edges; the
/// map is RGBA half float, repeating in `s` and clamped in `t`. Three's code
/// weights each texel by its luminance alone โ€” there is no `sin ฮธ` term.
#[derive(Default)]
pub struct EnvMapCdfGenerator {
    map: Option<Texture>,
    marginal_weights: Option<Texture>,
    conditional_weights: Option<Texture>,
    total_sum: f64,
}

impl EnvMapCdfGenerator {
    /// `new EnvMapCDFGenerator()` โ€” no map and no tables yet.
    pub fn new() -> Self {
        Self::default()
    }

    /// `updateFrom( hdr )` โ€” preprocesses the map and builds both CDF tables
    /// and the luminance total from it.
    pub fn update_from(&mut self, hdr: &Texture) {
        self.update_map_only(hdr);
        let map = self.map.as_ref().expect("the map was set just above");
        let (width, height) = map.size();
        let halves: Vec<u16> = bytemuck::pod_collect_to_vec(
            map.borrow()
                .data
                .as_ref()
                .expect("a preprocessed map has data"),
        );
        let tables = cdf_tables(width as usize, height as usize, &halves);
        install(
            &mut self.marginal_weights,
            cdf_texture(height, 1, &tables.marginal),
        );
        install(
            &mut self.conditional_weights,
            cdf_texture(width, height, &tables.conditional),
        );
        self.total_sum = tables.total_sum;
    }

    /// `updateMapOnly( hdr )` โ€” the preprocessed map alone (repeating in `s`,
    /// clamped in `t`), with the luminance total reset to 0.
    pub fn update_map_only(&mut self, hdr: &Texture) {
        let map = preprocess_env_map(hdr);
        map.set_wrapping(Wrapping::Repeat, Wrapping::ClampToEdge);
        install(&mut self.map, map);
        self.total_sum = 0.0;
    }

    /// `generator.map` โ€” the preprocessed RGBA half-float environment.
    pub fn map(&self) -> Option<&Texture> {
        self.map.as_ref()
    }

    /// `generator.marginalWeights` โ€” the `height ร— 1` marginal table.
    pub fn marginal_weights(&self) -> Option<&Texture> {
        self.marginal_weights.as_ref()
    }

    /// `generator.conditionalWeights` โ€” the `width ร— height` conditional table.
    pub fn conditional_weights(&self) -> Option<&Texture> {
        self.conditional_weights.as_ref()
    }

    /// `generator.totalSum` โ€” the summed luminance of every texel.
    pub fn total_sum(&self) -> f64 {
        self.total_sum
    }

    /// `dispose()` โ€” drops the map and the tables. A texture's GPU copy goes
    /// with its last handle, so graphs that still sample one keep it alive.
    pub fn dispose(&mut self) {
        self.marginal_weights = None;
        self.conditional_weights = None;
        self.map = None;
    }
}

/// The lobe [`ImportanceSampledEnvironment::sample_environment_brdf`] and
/// [`sample_environment_mis`](ImportanceSampledEnvironment::sample_environment_mis)
/// evaluate โ€” the object three passes them.
#[derive(Clone)]
pub struct EnvironmentLobe {
    /// `cameraWorldMatrix` โ€” the `mat4` uniform taking view space to world.
    pub camera_world_matrix: NodeRef,
    /// `viewReflectDir` โ€” the view-space GGX-sampled reflected ray.
    pub view_reflect_dir: NodeRef,
    /// `N` โ€” the view-space shading normal.
    pub n: NodeRef,
    /// `V` โ€” the view-space direction to the camera.
    pub v: NodeRef,
    /// `alpha` โ€” the GGX roughness ฮฑ.
    pub alpha: NodeRef,
    /// `f0` โ€” the Fresnel reflectance at normal incidence (`vec3`).
    pub f0: NodeRef,
}

/// `ImportanceSampledEnvironment` โ€” a preprocessed HDR environment (CDF
/// textures and uniforms) and the TSL lookups SSR's environment fallback
/// uses. See the module docs.
pub struct ImportanceSampledEnvironment {
    importance_sampling: bool,
    cdf: EnvMapCdfGenerator,
    total_sum_node: NodeRef,
    total_sum: SettableValue,
    size_node: NodeRef,
    size: SettableValue,
    intensity_node: NodeRef,
    intensity: SettableValue,
}

impl ImportanceSampledEnvironment {
    /// `new ImportanceSampledEnvironment( importanceSampling )` โ€” with
    /// `importance_sampling`, [`update_from`](Self::update_from) also builds
    /// the luminance CDF tables
    /// [`sample_environment_mis`](Self::sample_environment_mis) reads.
    pub fn new(importance_sampling: bool) -> Self {
        let (total_sum_node, total_sum) = uniform_settable(Type::F32, vec![0.0]);
        let (size_node, size) = uniform_settable(Type::Vec2, vec![1.0, 1.0]);
        let (intensity_node, intensity) = uniform_settable(Type::F32, vec![1.0]);
        Self {
            importance_sampling,
            cdf: EnvMapCdfGenerator::new(),
            total_sum_node,
            total_sum,
            size_node,
            size,
            intensity_node,
            intensity,
        }
    }

    /// `updateFrom( hdr )` โ€” takes an equirectangular HDR environment map
    /// (RGBA float, half float or 8-bit, with CPU pixel data).
    pub fn update_from(&mut self, hdr: &Texture) {
        if self.importance_sampling {
            self.cdf.update_from(hdr);
            self.total_sum.set(vec![self.cdf.total_sum()]);
        } else {
            self.cdf.update_map_only(hdr);
        }
        let (width, height) = self.cdf.map().expect("update_from sets the map").size();
        self.size.set(vec![width as f64, height as f64]);
    }

    /// `clear()` โ€” drops the environment and resets the uniforms; the
    /// lookups need another [`update_from`](Self::update_from) before they
    /// can be built again.
    pub fn clear(&mut self) {
        self.dispose();
        self.cdf = EnvMapCdfGenerator::new();
        self.total_sum.set(vec![0.0]);
        self.size.set(vec![1.0, 1.0]);
    }

    /// `this.intensity.value` โ€” the radiance scale every lookup applies. 1 by
    /// default.
    pub fn intensity(&self) -> &SettableValue {
        &self.intensity
    }

    /// The CDF generator, for its textures and luminance total.
    pub fn generator(&self) -> &EnvMapCdfGenerator {
        &self.cdf
    }

    /// `dispose()`.
    pub fn dispose(&mut self) {
        self.cdf.dispose();
    }

    fn map(&self) -> &Texture {
        self.cdf.map().expect(
            "three-rs: ImportanceSampledEnvironment has no environment; call update_from first",
        )
    }

    /// `texture( this._mapNode, uv ).level( 0 ).rgb` โ€” explicit LOD 0, since
    /// the equirect seam and poles would pull derivative-driven mip selection
    /// to the coarsest level.
    fn map_level_0(&self, uv: NodeRef) -> NodeRef {
        texture_level(self.map(), uv, float(0.0)).rgb()
    }

    /// `sampleReflect( { cameraWorldMatrix, viewReflectDir, sampleWeight } )` โ€”
    /// the environment along the reflected direction, no MIS.
    /// `sample_weight` defaults to `float( 1 )`.
    pub fn sample_reflect(
        &self,
        camera_world_matrix: &NodeRef,
        view_reflect_dir: NodeRef,
        sample_weight: Option<NodeRef>,
    ) -> NodeRef {
        let sample_weight = sample_weight.unwrap_or_else(|| float(1.0));
        let world_reflect_dir = camera_world_matrix
            .mul(vec4_join(vec![view_reflect_dir, float(0.0)]))
            .xyz()
            .normalize();
        let env_uv = equirect_uv(world_reflect_dir);
        self.map_level_0(env_uv)
            .mul(self.intensity_node.clone())
            .mul(sample_weight)
    }

    /// `sampleEnvironmentBRDF( lobe )` โ€” the environment for a screen-space
    /// miss along the BRDF-sampled ray only, weighted by `F ยท G2 / G1( NยทV )`.
    #[inline(never)]
    pub fn sample_environment_brdf(&self, lobe: &EnvironmentLobe) -> NodeRef {
        let m = &lobe.camera_world_matrix;
        let world_normal = to_var(
            None,
            m.mul(vec4_join(vec![lobe.n.clone(), float(0.0)]))
                .xyz()
                .normalize(),
        );
        let world_v = to_var(
            None,
            m.mul(vec4_join(vec![lobe.v.clone(), float(0.0)]))
                .xyz()
                .normalize(),
        );
        let n_dot_v = to_var(
            None,
            max(float(0.0), dot(world_normal.clone(), world_v.clone())),
        );
        let l1 = to_var(
            None,
            m.mul(vec4_join(vec![lobe.view_reflect_dir.clone(), float(0.0)]))
                .xyz()
                .normalize(),
        );
        let brdf_env_color = self.map_level_0(equirect_uv(l1.clone()));
        let h1 = to_var(None, world_v.add(l1.clone()).normalize());
        let n_dot_l1 = to_var(None, max(float(0.0), dot(world_normal, l1)));
        let v_dot_h1 = to_var(None, max(float(0.0), dot(world_v, h1)));
        let w1 = brdf_weight(lobe, &n_dot_l1, &n_dot_v, &v_dot_h1);
        brdf_env_color.mul(w1).mul(self.intensity_node.clone())
    }

    /// `sampleEnvironmentMIS( { โ€ฆlobe, Xi2 } )` โ€” the environment for a
    /// screen-space miss, estimated with multiple importance sampling between
    /// the BRDF-sampled ray and a direction drawn from the luminance CDF with
    /// `xi2.zw`. The CDF sample is skipped for near-mirror lobes (`alpha โ‰ค
    /// 0.01`). Needs `importance_sampling`.
    #[inline(never)]
    pub fn sample_environment_mis(&self, lobe: &EnvironmentLobe, xi2: NodeRef) -> NodeRef {
        let marginal = self.cdf.marginal_weights().expect(
            "three-rs: sample_environment_mis needs ImportanceSampledEnvironment::new( true ) \
             and an update_from",
        );
        let conditional = self
            .cdf
            .conditional_weights()
            .expect("the conditional table is built with the marginal one");
        let env_w = self.size_node.x();
        let env_h = self.size_node.y();
        let total_sum = self.total_sum_node.clone();
        let alpha = lobe.alpha.clone();
        let m = &lobe.camera_world_matrix;

        let world_normal = to_var(
            None,
            m.mul(vec4_join(vec![lobe.n.clone(), float(0.0)]))
                .xyz()
                .normalize(),
        );
        let world_v = to_var(
            None,
            m.mul(vec4_join(vec![lobe.v.clone(), float(0.0)]))
                .xyz()
                .normalize(),
        );
        let n_dot_v = to_var(
            None,
            max(float(0.0), dot(world_normal.clone(), world_v.clone())),
        );

        // MIS sample 1: the BRDF / reflected-ray direction.
        let l1 = to_var(
            None,
            m.mul(vec4_join(vec![lobe.view_reflect_dir.clone(), float(0.0)]))
                .xyz()
                .normalize(),
        );
        let brdf_env_color = self.map_level_0(equirect_uv(l1.clone()));
        let h1 = to_var(None, world_v.add(l1.clone()).normalize());
        let n_dot_l1 = to_var(None, max(float(0.0), dot(world_normal.clone(), l1.clone())));
        let n_dot_h1 = to_var(None, max(float(0.0), dot(world_normal.clone(), h1.clone())));
        let v_dot_h1 = to_var(None, max(float(0.0), dot(world_v.clone(), h1)));

        let pdf_brdf1 = d_gtr(alpha.clone(), n_dot_h1, float(2.0))
            .mul(smith_g(n_dot_v.clone(), alpha.clone()))
            .div(max(float(1e-6), float(4.0).mul(n_dot_v.clone())))
            .max(float(1e-8));
        let pdf_env1 = env_w
            .mul(env_h.clone())
            .mul(luminance(brdf_env_color.clone()).div(total_sum.clone()))
            .mul(equirect_dir_pdf(l1))
            .max(float(1e-8));
        let w1 = mis_power_heuristic(pdf_brdf1, pdf_env1);
        let weight1 = brdf_weight(lobe, &n_dot_l1, &n_dot_v, &v_dot_h1);
        let result = to_var(None, brdf_env_color.mul(weight1).mul(w1));

        // MIS sample 2: the env-luminance CDF direction.
        let r_env = vec2_join(vec![xi2.z(), xi2.w()]);
        let v_cdf = texture_with_uv(marginal, vec2_join(vec![r_env.x(), float(0.0)])).x();
        let u_cdf = texture_with_uv(conditional, vec2_join(vec![r_env.y(), v_cdf.clone()])).x();
        let is_env_uv = vec2_join(vec![u_cdf, v_cdf]);
        // Three passes the `vec2` UV to `equirectUV()`, which reads `.z` off
        // it, and then uses the `vec2` result as a direction; the port keeps
        // both, so the widening `vec3( v, 0.0 )`s are three's.
        let env_dir_ws = equirect_uv(is_env_uv.clone());
        let env_half = world_v.add(env_dir_ws.clone()).normalize();
        let env_n_dot_l = max(float(0.0), dot(world_normal.clone(), env_dir_ws.clone()));
        let env_n_dot_h = max(float(0.0), dot(world_normal, env_half.clone()));
        let env_v_dot_h = max(float(0.0), dot(world_v, env_half));

        let inner = self.mis_env_sample(
            lobe,
            &result,
            &n_dot_v,
            is_env_uv,
            env_dir_ws,
            env_n_dot_l.clone(),
            env_n_dot_h,
            env_v_dot_h,
        );
        let cdf_branch = if_then(
            alpha.greater_than(float(0.01)),
            vec![if_then(env_n_dot_l.greater_than(float(0.001)), inner)],
        );

        crate::nodes::tsl::block(
            vec![result.clone(), cdf_branch],
            result.mul(self.intensity_node.clone()),
        )
    }

    /// The body of `sampleEnvironmentMIS()`'s inner `If( envNdotL > 0.001 )`:
    /// the CDF sample's contribution, added to `result`.
    #[allow(clippy::too_many_arguments)]
    #[inline(never)]
    fn mis_env_sample(
        &self,
        lobe: &EnvironmentLobe,
        result: &NodeRef,
        n_dot_v: &NodeRef,
        is_env_uv: NodeRef,
        env_dir_ws: NodeRef,
        env_n_dot_l: NodeRef,
        env_n_dot_h: NodeRef,
        env_v_dot_h: NodeRef,
    ) -> Vec<NodeRef> {
        let alpha = lobe.alpha.clone();
        let env_w = self.size_node.x();
        let env_h = self.size_node.y();
        // The GGX D both the BRDF pdf and the specular BRDF use.
        let d = to_var(None, d_gtr(alpha.clone(), env_n_dot_h, float(2.0)));
        let sampled_color = self.map_level_0(is_env_uv);
        let pdf_env2 = env_w
            .mul(env_h)
            .mul(luminance(sampled_color.clone()).div(self.total_sum_node.clone()))
            .mul(equirect_dir_pdf(env_dir_ws))
            .max(float(1e-8));
        let pdf_brdf2 = d
            .mul(smith_g(n_dot_v.clone(), alpha.clone()))
            .div(max(float(1e-6), float(4.0).mul(n_dot_v.clone())))
            .max(float(1e-8));
        let w2 = mis_power_heuristic(pdf_env2.clone(), pdf_brdf2);
        let env_brdf_spec = d
            .mul(geometry_term(env_n_dot_l.clone(), n_dot_v.clone(), alpha))
            .div(max(
                float(1e-6),
                float(4.0).mul(env_n_dot_l.clone()).mul(n_dot_v.clone()),
            ));
        let env_fresnel_weight = f_schlick(lobe.f0.clone(), env_v_dot_h);
        vec![
            d.clone(),
            result.add_assign(
                sampled_color
                    .mul(env_brdf_spec)
                    .mul(env_fresnel_weight)
                    .mul(env_n_dot_l)
                    .div(pdf_env2)
                    .mul(w2),
            ),
        ]
    }
}

/// `F_Schlick( f0, VdotH1 ).mul( GeometryTerm( NdotL1, NdotV, alpha ) ).div(
/// SmithG( NdotV, alpha ).max( 1e-4 ) )` โ€” the BRDF sample's Monte-Carlo
/// weight `F ยท G1( NยทL )`, with GGX D cancelled analytically.
fn brdf_weight(
    lobe: &EnvironmentLobe,
    n_dot_l1: &NodeRef,
    n_dot_v: &NodeRef,
    v_dot_h1: &NodeRef,
) -> NodeRef {
    f_schlick(lobe.f0.clone(), v_dot_h1.clone())
        .mul(geometry_term(
            n_dot_l1.clone(),
            n_dot_v.clone(),
            lobe.alpha.clone(),
        ))
        .div(smith_g(n_dot_v.clone(), lobe.alpha.clone()).max(float(1e-4)))
}

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

    /// A 4ร—2 RGBA float environment from per-texel `( r, g, b )`.
    fn env(texels: &[[f32; 3]; 8]) -> Texture {
        let data: Vec<f32> = texels
            .iter()
            .flat_map(|&[r, g, b]| [r, g, b, 1.0])
            .collect();
        Texture::data_rgba32float(4, 2, &data)
    }

    fn halves(texture: &Texture) -> Vec<f32> {
        let data = texture.borrow().data.clone().unwrap();
        bytemuck::pod_collect_to_vec::<u8, u16>(&data)
            .into_iter()
            .map(from_half_float)
            .collect()
    }

    // Luminance is 0.2126 r + 0.7152 g + 0.0722 b; a grey texel ( v, v, v )
    // weighs v (to within an ulp), and every value below is exact in half
    // precision, so the preprocessing changes nothing.
    //
    // Row 0: grey 1, 2, 3, 4      weights 1, 2, 3, 4      row sum 10
    //        conditional CDF      0.1, 0.3, 0.6, 1.0
    // Row 1: black, black, grey 5, pure green 4
    //                             weights 0, 0, 5, 0.7152ยท4 = 2.8608
    //                             row sum 7.8608
    //        conditional CDF      0, 0, 5/7.8608 = 0.63607, 1.0
    // Marginal: row sums 10, 7.8608, total 17.8608
    //        marginal CDF         10/17.8608 = 0.55988, 1.0
    //
    // Conditional table, row y, column x: the first column whose CDF is at
    // least ( x + 1 ) / 4, as ( col + 0.5 ) / 4:
    //   row 0: 0.25 โ†’ col 1 (0.3), 0.5 โ†’ col 2 (0.6), 0.75 โ†’ col 3, 1.0 โ†’ col 3
    //          = 0.375, 0.625, 0.875, 0.875
    //   row 1: 0.25 โ†’ col 2 (0.636), 0.5 โ†’ col 2, 0.75 โ†’ col 3, 1.0 โ†’ col 3
    //          = 0.625, 0.625, 0.875, 0.875
    // Marginal table, texel i: the first row whose CDF is at least
    // ( i + 1 ) / 2, as ( row + 0.5 ) / 2:
    //   0.5 โ†’ row 0 (0.55988), 1.0 โ†’ row 1  = 0.25, 0.75
    const TEXELS: [[f32; 3]; 8] = [
        [1.0, 1.0, 1.0],
        [2.0, 2.0, 2.0],
        [3.0, 3.0, 3.0],
        [4.0, 4.0, 4.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [5.0, 5.0, 5.0],
        [0.0, 4.0, 0.0],
    ];

    #[test]
    fn cdf_tables_match_three() {
        let mut generator = EnvMapCdfGenerator::new();
        generator.update_from(&env(&TEXELS));

        let marginal = generator.marginal_weights().unwrap();
        let conditional = generator.conditional_weights().unwrap();
        assert_eq!(halves(marginal), [0.25, 0.75]);
        assert_eq!(
            halves(conditional),
            [0.375, 0.625, 0.875, 0.875, 0.625, 0.625, 0.875, 0.875]
        );
        assert!((generator.total_sum() - 17.8608).abs() < 1e-9);

        // `new DataTexture( data, height, 1 )` and `( data, width, height )`,
        // `HalfFloatType` + `RedFormat`, linear, clamped, no mipmaps.
        assert_eq!(marginal.size(), (2, 1));
        assert_eq!(conditional.size(), (4, 2));
        for table in [marginal, conditional] {
            let inner = table.borrow();
            assert_eq!(inner.format, wgpu::TextureFormat::R16Float);
            assert_eq!(inner.min_filter, MinFilter::Linear);
            assert_eq!(inner.mag_filter, TextureFilter::Linear);
            assert_eq!(inner.wrap_s, Wrapping::ClampToEdge);
            assert_eq!(inner.wrap_t, Wrapping::ClampToEdge);
            assert!(!inner.generate_mipmaps && !inner.flip_y);
        }

        // The map: RGBA half floats, repeating in s, clamped in t.
        let map = generator.map().unwrap();
        let inner = map.borrow();
        assert_eq!(inner.format, wgpu::TextureFormat::Rgba16Float);
        assert_eq!(
            (inner.wrap_s, inner.wrap_t),
            (Wrapping::Repeat, Wrapping::ClampToEdge)
        );
        drop(inner);
        assert_eq!(
            &halves(map)[24..32],
            [5.0, 5.0, 5.0, 1.0, 0.0, 4.0, 0.0, 1.0]
        );
    }

    #[test]
    fn flip_y_reverses_the_rows_first() {
        // With `flipY` the rows swap before anything is summed: row 0 is now
        // the 7.8608 row and row 1 the 10 row, so the marginal CDF is
        // 7.8608 / 17.8608 = 0.44012, 1.0 and both quantiles land in row 1:
        // 0.75, 0.75. The conditional rows swap with them.
        let hdr = env(&TEXELS);
        hdr.set_flip_y(true);
        let mut generator = EnvMapCdfGenerator::new();
        generator.update_from(&hdr);
        assert_eq!(halves(generator.marginal_weights().unwrap()), [0.75, 0.75]);
        assert_eq!(
            halves(generator.conditional_weights().unwrap()),
            [0.625, 0.625, 0.875, 0.875, 0.375, 0.625, 0.875, 0.875]
        );
        assert!(!generator.map().unwrap().borrow().flip_y);
    }

    #[test]
    fn a_second_update_of_the_same_size_keeps_the_textures() {
        let mut generator = EnvMapCdfGenerator::new();
        generator.update_from(&env(&TEXELS));
        let ids = (
            generator.map().unwrap().id(),
            generator.marginal_weights().unwrap().id(),
        );
        let mut flipped = TEXELS;
        flipped.rotate_left(4);
        generator.update_from(&env(&flipped));
        assert_eq!(
            ids,
            (
                generator.map().unwrap().id(),
                generator.marginal_weights().unwrap().id()
            )
        );
        assert_eq!(halves(generator.marginal_weights().unwrap()), [0.75, 0.75]);
    }
}