bread-format 0.5.0

Reads and writes .bread files: tiny, lossless meshes and scenes, with room for slicer data such as editable supports.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
/*
 * SPDX-FileCopyrightText: 2026 SynrgStudio
 * SPDX-License-Identifier: Apache-2.0
 */

//! Mesh chunk version 2 (`SPEC.md`, sections 6.6 to 6.9): the same mesh as
//! version 1, kept as exactly, in fewer bytes.
//!
//! Triangles go in their own order (connectivity A) or in the order the
//! triangle codec compresses best, with the way back (connectivity B).
//! Vertices go through meshoptimizer's vertex codec, or are predicted from
//! the triangles around them and only the difference is stored: entropy-coded
//! with rANS, or as byte planes for the chunk's zstd.
//!
//! ```text
//! metadata      as in version 1
//! counts        u32 vertices, u32 triangles
//! codings       u8 connectivity (0 A, 1 B), u8 vertex coding
//!               (0 meshopt, 1 predicted + rANS, 2 predicted + planes)
//! parts         u32 length + bytes each:
//!               A: index buffer, corners
//!               B: index buffer, permutation, corners
//!               then meshopt: vertex buffer; rANS: tables, stream, raw
//!               bits; planes: 12 byte planes
//! ```

use crate::bytes::{In, Out, fits_u32};
use crate::mesh::{DecodeMeshError, EncodeMeshError, Mesh};
use crate::meta::{read_meta, write_meta};
use crate::parallel;

/// The first byte of a vertex buffer: attribute codec version 1.
const VERTEX_HEADER: u8 = 0xa1;
/// The first byte of an index buffer: triangle codec version 1.
const INDEX_HEADER: u8 = 0xe1;
const VERTEX_VERSION: i32 = 1;
const VERTEX_LEVEL: i32 = 2;
const VERTICES_PER_BLOCK: usize = 256;
const BLOCK_HEADER_BYTES: usize = 3;
const INDEX_BUFFER_OVERHEAD: usize = 17;

/// How a version 2 mesh stores its triangles.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Connectivity {
    /// In their own order, through the triangle codec, with each first
    /// corner in 2 bits.
    InOrder,
    /// Reordered for the triangle codec, with the permutation back to their
    /// order and each first corner.
    Reordered,
}

/// How a version 2 mesh stores its vertices.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VertexCoding {
    /// meshoptimizer's vertex codec, version 1.
    Meshopt,
    /// Predicted from the triangles, the differences coded with rANS.
    Rans,
    /// Predicted from the triangles, the differences as 12 byte planes.
    Planes,
}

/// The codings a version 2 mesh payload uses.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MeshCoding {
    pub connectivity: Connectivity,
    pub vertices: VertexCoding,
}

// ---------------------------------------------------------------- bits

/// Writes values least significant bit first.
#[derive(Default)]
struct BitWriter {
    bytes: Vec<u8>,
    acc: u64,
    count: u32,
}

impl BitWriter {
    fn put(&mut self, value: u32, bits: u32) {
        if bits == 0 {
            return;
        }
        self.acc |= (u64::from(value) & ((1u64 << bits) - 1)) << self.count;
        self.count += bits;
        while self.count >= 8 {
            self.bytes.push(self.acc as u8);
            self.acc >>= 8;
            self.count -= 8;
        }
    }

    fn finish(mut self) -> Vec<u8> {
        if self.count > 0 {
            self.bytes.push(self.acc as u8);
        }
        self.bytes
    }
}

/// Reads what a [`BitWriter`] wrote, and nothing else: [`BitReader::done`]
/// checks that every byte was needed and the padding is zero.
struct BitReader<'a> {
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> BitReader<'a> {
    fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, pos: 0 }
    }

    fn get(&mut self, bits: u32) -> Option<u32> {
        if bits == 0 {
            return Some(0);
        }
        let byte = self.pos >> 3;
        let value = match self.bytes.get(byte..byte + 8) {
            // Eight whole bytes ahead: read them at once.
            Some(word) => {
                let word = u64::from_le_bytes(word.try_into().ok()?);
                (word >> (self.pos & 7)) as u32 & ((1u64 << bits) - 1) as u32
            }
            // Near the end: bit by bit, so running out is noticed.
            None => {
                let mut value = 0u32;
                for i in 0..bits {
                    let byte = *self.bytes.get((self.pos + i as usize) >> 3)?;
                    value |= u32::from((byte >> ((self.pos + i as usize) & 7)) & 1) << i;
                }
                value
            }
        };
        self.pos += bits as usize;
        Some(value)
    }

    fn done(&self) -> bool {
        let used = self.pos.div_ceil(8);
        used == self.bytes.len()
            && (self.pos & 7 == 0 || self.bytes[used - 1] >> (self.pos & 7) == 0)
    }
}

/// The number of bits each permutation entry takes: enough for `n - 1`, and
/// at least 1.
fn permutation_bits(triangles: usize) -> u32 {
    usize::BITS - (triangles.max(2) - 1).leading_zeros()
}

