three-rs 0.2.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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
//! The renderer's half of the node system: turning a built `NodeProgram` into
//! wgpu shader modules, bind-group layouts, a pipeline and, per draw, the bind
//! groups and uniform bytes. This is `WebGPUPipelineUtils` +
//! `WebGPUBindingUtils` + `Bindings.updateBinding()`, generically driven by the
//! descriptors the node builder produced — there is nothing per-material here.

use crate::materials::{DepthFunc, Side};
use crate::math::{Color, Matrix3, Matrix4, Vector2, Vector3, Vector4};
use crate::nodes::wgsl::TextureKind;
use crate::nodes::{BindingDesc, BufferSource, NodeProgram, Type, UniformMember, UniformSource};

/// Everything about a pass that the pipeline has to bake in, beyond the shader.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct RenderState {
    pub color_format: wgpu::TextureFormat,
    /// How many colour attachments the pass has — `renderTarget.textures.length`,
    /// 1 for everything but an MRT pass. The fragment stage declares one
    /// `@location` per attachment, so a pipeline with the wrong count is a
    /// validation error rather than wrong pixels; it is part of the key for the
    /// same reason the format is.
    pub color_attachments: u32,
    /// One entry per colour attachment **past the first**: its format and the
    /// blend state `MRTNode.getBlendMode( texture.name )` gives it.
    ///
    /// Three sizes the `targets` array from `renderObject.context.textures` and
    /// takes each entry's format from that texture, so an MRT whose attachments
    /// have different types — `webgpu_postprocessing_bloom_emissive`'s
    /// `rgba16float` colour beside an `rgba8unorm` emissive — is one pipeline
    /// with two different target formats. A fixed array keeps `RenderState`
    /// `Copy` and part of the pipeline key; `MAX_EXTRA_COLOR_ATTACHMENTS` is
    /// the port's ceiling, not WebGPU's (which is 8).
    pub extra_color_targets: [Option<ExtraColorTarget>; MAX_EXTRA_COLOR_ATTACHMENTS],
    pub depth_format: Option<wgpu::TextureFormat>,
    pub sample_count: u32,
    pub side: Side,
    pub depth_test: bool,
    pub depth_write: bool,
    /// `Material.depthFunc`, read only while `depth_test` is on.
    pub depth_func: DepthFunc,
    /// `Material.alphaToCoverage`; the pipeline enables it only on a
    /// multisampled target, as `createRenderPipeline()` does.
    pub alpha_to_coverage: bool,
    /// `WebGPUPipelineUtils.createRenderPipeline()`'s `materialBlending`, i.e.
    /// `MeshBasicNodeMaterial::blend_state()`. Part of the key because an
    /// additive and an opaque pipeline share one program.
    pub blend: Option<wgpu::BlendState>,
    /// `WebGPUUtils.getPrimitiveTopology( object, material )`. It comes from
    /// the *object*, not the material, so one `LineBasicNodeMaterial` shared by
    /// a `Line` and a `LineSegments` needs two pipelines off one program —
    /// which is exactly why this is part of the key.
    pub topology: wgpu::PrimitiveTopology,
    /// `_getPrimitiveState()`: set only for an indexed `Line` that is not a
    /// `LineSegments`, from the index array's type.
    pub strip_index_format: Option<wgpu::IndexFormat>,
}

/// How many colour attachments past the first a pass may have here.
pub const MAX_EXTRA_COLOR_ATTACHMENTS: usize = 3;

/// One MRT colour attachment past the first, as the pipeline descriptor wants
/// it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ExtraColorTarget {
    pub format: wgpu::TextureFormat,
    pub blend: Option<wgpu::BlendState>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PipelineKey {
    pub program: u64,
    pub state: RenderState,
}

/// A compiled material: the two shader modules plus the layouts its bind groups
/// are built against — `NodeBuilderState`'s compiled half, shared by every
/// render object whose `cache_key` matches.
///
/// Deliberately *not* the `NodeProgram` it was compiled from. A binding
/// description names a texture and an instanced attribute's array; holding the
/// first-seen draw's copy here would keep those alive for the life of the
/// renderer (#56) and, worse, would be the wrong copy for the second material
/// that hashes to this program. The descriptions the per-draw resolution needs
/// live with the material that owns them, in `Renderer::node_builder_states`.
pub struct Program {
    vertex_module: wgpu::ShaderModule,
    fragment_module: wgpu::ShaderModule,
    pub layouts: Vec<wgpu::BindGroupLayout>,
    pipeline_layout: wgpu::PipelineLayout,
    /// `WebGPUAttributeUtils.createShaderVertexBuffers()`'s result, shape only:
    /// stride, step mode and attributes per buffer, with the buffer itself —
    /// the `Rc<InstanceBuffer>` a `VertexBufferDesc` carries — left behind.
    vertex_layouts: Vec<VertexLayout>,
}

/// One `GPUVertexBufferLayout` as the pipeline bakes it in.
struct VertexLayout {
    array_stride: u64,
    step_mode: wgpu::VertexStepMode,
    attributes: Vec<wgpu::VertexAttribute>,
}

