frust-engine 0.6.3

Sparse-strip GPU render pipeline for Frust: compiles a scene display list into strips and records the draw passes on wgpu.
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
//! The registry of caller-owned textures a frame's strip passes sample, and
//! the per-frame batching that binds each of them in turn.
//!
//! The engine draws two kinds of image. An atlas-backed one lives in the
//! renderer's own array texture, addressed by layer and rectangle, and every
//! draw of the frame reads it through the same binding. An *external* one is a
//! texture the host created and still owns — a video frame, a plugin's import,
//! an offscreen page some other pass rendered — which the engine never copies
//! and never packs. It is bound as a whole texture instead, and the strip
//! shader's image path reads it through a second binding chosen by the encoded
//! paint's source-kind bit (see `shaders/helpers.wgsl`).
//!
//! One binding, many textures: a frame that draws two different external
//! textures cannot bind both at once, so its instances are split into *runs* —
//! maximal spans of consecutive instances sharing one texture — and each run is
//! drawn with that texture bound. [`ExternalRuns`] assigns the slot numbers the
//! renderer's pass segments name a run by; the renderer builds one group-1 bind
//! group per slot and sets it between runs.
//!
//! Nothing here allocates a texture or copies a texel. [`ExternalTextures`] is
//! a plain map the host writes through
//! `EngineRenderer::bind_texture`/`unbind_texture`, and it is generic over the
//! stored view type — like `frust_gpu::TextureRegistry`, so its own behaviour
//! stays host-testable with a stand-in value and no GPU device in the loop.
//!
//! The *extent* half of the same registry lives on the compiler side, in
//! [`crate::compile::external::ExternalExtents`]: the walk needs it to compose
//! a paint transform, while only a pass needs the view. The one call that
//! writes both keeps them in step.
//!
//! ## The strip programs' one explicit layout
//!
//! Every strip pipeline — each render-state variant, and each
//! [paint-class](super::strips::PaintClass) specialisation of it — is built
//! against ONE explicit `wgpu::PipelineLayout`, [`strip_pipeline_layout`],
//! rather than the layout wgpu would derive from its module. A derived layout
//! is whatever bindings the module's entry points statically *use*, and a class
//! program that no longer reads, say, the atlas array would derive a layout
//! without it; the bind groups built for one class would then not fit another.
//! The explicit layout is exactly the one the whole (Uber) program derives —
//! [`strip_bind_group_layout_entries`] restates it entry for entry, and its
//! test holds that restatement to the naga reflection of `strip.wgsl` — so a
//! bind group built against any strip pipeline binds under every other one.
//!
//! There is one such layout per program family
//! ([`crate::gpu::shader_src`]'s *The two program families*): the manual
//! family's (`FRUST_ENGINE_IMAGE_SAMPLER=0`) is the one above, and the sampler
//! family's — the default — adds the one sampler its image programs filter the
//! atlas through — group 1, binding 2 — and declares the atlas binding
//! filterable. Every binding before it keeps its
//! number, so the families differ by that one entry, and the four-group
//! WebGL2 ceiling is untouched.

use std::collections::HashMap;

use frust_gpu::SceneTextureId;
use vello_common::encode::EncodedExternalTexture;

use super::atlas::{extend_mode, pack_image_offset, pack_image_params, pack_image_size, pack_tint};
use super::config::GpuConfig;
use super::paint_texture::{GpuEncodedImage, GpuEncodedPaint};

/// How many bind groups the strip programs declare — the WebGL2 ceiling of
/// four, exactly (see [`crate::gpu::pipelines`]'s *Bind groups*).
pub const STRIP_BIND_GROUP_COUNT: usize = 4;

/// One sampled, non-multisampled texture binding: what every strip texture
/// binding is, every one of them read by `textureLoad` but the sampler
/// family's atlas array.
const fn load_only_texture(
    binding: u32,
    visibility: wgpu::ShaderStages,
    sample_type: wgpu::TextureSampleType,
    view_dimension: wgpu::TextureViewDimension,
) -> wgpu::BindGroupLayoutEntry {
    wgpu::BindGroupLayoutEntry {
        binding,
        visibility,
        ty: wgpu::BindingType::Texture {
            sample_type,
            view_dimension,
            multisampled: false,
        },
        count: None,
    }
}

