crcbl-render 0.17.0

Crucible renderer: the 3D frame on crcbl-frame — meshes, materials, lights, effects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
//! The instance array: one storage-buffer element per drawable object, written
//! by **delta upload** — the changed elements only, coalesced into runs.
//!
//! ```text
//!  set(handle, instance) ──▶ mirror[index] rewritten
//!                            └─▶ index marked dirty in every frame's range set
//!
//!  begin_frame(device) ──▶ rotate to the next frame's buffer
//!                          └─▶ one write_buffer per dirty run, then clear
//!                              └─▶ nothing dirty ⇒ nothing written at all
//! ```
//!
//! Topic 03 §3.2: "`GpuInstance` array (SSBO):
//! transform, mesh id, material id, flags. Written by delta upload each frame
//! (**changed instances only — dirty ranges, not full re-upload**)." The
//! emphasis is the whole reason this type exists rather than a `Vec` re-uploaded
//! every frame, which is what [`crate::sprite_pass`] does and what §3.2 is
//! written against.
//!
//! The record itself — and which of its fields are reserved rather than working
//! — is [`crcbl_shaders::mesh::GpuInstance`], because the layout is a contract
//! with `mesh.slang` and belongs in the crate that owns that source.
//!
//! # Dirty runs, and the case they exist for
//!
//! The pool holds a sorted list of half-open runs that never touch and
//! never overlap. Marking an index that **abuts** a run extends it; marking one
//! that closes the gap between two runs merges them; marking one that is already
//! inside a run does nothing. So writing instances 3, 4 and 5 produces one
//! upload of three elements and writing 3 and 900 produces two uploads of one —
//! whatever order the marks arrive in.
//!
//! Nothing merges across a gap, however small: two runs one element apart stay
//! two. A gap-tolerant merge trades bytes for calls, and choosing where that
//! trade sits needs a real scene to measure — §3.3's, which is also the first
//! thing that will move more than one instance per frame.
//!
//! # A ring, because the previous frame is still reading
//!
//! There is one buffer **per frame in flight**, for the reason
//! [`crate::forward`]'s uniform ring gives: an instance the CPU rewrites while
//! the GPU is still drawing last frame is a read-after-write hazard across
//! submissions, which is stage 2's headline risk for
//! that stage.
//!
//! The consequence is that a write is dirty in **every** slot's range set, not
//! one: each slot is a separate copy of the same logical array and each has to
//! be caught up before it is drawn from. So one changed instance costs one small
//! upload per frame for the next `frames_in_flight` frames, and then nothing.
//! That is still delta upload — the cost is the size of what changed, not the
//! size of the array — and [`InstancePool::begin_frame`] writes nothing at all
//! once every slot has caught up.
//!
//! # Where each instance was last frame
//!
//! [`GpuInstance::previous_transform`] is the pool's field, not the caller's,
//! the way the liveness bit is. [`InstancePool::set`] fills it from what the
//! record already holds, [`InstancePool::insert`] fills it from the new
//! transform — a spawn did not travel from anywhere — and
//! [`InstancePool::rotate`] puts back at rest whatever moved a frame ago and
//! has not moved again. [`InstancePool::set_bases`] is the one write that is
//! **not** a move: it rewrites a record's two base vertices and leaves this
//! field and the carry-forward enrolment exactly as they stand, because the
//! per-frame re-point a skinned object goes through is not a caller saying the
//! object travelled.
//!
//! That last step is what makes the field mean *last frame* rather than *the
//! last time anyone wrote this*. An instance that moves for a while and then
//! stands still is written on the frames it moves and not after; without the
//! carry-forward its record would go on naming the transform it left, and every
//! temporal consumer would go on smearing it along a journey that finished.
//!
//! It is the frame *before* the one being assembled that settles, because a
//! caller writes a frame's instances and then calls
//! [`InstancePool::begin_frame`] — so the moves of the frame about to be drawn
//! have already happened by the time the rotate runs, and settling those would
//! erase each one before it was ever drawn.
//!
//! The cost is one comparison per element per frame after the last write to it,
//! and one extra upload of that element on the first frame it stands still.
//! Nothing reads the field yet — topic 43 §9 is why
//! it is here before its readers are.
//!
//! # The pool never grows
//!
//! Capacity is fixed by [`InstancePoolDesc`] and [`InstancePool::insert`] fails
//! by name when it runs out. Same reason [`crate::mesh_pool`] gives: growing a
//! device buffer means allocating a larger one, copying, and re-pointing every
//! bind group that names the old one, in the middle of a frame.
//!
//! Unlike the geometry pools there is no fragmentation to report, because every
//! instance is the same size: a slot that comes back fits whatever asks for it
//! next. [`InstancePoolError::PoolFull`] therefore names one number where
//! [`crate::mesh_pool::MeshPoolError::PoolExhausted`] names three.
//!
//! # What a removed instance leaves behind, and the one bit that says so
//!
//! [`InstancePool::remove`] frees the slot and retires the handle, and it
//! rewrites **one field** of the element: it clears
//! [`GpuInstance::LIVE`] from the record's flags and marks
//! the slot dirty, so the next [`InstancePool::begin_frame`] carries the
//! removal to the GPU. Everything else the removed instance held — its
//! transform, its mesh id — stays exactly as it was.
//!
//! That is the whole contract, and it is what §3.3's cull pass needs: the pass
//! walks the array from element zero, so a freed slot has to be able to say it
//! is one. `cull.slang` asks the bit before it reads anything else in the
//! record, and [`crate::cull::visible_instances`] — the CPU oracle — does the
//! same.
//!
//! **The bit is the pool's, not the caller's.** [`InstancePool::insert`] and
//! [`InstancePool::set`] set it whatever the caller passed, so an instance
//! cannot arrive dead by omission, and a caller cannot clear it without going
//! through `remove`. The other bits of the word are the caller's and are left
//! alone.
//!
//! ## Why the record is not zeroed
//!
//! [`crate::mesh_pool::MeshPool::free`] clears its table entry to all zeroes,
//! and the analogous move here would be to zero the whole instance. It is the
//! wrong one. What the mesh table is doing is *writing the empty value of the
//! field that answers the question* — `index_count` — and the fields it clears
//! with it are ones a stale entry would otherwise be misread through; the
//! parallel here is clearing the liveness bit, which is that same write.
//!
//! Zeroing the rest would make it worse rather than tidier. A zeroed transform
//! is a degenerate box at the origin and a zeroed mesh id names table entry 0,
//! which is an ordinary resident mesh — so a consumer that skipped the liveness
//! check would see a removed instance as *visible whenever the origin is*,
//! where leaving the record alone at least leaves it wherever it was. The
//! failure a shortcut produces should not be the one that looks plausible.

use core::ops::Range;

use crcbl_core::{Handle, Pool};
use crcbl_hal::{BufferDesc, BufferHandle, BufferUsage, Device, HalError, MemoryLocation};
use crcbl_shaders::mesh::{GpuInstance, INSTANCE_STRIDE};

/// Marker type for [`InstanceHandle`]. Uninhabited.
#[derive(Debug)]
pub enum Instance {}

/// An instance in an [`InstancePool`]. Generational, so a removed instance's
/// handle stops resolving rather than naming whatever took its slot.
pub type InstanceHandle = Handle<Instance>;

/// What an [`InstancePool`] refuses to do.
#[derive(Debug, thiserror::Error)]
pub enum InstancePoolError {
    /// Every slot is in use.
    ///
    /// `in_use` is normally the number of live instances. It can exceed that
    /// only through generation exhaustion — see [`Pool`]'s docs on retired
    /// slots, which needs about 4.3 billion removals of one slot — and the two
    /// are reported together so that case is legible rather than baffling.
    #[error("the instance pool holds {capacity} instances and {in_use} of them are in use")]
    PoolFull {
        /// The pool's fixed capacity.
        capacity: u32,
        /// Slots that cannot be handed out.
        in_use: u32,
    },
    /// A box handed over for an instance to be culled by has a non-finite
    /// corner, or a `min` past its `max` on some axis.
    ///
    /// Refused rather than written, because the culls would test it as given:
    /// an inverted box reaches no frustum and a `NaN` lane fails every plane
    /// test, so either would drop the object from every view without a word.
    /// See [`SkinnedInstanceDesc::bounds`](crate::SkinnedInstanceDesc::bounds).
    #[error(
        "an instance box must be finite with min <= max on every axis, and this one has min \
         {min:?} and max {max:?}"
    )]
    InvalidBounds {
        /// The box's lowest corner, as given.
        min: [f32; 3],
        /// Its highest corner, as given.
        max: [f32; 3],
    },
    /// Anything the seam refused.
    #[error(transparent)]
    Hal(#[from] HalError),
}

/// Flattens a pool error into the seam's, for callers whose constructors are
/// already `Result<_, HalError>`.
///
/// Same shape and same reason as [`crate::mesh_pool::MeshPoolError`]'s: the seam
/// has no vocabulary for "this pool is full", so everything that is not already
/// a [`HalError`] arrives as [`Backend`](HalError::Backend), which renders
/// verbatim and keeps the numbers.
impl From<InstancePoolError> for HalError {
    fn from(error: InstancePoolError) -> Self {
        match error {
            InstancePoolError::Hal(hal) => hal,
            other => Self::Backend(other.to_string()),
        }
    }
}

/// How large an [`InstancePool`] is, and what to call it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InstancePoolDesc<'a> {
    /// Debug name; each frame's buffer is named after it.
    pub label: Option<&'a str>,
    /// Instances the pool can hold. Fixed — see the [module docs](self).
    pub capacity: u32,
    /// How many buffers to keep, one per frame in flight. Must be at least one.
    pub frames_in_flight: usize,
}

