sim-engine 0.4.2

A validated high-performance visualization engine for simulations
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
use super::RendererPreparationMetrics;
use super::glyph_batch::GlyphBatchSpan;
use std::error::Error;
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;

#[cfg(test)]
use super::ImageSprite2d;
use super::{
    BlendMode, COLOR_MAP_LUT_SIZE, Camera2d, CameraUniform, Color, ColorMap, CompositeUniform,
    DynamicMesh2d, GeometryExtents, GeometryValidationCache, GeometryValidationSource, GpuTimingId,
    HeatmapUniform, Image2d, ImageBatch2d, ImageBatchPlacement, ImageSampling, ImageTexelRect,
    ImageUniform, LogicalScreenPosition, LogicalViewport, LogicalViewportRegion, ParticleField2d,
    ParticleGpu, ParticleStatistics, PreparedDrawBatch, PreparedScene, PreparedScreenScene, Rect,
    RenderStatus, RenderTarget2d, RendererFrameError, RendererFrameMetrics, ScalarFieldSampling,
    ScalarFieldTexture, Scene, ScissorRect, ScreenClipRect, ScreenScene, TessellationStats, Vec2,
    Vertex, WgpuRenderer, geometry_is_safe_for, geometry_is_safe_for_cached, image,
    is_portable_shader_source, logical_viewport_scissor, offset_scissor,
    particle_statistics_with_budget, particle_visible_index_is_selected, render_report,
    scalar_normalization_is_portable, scene_estimate_fits_streaming_device, screen_camera,
    screen_clip_to_scissor, tessellate_scene, uniformly_sampled_index,
    validate_particle_staging_capacity,
};

mod cache;
mod compose;
mod encoding;
#[cfg(test)]
pub(super) use encoding::assert_gpu_encoding_contract;
#[cfg(test)]
pub(super) use encoding::encode_glyph_batch_for_test;
pub(super) use encoding::encode_screen_primitives;
#[cfg(test)]
pub(super) use encoding::encode_screen_primitives_for_test;
mod present;
mod primitives;
mod streaming;
#[cfg(test)]
mod tests;
mod uniform_uploads;
pub(super) use cache::FrameCache;
#[cfg(test)]
pub(super) use cache::assert_gpu_binding_sharing_contract;
pub use cache::{FrameCacheBudget, FrameCacheStatistics};
#[cfg(test)]
pub(super) use uniform_uploads::assert_gpu_uniform_upload_contract;

/// Work category constrained by a [`FrameBudget`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameBudgetResource {
    /// Number of ordered sources in one frame.
    Passes,
    /// Scene commands referenced by the frame.
    Commands,
    /// Generated or retained vertices referenced by the frame.
    Vertices,
    /// Bytes uploaded while preparing the frame.
    UploadBytes,
    /// Nominal retained texel-storage bytes referenced by images, fields, or targets.
    TextureBytes,
    /// Conservative number of GPU draw calls encoded by the frame.
    DrawCalls,
}

/// Explicit upper bounds for one heterogeneous presentation frame.
///
/// The default is deliberately finite. Production hosts should select limits
/// from their frame-time and memory budgets instead of treating it as a device
/// capability query.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameBudget {
    max_passes: usize,
    max_commands: usize,
    max_vertices: usize,
    max_upload_bytes: usize,
    max_texture_bytes: usize,
    max_draw_calls: usize,
}

impl FrameBudget {
    /// Creates an exact set of frame-work limits.
    pub const fn new(
        max_passes: usize,
        max_commands: usize,
        max_vertices: usize,
        max_upload_bytes: usize,
        max_texture_bytes: usize,
        max_draw_calls: usize,
    ) -> Self {
        Self {
            max_passes,
            max_commands,
            max_vertices,
            max_upload_bytes,
            max_texture_bytes,
            max_draw_calls,
        }
    }

    /// Returns the maximum number of ordered sources.
    pub const fn max_passes(self) -> usize {
        self.max_passes
    }

    /// Returns the maximum referenced scene-command count.
    pub const fn max_commands(self) -> usize {
        self.max_commands
    }

    /// Returns the maximum referenced or generated vertex count.
    pub const fn max_vertices(self) -> usize {
        self.max_vertices
    }

    /// Returns the maximum bytes uploaded while presenting the frame.
    pub const fn max_upload_bytes(self) -> usize {
        self.max_upload_bytes
    }

    /// Returns the maximum referenced nominal texel-storage bytes.
    pub const fn max_texture_bytes(self) -> usize {
        self.max_texture_bytes
    }

    /// Returns the maximum conservative draw-call count.
    pub const fn max_draw_calls(self) -> usize {
        self.max_draw_calls
    }
}

impl Default for FrameBudget {
    fn default() -> Self {
        Self::new(
            64,
            100_000,
            4_000_000,
            256 * 1024 * 1024,
            512 * 1024 * 1024,
            65_536,
        )
    }
}

/// Shared placement and ordering state for one frame item.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct FramePassOptions {
    order: i32,
    viewport: Option<LogicalViewportRegion>,
    clip: Option<ScreenClipRect>,
}

impl FramePassOptions {
    /// Creates a full-surface, unclipped item at `order`.
    pub const fn new(order: i32) -> Self {
        Self {
            order,
            viewport: None,
            clip: None,
        }
    }

    /// Positions the item in a logical sub-viewport of the surface.
    pub const fn with_viewport(mut self, viewport: LogicalViewportRegion) -> Self {
        self.viewport = Some(viewport);
        self
    }

    /// Intersects all source-local clips with an additional item-local clip.
    pub const fn with_clip(mut self, clip: ScreenClipRect) -> Self {
        self.clip = Some(clip);
        self
    }

    /// Returns the stable ordering key. Lower values draw first.
    pub const fn order(self) -> i32 {
        self.order
    }

    /// Returns the optional positioned logical viewport.
    pub const fn viewport(self) -> Option<LogicalViewportRegion> {
        self.viewport
    }

    /// Returns the optional item-local logical clip.
    pub const fn clip(self) -> Option<ScreenClipRect> {
        self.clip
    }
}

/// Renderer-owned source category used in structured composer errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameSourceKind {
    /// Streaming world-space or fixed-screen scene tessellated for this frame.
    StreamingScene,
    /// Immutable prepared world or screen geometry.
    PreparedScene,
    /// Mutable retained triangle geometry.
    DynamicMesh,
    /// Instanced retained particle resource.
    ParticleField,
    /// Renderer-owned scalar texture.
    ScalarField,
    /// Retained sRGB RGBA image or atlas.
    Image,
    /// Retained host-shaped glyph atlas and positioned run.
    Glyph,
    /// Offscreen color texture.
    RenderTarget,
}

/// Ordered frame inputs grouped by their renderer source path.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct FrameSourceStatistics {
    streaming_scenes: usize,
    prepared_scenes: usize,
    dynamic_meshes: usize,
    particle_fields: usize,
    scalar_fields: usize,
    images: usize,
    glyph_runs: usize,
    render_targets: usize,
}

impl FrameSourceStatistics {
    /// Returns streaming world and fixed-screen scenes.
    pub const fn streaming_scenes(self) -> usize {
        self.streaming_scenes
    }

    /// Returns immutable prepared world and fixed-screen scenes.
    pub const fn prepared_scenes(self) -> usize {
        self.prepared_scenes
    }

