primase-core 0.2.0

primase core: the world store, core components, frames, the pass manager and gates
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
//! A prototype's levels of detail ([`Lods`], `core.lods`) and the pass that chooses one (`select-lod`). The forms
//! stay in the world as the source authored them; a writer sees the one form a pipeline's pass leaves it, so no
//! writer chooses on its own.

use std::collections::BTreeSet;

use serde::Serialize;
use serde_json::Value;

use crate::pass::{Given, OptionSpec, Options, Pass, PassLog, PassSpec};
use crate::{
    Collision, Component, Entity, Instance, JsonError, MaterialVariants, Mesh, MeshFile, Name, Source,
    Transform, World,
};

/// A prototype's levels of detail (`core.lods`), on the prototype. Its own mesh is one form, at level `detail` (0 the
/// finest, larger coarser, 0 high to 3 very low in one source); `forms` are its other forms, as the source lists them
/// (finest first by convention; nothing depends on it).
///
/// A form is an entity of its own that holds everything the prototype holds for its mesh: `core.mesh_file` or
/// `core.mesh`, `core.material_variants` when it has variants, and every dialect component that goes with that mesh
/// (a reader that attaches per-primitive data to a prototype attaches it to each form too). A form with neither mesh
/// component draws nothing. Forms are not placed and have no forms of their own. What stays with the prototype
/// whichever form is drawn (its name, collision, identity) is the prototype's: a reader may give a form its own name
/// or source too, and [`SelectLod`] leaves those behind.
///
/// `default` is the form the source itself takes: 0 the prototype's own mesh, `k` `forms[k - 1]`; `None` when the
/// source authors none (a source switches between its levels by distance).
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Lods {
    pub detail: u8,
    pub forms: Vec<LodForm>,
    pub default: Option<usize>,
}

/// One other form of a prototype: its level and the entity holding it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub struct LodForm {
    pub detail: u8,
    pub form: Entity,
}

impl Lods {
    /// The levels of [own mesh, forms...], in that order.
    pub fn levels(&self) -> impl Iterator<Item = u8> + '_ {
        std::iter::once(self.detail).chain(self.forms.iter().map(|f| f.detail))
    }

    /// The form `choice` takes, as an index into [own mesh, forms...] ([`LodChoice::AtMost`] takes the coarsest form
    /// whose level is `n` or finer); [`LodChoice::Default`] without an authored default takes the finest.
    pub fn choose(&self, choice: LodChoice) -> usize {
        match choice {
            LodChoice::Finest => 0,
            LodChoice::Default => self.default.unwrap_or(0),
            LodChoice::AtMost(n) => {
                let mut best: Option<(usize, u8)> = None;
                for (i, d) in self.levels().enumerate() {
                    if d <= n && best.is_none_or(|(_, b)| d > b) {
                        best = Some((i, d));
                    }
                }
                best.map_or(0, |(i, _)| i)
            }
        }
    }
}

impl Component for Lods {
    const NAME: &'static str = "core.lods";
    const GEOMETRY: crate::Geometry = crate::Geometry::None;
    /// The forms are entities of their own (a pass mapping a prototype maps its forms itself).
    fn apply_linear(&self, _l: &crate::Linear) -> crate::world::Linearity<Self> {
        crate::world::Linearity::Invariant
    }
    fn to_json(&self) -> Result<Option<Value>, JsonError> {
        crate::json::to_value(self).map(Some)
    }
    fn references(&self, out: &mut Vec<Entity>) {
        out.extend(self.forms.iter().map(|f| f.form));
    }
    /// `default`, when there is one, one of the forms; with other forms, on an entity with a mesh; each form another
    /// alive entity, listed once, not placed and without forms of its own. [`SelectLod`] checks it before it changes
    /// anything, so it checks liveness itself, beside the world's check of every reference.
    fn verify(&self, e: Entity, w: &World) -> Result<(), String> {
        if let Some(d) = self.default
            && d > self.forms.len()
        {
            return Err(format!("default form {d} of {}", self.forms.len() + 1));
        }
        if !self.forms.is_empty() && !(w.has::<Mesh>(e) || w.has::<MeshFile>(e)) {
            return Err("other forms of an entity without a mesh".into());
        }
        let mut seen = BTreeSet::new();
        for f in &self.forms {
            if f.form == e || !w.is_alive(f.form) {
                return Err(format!("form {:?} is not another alive entity", f.form));
            }
            if !seen.insert(f.form) {
                return Err(format!("form {:?} is listed twice", f.form));
            }
            if w.has::<Instance>(f.form) || w.has::<Transform>(f.form) || w.has::<Lods>(f.form) {
                return Err(format!("form {:?} is placed or has forms of its own", f.form));
            }
        }
        Ok(())
    }
}

