agg-gui-wgpu 0.5.0

Hardware-accelerated wgpu renderer for agg-gui — DrawCtx via Vulkan/DX12/Metal/WebGL2, with SSAA, screenshot capture, and a custom render-pass hook
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
//! All `wgpu::RenderPipeline` objects and associated `wgpu::BindGroupLayout`
//! resources created once at startup.
//!
//! `WgpuPipelines` is created in `WgpuGfxCtx::new()` and stored for the
//! lifetime of the context.  Every pipeline, bind-group layout, and shared
//! sampler lives here.
//!
//! Uniform data is NOT stored here — each draw command in `end_frame` creates
//! a small `wgpu::Buffer` via `device.create_buffer_init()` so that multiple
//! draw commands of the same pipeline type in one frame do not overwrite each
//! other's uniforms.

use std::mem::size_of;

use bytemuck::{Pod, Zeroable};

use crate::shaders::{
    AA_SOLID_WGSL, AA_TEXTURE_WGSL, GRADIENT_WGSL, LAYER_WGSL, LCB_FLATTEN_WGSL, LCB_WGSL,
    LCD_WGSL, SOLID_WGSL, TEXT_GRAY_WGSL, TEX_DOWNSAMPLE_3X_WGSL, TEX_DOWNSAMPLE_4X_WGSL, TEX_WGSL,
};

// ---------------------------------------------------------------------------
// Uniform structs (byte layouts must exactly match WGSL structs in shaders.rs)
// ---------------------------------------------------------------------------

/// 32-byte uniform block for the solid and AA-solid pipelines.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub(crate) struct SolidUniforms {
    pub resolution: [f32; 2],
    pub _pad: [f32; 2],
    pub color: [f32; 4],
}
const _: () = assert!(size_of::<SolidUniforms>() == 32);

/// 32-byte uniform block for the AA-texture pipeline (agg-sharp port).
/// Same layout as `SolidUniforms` but kept as a separate type so the
/// pipeline plumbing can read intent at a glance.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub(crate) struct AaTexUniforms {
    pub resolution: [f32; 2],
    pub _pad: [f32; 2],
    pub color: [f32; 4],
}
const _: () = assert!(size_of::<AaTexUniforms>() == 32);

/// 32-byte uniform block for the textured-quad pipeline. `tint` is
/// a per-draw RGBA multiplier so callers can fade image blits — set
/// `[1.0, 1.0, 1.0, alpha]` for a straight alpha fade, or zero out
/// channels for a quick recolor. The 4×4-box downsample pipeline
/// reuses the same layout and simply doesn't read `tint`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub(crate) struct TexUniforms {
    pub resolution: [f32; 2],
    pub _pad: [f32; 2],
    pub tint: [f32; 4],
}
const _: () = assert!(size_of::<TexUniforms>() == 32);

/// 48-byte uniform block for the layer-composite pipeline.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub(crate) struct LayerUniforms {
    pub resolution: [f32; 2],
    pub alpha: f32,
    pub mask_enabled: u32,
    pub layer_size: [f32; 2],
    pub mask_radius: f32,
    pub _pad0: f32,
    pub mask_rect: [f32; 4],
}
const _: () = assert!(size_of::<LayerUniforms>() == 48);

/// 32-byte uniform block for the LCD subpixel-text pipeline.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub(crate) struct LcdUniforms {
    pub resolution: [f32; 2],
    pub channel: u32,
    pub _pad: u32,
    pub color: [f32; 4],
}
const _: () = assert!(size_of::<LcdUniforms>() == 32);

/// 16-byte uniform block for the LCD backbuffer composite pipeline.
///
/// `global_alpha` folds the context's active alpha into the premultiplied
/// blit so a direct `set_global_alpha` fades cached LCD text — matching the
/// Textured and LcdMask paths, which already carry it.  Occupies the slot
/// that used to be dead padding, so the struct is still 16 bytes.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub(crate) struct LcbUniforms {
    pub resolution: [f32; 2],
    pub channel: u32,
    pub global_alpha: f32,
}
const _: () = assert!(size_of::<LcbUniforms>() == 16);

// ---------------------------------------------------------------------------
// Blend states
// ---------------------------------------------------------------------------