    /// Returns retained dynamic triangle meshes.
    pub const fn dynamic_meshes(self) -> usize {
        self.dynamic_meshes
    }

    /// Returns retained particle fields.
    pub const fn particle_fields(self) -> usize {
        self.particle_fields
    }

    /// Returns retained scalar fields.
    pub const fn scalar_fields(self) -> usize {
        self.scalar_fields
    }

    /// Returns image, world-image, and atlas-batch sources.
    pub const fn images(self) -> usize {
        self.images
    }

    /// Returns host-shaped glyph runs.
    pub const fn glyph_runs(self) -> usize {
        self.glyph_runs
    }

    /// Returns composed 2D or retained-3D color targets.
    pub const fn render_targets(self) -> usize {
        self.render_targets
    }

    /// Returns all accepted frame sources.
    pub const fn total(self) -> usize {
        self.streaming_scenes
            .saturating_add(self.prepared_scenes)
            .saturating_add(self.dynamic_meshes)
            .saturating_add(self.particle_fields)
            .saturating_add(self.scalar_fields)
            .saturating_add(self.images)
            .saturating_add(self.glyph_runs)
            .saturating_add(self.render_targets)
    }

    const fn single(source: FrameSourceKind) -> Self {
        let mut counts = Self {
            streaming_scenes: 0,
            prepared_scenes: 0,
            dynamic_meshes: 0,
            particle_fields: 0,
            scalar_fields: 0,
            images: 0,
            glyph_runs: 0,
            render_targets: 0,
        };
        match source {
            FrameSourceKind::StreamingScene => counts.streaming_scenes = 1,
            FrameSourceKind::PreparedScene => counts.prepared_scenes = 1,
            FrameSourceKind::DynamicMesh => counts.dynamic_meshes = 1,
            FrameSourceKind::ParticleField => counts.particle_fields = 1,
            FrameSourceKind::ScalarField => counts.scalar_fields = 1,
            FrameSourceKind::Image => counts.images = 1,
            FrameSourceKind::Glyph => counts.glyph_runs = 1,
            FrameSourceKind::RenderTarget => counts.render_targets = 1,
        }
        counts
    }

    const fn adding(self, other: Self) -> Self {
        Self {
            streaming_scenes: self.streaming_scenes.saturating_add(other.streaming_scenes),
            prepared_scenes: self.prepared_scenes.saturating_add(other.prepared_scenes),
            dynamic_meshes: self.dynamic_meshes.saturating_add(other.dynamic_meshes),
            particle_fields: self.particle_fields.saturating_add(other.particle_fields),
            scalar_fields: self.scalar_fields.saturating_add(other.scalar_fields),
            images: self.images.saturating_add(other.images),
            glyph_runs: self.glyph_runs.saturating_add(other.glyph_runs),
            render_targets: self.render_targets.saturating_add(other.render_targets),
        }
    }
}

/// Failure while constructing or presenting a heterogeneous frame.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FrameComposerError {
    /// The one frame clear color was not normalized linear RGBA.
    InvalidBackground,
    /// A target-composition opacity was non-finite or outside `0.0..=1.0`.
    InvalidOpacity,
    /// An image tint was not normalized linear RGBA.
    InvalidTint,
    /// An atlas source rectangle did not fit its image.
    InvalidImageRegion,
    /// A world image rectangle or pseudo-depth was invalid.
    InvalidWorldImage,
    /// Scalar endpoints were invalid or their finite subtraction overflowed.
    InvalidValueRange {
        /// Lower scalar endpoint supplied by the host.
        minimum: f32,
        /// Upper scalar endpoint supplied by the host.
        maximum: f32,
    },
    /// A retained source belongs to another renderer or recovery generation.
    RendererMismatch {
        /// Category of the rejected retained source.
        source: FrameSourceKind,
    },
    /// A frame-work limit would be exceeded.
    BudgetExceeded {
        /// Work category whose limit was exceeded.
        resource: FrameBudgetResource,
        /// Configured upper bound.
        limit: usize,
        /// Conservative work after accepting the requested item.
        actual: usize,
    },
    /// CPU storage for frame construction could not be reserved.
    AllocationFailed {
        /// Minimum additional bytes requested by the failed reservation.
        requested_bytes: usize,
    },
    /// Validation, surface acquisition, or GPU-capacity work failed.
    Frame(RendererFrameError),
}

impl fmt::Display for FrameComposerError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidBackground => write!(formatter, "frame background must be normalized"),
            Self::InvalidOpacity => write!(formatter, "frame opacity must be finite and in 0..=1"),
            Self::InvalidTint => write!(formatter, "frame image tint must be normalized"),
            Self::InvalidImageRegion => write!(formatter, "frame image region is out of bounds"),
            Self::InvalidWorldImage => {
                write!(formatter, "frame world image rectangle or depth is invalid")
            }
            Self::InvalidValueRange { minimum, maximum } => {
                write!(formatter, "invalid frame scalar range {minimum}..{maximum}")
            }
            Self::RendererMismatch { source } => {
                write!(formatter, "frame {source:?} belongs to another renderer")
            }
            Self::BudgetExceeded {
                resource,
                limit,
                actual,
            } => write!(
                formatter,
                "frame {resource:?} work exceeds its limit {limit}: {actual}"
            ),
            Self::AllocationFailed { requested_bytes } => write!(
                formatter,
                "could not reserve {requested_bytes} additional bytes for frame construction"
            ),
            Self::Frame(error) => write!(formatter, "frame presentation failed: {error}"),
        }
    }
}

impl Error for FrameComposerError {}

impl From<RendererFrameError> for FrameComposerError {
    fn from(error: RendererFrameError) -> Self {
        Self::Frame(error)
    }
}

/// Bounded work and retained allocations referenced by one composed frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct FrameStatistics {
    pass_count: usize,
    command_count: usize,
    vertex_count: usize,
    streaming_vertex_count: usize,
    reused_vertex_count: usize,
    upload_bytes: usize,
    streaming_upload_bytes: usize,
    texture_bytes: usize,
    retained_cpu_bytes: usize,
    retained_buffer_bytes: usize,
    draw_calls: usize,
    source_counts: FrameSourceStatistics,
}

impl FrameStatistics {
    /// Returns ordered source count.
    pub const fn pass_count(self) -> usize {
        self.pass_count
    }

    /// Returns referenced scene-command count.
    pub const fn command_count(self) -> usize {
        self.command_count
    }

    /// Returns referenced or generated vertex count.
    pub const fn vertex_count(self) -> usize {
        self.vertex_count
    }

    /// Returns streaming vertices selected by draws during this frame.
    /// Repeated references may share one within-frame upload; use
    /// [`Self::streaming_upload_bytes`] for the actual transferred payload.
    pub const fn streaming_vertex_count(self) -> usize {
        self.streaming_vertex_count
    }

    /// Returns retained prepared or dynamic vertices reused without frame upload.
    pub const fn reused_vertex_count(self) -> usize {
        self.reused_vertex_count
    }

    /// Returns bytes uploaded for streaming vertices and per-item uniforms.
    pub const fn upload_bytes(self) -> usize {
        self.upload_bytes
    }

    /// Returns the subset uploaded for streaming geometry or instances.
    pub const fn streaming_upload_bytes(self) -> usize {
        self.streaming_upload_bytes
    }