impl Program {
    pub fn new(device: &wgpu::Device, node: &NodeProgram) -> Self {
        let vertex_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("three-rs vertex"),
            source: wgpu::ShaderSource::Wgsl(node.vertex_wgsl.clone().into()),
        });
        let fragment_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("three-rs fragment"),
            source: wgpu::ShaderSource::Wgsl(node.fragment_wgsl.clone().into()),
        });

        let layouts: Vec<wgpu::BindGroupLayout> = node
            .groups
            .iter()
            .map(|bindings| {
                let entries: Vec<wgpu::BindGroupLayoutEntry> = bindings
                    .iter()
                    .enumerate()
                    .map(|(binding, desc)| layout_entry(binding as u32, desc))
                    .collect();
                device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: Some("three-rs bindings"),
                    entries: &entries,
                })
            })
            .collect();

        let refs: Vec<Option<&wgpu::BindGroupLayout>> = layouts.iter().map(Some).collect();
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("three-rs pipeline layout"),
            bind_group_layouts: &refs,
            immediate_size: 0,
        });

        // `WebGPUAttributeUtils.createShaderVertexBuffers()`: the attributes
        // grouped into buffers — one per geometry attribute, one per instanced
        // buffer, in first-use order. The layouts are computed from the same
        // `AttributeSlot`s the renderer binds from, so a slot and its buffer
        // cannot disagree.
        let vertex_layouts = node
            .vertex_buffers()
            .iter()
            .map(|desc| VertexLayout {
                array_stride: desc.array_stride,
                step_mode: if desc.instanced {
                    wgpu::VertexStepMode::Instance
                } else {
                    wgpu::VertexStepMode::Vertex
                },
                attributes: desc
                    .attributes
                    .iter()
                    .map(|(location, ty, offset)| wgpu::VertexAttribute {
                        format: vertex_format(*ty),
                        offset: *offset,
                        shader_location: *location,
                    })
                    .collect(),
            })
            .collect();

        Self {
            vertex_module,
            fragment_module,
            layouts,
            pipeline_layout,
            vertex_layouts,
        }
    }

    /// `WebGPUPipelineUtils.createRenderPipeline()`.
    pub fn create_pipeline(
        &self,
        device: &wgpu::Device,
        state: RenderState,
    ) -> wgpu::RenderPipeline {
        let buffers: Vec<Option<wgpu::VertexBufferLayout>> = self
            .vertex_layouts
            .iter()
            .map(|layout| {
                Some(wgpu::VertexBufferLayout {
                    array_stride: layout.array_stride,
                    step_mode: layout.step_mode,
                    attributes: layout.attributes.as_slice(),
                })
            })
            .collect();

        let targets: Vec<Option<wgpu::ColorTargetState>> = (0..state.color_attachments)
            .map(|i| {
                // Attachment 0 is the material's own format and blending;
                // `MRTNode.blendModes.output` is `MaterialBlending`, which
                // resolves to exactly that. Every attachment past it carries
                // its own format and whatever `setBlendMode` gave it.
                let extra = (i > 0)
                    .then(|| state.extra_color_targets[i as usize - 1])
                    .flatten();
                Some(wgpu::ColorTargetState {
                    format: extra.map_or(state.color_format, |extra| extra.format),
                    // `undefined` for an opaque `NormalBlending` material, which
                    // is every rung up to 9; see `materials::blending`.
                    blend: extra.map_or(state.blend, |extra| extra.blend),
                    write_mask: wgpu::ColorWrites::ALL,
                })
            })
            .collect();

        device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("three-rs pipeline"),
            layout: Some(&self.pipeline_layout),
            vertex: wgpu::VertexState {
                module: &self.vertex_module,
                entry_point: Some("main"),
                compilation_options: Default::default(),
                buffers: &buffers,
            },
            fragment: Some(wgpu::FragmentState {
                module: &self.fragment_module,
                entry_point: Some("main"),
                compilation_options: Default::default(),
                // One target per colour attachment. `MRTNode.blendModes` gives
                // three a per-output blend — `MaterialBlending` for `output`
                // and `NoBlending` for the rest — but every MRT material on
                // this ladder is opaque, where both resolve to no blend state
                // at all, so the material's own is used for each.
                targets: &targets,
            }),
            primitive: wgpu::PrimitiveState {
                topology: state.topology,
                strip_index_format: state.strip_index_format,
                // `_getPrimitiveState()`: `FrontSide` → CCW front faces,
                // `BackSide` → CW, both culling the back face.
                front_face: match state.side {
                    Side::Front | Side::Double => wgpu::FrontFace::Ccw,
                    Side::Back => wgpu::FrontFace::Cw,
                },
                // `DoubleSide` → `cullMode: 'none'`.
                cull_mode: match state.side {
                    Side::Double => None,
                    _ => Some(wgpu::Face::Back),
                },
                unclipped_depth: false,
                polygon_mode: wgpu::PolygonMode::Fill,
                conservative: false,
            },
            depth_stencil: state.depth_format.map(|format| wgpu::DepthStencilState {
                format,
                depth_write_enabled: Some(state.depth_write),
                // `_getDepthCompare()`: `Material.depthFunc` one-to-one,
                // and `'always'` with `depthTest` off.
                depth_compare: Some(if state.depth_test {
                    match state.depth_func {
                        DepthFunc::Never => wgpu::CompareFunction::Never,
                        DepthFunc::Always => wgpu::CompareFunction::Always,
                        DepthFunc::Less => wgpu::CompareFunction::Less,
                        DepthFunc::LessEqual => wgpu::CompareFunction::LessEqual,
                        DepthFunc::Equal => wgpu::CompareFunction::Equal,
                        DepthFunc::GreaterEqual => wgpu::CompareFunction::GreaterEqual,
                        DepthFunc::Greater => wgpu::CompareFunction::Greater,
                        DepthFunc::NotEqual => wgpu::CompareFunction::NotEqual,
                    }
                } else {
                    wgpu::CompareFunction::Always
                }),
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState::default(),
            }),
            multisample: wgpu::MultisampleState {
                count: state.sample_count,
                mask: !0,
                // `alphaToCoverageEnabled: material.alphaToCoverage &&
                // sampleCount > 1`.
                alpha_to_coverage_enabled: state.alpha_to_coverage && state.sample_count > 1,
            },
            multiview_mask: None,
            cache: None,
        })
    }
}