/// Standard 2-D composite: src-alpha / one-minus-src-alpha for colour, with
/// premultiplied-style alpha accumulation so the same pipelines work both
/// against the surface (alpha stays pinned at 1) and against transparent layer
/// textures (alpha builds up correctly so the layer composite has something
/// to alpha-blend against).
///
/// Earlier wired this with `alpha = { Zero, One }` to "preserve framebuffer
/// alpha", which is fine on the surface but leaves layer textures stuck at
/// alpha=0 — the composite then degenerates into pure additive blending and
/// every windowed widget renders as washed-out white.  `One / OMSA` is a
/// fixed-point at 1 on the surface AND accumulates correctly on a layer.
const BLEND_STANDARD: wgpu::BlendState = wgpu::BlendState {
    color: wgpu::BlendComponent {
        src_factor: wgpu::BlendFactor::SrcAlpha,
        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,
    },
};

/// Premultiplied src-over for layer compositing and LCB backbuffer blits.
const BLEND_PREMUL: wgpu::BlendState = 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,
    },
};

// ---------------------------------------------------------------------------
// WgpuPipelines
// ---------------------------------------------------------------------------

/// All render pipelines, bind-group layouts, and shared samplers.
///
/// Created once per [`crate::WgpuGfxCtx`] and reused for the lifetime of the
/// context.  The struct is publicly visible so library primitives like
/// [`crate::ssaa::SsaaFramebuffer::blit_to`] can take a reference and reuse
/// the shared 2-D textured-quad pipeline; the fields themselves stay
/// `pub(crate)` so external code can hold a `&WgpuPipelines` but cannot
/// reach in and rebuild individual pipelines.
pub struct WgpuPipelines {
    // ── Solid colour ─────────────────────────────────────────────────────────
    pub solid_pipeline: wgpu::RenderPipeline,
    pub solid_bgl: wgpu::BindGroupLayout,

    // ── AA solid colour (per-vertex alpha from tess2 halo strip) ─────────────
    pub aa_solid_pipeline: wgpu::RenderPipeline,
    pub aa_solid_bgl: wgpu::BindGroupLayout,

    // ── AA texture (agg-sharp `Graphics2DGpu` port; 1024-wide α-step texture).
    pub aa_texture_pipeline: wgpu::RenderPipeline,
    pub aa_texture_bgl0: wgpu::BindGroupLayout,
    pub aa_texture_bgl1: wgpu::BindGroupLayout,

    // ── Gradient (linear + radial, SVG spread modes) ──────────────────────────
    pub gradient_pipeline: wgpu::RenderPipeline,
    pub gradient_bgl0: wgpu::BindGroupLayout,
    pub gradient_bgl1: wgpu::BindGroupLayout,

    // ── Textured quad (image blit, Label backbuffer) ──────────────────────────
    pub tex_pipeline: wgpu::RenderPipeline,
    pub tex_bgl0: wgpu::BindGroupLayout,
    pub tex_bgl1: wgpu::BindGroupLayout,

    // ── 3×3-box downsample (SSAA at 3× linear) ─────────────────────────────
    /// Same vertex / bind groups as `tex_pipeline`; fragment shader runs
    /// 9 point-aligned taps at {-1, 0, +1}² texel offsets for an exact 3×3
    /// box average.  Used by the cube widget at the 9× SSAA setting where
    /// a single bilinear minification would drop 5 of 9 source texels per
    /// output pixel (visually indistinguishable from no AA).
    pub tex_downsample_3x_pipeline: wgpu::RenderPipeline,

    // ── 4×4-box downsample (SSAA at 4× linear) ─────────────────────────────
    /// Same vertex / bind groups as `tex_pipeline`; fragment shader runs
    /// 4 bilinear taps in a 2×2 quadrant grid for an exact 4×4 box average.
    /// Used by the cube widget at the highest SSAA setting where a single
    /// bilinear minification would drop 12 of 16 source texels per output.
    pub tex_downsample_4x_pipeline: wgpu::RenderPipeline,

    // ── Layer composite (SDF rounded-corner mask in fragment shader) ──────────
    pub layer_pipeline: wgpu::RenderPipeline,
    pub layer_bgl0: wgpu::BindGroupLayout,
    pub layer_bgl1: wgpu::BindGroupLayout,