/// The float sample type every strip `texture_2d<f32>` binding takes but the
/// sampler family's atlas array.
///
/// Unfilterable, because no other strip binding is ever read through a
/// sampler — exactly what wgpu derives for a float texture no sampler touches,
/// and what lets the binding accept a filterable *and* an unfilterable float
/// texture.
const UNFILTERABLE: wgpu::TextureSampleType = wgpu::TextureSampleType::Float { filterable: false };

/// The float sample type of a texture a filtering sampler reads: the sampler
/// family's atlas array, which is `Rgba8Unorm` and so always filterable.
const FILTERABLE: wgpu::TextureSampleType = wgpu::TextureSampleType::Float { filterable: true };

/// The sampler family's sampler binding: group 1, after the two image
/// textures.
pub const ATLAS_SAMPLER_BINDING: u32 = 2;

/// The strip programs' bind-group layout entries, group by group, exactly as
/// wgpu derives them from the whole (Uber) program of the family
/// `image_sampler` names — binding, type and visibility.
///
/// | Group | Binding | WGSL | Read by |
/// |---|---|---|---|
/// | 0 | 0 | `alphas_texture: texture_2d<u32>` | fragment |
/// | 0 | 1 | `config: Config` (uniform, [`GpuConfig::SIZE`] bytes) | vertex, fragment |
/// | 0 | 2 | `layer_input_texture: texture_2d<f32>` | fragment |
/// | 1 | 0 | `atlas_texture_array: texture_2d_array<f32>` (filterable in the sampler family) | fragment |
/// | 1 | 1 | `external_texture: texture_2d<f32>` | fragment |
/// | 1 | 2 | `atlas_sampler: sampler` (filtering; the sampler family only) | fragment |
/// | 2 | 0 | `encoded_paints_texture: texture_2d<u32>` | vertex, fragment |
/// | 3 | 0 | `gradient_texture: texture_2d<f32>` | fragment |
#[must_use]
pub fn strip_bind_group_layout_entries(
    image_sampler: bool,
) -> [Vec<wgpu::BindGroupLayoutEntry>; STRIP_BIND_GROUP_COUNT] {
    use wgpu::ShaderStages as S;
    use wgpu::TextureSampleType as T;
    use wgpu::TextureViewDimension as D;
    [
        vec![
            load_only_texture(0, S::FRAGMENT, T::Uint, D::D2),
            wgpu::BindGroupLayoutEntry {
                binding: 1,
                visibility: S::VERTEX_FRAGMENT,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Uniform,
                    has_dynamic_offset: false,
                    min_binding_size: wgpu::BufferSize::new(GpuConfig::SIZE),
                },
                count: None,
            },
            load_only_texture(2, S::FRAGMENT, UNFILTERABLE, D::D2),
        ],
        if image_sampler {
            vec![
                load_only_texture(0, S::FRAGMENT, FILTERABLE, D::D2Array),
                load_only_texture(1, S::FRAGMENT, UNFILTERABLE, D::D2),
                wgpu::BindGroupLayoutEntry {
                    binding: ATLAS_SAMPLER_BINDING,
                    visibility: S::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
            ]
        } else {
            vec![
                load_only_texture(0, S::FRAGMENT, UNFILTERABLE, D::D2Array),
                load_only_texture(1, S::FRAGMENT, UNFILTERABLE, D::D2),
            ]
        },
        vec![load_only_texture(0, S::VERTEX_FRAGMENT, T::Uint, D::D2)],
        vec![load_only_texture(0, S::FRAGMENT, UNFILTERABLE, D::D2)],
    ]
}

/// The one explicit pipeline layout every strip pipeline of the family
/// `image_sampler` names is built against.
///
/// Created once per renderer, at shader registration
/// ([`crate::gpu::pipelines::EngineShaders::register`]). A pipeline built
/// against it hands out these same group layouts from
/// `get_bind_group_layout`, so the renderer's existing bind-group construction
/// is unchanged and its groups bind under every strip variant and class.
///
/// **One layout per renderer is the convention** (two with the image sampler,
/// where the atlas-layer pass takes the sampler-free one). The pipeline cache
/// keys a pipeline on the layout through an interning table with a fixed
/// number of slots (`frust_gpu::pipeline::LAYOUT_BITS`); a layout created per
/// frame, per class or per variant would fill it and push every later pipeline
/// onto the uncached, compile-per-request path. Build another only with a
/// reason the table can afford.
#[must_use]
pub fn strip_pipeline_layout(device: &wgpu::Device, image_sampler: bool) -> wgpu::PipelineLayout {
    let entries = strip_bind_group_layout_entries(image_sampler);
    let groups = entries.each_ref().map(|entries| {
        device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("frust-engine strip group"),
            entries,
        })
    });
    let refs = groups.each_ref().map(Some);
    device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
        label: Some("frust-engine strip layout"),
        bind_group_layouts: &refs,
        immediate_size: 0,
    })
}