    /// Returns nominal retained texel-storage bytes referenced by frame items.
    ///
    /// Repeated image, scalar-field, and render-target identities are
    /// deduplicated. Opaque backend alignment, tiling, page allocation, and
    /// metadata are excluded because wgpu does not expose them. Color-map LUT
    /// bytes follow the renderer's one-entry cache:
    /// adjacent equal LUTs share one allocation, while an `A, B, A` sequence
    /// keeps three views/allocations alive until submission.
    pub const fn texture_bytes(self) -> usize {
        self.texture_bytes
    }

    /// Returns CPU recovery or source bytes held by unique referenced resources.
    ///
    /// Drawing the same scene, buffer, image, atlas, or run more than once does
    /// not multiply this value. Distinct retained resources are summed even when
    /// their contents happen to be equal.
    pub const fn retained_cpu_bytes(self) -> usize {
        self.retained_cpu_bytes
    }

    /// Returns GPU vertex or instance-buffer bytes held by unique referenced resources.
    pub const fn retained_buffer_bytes(self) -> usize {
        self.retained_buffer_bytes
    }

    /// Returns the conservative number of scheduled draw calls.
    pub const fn draw_calls(self) -> usize {
        self.draw_calls
    }

    /// Returns accepted ordered inputs grouped by renderer source path.
    pub const fn source_counts(self) -> FrameSourceStatistics {
        self.source_counts
    }

    fn adding(self, other: Self) -> Self {
        Self {
            pass_count: self.pass_count.saturating_add(other.pass_count),
            command_count: self.command_count.saturating_add(other.command_count),
            vertex_count: self.vertex_count.saturating_add(other.vertex_count),
            streaming_vertex_count: self
                .streaming_vertex_count
                .saturating_add(other.streaming_vertex_count),
            reused_vertex_count: self
                .reused_vertex_count
                .saturating_add(other.reused_vertex_count),
            upload_bytes: self.upload_bytes.saturating_add(other.upload_bytes),
            streaming_upload_bytes: self
                .streaming_upload_bytes
                .saturating_add(other.streaming_upload_bytes),
            texture_bytes: self.texture_bytes.saturating_add(other.texture_bytes),
            retained_cpu_bytes: self
                .retained_cpu_bytes
                .saturating_add(other.retained_cpu_bytes),
            retained_buffer_bytes: self
                .retained_buffer_bytes
                .saturating_add(other.retained_buffer_bytes),
            draw_calls: self.draw_calls.saturating_add(other.draw_calls),
            source_counts: self.source_counts.adding(other.source_counts),
        }
    }

    fn without_uploads(mut self) -> Self {
        self.upload_bytes = 0;
        self.streaming_upload_bytes = 0;
        self
    }
}

/// Outcome and diagnostics for one composed frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameReport {
    status: RenderStatus,
    statistics: FrameStatistics,
    metrics: RendererFrameMetrics,
    gpu_timing_id: Option<GpuTimingId>,
}

impl FrameReport {
    /// Returns whether the frame was drawn or temporarily skipped.
    pub const fn status(self) -> RenderStatus {
        self.status
    }

    /// Returns bounded work referenced by the frame.
    pub const fn statistics(self) -> FrameStatistics {
        self.statistics
    }

    /// Returns CPU-side timing and tessellation diagnostics.
    pub const fn metrics(self) -> RendererFrameMetrics {
        self.metrics
    }

    /// Correlation key for this submitted pass's optional asynchronous GPU time.
    /// Skipped frames never reserve a key. `None` also covers disabled/unavailable
    /// timing or a full diagnostics ring; inspect renderer timing statistics.
    pub const fn gpu_timing_id(self) -> Option<GpuTimingId> {
        self.gpu_timing_id
    }

    /// Returns command encoders submitted by this composed frame.
    pub const fn command_encoder_count(self) -> usize {
        match self.status {
            RenderStatus::Drawn => 1,
            RenderStatus::Skipped(_) => 0,
        }
    }

    /// Returns GPU render passes encoded by this composed frame.
    pub const fn render_pass_count(self) -> usize {
        match self.status {
            RenderStatus::Drawn => 1,
            RenderStatus::Skipped(_) => 0,
        }
    }

    /// Returns queue submissions performed by this composed frame.
    pub const fn queue_submission_count(self) -> usize {
        match self.status {
            RenderStatus::Drawn => 1,
            RenderStatus::Skipped(_) => 0,
        }
    }

    /// Returns surface presents performed by this composed frame.
    pub const fn surface_present_count(self) -> usize {
        match self.status {
            RenderStatus::Drawn => 1,
            RenderStatus::Skipped(_) => 0,
        }
    }
}

/// One bounded, stably ordered surface frame under construction.
///
/// Calling [`FrameComposer::present`] performs all fallible geometry and
/// ownership validation before acquiring the surface. A successful frame uses
/// one clear, one command encoder, one queue submission, and one present.
#[must_use = "a frame composer does no rendering until present is called"]
pub struct FrameComposer<'frame> {
    renderer: &'frame mut WgpuRenderer,
    background: Color,
    budget: FrameBudget,
    items: Vec<FrameItem<'frame>>,
    retained_resources: Vec<RetainedResourceKey>,
    scalar_luts: Vec<ScalarLutPlan>,
    scalar_lut_upload_count: usize,
    scalar_lut_allocation_count: usize,
    planned: FrameStatistics,
    next_insertion: usize,
    cache: FrameCache,
}

impl Drop for FrameComposer<'_> {
    fn drop(&mut self) {
        self.cache.items = cache::recycle_items(std::mem::take(&mut self.items));
        self.retained_resources.clear();
        self.scalar_luts.clear();
        self.cache.retained_resources = std::mem::take(&mut self.retained_resources);
        self.cache.scalar_luts = std::mem::take(&mut self.scalar_luts);
        self.cache.finish();
        self.renderer.frame_cache = std::mem::take(&mut self.cache);
    }
}