// ---------------------------------------------------------------- prediction

/// `f32` bits mapped to a `u32` that orders as the values do.
fn ord(value: f32) -> u32 {
    let bits = value.to_bits();
    if bits & 0x8000_0000 != 0 {
        !bits
    } else {
        bits | 0x8000_0000
    }
}

fn unord(ordered: u32) -> f32 {
    f32::from_bits(if ordered & 0x8000_0000 != 0 {
        ordered & 0x7fff_ffff
    } else {
        !ordered
    })
}

fn zigzag(value: u32) -> u32 {
    let value = value as i32;
    ((value << 1) ^ (value >> 31)) as u32
}

fn unzigzag(value: u32) -> u32 {
    (value >> 1) ^ (value & 1).wrapping_neg()
}

/// The triangles around each vertex, and across each edge, of a triangle
/// list. Built in linear time whatever the triangles, so a damaged or hostile
/// mesh cannot make prediction slow.
struct Topology {
    /// For vertex `v`, `around[start[v]..start[v + 1]]` lists the triangles
    /// that have it as a corner, each once, in increasing order.
    start: Vec<u32>,
    around: Vec<u32>,
    /// For corner `k` of triangle `t`, at `3 t + k`: the first triangle other
    /// than `t`, in increasing order, with an edge between the other two
    /// corners, or `u32::MAX`.
    across: Vec<u32>,
}

const NONE: u32 = u32::MAX;

fn topology(triangles: &[[u32; 3]], vertex_count: usize) -> Topology {
    // The two halves do not depend on each other.
    let ((start, around), across) = parallel::join(
        || around_vertices(triangles, vertex_count),
        || across_edges(triangles, vertex_count),
    );
    Topology {
        start,
        around,
        across,
    }
}

/// `Topology::start` and `Topology::around`.
fn around_vertices(triangles: &[[u32; 3]], vertex_count: usize) -> (Vec<u32>, Vec<u32>) {
    // Triangles around each vertex, a triangle with a repeated corner once.
    let mut start = vec![0u32; vertex_count + 1];
    for t in triangles {
        for (k, &v) in t.iter().enumerate() {
            if !t[..k].contains(&v) {
                start[v as usize + 1] += 1;
            }
        }
    }
    for v in 0..vertex_count {
        start[v + 1] += start[v];
    }
    let mut fill = start.clone();
    let mut around = vec![0u32; start[vertex_count] as usize];
    for (i, t) in triangles.iter().enumerate() {
        for (k, &v) in t.iter().enumerate() {
            if !t[..k].contains(&v) {
                around[fill[v as usize] as usize] = i as u32;
                fill[v as usize] += 1;
            }
        }
    }
    (start, around)
}

/// `Topology::across`.
fn across_edges(triangles: &[[u32; 3]], vertex_count: usize) -> Vec<u32> {
    // Edges grouped by their lower vertex, then sorted by their higher one:
    // the per-vertex lists are short in any mesh, and the total work is
    // bounded by the triangle count whatever their degrees.
    let mut edge_start = vec![0u32; vertex_count + 1];
    for t in triangles {
        for k in 0..3 {
            let low = t[(k + 1) % 3].min(t[(k + 2) % 3]);
            edge_start[low as usize + 1] += 1;
        }
    }
    for v in 0..vertex_count {
        edge_start[v + 1] += edge_start[v];
    }
    let mut fill = edge_start.clone();
    // The higher vertex in the top half, the triangle's corner `3 t + k` in
    // the bottom one, so sorting orders by edge, then by triangle.
    let mut edges = vec![0u64; 3 * triangles.len()];
    for (i, t) in triangles.iter().enumerate() {
        for k in 0..3 {
            let (a, b) = (t[(k + 1) % 3], t[(k + 2) % 3]);
            let slot = &mut fill[a.min(b) as usize];
            edges[*slot as usize] = (u64::from(a.max(b)) << 32) | (3 * i + k) as u64;
            *slot += 1;
        }
    }
    let mut across = vec![NONE; 3 * triangles.len()];
    for v in 0..vertex_count {
        let list = &mut edges[edge_start[v] as usize..edge_start[v + 1] as usize];
        list.sort_unstable();
        for group in list.chunk_by(|x, y| x >> 32 == y >> 32) {
            let triangle = |e: &u64| (*e as u32) / 3;
            // The first triangle on the edge, and the first one after it:
            // a triangle meets an edge at most three times.
            let first = triangle(&group[0]);
            let second = group
                .iter()
                .map(triangle)
                .find(|&t| t != first)
                .unwrap_or(NONE);
            for e in group {
                let t = triangle(e);
                across[*e as u32 as usize] = if t == first { second } else { first };
            }
        }
    }
    across
}