/// Every externally owned texture currently bound, keyed by the
/// [`SceneTextureId`] its owner minted.
///
/// The stored view must be a non-array 2D view of a float-sampleable texture
/// whose texture carries `wgpu::TextureUsages::TEXTURE_BINDING`; only mip level
/// 0 is ever read. `wgpu` rejects anything else when the bind group is built,
/// which is the frame the mistake surfaces on.
///
/// Generic over the view type (default `wgpu::TextureView`, the real engine's)
/// so the map's own insert/replace/remove behaviour is testable without a
/// device.
#[derive(Debug)]
pub struct ExternalTextures<V = wgpu::TextureView> {
    /// Keyed by the id's opaque value rather than by [`SceneTextureId`]
    /// itself: a display list names an external texture by that same plain
    /// `u64` (`frust_scene::Command::SceneTexture`), and a lookup from a
    /// compiled frame has nothing but that number to ask with.
    views: HashMap<u64, V>,
}

impl<V> Default for ExternalTextures<V> {
    fn default() -> Self {
        Self {
            views: HashMap::new(),
        }
    }
}

impl<V> ExternalTextures<V> {
    /// An empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers `view` under `id`, returning whatever was registered before.
    pub fn bind(&mut self, id: SceneTextureId, view: V) -> Option<V> {
        self.views.insert(id.get(), view)
    }

    /// Removes and returns the view registered under `id`.
    pub fn unbind(&mut self, id: SceneTextureId) -> Option<V> {
        self.views.remove(&id.get())
    }

    /// The view registered under `id`.
    #[must_use]
    pub fn get(&self, id: SceneTextureId) -> Option<&V> {
        self.views.get(&id.get())
    }

    /// The view registered under the opaque id a display list names, which is
    /// the only form a compiled frame carries.
    #[must_use]
    pub fn view(&self, key: u64) -> Option<&V> {
        self.views.get(&key)
    }

    /// How many textures are bound.
    #[must_use]
    pub fn len(&self) -> usize {
        self.views.len()
    }

    /// Whether nothing is bound.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.views.is_empty()
    }
}

/// The distinct external textures one frame draws with, in the order it first
/// names them.
///
/// A frame's instances are drawn in painter order, and the single external
/// binding has to be re-set whenever that order crosses from one texture to
/// another. Rather than carry the id on every instance, each encoded paint
/// takes a *slot* — its index here — and the renderer's pass segments carry
/// that slot, so a run of instances sharing a texture is one segment and one
/// bind-group set.
///
/// Filled while the frame's paints are lowered and read while its passes are
/// recorded; cleared and refilled per frame, never growing past the number of
/// external textures a single frame draws.
#[derive(Debug, Default)]
pub struct ExternalRuns {
    keys: Vec<u64>,
}

impl ExternalRuns {
    /// An empty set of runs.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Forgets the previous frame's textures, keeping the allocation.
    pub fn clear(&mut self) {
        self.keys.clear();
    }

    /// The slot `key` is drawn under, assigning one if this frame has not
    /// named it yet.
    ///
    /// A linear scan rather than a map: a frame draws a handful of external
    /// textures at most — the binding is re-set between runs, so a scene that
    /// interleaved hundreds would be paying far more for the pass breaks than
    /// for the lookup.
    ///
    /// `None` once the slot numbering would leave `u32`, which no real frame
    /// reaches; the paint is then left unresolved and its draws skipped, which
    /// is the same answer an unbound texture gets.
    pub fn slot_of(&mut self, key: u64) -> Option<u32> {
        if let Some(index) = self.keys.iter().position(|held| *held == key) {
            return u32::try_from(index).ok();
        }
        let slot = u32::try_from(self.keys.len()).ok()?;
        self.keys.push(key);
        Some(slot)
    }

