lactea-render 0.2.3

wgpu backend reproducing the MilkDrop 2 frame pipeline; host-provided device, renders into any texture.
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
// SPDX-FileCopyrightText: 2026 Gianluca Boiano
// SPDX-FileCopyrightText: 2005-2013 Nullsoft, Inc.
// SPDX-License-Identifier: (MIT OR Apache-2.0) AND BSD-3-Clause

//! Pass execution: batches, user shader passes, MD2 warp / composite, blur, blit and sprites.

use super::*;

impl Renderer {
    /// Replace the FFT texture's contents ([`TexSource::Fft`]). Recorded as a buffer copy in `encoder` rather than
    /// `queue.write_texture`: a queue write lands at the next submit, ahead of everything already recorded, so two
    /// frames of one renderer recorded into one encoder would both see the last spectrum.
    pub(super) fn record_fft_upload(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        fft: &[f32; FFT_BINS],
    ) {
        let staging = buffer_init(
            &self.device,
            "fft",
            fft.as_slice(),
            wgpu::BufferUsages::COPY_SRC,
        );
        encoder.copy_buffer_to_texture(
            wgpu::TexelCopyBufferInfo {
                buffer: &staging,
                layout: wgpu::TexelCopyBufferLayout {
                    offset: 0,
                    bytes_per_row: Some(FFT_BINS as u32 * 4),
                    rows_per_image: Some(1),
                },
            },
            wgpu::TexelCopyTextureInfo {
                texture: &self.fft.texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            wgpu::Extent3d {
                width: FFT_BINS as u32,
                height: 1,
                depth_or_array_layers: 1,
            },
        );
    }

    /// [`record_fft_upload`](Self::record_fft_upload) for BeatDrop's [`crate::BeatDropTexture`]: the two rows of
    /// `sampler_fft` in one copy, the two rows of `sampler_wave` in one copy each (a row of `NUM_WAVEFORM_SAMPLES`
    /// floats is not a multiple of the 256-byte row pitch a two-row copy needs).
    pub(super) fn record_beatdrop_upload(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        data: &crate::BeatDropTexture,
    ) {
        let Some(wave) = &self.wave else {
            return;
        };
        let texels = data.texels();
        let staging = buffer_init(
            &self.device,
            "beatdrop",
            texels.as_slice(),
            wgpu::BufferUsages::COPY_SRC,
        );
        let copy = |encoder: &mut wgpu::CommandEncoder,
                    tex: &Tex,
                    first_texel: usize,
                    y: u32,
                    rows: u32| {
            encoder.copy_buffer_to_texture(
                wgpu::TexelCopyBufferInfo {
                    buffer: &staging,
                    layout: wgpu::TexelCopyBufferLayout {
                        offset: (first_texel * 4) as u64,
                        // the pitch of a copy of several rows must be a multiple of 256 bytes; one row has none
                        bytes_per_row: (rows > 1).then_some(tex.w * 4),
                        rows_per_image: (rows > 1).then_some(rows),
                    },
                },
                wgpu::TexelCopyTextureInfo {
                    texture: &tex.texture,
                    mip_level: 0,
                    origin: wgpu::Origin3d { x: 0, y, z: 0 },
                    aspect: wgpu::TextureAspect::All,
                },
                wgpu::Extent3d {
                    width: tex.w,
                    height: rows,
                    depth_or_array_layers: 1,
                },
            );
        };
        copy(encoder, &self.fft, 0, 0, 2);
        let wave_at = 2 * crate::BEATDROP_FFT_BINS;
        copy(encoder, wave, wave_at, 0, 1);
        copy(encoder, wave, wave_at + crate::BEATDROP_WAVE_SAMPLES, 1, 1);
    }

    /// BeatDrop's `sampler_wave` as the translator hands it over: the user texture `"wave"` under
    /// `CompatRules::md2_shader_inputs` (the frozen ABI has no [`TexSource`] for it, so it cannot be told from a
    /// host-supplied image by its source; BeatDrop binds `m_lpWaveTexture` to the root name `wave` before looking
    /// for an image, and so does this). `None` for every other source.
    pub(super) fn engine_wave(&self, src: &TexSource) -> Option<&Tex> {
        match src {
            TexSource::User(name) if self.rules.md2_shader_inputs && name == "wave" => {
                self.wave.as_ref()
            }
            _ => None,
        }
    }

    pub(super) fn tex_bind_group(
        &self,
        view: &wgpu::TextureView,
        sampler: &wgpu::Sampler,
    ) -> wgpu::BindGroup {
        self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("tex"),
            layout: &self.pipes.bgl_tex,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(sampler),
                },
            ],
        })
    }

    /// Execute a fixed-function batch in one render pass.
    pub(super) fn run_batch(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        batch: &Batch,
        view: &wgpu::TextureView,
        load: wgpu::LoadOp<wgpu::Color>,
        tex_bg: Option<&wgpu::BindGroup>,
        leak_bg: Option<&wgpu::BindGroup>,
        label: &str,
    ) {
        self.run_batch_range(
            encoder,
            batch,
            0..batch.cmds.len(),
            view,
            load,
            tex_bg,
            leak_bg,
            label,
        );
    }

    /// [`Renderer::run_batch`] for the commands `cmds` only.
    pub(super) fn run_batch_range(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        batch: &Batch,
        cmds: std::ops::Range<usize>,
        view: &wgpu::TextureView,
        load: wgpu::LoadOp<wgpu::Color>,
        tex_bg: Option<&wgpu::BindGroup>,
        leak_bg: Option<&wgpu::BindGroup>,
        label: &str,
    ) {
        let wf_buf = buffer_init(&self.device, "wf", &batch.wf, wgpu::BufferUsages::VERTEX);
        let mesh_buf = buffer_init(
            &self.device,
            "mesh",
            &batch.mesh,
            wgpu::BufferUsages::VERTEX,
        );
        // `image=` textures: projectM's built-in noise textures by name, else the user texture (a 1x1 black
        // placeholder when the host had none, like projectM's `TextureManager` placeholder) with the sampler its
        // prefix asks for (no mip chain: filter only)
        let image_bgs: Vec<wgpu::BindGroup> = batch
            .images
            .iter()
            .map(|im| {
                let view = match noise::builtin_image_noise(&im.name) {
                    Some(n) => &self.noise[n.index()].view,
                    None => self
                        .user_tex
                        .get(&im.name)
                        .map_or(&self.dummy_2d.view, |u| u.view_at(self.anim_time)),
                };
                let filter = if im.linear {
                    Filter::Linear
                } else {
                    Filter::Point
                };
                let address = if im.wrap {
                    Address::Wrap
                } else {
                    Address::Clamp
                };
                self.tex_bind_group(view, self.pipes.sampler(filter, address, true))
            })
            .collect();
        let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some(label),
            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                view,
                depth_slice: None,
                resolve_target: None,
                ops: wgpu::Operations {
                    load,
                    store: wgpu::StoreOp::Store,
                },
            })],
            ..Default::default()
        });
        for &(kind, blend, prim, start, count) in &batch.cmds[cmds] {
            match kind {
                Kind::Flat => {
                    pass.set_pipeline(&self.pipes.flat[&(blend, prim, self.rules.main_alpha)]);
                    pass.set_vertex_buffer(0, wf_buf.slice(..));
                }
                // every caller binds the groups its batch kinds need; a draw without one is skipped
                Kind::Tex => {
                    let Some(bg) = tex_bg else {
                        continue;
                    };
                    pass.set_pipeline(&self.pipes.tex[&(blend, self.rules.main_alpha)]);
                    pass.set_bind_group(0, bg, &[]);
                    pass.set_vertex_buffer(0, mesh_buf.slice(..));
                }
                Kind::TexLeak => {
                    let Some(bg) = leak_bg else {
                        continue;
                    };
                    pass.set_pipeline(&self.pipes.tex[&(blend, self.rules.main_alpha)]);
                    pass.set_bind_group(0, bg, &[]);
                    pass.set_vertex_buffer(0, mesh_buf.slice(..));
                }
                Kind::TexImage(i) => {
                    pass.set_pipeline(&self.pipes.tex[&(blend, self.rules.main_alpha)]);
                    pass.set_bind_group(0, &image_bgs[i as usize], &[]);
                    pass.set_vertex_buffer(0, mesh_buf.slice(..));
                }
            }
            pass.draw(start..start + count, 0..1);
        }
    }

    pub(super) fn texsize_of(
        &self,
        src: &TexSource,
        user_tex: &HashMap<String, UserTex>,
    ) -> [f32; 4] {
        let (w, h) = match src {
            TexSource::Main => (self.cfg.width, self.cfg.height),
            TexSource::Blur(k) if (1..=3).contains(k) => {
                let t = &self.blur[*k as usize * 2 - 1];
                (t.w, t.h)
            }
            TexSource::Blur(_) => (1, 1),
            TexSource::Noise(n) => {
                let t = &self.noise[n.index()];
                (t.w, t.h)
            }
            TexSource::User(name) => match self.engine_wave(src) {
                Some(w) => (w.w, w.h),
                None => match user_tex.get(&name.to_ascii_lowercase()) {
                    Some(u) => (u.tex.w, u.tex.h),
                    None => (1, 1),
                },
            },
            TexSource::Fft => (self.fft.w, self.fft.h),
        };
        [w as f32, h as f32, 1.0 / w as f32, 1.0 / h as f32]
    }

    /// Uniforms of the GPU warp vertex stage ([`shaders::warp_vs`]).
    pub(super) fn warp_vs_uniforms(
        &self,
        wg: &crate::frame::WarpGpu,
        toff: [f32; 2],
        diffuse: f32,
    ) -> WarpVsUniforms {
        let (ax, ay) = (self.geom.aspect_x, self.geom.aspect_y);
        WarpVsUniforms {
            aspect: [ax, ay, 1.0 / ax, 1.0 / ay],
            timing: [wg.anim.warp_time, wg.anim.scale_inv, toff[0], toff[1]],
            factors: wg.anim.f,
            diffuse: [diffuse, diffuse, diffuse, 1.0],
        }
    }

    pub(super) fn warp_vs_bind_group(&self, vu: &WarpVsUniforms) -> wgpu::BindGroup {
        let buf = buffer_init(
            &self.device,
            "warp_vs",
            std::slice::from_ref(vu),
            wgpu::BufferUsages::UNIFORM,
        );
        self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("warp_vs"),
            layout: &self.pipes.bgl_warp_vs,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: buf.as_entire_binding(),
            }],
        })
    }

    /// `WarpedBlit_NoShaders` with the GPU vertex stage: `next = prev(uv) * decay`.
    pub(super) fn run_warp_gpu_plain(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        verts: &[WarpVertex],
        vu: &WarpVsUniforms,
        src: &wgpu::TextureView,
        samp: &wgpu::Sampler,
        target: &wgpu::TextureView,
    ) {
        let tex_bg = self.tex_bind_group(src, samp);
        let vs_bg = self.warp_vs_bind_group(vu);
        let vb = buffer_init(
            &self.device,
            "warp verts",
            verts,
            wgpu::BufferUsages::VERTEX,
        );
        let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some("warp (no shader, gpu uv)"),
            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                view: target,
                depth_slice: None,
                resolve_target: None,
                ops: wgpu::Operations {
                    load: wgpu::LoadOp::Clear(BLACK),
                    store: wgpu::StoreOp::Store,
                },
            })],
            ..Default::default()
        });
        rp.set_pipeline(&self.pipes.warp_tex[&self.rules.main_alpha]);
        rp.set_bind_group(0, &tex_bg, &[]);
        rp.set_bind_group(1, &vs_bg, &[]);
        rp.set_vertex_buffer(0, vb.slice(..));
        if let Some((ib, n)) = &self.warp_indices {
            rp.set_index_buffer(ib.slice(..), wgpu::IndexFormat::Uint32);
            rp.draw_indexed(0..*n, 0, 0..1);
        }
    }

    /// Draw a user warp / composite shader over `verts` into `target`.
    pub(super) fn run_shader_pass(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        pass: &ShaderPass,
        mu: &MilkUniforms,
        ext: &ShaderExt,
        main_view: &wgpu::TextureView,
        verts: PassVerts<'_>,
        target: &wgpu::TextureView,
        label: &str,
        warp_wrap: Option<Address>,
    ) {
        self.run_shader_pass_ex(
            encoder,
            pass,
            &self.user_tex,
            PassBlend::Replace,
            mu,
            ext,
            main_view,
            verts,
            target,
            label,
            warp_wrap,
        );
    }

    /// [`Renderer::run_shader_pass`] with the user textures of the pass's own preset (`m_OldShaders` keeps the old
    /// preset's) and MD2's blend-pass output modes: `Replace` clears the target first, the alpha modes keep it and
    /// blend the shader's output by the vertex alpha.
    pub(super) fn run_shader_pass_ex(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        pass: &ShaderPass,
        user_tex: &HashMap<String, UserTex>,
        blend: PassBlend,
        mu: &MilkUniforms,
        ext: &ShaderExt,
        main_view: &wgpu::TextureView,
        verts: PassVerts<'_>,
        target: &wgpu::TextureView,
        label: &str,
        warp_wrap: Option<Address>,
    ) {
        let mu_buf = buffer_init(
            &self.device,
            "mu",
            std::slice::from_ref(mu),
            wgpu::BufferUsages::UNIFORM,
        );
        let mx_buf = buffer_init(
            &self.device,
            "mx",
            std::slice::from_ref(&ext.uniforms()),
            wgpu::BufferUsages::UNIFORM,
        );
        let bg0 = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("mu"),
            layout: &self.pipes.bgl_mu,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: mu_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: BINDING_MILK_EXT,
                    resource: mx_buf.as_entire_binding(),
                },
            ],
        });

        let mut pu = PassUniforms::default();
        for (i, src) in pass
            .interface
            .texsize_slots
            .iter()
            .enumerate()
            .take(lactea_abi::MAX_PASS_TEXTURES)
        {
            pu.texsize[i] = self.texsize_of(src, user_tex);
        }
        let pu_buf = buffer_init(
            &self.device,
            "pu",
            std::slice::from_ref(&pu),
            wgpu::BufferUsages::UNIFORM,
        );

        let mut entries: Vec<wgpu::BindGroupEntry> = vec![wgpu::BindGroupEntry {
            binding: lactea_abi::BINDING_PASS_UNIFORMS,
            resource: pu_buf.as_entire_binding(),
        }];
        // projectM `WarpedBlit`: the per-frame `wrap` sampler replaces the one of the first `main` descriptor.
        let wrap_slot = warp_wrap.and_then(|_| {
            let name_of = |t: &lactea_abi::TextureBinding| {
                t.hlsl_name
                    .strip_prefix("sampler_")
                    .unwrap_or(&t.hlsl_name)
                    .to_owned()
            };
            let first = pass
                .interface
                .textures
                .iter()
                .enumerate()
                .filter(|(_, t)| matches!(t.source, TexSource::Main))
                .min_by(|a, b| name_of(a.1).cmp(&name_of(b.1)))?;
            // `main` is always in the set: if it sorts first and the shader does not use it, no bound sampler changes
            (name_of(first.1).as_str() <= "main").then_some(first.0)
        });
        for (i, t) in pass.interface.textures.iter().enumerate() {
            let want3d = t.dim == TexDim::D3;
            let view: &wgpu::TextureView = match &t.source {
                TexSource::Main => main_view,
                TexSource::Blur(k) if (1..=3).contains(k) => &self.blur[*k as usize * 2 - 1].view,
                TexSource::Blur(_) => &self.dummy_2d.view,
                TexSource::Noise(n) => &self.noise[n.index()].view,
                TexSource::User(name) => match self.engine_wave(&t.source) {
                    Some(w) => &w.view,
                    None => match user_tex.get(&name.to_ascii_lowercase()) {
                        Some(u) => u.view_at(self.anim_time),
                        None => &self.dummy_2d.view,
                    },
                },
                TexSource::Fft => &self.fft.view,
            };
            // the bound view must match the declared dimensionality
            let is3d = matches!(
                &t.source,
                TexSource::Noise(NoiseTex::VolLq | NoiseTex::VolHq)
            );
            let view = if want3d == is3d {
                view
            } else if want3d {
                &self.dummy_3d.view
            } else {
                &self.dummy_2d.view
            };
            entries.push(wgpu::BindGroupEntry {
                binding: lactea_abi::texture_binding(i as u32),
                resource: wgpu::BindingResource::TextureView(view),
            });
            let (filter, address) = match (wrap_slot == Some(i), warp_wrap) {
                (true, Some(a)) => (Filter::Linear, a),
                _ => (t.filter, t.address),
            };
            entries.push(wgpu::BindGroupEntry {
                binding: lactea_abi::sampler_binding(i as u32),
                resource: wgpu::BindingResource::Sampler(self.pipes.sampler(
                    filter,
                    address,
                    self.rules.no_mipmaps,
                )),
            });
        }
        let bg1 = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("pass"),
            layout: &pass.pass_bgl,
            entries: &entries,
        });

        let (vb, count, vs_bg) = match &verts {
            PassVerts::Mesh(v) => (
                buffer_init(&self.device, "verts", v, wgpu::BufferUsages::VERTEX),
                v.len(),
                None,
            ),
            PassVerts::Warp(v, vu) => (
                buffer_init(&self.device, "warp verts", v, wgpu::BufferUsages::VERTEX),
                v.len(),
                Some(self.warp_vs_bind_group(vu)),
            ),
        };
        let sink = pass.uv_sink.then_some(wgpu::RenderPassColorAttachment {
            view: &self.warp_sink.view,
            depth_slice: None,
            resolve_target: None,
            ops: wgpu::Operations {
                load: wgpu::LoadOp::Clear(BLACK),
                store: wgpu::StoreOp::Store,
            },
        });
        let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some(label),
            color_attachments: &[
                Some(wgpu::RenderPassColorAttachment {
                    view: target,
                    depth_slice: None,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: match blend {
                            PassBlend::Replace => wgpu::LoadOp::Clear(BLACK),
                            _ => wgpu::LoadOp::Load,
                        },
                        store: wgpu::StoreOp::Store,
                    },
                }),
                sink,
            ][..if pass.uv_sink { 2 } else { 1 }],
            ..Default::default()
        });
        let alpha_pipeline = match (blend, pass.alpha_pipelines.as_ref()) {
            (PassBlend::SrcAlpha, Some(a)) => Some(&a[0]),
            (PassBlend::InvSrcAlpha, Some(a)) => Some(&a[1]),
            (PassBlend::Replace, _) => Some(&pass.pipeline),
            _ => None,
        };
        let pipeline = match &vs_bg {
            Some(_) => pass.warp_pipeline.as_ref(),
            None => alpha_pipeline,
        };
        // `render_frame` rejects the inputs that lack a pipeline (a blend without `md2_preset_blend`); a
        // warp-vertex pass is only requested for warp shader passes.
        let Some(pipeline) = pipeline else {
            return;
        };
        rp.set_pipeline(pipeline);
        if let Some(bg) = &vs_bg {
            rp.set_bind_group(2, bg, &[]);
        }
        rp.set_bind_group(0, &bg0, &[]);
        rp.set_bind_group(1, &bg1, &[]);
        rp.set_vertex_buffer(0, vb.slice(..));
        match (&verts, &self.warp_indices) {
            (PassVerts::Mesh(_), _) => rp.draw(0..count as u32, 0..1),
            (PassVerts::Warp(..), Some((ib, n))) => {
                rp.set_index_buffer(ib.slice(..), wgpu::IndexFormat::Uint32);
                rp.draw_indexed(0..*n, 0, 0..1);
            }
            // only projectM's rules request a warp-vertex pass, and they always have the index buffer
            (PassVerts::Warp(..), None) => {}
        }
    }

    /// The warp pass of `RenderFrame` while MD2 blends two presets: `WarpedBlit_Shaders` / `WarpedBlit_NoShaders` once
    /// or twice depending on which of the two presets use a warp shader ([`blend::warp_passes`]), all on the *blended*
    /// warp mesh (`warp_uv`, per-vertex alpha `bl.vert_alpha`). Pass 0 is opaque (old shader / old state), pass 1
    /// is alpha-blended over it (new shader); tiles whose alphas make a pass invisible are not drawn.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn run_md2_warp(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        input: &FrameInput,
        bl: &blend::Md2Blend,
        warp_uv: &[[f32; 2]],
        decay: f32,
        toff: [f32; 2],
        sampler_wrap: Address,
        prev_i: usize,
        next_i: usize,
    ) {
        let rules = self.rules;
        let (w, h) = (self.cfg.width, self.cfg.height);
        let old = self.old_shaders.as_ref();
        let passes = blend::warp_passes(
            old.is_some_and(|o| o.warp.is_some()),
            self.warp_shader.is_some(),
        );
        let src = &self.main[prev_i].view;
        let target = &self.main[next_i].view;
        for dp in passes {
            let cull = dp.cull_tiles.then_some(dp.flip_culling);
            let mode = match (dp.alpha_blend, dp.flip_alpha) {
                (false, _) => PassBlend::Replace,
                (true, false) => PassBlend::SrcAlpha,
                (true, true) => PassBlend::InvSrcAlpha,
            };
            if dp.shaders {
                let (pass, user_tex, q, blur, ext, rand) = match dp.which {
                    Which::New => {
                        let Some(pass) = self.warp_shader.as_ref() else {
                            continue;
                        };
                        (
                            pass,
                            &self.user_tex,
                            &input.q,
                            &input.blur,
                            &input.ext,
                            input.warp_random.as_ref(),
                        )
                    }
                    Which::Old => {
                        let Some((o, pass)) = old.and_then(|o| Some((o, o.warp.as_ref()?))) else {
                            continue;
                        };
                        (
                            pass,
                            &o.user_tex,
                            &bl.old.q,
                            &bl.old.blur,
                            &bl.old.ext,
                            bl.old.warp_random.as_ref(),
                        )
                    }
                };
                let diffuse = if rules.shader_warp_diffuse_is_decay {
                    decay
                } else {
                    1.0
                };
                let mut rng = Rng::new(self.cfg.seed).fork(input.frame as u64 * 4);
                let mu = uniforms::fill_milk_uniforms(
                    &MilkUniformInputs {
                        q,
                        blur,
                        ext,
                        ..MilkUniformInputs::for_frame(w, h, input, &self.rot, &rules, rand)
                    },
                    &mut rng,
                );
                let mut verts = geom::build_warp_vertices_blend(
                    &self.geom,
                    warp_uv,
                    [diffuse, diffuse, diffuse],
                    &bl.vert_alpha,
                    toff,
                    rules.ang_seam_fix,
                    cull,
                );
                geom::fix_nan_uv_triangles(&mut verts);
                let warp_wrap = rules.warp_main_sampler_from_wrap.then_some(sampler_wrap);
                self.run_shader_pass_ex(
                    encoder,
                    pass,
                    user_tex,
                    mode,
                    &mu,
                    ext,
                    src,
                    PassVerts::Mesh(&verts),
                    target,
                    "warp (shader, blend)",
                    warp_wrap,
                );
            } else {
                let mut verts = geom::build_warp_vertices_blend(
                    &self.geom,
                    warp_uv,
                    [decay, decay, decay],
                    &bl.vert_alpha,
                    toff,
                    rules.ang_seam_fix,
                    cull,
                );
                geom::fix_nan_uv_triangles(&mut verts);
                let mut b = Batch::default();
                b.tex(
                    match mode {
                        PassBlend::Replace => Blend::Replace,
                        PassBlend::SrcAlpha => Blend::SrcAlpha,
                        PassBlend::InvSrcAlpha => Blend::InvSrcAlpha,
                    },
                    &verts,
                );
                let bg = self
                    .tex_bind_group(src, self.pipes.sampler(Filter::Linear, sampler_wrap, false));
                self.run_batch(
                    encoder,
                    &b,
                    target,
                    if mode == PassBlend::Replace {
                        wgpu::LoadOp::Clear(BLACK)
                    } else {
                        wgpu::LoadOp::Load
                    },
                    Some(&bg),
                    None,
                    "warp (no shader, blend)",
                );
            }
        }
    }

    /// The composite of `RenderFrame` while MD2 blends two presets: `ShowToUser_NoShaders` (whole quad, from the
    /// blended per-frame values) and / or `ShowToUser_Shaders` with the old and the new composite shader on the 32x24
    /// composite mesh, whose vertex alphas are looked up in the warp mesh's ([`blend::comp_passes`],
    /// [`blend::comp_vertex_alpha`]).
    pub(super) fn run_md2_comp(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        input: &FrameInput,
        bl: &blend::Md2Blend,
        aspect: (f32, f32),
        prev_i: usize,
        next_i: usize,
    ) {
        let rules = self.rules;
        let (w, h) = (self.cfg.width, self.cfg.height);
        let old = self.old_shaders.as_ref();
        let passes = blend::comp_passes(
            old.is_some_and(|o| o.comp.is_some()),
            self.comp_shader.is_some(),
        );
        let shade = geom::hue_shade(
            input.time as f32,
            input.hue_offsets.unwrap_or(self.rot.hue_offsets),
        );
        let mut grid = geom::comp_grid(w, h, aspect, &shade, rules.comp_uv_half_texel);
        let mut alpha = vec![255u8; grid.len()];
        for j in 0..geom::FCGSY {
            for i in 0..geom::FCGSX {
                let (x01, y01) = blend::comp_vertex_xy01(i, j, w, h);
                let a = blend::comp_vertex_alpha(
                    &bl.vert_alpha,
                    [self.geom.gx, self.geom.gy],
                    x01,
                    y01,
                );
                alpha[i + j * geom::FCGSX] = a;
                grid[i + j * geom::FCGSX].diffuse[3] = f32::from(a) / 255.0;
            }
        }
        let idx = geom::comp_indices();
        for dp in passes {
            if dp.shaders {
                let mut verts = Vec::with_capacity(idx.len());
                for t in idx.as_chunks::<3>().0 {
                    if !dp.cull_tiles
                        || blend::tile_needed(
                            [alpha[t[0]], alpha[t[1]], alpha[t[2]]],
                            dp.flip_culling,
                        )
                    {
                        verts.extend(t.iter().map(|&k| grid[k]));
                    }
                }
                let mode = match (dp.alpha_blend, dp.flip_alpha) {
                    (false, _) => PassBlend::Replace,
                    (true, false) => PassBlend::SrcAlpha,
                    (true, true) => PassBlend::InvSrcAlpha,
                };
                let (pass, user_tex, q, blur, ext, rand) = match dp.which {
                    Which::New => {
                        let Some(pass) = self.comp_shader.as_ref() else {
                            continue;
                        };
                        (
                            pass,
                            &self.user_tex,
                            &input.q,
                            &input.blur,
                            &input.ext,
                            input.comp_random.as_ref(),
                        )
                    }
                    Which::Old => {
                        let Some((o, pass)) = old.and_then(|o| Some((o, o.comp.as_ref()?))) else {
                            continue;
                        };
                        (
                            pass,
                            &o.user_tex,
                            &bl.old.q,
                            &bl.old.blur,
                            &bl.old.ext,
                            bl.old.comp_random.as_ref(),
                        )
                    }
                };
                let mut rng = Rng::new(self.cfg.seed).fork(input.frame as u64 * 4 + 1);
                let mu = uniforms::fill_milk_uniforms(
                    &MilkUniformInputs {
                        q,
                        blur,
                        ext,
                        ..MilkUniformInputs::for_frame(w, h, input, &self.rot, &rules, rand)
                    },
                    &mut rng,
                );
                let comp_src = if rules.comp_reads_previous {
                    prev_i
                } else {
                    next_i
                };
                let main_view = self.comp_source_view(&self.main[comp_src].view);
                self.run_shader_pass_ex(
                    encoder,
                    pass,
                    user_tex,
                    mode,
                    &mu,
                    ext,
                    main_view,
                    PassVerts::Mesh(&verts),
                    &self.final_tex.view,
                    "comp (shader, blend)",
                    None,
                );
            } else {
                let b = self.build_noshader_comp(input);
                let wrap = if self.rules.noshader_comp_wrap {
                    Address::Wrap
                } else {
                    Address::Clamp
                };
                let src_view = self.comp_source_view(&self.main[next_i].view);
                let bg =
                    self.tex_bind_group(src_view, self.pipes.sampler(Filter::Linear, wrap, false));
                self.run_batch(
                    encoder,
                    &b,
                    &self.final_tex.view,
                    wgpu::LoadOp::Clear(BLACK),
                    Some(&bg),
                    None,
                    "comp (no shader, blend)",
                );
            }
        }
    }

    /// `BlurPasses`.
    pub(super) fn run_blur(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        input: &FrameInput,
        source: &wgpu::TextureView,
        levels: u8,
    ) {
        let passes = blur::pass_count(levels);
        let (bmin, bmax) = blur::safe_blur_min_max(input.blur.min, input.blur.max);
        let (scale, bias) = blur::scale_bias(bmin, bmax);
        let ht = blur::horizontal_taps();
        let vt = blur::vertical_taps();
        let edge_darken = input.blur.edge_darken;
        let clamp_linear = self.pipes.sampler(Filter::Linear, Address::Clamp, false);
        for i in 0..passes {
            let (src_view, sw, sh) = if i == 0 {
                (source, self.cfg.width as f32, self.cfg.height as f32)
            } else {
                (
                    &self.blur[i - 1].view,
                    self.blur[i - 1].w as f32,
                    self.blur[i - 1].h as f32,
                )
            };
            let c0 = [sw, sh, 1.0 / sw, 1.0 / sh];
            let mut p = [[0.0f32; 4]; 6]; // c0, c1, c2, c3, c5, c6
            p[0] = c0;
            if i % 2 == 0 {
                p[1] = ht.w;
                p[2] = ht.d;
                // projectM samples the (upside-down stored) source of the first pass with
                // `uv.y` flipped, so its "+1 texel" bias moves the other way (c3.w).
                let vsign = if i == 0 && self.rules.blur_src_flipped {
                    -1.0
                } else {
                    1.0
                };
                p[3] = [scale[i / 2], bias[i / 2], ht.w_div, vsign];
            } else {
                p[4] = [vt.w[0], vt.w[1], vt.d[0], vt.d[1]];
                p[5] = if i == 1 {
                    [vt.w_div, 1.0 - edge_darken, edge_darken, 5.0]
                } else {
                    [vt.w_div, 1.0, 0.0, 5.0]
                };
            }
            let make = || {
                let ub = buffer_init(
                    &self.device,
                    "blur params",
                    std::slice::from_ref(&p),
                    wgpu::BufferUsages::UNIFORM,
                );
                self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some("blur"),
                    layout: &self.pipes.bgl_blur,
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: ub.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: wgpu::BindingResource::TextureView(src_view),
                        },
                        wgpu::BindGroupEntry {
                            binding: 2,
                            resource: wgpu::BindingResource::Sampler(clamp_linear),
                        },
                    ],
                })
            };
            // Pass 0 reads the caller's source image; every later pass reads `blur[i - 1]`, so its bind group
            // only changes with the uniform values (constant unless the preset's blur settings move).
            let bg = if i == 0 {
                make()
            } else {
                let mut cache = self.blur_bg.borrow_mut();
                if cache.len() <= i {
                    cache.resize(i + 1, None);
                }
                match &cache[i] {
                    Some((key, bg)) if *key == p => bg.clone(),
                    _ => {
                        let bg = make();
                        cache[i] = Some((p, bg.clone()));
                        bg
                    }
                }
            };
            let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("blur"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &self.blur[i].view,
                    depth_slice: None,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(BLACK),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                ..Default::default()
            });
            rp.set_pipeline(if i % 2 == 0 {
                &self.pipes.blur_h
            } else {
                &self.pipes.blur_v
            });
            rp.set_bind_group(0, &bg, &[]);
            rp.draw(0..3, 0..1);
        }
    }

    /// The blit's uniform buffer and bind group depend only on the sizes, the output format and `final_tex`, so they
    /// are built once and dropped when `final_tex` is replaced (`resize`).
    pub(super) fn blit(&self, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView) {
        let mut cache = self.blit_bg.borrow_mut();
        let bg = cache.get_or_insert_with(|| {
            let (w, h) = (self.cfg.width as f32, self.cfg.height as f32);
            let srgb = if self.cfg.output_format.is_srgb() {
                1.0
            } else {
                0.0
            };
            let params: [[f32; 4]; 2] = [[w, h, w, h], [srgb, 0.0, 0.0, 0.0]];
            let ub = buffer_init(
                &self.device,
                "blit",
                std::slice::from_ref(&params),
                wgpu::BufferUsages::UNIFORM,
            );
            self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("blit"),
                layout: &self.pipes.bgl_blit,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: ub.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::TextureView(&self.final_tex.view),
                    },
                ],
            })
        });
        let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some("blit"),
            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                view: target,
                depth_slice: None,
                resolve_target: None,
                ops: wgpu::Operations {
                    load: wgpu::LoadOp::Clear(BLACK),
                    store: wgpu::StoreOp::Store,
                },
            })],
            ..Default::default()
        });
        rp.set_pipeline(&self.pipes.blit);
        rp.set_bind_group(0, &*bg, &[]);
        rp.draw(0..3, 0..1);
    }

    // ---- MD3 sprites -------------------------------------------------------------------------

    pub(super) fn sprite_pass(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        view: &wgpu::TextureView,
        draws: &[&SpriteDraw],
    ) {
        self.pipes.sprites(&self.device).draw(
            &self.device,
            encoder,
            view,
            &self.pipes.bgl_tex,
            self.pipes.sampler(Filter::Linear, Address::Wrap, false),
            &self.sprite_images,
            draws,
        );
    }

    /// The sprites of `layer` (1..=4) onto `view`, one after the other in the order of `FrameInput::sprites`. With
    /// `CompatRules::md3_sprite_burn_twice` a burning sprite is drawn a second time right after the first (MD3 burns
    /// into the very image it draws on).
    pub(super) fn draw_sprite_layer(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        input: &FrameInput,
        layer: u8,
        view: &wgpu::TextureView,
    ) {
        let mut draws: Vec<&SpriteDraw> = Vec::new();
        for s in input.sprites.iter().filter(|s| s.layer == layer) {
            draws.push(s);
            if s.burn && self.rules.md3_sprite_burn_twice {
                draws.push(s);
            }
        }
        if !draws.is_empty() {
            self.sprite_pass(encoder, view, &draws);
        }
    }

    /// Layer 0: the sprites onto the composited image; those that burn are also drawn into the feedback image,
    /// which the composite has already read.
    pub(super) fn draw_top_sprites(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        input: &FrameInput,
        final_view: &wgpu::TextureView,
        feedback_view: &wgpu::TextureView,
    ) {
        let draws: Vec<&SpriteDraw> = input.sprites.iter().filter(|s| s.layer == 0).collect();
        if draws.is_empty() {
            return;
        }
        self.sprite_pass(encoder, final_view, &draws);
        let burn: Vec<&SpriteDraw> = draws.iter().copied().filter(|s| s.burn).collect();
        if !burn.is_empty() {
            self.sprite_pass(encoder, feedback_view, &burn);
        }
    }

    /// Shapes, waves, main wave and borders into the feedback image `main[next_i]`. Sprite layers 2 (after the
    /// shapes) and 3 (after the waves) split the batch into several passes; without them it is one pass.
    pub(super) fn run_overlay(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        input: &FrameInput,
        b: &Batch,
        next_i: usize,
        bg: &wgpu::BindGroup,
        leak: &wgpu::BindGroup,
    ) {
        const LABEL: &str = "shapes+waves+borders";
        let view = &self.main[next_i].view;
        if !input.sprites.iter().any(|s| matches!(s.layer, 2 | 3)) {
            self.run_batch(
                encoder,
                b,
                view,
                wgpu::LoadOp::Load,
                Some(bg),
                Some(leak),
                LABEL,
            );
            return;
        }
        let end = b.cmds.len();
        for (range, layer) in [
            (0..b.marks[0], Some(2)),
            (b.marks[0]..b.marks[1], Some(3)),
            (b.marks[1]..end, None),
        ] {
            if !range.is_empty() {
                self.run_batch_range(
                    encoder,
                    b,
                    range,
                    view,
                    wgpu::LoadOp::Load,
                    Some(bg),
                    Some(leak),
                    LABEL,
                );
            }
            if let Some(layer) = layer {
                self.draw_sprite_layer(encoder, input, layer, view);
            }
        }
    }
}