struct ScalarLutPlan {
    sort_key: (i32, usize),
    lut: [u8; COLOR_MAP_LUT_SIZE as usize * 4],
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct RetainedResourceKey {
    address: usize,
    class: u8,
}

#[derive(Debug, Clone, Copy)]
struct RetainedResourceAccounting {
    key: RetainedResourceKey,
    cpu_bytes: usize,
    buffer_bytes: usize,
    texture_bytes: usize,
}

impl RetainedResourceAccounting {
    const fn new(
        key: RetainedResourceKey,
        cpu_bytes: usize,
        buffer_bytes: usize,
        texture_bytes: usize,
    ) -> Self {
        Self {
            key,
            cpu_bytes,
            buffer_bytes,
            texture_bytes,
        }
    }
}

fn retained_key<T>(resource: &T, class: u8) -> RetainedResourceKey {
    RetainedResourceKey {
        address: std::ptr::from_ref(resource).cast::<()>() as usize,
        class,
    }
}

fn retained_arc_key<T>(resource: &Arc<T>, class: u8) -> RetainedResourceKey {
    RetainedResourceKey {
        address: Arc::as_ptr(resource).cast::<()>() as usize,
        class,
    }
}

fn account_new_retained_resources(
    existing: &[RetainedResourceKey],
    resources: &[RetainedResourceAccounting],
    mut work: FrameStatistics,
) -> (FrameStatistics, usize) {
    let mut missing = 0usize;
    for (index, resource) in resources.iter().enumerate() {
        let already_in_request = resources[..index]
            .iter()
            .any(|previous| previous.key == resource.key);
        if !already_in_request && !existing.contains(&resource.key) {
            missing = missing.saturating_add(1);
            work.retained_cpu_bytes = work.retained_cpu_bytes.saturating_add(resource.cpu_bytes);
            work.retained_buffer_bytes = work
                .retained_buffer_bytes
                .saturating_add(resource.buffer_bytes);
            work.texture_bytes = work.texture_bytes.saturating_add(resource.texture_bytes);
        }
    }
    (work, missing)
}

fn scalar_lut_counts_with_inserted(
    planned: &[ScalarLutPlan],
    insertion: usize,
    inserted: &[u8; COLOR_MAP_LUT_SIZE as usize * 4],
    initial_cache: Option<&[u8; COLOR_MAP_LUT_SIZE as usize * 4]>,
) -> (usize, usize) {
    debug_assert!(insertion <= planned.len());
    let mut previous: Option<&[u8; COLOR_MAP_LUT_SIZE as usize * 4]> = None;
    let mut allocation_count = 0usize;
    let mut first_matches_initial = false;
    for merged_index in 0..=planned.len() {
        let current = if merged_index == insertion {
            inserted
        } else {
            let source_index = merged_index - usize::from(merged_index > insertion);
            &planned[source_index].lut
        };
        if previous.is_none() {
            first_matches_initial = initial_cache == Some(current);
        }
        if previous != Some(current) {
            allocation_count = allocation_count.saturating_add(1);
        }
        previous = Some(current);
    }
    let upload_count = allocation_count.saturating_sub(usize::from(first_matches_initial));
    (upload_count, allocation_count)
}

enum FrameItem<'frame> {
    Scene {
        scene: &'frame Scene,
        camera: Camera2d,
        options: FramePassOptions,
        insertion: usize,
    },
    ScreenScene {
        scene: &'frame ScreenScene,
        options: FramePassOptions,
        insertion: usize,
    },
    Prepared {
        scene: &'frame PreparedScene,
        camera: Camera2d,
        options: FramePassOptions,
        insertion: usize,
    },
    PreparedScreen {
        scene: &'frame PreparedScreenScene,
        options: FramePassOptions,
        insertion: usize,
    },
    ScreenPrimitives {
        batch: &'frame super::ScreenPrimitiveBatch2d,
        options: FramePassOptions,
        insertion: usize,
    },
    Dynamic {
        mesh: &'frame DynamicMesh2d,
        camera: Camera2d,
        options: FramePassOptions,
        insertion: usize,
    },
    Particle {
        field: &'frame mut ParticleField2d,
        camera: Camera2d,
        options: FramePassOptions,
        insertion: usize,
    },
    Scalar {
        texture: &'frame ScalarFieldTexture,
        color_map: &'frame ColorMap,
        minimum: f32,
        maximum: f32,
        value_extent: f32,
        sampling: ScalarFieldSampling,
        options: FramePassOptions,
        insertion: usize,
    },
    Image {
        image: &'frame Image2d,
        source: ImageTexelRect,
        tint: Color,
        sampling: ImageSampling,
        options: FramePassOptions,
        insertion: usize,
    },
    WorldImage {
        image: &'frame Image2d,
        source: ImageTexelRect,
        rectangle: Rect,
        depth: f32,
        camera: Camera2d,
        tint: Color,
        sampling: ImageSampling,
        options: FramePassOptions,
        insertion: usize,
    },
    ImageBatch {
        image: &'frame Image2d,
        batch: &'frame ImageBatch2d,
        spans: Option<&'frame [GlyphBatchSpan]>,
        placement: ImageBatchPlacement,
        sampling: ImageSampling,
        options: FramePassOptions,
        insertion: usize,
    },
    Target {
        target: &'frame RenderTarget2d,
        blend_mode: BlendMode,
        opacity: f32,
        options: FramePassOptions,
        insertion: usize,
    },
}

impl FrameItem<'_> {
    fn sort_key(&self) -> (i32, usize) {
        match self {
            Self::Scene {
                options, insertion, ..
            }
            | Self::ScreenScene {
                options, insertion, ..
            }
            | Self::Prepared {
                options, insertion, ..
            }
            | Self::PreparedScreen {
                options, insertion, ..
            }
            | Self::ScreenPrimitives {
                options, insertion, ..
            }
            | Self::Dynamic {
                options, insertion, ..
            }
            | Self::Particle {
                options, insertion, ..
            }
            | Self::Scalar {
                options, insertion, ..
            }
            | Self::Image {
                options, insertion, ..
            }
            | Self::WorldImage {
                options, insertion, ..
            }
            | Self::ImageBatch {
                options, insertion, ..
            }
            | Self::Target {
                options, insertion, ..
            } => (options.order(), *insertion),
        }
    }
}

impl WgpuRenderer {
    /// Begins one bounded heterogeneous frame with a single clear color.
    pub fn begin_frame(
        &mut self,
        background: Color,
        budget: FrameBudget,
    ) -> Result<FrameComposer<'_>, FrameComposerError> {
        if !background.is_normalized() {
            return Err(FrameComposerError::InvalidBackground);
        }
        let mut cache = std::mem::take(&mut self.frame_cache);
        cache.begin();
        let items = std::mem::take(&mut cache.items);
        let retained_resources = std::mem::take(&mut cache.retained_resources);
        let scalar_luts = std::mem::take(&mut cache.scalar_luts);
        Ok(FrameComposer {
            renderer: self,
            background,
            budget,
            items,
            retained_resources,
            scalar_luts,
            scalar_lut_upload_count: 0,
            scalar_lut_allocation_count: 0,
            planned: FrameStatistics::default(),
            next_insertion: 0,
            cache,
        })
    }
}

#[derive(Debug, Clone, Copy)]
struct ResolvedViewport {
    viewport: LogicalViewport,
    origin: Vec2,
    scissor: ScissorRect,
    item_clip: Option<ScissorRect>,
    item_clipped_out: bool,
}