    // ── LCD subpixel text — three pipelines, one per colour channel ───────────
    pub lcd_r: wgpu::RenderPipeline,
    pub lcd_g: wgpu::RenderPipeline,
    pub lcd_b: wgpu::RenderPipeline,
    pub lcd_bgl0: wgpu::BindGroupLayout,
    pub lcd_bgl1: wgpu::BindGroupLayout,

    // ── LCD backbuffer composite — three pipelines, premultiplied blend ───────
    pub lcb_r: wgpu::RenderPipeline,
    pub lcb_g: wgpu::RenderPipeline,
    pub lcb_b: wgpu::RenderPipeline,
    pub lcb_bgl0: wgpu::BindGroupLayout,
    pub lcb_bgl1: wgpu::BindGroupLayout,

    // ── Inside-layer text (single-pass, alpha-writing) ────────────────────────
    /// Grayscale coverage text quad — reuses the LCD bind-group layouts but
    /// writes ALL colour channels + alpha in one pass.  Routed to when text is
    /// drawn inside a compositing layer so the layer texture gets correct alpha
    /// (the 3-pass LCD pipelines never write alpha → washed-out glyphs on pop).
    pub text_gray: wgpu::RenderPipeline,
    /// Two-plane backbuffer flatten quad — reuses the LCB bind-group layouts,
    /// collapses the per-channel planes into a single premultiplied sample in
    /// one alpha-writing pass.  Inside-layer companion to `lcb_r/g/b`.
    pub lcb_flatten: wgpu::RenderPipeline,

    // ── Shared samplers ───────────────────────────────────────────────────────
    /// Nearest-neighbour — used for LCD mask textures and Label backbuffers.
    pub nearest_sampler: wgpu::Sampler,
    /// Bilinear — used for images, layers, and gradient ramp textures.
    pub linear_sampler: wgpu::Sampler,
}