/// Which form [`SelectLod`] takes of each prototype.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LodChoice {
    /// The prototype's own mesh.
    Finest,
    /// [`Lods::default`], the source's own choice; the finest where it authored none (a named, counted fallback).
    Default,
    /// The coarsest form at level `N` or finer (the first listed of equal levels); the finest when none is.
    AtMost(u8),
}

/// `select-lod{form=finest|default|N}` (`form` required: which level of detail is drawn is the program's choice):
/// every prototype with [`Lods`] is left with the one form [`Lods::choose`] takes (with `default`, the finest where the source authored no default, counted as an
/// approximation). Another form than its own takes the prototype's own geometry away with everything that describes
/// it ([`World::remove_mesh`]: `core.mesh`, `core.mesh_file` and every per-primitive component, dialect data
/// included, so nothing describing the discarded form stays behind when the chosen form lacks it), then every
/// component the form holds replaces the prototype's of that name ([`World::move_components`]), so the form's
/// dialect data travels with it without the pass knowing it, except what stays with the prototype (`core.name`,
/// `core.source`, `core.collision`: the form's own are left behind, counted); a form that draws nothing leaves the
/// prototype without a mesh. The other forms are despawned and `core.lods` removed. The whole selection is checked before anything
/// changes: an invalid one (a prototype drawn with another form while it holds an opaque component, which can't be
/// told to follow, among them) is an error and leaves the world as it was.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SelectLod {
    pub choice: LodChoice,
}

/// `select-lod`'s declaration ([`crate::pass::PassRegistry::add`]).
pub const SELECT_LOD: PassSpec = PassSpec {
    name: "select-lod",
    options: &[OptionSpec::required(
        "form",
        "finest",
        "finest, default (the form the source names, else the finest) or a level 0-255: the form each prototype \
         keeps (LodChoice)",
    )],
    make: select_lod,
};

fn select_lod(g: &Given) -> Result<Box<dyn Pass>, String> {
    let choice = match g.required("form") {
        "finest" => LodChoice::Finest,
        "default" => LodChoice::Default,
        n => LodChoice::AtMost(
            n.parse()
                .map_err(|_| format!("form={n} is not finest, default or a level 0-255"))?,
        ),
    };
    Ok(Box::new(SelectLod { choice }))
}