    /// The texture each slot names, indexed by slot number.
    #[must_use]
    pub fn keys(&self) -> &[u64] {
        &self.keys
    }

    /// The texture drawn under `slot`.
    #[must_use]
    pub fn key_at(&self, slot: u32) -> Option<u64> {
        self.keys.get(slot as usize).copied()
    }

    /// How many distinct external textures the frame draws.
    #[must_use]
    pub fn len(&self) -> usize {
        self.keys.len()
    }

    /// Whether the frame draws none.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.keys.is_empty()
    }
}

/// Lower one encoded external texture into the record the strip shader samples.
///
/// The external counterpart of [`super::atlas::lower_encoded_image`], and the
/// same record type: the shader reads both through one `GpuEncodedImage`
/// layout and picks the binding from the source-kind bit
/// [`pack_image_params`] sets here. What differs is where the numbers come
/// from — the source region is the caller's own texel rectangle rather than an
/// atlas allocation, there is no atlas layer to name (hence the zero index,
/// which the shader never reads on this path) and no padding, since nothing
/// was packed beside it.
///
/// The transform is already the inverse mapping — device space into texel
/// space — because that is the direction the shader applies it in; narrowing
/// it to `f32` here is what the record and the WGSL both read it at.
#[must_use]
pub fn lower_encoded_external(entry: &EncodedExternalTexture) -> GpuEncodedPaint {
    let region = entry.source_region;
    let (tint, tint_mode) = pack_tint(entry.tint);

    GpuEncodedPaint::Image(GpuEncodedImage {
        image_params: pack_image_params(
            entry.sampler.quality as u32,
            extend_mode(entry.sampler.x_extend),
            extend_mode(entry.sampler.y_extend),
            0,
            true,
        ),
        image_size: pack_image_size(
            region.x1.saturating_sub(region.x0),
            region.y1.saturating_sub(region.y0),
        ),
        image_offset: pack_image_offset(region.x0, region.y0),
        transform: entry.transform.as_coeffs().map(|coeff| coeff as f32),
        tint,
        tint_mode,
        image_padding: 0,
    })
}

/// What a cached bind group samples as its layer input.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GroupSource {
    /// The transparent placeholder, which composites nothing.
    Placeholder,
    /// A pooled page, by the id its texture minted for itself. A pool that
    /// drops a texture and allocates another never reuses the id, so an entry
    /// naming a dropped texture can only ever miss.
    Pooled(SceneTextureId),
    /// A retained page: the address of its view in the store, the store's
    /// generation and the page extent. The store advances its generation
    /// whenever a page is added, evicted, re-rendered at another extent or the
    /// store is cleared, which are the only ways a view's address can come to
    /// name a different texture.
    Retained {
        /// Address of the page's view inside the store.
        view: usize,
        /// The store's generation when the key was made.
        generation: u64,
        /// The page extent.
        size: (u32, u32),
    },
}

/// Which bind group over a [`GroupSource`] a [`SourceGroupCache`] entry is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GroupKind {
    /// Group 0 of a strip pass over page-config buffer `slot`: the frame's
    /// coverage, that buffer, and the source as the layer input.
    Resources {
        /// Index into the renderer's page-config buffers.
        slot: u32,
    },
    /// The filter pass's source group (its group 1).
    FilterSource,
}

/// A cache key: one kind of group over one source.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SourceGroupKey {
    /// Which group.
    pub kind: GroupKind,
    /// What it samples.
    pub source: GroupSource,
}

/// The most entries a cache keeps before it drops them all.
///
/// A steady frame touches a handful (one per live page and composite source);
/// the bound only matters if the sweep is never driven.
const SOURCE_GROUP_CAP: usize = 64;

/// Bind groups that name a pooled or retained page, kept across frames.
///
/// A page-round resources group, a composite or backdrop segment's group and a
/// filter pass's source group each name one texture view; rebuilding them for
/// the same view every frame is pure cost. Entries are keyed by what they
/// sample ([`SourceGroupKey`]) and **swept every frame**: [`Self::sweep`] drops
/// every entry the frame did not use, so a group never keeps a texture alive
/// past the frame after the last one that drew with it — well inside the page
/// pool's own keep-alive window, which has no eviction hook to invalidate
/// against. The owner clears the cache outright when an input every entry
/// shares (the coverage view) is recreated.
///
/// Generic over the group type so the keying and sweep rules test without a
/// device.
#[derive(Debug)]
pub struct SourceGroupCache<G = wgpu::BindGroup> {
    entries: HashMap<SourceGroupKey, (G, bool)>,
    created: u64,
}