/// The prediction of vertex `v` from the vertices before it (`SPEC.md`,
/// section 6.8). Every step is in `f64`, and the result is rounded to `f32`
/// once; a result that is not finite becomes `+0.0`.
fn predict(v: u32, triangles: &[[u32; 3]], topo: &Topology, vertices: &[[f32; 3]]) -> [f32; 3] {
    let at = |i: u32| vertices[i as usize].map(f64::from);
    let (mut para, mut para_n) = ([0f64; 3], 0u32);
    let (mut mid, mut mid_n) = ([0f64; 3], 0u32);
    let (mut near, mut near_n) = ([0f64; 3], 0u32);
    let list = &topo.around[topo.start[v as usize] as usize..topo.start[v as usize + 1] as usize];
    for &t in list {
        let tri = triangles[t as usize];
        let k = tri.iter().position(|&c| c == v).unwrap_or(0);
        let (a, b) = (tri[(k + 1) % 3], tri[(k + 2) % 3]);
        let (known_a, known_b) = (a < v, b < v);
        if known_a {
            let p = at(a);
            (0..3).for_each(|i| near[i] += p[i]);
            near_n += 1;
        }
        if known_b {
            let p = at(b);
            (0..3).for_each(|i| near[i] += p[i]);
            near_n += 1;
        }
        if known_a && known_b {
            let (pa, pb) = (at(a), at(b));
            (0..3).for_each(|i| mid[i] += 0.5 * (pa[i] + pb[i]));
            mid_n += 1;
            let other = topo.across[3 * t as usize + k];
            if other != NONE {
                let opposite = triangles[other as usize]
                    .into_iter()
                    .find(|&c| c != a && c != b);
                if let Some(c) = opposite.filter(|&c| c < v) {
                    let pc = at(c);
                    (0..3).for_each(|i| para[i] += (pa[i] + pb[i]) - pc[i]);
                    para_n += 1;
                }
            }
        }
    }
    let (sum, n) = if para_n > 0 {
        (para, para_n)
    } else if mid_n > 0 {
        (mid, mid_n)
    } else if near_n > 0 {
        (near, near_n)
    } else if v > 0 {
        (at(v - 1), 1)
    } else {
        ([0.0; 3], 1)
    };
    sum.map(|s| {
        let p = (s / f64::from(n)) as f32;
        if p.is_finite() { p } else { 0.0 }
    })
}

/// The residuals of `vertices`, in vertex order, each component zigzagged.
fn residuals(vertices: &[[f32; 3]], triangles: &[[u32; 3]]) -> Vec<[u32; 3]> {
    let topo = topology(triangles, vertices.len());
    (0..vertices.len())
        .map(|v| {
            let p = predict(v as u32, triangles, &topo, vertices);
            std::array::from_fn(|i| zigzag(ord(vertices[v][i]).wrapping_sub(ord(p[i]))))
        })
        .collect()
}

fn rebuild(residuals: &[[u32; 3]], triangles: &[[u32; 3]], topo: &Topology) -> Vec<[f32; 3]> {
    let mut vertices = vec![[0f32; 3]; residuals.len()];
    for v in 0..residuals.len() {
        let p = predict(v as u32, triangles, topo, &vertices);
        vertices[v] =
            std::array::from_fn(|i| unord(ord(p[i]).wrapping_add(unzigzag(residuals[v][i]))));
    }
    vertices
}

// ---------------------------------------------------------------- rANS

const PROB_BITS: u32 = 12;
const PROB_TOTAL: u32 = 1 << PROB_BITS;
const RANS_LOW: u32 = 1 << 23;
/// High bits of a residual folded into its symbol, besides its bit length.
const TOP_BITS: u32 = 2;
const SYMBOLS: usize = 33 << TOP_BITS;
const BUCKETS: usize = 34;
const CONTEXTS: usize = 3 * BUCKETS;

fn bit_length(r: u32) -> u32 {
    u32::BITS - r.leading_zeros()
}

/// The context of component `comp` of a residual: the bit lengths of what
/// came before it.
fn context(comp: usize, current: &[u32; 3], previous: &[u32; 3]) -> usize {
    let bucket = match comp {
        0 => previous.iter().map(|&r| bit_length(r)).max().unwrap_or(0),
        1 => bit_length(current[0]),
        _ => (bit_length(current[0]) + bit_length(current[1])).div_ceil(2),
    };
    comp * BUCKETS + bucket as usize
}

/// A residual as a symbol (its bit length and up to two bits under the top
/// one) and the raw bits below those.
fn symbol(r: u32) -> (u32, u32, u32) {
    let length = bit_length(r);
    if length <= 1 {
        return (length << TOP_BITS, 0, 0);
    }
    let low = length - 1;
    let top = low.min(TOP_BITS);
    let raw_bits = low - top;
    let high = ((r >> raw_bits) & ((1 << top) - 1)) << (TOP_BITS - top);
    let raw = r & ((1u64 << raw_bits) - 1) as u32;
    ((length << TOP_BITS) | high, raw, raw_bits)
}