fn layout_entry(binding: u32, desc: &BindingDesc) -> wgpu::BindGroupLayoutEntry {
    match desc {
        BindingDesc::Uniforms { visibility, .. } => wgpu::BindGroupLayoutEntry {
            binding,
            visibility: visibility.stages(),
            ty: wgpu::BindingType::Buffer {
                ty: wgpu::BufferBindingType::Uniform,
                has_dynamic_offset: false,
                min_binding_size: None,
            },
            count: None,
        },
        BindingDesc::Buffer {
            visibility, source, ..
        } => wgpu::BindGroupLayoutEntry {
            binding,
            visibility: visibility.stages(),
            ty: wgpu::BindingType::Buffer {
                // The same rule as the WGSL declaration: `read_write` in
                // compute, `read` in the vertex and fragment stages, which is
                // why the page asks for `maxStorageBuffersInVertexStage: 1`.
                ty: match source {
                    // `.toReadOnly()` is `read` in a kernel too.
                    BufferSource::StorageData {
                        read_only: true, ..
                    } => wgpu::BufferBindingType::Storage { read_only: true },
                    source if source.is_storage() => wgpu::BufferBindingType::Storage {
                        read_only: !visibility.compute,
                    },
                    _ => wgpu::BufferBindingType::Uniform,
                },
                has_dynamic_offset: false,
                min_binding_size: None,
            },
            count: None,
        },
        // `WebGPUBindingUtils.createBindingsLayout()`'s `storageTexture`
        // branch: the texture's own format, the node's access in a compute
        // stage and `read-only` in any other, and a 3D view for a
        // `Storage3DTexture`.
        BindingDesc::Texture {
            kind:
                TextureKind::Storage {
                    format,
                    access,
                    dim3,
                },
            visibility,
            ..
        } => wgpu::BindGroupLayoutEntry {
            binding,
            visibility: visibility.stages(),
            ty: wgpu::BindingType::StorageTexture {
                access: if visibility.compute {
                    access.wgpu()
                } else {
                    wgpu::StorageTextureAccess::ReadOnly
                },
                format: *format,
                view_dimension: if *dim3 {
                    wgpu::TextureViewDimension::D3
                } else {
                    wgpu::TextureViewDimension::D2
                },
            },
            count: None,
        },
        BindingDesc::Texture {
            kind, visibility, ..
        } => wgpu::BindGroupLayoutEntry {
            binding,
            visibility: visibility.stages(),
            ty: wgpu::BindingType::Texture {
                sample_type: match kind {
                    // The morph data texture is `rgba32float`, which wgpu only
                    // lets a pipeline filter with the `FLOAT32_FILTERABLE`
                    // feature. It is only ever `textureLoad`ed, so declaring it
                    // non-filterable costs nothing and keeps the feature set to
                    // the WebGPU baseline three.js targets.
                    TextureKind::Float2DArray | TextureKind::FloatData2D => {
                        wgpu::TextureSampleType::Float { filterable: false }
                    }
                    // `r32uint` — `BatchedMesh._indirectTexture`.
                    TextureKind::Uint2D => wgpu::TextureSampleType::Uint,
                    TextureKind::Depth2D
                    | TextureKind::DepthMultisampled2D
                    | TextureKind::DepthCompare2D
                    | TextureKind::DepthCube => wgpu::TextureSampleType::Depth,
                    _ => wgpu::TextureSampleType::Float { filterable: true },
                },
                view_dimension: match kind {
                    TextureKind::Cube | TextureKind::DepthCube => wgpu::TextureViewDimension::Cube,
                    TextureKind::Float2DArray | TextureKind::Sampled2DArray => {
                        wgpu::TextureViewDimension::D2Array
                    }
                    TextureKind::Float3D => wgpu::TextureViewDimension::D3,
                    _ => wgpu::TextureViewDimension::D2,
                },
                multisampled: matches!(kind, TextureKind::DepthMultisampled2D),
            },
            count: None,
        },
        BindingDesc::Sampler {
            kind, visibility, ..
        } => wgpu::BindGroupLayoutEntry {
            binding,
            visibility: visibility.stages(),
            ty: wgpu::BindingType::Sampler(match kind {
                TextureKind::Depth2D => wgpu::SamplerBindingType::NonFiltering,
                TextureKind::DepthCompare2D | TextureKind::DepthCube => {
                    wgpu::SamplerBindingType::Comparison
                }
                _ => wgpu::SamplerBindingType::Filtering,
            }),
            count: None,
        },
    }
}

