viewport-lib 0.19.0

3D viewport rendering library
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
//! Scatter-volume pipeline state and per-frame upload.
//!
//! The scatter pass renders each visible `ScatterVolume` as a separate
//! instanced draw whose vertex shader projects the volume's world bounding box
//! to a screen-space rectangle. Only pixels inside that rectangle execute the
//! ray-march; volumes that do not touch a pixel cost nothing on that pixel.
//!
//! Pipeline layout:
//!
//!   group 0: shared camera (matches mesh / projected_tet bindings)
//!   group 1: per-volume `GpuScatterVolume` uniform with dynamic offset
//!   group 2: per-volume colourmap LUT + 3D density texture + samplers
//!   group 3: shared per-frame uniform (time / blue noise / frame index) +
//!            opaque depth texture + depth sampler
//!
//! The temporal blend is no longer inside the scatter shader -- a separate
//! single-attachment temporal-resolve pass (when `ScatterSettings::temporal`
//! is on) reads `raw_current` and the previous frame's history slot, blends,
//! and writes the new history slot. The composite pass then samples either
//! the history slot (when temporal is on) or `raw_current` (when off) and
//! composites onto the HDR target with premultiplied alpha-over.

use crate::scene::scatter_volume::{
    ColourSource, GpuRefractionVolume, GpuScatterVolume, ScatterVolume,
};

/// Hard cap on the number of volumes per frame. Same as the original cap; the
/// per-volume draw flow handles up to this many active volumes.
pub const MAX_SCATTER_VOLUMES: usize = 16;

/// Scatter-volume (participating media) pipelines, layouts, and per-frame
/// upload buffers. All device-shared and lazily built by the `ensure_scatter_*`
/// methods; the uploaded density textures are keyed elsewhere.
#[derive(Default)]
pub(crate) struct ScatterResources {
    /// Render pipeline for the scatter-volume pass. None until first item submitted.
    pub(crate) pipeline: Option<wgpu::RenderPipeline>,
    /// Group 1 layout (per-volume uniform with dynamic offset).
    pub(crate) per_volume_bgl: Option<wgpu::BindGroupLayout>,
    /// Group 2 layout (per-volume LUT + density texture + samplers).
    pub(crate) per_volume_tex_bgl: Option<wgpu::BindGroupLayout>,
    /// Group 3 layout (per-frame uniform + opaque depth + samplers).
    pub(crate) frame_bgl: Option<wgpu::BindGroupLayout>,
    /// Per-volume uniform buffer holding the packed `GpuScatterVolume` array,
    /// stride-padded for dynamic offsetting.
    pub(crate) per_volume_buffer: Option<wgpu::Buffer>,
    /// Bind group for the per-volume uniform (group 1).
    pub(crate) per_volume_bg: Option<wgpu::BindGroup>,
    /// Stride between dynamic-offset uniform slots, in bytes.
    pub(crate) per_volume_stride: u32,
    /// Capacity of `per_volume_buffer` in slots.
    pub(crate) per_volume_capacity: u32,
    /// Per-frame uniform buffer (group 3 binding 0).
    pub(crate) frame_uniform_buffer: Option<wgpu::Buffer>,
    /// Cache of group 2 bind groups, keyed by `(lut_id, density_id)`.
    pub(crate) per_volume_tex_cache: Vec<((usize, usize), wgpu::BindGroup)>,
    /// Bind group for group 3, rebuilt when opaque depth view changes.
    pub(crate) frame_bg: Option<wgpu::BindGroup>,
    /// Linear sampler used to read opaque depth in the scatter pass.
    pub(crate) depth_sampler: Option<wgpu::Sampler>,
    /// Linear-clamp sampler used to read the colourmap LUT in the scatter pass.
    pub(crate) colourmap_sampler: Option<wgpu::Sampler>,
    /// 1x1x1 R32Float fallback view bound at the per-volume 3D density slot.
    pub(crate) density_fallback_view: Option<wgpu::TextureView>,
    /// Token combining (depth view, frame uniform buffer) for `frame_bg` reuse.
    pub(crate) bound_depth: u64,
    /// Composite pipeline that blends a scatter intermediate onto the HDR target.
    pub(crate) composite_pipeline: Option<wgpu::RenderPipeline>,
    /// Bind group layout for the composite pass (one sampled RGBA16F + sampler).
    pub(crate) composite_bgl: Option<wgpu::BindGroupLayout>,
    /// Bilinear-clamp sampler used by the composite pass.
    pub(crate) composite_sampler: Option<wgpu::Sampler>,
    /// Temporal-resolve pipeline: mixes (raw_current, history_prev) into history_new.
    pub(crate) temporal_resolve_pipeline: Option<wgpu::RenderPipeline>,
    /// Bind group layout for the temporal-resolve pass.
    pub(crate) temporal_resolve_bgl: Option<wgpu::BindGroupLayout>,
    /// Per-frame uniform buffer for the temporal-resolve pass.
    pub(crate) temporal_resolve_uniform_buffer: Option<wgpu::Buffer>,
    /// Refraction pass: per-volume distortion using a noise-driven gradient.
    pub(crate) refraction_pipeline: Option<wgpu::RenderPipeline>,
    /// Bind group layout for the refraction pass's per-volume uniform.
    pub(crate) refraction_per_volume_bgl: Option<wgpu::BindGroupLayout>,
    /// Bind group layout for the refraction pass's source-scene + depth bindings.
    pub(crate) refraction_source_bgl: Option<wgpu::BindGroupLayout>,
    /// Dynamic-offset uniform buffer holding every refractive volume's params.
    pub(crate) refraction_per_volume_buffer: Option<wgpu::Buffer>,
    /// Stride between refractive-volume slots.
    pub(crate) refraction_per_volume_stride: u32,
    /// Capacity (slot count) the refraction per-volume buffer is sized for.
    pub(crate) refraction_per_volume_capacity: u32,
    /// Dynamic-offset bind group for the refraction per-volume uniform buffer.
    pub(crate) refraction_per_volume_bg: Option<wgpu::BindGroup>,
    /// Blit pipeline that copies the HDR target into the refraction source texture.
    pub(crate) refraction_blit_pipeline: Option<wgpu::RenderPipeline>,
}