/// The residual a symbol and its raw bits stand for, or `None` for a symbol
/// no writer produces.
fn residual(symbol: u32, raw: &mut BitReader<'_>) -> Option<u32> {
    let length = symbol >> TOP_BITS;
    let high = symbol & ((1 << TOP_BITS) - 1);
    if length <= 1 {
        return (high == 0).then_some(length);
    }
    let low = length - 1;
    let top = low.min(TOP_BITS);
    if high & ((1 << (TOP_BITS - top)) - 1) != 0 {
        return None;
    }
    let raw_bits = low - top;
    Some((1 << low) | ((high >> (TOP_BITS - top)) << raw_bits) | raw.get(raw_bits)?)
}

/// Symbol frequencies summing to `PROB_TOTAL`, each used symbol at least 1.
fn normalize(counts: &[u64]) -> Vec<u32> {
    let total: u64 = counts.iter().sum();
    let mut freq: Vec<u32> = counts
        .iter()
        .map(|&c| {
            if c == 0 {
                0
            } else {
                ((c * u64::from(PROB_TOTAL)) / total).max(1) as u32
            }
        })
        .collect();
    let mut sum: i64 = freq.iter().map(|&f| i64::from(f)).sum();
    while sum != i64::from(PROB_TOTAL) {
        let (i, _) = freq
            .iter()
            .enumerate()
            .max_by_key(|&(i, &f)| (f, std::cmp::Reverse(i)))
            .expect("a used context has a symbol");
        if sum > i64::from(PROB_TOTAL) {
            let take = (sum - i64::from(PROB_TOTAL)).min(i64::from(freq[i]) - 1);
            freq[i] -= take as u32;
            sum -= take;
        } else {
            freq[i] += (i64::from(PROB_TOTAL) - sum) as u32;
            sum = i64::from(PROB_TOTAL);
        }
    }
    freq
}

/// A context's table: frequencies, their running sums, and the symbol of
/// every slot.
struct Table {
    freq: Vec<u32>,
    cum: Vec<u32>,
    slots: Vec<u16>,
}

fn table(freq: Vec<u32>) -> Table {
    let mut cum = vec![0u32; freq.len() + 1];
    for s in 0..freq.len() {
        cum[s + 1] = cum[s] + freq[s];
    }
    let mut slots = vec![0u16; PROB_TOTAL as usize];
    for s in 0..freq.len() {
        slots[cum[s] as usize..cum[s + 1] as usize].fill(s as u16);
    }
    Table { freq, cum, slots }
}

/// Codes residuals as (tables, stream, raw bits).
fn encode_rans(residuals: &[[u32; 3]]) -> [Vec<u8>; 3] {
    let mut counts = vec![0u64; CONTEXTS * SYMBOLS];
    let mut items = Vec::with_capacity(3 * residuals.len());
    let mut raw = BitWriter::default();
    let mut previous = [0u32; 3];
    for r in residuals {
        for comp in 0..3 {
            let c = context(comp, r, &previous);
            let (s, bits, n) = symbol(r[comp]);
            counts[c * SYMBOLS + s as usize] += 1;
            items.push((c, s as usize));
            raw.put(bits, n);
        }
        previous = *r;
    }
    let mut tables_out = BitWriter::default();
    let mut tables = Vec::with_capacity(CONTEXTS);
    for c in 0..CONTEXTS {
        let counts = &counts[c * SYMBOLS..(c + 1) * SYMBOLS];
        if counts.iter().all(|&n| n == 0) {
            tables_out.put(0, 1);
            tables.push(None);
            continue;
        }
        tables_out.put(1, 1);
        let freq = normalize(counts);
        for &f in &freq {
            if f == 0 {
                tables_out.put(0, 1);
            } else {
                tables_out.put(1, 1);
                tables_out.put(f - 1, PROB_BITS);
            }
        }
        tables.push(Some(table(freq)));
    }
    // rANS codes backwards, so the decoder reads forwards.
    let mut x = RANS_LOW;
    let mut stream = Vec::with_capacity(items.len());
    for &(c, s) in items.iter().rev() {
        let t = tables[c].as_ref().expect("a used context has a table");
        let (f, start) = (t.freq[s], t.cum[s]);
        let limit = ((RANS_LOW >> PROB_BITS) << 8) * f;
        while x >= limit {
            stream.push(x as u8);
            x >>= 8;
        }
        x = ((x / f) << PROB_BITS) + (x % f) + start;
    }
    stream.extend(x.to_le_bytes());
    stream.reverse();
    [tables_out.finish(), stream, raw.finish()]
}