fn vertex_format(ty: Type) -> wgpu::VertexFormat {
    match ty {
        Type::Vec2 => wgpu::VertexFormat::Float32x2,
        Type::Vec3 => wgpu::VertexFormat::Float32x3,
        Type::Vec4 => wgpu::VertexFormat::Float32x4,
        Type::F32 => wgpu::VertexFormat::Float32,
        // `WebGPUAttributeUtils.createAttribute()` reads the format off the
        // attribute's own typed array: `skinIndex` is a `Uint32Array` by the
        // time it reaches the GPU, so `uvec4` is `uint32x4`, not a float format
        // the shader casts.
        Type::UVec4 => wgpu::VertexFormat::Uint32x4,
        other => panic!("three-rs: {other:?} is not a vertex attribute type"),
    }
}

/// Everything a `UniformSource` can be resolved against: the camera of the pass,
/// the object being drawn, its material, and the renderer's frame state. The
/// defaults are three.js': an identity model matrix, a white opaque material,
/// `Scene`'s background rotation/blurriness/intensity and `Texture`'s identity
/// uv transform.
/// One light as the uniform writer sees it: three.js' `LightsNode` resolves a
/// `PointLight` to exactly these four values per render.
#[derive(Clone, Copy, Debug)]
#[doc(hidden)]
pub struct LightState {
    /// `light.color * light.intensity`, in the working colour space.
    pub color: Color,
    /// The light's world position through the camera's view matrix.
    pub view_position: Vector3,
    /// `light.distance` — the shader's `cutoffDistance`.
    pub distance: f64,
    /// `light.decay`.
    pub decay: f64,
    /// `light.matrixWorld`'s translation — `lightPosition()`.
    pub world_position: Vector3,
    /// `light.target.matrixWorld`'s translation — `lightTargetPosition()`.
    pub target_position: Vector3,
    /// `HemisphereLight.groundColor * intensity`, in the working colour space.
    pub ground_color: Color,
    /// `cos( light.angle )` — `SpotLightNode.update()`.
    pub cone_cos: f64,
    /// `cos( light.angle * ( 1 - light.penumbra ) )`.
    pub penumbra_cos: f64,
    /// `light.shadow.matrix` — bias ∘ projection ∘ the shadow camera's
    /// `matrixWorldInverse`, or `makeTranslation( -lightWorldPos )` for a
    /// point light.
    pub shadow_matrix: Matrix4,
    /// `light.shadow.camera.near` / `.far` — `PointShadowNode`'s depth test.
    pub shadow_camera_near: f64,
    pub shadow_camera_far: f64,
    /// `light.shadow.bias`.
    pub shadow_bias: f64,
    /// `light.shadow.normalBias`.
    pub shadow_normal_bias: f64,
    /// `light.shadow.radius`.
    pub shadow_radius: f64,
    /// `light.shadow.blurSamples` (VSM).
    pub shadow_blur_samples: f64,
    /// `light.shadow.mapSize`.
    pub shadow_map_size: Vector2,
    /// `light.shadow.intensity`.
    pub shadow_intensity: f64,
}

impl Default for LightState {
    fn default() -> Self {
        Self {
            color: Color::new(0.0, 0.0, 0.0),
            view_position: Vector3::new(0.0, 0.0, 0.0),
            distance: 0.0,
            decay: 2.0,
            world_position: Vector3::new(0.0, 0.0, 0.0),
            target_position: Vector3::new(0.0, 0.0, 0.0),
            ground_color: Color::new(0.0, 0.0, 0.0),
            cone_cos: 0.0,
            penumbra_cos: 0.0,
            shadow_matrix: Matrix4::identity(),
            shadow_camera_near: 0.5,
            shadow_camera_far: 500.0,
            shadow_bias: 0.0,
            shadow_normal_bias: 0.0,
            shadow_radius: 1.0,
            shadow_blur_samples: 8.0,
            shadow_map_size: Vector2::new(512.0, 512.0),
            shadow_intensity: 1.0,
        }
    }
}