impl<G> Default for SourceGroupCache<G> {
    fn default() -> Self {
        Self {
            entries: HashMap::new(),
            created: 0,
        }
    }
}

impl<G: Clone> SourceGroupCache<G> {
    /// The group under `key`, built by `build` on a miss. Marks it used.
    pub fn get_or_build(&mut self, key: SourceGroupKey, build: impl FnOnce() -> G) -> G {
        if let Some((group, used)) = self.entries.get_mut(&key) {
            *used = true;
            return group.clone();
        }
        if self.entries.len() >= SOURCE_GROUP_CAP {
            self.entries.clear();
        }
        self.created = self.created.saturating_add(1);
        let group = build();
        self.entries.insert(key, (group.clone(), true));
        group
    }

    /// Ends a frame: drops every entry the frame did not use and marks the
    /// survivors unused for the next one.
    pub fn sweep(&mut self) {
        self.entries.retain(|_, (_, used)| std::mem::take(used));
    }

    /// Drops every entry.
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// How many groups this cache has built, over its life.
    #[must_use]
    pub fn created(&self) -> u64 {
        self.created
    }

    /// How many entries it holds right now.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether it holds none.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

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

    use peniko::{ImageQuality, ImageSampler};
    use vello_common::TextureId;
    use vello_common::geometry::RectU16;
    use vello_common::kurbo::Affine;

    fn key(slot: u32, source: GroupSource) -> SourceGroupKey {
        SourceGroupKey {
            kind: GroupKind::Resources { slot },
            source,
        }
    }

    #[test]
    fn a_source_group_is_built_once_while_it_keeps_being_used() {
        let mut cache: SourceGroupCache<u32> = SourceGroupCache::default();
        let page = key(0, GroupSource::Pooled(SceneTextureId::mint()));
        for _ in 0..4 {
            assert_eq!(cache.get_or_build(page, || 7), 7);
            cache.sweep();
        }
        assert_eq!(cache.created(), 1);
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn a_sweep_drops_a_group_the_frame_did_not_use() {
        let mut cache: SourceGroupCache<u32> = SourceGroupCache::default();
        let kept = key(0, GroupSource::Placeholder);
        let gone = key(1, GroupSource::Pooled(SceneTextureId::mint()));
        cache.get_or_build(kept, || 1);
        cache.get_or_build(gone, || 2);
        cache.sweep();
        assert_eq!(cache.len(), 2, "both were used by the frame just ended");
        cache.get_or_build(kept, || 1);
        cache.sweep();
        assert_eq!(cache.len(), 1, "the unused one is dropped");
        cache.get_or_build(gone, || 3);
        assert_eq!(cache.created(), 3, "a dropped group is rebuilt on return");
    }

    #[test]
    fn keys_differ_by_slot_source_and_retained_generation() {
        let mut cache: SourceGroupCache<u32> = SourceGroupCache::default();
        let retained = |generation| GroupSource::Retained {
            view: 0x10,
            generation,
            size: (8, 8),
        };
        cache.get_or_build(key(0, retained(1)), || 1);
        cache.get_or_build(key(0, retained(2)), || 2);
        cache.get_or_build(key(1, retained(1)), || 3);
        cache.get_or_build(key(0, GroupSource::Pooled(SceneTextureId::mint())), || 4);
        cache.get_or_build(key(0, GroupSource::Pooled(SceneTextureId::mint())), || 5);
        assert_eq!(cache.created(), 5);
    }

    #[test]
    fn clear_forgets_everything_and_a_runaway_cache_is_bounded() {
        let mut cache: SourceGroupCache<u32> = SourceGroupCache::default();
        for n in 0..(SOURCE_GROUP_CAP as u32 * 3) {
            cache.get_or_build(key(n, GroupSource::Placeholder), || n);
            assert!(cache.len() <= SOURCE_GROUP_CAP);
        }
        cache.clear();
        assert!(cache.is_empty());
    }

    /// A registry over plain values, which is all the map's own behaviour
    /// needs — minting a real `wgpu::TextureView` would require a device.
    fn registry() -> ExternalTextures<&'static str> {
        ExternalTextures::new()
    }