fn decode_rans(
    tables: &[u8],
    stream: &[u8],
    raw: &[u8],
    vertex_count: usize,
) -> Option<Vec<[u32; 3]>> {
    let mut bits = BitReader::new(tables);
    let mut parsed: Vec<Option<Table>> = Vec::with_capacity(CONTEXTS);
    for _ in 0..CONTEXTS {
        if bits.get(1)? == 0 {
            parsed.push(None);
            continue;
        }
        let mut freq = Vec::with_capacity(SYMBOLS);
        for _ in 0..SYMBOLS {
            freq.push(if bits.get(1)? == 0 {
                0
            } else {
                bits.get(PROB_BITS)? + 1
            });
        }
        if freq.iter().sum::<u32>() != PROB_TOTAL {
            return None;
        }
        parsed.push(Some(table(freq)));
    }
    if !bits.done() {
        return None;
    }
    let (head, mut rest) = stream.split_first_chunk::<4>()?;
    let mut x = u64::from(u32::from_be_bytes(*head));
    let mut raw = BitReader::new(raw);
    let mut out = Vec::with_capacity(vertex_count);
    let mut previous = [0u32; 3];
    for _ in 0..vertex_count {
        let mut r = [0u32; 3];
        for comp in 0..3 {
            let t = parsed[context(comp, &r, &previous)].as_ref()?;
            let slot = (x & u64::from(PROB_TOTAL - 1)) as u32;
            let s = usize::from(t.slots[slot as usize]);
            x = u64::from(t.freq[s]) * (x >> PROB_BITS) + u64::from(slot - t.cum[s]);
            while x < u64::from(RANS_LOW) {
                let (&byte, next) = rest.split_first()?;
                x = (x << 8) | u64::from(byte);
                rest = next;
            }
            r[comp] = residual(s as u32, &mut raw)?;
        }
        out.push(r);
        previous = r;
    }
    (x == u64::from(RANS_LOW) && rest.is_empty() && raw.done()).then_some(out)
}

// ---------------------------------------------------------------- planes

fn encode_planes(residuals: &[[u32; 3]]) -> Vec<u8> {
    let mut out = Vec::with_capacity(12 * residuals.len());
    for comp in 0..3 {
        for byte in 0..4 {
            out.extend(residuals.iter().map(|r| (r[comp] >> (8 * byte)) as u8));
        }
    }
    out
}

fn decode_planes(planes: &[u8], vertex_count: usize) -> Option<Vec<[u32; 3]>> {
    if Some(planes.len()) != vertex_count.checked_mul(12) {
        return None;
    }
    let mut out = vec![[0u32; 3]; vertex_count];
    for comp in 0..3 {
        for byte in 0..4 {
            let plane = &planes[(4 * comp + byte) * vertex_count..][..vertex_count];
            for (r, &b) in out.iter_mut().zip(plane) {
                r[comp] |= u32::from(b) << (8 * byte);
            }
        }
    }
    Some(out)
}

// ---------------------------------------------------------------- triangles

/// The corner a stored triangle has to start from to be `original`: 0, 1 or
/// 2, with `original[k] == stored[(k + r) % 3]`.
fn corner(stored: &[u32], original: &[u32; 3]) -> u32 {
    (0..3u32)
        .find(|&r| (0..3).all(|k| stored[(k + r as usize) % 3] == original[k]))
        .expect("the codec only rotates triangles")
}

fn encode_corners(stored: &[u32], triangles: &[[u32; 3]]) -> Vec<u8> {
    let mut out = BitWriter::default();
    for (s, t) in stored.as_chunks::<3>().0.iter().zip(triangles) {
        out.put(corner(s, t), 2);
    }
    out.finish()
}

/// The stored triangles, each turned to start from its corner.
fn apply_corners(stored: &[u32], corners: &[u8]) -> Option<Vec<[u32; 3]>> {
    let mut bits = BitReader::new(corners);
    let mut out = Vec::with_capacity(stored.len() / 3);
    for s in stored.as_chunks::<3>().0 {
        let r = bits.get(2)? as usize;
        if r > 2 {
            return None;
        }
        out.push(std::array::from_fn(|k| s[(k + r) % 3]));
    }
    bits.done().then_some(out)
}

fn encode_index_buffer(triangles: &[[u32; 3]], vertex_count: usize) -> (Vec<u8>, Vec<u32>) {
    let encoded = meshopt::encode_index_buffer(triangles.as_flattened(), vertex_count)
        .expect("whole triangles encode");
    assert_eq!(
        encoded.first(),
        Some(&INDEX_HEADER),
        "the triangle codec writes version 1"
    );
    let stored = meshopt::decode_index_buffer(&encoded, 3 * triangles.len())
        .expect("the codec reads its own output");
    (encoded, stored)
}

/// Whether connectivity B can store `mesh`: its vertices are numbered in the
/// order its triangles first use them, and every vertex is used.
pub(crate) fn in_first_use_order(mesh: &Mesh) -> bool {
    let mut next = 0u32;
    for &v in mesh.triangles.as_flattened() {
        if v == next {
            next += 1;
        } else if v > next {
            return false;
        }
    }
    next as usize == mesh.vertices.len()
}