#[derive(Clone, Copy, Debug)]
#[doc(hidden)]
pub struct UniformContext<'a> {
    /// `frame.object` — the render object this draw is for, for the one
    /// uniform source that reads it, [`UniformSource::ObjectUpdate`]. `None`
    /// for the passes three.js draws with its own internal `QuadMesh` or
    /// background mesh, which no application node can be attached to.
    pub object: Option<&'a crate::core::Object3D>,
    /// `frame.renderer.isOccluded()`'s answers for the render context being
    /// drawn — see [`crate::nodes::NodeFrame`]. `None` outside a scene pass
    /// and before a query has resolved.
    pub occluded: Option<&'a std::collections::HashSet<u32>>,
    pub camera_projection: Matrix4,
    pub camera_view: Matrix4,
    pub camera_world: Matrix4,
    /// `camera.projectionMatrixInverse`, kept beside the projection rather than
    /// inverted here: a camera maintains it in `updateProjectionMatrix()` and
    /// the two must not drift.
    pub camera_projection_inverse: Matrix4,
    pub model_world: Matrix4,
    pub material_color: Color,
    pub material_opacity: f64,
    pub material_alpha_test: f64,
    pub material_rotation: f64,
    /// `material.linewidth`.
    pub material_line_width: f64,
    pub material_reflectivity: f64,
    pub material_shininess: f64,
    pub material_specular: Color,
    pub material_emissive: Color,
    pub material_emissive_intensity: f64,
    pub material_metalness: f64,
    pub material_roughness: f64,
    pub material_bump_scale: f64,
    /// `MeshPhysicalMaterial.ior` / `.specularIntensity` / `.specularColor`,
    /// and `MeshStandardMaterial.normalScale`.
    pub material_ior: f64,
    pub material_specular_intensity: f64,
    pub material_specular_color: Color,
    /// `MeshPhysicalMaterial.sheen` / `.sheenColor` / `.sheenRoughness`.
    pub material_sheen: f64,
    pub material_sheen_color: Color,
    pub material_sheen_roughness: f64,
    /// `MeshPhysicalMaterial.diffuseRoughness`.
    pub material_diffuse_roughness: f64,
    pub material_normal_scale: Vector2,
    /// `MeshPhysicalMaterial.anisotropy` / `.anisotropyRotation` /
    /// `.clearcoat` / `.clearcoatRoughness` / `.clearcoatNormalScale`.
    pub material_anisotropy: f64,
    pub material_anisotropy_rotation: f64,
    pub material_clearcoat: f64,
    pub material_clearcoat_roughness: f64,
    pub material_clearcoat_normal_scale: Vector2,
    /// `MeshPhysicalMaterial.transmission` / `.thickness` /
    /// `.attenuationDistance` / `.attenuationColor`.
    pub material_transmission: f64,
    pub material_thickness: f64,
    pub material_attenuation_distance: f64,
    pub material_attenuation_color: Color,
    pub env_rotation: Matrix4,
    /// `material.envMapIntensity` — 1 on every material this rung builds.
    pub material_env_intensity: f64,
    /// `MeshStandardMaterial.aoMapIntensity`.
    pub material_ao_map_intensity: f64,
    pub background_rotation: Matrix4,
    pub background_blurriness: f64,
    pub background_intensity: f64,
    /// `scene.fog`'s colour (working space), `near`, `far` and `density` —
    /// whichever the fog kind has; the rest keep their defaults and are never
    /// read, because the other kind's node does not reference them.
    pub fog_color: Color,
    pub fog_near: f64,
    pub fog_far: f64,
    pub fog_density: f64,
    /// `viewportSize` — the bound target's dimensions.
    pub viewport_size: Vector2,
    /// `viewport` — `( x, y, width, height )` of the pass rectangle, in
    /// physical pixels. The whole target when nothing set a viewport.
    pub viewport: Vector4,
    /// `screenDPR` — `renderer.getPixelRatio()`.
    pub screen_dpr: f64,
    pub time: f64,
    /// `NodeFrame.deltaTime` / `NodeFrame.frameId`.
    pub delta_time: f64,
    pub frame_id: u32,
    /// `renderer.toneMappingExposure`.
    pub tone_mapping_exposure: f64,
    /// The lights of the pass, in `Scene.lights` order. Borrowed so the context
    /// stays `Copy` and can be spread with `..camera_uniforms` per draw.
    pub lights: &'a [LightState],
    /// `Morph.js`' `base` uniform: `1 - Σ morphTargetInfluences`.
    pub morph_base: f64,
    /// `mesh.morphTargetInfluences` — the `uniformArray` contents.
    pub morph_influences: &'a [f64],
    /// `SkinnedMesh.bindMatrix` / `.bindMatrixInverse`.
    pub bind_matrix: Matrix4,
    pub bind_matrix_inverse: Matrix4,
    /// `Sprite.center`, for `SpriteNodeMaterial`'s
    /// `reference( 'center', 'vec2', object )`.
    pub object_center: Vector2,
    /// `skeleton.boneMatrices` — the flat `mat4` array the bone buffer holds,
    /// already updated for this frame.
    pub bone_matrices: &'a [f32],
    /// An `ArrayCamera`'s sub-cameras' `matrixWorldInverse` and
    /// `projectionMatrix`, sixteen floats each in `camera.cameras` order —
    /// what `cameraViewMatrices` / `cameraProjectionMatrices` hold. Empty
    /// for every other camera.
    pub camera_view_matrices: &'a [f32],
    pub camera_projection_matrices: &'a [f32],
    /// The same sub-cameras' `viewport`s, in CSS pixels.
    pub camera_viewports: &'a [Vector4],
}