enum ReadySource<'frame> {
    Streaming,
    Prepared(&'frame wgpu::Buffer),
    Dynamic(&'frame wgpu::Buffer),
    ScreenPrimitives(&'frame wgpu::Buffer),
}

struct ReadyGeometry<'frame> {
    source: ReadySource<'frame>,
    vertex_count: usize,
    batches: Vec<PreparedDrawBatch>,
    camera_uniform: CameraUniform,
    viewport: ResolvedViewport,
}

enum ReadyItem<'frame> {
    Geometry(ReadyGeometry<'frame>),
    Particle {
        field: &'frame mut ParticleField2d,
        visible_count: usize,
        pending_statistics: ParticleStatistics,
        initial_cpu_allocation_bytes: usize,
        camera_uniform: CameraUniform,
        viewport: ResolvedViewport,
    },
    Scalar {
        texture: &'frame ScalarFieldTexture,
        color_map: &'frame ColorMap,
        uniform: HeatmapUniform,
        viewport: ResolvedViewport,
    },
    Image {
        image: &'frame Image2d,
        sampling: ImageSampling,
        uniform: ImageUniform,
        viewport: ResolvedViewport,
    },
    ImageBatch {
        image: &'frame Image2d,
        batch: &'frame ImageBatch2d,
        spans: Option<&'frame [GlyphBatchSpan]>,
        sampling: ImageSampling,
        uniform: ImageUniform,
        viewport: ResolvedViewport,
    },
    Target {
        target: &'frame RenderTarget2d,
        blend_mode: BlendMode,
        uniform: CompositeUniform,
        viewport: ResolvedViewport,
    },
}

#[derive(Clone)]
struct FrameBinding {
    _buffer: wgpu::Buffer,
    bind_group: wgpu::BindGroup,
}

fn preflight_frame_items(
    renderer: &WgpuRenderer,
    target_viewport: LogicalViewport,
    items: &[FrameItem<'_>],
    preparation: &mut RendererPreparationMetrics,
) -> Result<(), FrameComposerError> {
    for item in items {
        let retained_image = matches!(
            item,
            FrameItem::Image { .. } | FrameItem::WorldImage { .. } | FrameItem::ImageBatch { .. }
        );
        let retained_started = Instant::now();
        match item {
            FrameItem::Scene {
                camera, options, ..
            } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                CameraUniform::new_in_region(
                    *camera,
                    viewport.viewport,
                    viewport.origin,
                    target_viewport,
                )
                .ok_or(RendererFrameError::InvalidGeometryTransform)?;
            }
            FrameItem::ScreenScene { options, .. } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let camera = screen_camera(viewport.viewport)
                    .map_err(|_| RendererFrameError::InvalidGeometryTransform)?;
                CameraUniform::new_in_region(
                    camera,
                    viewport.viewport,
                    viewport.origin,
                    target_viewport,
                )
                .ok_or(RendererFrameError::InvalidGeometryTransform)?;
            }
            FrameItem::ScreenPrimitives { batch, options, .. } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                primitives::validate(
                    batch,
                    viewport,
                    target_viewport,
                    renderer.scale_factor as f32,
                    preparation,
                )?;
            }
            FrameItem::Prepared {
                scene,
                camera,
                options,
                ..
            } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let uniform = CameraUniform::new_in_region(
                    *camera,
                    viewport.viewport,
                    viewport.origin,
                    target_viewport,
                )
                .ok_or(RendererFrameError::InvalidGeometryTransform)?;
                let validation_started = Instant::now();
                let safe = geometry_is_safe_for_cached(
                    Some(&scene.geometry_validation_cache),
                    scene.geometry_extents,
                    GeometryValidationSource::Tessellated(&scene.vertices),
                    uniform,
                );
                preparation.geometry_validation += validation_started.elapsed();
                if !safe {
                    return Err(RendererFrameError::InvalidGeometryTransform.into());
                }
            }
            FrameItem::PreparedScreen { scene, options, .. } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let camera = screen_camera(viewport.viewport)
                    .map_err(|_| RendererFrameError::InvalidGeometryTransform)?;
                let uniform = CameraUniform::new_in_region(
                    camera,
                    viewport.viewport,
                    viewport.origin,
                    target_viewport,
                )
                .ok_or(RendererFrameError::InvalidGeometryTransform)?;
                let validation_started = Instant::now();
                let safe = geometry_is_safe_for_cached(
                    Some(&scene.scene.geometry_validation_cache),
                    scene.scene.geometry_extents,
                    GeometryValidationSource::Tessellated(&scene.scene.vertices),
                    uniform,
                );
                preparation.geometry_validation += validation_started.elapsed();
                if !safe {
                    return Err(RendererFrameError::InvalidGeometryTransform.into());
                }
            }
            FrameItem::Dynamic {
                mesh,
                camera,
                options,
                ..
            } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let uniform = CameraUniform::new_in_region(
                    *camera,
                    viewport.viewport,
                    viewport.origin,
                    target_viewport,
                )
                .ok_or(RendererFrameError::InvalidGeometryTransform)?;
                let validation_started = Instant::now();
                let safe = geometry_is_safe_for_cached(
                    Some(&mesh.geometry_validation_cache),
                    mesh.geometry_extents,
                    GeometryValidationSource::Dynamic(&mesh.vertices),
                    uniform,
                );
                preparation.geometry_validation += validation_started.elapsed();
                if !safe {
                    return Err(RendererFrameError::InvalidGeometryTransform.into());
                }
            }
            FrameItem::Particle {
                camera, options, ..
            } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                CameraUniform::new(*camera, viewport.viewport)
                    .ok_or(RendererFrameError::InvalidGeometryTransform)?;
                CameraUniform::new_in_region(
                    *camera,
                    viewport.viewport,
                    viewport.origin,
                    target_viewport,
                )
                .ok_or(RendererFrameError::InvalidGeometryTransform)?;
            }
            FrameItem::Scalar {
                texture,
                minimum,
                maximum,
                value_extent,
                sampling,
                options,
                ..
            } => {
                if !scalar_normalization_is_portable(texture, *minimum, *value_extent) {
                    return Err(FrameComposerError::InvalidValueRange {
                        minimum: *minimum,
                        maximum: *maximum,
                    });
                }
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let region = LogicalViewportRegion::new(
                    LogicalScreenPosition::from_vec2(viewport.origin),
                    viewport.viewport,
                )
                .map_err(|_| RendererFrameError::InvalidViewport)?;
                HeatmapUniform::new(
                    *minimum,
                    *value_extent,
                    texture.width(),
                    texture.height(),
                    *sampling,
                )
                .in_region(region, target_viewport)
                .ok_or(RendererFrameError::InvalidGeometryTransform)?;
            }
            FrameItem::Image { options, .. } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let region = LogicalViewportRegion::new(
                    LogicalScreenPosition::from_vec2(viewport.origin),
                    viewport.viewport,
                )
                .map_err(|_| RendererFrameError::InvalidViewport)?;
                CompositeUniform::in_region(1.0, region, target_viewport)
                    .ok_or(RendererFrameError::InvalidGeometryTransform)?;
            }
            FrameItem::Target {
                opacity, options, ..
            } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let region = LogicalViewportRegion::new(
                    LogicalScreenPosition::from_vec2(viewport.origin),
                    viewport.viewport,
                )
                .map_err(|_| RendererFrameError::InvalidViewport)?;
                CompositeUniform::in_region(*opacity, region, target_viewport)
                    .ok_or(RendererFrameError::InvalidGeometryTransform)?;
            }
            FrameItem::WorldImage {
                rectangle,
                depth,
                camera,
                options,
                ..
            } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let camera_uniform = CameraUniform::new_in_region(
                    *camera,
                    viewport.viewport,
                    viewport.origin,
                    target_viewport,
                )
                .ok_or(RendererFrameError::InvalidGeometryTransform)?;
                let minimum = rectangle.min();
                let maximum = rectangle.max();
                for world in [
                    Vec2::new(minimum.x, maximum.y),
                    maximum,
                    minimum,
                    Vec2::new(maximum.x, minimum.y),
                ] {
                    let screen = camera_uniform.world_to_screen(world, *depth);
                    let clip = Vec2::new(
                        screen.x.mul_add(
                            camera_uniform.screen_to_clip[0],
                            camera_uniform.screen_to_clip[2],
                        ),
                        screen.y.mul_add(
                            camera_uniform.screen_to_clip[1],
                            camera_uniform.screen_to_clip[3],
                        ),
                    );
                    if ![clip.x, clip.y].into_iter().all(is_portable_shader_source) {
                        return Err(RendererFrameError::InvalidGeometryTransform.into());
                    }
                }
            }
            FrameItem::ImageBatch {
                batch,
                options,
                placement,
                ..
            } => {
                let viewport = resolve_viewport(renderer, target_viewport, *options)?;
                let uniform = image::batch_uniform(target_viewport, viewport.origin, *placement)?;
                if !batch.sprites_are_safe_for_target(
                    Vec2::new(uniform.uv_rect[0], uniform.uv_rect[1]),
                    uniform.destination,
                ) {
                    return Err(RendererFrameError::InvalidGeometryTransform.into());
                }
            }
        }
        if retained_image {
            preparation.retained_draws += retained_started.elapsed();
        }
    }
    Ok(())
}