impl WgpuPipelines {
    /// Construct all pipelines.  Panics if any WGSL shader fails to compile —
    /// a startup crash is preferable to a silent runtime black screen.
    ///
    /// `sample_count` bakes into every pipeline's `MultisampleState`.  Must
    /// match the sample count of every render-pass color attachment those
    /// pipelines target — both the surface MSAA buffer and any layer MSAA
    /// buffers.  `1` disables MSAA.
    pub(crate) fn new(
        device: &wgpu::Device,
        surface_format: wgpu::TextureFormat,
        sample_count: u32,
    ) -> Self {
        // ── Samplers ─────────────────────────────────────────────────────────
        let nearest_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("nearest"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Nearest,
            min_filter: wgpu::FilterMode::Nearest,
            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
            ..Default::default()
        });
        let linear_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("linear"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::MipmapFilterMode::Linear,
            ..Default::default()
        });

        // ── Shader modules ───────────────────────────────────────────────────
        let solid_sm = mk_shader(device, "solid", SOLID_WGSL);
        let aa_solid_sm = mk_shader(device, "aa_solid", AA_SOLID_WGSL);
        let gradient_sm = mk_shader(device, "gradient", GRADIENT_WGSL);
        let aa_texture_sm = mk_shader(device, "aa_texture", AA_TEXTURE_WGSL);
        let tex_sm = mk_shader(device, "tex", TEX_WGSL);
        let layer_sm = mk_shader(device, "layer", LAYER_WGSL);
        let lcd_sm = mk_shader(device, "lcd", LCD_WGSL);
        let lcb_sm = mk_shader(device, "lcb", LCB_WGSL);

        // ── Bind group layouts ───────────────────────────────────────────────
        let solid_bgl = mk_uniform_bgl(device, "solid", size_of::<SolidUniforms>() as u64);
        let aa_solid_bgl = mk_uniform_bgl(device, "aa_solid", size_of::<SolidUniforms>() as u64);
        let gradient_bgl0 = mk_uniform_bgl(
            device,
            "gradient0",
            size_of::<crate::gradient::GradientUniforms>() as u64,
        );
        let gradient_bgl1 = mk_tex1_bgl(device, "gradient1");
        let aa_texture_bgl0 =
            mk_uniform_bgl(device, "aa_texture0", size_of::<AaTexUniforms>() as u64);
        let aa_texture_bgl1 = mk_tex1_bgl(device, "aa_texture1");
        let tex_bgl0 = mk_uniform_bgl(device, "tex0", size_of::<TexUniforms>() as u64);
        let tex_bgl1 = mk_tex1_bgl(device, "tex1");
        let layer_bgl0 = mk_uniform_bgl(device, "layer0", size_of::<LayerUniforms>() as u64);
        let layer_bgl1 = mk_tex1_bgl(device, "layer1");
        let lcd_bgl0 = mk_uniform_bgl(device, "lcd0", size_of::<LcdUniforms>() as u64);
        let lcd_bgl1 = mk_tex1_bgl(device, "lcd1");
        let lcb_bgl0 = mk_uniform_bgl(device, "lcb0", size_of::<LcbUniforms>() as u64);
        let lcb_bgl1 = mk_tex2_bgl(device, "lcb1");

        // ── Pipeline layouts ─────────────────────────────────────────────────
        let solid_pl = mk_layout(device, "solid", &[&solid_bgl]);
        let aa_solid_pl = mk_layout(device, "aa_solid", &[&aa_solid_bgl]);
        let gradient_pl = mk_layout(device, "gradient", &[&gradient_bgl0, &gradient_bgl1]);
        let aa_texture_pl = mk_layout(device, "aa_texture", &[&aa_texture_bgl0, &aa_texture_bgl1]);
        let tex_pl = mk_layout(device, "tex", &[&tex_bgl0, &tex_bgl1]);
        let layer_pl = mk_layout(device, "layer", &[&layer_bgl0, &layer_bgl1]);
        let lcd_pl = mk_layout(device, "lcd", &[&lcd_bgl0, &lcd_bgl1]);
        let lcb_pl = mk_layout(device, "lcb", &[&lcb_bgl0, &lcb_bgl1]);

        // ── Shared vertex attribute slices ───────────────────────────────────
        // Defined once here so that every VertexBufferLayout below can borrow them
        // without moving them (they outlive all the pipeline create calls below).
        let pos2_attrs = [wgpu::VertexAttribute {
            format: wgpu::VertexFormat::Float32x2,
            offset: 0,
            shader_location: 0,
        }];
        let pos2_alpha_attrs = [
            wgpu::VertexAttribute {
                format: wgpu::VertexFormat::Float32x2,
                offset: 0,
                shader_location: 0,
            },
            wgpu::VertexAttribute {
                format: wgpu::VertexFormat::Float32,
                offset: 8,
                shader_location: 1,
            },
        ];
        let pos2_uv2_attrs = [
            wgpu::VertexAttribute {
                format: wgpu::VertexFormat::Float32x2,
                offset: 0,
                shader_location: 0,
            },
            wgpu::VertexAttribute {
                format: wgpu::VertexFormat::Float32x2,
                offset: 8,
                shader_location: 1,
            },
        ];

        // Convenience closures — return a fresh VertexBufferLayout borrowing the
        // attribute arrays above.  Closures rather than values avoid requiring
        // VertexBufferLayout to be Copy (it only derives Clone).
        let vbl_pos2 = || wgpu::VertexBufferLayout {
            array_stride: 8,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &pos2_attrs,
        };
        let vbl_pos2_alpha = || wgpu::VertexBufferLayout {
            array_stride: 12,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &pos2_alpha_attrs,
        };
        let vbl_pos2_uv2 = || wgpu::VertexBufferLayout {
            array_stride: 16,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &pos2_uv2_attrs,
        };

        // ── Render pipelines ─────────────────────────────────────────────────
        let solid_pipeline = build_pipeline(
            device,
            "solid",
            &solid_pl,
            &solid_sm,
            &solid_sm,
            &[vbl_pos2()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        let aa_solid_pipeline = build_pipeline(
            device,
            "aa_solid",
            &aa_solid_pl,
            &aa_solid_sm,
            &aa_solid_sm,
            &[vbl_pos2_alpha()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        // Premultiplied blend matches agg-sharp's
        // `gl.BlendFunc(One, OneMinusSrcAlpha)` in `Graphics2DGpu.PreRender`
        // — the shader emits `(color * α, α)` and the blend composites it
        // correctly into the surface.
        let aa_texture_pipeline = build_pipeline(
            device,
            "aa_texture",
            &aa_texture_pl,
            &aa_texture_sm,
            &aa_texture_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_PREMUL),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        let gradient_pipeline = build_pipeline(
            device,
            "gradient",
            &gradient_pl,
            &gradient_sm,
            &gradient_sm,
            &[vbl_pos2_alpha()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        let tex_pipeline = build_pipeline(
            device,
            "tex",
            &tex_pl,
            &tex_sm,
            &tex_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        let tex_ds3_sm = mk_shader(device, "tex_downsample_3x", TEX_DOWNSAMPLE_3X_WGSL);
        let tex_downsample_3x_pipeline = build_pipeline(
            device,
            "tex_downsample_3x",
            &tex_pl,
            &tex_ds3_sm,
            &tex_ds3_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        let tex_ds4_sm = mk_shader(device, "tex_downsample_4x", TEX_DOWNSAMPLE_4X_WGSL);
        let tex_downsample_4x_pipeline = build_pipeline(
            device,
            "tex_downsample_4x",
            &tex_pl,
            &tex_ds4_sm,
            &tex_ds4_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        let layer_pipeline = build_pipeline(
            device,
            "layer",
            &layer_pl,
            &layer_sm,
            &layer_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_PREMUL),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        let lcd_r = build_pipeline(
            device,
            "lcd_r",
            &lcd_pl,
            &lcd_sm,
            &lcd_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::RED,
            sample_count,
        );
        let lcd_g = build_pipeline(
            device,
            "lcd_g",
            &lcd_pl,
            &lcd_sm,
            &lcd_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::GREEN,
            sample_count,
        );
        let lcd_b = build_pipeline(
            device,
            "lcd_b",
            &lcd_pl,
            &lcd_sm,
            &lcd_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::BLUE,
            sample_count,
        );
        let lcb_r = build_pipeline(
            device,
            "lcb_r",
            &lcb_pl,
            &lcb_sm,
            &lcb_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_PREMUL),
            wgpu::ColorWrites::RED,
            sample_count,
        );
        let lcb_g = build_pipeline(
            device,
            "lcb_g",
            &lcb_pl,
            &lcb_sm,
            &lcb_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_PREMUL),
            wgpu::ColorWrites::GREEN,
            sample_count,
        );
        let lcb_b = build_pipeline(
            device,
            "lcb_b",
            &lcb_pl,
            &lcb_sm,
            &lcb_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_PREMUL),
            wgpu::ColorWrites::BLUE,
            sample_count,
        );

        // Inside-layer text: single-pass, alpha-writing.  `text_gray` reuses the
        // LCD layout (`lcd_pl`) + a straight-alpha blend; `lcb_flatten` reuses
        // the LCB layout (`lcb_pl`) + a premultiplied blend.  Both write ALL
        // channels so a transparent layer accumulates correct alpha.
        let text_gray_sm = mk_shader(device, "text_gray", TEXT_GRAY_WGSL);
        let text_gray = build_pipeline(
            device,
            "text_gray",
            &lcd_pl,
            &text_gray_sm,
            &text_gray_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_STANDARD),
            wgpu::ColorWrites::ALL,
            sample_count,
        );
        let lcb_flatten_sm = mk_shader(device, "lcb_flatten", LCB_FLATTEN_WGSL);
        let lcb_flatten = build_pipeline(
            device,
            "lcb_flatten",
            &lcb_pl,
            &lcb_flatten_sm,
            &lcb_flatten_sm,
            &[vbl_pos2_uv2()],
            surface_format,
            Some(BLEND_PREMUL),
            wgpu::ColorWrites::ALL,
            sample_count,
        );

        Self {
            solid_pipeline,
            solid_bgl,
            aa_solid_pipeline,
            aa_solid_bgl,
            aa_texture_pipeline,
            aa_texture_bgl0,
            aa_texture_bgl1,
            gradient_pipeline,
            gradient_bgl0,
            gradient_bgl1,
            tex_pipeline,
            tex_bgl0,
            tex_bgl1,
            tex_downsample_3x_pipeline,
            tex_downsample_4x_pipeline,
            layer_pipeline,
            layer_bgl0,
            layer_bgl1,
            lcd_r,
            lcd_g,
            lcd_b,
            lcd_bgl0,
            lcd_bgl1,
            lcb_r,
            lcb_g,
            lcb_b,
            lcb_bgl0,
            lcb_bgl1,
            text_gray,
            lcb_flatten,
            nearest_sampler,
            linear_sampler,
        }
    }
}

// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------

fn mk_shader(device: &wgpu::Device, label: &str, src: &str) -> wgpu::ShaderModule {
    device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some(label),
        source: wgpu::ShaderSource::Wgsl(src.into()),
    })
}

/// Bind-group layout with a single uniform buffer at binding 0, visible to
/// both vertex and fragment stages.
fn mk_uniform_bgl(device: &wgpu::Device, label: &str, size: u64) -> wgpu::BindGroupLayout {
    device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some(label),
        entries: &[wgpu::BindGroupLayoutEntry {
            binding: 0,
            visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
            ty: wgpu::BindingType::Buffer {
                ty: wgpu::BufferBindingType::Uniform,
                has_dynamic_offset: false,
                min_binding_size: wgpu::BufferSize::new(size),
            },
            count: None,
        }],
    })
}