    fn entry(region: RectU16) -> EncodedExternalTexture {
        EncodedExternalTexture {
            texture_id: TextureId(1),
            source_region: region,
            sampler: ImageSampler {
                quality: ImageQuality::Medium,
                ..ImageSampler::default()
            },
            may_have_transparency: true,
            transform: Affine::IDENTITY,
            tint: None,
        }
    }

    #[test]
    fn a_bound_view_is_reachable_by_id_and_by_the_scenes_own_number() {
        let mut textures = registry();
        let id = fresh_id();

        assert!(textures.bind(id, "view").is_none());

        assert_eq!(textures.get(id), Some(&"view"));
        assert_eq!(textures.view(id.get()), Some(&"view"));
        assert_eq!(textures.len(), 1);
    }

    #[test]
    fn rebinding_returns_the_previous_view() {
        let mut textures = registry();
        let id = fresh_id();
        textures.bind(id, "first");

        assert_eq!(textures.bind(id, "second"), Some("first"));
        assert_eq!(textures.get(id), Some(&"second"));
    }

    #[test]
    fn unbinding_returns_the_view_and_empties_the_registry() {
        let mut textures = registry();
        let id = fresh_id();
        textures.bind(id, "view");

        assert_eq!(textures.unbind(id), Some("view"));
        assert!(textures.is_empty());
        assert_eq!(textures.view(id.get()), None);
    }

    #[test]
    fn an_unbound_id_resolves_to_nothing() {
        let textures = registry();

        assert_eq!(textures.view(4_242), None);
    }

    #[test]
    fn a_slot_is_assigned_once_per_texture_and_reused_after() {
        let mut runs = ExternalRuns::new();

        assert_eq!(runs.slot_of(7), Some(0));
        assert_eq!(runs.slot_of(9), Some(1));
        assert_eq!(runs.slot_of(7), Some(0), "the same texture keeps its slot");
        assert_eq!(runs.keys(), &[7, 9]);
        assert_eq!(runs.key_at(1), Some(9));
        assert_eq!(runs.key_at(2), None);
    }

    #[test]
    fn clearing_forgets_the_previous_frames_textures() {
        let mut runs = ExternalRuns::new();
        runs.slot_of(7);

        runs.clear();

        assert!(runs.is_empty());
        assert_eq!(runs.slot_of(9), Some(0), "slots restart each frame");
    }

    #[test]
    fn a_lowered_external_record_names_the_external_source_and_no_atlas_layer() {
        let record = lower_encoded_external(&entry(RectU16 {
            x0: 0,
            y0: 0,
            x1: 64,
            y1: 32,
        }));

        let GpuEncodedPaint::Image(image) = record else {
            panic!("an external texture lowers to the image record");
        };
        assert_eq!(
            (image.image_params >> 14) & 0x1,
            1,
            "the source-kind bit selects the external binding"
        );
        assert_eq!((image.image_params >> 6) & 0xFF, 0, "no atlas layer");
        assert_eq!(image.image_size, pack_image_size(64, 32));
        assert_eq!(image.image_offset, pack_image_offset(0, 0));
        assert_eq!(image.image_padding, 0, "nothing was packed beside it");
    }

    #[test]
    fn a_sub_rectangle_lowers_as_its_own_offset_and_extent() {
        let record = lower_encoded_external(&entry(RectU16 {
            x0: 8,
            y0: 4,
            x1: 24,
            y1: 20,
        }));

        let GpuEncodedPaint::Image(image) = record else {
            panic!("an external texture lowers to the image record");
        };
        assert_eq!(image.image_offset, pack_image_offset(8, 4));
        assert_eq!(image.image_size, pack_image_size(16, 16));
    }

    /// The explicit strip layout of each family is the layout wgpu would
    /// derive from that family's whole strip program, entry for entry: every
    /// binding the program declares is present with the type naga reflects for
    /// it — a float texture filterable exactly when a sampler reads it —
    /// visible to exactly the stages whose entry point uses it, and nothing
    /// else is declared. That is what makes switching the Uber program from a
    /// derived to this explicit layout a no-op, and what lets every class
    /// program of the family — a subset of the Uber one's uses — validate
    /// against it.
    #[test]
    fn the_explicit_strip_layout_is_the_uber_programs_derived_layout() {
        for image_sampler in [false, true] {
            explicit_layout_matches_derived(image_sampler);
        }
    }