#[cfg(test)]
fn image_sprites_are_safe_for_target(
    sprites: &[ImageSprite2d],
    viewport_origin: Vec2,
    clip_transform: [f32; 4],
) -> bool {
    image::sprites_are_safe_for_target(sprites, viewport_origin, clip_transform)
}

fn set_particle_rendered(items: &mut [ReadyItem<'_>], presented: bool) {
    for item in items {
        if let ReadyItem::Particle {
            field,
            visible_count,
            pending_statistics,
            viewport,
            ..
        } = item
        {
            field.statistics = ParticleStatistics {
                rendered: if presented && !viewport.item_clipped_out {
                    *visible_count
                } else {
                    0
                },
                ..*pending_statistics
            };
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn prepare_particle_item<'frame>(
    field: &'frame mut ParticleField2d,
    camera: Camera2d,
    viewport: ResolvedViewport,
    target_viewport: LogicalViewport,
    ready: &mut Vec<ReadyItem<'frame>>,
    statistics: &mut FrameStatistics,
) -> Result<(), FrameComposerError> {
    let initial_cpu_allocation_bytes = field.cpu_allocation_bytes();
    let local_uniform = CameraUniform::new(camera, viewport.viewport)
        .ok_or(RendererFrameError::InvalidGeometryTransform)?;
    let target_uniform =
        CameraUniform::new_in_region(camera, viewport.viewport, viewport.origin, target_viewport)
            .ok_or(RendererFrameError::InvalidGeometryTransform)?;
    let instance_count = field.instances.len();
    let visibility_checked = instance_count.min(field.budget.max_visibility_checks_per_frame);

    let (visible_count, selected_count) = if visibility_checked < instance_count {
        field.visible_instances.clear();
        validate_particle_staging_capacity(&field.visible_instances, visibility_checked)?;
        for candidate_index in 0..visibility_checked {
            let source_index =
                uniformly_sampled_index(candidate_index, instance_count, visibility_checked);
            let instance = field.instances[source_index];
            if !instance.is_safe_for(target_uniform, target_viewport) {
                return Err(RendererFrameError::InvalidGeometryTransform.into());
            }
            let Some(intersects) =
                instance.validated_viewport_intersection(local_uniform, viewport.viewport)
            else {
                return Err(RendererFrameError::InvalidGeometryTransform.into());
            };
            if intersects {
                field.visible_instances.push(instance);
            }
        }
        let visible_count = field.visible_instances.len();
        let selected_count = visible_count.min(field.budget.instance_limit());
        if selected_count < visible_count {
            let mut visible_index = 0;
            field.visible_instances.retain(|_| {
                let selected = particle_visible_index_is_selected(
                    visible_index,
                    visible_count,
                    selected_count,
                );
                visible_index += 1;
                selected
            });
        }
        (visible_count, selected_count)
    } else {
        let visible_count = visible_particle_count_for_frame(
            &field.instances,
            local_uniform,
            target_uniform,
            viewport.viewport,
            target_viewport,
        )?;
        let selected_count = visible_count.min(field.budget.instance_limit());
        if selected_count == field.instances.len() {
            field.visible_instances.clear();
        } else {
            field.visible_instances.clear();
            validate_particle_staging_capacity(&field.visible_instances, selected_count)?;
            let mut visible_index = 0;
            for instance in field.instances.iter().copied() {
                if !instance.intersects_viewport(local_uniform, viewport.viewport) {
                    continue;
                }
                let selected_index = field.visible_instances.len();
                if selected_index < selected_count
                    && particle_visible_index_is_selected(
                        visible_index,
                        visible_count,
                        selected_count,
                    )
                {
                    field.visible_instances.push(instance);
                }
                visible_index += 1;
            }
        }
        (visible_count, selected_count)
    };
    let pending_statistics = particle_statistics_with_budget(
        instance_count,
        visibility_checked,
        visible_count,
        selected_count,
        0,
    );
    *statistics = statistics.adding(FrameStatistics {
        pass_count: 1,
        command_count: usize::from(instance_count > 0),
        vertex_count: selected_count.saturating_mul(6),
        streaming_vertex_count: selected_count.saturating_mul(6),
        reused_vertex_count: 0,
        upload_bytes: selected_count
            .saturating_mul(std::mem::size_of::<ParticleGpu>())
            .saturating_add(std::mem::size_of::<CameraUniform>()),
        streaming_upload_bytes: selected_count.saturating_mul(std::mem::size_of::<ParticleGpu>()),
        texture_bytes: 0,
        retained_cpu_bytes: 0,
        retained_buffer_bytes: 0,
        draw_calls: usize::from(selected_count > 0),
        source_counts: FrameSourceStatistics::default(),
    });
    ready.push(ReadyItem::Particle {
        field,
        visible_count: selected_count,
        pending_statistics,
        initial_cpu_allocation_bytes,
        camera_uniform: target_uniform,
        viewport,
    });
    Ok(())
}

fn visible_particle_count_for_frame(
    instances: &[ParticleGpu],
    local_camera: CameraUniform,
    target_camera: CameraUniform,
    viewport: LogicalViewport,
    target_viewport: LogicalViewport,
) -> Result<usize, RendererFrameError> {
    let mut visible = 0;
    for instance in instances {
        let Some(intersects) = instance.validated_viewport_intersection(local_camera, viewport)
        else {
            return Err(RendererFrameError::InvalidGeometryTransform);
        };
        if !instance.is_safe_for(target_camera, target_viewport) {
            return Err(RendererFrameError::InvalidGeometryTransform);
        }
        visible += usize::from(intersects);
    }
    Ok(visible)
}

// Keep grown scratch reachable on every structured failure, both for reuse
// and for the cache's conservative owned-capacity peak accounting.
fn with_streaming_batches(
    pool: &mut Vec<Vec<PreparedDrawBatch>>,
    prepare: impl FnOnce(&mut Vec<PreparedDrawBatch>) -> Result<(), FrameComposerError>,
) -> Result<(), FrameComposerError> {
    let mut batches = pool.pop().unwrap_or_default();
    let result = prepare(&mut batches);
    if batches.capacity() > 0 {
        batches.clear();
        pool.push(batches);
    }
    result
}

#[allow(clippy::too_many_arguments)]
fn prepare_streaming_scene<'frame>(
    scene: &'frame Scene,
    camera: Camera2d,
    options: FramePassOptions,
    renderer: &WgpuRenderer,
    target_viewport: LogicalViewport,
    streaming_vertices: &mut Vec<Vertex>,
    ready: &mut Vec<ReadyItem<'frame>>,
    statistics: &mut FrameStatistics,
    aggregate: &mut TessellationStats,
    batches: &mut Vec<PreparedDrawBatch>,
    proofs: &mut streaming::StreamingSceneProofs<'frame>,
) -> Result<(), FrameComposerError> {
    let viewport = resolve_viewport(renderer, target_viewport, options)?;
    let camera_uniform =
        CameraUniform::new_in_region(camera, viewport.viewport, viewport.origin, target_viewport)
            .ok_or(RendererFrameError::InvalidGeometryTransform)?;
    if !scene_estimate_fits_streaming_device(
        scene,
        &renderer.device,
        streaming_vertices.len(),
        renderer.vertex_capacity,
    ) {
        return Err(RendererFrameError::GeometryCapacityTooLarge.into());
    }
    proofs.prepare(
        scene,
        camera_uniform,
        viewport,
        streaming_vertices,
        ready,
        statistics,
        aggregate,
        batches,
    )
}

#[allow(clippy::too_many_arguments)]
fn prepare_streaming_scene_resolved<'frame>(
    scene: &Scene,
    camera_uniform: CameraUniform,
    viewport: ResolvedViewport,
    streaming_vertices: &mut Vec<Vertex>,
    ready: &mut Vec<ReadyItem<'frame>>,
    statistics: &mut FrameStatistics,
    aggregate: &mut TessellationStats,
    batches: &mut Vec<PreparedDrawBatch>,
    preparation: &mut RendererPreparationMetrics,
) -> Result<(), FrameComposerError> {
    let vertex_start = streaming_vertices.len();
    let tessellation_started = Instant::now();
    let stats =
        tessellate_scene(scene, streaming_vertices, batches).map_err(RendererFrameError::from)?;
    preparation.tessellation += tessellation_started.elapsed();
    let validation_started = Instant::now();
    let vertices = &streaming_vertices[vertex_start..];
    let extents = GeometryExtents::from_vertices(vertices);
    if !geometry_is_safe_for(
        extents,
        GeometryValidationSource::Tessellated(vertices),
        camera_uniform,
    ) {
        streaming_vertices.truncate(vertex_start);
        return Err(RendererFrameError::InvalidGeometryTransform.into());
    }
    preparation.geometry_validation += validation_started.elapsed();
    *statistics = statistics.adding(FrameStatistics {
        pass_count: 1,
        command_count: scene.command_count(),
        vertex_count: stats.vertex_count,
        streaming_vertex_count: stats.vertex_count,
        reused_vertex_count: 0,
        upload_bytes: stats
            .upload_bytes
            .saturating_add(std::mem::size_of::<CameraUniform>()),
        streaming_upload_bytes: stats.upload_bytes,
        texture_bytes: 0,
        retained_cpu_bytes: 0,
        retained_buffer_bytes: 0,
        draw_calls: batches.len(),
        source_counts: FrameSourceStatistics::default(),
    });
    add_tessellation_stats(aggregate, stats);
    ready.push(ReadyItem::Geometry(ReadyGeometry {
        source: ReadySource::Streaming,
        vertex_count: vertices.len(),
        batches: std::mem::take(batches),
        camera_uniform,
        viewport,
    }));
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn prepare_retained_geometry<'frame>(
    source: ReadySource<'frame>,
    vertex_count: usize,
    extents: GeometryExtents,
    geometry_validation: GeometryValidationSource<'_>,
    geometry_validation_cache: Option<&GeometryValidationCache>,
    batches: &[PreparedDrawBatch],
    command_count: usize,
    source_stats: TessellationStats,
    camera: Camera2d,
    viewport: ResolvedViewport,
    target_viewport: LogicalViewport,
    ready: &mut Vec<ReadyItem<'frame>>,
    statistics: &mut FrameStatistics,
    aggregate: &mut TessellationStats,
    mut owned_batches: Vec<PreparedDrawBatch>,
    preparation: &mut RendererPreparationMetrics,
) -> Result<(), FrameComposerError> {
    let camera_uniform =
        CameraUniform::new_in_region(camera, viewport.viewport, viewport.origin, target_viewport)
            .ok_or(RendererFrameError::InvalidGeometryTransform)?;
    let validation_started = Instant::now();
    let geometry_is_safe = geometry_is_safe_for_cached(
        geometry_validation_cache,
        extents,
        geometry_validation,
        camera_uniform,
    );
    preparation.geometry_validation += validation_started.elapsed();
    if !geometry_is_safe {
        return Err(RendererFrameError::InvalidGeometryTransform.into());
    }
    owned_batches
        .try_reserve(batches.len())
        .map_err(|_| FrameComposerError::AllocationFailed {
            requested_bytes: batches
                .len()
                .saturating_mul(std::mem::size_of::<PreparedDrawBatch>()),
        })?;
    owned_batches.extend_from_slice(batches);
    *statistics = statistics.adding(FrameStatistics {
        pass_count: 1,
        command_count,
        vertex_count,
        streaming_vertex_count: 0,
        reused_vertex_count: vertex_count,
        upload_bytes: std::mem::size_of::<CameraUniform>(),
        streaming_upload_bytes: 0,
        texture_bytes: 0,
        retained_cpu_bytes: 0,
        retained_buffer_bytes: 0,
        draw_calls: owned_batches.len(),
        source_counts: FrameSourceStatistics::default(),
    });
    add_tessellation_stats(aggregate, source_stats);
    ready.push(ReadyItem::Geometry(ReadyGeometry {
        source,
        vertex_count,
        batches: owned_batches,
        camera_uniform,
        viewport,
    }));
    Ok(())
}

fn resolve_viewport(
    renderer: &WgpuRenderer,
    target: LogicalViewport,
    options: FramePassOptions,
) -> Result<ResolvedViewport, FrameComposerError> {
    let (viewport, origin) = match options.viewport() {
        Some(region) => (region.viewport(), region.origin().to_vec2()),
        None => (target, Vec2::ZERO),
    };
    let max = origin + viewport.size();
    if !origin.is_finite()
        || !max.is_finite()
        || origin.x < 0.0
        || origin.y < 0.0
        || max.x > target.width()
        || max.y > target.height()
    {
        return Err(RendererFrameError::InvalidViewport.into());
    }
    let scissor = logical_viewport_scissor(
        origin,
        viewport,
        renderer.scale_factor as f32,
        renderer.config.width,
        renderer.config.height,
    )
    .ok_or(RendererFrameError::InvalidViewport)?;
    let (item_clip, item_clipped_out) = match options.clip() {
        Some(clip) => {
            let resolved = screen_clip_to_scissor(clip, viewport, renderer.scale_factor as f32)
                .and_then(|clip| offset_scissor(clip, scissor));
            (resolved, resolved.is_none())
        }
        None => (None, false),
    };
    Ok(ResolvedViewport {
        viewport,
        origin,
        scissor,
        item_clip,
        item_clipped_out,
    })
}

fn create_frame_binding(renderer: &mut WgpuRenderer, item: &ReadyItem<'_>) -> FrameBinding {
    match item {
        ReadyItem::Geometry(geometry) => create_camera_binding(renderer, &geometry.camera_uniform),
        ReadyItem::Particle { camera_uniform, .. } => {
            create_camera_binding(renderer, camera_uniform)
        }
        ReadyItem::Scalar {
            texture,
            color_map,
            uniform,
            ..
        } => {
            let color_map_view = renderer.color_map_view(color_map);
            let scalar_view = texture
                .texture
                .create_view(&wgpu::TextureViewDescriptor::default());
            let bytes = bytemuck::bytes_of(uniform);
            let buffer = renderer.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("sim-engine frame heatmap uniform"),
                size: bytes.len() as wgpu::BufferAddress,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            renderer.queue.write_buffer(&buffer, 0, bytes);
            let bind_group = renderer
                .device
                .create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some("sim-engine frame heatmap bind group"),
                    layout: &renderer.heatmap_bind_group_layout,
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: wgpu::BindingResource::TextureView(&scalar_view),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: wgpu::BindingResource::TextureView(&color_map_view),
                        },
                        wgpu::BindGroupEntry {
                            binding: 2,
                            resource: buffer.as_entire_binding(),
                        },
                    ],
                });
            FrameBinding {
                _buffer: buffer,
                bind_group,
            }
        }
        ReadyItem::Image {
            image,
            sampling,
            uniform,
            ..
        }
        | ReadyItem::ImageBatch {
            image,
            sampling,
            uniform,
            ..
        } => {
            let bytes = bytemuck::bytes_of(uniform);
            let buffer = renderer.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("sim-engine frame image uniform"),
                size: bytes.len() as wgpu::BufferAddress,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            renderer.queue.write_buffer(&buffer, 0, bytes);
            let bind_group = renderer
                .device
                .create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some("sim-engine frame image bind group"),
                    layout: &renderer.image_renderer.bind_group_layout,
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: wgpu::BindingResource::TextureView(&image.view),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: wgpu::BindingResource::Sampler(
                                renderer.image_renderer.sampler(*sampling),
                            ),
                        },
                        wgpu::BindGroupEntry {
                            binding: 2,
                            resource: buffer.as_entire_binding(),
                        },
                    ],
                });
            FrameBinding {
                _buffer: buffer,
                bind_group,
            }
        }
        ReadyItem::Target {
            target, uniform, ..
        } => {
            let bytes = bytemuck::bytes_of(uniform);
            let buffer = renderer.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("sim-engine frame composition uniform"),
                size: bytes.len() as wgpu::BufferAddress,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            renderer.queue.write_buffer(&buffer, 0, bytes);
            let bind_group = renderer
                .device
                .create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some("sim-engine frame composition bind group"),
                    layout: &renderer.composition_pipelines.bind_group_layout,
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: wgpu::BindingResource::TextureView(&target.view),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: buffer.as_entire_binding(),
                        },
                    ],
                });
            FrameBinding {
                _buffer: buffer,
                bind_group,
            }
        }
    }
}