/// One `GpuInstance` array, mirrored on the host and uploaded by delta.
///
/// See the [module docs](self) for the coalescing rule, the ring, and what a
/// removal leaves behind — all three are decisions rather than accidents.
#[derive(Debug)]
pub struct InstancePool {
    /// One per frame in flight; `frame` says which is current.
    buffers: Vec<BufferHandle>,
    /// What has changed and not yet reached each buffer. Same length as
    /// `buffers`, and indexed the same way.
    dirty: Vec<DirtyRanges>,
    /// The slot `begin_frame` last rotated to.
    frame: usize,

    /// The array exactly as a buffer holds it, so a dirty run is one contiguous
    /// slice and there is no second copy of an instance to drift from this one.
    mirror: Vec<u8>,
    /// Generational slot allocation, and nothing else: the values live in
    /// `mirror`. Reusing the crate's pool rather than writing a second
    /// allocator that gets the generation-retirement rule subtly different.
    slots: Pool<()>,
    /// Elements a caller has written since the last [`InstancePool::rotate`],
    /// which is what the frame being assembled now moves.
    written_this_frame: Vec<u32>,
    /// `written_this_frame` as it stood at the last [`InstancePool::rotate`]:
    /// what the frame already drawn moved, and so what the next rotate has to
    /// put back at rest. See the [module docs](self) on the previous transform.
    written_last_frame: Vec<u32>,
    /// Whether each element is already in `written_this_frame`, so a caller
    /// that writes one instance repeatedly in a frame enrols it once. Indexed
    /// by element, `capacity` long.
    moved_mark: Vec<bool>,
    capacity: u32,
    /// One past the highest element [`InstancePool::insert`] has ever handed
    /// out — what [`InstancePool::slot_count`] reports.
    high_water: u32,
    /// How many times the mirror has been written — [`InstancePool::revision`].
    revision: u64,
    /// Elements written since the last [`InstancePool::take_written`], each
    /// once, in the order they were first written.
    written_since_taken: Vec<u32>,
    /// Whether each element is already in `written_since_taken`. Indexed by
    /// element, `capacity` long.
    written_since_taken_mark: Vec<bool>,
}

impl InstancePool {
    /// Creates the ring, with every buffer **cleared**.
    ///
    /// Cleared rather than left as the driver handed it over, and for
    /// [`crate::mesh_pool::MeshPool`]'s reason rather than for tidiness: a slot
    /// nothing has written is read by anything that walks the array, and "a
    /// zeroed element is a dead instance" is only a contract if it holds for
    /// the slots no one has touched as well as for the ones
    /// [`InstancePool::remove`] clears. A driver's leftovers are not zeroes,
    /// and a leftover with [`GpuInstance::LIVE`] set is an instance the cull
    /// pass keeps, at a transform nobody chose.
    ///
    /// This is why it was not always so: until the flags word had a defined
    /// bit, zeroing bought nothing an element written before it is addressed
    /// did not already have.
    ///
    /// The device buffers are created **before** the host mirror, so a capacity
    /// no device could hold arrives as a [`HalError`] naming the buffer rather
    /// than as an aborting host allocation of the same size.
    ///
    /// # Errors
    ///
    /// [`InstancePoolError::Hal`] from any seam call. A failure part-way through
    /// releases whatever was already created.
    ///
    /// # Panics
    ///
    /// If `frames_in_flight` is zero: a ring with no buffers has nothing to
    /// bind, and every later index into it would be the real failure.
    pub fn new(
        device: &dyn Device,
        desc: &InstancePoolDesc<'_>,
    ) -> Result<Self, InstancePoolError> {
        assert!(
            desc.frames_in_flight > 0,
            "an instance pool needs at least one buffer to bind"
        );
        let stem = desc.label.unwrap_or("instance pool");
        let size = u64::from(desc.capacity) * INSTANCE_STRIDE as u64;
        let cleared = vec![0u8; desc.capacity as usize * INSTANCE_STRIDE];
        let mut buffers = Vec::with_capacity(desc.frames_in_flight);
        for frame in 0..desc.frames_in_flight {
            let created = device
                .create_buffer(&BufferDesc {
                    label: Some(&format!("{stem} instances {frame}")),
                    size,
                    // Host-visible, so a delta is a `write_buffer` rather than a
                    // staging copy needing a barrier inside a frame.
                    // `crate::graph` is the only thing allowed to record a
                    // barrier during a frame, and a per-frame transfer submit is
                    // what §3.1's start-up upload path deliberately is not.
                    usage: BufferUsage::STORAGE,
                    memory: MemoryLocation::HostUpload,
                })
                // A buffer created and not cleared is one the failure path below
                // has to release, so the clear happens here rather than in a
                // second loop over the finished ring.
                .and_then(|buffer| match device.write_buffer(buffer, 0, &cleared) {
                    Ok(()) => Ok(buffer),
                    Err(error) => {
                        device.destroy_buffer(buffer);
                        Err(error)
                    }
                });
            match created {
                Ok(buffer) => buffers.push(buffer),
                Err(error) => {
                    for buffer in buffers {
                        device.destroy_buffer(buffer);
                    }
                    return Err(error.into());
                }
            }
        }
        Ok(Self {
            buffers,
            dirty: vec![DirtyRanges::default(); desc.frames_in_flight],
            frame: 0,
            mirror: vec![0; desc.capacity as usize * INSTANCE_STRIDE],
            slots: Pool::new(),
            written_this_frame: Vec::new(),
            written_last_frame: Vec::new(),
            moved_mark: vec![false; desc.capacity as usize],
            capacity: desc.capacity,
            high_water: 0,
            revision: 0,
            written_since_taken: Vec::new(),
            written_since_taken_mark: vec![false; desc.capacity as usize],
        })
    }

    /// The buffers, one per frame in flight, in slot order.
    ///
    /// A caller builds one bind group per slot naming the buffer at that slot,
    /// and draws frame `n` with the group whose slot [`InstancePool::begin_frame`]
    /// returned.
    #[must_use]
    pub fn buffers(&self) -> &[BufferHandle] {
        &self.buffers
    }

    /// How many instances the pool can hold.
    #[must_use]
    pub const fn capacity(&self) -> u32 {
        self.capacity
    }