impl Pass for SelectLod {
    fn name(&self) -> &'static str {
        "select-lod"
    }
    fn options(&self) -> Options {
        let form = match self.choice {
            LodChoice::Finest => "finest".to_owned(),
            LodChoice::Default => "default".to_owned(),
            LodChoice::AtMost(n) => n.to_string(),
        };
        Options::from([("form".to_owned(), form)])
    }
    /// A prototype drawn with another form loses its own mesh and variants (and keeps the form's).
    fn removes(&self) -> &[&'static str] {
        &[Lods::NAME, Mesh::NAME, MeshFile::NAME, MaterialVariants::NAME]
    }
    fn modifies(&self) -> &[&'static str] {
        &[Mesh::NAME, MeshFile::NAME, MaterialVariants::NAME]
    }
    fn despawns_entities(&self) -> bool {
        true
    }
    /// The per-primitive data of a prototype's own form ([`World::remove_mesh`]) is whatever its reader attached; each
    /// name taken from a prototype the chosen form doesn't give back is recorded.
    fn removes_by_log(&self) -> bool {
        true
    }
    fn approximates(&self) -> &'static str {
        match self.choice {
            LodChoice::Default => "the finest form where the source authored no default",
            _ => "",
        }
    }
    fn run(&self, w: &mut World, log: &mut PassLog) -> Result<(), String> {
        let all: Vec<(Entity, Lods)> = w.iter::<Lods>().map(|(e, l)| (e, l.clone())).collect();
        let mut forms = BTreeSet::new();
        for (proto, lods) in &all {
            lods.verify(*proto, w).map_err(|m| format!("{proto:?}: {m}"))?;
            for f in &lods.forms {
                if !forms.insert(f.form) {
                    return Err(format!("form {:?} belongs to more than one prototype", f.form));
                }
            }
            if lods.choose(self.choice) != 0 {
                w.check_remove_mesh(*proto)?;
            }
        }
        if let Some((name, holder, form)) = w
            .references_to(&forms)
            .into_iter()
            .find(|(name, _, _)| *name != Lods::NAME)
        {
            return Err(format!("{name} on {holder:?} refers to form {form:?}"));
        }
        for (proto, lods) in all {
            let chosen = lods.choose(self.choice);
            if self.choice == LodChoice::Default && lods.default.is_none() {
                log.approximate("prototypes without an authored default: the finest form", 1);
            }
            match chosen.checked_sub(1).map(|k| lods.forms[k]) {
                None => log.count("prototypes drawn with their own form", 1),
                Some(f) => {
                    if let Some(own) = w.get::<Mesh>(proto) {
                        let n = own.primitives().len() as u64;
                        log.dropped(
                            crate::ledger::Measure::Primitives,
                            "the prototype's own form left out",
                            n,
                        );
                    }
                    for name in w.remove_mesh(proto)? {
                        log.record_removed(name);
                    }
                    // what stays with the prototype whichever form is drawn is its own: a form's own name, identity or
                    // collision (a reader that names each form) is left behind
                    for (left, name) in [
                        (w.remove::<Name>(f.form).is_some(), Name::NAME),
                        (w.remove::<Source>(f.form).is_some(), Source::NAME),
                        (w.remove::<Collision>(f.form).is_some(), Collision::NAME),
                    ] {
                        if left {
                            log.record_removed(name);
                            log.count(
                                &format!(
                                    "{name} of the chosen form left behind: the prototype keeps its own"
                                ),
                                1,
                            );
                        }
                    }
                    let moved = w.move_components(f.form, proto)?;
                    debug_assert!(moved, "checked alive above");
                    let draws = w.has::<Mesh>(proto) || w.has::<MeshFile>(proto);
                    log.count(
                        if draws {
                            "prototypes drawn with another form"
                        } else {
                            "prototypes whose chosen form draws nothing"
                        },
                        1,
                    );
                }
            }
            for (k, f) in lods.forms.iter().enumerate() {
                if k + 1 != chosen {
                    crate::ledger::despawn(w, f.form, log, "forms left out");
                    log.count("forms left out", 1);
                }
            }
            w.remove::<Lods>(proto);
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pass::{PassManager, PassRegistry, RunReport};
    use crate::{Opaque, OpaqueData};

    /// A dialect's per-form data, as a reader attaches it to each form: what a test sees moving with the form taken.
    #[derive(Clone, Debug, PartialEq)]
    struct Tag(String);
    impl Component for Tag {
        const NAME: &'static str = "game.tag";
        const GEOMETRY: crate::Geometry = crate::Geometry::None;
    }

    /// A prototype at level 0 with two other forms (level 1 from a file, level 3 drawing nothing), default the
    /// level-1 form; each form tags itself and the coarse one carries opaque game data, so a test sees what moved; the
    /// name is the prototype's.
    fn world() -> (World, Entity, [Entity; 2]) {
        let mut w = World::new();
        let proto = w.spawn();
        w.insert(proto, Mesh::default());
        w.insert(proto, Name("the prototype".into()));
        w.insert(proto, Tag("finest".into()));
        let coarse = w.spawn();
        w.insert(
            coarse,
            MeshFile {
                path: "p.glb".into(),
                mesh: 1,
                materials: vec![],
            },
        );
        w.insert(coarse, Tag("coarse".into()));
        w.insert_opaque(
            coarse,
            "game.form",
            Opaque {
                version: 1,
                data: OpaqueData::Json(serde_json::json!({"lod": 1})),
            },
        )
        .unwrap();
        let nothing = w.spawn();
        w.insert(nothing, Tag("nothing".into()));
        w.insert(
            proto,
            Lods {
                detail: 0,
                forms: vec![
                    LodForm {
                        detail: 1,
                        form: coarse,
                    },
                    LodForm {
                        detail: 3,
                        form: nothing,
                    },
                ],
                default: Some(1),
            },
        );
        (w, proto, [coarse, nothing])
    }

    fn manager() -> PassManager {
        let mut r = PassRegistry::new();
        r.add(&SELECT_LOD);
        PassManager::new(r)
    }

    #[test]
    fn select_lod_refuses_a_dead_form_before_it_changes_anything() {
        // run, unlike convert, does not verify its input: the pass checks its forms itself
        let (mut w, proto, forms) = world();
        w.despawn(forms[0]);
        let m = manager();
        let err = m
            .run(
                &mut w,
                &m.registry.parse("select-lod{form=default}").unwrap(),
                &mut RunReport::default(),
            )
            .unwrap_err();
        assert!(err.to_string().contains("not another alive entity"), "{err}");
        assert!(w.has::<Mesh>(proto), "the prototype keeps its geometry");
        assert!(w.has::<Lods>(proto) && w.is_alive(forms[1]));
    }

    #[test]
    fn lods_verify_and_keep_their_forms_in_closures_and_subsets() {
        let (w, proto, forms) = world();
        assert_eq!(w.verify_components(), vec![]);
        let keep = w.closure([proto]);
        assert_eq!(keep, [proto, forms[0], forms[1]].into());
        assert_eq!(w.subset(&keep).verify_components(), vec![]);
    }

    /// A reader may name each form and give it its own source; the form drawn leaves them behind and the prototype keeps
    /// its own, counted.
    #[test]
    fn the_prototype_keeps_its_name_and_source_whichever_form_is_drawn() {
        let (mut w, proto, forms) = world();
        w.insert(proto, Source("proto".into()));
        w.insert(forms[0], Name("proto#lod1".into()));
        w.insert(forms[0], Source("proto/lod1".into()));
        assert_eq!(w.verify_components(), vec![]);
        let m = manager().with_pass_checks(true);
        let mut report = RunReport::default();
        m.run(
            &mut w,
            &m.registry.parse("select-lod{form=default}").unwrap(),
            &mut report,
        )
        .unwrap();
        assert_eq!(w.get::<Tag>(proto).unwrap().0, "coarse");
        assert_eq!(w.get::<Name>(proto).unwrap().0, "the prototype");
        assert_eq!(w.get::<Source>(proto).unwrap().0, "proto");
        let counts = &report.steps[0].log.counts;
        assert_eq!(
            counts["core.name of the chosen form left behind: the prototype keeps its own"],
            1
        );
        assert_eq!(
            counts["core.source of the chosen form left behind: the prototype keeps its own"],
            1
        );
    }

    #[test]
    fn choice_follows_the_reference_rule() {
        let lods = |detail: u8, levels: &[u8], default: Option<usize>| Lods {
            detail,
            forms: levels
                .iter()
                .enumerate()
                .map(|(i, &d)| LodForm {
                    detail: d,
                    form: Entity::from_index(10 + i as u32),
                })
                .collect(),
            default,
        };
        let l = lods(0, &[1, 1, 3], Some(2));
        assert_eq!(l.choose(LodChoice::Finest), 0);
        assert_eq!(l.choose(LodChoice::Default), 2);
        // no authored default: the finest
        assert_eq!(lods(0, &[1, 3], None).choose(LodChoice::Default), 0);
        // coarsest at most N; of equal levels the first listed; none at most N: the finest
        assert_eq!(l.choose(LodChoice::AtMost(0)), 0);
        assert_eq!(l.choose(LodChoice::AtMost(2)), 1);
        assert_eq!(l.choose(LodChoice::AtMost(255)), 3);
        assert_eq!(lods(2, &[3], None).choose(LodChoice::AtMost(1)), 0);
        // the order does not matter to the levels
        assert_eq!(lods(0, &[3, 1], None).choose(LodChoice::AtMost(2)), 2);
    }

    #[test]
    fn default_without_an_authored_default_takes_the_finest_named_and_counted() {
        let (mut w, proto, forms) = world();
        w.get_mut::<Lods>(proto).unwrap().default = None;
        assert_eq!(w.verify_components(), vec![]);
        let m = manager();
        let mut report = RunReport::default();
        m.run(
            &mut w,
            &m.registry.parse("select-lod{form=default}").unwrap(),
            &mut report,
        )
        .unwrap();
        assert_eq!(w.get::<Tag>(proto).unwrap().0, "finest");
        assert!(forms.iter().all(|f| !w.is_alive(*f)));
        let step = &report.steps[0];
        assert_eq!(
            step.log.approximations,
            [(
                "prototypes without an authored default: the finest form".to_owned(),
                1
            )]
            .into()
        );
        assert_eq!(
            step.approximates,
            "the finest form where the source authored no default"
        );
        // an authored default is no approximation; neither are the other choices
        let (mut w, _, _) = world();
        let mut report = RunReport::default();
        m.run(
            &mut w,
            &m.registry.parse("select-lod{form=default}").unwrap(),
            &mut report,
        )
        .unwrap();
        assert!(report.steps[0].log.approximations.is_empty());
        assert_eq!(
            SelectLod {
                choice: LodChoice::Finest
            }
            .approximates(),
            ""
        );
    }

    #[test]
    fn select_lod_takes_one_form_with_everything_it_holds() {
        for (pipeline, name, mesh, file, opaque) in [
            ("select-lod{form=finest}", "finest", true, false, false),
            ("select-lod{form=default}", "coarse", false, true, true),
            ("select-lod{form=2}", "coarse", false, true, true),
            ("select-lod{form=3}", "nothing", false, false, false),
        ] {
            let (mut w, proto, forms) = world();
            let m = manager();
            let mut report = RunReport::default();
            m.run(&mut w, &m.registry.parse(pipeline).unwrap(), &mut report)
                .unwrap();
            assert_eq!(w.get::<Tag>(proto).unwrap().0, name, "{pipeline}");
            assert_eq!(
                w.get::<Name>(proto).unwrap().0,
                "the prototype",
                "{pipeline}: the name stays"
            );
            assert_eq!(
                (w.has::<Mesh>(proto), w.has::<MeshFile>(proto)),
                (mesh, file),
                "{pipeline}"
            );
            assert_eq!(w.get_opaque(proto, "game.form").is_some(), opaque, "{pipeline}");
            assert!(!w.has::<Lods>(proto));
            assert!(forms.iter().all(|f| !w.is_alive(*f)), "{pipeline}: a form left");
            assert_eq!(w.entity_count(), 1);
            let step = &report.steps[0];
            let left_out = if name == "finest" { 2 } else { 1 };
            assert_eq!(step.log.counts["forms left out"], left_out, "{pipeline}");
            // the discarded forms' dialect data goes with them, undeclared
            let mut removed = vec!["core.lods"];
            removed.extend((!mesh).then_some("core.mesh"));
            removed.extend((!file).then_some("core.mesh_file"));
            removed.extend((!opaque).then_some("game.form"));
            assert_eq!(step.removed, removed, "{pipeline}");
        }
        assert_eq!(
            SELECT_LOD
                .instance(&Options::from([("form".into(), "2".into())]))
                .unwrap()
                .options()["form"],
            "2"
        );
        // no form is refused; every form prints back as it was given
        assert!(SELECT_LOD.instance(&Options::new()).is_err());
        for form in ["finest", "default", "0", "255"] {
            let given = Options::from([("form".into(), form.into())]);
            assert_eq!(SELECT_LOD.instance(&given).unwrap().options(), given, "{form}");
        }
        assert!(
            SELECT_LOD
                .instance(&Options::from([("form".into(), "high".into())]))
                .is_err()
        );
        assert!(
            SELECT_LOD
                .instance(&Options::from([("lod".into(), "1".into())]))
                .is_err()
        );
    }

    #[test]
    fn select_lod_preserves_opaque_shader_inputs() {
        use crate::{Primitive, Semantic, Stream, Topology};
        use std::collections::BTreeMap;
        let (mut w, proto, forms) = world();
        let raw = vec![[-3.0, 0.5, 9.0, -0.0]; 3];
        w.insert(
            forms[0],
            Mesh::new(vec![Primitive {
                topology: Topology::Triangles,
                streams: BTreeMap::from([
                    (Semantic::Position, Stream::F32x3(vec![[0.0; 3]; 3])),
                    (Semantic::ShaderInput(4), Stream::F32x4(raw.clone())),
                ]),
                indices: None,
                material: None,
            }]),
        );
        let m = manager();
        m.run(
            &mut w,
            &m.registry.parse("select-lod{form=default}").unwrap(),
            &mut RunReport::default(),
        )
        .unwrap();
        let mesh = w.get::<Mesh>(proto).unwrap();
        let Stream::F32x4(values) = &mesh.primitives()[0].streams[&Semantic::ShaderInput(4)] else {
            panic!("opaque stream missing")
        };
        assert_eq!(
            values.iter().map(|v| v.map(f32::to_bits)).collect::<Vec<_>>(),
            raw.iter().map(|v| v.map(f32::to_bits)).collect::<Vec<_>>()
        );
    }

    /// Dialect data per primitive of its entity's mesh, as a reader attaches it.
    #[derive(Clone, Debug, PartialEq)]
    struct PerPrimitive(Vec<u8>);
    impl Component for PerPrimitive {
        const NAME: &'static str = "game.per_primitive";
        const GEOMETRY: crate::Geometry = crate::Geometry::PerPrimitive;
        fn remap_primitives(&self, from: &[usize]) -> Option<Result<Self, String>> {
            Some(crate::remap_list(&self.0, from).map(PerPrimitive))
        }
    }

    /// The prototype's own form takes its per-primitive data with it when another form is drawn, whether its mesh is
    /// loaded or still only in its file (a world before its load): nothing describing the discarded form stays on the
    /// prototype when the chosen form has no such data of its own.
    #[test]
    fn the_own_forms_per_primitive_data_leaves_with_it() {
        for loaded in [true, false] {
            let (mut w, proto, _) = world();
            if !loaded {
                w.remove::<Mesh>(proto);
                w.insert(
                    proto,
                    MeshFile {
                        path: "own.glb".into(),
                        mesh: 0,
                        materials: vec![],
                    },
                );
            }
            w.insert(proto, PerPrimitive(vec![]));
            w.insert(proto, MaterialVariants(vec![]));
            let m = manager().with_pass_checks(true);
            let mut report = RunReport::default();
            m.run(
                &mut w,
                &m.registry.parse("select-lod{form=default}").unwrap(),
                &mut report,
            )
            .unwrap();
            assert_eq!(w.get::<Tag>(proto).unwrap().0, "coarse");
            assert!(
                !w.has::<PerPrimitive>(proto) && !w.has::<MaterialVariants>(proto),
                "loaded {loaded}: the own form's per-primitive data stayed"
            );
            assert_eq!(
                w.get::<MeshFile>(proto).unwrap().path,
                std::path::Path::new("p.glb")
            );
            let removed = &report.steps[0].removed;
            for name in [PerPrimitive::NAME, MaterialVariants::NAME] {
                assert!(removed.iter().any(|n| n == name), "loaded {loaded}: {removed:?}");
            }
        }
        // the own form drawn: its data stays
        let (mut w, proto, _) = world();
        w.insert(proto, PerPrimitive(vec![]));
        let m = manager().with_pass_checks(true);
        m.run(
            &mut w,
            &m.registry.parse("select-lod{form=finest}").unwrap(),
            &mut RunReport::default(),
        )
        .unwrap();
        assert!(w.has::<PerPrimitive>(proto) && w.has::<Mesh>(proto));
    }

    /// An opaque component on a prototype drawn with another form can't be told to follow its own form's removal: the
    /// selection is refused before anything changes. Drawn with its own form, nothing is removed and it passes.
    #[test]
    fn an_opaque_component_on_a_prototype_drawn_with_another_form_is_refused() {
        let opaque = || Opaque {
            version: 1,
            data: OpaqueData::Bytes(vec![1]),
        };
        let (mut w, proto, forms) = world();
        w.insert_opaque(proto, "game.thing", opaque()).unwrap();
        let m = manager().with_pass_checks(true);
        let err = m
            .run(
                &mut w,
                &m.registry.parse("select-lod{form=default}").unwrap(),
                &mut RunReport::default(),
            )
            .unwrap_err();
        assert!(err.to_string().contains("game.thing"), "{err}");
        assert!(w.has::<Mesh>(proto) && w.has::<Lods>(proto));
        assert!(forms.iter().all(|f| w.is_alive(*f)));
        assert_eq!(w.get::<Tag>(proto).unwrap().0, "finest");
        m.run(
            &mut w,
            &m.registry.parse("select-lod{form=finest}").unwrap(),
            &mut RunReport::default(),
        )
        .unwrap();
        assert!(w.get_opaque(proto, "game.thing").is_some() && w.has::<Mesh>(proto));
    }

    #[test]
    fn an_invalid_selection_is_refused_without_mutation() {
        // an out-of-range default, also on a prototype after a valid one
        let (mut w, proto, forms) = world();
        let second = w.spawn();
        w.insert(second, Mesh::default());
        w.insert(
            second,
            Lods {
                detail: 0,
                forms: vec![],
                default: Some(99),
            },
        );
        let err = SelectLod {
            choice: LodChoice::Default,
        }
        .run(&mut w, &mut PassLog::default())
        .unwrap_err();
        assert!(err.contains("default form 99 of 1"), "{err}");
        assert_eq!(w.get::<Tag>(proto).unwrap().0, "finest");
        assert!(forms.iter().all(|e| w.is_alive(*e)) && w.has::<Lods>(second));
        // a form shared by two prototypes
        w.insert(second, w.get::<Lods>(proto).unwrap().clone());
        let err = SelectLod {
            choice: LodChoice::Default,
        }
        .run(&mut w, &mut PassLog::default())
        .unwrap_err();
        assert!(err.contains("more than one prototype"), "{err}");
        // a form something else refers to
        w.remove::<Lods>(second);
        let placed = w.spawn();
        w.insert(
            placed,
            Instance {
                prototype: forms[0],
                variant: 0,
            },
        );
        let err = SelectLod {
            choice: LodChoice::Default,
        }
        .run(&mut w, &mut PassLog::default())
        .unwrap_err();
        assert!(
            err.contains("core.instance") && err.contains("refers to form"),
            "{err}"
        );
        assert!(forms.iter().all(|e| w.is_alive(*e)));
        assert_eq!(w.get::<Tag>(proto).unwrap().0, "finest");
    }

    #[test]
    fn malformed_lods_fail_verification() {
        let failure = |edit: &dyn Fn(&mut World, Entity, [Entity; 2])| {
            let (mut w, proto, forms) = world();
            edit(&mut w, proto, forms);
            w.verify_components()
                .into_iter()
                .map(|(_, _, m)| m)
                .collect::<Vec<_>>()
        };
        let twice = failure(&|w, proto, _| {
            let lods = w.get_mut::<Lods>(proto).unwrap();
            lods.forms[1] = lods.forms[0];
        });
        assert!(twice[0].contains("listed twice"), "{twice:?}");
        let placed = failure(&|w, _, forms| {
            w.insert(forms[0], Transform::IDENTITY);
        });
        assert!(placed[0].contains("placed"), "{placed:?}");
        let meshless = failure(&|w, proto, _| {
            w.remove::<Mesh>(proto);
        });
        assert!(meshless[0].contains("without a mesh"), "{meshless:?}");
        let gone = failure(&|w, _, forms| {
            w.despawn(forms[1]);
        });
        assert!(gone[0].contains("not another alive entity"), "{gone:?}");
    }
}