/// Connectivity B's order: the triangles reordered for the codec, with the
/// vertices renumbered by first use in that order. Returns the reordered
/// triangles (their own first corners) and vertices, and the original index
/// of each reordered triangle.
fn reorder(mesh: &Mesh) -> (Vec<[u32; 3]>, Vec<[f32; 3]>, Vec<u32>) {
    let n = mesh.vertices.len();
    // Match each reordered triangle to an original one by a key that does
    // not depend on the first corner (the smallest of its three rotations);
    // equal triangles match in any order.
    let key = |t: &[u32]| -> (u32, u32, u32) {
        (0..3)
            .map(|r| (t[r], t[(r + 1) % 3], t[(r + 2) % 3]))
            .min()
            .unwrap_or_default()
    };
    // The originals' keys do not need the new order, so they are sorted
    // while the optimizer finds it.
    let (reordered, originals) = parallel::join(
        || {
            let order = meshopt::optimize_vertex_cache(mesh.triangles.as_flattened(), n);
            let mut reordered: Vec<_> = order
                .as_chunks::<3>()
                .0
                .iter()
                .enumerate()
                .map(|(j, t)| (key(t), j as u32))
                .collect();
            reordered.sort_unstable();
            reordered
        },
        || {
            let mut originals: Vec<_> = mesh
                .triangles
                .iter()
                .enumerate()
                .map(|(i, t)| (key(t), i as u32))
                .collect();
            originals.sort_unstable();
            originals
        },
    );
    let mut permutation = vec![0u32; mesh.triangles.len()];
    for (o, r) in originals.iter().zip(&reordered) {
        debug_assert_eq!(o.0, r.0);
        permutation[r.1 as usize] = o.1;
    }
    let mut renumber = vec![NONE; n];
    let mut vertices = Vec::with_capacity(n);
    let triangles = permutation
        .iter()
        .map(|&p| {
            mesh.triangles[p as usize].map(|v| {
                let new = &mut renumber[v as usize];
                if *new == NONE {
                    *new = vertices.len() as u32;
                    vertices.push(mesh.vertices[v as usize]);
                }
                *new
            })
        })
        .collect();
    (triangles, vertices, permutation)
}

// ---------------------------------------------------------------- payload

/// The triangles and vertices of `mesh` as the vertex coding sees them, and
/// the connectivity parts.
struct Prepared {
    parts: Vec<Vec<u8>>,
    triangles: Vec<[u32; 3]>,
    vertices: Vec<[f32; 3]>,
}

fn prepare(mesh: &Mesh, connectivity: Connectivity) -> Prepared {
    let n = mesh.vertices.len();
    match connectivity {
        Connectivity::InOrder => {
            let (index, stored) = encode_index_buffer(&mesh.triangles, n);
            let corners = encode_corners(&stored, &mesh.triangles);
            Prepared {
                parts: vec![index, corners],
                triangles: mesh.triangles.clone(),
                vertices: mesh.vertices.clone(),
            }
        }
        Connectivity::Reordered => {
            let (triangles, vertices, permutation) = reorder(mesh);
            let (index, stored) = encode_index_buffer(&triangles, n);
            let bits = permutation_bits(triangles.len());
            let mut perm = BitWriter::default();
            for &p in &permutation {
                perm.put(p, bits);
            }
            let corners = encode_corners(&stored, &triangles);
            Prepared {
                parts: vec![index, perm.finish(), corners],
                triangles,
                vertices,
            }
        }
    }
}

fn vertex_parts(prepared: &Prepared, coding: VertexCoding, residuals: &[[u32; 3]]) -> Vec<Vec<u8>> {
    match coding {
        VertexCoding::Meshopt => vec![
            meshopt::encode_vertex_buffer_level(
                &prepared.vertices,
                VERTEX_LEVEL,
                Some(VERTEX_VERSION),
            )
            .expect("12-byte vertices encode"),
        ],
        VertexCoding::Rans => encode_rans(residuals).into(),
        VertexCoding::Planes => vec![encode_planes(residuals)],
    }
}

fn assemble(
    mesh: &Mesh,
    coding: MeshCoding,
    parts: &[Vec<u8>],
) -> Result<Vec<u8>, EncodeMeshError> {
    if parts.iter().any(|part| !fits_u32(part.len())) {
        return Err(EncodeMeshError::TooLarge);
    }
    let mut out = Out::default();
    write_meta(&mut out, mesh.meta.as_ref());
    out.len(mesh.vertices.len());
    out.len(mesh.triangles.len());
    out.u8(match coding.connectivity {
        Connectivity::InOrder => 0,
        Connectivity::Reordered => 1,
    });
    out.u8(match coding.vertices {
        VertexCoding::Meshopt => 0,
        VertexCoding::Rans => 1,
        VertexCoding::Planes => 2,
    });
    for part in parts {
        out.bytes(part);
    }
    Ok(out.0)
}