impl Default for UniformContext<'_> {
    fn default() -> Self {
        Self {
            camera_projection: Matrix4::identity(),
            camera_view: Matrix4::identity(),
            camera_world: Matrix4::identity(),
            camera_projection_inverse: Matrix4::identity(),
            model_world: Matrix4::identity(),
            material_color: Color::new(1.0, 1.0, 1.0),
            material_opacity: 1.0,
            material_alpha_test: 0.0,
            material_rotation: 0.0,
            material_line_width: 1.0,
            material_reflectivity: 1.0,
            material_shininess: 30.0,
            material_specular: Color::new(
                0x11 as f64 / 255.0,
                0x11 as f64 / 255.0,
                0x11 as f64 / 255.0,
            ),
            material_emissive: Color::new(0.0, 0.0, 0.0),
            material_emissive_intensity: 1.0,
            // `MeshStandardMaterial` defaults.
            material_metalness: 0.0,
            material_roughness: 1.0,
            material_bump_scale: 1.0,
            // `MeshPhysicalMaterial`'s defaults.
            material_ior: 1.5,
            material_specular_intensity: 1.0,
            material_specular_color: Color::new(1.0, 1.0, 1.0),
            material_sheen: 0.0,
            material_sheen_color: Color::new(0.0, 0.0, 0.0),
            material_sheen_roughness: 1.0,
            material_diffuse_roughness: 0.0,
            material_normal_scale: Vector2::new(1.0, 1.0),
            material_anisotropy: 0.0,
            material_anisotropy_rotation: 0.0,
            material_clearcoat: 0.0,
            material_clearcoat_roughness: 0.0,
            material_transmission: 0.0,
            material_thickness: 0.0,
            material_attenuation_distance: f64::INFINITY,
            material_attenuation_color: Color::new(1.0, 1.0, 1.0),
            material_clearcoat_normal_scale: Vector2::new(1.0, 1.0),
            env_rotation: Matrix4::identity(),
            material_env_intensity: 1.0,
            material_ao_map_intensity: 1.0,
            background_rotation: Matrix4::identity(),
            background_blurriness: 0.0,
            background_intensity: 1.0,
            fog_color: Color::new(1.0, 1.0, 1.0),
            fog_near: 1.0,
            fog_far: 1000.0,
            fog_density: 0.00025,
            viewport_size: Vector2::new(0.0, 0.0),
            viewport: Vector4::new(0.0, 0.0, 0.0, 0.0),
            screen_dpr: 1.0,
            camera_view_matrices: &[],
            camera_projection_matrices: &[],
            camera_viewports: &[],
            time: 0.0,
            delta_time: 0.0,
            frame_id: 0,
            tone_mapping_exposure: 1.0,
            lights: &[],
            morph_base: 1.0,
            morph_influences: &[],
            bind_matrix: Matrix4::identity(),
            bind_matrix_inverse: Matrix4::identity(),
            object_center: Vector2::new(0.5, 0.5),
            bone_matrices: &[],
            object: None,
            occluded: None,
        }
    }
}