/// Per-frame uniform layout shared across every per-volume draw.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable, Default)]
pub(crate) struct ScatterFrameUniformRaw {
    /// x = elapsed seconds since renderer start. yzw reserved.
    pub time_pack: [f32; 4],
    /// x = global step count, y = blue noise enabled (0/1),
    /// z = frame index low 32, w = reserved.
    pub count_pack: [u32; 4],
}

/// Uniform layout for the temporal-resolve pass.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable, Default)]
pub(crate) struct ScatterTemporalUniformRaw {
    pub prev_view_proj: [[f32; 4]; 4],
    /// x = blend factor (0..1), y = history valid (0/1),
    /// z = reserved, w = reserved.
    pub temporal_pack: [f32; 4],
}

impl crate::resources::DeviceResources {
    // ---------------------------------------------------------------------
    // Bind group layouts
    // ---------------------------------------------------------------------

    fn ensure_scatter_per_volume_bgl(&mut self, device: &wgpu::Device) {
        if self.scatter.per_volume_bgl.is_some() {
            return;
        }
        let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("scatter_per_volume_bgl"),
            entries: &[wgpu::BindGroupLayoutEntry {
                binding: 0,
                visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Uniform,
                    has_dynamic_offset: true,
                    // GpuScatterVolume = 144 bytes; the actual slot stride is
                    // padded to `min_uniform_buffer_offset_alignment`. The
                    // bound range is exactly the struct size.
                    min_binding_size: std::num::NonZeroU64::new(
                        std::mem::size_of::<GpuScatterVolume>() as u64,
                    ),
                },
                count: None,
            }],
        });
        self.scatter.per_volume_bgl = Some(bgl);
    }

    fn ensure_scatter_per_volume_tex_bgl(&mut self, device: &wgpu::Device) {
        if self.scatter.per_volume_tex_bgl.is_some() {
            return;
        }
        let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("scatter_per_volume_tex_bgl"),
            entries: &[
                // 0: colourmap LUT (256x1 RGBA, used when FLAG_USE_RAMP).
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                // 1: LUT sampler.
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
                // 2: 3D density texture (used when FLAG_USE_DENSITY_TEXTURE).
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: false },
                        view_dimension: wgpu::TextureViewDimension::D3,
                        multisampled: false,
                    },
                    count: None,
                },
                // 3: 3D density sampler.
                wgpu::BindGroupLayoutEntry {
                    binding: 3,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
                    count: None,
                },
            ],
        });
        self.scatter.per_volume_tex_bgl = Some(bgl);
    }

    fn ensure_scatter_frame_bgl(&mut self, device: &wgpu::Device) {
        if self.scatter.frame_bgl.is_some() {
            return;
        }
        let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("scatter_frame_bgl"),
            entries: &[
                // 0: per-frame uniform (time, blue noise, frame index).
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: std::num::NonZeroU64::new(std::mem::size_of::<
                            ScatterFrameUniformRaw,
                        >()
                            as u64),
                    },
                    count: None,
                },
                // 1: opaque depth texture.
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Depth,
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                // 2: depth sampler (NonFiltering for the textureLoad path).
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
                    count: None,
                },
            ],
        });
        self.scatter.frame_bgl = Some(bgl);
    }

    fn ensure_scatter_temporal_resolve_bgl(&mut self, device: &wgpu::Device) {
        if self.scatter.temporal_resolve_bgl.is_some() {
            return;
        }
        let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("scatter_temporal_resolve_bgl"),
            entries: &[
                // 0: temporal uniform (prev_view_proj + temporal_pack).
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: std::num::NonZeroU64::new(std::mem::size_of::<
                            ScatterTemporalUniformRaw,
                        >()
                            as u64),
                    },
                    count: None,
                },
                // 1: raw_current texture (this frame's scatter output).
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                // 2: history_prev texture.
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                // 3: bilinear sampler (reuses scatter composite sampler).
                wgpu::BindGroupLayoutEntry {
                    binding: 3,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
                // 4: opaque depth texture (for reprojection).
                wgpu::BindGroupLayoutEntry {
                    binding: 4,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Depth,
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                // 5: depth sampler.
                wgpu::BindGroupLayoutEntry {
                    binding: 5,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
                    count: None,
                },
            ],
        });
        self.scatter.temporal_resolve_bgl = Some(bgl);
    }

    fn ensure_scatter_density_fallback(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
        if self.scatter.density_fallback_view.is_some() {
            return;
        }
        let tex = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("scatter_density_fallback"),
            size: wgpu::Extent3d {
                width: 1,
                height: 1,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D3,
            format: wgpu::TextureFormat::R32Float,
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
            view_formats: &[],
        });
        let data: [f32; 1] = [1.0];
        queue.write_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &tex,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            bytemuck::cast_slice(&data),
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(4),
                rows_per_image: Some(1),
            },
            wgpu::Extent3d {
                width: 1,
                height: 1,
                depth_or_array_layers: 1,
            },
        );
        self.scatter.density_fallback_view =
            Some(tex.create_view(&wgpu::TextureViewDescriptor::default()));
    }

    fn ensure_scatter_depth_sampler(&mut self, device: &wgpu::Device) {
        if self.scatter.depth_sampler.is_some() {
            return;
        }
        self.scatter.depth_sampler = Some(crate::resources::builders::clamp_nearest_sampler(
            device,
            "scatter_depth_sampler",
        ));
    }

    fn ensure_scatter_colourmap_sampler(&mut self, device: &wgpu::Device) {
        if self.scatter.colourmap_sampler.is_some() {
            return;
        }
        self.scatter.colourmap_sampler = Some(crate::resources::builders::clamp_linear_sampler(
            device,
            "scatter_colourmap_sampler",
        ));
    }

    // ---------------------------------------------------------------------
    // Pipelines
    // ---------------------------------------------------------------------

    pub(crate) fn ensure_scatter_pipeline(
        &mut self,
        device: &wgpu::Device,
        colour_format: wgpu::TextureFormat,
    ) {
        if self.scatter.pipeline.is_some() {
            return;
        }
        self.ensure_scatter_per_volume_bgl(device);
        self.ensure_scatter_per_volume_tex_bgl(device);
        self.ensure_scatter_frame_bgl(device);

        let per_vol = self.scatter.per_volume_bgl.as_ref().unwrap();
        let per_tex = self.scatter.per_volume_tex_bgl.as_ref().unwrap();
        let frame_bgl = self.scatter.frame_bgl.as_ref().unwrap();

        let shader = crate::resources::builders::wgsl_module(
            device,
            "scatter_volume_shader",
            crate::resources::builders::wgsl_source!("scatter_volume"),
        );

        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("scatter_volume_pipeline_layout"),
            bind_group_layouts: &[&self.camera_bind_group_layout, per_vol, per_tex, frame_bgl],
            push_constant_ranges: &[],
        });

        // Premultiplied alpha-over: per-volume draws composite into the
        // (cleared) raw_current target in back-to-front order.
        let blend = wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                operation: wgpu::BlendOperation::Add,
            },
        };

        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("scatter_volume_pipeline"),
            layout: Some(&layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vs_main"),
                buffers: &[],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fs_main"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: colour_format,
                    blend: Some(blend),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                cull_mode: None,
                ..Default::default()
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState {
                count: 1,
                ..Default::default()
            },
            multiview: None,
            cache: None,
        });

        self.scatter.pipeline = Some(pipeline);
    }

    pub(crate) fn ensure_scatter_composite_pipeline(
        &mut self,
        device: &wgpu::Device,
        colour_format: wgpu::TextureFormat,
    ) {
        if self.scatter.composite_pipeline.is_some() {
            return;
        }
        let bgl = crate::resources::builders::texture_sampler_bgl(
            device,
            "scatter_composite_bgl",
            wgpu::ShaderStages::FRAGMENT,
        );
        let sampler =
            crate::resources::builders::clamp_linear_sampler(device, "scatter_composite_sampler");
        let shader = crate::resources::builders::wgsl_module(
            device,
            "scatter_composite_shader",
            crate::resources::builders::wgsl_source!("scatter_composite"),
        );
        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("scatter_composite_pipeline_layout"),
            bind_group_layouts: &[&bgl],
            push_constant_ranges: &[],
        });
        let blend = wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                operation: wgpu::BlendOperation::Add,
            },
        };
        let pipeline = crate::resources::builders::build_fullscreen_pipeline(
            device,
            "scatter_composite_pipeline",
            &layout,
            &shader,
            colour_format,
            Some(blend),
        );
        self.scatter.composite_pipeline = Some(pipeline);
        self.scatter.composite_bgl = Some(bgl);
        self.scatter.composite_sampler = Some(sampler);
    }

    pub(crate) fn ensure_scatter_temporal_resolve_pipeline(&mut self, device: &wgpu::Device) {
        if self.scatter.temporal_resolve_pipeline.is_some() {
            return;
        }
        self.ensure_scatter_temporal_resolve_bgl(device);
        let bgl = self.scatter.temporal_resolve_bgl.as_ref().unwrap();
        let shader = crate::resources::builders::wgsl_module(
            device,
            "scatter_temporal_resolve_shader",
            crate::resources::builders::wgsl_source!("scatter_temporal_resolve"),
        );
        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("scatter_temporal_resolve_pipeline_layout"),
            bind_group_layouts: &[bgl],
            push_constant_ranges: &[],
        });
        // History textures are RGBA16F. Blend is None: this pass owns the new
        // history fully and overwrites it.
        let pipeline = crate::resources::builders::build_fullscreen_pipeline(
            device,
            "scatter_temporal_resolve_pipeline",
            &layout,
            &shader,
            wgpu::TextureFormat::Rgba16Float,
            None,
        );
        self.scatter.temporal_resolve_pipeline = Some(pipeline);
    }

    // ---------------------------------------------------------------------
    // Per-frame uniform / bind group construction
    // ---------------------------------------------------------------------

    /// Pack visible volumes into the per-volume dynamic-offset uniform buffer.
    /// Volumes are written in submission order (caller is responsible for
    /// back-to-front sort). Returns the number of slots written.
    pub(crate) fn write_scatter_per_volume_buffer(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        volumes: &[(ScatterVolume, f32, u32)],
    ) -> u32 {
        // Stride = aligned per-volume uniform slot size. Recomputed once.
        let align = device.limits().min_uniform_buffer_offset_alignment as u64;
        let struct_size = std::mem::size_of::<GpuScatterVolume>() as u64;
        let stride = ((struct_size + align - 1) / align * align).max(struct_size) as u32;
        let capacity = volumes.len().min(MAX_SCATTER_VOLUMES).max(1) as u32;
        let buffer_size = (stride as u64) * (capacity as u64);

        let need_realloc = self.scatter.per_volume_buffer.is_none()
            || self.scatter.per_volume_stride != stride
            || self.scatter.per_volume_capacity < capacity;
        if need_realloc {
            let buf = device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("scatter_per_volume_uniform"),
                size: buffer_size,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            self.scatter.per_volume_buffer = Some(buf);
            self.scatter.per_volume_stride = stride;
            self.scatter.per_volume_capacity = capacity;
            self.scatter.per_volume_bg = None;
        }

        // Build the dynamic-offset bind group lazily.
        if self.scatter.per_volume_bg.is_none() {
            self.ensure_scatter_per_volume_bgl(device);
            let bgl = self.scatter.per_volume_bgl.as_ref().unwrap();
            let buf = self.scatter.per_volume_buffer.as_ref().unwrap();
            let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("scatter_per_volume_bg"),
                layout: bgl,
                entries: &[wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                        buffer: buf,
                        offset: 0,
                        size: std::num::NonZeroU64::new(struct_size),
                    }),
                }],
            });
            self.scatter.per_volume_bg = Some(bg);
        }

        // Pack and upload.
        let mut bytes = vec![0u8; buffer_size as usize];
        let mut n: u32 = 0;
        for (volume, mult, flags) in volumes.iter() {
            if n as usize >= MAX_SCATTER_VOLUMES {
                break;
            }
            if let Some(packed) = GpuScatterVolume::pack(volume, *mult, *flags) {
                let offset = (n as usize) * (stride as usize);
                let src = bytemuck::bytes_of(&packed);
                bytes[offset..offset + src.len()].copy_from_slice(src);
                n += 1;
            }
        }
        if let Some(buf) = self.scatter.per_volume_buffer.as_ref() {
            queue.write_buffer(
                buf,
                0,
                &bytes[..(n as usize * stride as usize).max(stride as usize)],
            );
        }
        n
    }

    /// Write the per-frame uniform (time / blue noise / frame index) and
    /// build / rebuild the frame bind group for the given opaque depth view.
    pub(crate) fn write_scatter_frame_uniform(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        depth_view: &wgpu::TextureView,
        depth_view_token: u64,
        time_seconds: f32,
        global_steps: u32,
        blue_noise_jitter: bool,
        frame_index: u64,
    ) {
        self.ensure_scatter_frame_bgl(device);
        self.ensure_scatter_depth_sampler(device);
        if self.scatter.frame_uniform_buffer.is_none() {
            let buf = device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("scatter_frame_uniform"),
                size: std::mem::size_of::<ScatterFrameUniformRaw>() as u64,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            self.scatter.frame_uniform_buffer = Some(buf);
            self.scatter.frame_bg = None;
        }
        let raw = ScatterFrameUniformRaw {
            time_pack: [time_seconds, 0.0, 0.0, 0.0],
            count_pack: [
                global_steps.clamp(1, 128),
                if blue_noise_jitter { 1 } else { 0 },
                frame_index as u32,
                0,
            ],
        };
        if let Some(buf) = self.scatter.frame_uniform_buffer.as_ref() {
            queue.write_buffer(buf, 0, bytemuck::bytes_of(&raw));
        }
        if self.scatter.frame_bg.is_none() || self.scatter.bound_depth != depth_view_token {
            let bgl = self.scatter.frame_bgl.as_ref().unwrap();
            let buf = self.scatter.frame_uniform_buffer.as_ref().unwrap();
            let sampler = self.scatter.depth_sampler.as_ref().unwrap();
            let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("scatter_frame_bg"),
                layout: bgl,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::TextureView(depth_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: wgpu::BindingResource::Sampler(sampler),
                    },
                ],
            });
            self.scatter.frame_bg = Some(bg);
            self.scatter.bound_depth = depth_view_token;
        }
    }

    /// Look up or build a group 2 bind group for the (lut_id, density_id)
    /// pair. Pass `u32::MAX` for either id to bind the fallback.
    pub(crate) fn ensure_scatter_per_volume_tex_bg(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        lut_id: usize,
        density_id: usize,
    ) -> wgpu::BindGroup {
        self.ensure_scatter_per_volume_tex_bgl(device);
        self.ensure_scatter_colourmap_sampler(device);
        self.ensure_scatter_density_fallback(device, queue);

        let key = (lut_id, density_id);
        if let Some((_, bg)) = self
            .scatter
            .per_volume_tex_cache
            .iter()
            .find(|(k, _)| *k == key)
        {
            return bg.clone();
        }
        let bgl = self.scatter.per_volume_tex_bgl.as_ref().unwrap();
        let lut_sampler = self.scatter.colourmap_sampler.as_ref().unwrap();
        let density_sampler = self.scatter.depth_sampler.as_ref().unwrap();
        let lut_view: &wgpu::TextureView = if lut_id == usize::MAX {
            &self.content.fallback_lut_view
        } else {
            self.content
                .colourmap_views
                .get(lut_id)
                .unwrap_or(&self.content.fallback_lut_view)
        };
        let density_fallback = self.scatter.density_fallback_view.as_ref().unwrap();
        let density_view: &wgpu::TextureView = if density_id == usize::MAX {
            density_fallback
        } else {
            self.content
                .volume_textures
                .get(density_id)
                .map(|(_, v)| v)
                .unwrap_or(density_fallback)
        };
        let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("scatter_per_volume_tex_bg"),
            layout: bgl,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(lut_view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(lut_sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::TextureView(density_view),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: wgpu::BindingResource::Sampler(density_sampler),
                },
            ],
        });
        self.scatter.per_volume_tex_cache.push((key, bg.clone()));
        bg
    }

    /// Resolve a volume's `(lut_id, density_id)` pair. `usize::MAX` indicates
    /// the fallback should be bound.
    pub(crate) fn scatter_volume_tex_ids(volume: &ScatterVolume) -> (usize, usize) {
        let lut_id = match volume.colour {
            ColourSource::Ramp(id) => id.0,
            _ => usize::MAX,
        };
        let density_id = volume.density_texture.map(|id| id.0).unwrap_or(usize::MAX);
        (lut_id, density_id)
    }

    /// Clear the per-volume texture bind group cache. Call when the
    /// underlying texture vectors may have been mutated (uploads added).
    pub(crate) fn clear_scatter_per_volume_tex_cache(&mut self) {
        self.scatter.per_volume_tex_cache.clear();
    }

    /// Stride between dynamic-offset slots, in bytes.
    pub(crate) fn scatter_per_volume_stride(&self) -> u32 {
        self.scatter.per_volume_stride
    }

    // ---------------------------------------------------------------------
    // Composite + temporal-resolve helpers
    // ---------------------------------------------------------------------

    pub(crate) fn make_scatter_composite_bg(
        &self,
        device: &wgpu::Device,
        source_view: &wgpu::TextureView,
    ) -> wgpu::BindGroup {
        let bgl = self.scatter.composite_bgl.as_ref().unwrap();
        let sampler = self.scatter.composite_sampler.as_ref().unwrap();
        device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("scatter_composite_bg"),
            layout: bgl,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(source_view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(sampler),
                },
            ],
        })
    }

    /// Build a temporal-resolve bind group sampling `(raw_view, history_view)`
    /// alongside the bound depth and uniform.
    pub(crate) fn make_scatter_temporal_resolve_bg(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        raw_view: &wgpu::TextureView,
        history_view: &wgpu::TextureView,
        depth_view: &wgpu::TextureView,
    ) -> wgpu::BindGroup {
        // The composite sampler is built by the composite pipeline; the
        // depth sampler is built when the per-frame uniform is written.
        // Either may not exist yet on the first frame, so ensure both here.
        self.ensure_scatter_temporal_resolve_bgl(device);
        self.ensure_scatter_depth_sampler(device);
        self.ensure_scatter_composite_pipeline(device, wgpu::TextureFormat::Rgba16Float);
        if self.scatter.temporal_resolve_uniform_buffer.is_none() {
            self.write_scatter_temporal_uniform(device, queue, [[0.0; 4]; 4], 0.0, false);
        }
        let bgl = self.scatter.temporal_resolve_bgl.as_ref().unwrap();
        let buf = self
            .scatter
            .temporal_resolve_uniform_buffer
            .as_ref()
            .unwrap();
        let bilinear = self.scatter.composite_sampler.as_ref().unwrap();
        let depth_sampler = self.scatter.depth_sampler.as_ref().unwrap();
        device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("scatter_temporal_resolve_bg"),
            layout: bgl,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::TextureView(raw_view),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::TextureView(history_view),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: wgpu::BindingResource::Sampler(bilinear),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: wgpu::BindingResource::TextureView(depth_view),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: wgpu::BindingResource::Sampler(depth_sampler),
                },
            ],
        })
    }

    // ---------------------------------------------------------------------
    // Refraction pass
    // ---------------------------------------------------------------------

    fn ensure_scatter_refraction_per_volume_bgl(&mut self, device: &wgpu::Device) {
        if self.scatter.refraction_per_volume_bgl.is_some() {
            return;
        }
        let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("scatter_refraction_per_volume_bgl"),
            entries: &[wgpu::BindGroupLayoutEntry {
                binding: 0,
                visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Uniform,
                    has_dynamic_offset: true,
                    min_binding_size: std::num::NonZeroU64::new(std::mem::size_of::<
                        GpuRefractionVolume,
                    >() as u64),
                },
                count: None,
            }],
        });
        self.scatter.refraction_per_volume_bgl = Some(bgl);
    }

    fn ensure_scatter_refraction_source_bgl(&mut self, device: &wgpu::Device) {
        if self.scatter.refraction_source_bgl.is_some() {
            return;
        }
        let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("scatter_refraction_source_bgl"),
            entries: &[
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Depth,
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
            ],
        });
        self.scatter.refraction_source_bgl = Some(bgl);
    }

    pub(crate) fn ensure_scatter_refraction_pipeline(
        &mut self,
        device: &wgpu::Device,
        colour_format: wgpu::TextureFormat,
    ) {
        if self.scatter.refraction_pipeline.is_some() {
            return;
        }
        self.ensure_scatter_refraction_per_volume_bgl(device);
        self.ensure_scatter_refraction_source_bgl(device);
        self.ensure_scatter_composite_pipeline(device, colour_format);

        let per_vol = self.scatter.refraction_per_volume_bgl.as_ref().unwrap();
        let source_bgl = self.scatter.refraction_source_bgl.as_ref().unwrap();

        let shader = crate::resources::builders::wgsl_module(
            device,
            "scatter_refraction_shader",
            crate::resources::builders::wgsl_source!("scatter_refraction"),
        );

        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("scatter_refraction_pipeline_layout"),
            bind_group_layouts: &[&self.camera_bind_group_layout, per_vol, source_bgl],
            push_constant_ranges: &[],
        });

        // Replace blend: the distorted sample overwrites the HDR pixel before
        // the scatter pass composites on top.
        let pipeline = crate::resources::builders::build_fullscreen_pipeline(
            device,
            "scatter_refraction_pipeline",
            &layout,
            &shader,
            colour_format,
            None,
        );

        self.scatter.refraction_pipeline = Some(pipeline);
    }

    /// Build a render pipeline that samples a source colour texture (via the
    /// composite BGL / shader) and writes it to a render target with replace
    /// blend. Used to copy the HDR scene into the refraction source texture
    /// before the per-volume distortion runs.
    pub(crate) fn ensure_scatter_refraction_blit_pipeline(
        &mut self,
        device: &wgpu::Device,
        colour_format: wgpu::TextureFormat,
    ) {
        if self.scatter.refraction_blit_pipeline.is_some() {
            return;
        }
        self.ensure_scatter_composite_pipeline(device, colour_format);
        let bgl = self.scatter.composite_bgl.as_ref().unwrap();
        let shader = crate::resources::builders::wgsl_module(
            device,
            "scatter_refraction_blit_shader",
            crate::resources::builders::wgsl_source!("scatter_composite"),
        );
        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("scatter_refraction_blit_pipeline_layout"),
            bind_group_layouts: &[bgl],
            push_constant_ranges: &[],
        });
        let pipeline = crate::resources::builders::build_fullscreen_pipeline(
            device,
            "scatter_refraction_blit_pipeline",
            &layout,
            &shader,
            colour_format,
            None,
        );
        self.scatter.refraction_blit_pipeline = Some(pipeline);
    }

    /// Pack visible refractive volumes into the dynamic-offset uniform buffer.
    /// Returns the number of slots written.
    pub(crate) fn write_scatter_refraction_per_volume_buffer(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        volumes: &[(ScatterVolume, f32)],
        time_seconds: f32,
    ) -> u32 {
        let align = device.limits().min_uniform_buffer_offset_alignment as u64;
        let struct_size = std::mem::size_of::<GpuRefractionVolume>() as u64;
        let stride = ((struct_size + align - 1) / align * align).max(struct_size) as u32;
        let capacity = volumes.len().min(MAX_SCATTER_VOLUMES).max(1) as u32;
        let buffer_size = (stride as u64) * (capacity as u64);

        let need_realloc = self.scatter.refraction_per_volume_buffer.is_none()
            || self.scatter.refraction_per_volume_stride != stride
            || self.scatter.refraction_per_volume_capacity < capacity;
        if need_realloc {
            let buf = device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("scatter_refraction_per_volume_uniform"),
                size: buffer_size,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            self.scatter.refraction_per_volume_buffer = Some(buf);
            self.scatter.refraction_per_volume_stride = stride;
            self.scatter.refraction_per_volume_capacity = capacity;
            self.scatter.refraction_per_volume_bg = None;
        }

        if self.scatter.refraction_per_volume_bg.is_none() {
            self.ensure_scatter_refraction_per_volume_bgl(device);
            let bgl = self.scatter.refraction_per_volume_bgl.as_ref().unwrap();
            let buf = self.scatter.refraction_per_volume_buffer.as_ref().unwrap();
            let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("scatter_refraction_per_volume_bg"),
                layout: bgl,
                entries: &[wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                        buffer: buf,
                        offset: 0,
                        size: std::num::NonZeroU64::new(struct_size),
                    }),
                }],
            });
            self.scatter.refraction_per_volume_bg = Some(bg);
        }

        let mut bytes = vec![0u8; buffer_size as usize];
        let mut n: u32 = 0;
        for (volume, _) in volumes.iter() {
            if n as usize >= MAX_SCATTER_VOLUMES {
                break;
            }
            if let Some(packed) = GpuRefractionVolume::pack(volume, time_seconds) {
                let offset = (n as usize) * (stride as usize);
                let src = bytemuck::bytes_of(&packed);
                bytes[offset..offset + src.len()].copy_from_slice(src);
                n += 1;
            }
        }
        if let Some(buf) = self.scatter.refraction_per_volume_buffer.as_ref() {
            queue.write_buffer(
                buf,
                0,
                &bytes[..(n as usize * stride as usize).max(stride as usize)],
            );
        }
        n
    }

    /// Stride between dynamic-offset slots in the refraction per-volume buffer.
    pub(crate) fn scatter_refraction_per_volume_stride(&self) -> u32 {
        self.scatter.refraction_per_volume_stride
    }

    /// Build the bind group sampling the refraction source texture + depth.
    pub(crate) fn make_scatter_refraction_source_bg(
        &mut self,
        device: &wgpu::Device,
        source_view: &wgpu::TextureView,
        depth_view: &wgpu::TextureView,
    ) -> wgpu::BindGroup {
        self.ensure_scatter_refraction_source_bgl(device);
        self.ensure_scatter_composite_pipeline(device, wgpu::TextureFormat::Rgba16Float);
        let bgl = self.scatter.refraction_source_bgl.as_ref().unwrap();
        let sampler = self.scatter.composite_sampler.as_ref().unwrap();
        device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("scatter_refraction_source_bg"),
            layout: bgl,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(source_view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::TextureView(depth_view),
                },
            ],
        })
    }

    /// Write the temporal-resolve uniform.
    pub(crate) fn write_scatter_temporal_uniform(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        prev_view_proj: [[f32; 4]; 4],
        blend: f32,
        history_valid: bool,
    ) {
        if self.scatter.temporal_resolve_uniform_buffer.is_none() {
            let buf = device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("scatter_temporal_resolve_uniform"),
                size: std::mem::size_of::<ScatterTemporalUniformRaw>() as u64,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            self.scatter.temporal_resolve_uniform_buffer = Some(buf);
        }
        let raw = ScatterTemporalUniformRaw {
            prev_view_proj,
            temporal_pack: [
                blend.clamp(0.0, 0.99),
                if history_valid { 1.0 } else { 0.0 },
                0.0,
                0.0,
            ],
        };
        if let Some(buf) = self.scatter.temporal_resolve_uniform_buffer.as_ref() {
            queue.write_buffer(buf, 0, bytemuck::bytes_of(&raw));
        }
    }
}