    fn explicit_layout_matches_derived(image_sampler: bool) {
        use crate::gpu::shader_src::strip_program_source;
        use crate::gpu::strips::PaintClass;
        use wgpu::naga;
        let src = strip_program_source(PaintClass::Uber, image_sampler);
        let module = naga::front::wgsl::parse_str(&src).expect("the strip source parses");
        let info = naga::valid::Validator::new(
            naga::valid::ValidationFlags::all(),
            naga::valid::Capabilities::empty(),
        )
        .validate(&module)
        .expect("the strip source validates");
        let sampled = |handle| {
            (0..module.entry_points.len()).any(|index| {
                info.get_entry_point(index)
                    .sampling_set
                    .iter()
                    .any(|key| key.image == handle)
            })
        };

        let entries = strip_bind_group_layout_entries(image_sampler);
        let mut declared = 0;
        for (handle, global) in module.global_variables.iter() {
            let Some(binding) = &global.binding else {
                continue;
            };
            declared += 1;
            let entry = entries
                .get(binding.group as usize)
                .and_then(|group| group.iter().find(|e| e.binding == binding.binding))
                .unwrap_or_else(|| panic!("no layout entry for {binding:?}"));

            let mut stages = wgpu::ShaderStages::NONE;
            for (index, entry_point) in module.entry_points.iter().enumerate() {
                if !info.get_entry_point(index)[handle].is_empty() {
                    stages |= match entry_point.stage {
                        naga::ShaderStage::Vertex => wgpu::ShaderStages::VERTEX,
                        naga::ShaderStage::Fragment => wgpu::ShaderStages::FRAGMENT,
                        _ => wgpu::ShaderStages::NONE,
                    };
                }
            }
            assert_eq!(entry.visibility, stages, "{:?} visibility", global.name);

            match (&module.types[global.ty].inner, entry.ty) {
                (
                    naga::TypeInner::Image {
                        dim: naga::ImageDimension::D2,
                        arrayed,
                        class: naga::ImageClass::Sampled { kind, multi: false },
                    },
                    wgpu::BindingType::Texture {
                        sample_type,
                        view_dimension,
                        multisampled: false,
                    },
                ) => {
                    let expected_dim = if *arrayed {
                        wgpu::TextureViewDimension::D2Array
                    } else {
                        wgpu::TextureViewDimension::D2
                    };
                    assert_eq!(view_dimension, expected_dim, "{:?}", global.name);
                    let expected_sample = match kind {
                        naga::ScalarKind::Uint => wgpu::TextureSampleType::Uint,
                        naga::ScalarKind::Float if sampled(handle) => FILTERABLE,
                        naga::ScalarKind::Float => UNFILTERABLE,
                        other => panic!("unexpected sample kind {other:?}"),
                    };
                    assert_eq!(sample_type, expected_sample, "{:?}", global.name);
                }
                (
                    naga::TypeInner::Sampler { comparison: false },
                    wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                ) => {
                    assert_eq!(binding.group, 1);
                    assert_eq!(binding.binding, ATLAS_SAMPLER_BINDING);
                }
                (
                    naga::TypeInner::Struct { span, .. },
                    wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size,
                    },
                ) => {
                    assert_eq!(global.space, naga::AddressSpace::Uniform);
                    assert_eq!(
                        min_binding_size.map(|size| size.get()),
                        Some(u64::from(*span))
                    );
                }
                (shader, layout) => panic!("{:?}: {shader:?} vs {layout:?}", global.name),
            }
        }
        assert_eq!(
            declared,
            entries.iter().map(Vec::len).sum::<usize>(),
            "the layout declares a binding the program does not"
        );
        assert_eq!(
            entries[1].len(),
            if image_sampler { 3 } else { 2 },
            "only the sampler family declares the sampler"
        );
    }

    /// A fresh [`SceneTextureId`], minted the only way this crate can: off a
    /// texture handle built over stand-in values.
    fn fresh_id() -> SceneTextureId {
        frust_gpu::Texture::new(
            (),
            (),
            frust_gpu::TextureDesc {
                width: 1,
                height: 1,
                format: wgpu::TextureFormat::Rgba8Unorm,
                usage: wgpu::TextureUsages::TEXTURE_BINDING,
                label: None,
            },
        )
        .as_scene_texture()
    }
}