/// What every coding needs of a mesh, and whether `coding` can store it.
fn check(mesh: &Mesh, coding: MeshCoding) -> Result<(), EncodeMeshError> {
    let n = mesh.vertices.len();
    if !fits_u32(n) || !fits_u32(mesh.triangles.len()) {
        return Err(EncodeMeshError::TooLarge);
    }
    if mesh
        .triangles
        .as_flattened()
        .iter()
        .any(|&v| v as usize >= n)
    {
        return Err(EncodeMeshError::VertexOutOfRange);
    }
    if coding.connectivity == Connectivity::Reordered && !in_first_use_order(mesh) {
        return Err(EncodeMeshError::Unsupported);
    }
    if coding.vertices == VertexCoding::Rans && n > 3 * mesh.triangles.len() {
        return Err(EncodeMeshError::Unsupported);
    }
    Ok(())
}

/// Encodes `mesh` as a mesh chunk payload of version 2 with the given
/// codings.
///
/// Connectivity B needs the vertices in first-use order, every one used; the
/// rANS coding needs no more than three vertices per triangle.
pub fn encode_mesh_v2_with(mesh: &Mesh, coding: MeshCoding) -> Result<Vec<u8>, EncodeMeshError> {
    check(mesh, coding)?;
    let prepared = prepare(mesh, coding.connectivity);
    let residuals = match coding.vertices {
        VertexCoding::Meshopt => Vec::new(),
        _ => residuals(&prepared.vertices, &prepared.triangles),
    };
    let mut parts = prepared.parts.clone();
    parts.extend(vertex_parts(&prepared, coding.vertices, &residuals));
    assemble(mesh, coding, &parts)
}

/// The zstd level the container compresses chunks at, to compare candidates
/// as they will be stored.
const STORED_LEVEL: i32 = 3;

fn stored_size(parts: &[Vec<u8>]) -> usize {
    zstd::bulk::compress(&parts.concat(), STORED_LEVEL).map_or(usize::MAX, |s| s.len())
}

/// Encodes `mesh` as a mesh chunk payload of version 2, choosing the codings
/// that store it smallest.
///
/// Connectivity B is tried only when the triangle codec needs more than 2
/// bytes a triangle in the mesh's own order, which is when exporters wrote
/// the triangles scattered. The three vertex codings are then compared in the
/// winning order. Every choice is made on the sizes the container stores.
pub fn encode_mesh_v2(mesh: &Mesh) -> Result<Vec<u8>, EncodeMeshError> {
    let a = MeshCoding {
        connectivity: Connectivity::InOrder,
        vertices: VertexCoding::Meshopt,
    };
    check(mesh, a)?;
    let in_order = prepare(mesh, Connectivity::InOrder);
    let nt = mesh.triangles.len();
    let (prepared, connectivity) =
        if nt > 0 && in_order.parts[0].len() > 2 * nt && in_first_use_order(mesh) {
            // B's order takes long to find: A's size is measured meanwhile.
            let (reordered, in_order_size) = parallel::join(
                || prepare(mesh, Connectivity::Reordered),
                || stored_size(&in_order.parts),
            );
            if stored_size(&reordered.parts) < in_order_size {
                (reordered, Connectivity::Reordered)
            } else {
                (in_order, Connectivity::InOrder)
            }
        } else {
            (in_order, Connectivity::InOrder)
        };
    let try_coding = |vertices: VertexCoding, residuals: &[[u32; 3]]| {
        let coding = MeshCoding {
            connectivity,
            vertices,
        };
        check(mesh, coding).ok()?;
        let mut parts = prepared.parts.clone();
        parts.extend(vertex_parts(&prepared, vertices, residuals));
        Some((stored_size(&parts), coding, parts))
    };
    // The vertex codings are tried side by side: meshopt on its own, and the
    // two that code the prediction's residuals once they are computed.
    let (meshopt, predicted) = parallel::join(
        || try_coding(VertexCoding::Meshopt, &[]),
        || {
            let residuals = residuals(&prepared.vertices, &prepared.triangles);
            let (rans, planes) = parallel::join(
                || try_coding(VertexCoding::Rans, &residuals),
                || try_coding(VertexCoding::Planes, &residuals),
            );
            [rans, planes]
        },
    );
    // The smallest wins; on a tie, the first in the order meshopt, rANS,
    // planes.
    let (_, coding, parts) = [meshopt]
        .into_iter()
        .chain(predicted)
        .flatten()
        .reduce(|best, next| if next.0 < best.0 { next } else { best })
        .expect("the meshopt coding stores any mesh");
    assemble(mesh, coding, &parts)
}

/// Decodes a mesh chunk payload of version 2.
pub fn decode_mesh_v2(payload: &[u8]) -> Result<Mesh, DecodeMeshError> {
    decode(&mut In(payload)).ok_or(DecodeMeshError::Malformed)?
}