impl UniformContext<'_> {
    /// `Bindings.updateBinding()`: the bytes of one generated uniform struct,
    /// each member written at the offset the builder gave it.
    pub fn bytes(&self, members: &[UniformMember], size: u32) -> Vec<u8> {
        let mut data = vec![0u8; size as usize];

        for member in members {
            let values: Vec<f32> = match &member.source {
                UniformSource::CameraProjectionMatrix => {
                    self.camera_projection.to_f32_array().to_vec()
                }
                UniformSource::CameraViewMatrix => self.camera_view.to_f32_array().to_vec(),
                // Never read: each sub-camera binds its own group; see
                // `UniformSource::CameraIndex`.
                UniformSource::CameraIndex => vec![0.0],
                UniformSource::CameraWorldMatrix => self.camera_world.to_f32_array().to_vec(),
                UniformSource::CameraPosition => {
                    let m = self.camera_world.to_f32_array();
                    vec![m[12], m[13], m[14]]
                }
                UniformSource::CameraProjectionMatrixInverse => {
                    self.camera_projection_inverse.to_f32_array().to_vec()
                }
                // `modelWorldMatrixInverse`: `self.value.copy(
                // object.matrixWorld ).invert()`, per object.
                UniformSource::ModelWorldMatrixInverse => {
                    let mut inverse = self.model_world;
                    inverse.invert();
                    inverse.to_f32_array().to_vec()
                }
                UniformSource::ModelWorldMatrix => self.model_world.to_f32_array().to_vec(),
                UniformSource::ModelNormalMatrix => {
                    let mut normal_matrix = Matrix3::identity();
                    normal_matrix.get_normal_matrix(&self.model_world);
                    normal_matrix.to_padded_f32_array().to_vec()
                }
                UniformSource::MaterialColor => vec![
                    self.material_color.r as f32,
                    self.material_color.g as f32,
                    self.material_color.b as f32,
                ],
                UniformSource::MaterialOpacity => vec![self.material_opacity as f32],
                UniformSource::MaterialAlphaTest => vec![self.material_alpha_test as f32],
                UniformSource::MaterialRotation => vec![self.material_rotation as f32],
                UniformSource::MaterialReflectivity => vec![self.material_reflectivity as f32],
                UniformSource::MaterialEnvIntensity => vec![self.material_env_intensity as f32],
                UniformSource::MaterialAoMapIntensity => {
                    vec![self.material_ao_map_intensity as f32]
                }
                UniformSource::MaterialShininess => vec![self.material_shininess as f32],
                UniformSource::MaterialSpecular => vec![
                    self.material_specular.r as f32,
                    self.material_specular.g as f32,
                    self.material_specular.b as f32,
                ],
                UniformSource::MaterialEmissive => vec![
                    self.material_emissive.r as f32,
                    self.material_emissive.g as f32,
                    self.material_emissive.b as f32,
                ],
                UniformSource::MaterialEmissiveIntensity => {
                    vec![self.material_emissive_intensity as f32]
                }
                UniformSource::MaterialLineWidth => vec![self.material_line_width as f32],
                UniformSource::MaterialMetalness => vec![self.material_metalness as f32],
                UniformSource::MaterialRoughness => vec![self.material_roughness as f32],
                UniformSource::MaterialBumpScale => vec![self.material_bump_scale as f32],
                UniformSource::MaterialIor => vec![self.material_ior as f32],
                UniformSource::MaterialSpecularIntensity => {
                    vec![self.material_specular_intensity as f32]
                }
                UniformSource::MaterialSpecularColor => vec![
                    self.material_specular_color.r as f32,
                    self.material_specular_color.g as f32,
                    self.material_specular_color.b as f32,
                ],
                UniformSource::MaterialSheen => vec![self.material_sheen as f32],
                UniformSource::MaterialSheenColor => vec![
                    self.material_sheen_color.r as f32,
                    self.material_sheen_color.g as f32,
                    self.material_sheen_color.b as f32,
                ],
                UniformSource::MaterialSheenRoughness => {
                    vec![self.material_sheen_roughness as f32]
                }
                UniformSource::MaterialDiffuseRoughness => {
                    vec![self.material_diffuse_roughness as f32]
                }
                UniformSource::MaterialNormalScale => vec![
                    self.material_normal_scale.x as f32,
                    self.material_normal_scale.y as f32,
                ],
                // `MaterialProperties.js`: `materialAnisotropyVector` is set
                // from the pair, not carried as two uniforms.
                UniformSource::MaterialAnisotropyVector => vec![
                    (self.material_anisotropy * self.material_anisotropy_rotation.cos()) as f32,
                    (self.material_anisotropy * self.material_anisotropy_rotation.sin()) as f32,
                ],
                UniformSource::MaterialClearcoat => vec![self.material_clearcoat as f32],
                UniformSource::MaterialClearcoatRoughness => {
                    vec![self.material_clearcoat_roughness as f32]
                }
                UniformSource::MaterialClearcoatNormalScale => vec![
                    self.material_clearcoat_normal_scale.x as f32,
                    self.material_clearcoat_normal_scale.y as f32,
                ],
                UniformSource::MaterialTransmission => vec![self.material_transmission as f32],
                UniformSource::MaterialThickness => vec![self.material_thickness as f32],
                // `attenuationDistance` defaults to `Infinity`, which reaches
                // the shader as an `f32` infinity and makes `volumeAttenuation`
                // take its `!= 0` branch with a zero coefficient — the same
                // transmittance of 1 the `+inf` comment in three describes.
                UniformSource::MaterialAttenuationDistance => {
                    vec![self.material_attenuation_distance as f32]
                }
                UniformSource::MaterialAttenuationColor => {
                    let c = self.material_attenuation_color;
                    vec![c.r as f32, c.g as f32, c.b as f32]
                }
                UniformSource::EnvRotationMatrix => self.env_rotation.to_f32_array().to_vec(),
                UniformSource::BackgroundRotation => {
                    self.background_rotation.to_f32_array().to_vec()
                }
                UniformSource::BackgroundBlurriness => vec![self.background_blurriness as f32],
                UniformSource::BackgroundIntensity => vec![self.background_intensity as f32],
                UniformSource::FogColor => vec![
                    self.fog_color.r as f32,
                    self.fog_color.g as f32,
                    self.fog_color.b as f32,
                ],
                UniformSource::FogNear => vec![self.fog_near as f32],
                UniformSource::FogFar => vec![self.fog_far as f32],
                UniformSource::FogDensity => vec![self.fog_density as f32],
                UniformSource::Time => vec![self.time as f32],
                UniformSource::DeltaTime => vec![self.delta_time as f32],
                // A `u32` member: written as its integer bits below, exact
                // for the first 2^24 frames.
                UniformSource::FrameId => vec![self.frame_id as f32],
                UniformSource::ViewportSize => {
                    vec![self.viewport_size.x as f32, self.viewport_size.y as f32]
                }
                UniformSource::Viewport => vec![
                    self.viewport.x as f32,
                    self.viewport.y as f32,
                    self.viewport.z as f32,
                    self.viewport.w as f32,
                ],
                UniformSource::ScreenDpr => vec![self.screen_dpr as f32],
                UniformSource::LightColorIntensity(i) => {
                    let light = &self.lights[*i];
                    vec![
                        light.color.r as f32,
                        light.color.g as f32,
                        light.color.b as f32,
                    ]
                }
                UniformSource::LightWorldPosition(i) => {
                    let p = self.lights[*i].world_position;
                    vec![p.x as f32, p.y as f32, p.z as f32]
                }
                UniformSource::LightCutoffDistance(i) => vec![self.lights[*i].distance as f32],
                UniformSource::LightDecay(i) => vec![self.lights[*i].decay as f32],
                UniformSource::LightViewPosition(i) => {
                    let p = self.lights[*i].view_position;
                    vec![p.x as f32, p.y as f32, p.z as f32]
                }
                // `Morph.js`' `OnObjectUpdate`: `base.value = 1 - Σ influences`
                // (or 1 when `morphTargetsRelative`).
                UniformSource::MorphBase => vec![self.morph_base as f32],
                UniformSource::BindMatrix => self.bind_matrix.to_f32_array().to_vec(),
                UniformSource::BindMatrixInverse => {
                    self.bind_matrix_inverse.to_f32_array().to_vec()
                }
                UniformSource::ObjectCenter => {
                    vec![self.object_center.x as f32, self.object_center.y as f32]
                }
                UniformSource::LightTargetPosition(i) => {
                    let p = self.lights[*i].target_position;
                    vec![p.x as f32, p.y as f32, p.z as f32]
                }
                UniformSource::LightGroundColor(i) => {
                    let c = self.lights[*i].ground_color;
                    vec![c.r as f32, c.g as f32, c.b as f32]
                }
                UniformSource::LightConeCos(i) => vec![self.lights[*i].cone_cos as f32],
                UniformSource::LightPenumbraCos(i) => vec![self.lights[*i].penumbra_cos as f32],
                UniformSource::ShadowMatrix(i) => {
                    self.lights[*i].shadow_matrix.to_f32_array().to_vec()
                }
                UniformSource::ShadowCameraNear(i) => {
                    vec![self.lights[*i].shadow_camera_near as f32]
                }
                UniformSource::ShadowCameraFar(i) => vec![self.lights[*i].shadow_camera_far as f32],
                UniformSource::ShadowBias(i) => vec![self.lights[*i].shadow_bias as f32],
                UniformSource::ShadowNormalBias(i) => {
                    vec![self.lights[*i].shadow_normal_bias as f32]
                }
                UniformSource::ShadowRadius(i) => vec![self.lights[*i].shadow_radius as f32],
                UniformSource::ShadowBlurSamples(i) => {
                    vec![self.lights[*i].shadow_blur_samples as f32]
                }
                UniformSource::ShadowMapSize(i) => {
                    let s = self.lights[*i].shadow_map_size;
                    vec![s.x as f32, s.y as f32]
                }
                UniformSource::ShadowIntensity(i) => vec![self.lights[*i].shadow_intensity as f32],
                UniformSource::ToneMappingExposure => vec![self.tone_mapping_exposure as f32],
                UniformSource::Value(values) => values.iter().map(|&v| v as f32).collect(),
                // Read per draw, so `sampleWeight.value = …` between two
                // `render_quad()` calls reaches the second one's buffer.
                UniformSource::Settable(cell) => cell.get().iter().map(|&v| v as f32).collect(),
                // Read when the buffer is written, from the object it names.
                UniformSource::Live(value) => value.get().iter().map(|&v| v as f32).collect(),
                // `Node.update( frame )` for a `NodeUpdateType.OBJECT` node,
                // run here rather than in a separate pre-pass because here is
                // where three runs it too: `Bindings.updateBindings()` calls
                // `nodeFrame.updateBeforeNode`/`updateNode` for the render
                // object whose buffer it is about to write.
                UniformSource::ObjectUpdate(update) => update
                    .value(&crate::nodes::NodeFrame {
                        object: self.object.expect(
                            "three-rs: an object-update uniform is only reachable from a draw that has a render object",
                        ),
                        occluded: self.occluded,
                    })
                    .iter()
                    .map(|&v| v as f32)
                    .collect(),
            };

            let offset = member.offset as usize;
            // The two members whose bits are not an f32's: `ComputeNode`'s
            // element count, `uniform( count, 'uint' )`, and PMREM's
            // `uniform( INTEGRATION_SIZE, 'int' )` loop bound. They ride
            // `UniformSource::Value` like every other baked value, so the type
            // is what says how to write them. Exact for magnitudes below 2^24,
            // which is every count a `dispatchWorkgroups` limit of 65535 groups
            // of 64 can reach anyway.
            if member.ty.component_type() == Type::U32 {
                let raw: Vec<u32> = values.iter().map(|&v| v as u32).collect();
                data[offset..offset + raw.len() * 4].copy_from_slice(bytemuck::cast_slice(&raw));
                continue;
            }
            if member.ty.component_type() == Type::I32 {
                let raw: Vec<i32> = values.iter().map(|&v| v as i32).collect();
                data[offset..offset + raw.len() * 4].copy_from_slice(bytemuck::cast_slice(&raw));
                continue;
            }
            data[offset..offset + values.len() * 4].copy_from_slice(bytemuck::cast_slice(&values));
        }

        data
    }
}