    /// How many instances are live.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.slots.len()
    }

    /// Whether no instance is live.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.slots.is_empty()
    }

    /// Array elements anything walking the pool has to cover: one past the
    /// highest slot ever handed out.
    ///
    /// **Not [`InstancePool::len`]**, which counts live instances and so shrinks
    /// when one is removed while the slots above it stay occupied. A cull
    /// dispatch sized by `len` would silently stop testing the instances past
    /// the gap, which reads as "those were culled".
    ///
    /// It never shrinks, because a slot handed out once may be handed out again
    /// and nothing compacts the array. Every element below it is either a live
    /// instance or a dead record — [`InstancePool::remove`] leaves one, and
    /// [`InstancePool::new`] clears the buffers so an untouched slot is one too
    /// — which is what makes walking the whole range safe.
    /// How many times any element of the array has been written, over the
    /// pool's whole life.
    ///
    /// **A number to compare with an older reading of itself, not to interpret**
    /// — two readings that differ say the instances a pass draws are not the
    /// ones it drew last time it looked, and two that agree say they are. What
    /// wants that is a consumer whose output is a function of the array and can
    /// be kept instead of recomputed. `ForwardRenderer`'s shadow atlas was the
    /// one there was, and it now asks [`take_written`](Self::take_written)
    /// instead, which says which elements moved rather than that one did.
    ///
    /// It moves on **every** write, including ones no draw could see: a rewrite
    /// with identical bytes (which [`set`](Self::set) deliberately does not
    /// filter), a re-point of a skinned object's bases, and the settling of a
    /// `previous_transform` the frame after a move stopped. Reading it as "the
    /// scene changed" is therefore conservative in the safe direction — it says
    /// *changed* more often than a byte comparison would, and never says
    /// *unchanged* when a byte moved.
    ///
    /// It counts writes rather than hashing the array because the array is the
    /// pool's own capacity in bytes and this is read once a frame.
    #[must_use]
    pub const fn revision(&self) -> u64 {
        self.revision
    }

    /// Appends every element written since the last call to `into`, each once
    /// and in the order it was first written, and forgets them here.
    ///
    /// [`revision`](Self::revision) says *that* the array changed; this says
    /// **which elements** did, on the same terms — every write counts,
    /// including a rewrite with identical bytes, a re-point of a skinned
    /// object's bases and the settling of a `previous_transform`, so an element
    /// is left out only when nothing wrote it. What wants that is a consumer
    /// that can tell which of its outputs an element could reach:
    /// `ForwardRenderer`'s shadow atlas tests each written element's bounds,
    /// before and after, against each shadow group's cull, so one moving
    /// instance redraws the maps it can be seen in rather than every map.
    ///
    /// The log is bounded by [`capacity`](Self::capacity), because an element
    /// already in it is not added again — a pool nobody drains costs at most
    /// one entry per element.
    pub fn take_written(&mut self, into: &mut Vec<u32>) {
        for index in self.written_since_taken.drain(..) {
            self.written_since_taken_mark[index as usize] = false;
            into.push(index);
        }
    }

    /// What element `index` holds, live or not, decoded from the host mirror,
    /// or [`None`] at or past [`slot_count`](Self::slot_count).
    ///
    /// By element rather than by handle, for a caller that learned the index
    /// from [`take_written`](Self::take_written): a removed instance's element
    /// still holds its last transform and mesh with
    /// [`GpuInstance::LIVE`] cleared, which is what says it draws nothing now.
    #[must_use]
    pub fn record(&self, index: u32) -> Option<GpuInstance> {
        (index < self.high_water).then(|| self.read(index))
    }

    #[must_use]
    pub const fn slot_count(&self) -> u32 {
        self.high_water
    }

    /// Takes a slot, writes `instance` into it **live**, and marks it dirty
    /// everywhere.
    ///
    /// [`GpuInstance::LIVE`] is set whatever `instance.flags` carried — see the
    /// [module docs](self) on why the bit is the pool's rather than the
    /// caller's.
    ///
    /// # Errors
    ///
    /// [`InstancePoolError::PoolFull`] when every slot is in use. The pool never
    /// grows — see the [module docs](self).
    pub fn insert(&mut self, instance: &GpuInstance) -> Result<InstanceHandle, InstancePoolError> {
        // `Pool` hands out a fresh index only when it has no vacant slot, so
        // "live plus permanently retired" is exactly the highest index it could
        // be about to create. Checking before inserting rather than after keeps
        // a refused insert from burning a generation.
        let in_use = self.in_use();
        if in_use >= self.capacity {
            return Err(InstancePoolError::PoolFull {
                capacity: self.capacity,
                in_use,
            });
        }
        let handle: InstanceHandle = self.slots.insert(()).cast();
        self.high_water = self.high_water.max(handle.index() + 1);
        // **A spawn did not travel from anywhere**, so the record is created
        // already at rest: whatever the slot held before belonged to another
        // instance, and carrying it forward would give the new one a motion
        // vector across the gap between them.
        self.write_live(handle.index(), instance, instance.transform);
        Ok(handle)
    }

    /// Rewrites `handle`'s instance and marks it dirty everywhere.
    ///
    /// Returns whether the handle named a live instance; a stale one writes
    /// nothing rather than overwriting whatever took its slot.
    ///
    /// [`GpuInstance::LIVE`] is set, exactly as [`InstancePool::insert`] sets
    /// it: a live instance cannot be made dead by rewriting it, only by
    /// [`InstancePool::remove`].
    ///
    /// A rewrite with the same bytes still marks the slot dirty. Filtering that
    /// would trade a comparison per write for an upload that mostly does not
    /// happen, and it would make "what got uploaded" depend on the old value —
    /// which is exactly the kind of thing a caller cannot reason about. Callers
    /// that know a value did not change do not call this.
    pub fn set(&mut self, handle: InstanceHandle, instance: &GpuInstance) -> bool {
        if !self.slots.contains(handle.cast()) {
            return false;
        }
        // Where it was is what the record already holds, which is why the
        // caller does not pass it — see [`GpuInstance::previous_transform`].
        let previous = self.read(handle.index()).transform;
        self.write_live(handle.index(), instance, previous);
        true
    }

    /// Rewrites `handle`'s two base vertices and **nothing else** — the
    /// previous transform the record already holds included.
    ///
    /// Returns whether the handle named a live instance; a stale one writes
    /// nothing, on [`InstancePool::set`]'s terms. The record keeps
    /// [`GpuInstance::LIVE`], because every path that puts one in the mirror is
    /// a path that set the bit.
    ///
    /// **It exists because a re-point is not a move.**
    /// [`ForwardRenderer::point_skinned_instances`](crate::forward::ForwardRenderer)
    /// rewrites every skinned object's pair of bases once a frame, between the
    /// [`rotate`](Self::rotate) and the upload, and doing that through
    /// [`set`](Self::set) made it a *transform* write: `set` takes the previous
    /// transform from the record it overwrites, and by then that record already
    /// carries this frame's transform, so `previous_transform` came out equal to
    /// `transform` for every skinned instance on every frame. A character that
    /// walked contributed nothing to the motion target from the walk.
    ///
    /// It also leaves the element out of the next [`rotate`](Self::rotate)'s
    /// carry-forward, which is the same reason from the other side: a call that
    /// runs for every skinned object every frame would keep each of them
    /// permanently written-this-frame, nothing would ever be put back at rest,
    /// and one move would be reported for ever. What enrols an element is a
    /// caller's own write — [`insert`](Self::insert) and [`set`](Self::set).
    pub fn set_bases(
        &mut self,
        handle: InstanceHandle,
        base_vertex: u32,
        previous_base_vertex: u32,
    ) -> bool {
        if !self.slots.contains(handle.cast()) {
            return false;
        }
        let index = handle.index();
        let mut record = self.read(index);
        record.base_vertex = base_vertex;
        record.previous_base_vertex = previous_base_vertex;
        self.write(index, &record);
        true
    }

    /// Rewrites `handle`'s flags word and **nothing else**, on
    /// [`set_bases`](Self::set_bases)' terms: a flag is not a move, so the
    /// transform history is left as it is and the element is not enrolled in the
    /// next [`rotate`](Self::rotate)'s carry-forward.
    ///
    /// [`GpuInstance::LIVE`] is kept set whatever `flags` says, because the pool
    /// owns that bit. Returns whether the handle named a live instance; a stale
    /// one writes nothing.
    pub fn set_flags(&mut self, handle: InstanceHandle, flags: u32) -> bool {
        if !self.slots.contains(handle.cast()) {
            return false;
        }
        let index = handle.index();
        let mut record = self.read(index);
        record.flags = flags | GpuInstance::LIVE;
        self.write(index, &record);
        true
    }

    /// Rewrites `handle`'s own box and the
    /// [`GpuInstance::OWNS_BOUNDS`] bit that says the culls read it, and
    /// **nothing else**, on [`set_bases`](Self::set_bases)' terms: a box is not
    /// a move, so the transform history is left as it is and the element is
    /// not enrolled in the next [`rotate`](Self::rotate)'s carry-forward.
    ///
    /// `owns` is the bit or zero; any other bit in it is ignored. Returns
    /// whether the handle named a live instance; a stale one writes nothing.
    pub fn set_bounds(
        &mut self,
        handle: InstanceHandle,
        owns: u32,
        bounds_min: [f32; 3],
        bounds_max: [f32; 3],
    ) -> bool {
        if !self.slots.contains(handle.cast()) {
            return false;
        }
        let index = handle.index();
        let mut record = self.read(index);
        record.flags =
            (record.flags & !GpuInstance::OWNS_BOUNDS) | (owns & GpuInstance::OWNS_BOUNDS);
        record.bounds_min = bounds_min;
        record.bounds_max = bounds_max;
        self.write(index, &record);
        true
    }

    /// Clears `bits` from the flags of every live instance that carries any of
    /// them, and writes only those.
    ///
    /// A walk over every slot ever handed out, for a change that is rare by
    /// nature — a view being released, whose bit every record has to stop
    /// carrying. [`GpuInstance::LIVE`] is never cleared here, whatever `bits`
    /// holds.
    pub fn clear_flags(&mut self, bits: u32) {
        let bits = bits & !GpuInstance::LIVE;
        for index in 0..self.high_water {
            let mut record = self.read(index);
            if record.flags & GpuInstance::LIVE == 0 || record.flags & bits == 0 {
                continue;
            }
            record.flags &= !bits;
            self.write(index, &record);
        }
    }

    /// What `handle`'s slot currently holds, decoded from the host mirror, or
    /// `None` if the handle is stale.
    ///
    /// The record carries [`GpuInstance::LIVE`], because the pool put it there:
    /// this is what the buffer holds, not what the caller passed.
    #[must_use]
    pub fn get(&self, handle: InstanceHandle) -> Option<GpuInstance> {
        self.slots
            .contains(handle.cast())
            .then(|| self.read(handle.index()))
    }

    /// Every live element of the array, in slot order.
    ///
    /// **The records the device would draw**, decoded from the same mirror the
    /// buffers are filled from — including
    /// [`GpuInstance::LIVE`](crcbl_shaders::mesh::GpuInstance::LIVE), which the
    /// pool sets, so a caller filters on the bit rather than on a second list
    /// it kept. Slots nothing has written are cleared, and a cleared record has
    /// the bit off.
    ///
    /// Added for `crate::probe_visibility`, which needs the scene as it stands
    /// rather than as it was described: a probe's visibility map is about where
    /// the walls *are*, and the walls arrive through
    /// [`InstancePool::insert`] rather than through the description.
    #[must_use]
    pub fn live(&self) -> Vec<GpuInstance> {
        (0..self.high_water)
            .map(|index| self.read(index))
            .filter(|record| record.flags & GpuInstance::LIVE != 0)
            .collect()
    }

    /// Every element the pool has handed out, in slot order, dead ones included
    /// — the array [`slot_count`](Self::slot_count) elements long that a cull
    /// dispatch walks, decoded from the host mirror.
    ///
    /// For `crate::cull`'s oracle, which indexes it the way the shader does:
    /// [`live`](Self::live) drops the dead slots and with them the indices.
    #[must_use]
    pub fn records(&self) -> Vec<GpuInstance> {
        (0..self.high_water).map(|index| self.read(index)).collect()
    }

    /// Which element of the array `handle` is, or `None` if the handle is stale.
    ///
    /// This is the number a shader indexes with, which is why it is public: a
    /// draw has to be able to say which instance it draws.
    #[must_use]
    pub fn index(&self, handle: InstanceHandle) -> Option<u32> {
        self.slots.contains(handle.cast()).then(|| handle.index())
    }

    /// Frees `handle`'s slot for reuse, retires the handle, and clears the
    /// element's [`GpuInstance::LIVE`] bit.
    ///
    /// Returns whether the handle named a live instance. The rest of the
    /// element's bytes are left as they are — see the [module docs](self) on
    /// why clearing the bit is the whole of what a removal owes the GPU, and why
    /// zeroing the record would be worse than leaving it.
    ///
    /// The clear reaches the device the way every other write does, through
    /// [`InstancePool::begin_frame`]: the slot is marked dirty in every buffer's
    /// range set, so a removal costs the same one small upload per frame in
    /// flight that a rewrite does.
    pub fn remove(&mut self, handle: InstanceHandle) -> bool {
        if self.slots.remove(handle.cast()).is_none() {
            return false;
        }
        let index = handle.index();
        let mut removed = self.read(index);
        removed.flags &= !GpuInstance::LIVE;
        self.write(index, &removed);
        true
    }

    /// Rotates to the next frame's buffer and writes that buffer's outstanding
    /// changes into it, returning the slot it rotated to.
    ///
    /// **A frame in which nothing was written performs no seam call at all.**
    /// That is §3.2's claim about this design, and it is the thing to check when
    /// wondering whether delta upload is really happening.
    ///
    /// # Errors
    ///
    /// [`HalError`] if a write failed. The runs it had already written stay
    /// written and the rest stay dirty, so a later frame retries them — a
    /// half-uploaded array that forgot the rest would be the worse failure.
    pub fn begin_frame(&mut self, device: &dyn Device) -> Result<usize, HalError> {
        let slot = self.rotate();
        self.flush(device)?;
        Ok(slot)
    }

    /// Rotates to the next frame's buffer **without uploading anything**, and
    /// returns the slot it rotated to.
    ///
    /// The half of [`begin_frame`](Self::begin_frame) that decides which buffer
    /// this frame writes, split out for the one caller that has an instance to
    /// write which *depends* on the slot:
    /// [`ForwardRenderer::begin_skinned_frame`](crate::forward::ForwardRenderer::begin_skinned_frame)
    /// points every skinned instance at the half of its region this frame's
    /// dispatch will fill, and the parity it reads comes from a
    /// [`Skinning`](crate::skinning::Skinning) that has to be handed this slot
    /// first. Rotating and uploading in one call would leave that write for the
    /// *next* frame's upload, which is a character posed one frame late.
    ///
    /// [`flush`](Self::flush) must follow it, or this frame draws whatever the
    /// frame before last left in this buffer.
    ///
    /// **This is also the frame boundary the previous transform is measured
    /// against**: an element the frame before last wrote and this one did not
    /// is put back at rest here, so an instance that stopped moving stops
    /// reporting a move. See [`GpuInstance::previous_transform`] and the
    /// [module docs](self).
    pub fn rotate(&mut self) -> usize {
        self.carry_forward();
        self.frame = (self.frame + 1) % self.buffers.len();
        self.frame
    }

    /// Writes the current slot's outstanding changes into its buffer.
    ///
    /// The other half of [`begin_frame`](Self::begin_frame). Idempotent: a
    /// second call with nothing newly written performs no seam call at all.
    ///
    /// # Errors
    ///
    /// [`HalError`] if a write failed, on [`begin_frame`](Self::begin_frame)'s
    /// terms — the runs already written stay written and the rest stay dirty.
    pub fn flush(&mut self, device: &dyn Device) -> Result<(), HalError> {
        let slot = self.frame;
        let buffer = self.buffers[slot];
        // Detached so the mirror can be borrowed while the runs are walked.
        let mut runs = core::mem::take(&mut self.dirty[slot].runs);
        for (done, run) in runs.iter().enumerate() {
            let at = run.start as usize * INSTANCE_STRIDE;
            let end = run.end as usize * INSTANCE_STRIDE;
            if let Err(error) = device.write_buffer(buffer, at as u64, &self.mirror[at..end]) {
                runs.drain(..done);
                self.dirty[slot].runs = runs;
                return Err(error);
            }
        }
        runs.clear();
        self.dirty[slot].runs = runs;
        Ok(())
    }

    #[cfg(all(test, not(target_arch = "wasm32")))]
    pub(super) fn replace_test_buffer(
        &mut self,
        slot: usize,
        replacement: BufferHandle,
    ) -> BufferHandle {
        core::mem::replace(&mut self.buffers[slot], replacement)
    }

    /// The elements [`take_written`](Self::take_written) would hand out now,
    /// without taking them: a test that checks a write is pending must leave it
    /// for the frame that reads it.
    #[cfg(all(test, not(target_arch = "wasm32")))]
    pub(super) fn pending_written(&self) -> &[u32] {
        &self.written_since_taken
    }

    /// Releases every buffer. The device must be idle.
    pub fn destroy(self, device: &dyn Device) {
        for buffer in self.buffers {
            device.destroy_buffer(buffer);
        }
    }

    /// Slots that cannot be handed out: live ones, plus any permanently
    /// withdrawn by generation exhaustion.
    fn in_use(&self) -> u32 {
        let used = self.slots.len() + self.slots.retired_slots();
        u32::try_from(used).unwrap_or(u32::MAX)
    }

    /// What the mirror holds at `index`, live or not.
    ///
    /// Decoded rather than kept beside the bytes, for the reason the mirror is
    /// bytes at all: a second copy of an instance is a second thing to drift
    /// from the one the buffer gets.
    fn read(&self, index: u32) -> GpuInstance {
        let at = index as usize * INSTANCE_STRIDE;
        let bytes: &[u8; INSTANCE_STRIDE] = self.mirror[at..at + INSTANCE_STRIDE]
            .try_into()
            .unwrap_or_else(|_| unreachable!("the mirror is capacity × INSTANCE_STRIDE"));
        GpuInstance::from_bytes(bytes)
    }

    /// [`InstancePool::write`] with [`GpuInstance::LIVE`] forced on and
    /// `previous_transform` set to `previous`, which is what every
    /// caller-supplied record goes through.
    ///
    /// Both fields are the pool's rather than the caller's, for the same
    /// reason: the pool is what knows the answer — that the record is live, and
    /// what it held a moment ago — and a caller asked for either would be
    /// keeping a second copy of what this one already has.
    ///
    /// It also enrols the element in the carry-forward the next
    /// [`InstancePool::rotate`] performs, which is what stops a moving instance
    /// that stopped from reporting the move it made forever.
    fn write_live(&mut self, index: u32, instance: &GpuInstance, previous: [f32; 16]) {
        self.write(
            index,
            &GpuInstance {
                flags: instance.flags | GpuInstance::LIVE,
                previous_transform: previous,
                ..*instance
            },
        );
        if !self.moved_mark[index as usize] {
            self.moved_mark[index as usize] = true;
            self.written_this_frame.push(index);
        }
    }

    /// Puts every element a caller wrote last frame at rest: its
    /// `previous_transform` becomes its `transform`, so a frame in which it is
    /// not written again reports no motion.
    ///
    /// **This is the half that makes the field mean "last frame" rather than
    /// "the last time anyone touched it".** Without it, an instance that moved
    /// once and then stood still would go on carrying the transform it moved
    /// from, and every temporal consumer would go on smearing it along a
    /// journey that finished.
    ///
    /// **It carries forward the frame *before* the one being assembled**, and
    /// that is the whole subtlety. A caller writes the instances for frame `n`
    /// and *then* calls [`InstancePool::begin_frame`], so by the time this runs
    /// the moves of frame `n` have already happened and each already carries
    /// where it came from — putting those at rest would erase the move before
    /// it was ever drawn. What is left to settle is frame `n - 1`'s writes that
    /// frame `n` did not repeat, which is exactly "it moved, and then it
    /// stopped".
    ///
    /// A record already at rest is skipped, so the cost of standing still is
    /// one comparison in the frame after the last move and nothing after that.
    /// The write goes through [`InstancePool::write`] rather than
    /// [`InstancePool::write_live`]: it must not enrol the element again, or a
    /// pool that had ever moved anything would re-examine it every frame
    /// forever.
    fn carry_forward(&mut self) {
        for at in 0..self.written_last_frame.len() {
            let index = self.written_last_frame[at];
            if self.moved_mark[index as usize] {
                // Written again for the frame about to be drawn, so it is
                // moving rather than stopping and its own write said where
                // from.
                continue;
            }
            let mut record = self.read(index);
            if record.previous_transform == record.transform {
                continue;
            }
            record.previous_transform = record.transform;
            self.write(index, &record);
        }
        self.written_last_frame.clear();
        core::mem::swap(&mut self.written_last_frame, &mut self.written_this_frame);
        for index in &self.written_last_frame {
            self.moved_mark[*index as usize] = false;
        }
    }

    /// Puts `instance` into the mirror at `index` and marks it dirty in every
    /// slot's range set.
    ///
    /// **The only thing that ever changes the mirror**, which is what makes
    /// [`revision`](Self::revision) and [`take_written`](Self::take_written)
    /// total answers rather than counts of the paths somebody remembered to
    /// instrument.
    fn write(&mut self, index: u32, instance: &GpuInstance) {
        let at = index as usize * INSTANCE_STRIDE;
        self.mirror[at..at + INSTANCE_STRIDE].copy_from_slice(&instance.to_bytes());
        self.revision = self.revision.wrapping_add(1);
        if !self.written_since_taken_mark[index as usize] {
            self.written_since_taken_mark[index as usize] = true;
            self.written_since_taken.push(index);
        }
        for dirty in &mut self.dirty {
            dirty.mark(index);
        }
    }
}