fn ready_uniform_bytes(item: &ReadyItem<'_>) -> usize {
    match item {
        ReadyItem::Geometry(_) | ReadyItem::Particle { .. } => std::mem::size_of::<CameraUniform>(),
        ReadyItem::Scalar { .. } => std::mem::size_of::<HeatmapUniform>(),
        ReadyItem::Image { .. } | ReadyItem::ImageBatch { .. } => {
            std::mem::size_of::<ImageUniform>()
        }
        ReadyItem::Target { .. } => std::mem::size_of::<CompositeUniform>(),
    }
}

fn create_camera_binding(renderer: &WgpuRenderer, uniform: &CameraUniform) -> FrameBinding {
    let label = "sim-engine frame camera uniform";
    let bytes = bytemuck::bytes_of(uniform);
    let buffer = renderer.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some(label),
        size: bytes.len() as wgpu::BufferAddress,
        usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    renderer.queue.write_buffer(&buffer, 0, bytes);
    let bind_group = renderer
        .device
        .create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("sim-engine frame camera bind group"),
            layout: &renderer.camera_bind_group_layout,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: buffer.as_entire_binding(),
            }],
        });
    FrameBinding {
        _buffer: buffer,
        bind_group,
    }
}

fn effective_scissor(
    viewport: ResolvedViewport,
    source_clip: Option<ScreenClipRect>,
    scale_factor: f32,
) -> Option<ScissorRect> {
    if viewport.item_clipped_out {
        return None;
    }
    let source = match source_clip {
        Some(clip) => screen_clip_to_scissor(clip, viewport.viewport, scale_factor)
            .and_then(|clip| offset_scissor(clip, viewport.scissor))?,
        None => viewport.scissor,
    };
    match viewport.item_clip {
        Some(item) => intersect_scissors(source, item),
        None => Some(source),
    }
}