/// A compiled `ComputeNode` — `WebGPUPipelineUtils.createComputePipeline()`.
///
/// The same shape as [`Program`] with one module and no vertex or render state:
/// a compute pipeline has nothing a pass can vary, so the program cache *is*
/// the pipeline cache and there is no second `PipelineKey` level.
pub struct ComputeProgramGpu {
    pub layouts: Vec<wgpu::BindGroupLayout>,
    pub pipeline: wgpu::ComputePipeline,
}

impl ComputeProgramGpu {
    pub fn new(device: &wgpu::Device, program: &crate::nodes::ComputeProgram) -> Self {
        let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("three-rs compute"),
            source: wgpu::ShaderSource::Wgsl(program.wgsl.clone().into()),
        });

        let layouts: Vec<wgpu::BindGroupLayout> = program
            .groups
            .iter()
            .map(|bindings| {
                let entries: Vec<wgpu::BindGroupLayoutEntry> = bindings
                    .iter()
                    .enumerate()
                    .map(|(binding, desc)| layout_entry(binding as u32, desc))
                    .collect();
                device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: Some("three-rs compute bindings"),
                    entries: &entries,
                })
            })
            .collect();

        let refs: Vec<Option<&wgpu::BindGroupLayout>> = layouts.iter().map(Some).collect();
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("three-rs compute pipeline layout"),
            bind_group_layouts: &refs,
            immediate_size: 0,
        });

        let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("three-rs compute pipeline"),
            layout: Some(&pipeline_layout),
            module: &module,
            entry_point: Some("main"),
            compilation_options: Default::default(),
            cache: None,
        });

        ComputeProgramGpu { layouts, pipeline }
    }
}