/// A sorted set of half-open element runs that never overlap and never touch.
///
/// "Never touch" is the load-bearing half: it is what makes marking 3, 4 and 5
/// one run rather than three, and it is maintained by [`DirtyRanges::mark`]
/// extending and merging rather than appending. A list that only appended would
/// produce one `write_buffer` per changed element, which is the cost §3.2's
/// delta upload exists to avoid — it would still be correct, and it would still
/// pass any test that only checked the bytes.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct DirtyRanges {
    runs: Vec<Range<u32>>,
}

impl DirtyRanges {
    /// Marks one element dirty, extending or merging the runs around it.
    ///
    /// Idempotent: marking an element already inside a run changes nothing, so
    /// a value written twice between uploads is uploaded once.
    ///
    /// `index` must be below `u32::MAX`, which an [`InstancePool`] index is:
    /// it is below a capacity that is itself a `u32`.
    fn mark(&mut self, index: u32) {
        // The first run that could contain or abut `index` from the left.
        // Everything before it ends strictly below `index`, so nothing there
        // can touch what this leaves behind.
        let at = self.runs.partition_point(|run| run.end < index);
        // Inside the run, or exactly one past its end.
        if self.runs.get(at).is_some_and(|run| run.start <= index) {
            if self.runs[at].end == index {
                self.runs[at].end = index + 1;
                // Extending may have closed the gap to the next run, which is
                // the case that turns "3 and 5, then 4" into one run.
                if self
                    .runs
                    .get(at + 1)
                    .is_some_and(|next| next.start == index + 1)
                {
                    let end = self.runs.remove(at + 1).end;
                    self.runs[at].end = end;
                }
            }
            return;
        }
        // Exactly one before the run. It cannot now touch the run before it:
        // that one ends strictly below `index`.
        if self.runs.get(at).is_some_and(|run| run.start == index + 1) {
            self.runs[at].start = index;
            return;
        }
        // A gap on both sides, or past the last run.
        self.runs.insert(at, index..index + 1);
    }
}