fn intersect_scissors(first: ScissorRect, second: ScissorRect) -> Option<ScissorRect> {
    let x = first.x.max(second.x);
    let y = first.y.max(second.y);
    let max_x = first
        .x
        .saturating_add(first.width)
        .min(second.x.saturating_add(second.width));
    let max_y = first
        .y
        .saturating_add(first.height)
        .min(second.y.saturating_add(second.height));
    (max_x > x && max_y > y).then_some(ScissorRect {
        x,
        y,
        width: max_x - x,
        height: max_y - y,
    })
}

fn validate_frame_budget(
    budget: FrameBudget,
    statistics: FrameStatistics,
) -> Result<(), FrameComposerError> {
    let work = [
        (
            FrameBudgetResource::Passes,
            budget.max_passes,
            statistics.pass_count,
        ),
        (
            FrameBudgetResource::Commands,
            budget.max_commands,
            statistics.command_count,
        ),
        (
            FrameBudgetResource::Vertices,
            budget.max_vertices,
            statistics.vertex_count,
        ),
        (
            FrameBudgetResource::UploadBytes,
            budget.max_upload_bytes,
            statistics.upload_bytes,
        ),
        (
            FrameBudgetResource::TextureBytes,
            budget.max_texture_bytes,
            statistics.texture_bytes,
        ),
        (
            FrameBudgetResource::DrawCalls,
            budget.max_draw_calls,
            statistics.draw_calls,
        ),
    ];
    for (resource, limit, actual) in work {
        if actual > limit {
            return Err(FrameComposerError::BudgetExceeded {
                resource,
                limit,
                actual,
            });
        }
    }
    Ok(())
}

fn add_tessellation_stats(aggregate: &mut TessellationStats, source: TessellationStats) {
    aggregate.command_count = aggregate.command_count.saturating_add(source.command_count);
    aggregate.rendered_command_count = aggregate
        .rendered_command_count
        .saturating_add(source.rendered_command_count);
    aggregate.dropped_command_count = aggregate
        .dropped_command_count
        .saturating_add(source.dropped_command_count);
    aggregate.command_counts = aggregate.command_counts.adding(source.command_counts);
    aggregate.rendered_counts = aggregate.rendered_counts.adding(source.rendered_counts);
    aggregate.dropped_counts = aggregate.dropped_counts.adding(source.dropped_counts);
    aggregate.vertex_count = aggregate.vertex_count.saturating_add(source.vertex_count);
    aggregate.draw_batch_count = aggregate
        .draw_batch_count
        .saturating_add(source.draw_batch_count);
    aggregate.upload_bytes = aggregate.upload_bytes.saturating_add(source.upload_bytes);
}

#[allow(clippy::too_many_arguments)]
fn frame_report(
    status: RenderStatus,
    statistics: FrameStatistics,
    tessellation: Duration,
    upload: Duration,
    camera_uniform_upload: Duration,
    surface_acquire: Duration,
    encode_submit_present: Duration,
    total_cpu: Duration,
    geometry_reused: bool,
    geometry_streamed: bool,
    tessellation_stats: TessellationStats,
) -> FrameReport {
    let report = render_report(
        status,
        tessellation,
        upload,
        camera_uniform_upload,
        surface_acquire,
        encode_submit_present,
        total_cpu,
        geometry_reused,
        geometry_streamed,
        tessellation_stats,
    );
    FrameReport {
        status: report.status,
        statistics,
        metrics: report.metrics,
        gpu_timing_id: None,
    }
}