/// Bind-group layout for group 1 with one `texture_2d<f32>` (binding 0) and
/// one filtering sampler (binding 1).
fn mk_tex1_bgl(device: &wgpu::Device, label: &str) -> wgpu::BindGroupLayout {
    device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some(label),
        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,
            },
        ],
    })
}

/// Bind-group layout for the LCB pipeline's group 1: two `texture_2d<f32>`
/// (bindings 0 and 1, colour plane + alpha plane) and one sampler (binding 2).
fn mk_tex2_bgl(device: &wgpu::Device, label: &str) -> wgpu::BindGroupLayout {
    device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some(label),
        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::Texture {
                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
                    view_dimension: wgpu::TextureViewDimension::D2,
                    multisampled: false,
                },
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 2,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                count: None,
            },
        ],
    })
}

fn mk_layout(
    device: &wgpu::Device,
    label: &str,
    bgls: &[&wgpu::BindGroupLayout],
) -> wgpu::PipelineLayout {
    let opt: Vec<Option<&wgpu::BindGroupLayout>> = bgls.iter().copied().map(Some).collect();
    device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
        label: Some(label),
        bind_group_layouts: &opt,
        immediate_size: 0,
    })
}

/// Create a `RenderPipeline` from a single shader module (vs and fs are the
/// same module), a single vertex buffer slot, a blend state, and a colour
/// write mask.  All 2D pipelines use `TriangleList` topology with no depth
/// test, no culling, and no MSAA.
fn build_pipeline(
    device: &wgpu::Device,
    label: &str,
    layout: &wgpu::PipelineLayout,
    vs: &wgpu::ShaderModule,
    fs: &wgpu::ShaderModule,
    vertex_buffers: &[wgpu::VertexBufferLayout<'_>],
    surface_format: wgpu::TextureFormat,
    blend: Option<wgpu::BlendState>,
    write_mask: wgpu::ColorWrites,
    sample_count: u32,
) -> wgpu::RenderPipeline {
    device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
        label: Some(label),
        layout: Some(layout),
        vertex: wgpu::VertexState {
            module: vs,
            entry_point: Some("vs_main"),
            compilation_options: Default::default(),
            buffers: vertex_buffers,
        },
        fragment: Some(wgpu::FragmentState {
            module: fs,
            entry_point: Some("fs_main"),
            compilation_options: Default::default(),
            targets: &[Some(wgpu::ColorTargetState {
                format: surface_format,
                blend,
                write_mask,
            })],
        }),
        primitive: wgpu::PrimitiveState {
            topology: wgpu::PrimitiveTopology::TriangleList,
            cull_mode: None,
            ..Default::default()
        },
        depth_stencil: None,
        multisample: wgpu::MultisampleState {
            count: sample_count,
            mask: !0,
            alpha_to_coverage_enabled: false,
        },
        multiview_mask: None,
        cache: None,
    })
}