fn decode(input: &mut In<'_>) -> Option<Result<Mesh, DecodeMeshError>> {
    let meta = read_meta(input)?;
    let n = input.len()?;
    let nt = input.len()?;
    let connectivity = match input.u8()? {
        0 => Connectivity::InOrder,
        1 => Connectivity::Reordered,
        _ => return None,
    };
    let coding = match input.u8()? {
        0 => VertexCoding::Meshopt,
        1 => VertexCoding::Rans,
        2 => VertexCoding::Planes,
        _ => return None,
    };
    let index = input.bytes()?;
    if index.first() != Some(&INDEX_HEADER)
        || nt > index.len().checked_sub(INDEX_BUFFER_OVERHEAD)?
    {
        return None;
    }
    let permutation = match connectivity {
        Connectivity::InOrder => None,
        Connectivity::Reordered => Some(input.bytes()?),
    };
    let corners = input.bytes()?;
    // Bound the vertex count by the coding before reserving anything.
    let vertex_parts: Vec<&[u8]> = match coding {
        VertexCoding::Meshopt => {
            let buffer = input.bytes()?;
            if buffer.first() != Some(&VERTEX_HEADER)
                || n.checked_mul(BLOCK_HEADER_BYTES)?
                    >= buffer.len().checked_mul(VERTICES_PER_BLOCK)?
            {
                return None;
            }
            vec![buffer]
        }
        VertexCoding::Rans => {
            if n > nt.checked_mul(3)? {
                return None;
            }
            vec![input.bytes()?, input.bytes()?, input.bytes()?]
        }
        VertexCoding::Planes => {
            let planes = input.bytes()?;
            if Some(planes.len()) != n.checked_mul(12) {
                return None;
            }
            vec![planes]
        }
    };
    if !input.is_empty() {
        return None;
    }

    // The triangles, with the topology the prediction walks, and the vertex
    // parts do not depend on each other, so they are decoded side by side.
    // The triangles' faults still come first, as if decoded one after the
    // other.
    let predicted = coding != VertexCoding::Meshopt;
    let (connectivity_part, vertex_part) = parallel::join(
        || {
            let stored: Vec<u32> = meshopt::decode_index_buffer(index, nt.checked_mul(3)?).ok()?;
            let triangles = apply_corners(&stored, corners)?;
            if triangles.as_flattened().iter().any(|&v| v as usize >= n) {
                return Some(Err(DecodeMeshError::VertexOutOfRange));
            }
            let topo = predicted.then(|| topology(&triangles, n));
            Some(Ok((triangles, topo)))
        },
        || match coding {
            VertexCoding::Meshopt => meshopt::decode_vertex_buffer(vertex_parts[0], n)
                .ok()
                .map(VertexPart::Vertices),
            VertexCoding::Rans => decode_rans(vertex_parts[0], vertex_parts[1], vertex_parts[2], n)
                .map(VertexPart::Residuals),
            VertexCoding::Planes => decode_planes(vertex_parts[0], n).map(VertexPart::Residuals),
        },
    );
    let (triangles, topo) = match connectivity_part? {
        Ok(decoded) => decoded,
        Err(err) => return Some(Err(err)),
    };
    let vertices = match (vertex_part?, topo) {
        (VertexPart::Vertices(vertices), _) => vertices,
        (VertexPart::Residuals(residuals), Some(topo)) => rebuild(&residuals, &triangles, &topo),
        (VertexPart::Residuals(_), None) => unreachable!("predicted codings build the topology"),
    };
    let mesh = match permutation {
        None => Mesh {
            meta: None,
            vertices,
            triangles,
        },
        Some(bytes) => restore_order(bytes, vertices, &triangles)?,
    };
    Some(Ok(Mesh { meta, ..mesh }))
}

/// The vertex part of a mesh, decoded: the vertices themselves, or the
/// prediction's residuals.
enum VertexPart {
    Vertices(Vec<[f32; 3]>),
    Residuals(Vec<[u32; 3]>),
}

/// Connectivity B: puts the triangles back in their order, and numbers the
/// vertices by first use in it.
fn restore_order(bytes: &[u8], vertices: Vec<[f32; 3]>, triangles: &[[u32; 3]]) -> Option<Mesh> {
    let nt = triangles.len();
    let bits = permutation_bits(nt);
    let mut reader = BitReader::new(bytes);
    let mut original = vec![None; nt];
    for t in triangles {
        let p = reader.get(bits)? as usize;
        let slot = original.get_mut(p)?;
        if slot.is_some() {
            return None;
        }
        *slot = Some(*t);
    }
    if !reader.done() {
        return None;
    }
    let mut renumber = vec![NONE; vertices.len()];
    let mut ordered = Vec::with_capacity(vertices.len());
    let triangles = original
        .into_iter()
        .map(|t| {
            t.map(|t| {
                t.map(|v| {
                    let new = &mut renumber[v as usize];
                    if *new == NONE {
                        *new = ordered.len() as u32;
                        ordered.push(vertices[v as usize]);
                    }
                    *new
                })
            })
        })
        .collect::<Option<Vec<_>>>()?;
    (ordered.len() == vertices.len()).then_some(Mesh {
        meta: None,
        vertices: ordered,
        triangles,
    })
}