// `Instance::create_device` is native-only: see the `crcbl_hal::device` module docs.
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;
    use crcbl_hal::null::{Event, NullInstance, Recorder};
    use crcbl_hal::{DeviceDesc, Instance};

    const FRAMES: usize = 2;

    fn open() -> (Recorder, Box<dyn Device>) {
        let recorder = Recorder::new();
        let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
        let adapter = instance.adapters().remove(0);
        let device = instance
            .create_device(&DeviceDesc::for_adapter(adapter.id))
            .expect("the null backend always opens");
        (recorder, device)
    }

    fn pool(device: &dyn Device, capacity: u32) -> InstancePool {
        InstancePool::new(
            device,
            &InstancePoolDesc {
                label: Some("test"),
                capacity,
                frames_in_flight: FRAMES,
            },
        )
        .expect("the null backend accepts every descriptor")
    }

    /// An instance whose transform is distinguishable from every other `n`, as
    /// a **caller** hands it over.
    ///
    /// Its flags do not carry [`GpuInstance::LIVE`] for the `n` the tests below
    /// use, which is what makes [`stored`] a different value and the pool's
    /// forcing of the bit observable.
    fn instance(n: u32) -> GpuInstance {
        GpuInstance {
            transform: [n as f32; 16],
            // The pool overwrites this, and a value here that the pool would
            // also have chosen could not tell that apart from it being passed
            // through. Every `n` the tests use is non-zero, so this is a
            // transform no caller record carries.
            previous_transform: [0.0; 16],
            mesh: n,
            material: n + 1,
            sector: n + 2,
            flags: n + 3,
            base_vertex: n + 4,
            previous_base_vertex: n + 5,
            bounds_min: [n as f32 + 6.0; 3],
            bounds_max: [n as f32 + 7.0; 3],
        }
    }

    /// [`instance`] as the pool stores it when it is **inserted**: the
    /// caller's record, live, and at rest.
    ///
    /// Refuses an `n` whose flags happen to carry the bit already, because a
    /// comparison against such a record would pass whether or not the pool set
    /// it — which is the one way an assertion about the bit can be vacuous. The
    /// same refusal covers the previous transform: `n` is non-zero for every
    /// caller this module builds, so `instance(n)`'s zeroed one is a value the
    /// pool has to have replaced.
    fn stored(n: u32) -> GpuInstance {
        let caller = instance(n);
        assert_eq!(
            caller.flags & GpuInstance::LIVE,
            0,
            "instance({n}) already carries the live bit; pick another n"
        );
        assert_ne!(
            n, 0,
            "instance(0)'s transform is its own zeroed previous one"
        );
        GpuInstance {
            flags: caller.flags | GpuInstance::LIVE,
            previous_transform: caller.transform,
            ..caller
        }
    }

    /// [`stored`] as the pool holds it after a **rewrite** from `was`: live,
    /// and carrying where it came from.
    fn stored_after(n: u32, was: u32) -> GpuInstance {
        GpuInstance {
            previous_transform: instance(was).transform,
            ..stored(n)
        }
    }

    /// Element `index` of the buffer at `slot`, decoded from the bytes that
    /// actually reached the device.
    ///
    /// Read back rather than taken from the pool's mirror, so an assertion is
    /// evidence about what a shader would read and not about the pool's
    /// bookkeeping agreeing with itself.
    fn on_device(recorder: &Recorder, pool: &InstancePool, slot: usize, index: u32) -> GpuInstance {
        let bytes = recorder
            .buffer_bytes(pool.buffers()[slot])
            .expect("the buffer is live");
        let at = index as usize * INSTANCE_STRIDE;
        GpuInstance::from_bytes(
            bytes[at..at + INSTANCE_STRIDE]
                .try_into()
                .expect("one element"),
        )
    }

    /// Every `BufferWritten` this recorder saw, as `(offset, len)`.
    fn writes(recorder: &Recorder) -> Vec<(u64, usize)> {
        recorder
            .events()
            .into_iter()
            .filter_map(|event| match event {
                Event::BufferWritten { offset, len, .. } => Some((offset, len)),
                _ => None,
            })
            .collect()
    }

    /// Drives `frames` uploads, discarding what they wrote — used to reach the
    /// steady state where every slot has caught up.
    fn settle(pool: &mut InstancePool, device: &dyn Device) {
        for _ in 0..FRAMES {
            pool.begin_frame(device).expect("the null device writes");
        }
    }

    /// A dirty set's runs as `(start, end)` pairs, which is what the assertions
    /// below compare — a bare `Range` literal in an array reads as a range the
    /// author meant to iterate, and `clippy` says so.
    fn runs(dirty: &DirtyRanges) -> Vec<(u32, u32)> {
        dirty.runs.iter().map(|run| (run.start, run.end)).collect()
    }

    mod scratch;

    // --- the coalescing, which is pure logic ---

    /// **The headline claim.** Adjacent marks are one run; separated marks are
    /// two. A list that appended would report three runs for the first case,
    /// which is one `write_buffer` per instance and the whole thing this design
    /// exists to avoid.
    #[test]
    fn adjacent_marks_coalesce_and_separated_ones_do_not() {
        let mut dirty = DirtyRanges::default();
        for index in [3u32, 4, 5] {
            dirty.mark(index);
        }
        assert_eq!(runs(&dirty), [(3, 6)], "3, 4 and 5 are one run of three");

        let mut dirty = DirtyRanges::default();
        dirty.mark(3);
        dirty.mark(900);
        assert_eq!(runs(&dirty), [(3, 4), (900, 901)], "a gap stays a gap");
    }

    /// The exact boundary, in both directions: two marks that **touch** are one
    /// run, two marks one element apart are two. Off by one either way and the
    /// first case fragments or the second merges across a gap it must not.
    #[test]
    fn the_boundary_between_touching_and_separated_is_one_element() {
        // `4` touches: 3..4 and 4..5 share an endpoint. `5` leaves one clear
        // element between them.
        for (second, expected) in [(4u32, &[(3, 5)][..]), (5, &[(3, 4), (5, 6)][..])] {
            let mut dirty = DirtyRanges::default();
            dirty.mark(3);
            dirty.mark(second);
            assert_eq!(runs(&dirty), expected, "marking 3 then {second}");
        }
        // And from the other side, because extending a run backwards is a
        // different branch of `mark` than extending it forwards.
        for (first, expected) in [(4u32, &[(3, 5)][..]), (5, &[(3, 4), (5, 6)][..])] {
            let mut dirty = DirtyRanges::default();
            dirty.mark(first);
            dirty.mark(3);
            assert_eq!(runs(&dirty), expected, "marking {first} then 3");
        }
    }

    /// Marking the element between two runs merges all three. Without the merge
    /// after an extend, this stays two runs that touch — which breaks the
    /// invariant everything else assumes and costs an upload forever after.
    #[test]
    fn closing_the_gap_between_two_runs_merges_them() {
        let mut dirty = DirtyRanges::default();
        dirty.mark(3);
        dirty.mark(5);
        assert_eq!(runs(&dirty), [(3, 4), (5, 6)]);
        dirty.mark(4);
        assert_eq!(runs(&dirty), [(3, 6)], "one run of three, not two touching");
    }

    /// Order does not matter: every permutation of the same marks produces the
    /// same runs. Coalescing that only worked left-to-right would pass the
    /// tests above and fail the moment a caller wrote its instances in any
    /// other order.
    #[test]
    fn coalescing_does_not_depend_on_the_order_marks_arrive_in() {
        let marks = [7u32, 3, 5, 4, 900, 6];
        let mut permuted = marks;
        // Every rotation is enough to move each element through every position.
        for shift in 0..marks.len() {
            permuted.rotate_left(shift.min(1));
            let mut dirty = DirtyRanges::default();
            for index in permuted {
                dirty.mark(index);
            }
            assert_eq!(
                runs(&dirty),
                [(3, 8), (900, 901)],
                "the order {permuted:?} produced different runs"
            );
        }
    }

    /// Marking the same element twice is one run of one, so a value rewritten
    /// between uploads is uploaded once.
    #[test]
    fn marking_the_same_element_twice_changes_nothing() {
        let mut dirty = DirtyRanges::default();
        dirty.mark(3);
        dirty.mark(3);
        assert_eq!(runs(&dirty), [(3, 4)]);
        // And re-marking inside an existing run leaves it alone.
        dirty.mark(4);
        dirty.mark(5);
        dirty.mark(4);
        assert_eq!(runs(&dirty), [(3, 6)]);
    }

    // --- the pool ---

    /// **The other headline claim: an unchanged frame uploads nothing.**
    ///
    /// Asserted on the recorded seam calls rather than on a "dirty" flag,
    /// because a flag is a claim about the pool's bookkeeping and this is a
    /// claim about what reached the device.
    #[test]
    fn a_frame_with_no_writes_uploads_nothing() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        pool.insert(&instance(1)).expect("room");
        settle(&mut pool, device.as_ref());

        let before = writes(&recorder).len();
        assert!(before > 0, "the insert must have reached the device at all");
        for _ in 0..FRAMES * 3 {
            pool.begin_frame(device.as_ref()).expect("writes");
        }
        assert_eq!(
            writes(&recorder).len(),
            before,
            "six frames with nothing written must perform no seam call"
        );

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **Every path that changes an instance moves the revision, and a frame
    /// that changed nothing leaves it alone.**
    ///
    /// [`InstancePool::revision`]'s whole contract, and the thing a consumer
    /// keeping something derived from the array — `ForwardRenderer`'s shadow
    /// atlas — bets a frame's correctness on. The list is deliberately every
    /// mutator this type has rather than a sample: a path that changed the
    /// mirror without moving the number would be a caster that moved under a
    /// shadow map nothing redrew.
    #[test]
    fn every_write_moves_the_revision_and_a_still_frame_does_not() {
        let (recorder, device) = open();
        let device = device.as_ref();
        let mut pool = pool(device, 16);
        let start = pool.revision();

        let first = pool.insert(&instance(1)).expect("room");
        let inserted = pool.revision();
        assert!(inserted > start, "an insert did not move the revision");

        assert!(pool.set(first, &instance(2)), "the handle is live");
        let written = pool.revision();
        assert!(written > inserted, "a rewrite did not move the revision");

        assert!(
            pool.set(first, &instance(2)),
            "a rewrite with the same bytes is still a write — see `InstancePool::set`"
        );
        let repeated = pool.revision();
        assert!(
            repeated > written,
            "a rewrite the pool does not filter must not be filtered here either"
        );

        assert!(pool.set_bases(first, 4, 8), "the handle is live");
        let repointed = pool.revision();
        assert!(repointed > repeated, "a re-point did not move the revision");

        // A move to somewhere the record was not, so the settle below has a
        // `previous_transform` to put back: rewriting an instance with the
        // bytes it already holds leaves the two equal, and the carry-forward
        // skips a record already at rest.
        assert!(pool.set(first, &instance(3)), "the handle is live");
        let moved = pool.revision();
        assert!(moved > repointed, "a move did not move the revision");

        // The frame after a move settles the record's `previous_transform`,
        // which is a write and so a change this number reports. Conservative in
        // the safe direction, and the reason the doc says so.
        settle(&mut pool, device);
        let settled = pool.revision();
        assert!(
            settled > moved,
            "the carry-forward wrote the mirror without saying so"
        );

        // And now the pool is quiet: nothing was written, so nothing moves,
        // however many frames go by.
        for _ in 0..FRAMES * 3 {
            pool.begin_frame(device).expect("the null device writes");
        }
        assert_eq!(
            pool.revision(),
            settled,
            "a run of frames nobody wrote in moved the revision, so nothing derived from this \
             array could ever be kept"
        );

        assert!(pool.remove(first), "the handle is live");
        assert!(
            pool.revision() > settled,
            "a removal did not move the revision, so an instance that left the scene would go \
             on casting a shadow"
        );

        pool.destroy(device);
        recorder.assert_valid();
    }

    /// **Every path that changes an element puts it in the written log, once,
    /// and a still frame puts nothing there.**
    ///
    /// [`InstancePool::take_written`]'s contract, which `ForwardRenderer`'s
    /// shadow atlas now bets a frame's correctness on in place of the revision:
    /// an element missing from the log is a caster that moved under a map
    /// nothing redrew. Every mutator this type has, as the revision's test
    /// above lists them.
    #[test]
    fn every_write_names_its_element_once_and_a_still_frame_names_none() {
        let (recorder, device) = open();
        let device = device.as_ref();
        let mut pool = pool(device, 16);
        let taken = |pool: &mut InstancePool| {
            let mut into = Vec::new();
            pool.take_written(&mut into);
            into
        };

        let first = pool.insert(&instance(1)).expect("room");
        let second = pool.insert(&instance(2)).expect("room");
        assert!(pool.set(first, &instance(3)), "the handle is live");
        assert_eq!(
            taken(&mut pool),
            [0, 1],
            "two inserts and a rewrite of the first name each element once, first write first"
        );
        assert!(taken(&mut pool).is_empty(), "a taken log is forgotten");

        assert!(pool.set(second, &instance(2)), "the handle is live");
        assert_eq!(
            taken(&mut pool),
            [1],
            "a same-bytes rewrite is still a write"
        );
        assert!(pool.set_bases(first, 4, 8), "the handle is live");
        assert_eq!(taken(&mut pool), [0], "a re-point is a write");
        assert!(pool.set_flags(second, 1 << 8), "the handle is live");
        assert_eq!(taken(&mut pool), [1], "a flag is a write");
        pool.clear_flags(1 << 8);
        assert_eq!(
            taken(&mut pool),
            [1],
            "clearing a flag writes what carried it"
        );

        // The first element moved above, so the frame after its move puts its
        // `previous_transform` back at rest — a write the log reports.
        settle(&mut pool, device);
        assert_eq!(
            taken(&mut pool),
            [0],
            "the carry-forward wrote the mirror without naming the element"
        );
        for _ in 0..FRAMES * 3 {
            pool.begin_frame(device).expect("the null device writes");
        }
        assert!(
            taken(&mut pool).is_empty(),
            "a run of frames nobody wrote in named an element"
        );

        assert!(pool.remove(first), "the handle is live");
        assert_eq!(taken(&mut pool), [0], "a removal is a write");
        let record = pool.record(0).expect("below the high-water mark");
        assert_eq!(
            record.flags & GpuInstance::LIVE,
            0,
            "a removed element reads back dead"
        );
        assert!(pool.record(2).is_none(), "past the high-water mark");

        pool.destroy(device);
        recorder.assert_valid();
    }

    /// [`InstancePool::set_bounds`] writes the box and the
    /// [`GpuInstance::OWNS_BOUNDS`] bit and nothing else: every other flag and
    /// field survives, a zero `owns` clears the bit, a bit `owns` carries
    /// besides it is ignored, the transform history is not touched, and a
    /// stale handle writes nothing.
    #[test]
    fn set_bounds_writes_the_box_and_its_bit_alone() {
        let (recorder, device) = open();
        let device = device.as_ref();
        let mut pool = pool(device, 4);
        let other_flags =
            (1 << 8) | GpuInstance::CASTS_NO_SHADOW | GpuInstance::BASE_VERTEX_OVERRIDE;
        let handle = pool
            .insert(&GpuInstance {
                flags: other_flags,
                ..instance(1)
            })
            .expect("room");
        assert!(pool.set(
            handle,
            &GpuInstance {
                flags: other_flags,
                ..instance(2)
            }
        ));
        let before = pool.get(handle).expect("live");

        let (min, max) = ([-3.0, -2.0, -1.0], [1.0, 2.0, 3.0]);
        assert!(
            pool.set_bounds(handle, GpuInstance::OWNS_BOUNDS | (1 << 20), min, max),
            "the handle is live"
        );
        let owned = pool.get(handle).expect("live");
        assert_eq!(
            owned,
            GpuInstance {
                flags: before.flags | GpuInstance::OWNS_BOUNDS,
                bounds_min: min,
                bounds_max: max,
                ..before
            },
            "the box and its bit are written, the extra bit in `owns` is not, and nothing \
             else moves — the previous transform included"
        );

        assert!(pool.set_bounds(handle, 0, [0.0; 3], [0.0; 3]));
        let cleared = pool.get(handle).expect("live");
        assert_eq!(
            cleared.flags, before.flags,
            "a zero `owns` clears the bit alone"
        );

        assert!(pool.remove(handle), "the handle is live");
        let mut written = Vec::new();
        pool.take_written(&mut written);
        let dead = pool.record(0).expect("below the high-water mark");
        assert!(
            !pool.set_bounds(handle, GpuInstance::OWNS_BOUNDS, min, max),
            "a stale handle is refused"
        );
        assert_eq!(pool.record(0), Some(dead), "and writes nothing");
        written.clear();
        pool.take_written(&mut written);
        assert!(written.is_empty(), "nor names the element as written");

        pool.destroy(device);
        recorder.assert_valid();
    }

    /// Three adjacent instances written in one frame reach the device as **one**
    /// write of three elements, and two separated ones as two writes of one.
    /// This is the coalescing test above, seen from the device's side.
    #[test]
    fn adjacent_writes_reach_the_device_as_one_call() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 1024);
        let handles: Vec<_> = (0..6)
            .map(|n| pool.insert(&instance(n)).expect("room"))
            .collect();
        settle(&mut pool, device.as_ref());

        let before = writes(&recorder).len();
        for handle in &handles[3..6] {
            pool.set(*handle, &instance(99));
        }
        pool.begin_frame(device.as_ref()).expect("writes");
        assert_eq!(
            writes(&recorder)[before..],
            [(3 * INSTANCE_STRIDE as u64, 3 * INSTANCE_STRIDE)],
            "instances 3, 4 and 5 must be one write of three elements"
        );

        // Drained, so the next measurement is one slot's own runs rather than
        // that slot's leftovers from the write above — each slot lags the
        // others by a frame, which is the ring doing its job.
        settle(&mut pool, device.as_ref());
        let before = writes(&recorder).len();
        pool.set(handles[0], &instance(98));
        pool.set(handles[5], &instance(98));
        pool.begin_frame(device.as_ref()).expect("writes");
        assert_eq!(
            writes(&recorder)[before..],
            [
                (0, INSTANCE_STRIDE),
                (5 * INSTANCE_STRIDE as u64, INSTANCE_STRIDE)
            ],
            "instances 0 and 5 are not adjacent and must be two writes"
        );

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// A write reaches **every** buffer in the ring, not just the one that
    /// happened to be current: each slot is a separate copy, and a slot that
    /// missed an update would draw a stale instance every other frame.
    #[test]
    fn a_write_reaches_every_buffer_in_the_ring() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        let handle = pool.insert(&instance(1)).expect("room");
        settle(&mut pool, device.as_ref());

        pool.set(handle, &instance(2));
        let mut written_to = Vec::new();
        for _ in 0..FRAMES {
            let slot = pool.begin_frame(device.as_ref()).expect("writes");
            written_to.push(pool.buffers()[slot]);
        }
        let targets: Vec<BufferHandle> = recorder
            .events()
            .into_iter()
            .filter_map(|event| match event {
                Event::BufferWritten { buffer, .. } => Some(buffer),
                _ => None,
            })
            .collect();
        for buffer in pool.buffers() {
            assert!(
                targets.contains(buffer),
                "buffer {buffer:?} never received the instance"
            );
        }
        assert_eq!(written_to.len(), FRAMES);

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// A full pool refuses by name, and takes nothing: the refused insert must
    /// not consume a slot, or a caller that retries would lose capacity each
    /// time it was told there was none.
    #[test]
    fn a_full_pool_fails_by_name_and_takes_nothing() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 2);
        let first = pool.insert(&instance(0)).expect("room");
        pool.insert(&instance(1)).expect("room");

        let error = pool
            .insert(&instance(2))
            .expect_err("two instances fill a pool of two");
        assert!(
            matches!(
                error,
                InstancePoolError::PoolFull {
                    capacity: 2,
                    in_use: 2
                }
            ),
            "got: {error:?}"
        );
        assert_eq!(pool.len(), 2, "a refused insert must not consume a slot");

        // And a freed slot is handed out again, so the refusal above is the
        // capacity rather than the pool having given up.
        assert!(pool.remove(first));
        let reused = pool.insert(&instance(3)).expect("the freed slot");
        assert_eq!(pool.index(reused), Some(0), "the low slot comes back");
        assert_eq!(pool.index(first), None, "and the old handle is retired");

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// The number a cull dispatch is sized by covers every occupied slot, and
    /// **it does not shrink when one is freed**.
    ///
    /// That is the whole difference from [`InstancePool::len`]: remove the
    /// middle instance of three and `len` says two, while the third is still at
    /// element 2 and a walk of `0..2` would never test it. A dispatch sized that
    /// way drops an instance from the picture and reports it as culled.
    #[test]
    fn the_slot_count_covers_every_occupied_element_and_never_shrinks() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 8);
        assert_eq!(pool.slot_count(), 0, "an empty pool has nothing to walk");

        let handles: Vec<InstanceHandle> = (0..3)
            .map(|index| pool.insert(&instance(index)).expect("room"))
            .collect();
        assert_eq!(pool.slot_count(), 3);

        assert!(pool.remove(handles[1]));
        assert_eq!(pool.len(), 2, "two are live");
        assert_eq!(
            pool.slot_count(),
            3,
            "and the highest is still at element 2, so a walk must still reach it"
        );

        // A reused slot does not raise it either: the element was already
        // covered.
        pool.insert(&instance(9)).expect("the freed slot");
        assert_eq!(pool.slot_count(), 3);

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// One buffer per frame in flight, each sized for the whole capacity — the
    /// ring is what the module docs claim it is, and a pool that quietly kept
    /// one buffer would pass every other test here.
    #[test]
    fn the_ring_is_one_buffer_per_frame_in_flight() {
        let (recorder, device) = open();
        let pool = pool(device.as_ref(), 16);
        assert_eq!(pool.buffers().len(), FRAMES);
        let mut distinct = pool.buffers().to_vec();
        distinct.sort_unstable_by_key(|buffer| buffer.to_bits());
        distinct.dedup();
        assert_eq!(distinct.len(), FRAMES, "the slots must not share a buffer");
        assert_eq!(pool.capacity(), 16);
        assert!(pool.is_empty());

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **Every buffer is cleared when the pool is created**, which is the other
    /// half of the removal contract: the cull pass walks from element zero, so
    /// the slots above the ones in use have to answer too, and a zeroed element
    /// is a dead instance.
    ///
    /// Asserted on the seam calls rather than on the bytes, and that is the
    /// only assertion available here: this recorder zero-fills a mappable
    /// buffer at creation, so a byte comparison would pass whether or not the
    /// pool ever wrote one. What a real driver leaves in a fresh host-visible
    /// allocation is its own business, which is why `crcbl-vk`'s `cull` e2e
    /// dispatches over a pool's whole capacity with only three slots inserted.
    #[test]
    fn every_buffer_is_cleared_when_the_pool_is_created() {
        let (recorder, device) = open();
        let pool = pool(device.as_ref(), 16);
        assert_eq!(
            writes(&recorder),
            vec![(0, 16 * INSTANCE_STRIDE); FRAMES],
            "each buffer in the ring must be cleared, whole, before anything reads it"
        );

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **Where an instance was last frame, across the three cases that decide
    /// it**: a spawn is at rest, a move carries where it moved from, and a
    /// frame with no move puts it back at rest.
    ///
    /// The third is the one the carry-forward exists for and the one a pool
    /// without it would fail: `set` alone leaves the record naming the
    /// transform it left, so an instance that moved once and then stood still
    /// would report that move on every frame afterwards, forever.
    ///
    /// The order is a frame's real one — write the instances, *then*
    /// `begin_frame` — because that is what decides which frame a move belongs
    /// to.
    ///
    /// Read back off the device rather than out of the mirror: this is what a
    /// shader would fetch.
    #[test]
    fn an_instance_carries_where_it_was_last_frame() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        let handle = pool.insert(&instance(7)).expect("room");
        settle(&mut pool, device.as_ref());
        let index = pool.index(handle).expect("live");
        let slot = pool.frame;
        assert_eq!(
            on_device(&recorder, &pool, slot, index).previous_transform,
            instance(7).transform,
            "a spawn did not travel from anywhere"
        );

        // The frame it moves: the record names both ends of the move.
        assert!(pool.set(handle, &instance(11)));
        let slot = pool
            .begin_frame(device.as_ref())
            .expect("the null device writes");
        let moved = on_device(&recorder, &pool, slot, index);
        assert_eq!(moved.transform, instance(11).transform, "it arrived");
        assert_eq!(
            moved.previous_transform,
            instance(7).transform,
            "and it says where from"
        );

        // The next frame, with nothing written: back at rest.
        let slot = pool
            .begin_frame(device.as_ref())
            .expect("the null device writes");
        let stopped = on_device(&recorder, &pool, slot, index);
        assert_eq!(
            stopped.transform,
            instance(11).transform,
            "it has not moved"
        );
        assert_eq!(
            stopped.previous_transform, stopped.transform,
            "so it must report no motion"
        );

        // And it stays at rest without being written again, in every buffer of
        // the ring rather than only the one the last frame rotated to.
        settle(&mut pool, device.as_ref());
        for slot in 0..FRAMES {
            let resting = on_device(&recorder, &pool, slot, index);
            assert_eq!(
                resting.previous_transform, resting.transform,
                "buffer {slot} still has it moving"
            );
        }

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **An instance that keeps moving is never put at rest.** The
    /// carry-forward runs a frame behind, so the case it must not touch is the
    /// one where the caller wrote the instance again — settling that would
    /// erase a move before the frame holding it was ever drawn.
    #[test]
    fn an_instance_still_moving_is_not_put_at_rest() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        let handle = pool.insert(&instance(1)).expect("room");
        settle(&mut pool, device.as_ref());
        let index = pool.index(handle).expect("live");

        for step in 2..6u32 {
            assert!(pool.set(handle, &instance(step)));
            let slot = pool
                .begin_frame(device.as_ref())
                .expect("the null device writes");
            let moving = on_device(&recorder, &pool, slot, index);
            assert_eq!(
                (moving.previous_transform, moving.transform),
                (instance(step - 1).transform, instance(step).transform),
                "the frame that moved it to {step} lost one end of the move"
            );
        }

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **A re-point rewrites the two bases and leaves the move alone**, which
    /// is the whole of why [`InstancePool::set_bases`] exists.
    ///
    /// The record is moved first, so it is carrying a previous transform that
    /// differs from its current one when the bases are rewritten — a call that
    /// went through [`InstancePool::set`] would take the previous transform
    /// from the record it overwrites, which by then holds the move's *end*, and
    /// the move would be gone. That is what
    /// [`crate::forward::ForwardRenderer`]'s per-frame re-point did to every
    /// skinned instance.
    ///
    /// The frame checked on the device is the one the move is drawn in. The
    /// frame after it puts the record back at rest, which is
    /// [`carry_forward`](InstancePool::carry_forward)'s job rather than this
    /// call's — [`an_instance_carries_where_it_was_last_frame`] is where that
    /// half is asserted, and the renderer-level
    /// `a_skinned_instance_that_moves_carries_the_transform_it_moved_from` is
    /// where the two meet.
    ///
    /// The stale half is [`InstancePool::set`]'s contract: `false`, and nothing
    /// written — asserted on the bytes that reached the device and on the
    /// uploads a later frame performs, because a mirror written without being
    /// marked dirty would satisfy only one of the two.
    #[test]
    fn setting_the_bases_keeps_the_transform_the_record_moved_from() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        let handle = pool.insert(&instance(7)).expect("room");
        assert!(pool.set(handle, &instance(11)), "the handle is live");

        assert!(pool.set_bases(handle, 40, 41), "the handle is still live");
        let repointed = GpuInstance {
            base_vertex: 40,
            previous_base_vertex: 41,
            ..stored_after(11, 7)
        };
        assert_eq!(
            pool.get(handle),
            Some(repointed),
            "a re-point owes the two bases it was given and every other field the record \
             already held — the previous transform above all"
        );

        let index = pool.index(handle).expect("live");
        let slot = pool
            .begin_frame(device.as_ref())
            .expect("the null device writes");
        assert_eq!(
            on_device(&recorder, &pool, slot, index),
            repointed,
            "the buffer this frame draws from did not take the re-point, so a shader reads \
             the bases of the frame before"
        );

        assert!(pool.remove(handle));
        for _ in 0..FRAMES * 2 {
            pool.begin_frame(device.as_ref())
                .expect("the null device writes");
        }
        recorder.clear();
        assert!(
            !pool.set_bases(handle, 60, 61),
            "a retired handle names no instance"
        );
        for _ in 0..FRAMES * 2 {
            pool.begin_frame(device.as_ref())
                .expect("the null device writes");
        }
        assert_eq!(
            writes(&recorder),
            [],
            "a stale re-point marked a slot dirty, so it wrote into whatever took it"
        );
        for slot in 0..FRAMES {
            let dead = on_device(&recorder, &pool, slot, index);
            assert_eq!(
                (dead.base_vertex, dead.previous_base_vertex),
                (40, 41),
                "buffer {slot} took a stale handle's bases"
            );
        }

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **Standing still costs one upload per buffer and then nothing.** That is
    /// the claim delta upload is here to make, and an unconditional
    /// carry-forward would break it — every instance would be dirty in every
    /// frame forever.
    ///
    /// The first assertion is what keeps the second from being vacuous: a
    /// carry-forward that never reached the device would also produce no
    /// uploads at rest.
    #[test]
    fn an_instance_at_rest_is_uploaded_once_and_then_never() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        let handle = pool.insert(&instance(7)).expect("room");
        settle(&mut pool, device.as_ref());

        assert!(pool.set(handle, &instance(11)));
        settle(&mut pool, device.as_ref());

        // The move has reached every buffer and the last of those frames
        // carried it forward, which marked the element dirty everywhere again.
        recorder.clear();
        pool.begin_frame(device.as_ref())
            .expect("the null device writes");
        let carried = writes(&recorder).len();
        assert_eq!(
            carried, 1,
            "the buffer that had not seen the carry-forward yet should have taken it, \
             and {carried} write(s) says the carry-forward never reached the device"
        );

        recorder.clear();
        for _ in 0..FRAMES * 2 {
            pool.begin_frame(device.as_ref())
                .expect("the null device writes");
        }
        assert_eq!(
            writes(&recorder),
            [],
            "an instance nobody has written costs no upload"
        );

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// What goes in comes out, field for field, and a removed handle stops
    /// resolving. The four ids are the same width and would permute silently,
    /// so the round trip is checked rather than assumed.
    #[test]
    fn an_instance_round_trips_through_the_pool() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        let handle = pool.insert(&instance(7)).expect("room");
        assert_eq!(pool.get(handle), Some(stored(7)));

        assert!(pool.set(handle, &instance(11)));
        assert_eq!(
            pool.get(handle),
            Some(stored_after(11, 7)),
            "a rewrite keeps where the instance was"
        );

        assert!(pool.remove(handle));
        assert_eq!(
            pool.get(handle),
            None,
            "a removed handle resolves to nothing"
        );
        assert!(!pool.set(handle, &instance(12)), "and cannot be written");
        assert!(!pool.remove(handle), "and cannot be removed twice");

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **Removing an instance clears its live bit on the device, and changes
    /// nothing else about the record.**
    ///
    /// The whole slice: a slot the cull pass walks past has to be able to say
    /// it is free, and the bit is the only thing a removal is allowed to
    /// rewrite. Asserted on the bytes in the buffer rather than on the mirror,
    /// because a removal that never reached a buffer is a removal the GPU does
    /// not know about.
    #[test]
    fn removing_an_instance_clears_its_live_bit_on_the_device() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        let first = pool.insert(&instance(1)).expect("room");
        let second = pool.insert(&instance(3)).expect("room");
        settle(&mut pool, device.as_ref());
        for slot in 0..FRAMES {
            assert_eq!(
                on_device(&recorder, &pool, slot, 0),
                stored(1),
                "buffer {slot} did not receive the live instance"
            );
        }

        assert!(pool.remove(first));
        settle(&mut pool, device.as_ref());
        for slot in 0..FRAMES {
            let removed = on_device(&recorder, &pool, slot, 0);
            assert_eq!(
                removed.flags & GpuInstance::LIVE,
                0,
                "buffer {slot} still holds a live instance at the removed slot: {removed:?}"
            );
            // And the rest of the record is exactly what it was. The pool
            // deliberately does not zero it — see the module docs — so a test
            // that only checked the bit would pass on an implementation that
            // wrote a degenerate instance at the origin instead.
            assert_eq!(
                removed,
                GpuInstance {
                    flags: stored(1).flags & !GpuInstance::LIVE,
                    ..stored(1)
                },
                "buffer {slot} rewrote more than the flags word"
            );
            assert_eq!(
                on_device(&recorder, &pool, slot, 1),
                stored(3),
                "buffer {slot} disturbed the instance beside the removed one"
            );
        }

        assert_eq!(
            pool.get(second),
            Some(stored(3)),
            "and the other handle still resolves"
        );

        // A second removal of the retired handle writes nothing at all, so a
        // caller cannot clear the live bit of whatever has since taken the slot.
        let before = writes(&recorder).len();
        assert!(!pool.remove(first));
        settle(&mut pool, device.as_ref());
        assert_eq!(
            writes(&recorder).len(),
            before,
            "removing a retired handle must upload nothing"
        );

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **A slot reused after a removal is live again, holding its new
    /// contents.** The other direction of the same defect: a liveness bit that
    /// stuck would make the pool leak capacity that looks allocated and draws
    /// nothing.
    #[test]
    fn a_reused_slot_is_live_again_with_its_new_contents() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        let first = pool.insert(&instance(1)).expect("room");
        assert!(pool.remove(first));
        settle(&mut pool, device.as_ref());

        let reused = pool.insert(&instance(5)).expect("the freed slot");
        assert_eq!(pool.index(reused), Some(0), "the low slot comes back");
        settle(&mut pool, device.as_ref());
        for slot in 0..FRAMES {
            assert_eq!(
                on_device(&recorder, &pool, slot, 0),
                stored(5),
                "buffer {slot} holds neither the new instance nor the live bit"
            );
        }
        assert_eq!(pool.get(reused), Some(stored(5)));

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// The bytes that reach the device are the instance's, at the offset its
    /// index names — checked by writing a high slot and reading back the offset
    /// the seam was given. An off-by-one in the element/byte conversion would
    /// put every instance one slot out and still upload the right number of
    /// bytes.
    #[test]
    fn an_instances_offset_is_its_index_times_the_stride() {
        let (recorder, device) = open();
        let mut pool = pool(device.as_ref(), 16);
        for n in 0..5 {
            pool.insert(&instance(n)).expect("room");
        }
        settle(&mut pool, device.as_ref());
        let handle = pool.insert(&instance(5)).expect("room");
        assert_eq!(pool.index(handle), Some(5));

        let before = writes(&recorder).len();
        pool.begin_frame(device.as_ref()).expect("writes");
        assert_eq!(
            writes(&recorder)[before..],
            [(5 * INSTANCE_STRIDE as u64, INSTANCE_STRIDE)]
        );

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// Everything created is destroyed.
    #[test]
    fn a_pool_leaks_nothing() {
        let (recorder, device) = open();
        let before = recorder.total_live_objects();
        let mut pool = pool(device.as_ref(), 16);
        pool.insert(&instance(0)).expect("room");
        settle(&mut pool, device.as_ref());
        assert!(recorder.total_live_objects() > before);

        pool.destroy(device.as_ref());
        assert_eq!(
            recorder.total_live_objects(),
            before,
            "every buffer created must be destroyed"
        );
        recorder.assert_valid();
    }

    /// A pool error that has to travel as a [`HalError`] keeps its message,
    /// which is the whole reason the exhaustion error names its numbers.
    #[test]
    fn a_pool_error_flattens_into_the_seams_without_losing_its_message() {
        let full = InstancePoolError::PoolFull {
            capacity: 4,
            in_use: 4,
        };
        let message = full.to_string();
        assert_eq!(HalError::from(full).to_string(), message);

        let hal = InstancePoolError::Hal(HalError::OutOfDeviceMemory);
        assert!(
            matches!(HalError::from(hal), HalError::OutOfDeviceMemory),
            "a HalError must not be stringified on the way through"
        );
    }
}