euv-engine 0.28.6

A high-performance 2D game engine built on the euv framework, featuring ECS, fixed-timestep game loop, canvas rendering, physics, collision detection, sprite animation, and audio.
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
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
use super::*;

thread_local! {
    static GL_TEX_IMAGE_2D_CACHE: RefCell<Option<Function>> = const { RefCell::new(None) };
}

/// Implements diagnostic helpers on [`WebGl2InitError`].
impl WebGl2InitError {
    /// Returns a short, machine-readable identifier for this error variant.
    ///
    /// Suitable for use as a stable error code in logs or telemetry.
    ///
    /// # Returns
    ///
    /// - `&'static str` - The error code (e.g. `"WEBGL_CONTEXT_UNAVAILABLE"`).
    pub fn code(&self) -> &'static str {
        match self {
            Self::CanvasNotFound(_) => GL_ERROR_CANVAS_NOT_FOUND,
            Self::CanvasQuery(_) => GL_ERROR_CANVAS_QUERY,
            Self::ContextUnavailable => GL_ERROR_CONTEXT_UNAVAILABLE,
            Self::ContextLookup(_) => GL_ERROR_CONTEXT_LOOKUP,
            Self::ContextCast => GL_ERROR_CONTEXT_CAST,
        }
    }
}

/// Implements the formatted message for [`WebGl2InitError`].
impl Display for WebGl2InitError {
    /// Formats the [`WebGl2InitError`] via the supplied formatter.
    ///
    /// # Arguments
    ///
    /// - `&mut Formatter<'_>` - The formatter receiving the formatted output.
    ///
    /// # Returns
    ///
    /// - `fmt::Result` - Result of the formatting operation.
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::CanvasNotFound(selector) => {
                write!(
                    formatter,
                    "[{}] canvas element {selector:?} not found in DOM",
                    self.code()
                )
            }
            Self::CanvasQuery(selector) => {
                write!(
                    formatter,
                    "[{}] querySelector({selector:?}) threw",
                    self.code()
                )
            }
            Self::ContextUnavailable => write!(
                formatter,
                "[{}] canvas.get_context('webgl2') returned null - the browser does not support WebGL 2 or the canvas already uses another context type",
                self.code()
            ),
            Self::ContextLookup(selector) => write!(
                formatter,
                "[{}] canvas.get_context('webgl2') threw while resolving {selector:?}",
                self.code()
            ),
            Self::ContextCast => write!(
                formatter,
                "[{}] get_context('webgl2') result could not be cast to WebGl2RenderingContext",
                self.code()
            ),
        }
    }
}

/// Implements the formatted message for [`WebGlProgramError`].
impl Display for WebGlProgramError {
    /// Formats the [`WebGlProgramError`] via the supplied formatter.
    ///
    /// The browser's own info log is appended verbatim, because a GLSL
    /// diagnostic is only actionable in the driver's own wording.
    ///
    /// # Arguments
    ///
    /// - `&mut Formatter<'_>` - The formatter receiving the formatted output.
    ///
    /// # Returns
    ///
    /// - `fmt::Result` - Result of the formatting operation.
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::ShaderCompile(log) => write!(formatter, "WebGL shader compilation failed: {log}"),
            Self::ProgramLink(log) => write!(formatter, "WebGL program link failed: {log}"),
        }
    }
}

/// Registers both WebGL error types as standard [`Error`] values.
impl Error for WebGl2InitError {}

impl Error for WebGlProgramError {}

/// Implements buffer creation, upload, and lifetime for a vertex, index,
/// or uniform buffer.
///
/// # Performance
///
/// The wrapper tracks its own byte capacity so
/// [`GlBuffer::upload`](super::GlBuffer::upload) can choose between two
/// paths without asking the driver. A payload that fits the existing
/// allocation goes through `bufferSubData` and leaves the driver's
/// storage untouched; one that does not goes through `bufferData` with
/// the new size, which orphans the old allocation so the driver can hand
/// the same memory back without a free-and-realloc pair. That second
/// path is the buffer-orphaning trick, and it is what stops a per-frame
/// vertex stream that grows by one vertex every frame from allocating a
/// new `WebGlBuffer` and leaving the old one for the garbage collector.
impl GlBuffer {
    /// Allocates a buffer sized for `size` bytes.
    ///
    /// The allocation carries no contents, so the first
    /// [`GlBuffer::upload`](super::GlBuffer::upload) re-orphans it at the
    /// exact size it needs. Binding to `target` first is required:
    /// `bufferData` sizes whichever buffer is bound to that target, so
    /// allocating into an unbound target silently resizes whatever was
    /// bound last.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to allocate against.
    /// - `u32` - The buffer target, as returned by
    ///   [`gl_buffer_target`].
    /// - `u32` - The size in bytes to reserve.
    /// - `u32` - The `bufferData` usage hint, as returned by
    ///   [`gl_buffer_usage_hint`].
    ///
    /// # Returns
    ///
    /// - `Option<GlBuffer>` - The allocated buffer, or `None` when the
    ///   driver refused to create one.
    pub fn create(
        context: &WebGl2RenderingContext,
        target: u32,
        size: u32,
        usage: u32,
    ) -> Option<GlBuffer> {
        let buffer: WebGlBuffer = context.create_buffer()?;
        context.bind_buffer(target, Some(&buffer));
        context.buffer_data_with_i32(target, size as i32, usage);
        Some(GlBuffer {
            buffer,
            capacity: size,
            usage,
        })
    }

    /// Allocates a buffer for a role, deriving both the target and the
    /// usage hint from the role itself.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to allocate against.
    /// - `BufferUsage` - The role the buffer will play.
    /// - `u32` - The size in bytes to reserve.
    ///
    /// # Returns
    ///
    /// - `Option<GlBuffer>` - The allocated buffer, or `None` when the
    ///   driver refused to create one.
    pub fn create_for(
        context: &WebGl2RenderingContext,
        usage: BufferUsage,
        size: u32,
    ) -> Option<GlBuffer> {
        GlBuffer::create(
            context,
            gl_buffer_target(usage),
            size,
            gl_buffer_usage_hint(usage),
        )
    }

    /// Reports whether the buffer's current allocation can hold `size`
    /// bytes without re-allocating.
    ///
    /// Exposed separately from
    /// [`GlBuffer::upload`](super::GlBuffer::upload) so a caller batching
    /// several regions into one buffer can size the whole batch once,
    /// rather than growing the buffer on every region.
    ///
    /// # Arguments
    ///
    /// - `u32` - The byte count to test.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when `size` is within the current capacity.
    pub fn fits(&self, size: u32) -> bool {
        size <= self.get_capacity()
    }

    /// Binds the buffer to `target`.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to bind against.
    /// - `u32` - The buffer target to bind to.
    pub fn bind(&self, context: &WebGl2RenderingContext, target: u32) {
        context.bind_buffer(target, Some(self.get_buffer()));
    }

    /// Writes `data` over the whole buffer, re-allocating only when the
    /// payload outgrows the current capacity.
    ///
    /// The reallocation path binds first, because `bufferData` operates
    /// on whichever buffer is bound to the target rather than on a buffer
    /// argument. The sub-data path binds too, so a caller that bound
    /// something else since the last upload still writes to the right
    /// object.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `u32` - The buffer target the buffer is bound under.
    /// - `&[u8]` - The bytes to write.
    pub fn upload(&mut self, context: &WebGl2RenderingContext, target: u32, data: &[u8]) {
        let size: u32 = data.len() as u32;
        if !self.fits(size) {
            self.grow(context, target, size);
        }
        context.bind_buffer(target, Some(self.get_buffer()));
        context.buffer_sub_data_with_i32_and_u8_array(target, 0, data);
    }

    /// Writes `data` at `offset` bytes into the buffer, leaving the rest
    /// untouched.
    ///
    /// The write is rejected outright when it would run past the end of
    /// the allocation. Checking here rather than letting the driver reject
    /// it is what turns a caller's off-by-one into a `false` return
    /// instead of a write outside the buffer.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `u32` - The buffer target the buffer is bound under.
    /// - `u32` - The byte offset to write at.
    /// - `&[u8]` - The bytes to write.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the write was issued, `false` when the
    ///   range fell outside the allocation.
    pub fn update(
        &mut self,
        context: &WebGl2RenderingContext,
        target: u32,
        offset: u32,
        data: &[u8],
    ) -> bool {
        let end: u32 = offset.saturating_add(data.len() as u32);
        if end > self.get_capacity() {
            return false;
        }
        context.bind_buffer(target, Some(self.get_buffer()));
        context.buffer_sub_data_with_i32_and_u8_array(target, offset as i32, data);
        true
    }

    /// Releases the buffer's driver-side storage and marks the wrapper
    /// empty.
    ///
    /// The capacity is zeroed so a later
    /// [`GlBuffer::upload`](super::GlBuffer::upload) against the same
    /// wrapper takes the reallocation path instead of writing into
    /// storage the driver has already reclaimed. Unbinding first matters
    /// because `deleteBuffer` on a bound buffer silently unbinds it,
    /// leaving the target's binding ambiguous for the next caller.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to release against.
    /// - `u32` - The buffer target the buffer is bound under.
    pub fn delete(&mut self, context: &WebGl2RenderingContext, target: u32) {
        context.bind_buffer(target, None);
        context.delete_buffer(Some(self.get_buffer()));
        self.set_capacity(0);
    }

    /// Reallocates the buffer to hold at least `size` bytes, orphaning
    /// the previous allocation.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to allocate against.
    /// - `u32` - The buffer target the buffer is bound under.
    /// - `u32` - The new minimum size in bytes.
    fn grow(&mut self, context: &WebGl2RenderingContext, target: u32, size: u32) {
        context.bind_buffer(target, Some(self.get_buffer()));
        context.buffer_data_with_i32(target, size as i32, self.get_usage());
        self.set_capacity(size);
    }
}

/// Implements texture allocation, DOM-source upload, sampling state, and
/// release.
///
/// # Performance
///
/// Wrap and filter are set once by
/// [`GlTexture::set_parameters`](super::GlTexture::set_parameters) and
/// never re-issued per frame, and a freshly created texture is pinned to
/// a single-level `LINEAR` minification filter because GL's own default
/// is mip-aware: a texture with no mip chain sampled under
/// `LINEAR_MIPMAP_LINEAR` is incomplete and reads as solid black.
impl GlTexture {
    /// Allocates a texture and uploads `pixels` into its base mip level.
    ///
    /// `pixels` is a tightly packed `width * height * 4` RGBA byte slice.
    /// Pass an empty slice to allocate storage without initializing it,
    /// which is what a render target wants; the contents are then
    /// undefined until something is drawn into or uploaded onto them.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to allocate against.
    /// - `u32` - The width in texels.
    /// - `u32` - The height in texels.
    /// - `GpuTextureFormat` - The texel format.
    /// - `&[u8]` - The tightly packed RGBA pixels, or empty.
    ///
    /// # Returns
    ///
    /// - `Option<GlTexture>` - The allocated texture, or `None` when the
    ///   driver refused to create one.
    pub fn create(
        context: &WebGl2RenderingContext,
        width: u32,
        height: u32,
        format: GpuTextureFormat,
        pixels: &[u8],
    ) -> Option<GlTexture> {
        let texture: WebGlTexture = context.create_texture()?;
        let target: u32 = gl_texture_target_2d();
        context.bind_texture(target, Some(&texture));
        let (internal, pixel_format, pixel_type): (i32, u32, u32) = gl_texture_layout(format);
        let payload: Option<&[u8]> = if pixels.is_empty() {
            None
        } else {
            Some(pixels)
        };
        let _: Result<(), JsValue> = context
            .tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
                target,
                0,
                internal,
                width as i32,
                height as i32,
                0,
                pixel_format,
                pixel_type,
                payload,
            );
        GlTexture::apply_default_parameters(context);
        Some(GlTexture {
            texture,
            width,
            height,
            levels: 1,
            mipmapped: false,
        })
    }

    /// Pins a fresh texture to a single-level minification filter, so it
    /// is complete before any mip chain exists.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to configure against.
    fn apply_default_parameters(context: &WebGl2RenderingContext) {
        let target: u32 = gl_texture_target_2d();
        let linear: i32 = gl_filter_mode(FilterMode::Linear) as i32;
        let clamp: i32 = gl_address_mode(AddressMode::ClampToEdge) as i32;
        context.tex_parameteri(target, WebGl2RenderingContext::TEXTURE_MIN_FILTER, linear);
        context.tex_parameteri(target, WebGl2RenderingContext::TEXTURE_MAG_FILTER, linear);
        context.tex_parameteri(target, WebGl2RenderingContext::TEXTURE_WRAP_S, clamp);
        context.tex_parameteri(target, WebGl2RenderingContext::TEXTURE_WRAP_T, clamp);
    }

    /// Uploads a decoded DOM image into a fresh texture, sized to the
    /// image's own dimensions.
    ///
    /// The image is flipped on upload, because a DOM image's origin is
    /// at the top left while GL's texture origin is at the bottom left.
    /// Flipping here rather than negating a v coordinate in the shader
    /// costs one unpack-time transpose and leaves the sampler state
    /// identical to what a raw pixel upload produces.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&HtmlImageElement` - The decoded image to upload.
    /// - `GpuTextureFormat` - The texel format to allocate as.
    ///
    /// # Returns
    ///
    /// - `Option<GlTexture>` - The uploaded texture, or `None` when the
    ///   driver refused to create one.
    pub fn create_from_image(
        context: &WebGl2RenderingContext,
        image: &HtmlImageElement,
        format: GpuTextureFormat,
    ) -> Option<GlTexture> {
        let width: u32 = image.natural_width().max(1);
        let height: u32 = image.natural_height().max(1);
        let texture: WebGlTexture = context.create_texture()?;
        let target: u32 = gl_texture_target_2d();
        context.pixel_storei(WebGl2RenderingContext::UNPACK_FLIP_Y_WEBGL, gl_flip_y(true));
        context.bind_texture(target, Some(&texture));
        let (internal, pixel_format, pixel_type): (i32, u32, u32) = gl_texture_layout(format);
        let _: Result<(), JsValue> = context.tex_image_2d_with_u32_and_u32_and_html_image_element(
            target,
            0,
            internal,
            pixel_format,
            pixel_type,
            image,
        );
        context.pixel_storei(
            WebGl2RenderingContext::UNPACK_FLIP_Y_WEBGL,
            gl_flip_y(false),
        );
        GlTexture::apply_default_parameters(context);
        Some(GlTexture {
            texture,
            width,
            height,
            levels: 1,
            mipmapped: false,
        })
    }

    /// Uploads another canvas element into a fresh texture, sized to
    /// that canvas's dimensions.
    ///
    /// This is the cheapest path from runtime-generated art to a sampled
    /// surface: a 2D context draws into the source canvas, and one
    /// `texImage2D` copies the result into the texture with no CPU round
    /// trip. The canvas is not flipped, because a 2D canvas is already
    /// addressed with a bottom-left origin in `drawImage`'s coordinate
    /// system.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&HtmlCanvasElement` - The source canvas to upload.
    /// - `GpuTextureFormat` - The texel format to allocate as.
    ///
    /// # Returns
    ///
    /// - `Option<GlTexture>` - The uploaded texture, or `None` when the
    ///   driver refused to create one.
    pub fn create_from_canvas(
        context: &WebGl2RenderingContext,
        canvas: &HtmlCanvasElement,
        format: GpuTextureFormat,
    ) -> Option<GlTexture> {
        let width: u32 = canvas.width().max(1);
        let height: u32 = canvas.height().max(1);
        let texture: WebGlTexture = context.create_texture()?;
        let target: u32 = gl_texture_target_2d();
        context.bind_texture(target, Some(&texture));
        let (internal, pixel_format, pixel_type): (i32, u32, u32) = gl_texture_layout(format);
        let _: Result<(), JsValue> = context.tex_image_2d_with_u32_and_u32_and_html_canvas_element(
            target,
            0,
            internal,
            pixel_format,
            pixel_type,
            canvas,
        );
        GlTexture::apply_default_parameters(context);
        Some(GlTexture {
            texture,
            width,
            height,
            levels: 1,
            mipmapped: false,
        })
    }

    /// Uploads an `ImageBitmap` into a fresh texture, sized to the
    /// bitmap's dimensions.
    ///
    /// The bitmap is reached through a cached `texImage2D` method
    /// reference rather than a typed `web-sys` overload, so the module
    /// does not have to enable the `ImageBitmap` feature for a single
    /// call. The method is looked up once per wasm instance and reused on
    /// every later upload, so the steady-state cost matches a typed
    /// binding's. The bitmap is flipped, exactly as for a DOM image.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&Object` - The `ImageBitmap` to upload.
    /// - `u32` - The bitmap's width in pixels.
    /// - `u32` - The bitmap's height in pixels.
    /// - `GpuTextureFormat` - The texel format to allocate as.
    ///
    /// # Returns
    ///
    /// - `Option<GlTexture>` - The uploaded texture, or `None` when the
    ///   driver refused to create one or the upload threw.
    pub fn create_from_bitmap(
        context: &WebGl2RenderingContext,
        bitmap: &Object,
        width: u32,
        height: u32,
        format: GpuTextureFormat,
    ) -> Option<GlTexture> {
        let texture: WebGlTexture = context.create_texture()?;
        let target: u32 = gl_texture_target_2d();
        context.pixel_storei(WebGl2RenderingContext::UNPACK_FLIP_Y_WEBGL, gl_flip_y(true));
        context.bind_texture(target, Some(&texture));
        let (internal, pixel_format, pixel_type): (i32, u32, u32) = gl_texture_layout(format);
        let outcome: Result<JsValue, JsValue> = GlTexture::call_tex_image_2d(
            context.as_ref(),
            internal,
            pixel_format,
            pixel_type,
            bitmap.as_ref(),
        );
        context.pixel_storei(
            WebGl2RenderingContext::UNPACK_FLIP_Y_WEBGL,
            gl_flip_y(false),
        );
        if outcome.is_err() {
            return None;
        }
        GlTexture::apply_default_parameters(context);
        Some(GlTexture {
            texture,
            width: width.max(1),
            height: height.max(1),
            levels: 1,
            mipmapped: false,
        })
    }

    /// Invokes the 5-argument `texImage2D(target, level, internalformat,
    /// format, type, source)` overload through a cached method
    /// reference.
    ///
    /// This is the only route to the `ImageBitmap` overload, which
    /// web-sys gates behind a feature this module does not enable for a
    /// single call. The `Function` is looked up once per wasm instance
    /// in a thread-local slot and reused on every later upload, so the
    /// steady-state cost is a direct call with no `Reflect::get`. The
    /// `Function` object lives on `WebGL2RenderingContext.prototype` and
    /// is therefore a per-class singleton, which is what makes keying
    /// the cache on the method name alone sound.
    ///
    /// The cache borrow is never held across a call out to JS. `context`
    /// is host-supplied, and a getter installed on it (or on
    /// `WebGL2RenderingContext.prototype`) runs arbitrary page code that
    /// can re-enter this module; a re-entry under a live `borrow_mut`
    /// would panic the wasm instance, which has no unwinder to catch it.
    /// The hit path therefore clones the cached `Function` out and drops
    /// the guard. The miss path uses `try_borrow_mut` and, if the cell is
    /// already borrowed, skips the write and resolves the `Function` fresh
    /// for this call only — a fresh `Reflect::get` costs one property
    /// lookup, whereas a wrong `Function` would silently render garbage.
    ///
    /// # Arguments
    ///
    /// - `&JsValue` - The context, as the JS receiver.
    /// - `i32` - The sized internal format.
    /// - `u32` - The pixel format.
    /// - `u32` - The pixel type.
    /// - `&JsValue` - The image source.
    ///
    /// # Returns
    ///
    /// - `Result<JsValue, JsValue>` - The call's own result, so a thrown
    ///   exception surfaces instead of being swallowed.
    fn call_tex_image_2d(
        context: &JsValue,
        internal: i32,
        pixel_format: u32,
        pixel_type: u32,
        source: &JsValue,
    ) -> Result<JsValue, JsValue> {
        let cached: Option<Function> = GL_TEX_IMAGE_2D_CACHE
            .with(|slot: &RefCell<Option<Function>>| slot.borrow().as_ref().cloned());
        let function: Function = match cached {
            Some(function) => function,
            None => Self::resolve_tex_image_2d(context)?,
        };
        function.call6(
            context,
            &JsValue::from_f64(gl_texture_target_2d() as f64),
            &JsValue::from_f64(GL_MIP_LEVEL_ZERO),
            &JsValue::from_f64(internal as f64),
            &JsValue::from_f64(pixel_format as f64),
            &JsValue::from_f64(pixel_type as f64),
            source,
        )
    }

    /// Resolves `WebGL2RenderingContext.prototype.texImage2D` and caches
    /// it when the cache slot is free.
    ///
    /// Split out of [`GlTexture::call_tex_image_2d`](Self::call_tex_image_2d)
    /// so the `Reflect::get` property lookup — arbitrary page code if the
    /// context or its prototype carries a getter — runs with no cache
    /// borrow live. The `try_borrow_mut` write is best-effort: a
    /// concurrent holder means this call already has the `Function` it
    /// needs, so dropping the write costs one lookup next frame and
    /// panicking would cost the whole instance.
    ///
    /// # Arguments
    ///
    /// - `&JsValue` - The context, as the JS receiver.
    ///
    /// # Returns
    ///
    /// - `Result<Function, JsValue>` - The `texImage2D` `Function`, or
    ///   the `Reflect::get` error when the property is absent.
    fn resolve_tex_image_2d(context: &JsValue) -> Result<Function, JsValue> {
        let name: &JsValue = &JsValue::from_str(GL_METHOD_TEX_IMAGE_2D);
        let found: JsValue = Reflect::get(context, name)?;
        let function: Function = found.unchecked_into();
        Self::store_tex_image_2d(&function);
        Ok(function)
    }

    /// Stores a resolved `texImage2D` `Function` in the thread-local cache
    /// when the slot is free.
    ///
    /// The `try_borrow_mut` write is best-effort and never panics. A
    /// holder at this point is a re-entrant call that has already resolved
    /// the `Function` it needs, so skipping the write costs one property
    /// lookup on some later frame; `borrow_mut()` would panic the wasm
    /// instance, which has no unwinder to catch it. The cache is an
    /// optimisation, so a dropped write is always the cheaper failure.
    ///
    /// # Arguments
    ///
    /// - `&Function` - The resolved `texImage2D` `Function` to cache.
    fn store_tex_image_2d(function: &Function) {
        GL_TEX_IMAGE_2D_CACHE.with(|slot: &RefCell<Option<Function>>| {
            match slot.try_borrow_mut() {
                Ok(mut borrow) => {
                    *borrow = Some(function.clone());
                }
                Err(_already_borrowed) => {}
            }
        });
    }

    /// Sets the wrap and filter modes the texture is sampled with.
    ///
    /// Four `texParameteri` calls, issued once at setup. The
    /// minification filter folds in the mip decision, so selecting a
    /// [`MipmapFilter`] the texture does not have requires a prior
    /// [`GlTexture::generate_mipmap`](super::GlTexture::generate_mipmap);
    /// without the chain the texture is incomplete and samples as solid
    /// black.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to configure against.
    /// - `FilterMode` - The interpolation mode within one mip level.
    /// - `MipmapFilter` - How two adjacent mip levels are combined.
    /// - `AddressMode` - What sampling does outside `[0, 1]`.
    pub fn set_parameters(
        &self,
        context: &WebGl2RenderingContext,
        filter: FilterMode,
        mipmap: MipmapFilter,
        address: AddressMode,
    ) {
        let target: u32 = gl_texture_target_2d();
        let wrap: i32 = gl_address_mode(address) as i32;
        context.tex_parameteri(
            target,
            WebGl2RenderingContext::TEXTURE_MIN_FILTER,
            gl_min_filter(filter, mipmap) as i32,
        );
        context.tex_parameteri(
            target,
            WebGl2RenderingContext::TEXTURE_MAG_FILTER,
            gl_filter_mode(filter) as i32,
        );
        context.tex_parameteri(target, WebGl2RenderingContext::TEXTURE_WRAP_S, wrap);
        context.tex_parameteri(target, WebGl2RenderingContext::TEXTURE_WRAP_T, wrap);
    }

    /// Builds the full mip chain and switches the texture to a mip-aware
    /// minification filter.
    ///
    /// Required before any filter that samples across levels: a texture
    /// with a mip-aware min filter and no chain is incomplete, and an
    /// incomplete texture samples as solid black. Costs a
    /// full-texture-sized pass, so it belongs at load time rather than in
    /// a per-frame path.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to generate against.
    /// - `FilterMode` - The interpolation mode within one mip level.
    /// - `MipmapFilter` - How two adjacent mip levels are combined.
    /// - `AddressMode` - What sampling does outside `[0, 1]`.
    pub fn generate_mipmap(
        &mut self,
        context: &WebGl2RenderingContext,
        filter: FilterMode,
        mipmap: MipmapFilter,
        address: AddressMode,
    ) {
        let mut levels: u32 = 1;
        let mut halved: u32 = self.get_width().max(self.get_height()).max(1);
        while halved > 1 {
            halved /= 2;
            levels += 1;
        }
        self.set_levels(levels);
        self.set_mipmapped(true);
        context.generate_mipmap(gl_texture_target_2d());
        self.set_parameters(context, filter, mipmap, address);
    }

    /// Writes a sub-rectangle of the texture without re-allocating it.
    ///
    /// The write is rejected when the rectangle falls outside the
    /// texture, so a caller cannot drive the driver past its own
    /// allocation. The rectangle is assumed to match the given format,
    /// which is the same assumption every sub-image call makes.
    ///
    /// The rectangle is carried as a [`GlScissor`] rather than as four
    /// loose arguments because its bounds are checked against the
    /// texture's own dimensions: passing the box as one value is what
    /// makes it a single value to compare, rather than four
    /// independently-supplied edges that could disagree.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&GlScissor` - The sub-rectangle, in texels, whose top-left is
    ///   the origin.
    /// - `GpuTextureFormat` - The texel format `data` is laid out in.
    /// - `&[u8]` - The tightly packed pixels for the sub-rectangle.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the write was issued, `false` when the
    ///   rectangle fell outside the texture.
    pub fn update(
        &self,
        context: &WebGl2RenderingContext,
        region: &GlScissor,
        format: GpuTextureFormat,
        data: &[u8],
    ) -> bool {
        let x: u32 = (*region.get_x()).max(0) as u32;
        let y: u32 = (*region.get_y()).max(0) as u32;
        let width: u32 = (*region.get_width()).max(0) as u32;
        let height: u32 = (*region.get_height()).max(0) as u32;
        let inside: bool = x.saturating_add(width) <= self.get_width()
            && y.saturating_add(height) <= self.get_height();
        if !inside {
            return false;
        }
        let (_, pixel_format, pixel_type): (i32, u32, u32) = gl_texture_layout(format);
        let _: Result<(), JsValue> = context
            .tex_sub_image_2d_with_i32_and_i32_and_u32_and_type_and_opt_u8_array(
                gl_texture_target_2d(),
                0,
                x as i32,
                y as i32,
                width as i32,
                height as i32,
                pixel_format,
                pixel_type,
                Some(data),
            );
        true
    }

    /// Releases the texture's driver-side storage and marks the wrapper
    /// empty.
    ///
    /// The dimensions are zeroed so a stale size can never be mistaken
    /// for a live allocation by a later sub-image write.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to release against.
    pub fn delete(&mut self, context: &WebGl2RenderingContext) {
        context.delete_texture(Some(self.get_texture()));
        self.set_width(0);
        self.set_height(0);
        self.set_mipmapped(false);
    }
}

/// Implements vertex array binding and attribute description.
///
/// # Performance
///
/// Everything a draw needs to know about its vertex data, which buffer
/// feeds which attribute at what stride and offset and how far each
/// instance advances, is recorded once into the array object. A draw
/// then costs one `bindVertexArray` instead of roughly three calls per
/// attribute, which is what makes it affordable to change mesh between
/// draws in the same frame.
impl GlVertexArray {
    /// Allocates an unbound vertex array object.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to allocate against.
    ///
    /// # Returns
    ///
    /// - `Option<GlVertexArray>` - The allocated array object, or `None`
    ///   when the driver refused to create one.
    pub fn create(context: &WebGl2RenderingContext) -> Option<GlVertexArray> {
        let vao: WebGlVertexArrayObject = context.create_vertex_array()?;
        Some(GlVertexArray { vao })
    }

    /// Makes the array object current, so subsequent attribute and
    /// buffer bindings are recorded into it rather than into the
    /// context's default state.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to bind against.
    pub fn bind(&self, context: &WebGl2RenderingContext) {
        context.bind_vertex_array(Some(self.get_vao()));
    }

    /// Binds `buffer` and describes every attribute in `layout` against
    /// it.
    ///
    /// A layout whose step mode is `Instance` sets a per-attribute
    /// divisor of one, which is what makes
    /// [`WebGl2Backend::draw_elements_instanced`](super::WebGl2Backend::draw_elements_instanced)
    /// advance the buffer once per instance rather than once per vertex.
    /// The divisor is set on every attribute rather than assumed fresh,
    /// because reusing an attribute index with a different step mode
    /// would otherwise inherit the previous divisor.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to record into.
    /// - `&GlBuffer` - The buffer the attributes read from.
    /// - `&VertexBufferLayout` - The stride, step mode, and attribute
    ///   list to record.
    pub fn set_layout(
        &self,
        context: &WebGl2RenderingContext,
        buffer: &GlBuffer,
        layout: &VertexBufferLayout,
    ) {
        self.bind(context);
        buffer.bind(context, WebGl2RenderingContext::ARRAY_BUFFER);
        let stride: i32 = layout.array_stride as i32;
        let divisor: u32 = match layout.step_mode {
            VertexStepMode::Vertex => 0,
            VertexStepMode::Instance => 1,
        };
        for attribute in layout.attributes.iter() {
            let index: u32 = attribute.shader_location;
            let (components, attribute_type, normalized): (u32, u32, bool) =
                gl_attribute_layout(attribute.format);
            context.enable_vertex_attrib_array(index);
            context.vertex_attrib_pointer_with_i32(
                index,
                components as i32,
                attribute_type,
                normalized,
                stride,
                attribute.offset as i32,
            );
            context.vertex_attrib_divisor(index, divisor);
        }
    }

    /// Binds a second buffer and describes the attributes that read from
    /// it, leaving the ones already recorded alone.
    ///
    /// The case a single buffer cannot express is a mesh whose positions
    /// advance per vertex while its per-instance transforms live in a
    /// separate buffer. One VAO records one array-buffer binding, so the
    /// second buffer is bound and described inside the same VAO.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to record into.
    /// - `&GlBuffer` - The buffer the attributes read from.
    /// - `&VertexBufferLayout` - The attributes to record, whose step
    ///   mode should be `Instance`.
    pub fn set_instance_layout(
        &self,
        context: &WebGl2RenderingContext,
        buffer: &GlBuffer,
        layout: &VertexBufferLayout,
    ) {
        self.set_layout(context, buffer, layout);
    }

    /// Records an explicit per-attribute divisor, overriding whatever
    /// the layout's step mode implied.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to record into.
    /// - `u32` - The attribute index, as recorded on the layout.
    /// - `u32` - The divisor: zero per vertex, one per instance.
    pub fn set_divisor(&self, context: &WebGl2RenderingContext, index: u32, divisor: u32) {
        self.bind(context);
        context.vertex_attrib_divisor(index, divisor);
    }

    /// Unbinds the array object, restoring the context's default vertex
    /// state.
    ///
    /// Worth calling before a draw that reads no vertex data at all, a
    /// full-screen triangle generated from `gl_VertexID` say, because
    /// leaving an array object bound would keep its attribute buffers
    /// alive in the driver's state.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to unbind against.
    pub fn unbind(context: &WebGl2RenderingContext) {
        context.bind_vertex_array(None);
    }

    /// Releases the array object's recorded state.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to release against.
    pub fn delete(&self, context: &WebGl2RenderingContext) {
        context.delete_vertex_array(Some(self.get_vao()));
    }
}

/// Implements framebuffer assembly, completeness checking, and
/// rebinding.
///
/// # Performance
///
/// The stored dimensions are what make a rebind after a window resize
/// cheap: a caller can compare the requested size against the stored one
/// and skip the whole re-assembly when they still agree, so binding a
/// framebuffer once per frame costs one `bindFramebuffer` and two integer
/// comparisons.
impl GlFramebuffer {
    /// Allocates a framebuffer with a color texture and, when `depth` is
    /// `Some`, a depth renderbuffer of the same size.
    ///
    /// The color attachment is always a [`GlTexture`], never a
    /// renderbuffer, so the rendered result can be sampled by a later
    /// pass or read back without a resolve blit. The depth attachment is
    /// a renderbuffer, which is cheaper than a depth texture and is
    /// correct for a depth buffer that is written and tested but never
    /// sampled.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to allocate against.
    /// - `u32` - The width in pixels.
    /// - `u32` - The height in pixels.
    /// - `GpuTextureFormat` - The color attachment's format.
    /// - `Option<GpuTextureFormat>` - The depth renderbuffer's format,
    ///   or `None` for a color-only target.
    ///
    /// # Returns
    ///
    /// - `Option<GlFramebuffer>` - The assembled framebuffer, or `None`
    ///   when the driver refused to allocate one of its parts.
    pub fn create(
        context: &WebGl2RenderingContext,
        width: u32,
        height: u32,
        color_format: GpuTextureFormat,
        depth_format: Option<GpuTextureFormat>,
    ) -> Option<GlFramebuffer> {
        let color: GlTexture = GlTexture::create(context, width, height, color_format, &[])?;
        let framebuffer: WebGlFramebuffer = context.create_framebuffer()?;
        let mut depth: Option<WebGlRenderbuffer> = None;
        context.bind_framebuffer(WebGl2RenderingContext::FRAMEBUFFER, Some(&framebuffer));
        context.framebuffer_texture_2d(
            WebGl2RenderingContext::FRAMEBUFFER,
            GL_DEFAULT_COLOR_ATTACHMENT,
            gl_texture_target_2d(),
            Some(color.get_texture()),
            0,
        );
        if let Some(format) = depth_format {
            let renderbuffer: WebGlRenderbuffer = context.create_renderbuffer()?;
            context.bind_renderbuffer(WebGl2RenderingContext::RENDERBUFFER, Some(&renderbuffer));
            context.renderbuffer_storage(
                WebGl2RenderingContext::RENDERBUFFER,
                gl_renderbuffer_format(format),
                width as i32,
                height as i32,
            );
            let attachment: u32 = if format == GpuTextureFormat::Depth24PlusStencil8 {
                WebGl2RenderingContext::DEPTH_STENCIL_ATTACHMENT
            } else {
                WebGl2RenderingContext::DEPTH_ATTACHMENT
            };
            context.framebuffer_renderbuffer(
                WebGl2RenderingContext::FRAMEBUFFER,
                attachment,
                WebGl2RenderingContext::RENDERBUFFER,
                Some(&renderbuffer),
            );
            depth = Some(renderbuffer);
        }
        context.bind_renderbuffer(WebGl2RenderingContext::RENDERBUFFER, None);
        context.bind_framebuffer(WebGl2RenderingContext::FRAMEBUFFER, None);
        Some(GlFramebuffer {
            framebuffer,
            depth,
            color,
            width,
            height,
        })
    }

    /// Binds the framebuffer as both the draw and read target, and
    /// checks that it is complete.
    ///
    /// Binding both targets is deliberate: WebGL 2 separates
    /// `DRAW_FRAMEBUFFER` from `READ_FRAMEBUFFER`, and a framebuffer
    /// bound to only one of them makes a later `readPixels` silently
    /// return the default framebuffer's contents instead. The status
    /// check is what turns a driver-side `FRAMEBUFFER_INCOMPLETE_*`,
    /// which otherwise shows up as a frame that renders nothing at all,
    /// into a value the caller can react to.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to bind against.
    ///
    /// # Returns
    ///
    /// - `GlFramebufferStatus` - Whether the framebuffer is renderable.
    pub fn bind(&self, context: &WebGl2RenderingContext) -> GlFramebufferStatus {
        let handle: &WebGlFramebuffer = self.get_framebuffer();
        context.bind_framebuffer(WebGl2RenderingContext::DRAW_FRAMEBUFFER, Some(handle));
        context.bind_framebuffer(WebGl2RenderingContext::READ_FRAMEBUFFER, Some(handle));
        gl_framebuffer_status(context.check_framebuffer_status(WebGl2RenderingContext::FRAMEBUFFER))
    }

    /// Reports whether the stored attachments still match `width` by
    /// `height`.
    ///
    /// The cheap half of a resize check: a caller that compares this
    /// before [`GlFramebuffer::create`](super::GlFramebuffer::create) can
    /// skip re-allocating every attachment just to rebind an unchanged
    /// render target once per frame.
    ///
    /// # Arguments
    ///
    /// - `u32` - The width to test against.
    /// - `u32` - The height to test against.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the attachments already match.
    pub fn matches(&self, width: u32, height: u32) -> bool {
        self.get_width() == width && self.get_height() == height
    }

    /// Releases the framebuffer and its depth renderbuffer.
    ///
    /// The color texture is deliberately left alone: it is a
    /// [`GlTexture`] the caller also owns, and a render target whose
    /// contents are about to be sampled must outlive its framebuffer.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to release against.
    pub fn delete(&mut self, context: &WebGl2RenderingContext) {
        if let Some(renderbuffer) = self.try_get_depth() {
            context.delete_renderbuffer(Some(&renderbuffer));
        }
        context.delete_framebuffer(Some(self.get_framebuffer()));
        self.set_width(0);
        self.set_height(0);
    }
}

/// Implements shader compilation, program linking, and cached uniform
/// upload.
///
/// # Performance
///
/// Every uniform name this program has ever been asked about is resolved
/// exactly once and cached, including the negative result for a name the
/// GLSL compiler optimized out. A per-frame `set_uniform_1f` is therefore
/// a hash lookup plus the upload, with no trip into the GL frontend's
/// uniform table. Matrix uploads additionally go through a fixed-size
/// stack array rather than a heap one, so uploading a [`Matrix4x4`]
/// costs no allocation on any frame, including the first.
impl GlProgram {
    /// Compiles and links a vertex and fragment shader into a program.
    ///
    /// Both shader objects are deleted once linking succeeds, since the
    /// program keeps the compiled code and the shader objects are pure
    /// intermediate products. On failure the browser's info log is
    /// returned verbatim, because a GLSL diagnostic is only actionable
    /// in the driver's own wording.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to compile against.
    /// - `&str` - The vertex shader source.
    /// - `&str` - The fragment shader source.
    ///
    /// # Returns
    ///
    /// - `Result<GlProgram, WebGlProgramError>` - The linked program with
    ///   an empty uniform cache, or the compile and link info log.
    pub fn create(
        context: &WebGl2RenderingContext,
        vertex_source: &str,
        fragment_source: &str,
    ) -> Result<GlProgram, WebGlProgramError> {
        let vertex_shader: WebGlShader =
            GlProgram::compile(context, GlShaderKind::Vertex, vertex_source)?;
        let fragment_shader: WebGlShader =
            GlProgram::compile(context, GlShaderKind::Fragment, fragment_source)?;
        let program: WebGlProgram = match context.create_program() {
            Some(value) => value,
            None => {
                return Err(WebGlProgramError::ProgramLink(
                    GL_PROGRAM_CREATE_FAILED.to_string(),
                ));
            }
        };
        context.attach_shader(&program, &vertex_shader);
        context.attach_shader(&program, &fragment_shader);
        context.link_program(&program);
        let linked: bool = context
            .get_program_parameter(&program, WebGl2RenderingContext::LINK_STATUS)
            .as_bool()
            .unwrap_or_default();
        context.delete_shader(Some(&vertex_shader));
        context.delete_shader(Some(&fragment_shader));
        if !linked {
            let log: String = context.get_program_info_log(&program).unwrap_or_default();
            context.delete_program(Some(&program));
            return Err(WebGlProgramError::ProgramLink(log));
        }
        Ok(GlProgram {
            program,
            uniforms: HashMap::new(),
            matrix_scratch: [0.0; GL_MAT4_FLOATS],
            blocks: HashMap::new(),
        })
    }

    /// Compiles one shader stage and returns it, or the compile info log.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to compile against.
    /// - `GlShaderKind` - The stage to compile for.
    /// - `&str` - The GLSL source.
    ///
    /// # Returns
    ///
    /// - `Result<WebGlShader, WebGlProgramError>` - The compiled shader,
    ///   or the compile info log.
    fn compile(
        context: &WebGl2RenderingContext,
        kind: GlShaderKind,
        source: &str,
    ) -> Result<WebGlShader, WebGlProgramError> {
        let shader: WebGlShader = match context.create_shader(gl_shader_type(kind)) {
            Some(value) => value,
            None => {
                return Err(WebGlProgramError::ShaderCompile(
                    GL_SHADER_CREATE_FAILED.to_string(),
                ));
            }
        };
        context.shader_source(&shader, source);
        context.compile_shader(&shader);
        let compiled: bool = context
            .get_shader_parameter(&shader, WebGl2RenderingContext::COMPILE_STATUS)
            .as_bool()
            .unwrap_or_default();
        if !compiled {
            let log: String = context.get_shader_info_log(&shader).unwrap_or_default();
            context.delete_shader(Some(&shader));
            return Err(WebGlProgramError::ShaderCompile(log));
        }
        Ok(shader)
    }

    /// Makes the program current, so subsequent uniform writes and
    /// draws target it.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to bind against.
    pub fn bind(&self, context: &WebGl2RenderingContext) {
        context.use_program(Some(self.get_program()));
    }

    /// Resolves a uniform's location once and caches it, including the
    /// case where the GLSL compiler removed the uniform entirely.
    ///
    /// Caching the `None` matters as much as caching the location: a
    /// uniform the compiler optimized out is the common case for a
    /// per-feature flag, and re-querying it every frame would walk the
    /// program's uniform table for a result that can never change.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to resolve against.
    /// - `&str` - The uniform name, with an explicit `[0]` index for
    ///   array uniforms per the `getUniformLocation` spec.
    ///
    /// # Returns
    ///
    /// - `Option<WebGlUniformLocation>` - The location, or `None` when
    ///   the uniform does not exist in the program.
    pub fn uniform(
        &mut self,
        context: &WebGl2RenderingContext,
        name: &str,
    ) -> Option<WebGlUniformLocation> {
        match self.get_uniforms().get(name) {
            Some(cached) => cached.clone(),
            None => {
                let resolved: Option<WebGlUniformLocation> =
                    context.get_uniform_location(self.get_program(), name);
                let mut table: HashMap<String, Option<WebGlUniformLocation>> =
                    self.get_uniforms().clone();
                table.insert(name.to_string(), resolved.clone());
                self.set_uniforms(table);
                resolved
            }
        }
    }

    /// Uploads a `float` uniform through its cached location.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&str` - The uniform name.
    /// - `f32` - The value to upload.
    pub fn set_uniform_1f(&mut self, context: &WebGl2RenderingContext, name: &str, value: f32) {
        let location: Option<WebGlUniformLocation> = self.uniform(context, name);
        context.uniform1f(location.as_ref(), value);
    }

    /// Uploads a `vec2` uniform through its cached location.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&str` - The uniform name.
    /// - `f32` - The x component.
    /// - `f32` - The y component.
    pub fn set_uniform_2f(&mut self, context: &WebGl2RenderingContext, name: &str, x: f32, y: f32) {
        let location: Option<WebGlUniformLocation> = self.uniform(context, name);
        context.uniform2f(location.as_ref(), x, y);
    }

    /// Uploads a `vec3` uniform through its cached location.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&str` - The uniform name.
    /// - `f32` - The x component.
    /// - `f32` - The y component.
    /// - `f32` - The z component.
    pub fn set_uniform_3f(
        &mut self,
        context: &WebGl2RenderingContext,
        name: &str,
        x: f32,
        y: f32,
        z: f32,
    ) {
        let location: Option<WebGlUniformLocation> = self.uniform(context, name);
        context.uniform3f(location.as_ref(), x, y, z);
    }

    /// Uploads a `vec4` uniform through its cached location.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&str` - The uniform name.
    /// - `f32` - The x component.
    /// - `f32` - The y component.
    /// - `f32` - The z component.
    /// - `f32` - The w component.
    pub fn set_uniform_4f(
        &mut self,
        context: &WebGl2RenderingContext,
        name: &str,
        x: f32,
        y: f32,
        z: f32,
        w: f32,
    ) {
        let location: Option<WebGlUniformLocation> = self.uniform(context, name);
        context.uniform4f(location.as_ref(), x, y, z, w);
    }

    /// Uploads a `mat4` uniform from the engine's [`Matrix4x4`], with no
    /// allocation.
    ///
    /// This is the single most important primitive the WebGL path was
    /// missing: the engine's matrices are `f64` and GL's uniforms are
    /// `f32`, so every upload needs a conversion, and doing that into a
    /// freshly allocated `Vec` would put a heap allocation in the middle
    /// of the draw path. The conversion goes through a fixed-size stack
    /// array, so it is a sixteen-element copy into memory that already
    /// exists.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&str` - The uniform name.
    /// - `&Matrix4x4` - The matrix to upload.
    pub fn set_uniform_mat4(
        &mut self,
        context: &WebGl2RenderingContext,
        name: &str,
        matrix: &Matrix4x4,
    ) {
        let location: Option<WebGlUniformLocation> = self.uniform(context, name);
        let mut scratch: [f32; GL_MAT4_FLOATS] = [0.0; GL_MAT4_FLOATS];
        gl_matrix4_into_f32(matrix, &mut scratch);
        context.uniform_matrix4fv_with_f32_array(location.as_ref(), false, &scratch);
    }

    /// Uploads a flat `vec4` array uniform from a caller-owned slice.
    ///
    /// The slice is handed to the driver directly, so there is no
    /// staging copy and no allocation: the only cost is the borrow for
    /// the duration of the call. `data.len()` should be a multiple of
    /// four, since a `vec4` array element is four floats; a length that
    /// is not is the caller's bug, and GL rejects it.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `&str` - The uniform name, with an explicit `[0]` index.
    /// - `&[f32]` - The packed float data.
    pub fn set_uniform_vec4_array(
        &mut self,
        context: &WebGl2RenderingContext,
        name: &str,
        data: &[f32],
    ) {
        let location: Option<WebGlUniformLocation> = self.uniform(context, name);
        context.uniform4fv_with_f32_array(location.as_ref(), data);
    }

    /// Points a named uniform block at a binding index, and caches the
    /// block's index so the lookup happens once.
    ///
    /// The driver's answer for a block name the program does not declare
    /// is `UNIFORM_BLOCK_INDEX`, which is not a valid index. That
    /// negative result is deliberately not cached: a miss here usually
    /// means the shader changed under the program, and caching it would
    /// make the mismatch permanent rather than recoverable on the next
    /// call.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to bind against.
    /// - `&str` - The block name as written in the shader.
    /// - `u32` - The binding point the block should read from.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the block exists and was bound.
    pub fn bind_uniform_block(
        &mut self,
        context: &WebGl2RenderingContext,
        name: &str,
        binding: u32,
    ) -> bool {
        let cached: Option<u32> = self.get_blocks().get(name).copied();
        let index: Option<u32> = match cached {
            Some(value) => Some(value),
            None => {
                let resolved: u32 = context.get_uniform_block_index(self.get_program(), name);
                if resolved == WebGl2RenderingContext::UNIFORM_BLOCK_INDEX {
                    None
                } else {
                    let mut table: HashMap<String, u32> = self.get_blocks().clone();
                    table.insert(name.to_string(), resolved);
                    self.set_blocks(table);
                    Some(resolved)
                }
            }
        };
        match index {
            Some(value) => {
                context.uniform_block_binding(self.get_program(), value, binding);
                true
            }
            None => false,
        }
    }

    /// Releases the program and drops its uniform cache.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to release against.
    pub fn delete(&mut self, context: &WebGl2RenderingContext) {
        context.delete_program(Some(self.get_program()));
        self.set_uniforms(HashMap::new());
        self.set_blocks(HashMap::new());
    }
}

/// Implements allocation, record upload, and binding-point management
/// for a uniform buffer.
///
/// # Performance
///
/// The capacity check mirrors [`GlBuffer`]'s, so a block whose record
/// count grows re-orphans rather than reallocating. Updating one record
/// at a fixed offset is what makes a per-object transform upload
/// possible without rewriting the whole block.
impl GlUniformBlock {
    /// Allocates a uniform buffer of at least `size` bytes and binds it
    /// to `binding`.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to allocate against.
    /// - `u32` - The byte capacity to reserve.
    /// - `u32` - The binding point the shader reads from.
    ///
    /// # Returns
    ///
    /// - `Option<GlUniformBlock>` - The allocated block, or `None` when
    ///   the driver refused to create one.
    pub fn create(
        context: &WebGl2RenderingContext,
        size: u32,
        binding: u32,
    ) -> Option<GlUniformBlock> {
        let capacity: u32 = size.max(1);
        let buffer: GlBuffer = GlBuffer::create(
            context,
            WebGl2RenderingContext::UNIFORM_BUFFER,
            capacity,
            WebGl2RenderingContext::DYNAMIC_DRAW,
        )?;
        let block: GlUniformBlock = GlUniformBlock {
            buffer: buffer.get_buffer().clone(),
            binding,
            capacity,
        };
        block.bind(context);
        Some(block)
    }

    /// Binds the block's buffer to its recorded binding point.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to bind against.
    pub fn bind(&self, context: &WebGl2RenderingContext) {
        context.bind_buffer_base(
            WebGl2RenderingContext::UNIFORM_BUFFER,
            self.get_binding(),
            Some(self.get_buffer()),
        );
    }

    /// Writes `data` at `offset` bytes, re-allocating only if the write
    /// would outgrow the block.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `u32` - The byte offset to write at.
    /// - `&[u8]` - The bytes to write.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the write was issued.
    pub fn update(&mut self, context: &WebGl2RenderingContext, offset: u32, data: &[u8]) -> bool {
        let end: u32 = offset.saturating_add(data.len() as u32);
        if end > self.get_capacity() {
            context.bind_buffer(
                WebGl2RenderingContext::UNIFORM_BUFFER,
                Some(self.get_buffer()),
            );
            context.buffer_data_with_i32(
                WebGl2RenderingContext::UNIFORM_BUFFER,
                end as i32,
                WebGl2RenderingContext::DYNAMIC_DRAW,
            );
            self.set_capacity(end);
        }
        context.bind_buffer(
            WebGl2RenderingContext::UNIFORM_BUFFER,
            Some(self.get_buffer()),
        );
        context.buffer_sub_data_with_i32_and_u8_array(
            WebGl2RenderingContext::UNIFORM_BUFFER,
            offset as i32,
            data,
        );
        true
    }

    /// Writes one [`Matrix4x4`] into the record at `offset`, in bytes.
    ///
    /// The conversion goes through a fixed-size stack array rather than a
    /// heap one, so uploading a per-object transform costs no
    /// allocation. The bound is checked rather than trusted, because an
    /// out-of-range offset would otherwise hand the driver a sub-range
    /// write that silently truncates the transform.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to upload through.
    /// - `u32` - The byte offset of the record to write.
    /// - `&Matrix4x4` - The transform to upload.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the record fit inside the block.
    pub fn set_mat4(
        &mut self,
        context: &WebGl2RenderingContext,
        offset: u32,
        matrix: &Matrix4x4,
    ) -> bool {
        if offset.saturating_add(GL_MAT4_BYTES) > self.get_capacity() {
            return false;
        }
        let mut scratch: [f32; GL_MAT4_FLOATS] = [0.0; GL_MAT4_FLOATS];
        gl_matrix4_into_f32(matrix, &mut scratch);
        let mut bytes: [u8; GL_MAT4_BYTES as usize] = [0; GL_MAT4_BYTES as usize];
        for (index, value) in scratch.iter().enumerate() {
            let raw: [u8; GL_F32_SIZE as usize] = value.to_ne_bytes();
            let base: usize = index * GL_F32_SIZE as usize;
            for (slot, byte) in bytes[base..base + GL_F32_SIZE as usize]
                .iter_mut()
                .zip(raw.iter())
            {
                *slot = *byte;
            }
        }
        self.update(context, offset, &bytes)
    }

    /// Releases the block's buffer.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to release against.
    pub fn delete(&mut self, context: &WebGl2RenderingContext) {
        context.bind_buffer_base(
            WebGl2RenderingContext::UNIFORM_BUFFER,
            self.get_binding(),
            None,
        );
        context.delete_buffer(Some(self.get_buffer()));
        self.set_capacity(0);
    }
}

/// Implements default construction for the fixed-function state shadow.
impl GlRenderState {
    /// Builds the state a freshly created WebGL 2 context is actually
    /// in.
    ///
    /// Every value here is one the driver has already set up, not a zero
    /// placeholder, and that is the point: seeding the diffing shadow
    /// with the real defaults means the first
    /// [`WebGl2Backend::apply_state`](super::WebGl2Backend::apply_state)
    /// emits only the calls that genuinely differ from what the driver
    /// already did, rather than a dozen redundant re-assertions. Getting
    /// a single default wrong would instead leave one field permanently
    /// un-applied, because the diff would consider it unchanged forever.
    ///
    /// Depth test off, depth write on, `LEQUAL` once enabled; blending
    /// off; culling off with counter-clockwise front faces; all four
    /// color channels writable; scissor off; viewport covering the given
    /// size.
    ///
    /// # Arguments
    ///
    /// - `i32` - The viewport width in pixels.
    /// - `i32` - The viewport height in pixels.
    ///
    /// # Returns
    ///
    /// - `GlRenderState` - The state a new context starts in.
    pub fn context_defaults(width: i32, height: i32) -> GlRenderState {
        GlRenderState {
            depth: GlDepthState {
                enabled: false,
                compare: CompareFunction::LessEqual,
                write_enabled: true,
            },
            blend: GlBlendState::default(),
            cull: GlCullState::default(),
            color_mask: GlColorMask {
                bits: GL_COLOR_WRITE_ALL,
            },
            scissor: None,
            viewport: GlViewport {
                x: 0,
                y: 0,
                width,
                height,
            },
        }
    }
}

/// Implements construction and lifecycle for the WebGL 2 backend.
impl WebGl2Backend {
    /// Builds a backend from a render configuration, resolving the
    /// canvas, scaling the backing store by the device pixel ratio, and
    /// acquiring the `webgl2` context.
    ///
    /// The shadow state is seeded with the values a freshly created GL
    /// context actually starts in rather than with all-zero
    /// placeholders, so the first [`WebGl2Backend::apply_state`] emits
    /// only the calls that genuinely differ from what the driver already
    /// set up.
    ///
    /// # Arguments
    ///
    /// - `&RenderConfig` - The rendering configuration.
    ///
    /// # Returns
    ///
    /// - `Result<WebGl2Backend, WebGl2InitError>` - The backend, or a
    ///   typed error describing the specific failure.
    pub fn init(config: &RenderConfig) -> Result<WebGl2Backend, WebGl2InitError> {
        let selector: String = config.get_canvas_selector().clone();
        let Some(window_value) = window() else {
            return Err(WebGl2InitError::CanvasNotFound(
                config.get_canvas_selector().clone(),
            ));
        };
        let Some(document_value) = window_value.document() else {
            return Err(WebGl2InitError::CanvasNotFound(
                config.get_canvas_selector().clone(),
            ));
        };
        let element: Element = document_value
            .query_selector(selector.as_str())
            .map_err(|_| WebGl2InitError::CanvasQuery(selector.to_string()))?
            .ok_or_else(|| WebGl2InitError::CanvasNotFound(config.get_canvas_selector().clone()))?;
        let canvas: HtmlCanvasElement = element.unchecked_into();
        let dpr: f64 = CanvasRenderer::detect_dpr();
        let physical_width: u32 = (config.get_width() * dpr).round() as u32;
        let physical_height: u32 = (config.get_height() * dpr).round() as u32;
        canvas.set_width(physical_width);
        canvas.set_height(physical_height);
        let context_object: Object = canvas
            .get_context(GL_CONTEXT_ID_WEBGL2)
            .map_err(|_| WebGl2InitError::ContextLookup(selector.clone()))?
            .ok_or(WebGl2InitError::ContextUnavailable)?;
        let context: WebGl2RenderingContext = context_object
            .dyn_into()
            .map_err(|_| WebGl2InitError::ContextCast)?;
        context.viewport(0, 0, physical_width as i32, physical_height as i32);
        Ok(WebGl2Backend {
            canvas,
            context,
            shadow: GlRenderState::context_defaults(physical_width as i32, physical_height as i32),
            active_unit: GL_TEXTURE_UNIT_NONE,
            bound_program: JsValue::UNDEFINED,
            clear_color_field: Color::new(0.0, 0.0, 0.0, 1.0),
            readback: Vec::new(),
        })
    }

    /// Reports whether the browser can create a WebGL 2 context at all.
    ///
    /// Creates a throwaway off-DOM canvas and requests a `webgl2`
    /// context. No shaders are compiled and nothing on the page is
    /// touched, so this is cheap enough to call as a capability probe
    /// before deciding which backend to construct.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when a `webgl2` context could be acquired.
    pub fn is_available() -> bool {
        let Some(window_value) = window() else {
            return false;
        };
        let Some(document_value) = window_value.document() else {
            return false;
        };
        let element: Element = match document_value.create_element(GL_DOM_TAG_CANVAS) {
            Ok(value) => value,
            Err(_) => return false,
        };
        let canvas: HtmlCanvasElement = element.unchecked_into();
        canvas
            .get_context(GL_CONTEXT_ID_WEBGL2)
            .ok()
            .flatten()
            .is_some()
    }

    /// Resizes the canvas backing store and re-points the shadow's
    /// viewport at the new size.
    ///
    /// The viewport is written into the shadow rather than issued
    /// immediately, because a resize changes the canvas dimensions and
    /// the viewport that was correct for the old ones must be
    /// re-asserted against the new ones even if the caller's own state
    /// value did not change. Seeding the shadow makes the next
    /// [`WebGl2Backend::apply_state`] emit it exactly once.
    ///
    /// # Arguments
    ///
    /// - `u32` - The new physical pixel width, already multiplied by the
    ///   device pixel ratio.
    /// - `u32` - The new physical pixel height.
    pub fn resize(&mut self, width: u32, height: u32) {
        let mut state: GlRenderState = *self.get_shadow();
        state.viewport = GlViewport {
            x: 0,
            y: 0,
            width: width as i32,
            height: height as i32,
        };
        self.set_shadow(state);
    }

    /// Resizes the canvas backing store and re-asserts the GL viewport
    /// immediately, rather than deferring it to the next
    /// [`WebGl2Backend::apply_state`].
    ///
    /// [`WebGl2Backend::resize`] is the cheaper call and is what a render
    /// loop should use, because the viewport it seeds is emitted once by
    /// the following state apply. This variant exists for the caller that
    /// resizes the DOM canvas itself and needs the GL viewport correct
    /// before the next paint, with no intervening apply.
    ///
    /// # Arguments
    ///
    /// - `u32` - The new physical pixel width, already multiplied by the
    ///   device pixel ratio.
    /// - `u32` - The new physical pixel height.
    pub fn resize_now(&mut self, width: u32, height: u32) {
        self.resize(width, height);
        self.get_context()
            .viewport(0, 0, width as i32, height as i32);
    }

    /// Reports whether the context has been lost, which happens when the
    /// browser reclaims the GPU: a tab backgrounded for long enough, a
    /// driver reset, a device change.
    ///
    /// Worth checking once per frame. Every subsequent GL call against a
    /// lost context is a no-op that reports no error, so a renderer that
    /// does not check silently draws nothing with a clean console.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the context is lost and must be rebuilt.
    pub fn is_context_lost(&self) -> bool {
        self.get_context().is_context_lost()
    }
}

/// Implements the state-diffing apply and the frame-level entry points.
///
/// Every method here exists to make the per-frame cost proportional to
/// what actually changed rather than to what was asked for.
impl WebGl2Backend {
    /// Applies a whole [`GlRenderState`], issuing a GL call only for
    /// each field that differs from the currently-bound state.
    ///
    /// The diff is against a shadow copy of the last applied state, not
    /// against the driver, so no `getParameter` round trip is needed: the
    /// driver is known to agree with the shadow because the shadow is
    /// only ever updated after the calls that establish it. This is the
    /// single largest saving in the whole backend, since a batch of a
    /// thousand draws sharing one state pays for it once instead of for a
    /// thousand `enable` / `depthFunc` / `blendFunc` / `cullFace` /
    /// `colorMask` sequences, each of which the driver re-validates.
    ///
    /// # Arguments
    ///
    /// - `&GlRenderState` - The state to make current.
    pub fn apply_state(&mut self, state: &GlRenderState) {
        let context: &WebGl2RenderingContext = self.get_context();
        let current: GlRenderState = *self.get_shadow();
        let depth_toggled: bool = current.depth.enabled != state.depth.enabled;
        if depth_toggled {
            WebGl2Backend::set_capability(
                context,
                WebGl2RenderingContext::DEPTH_TEST,
                state.depth.enabled,
            );
        }
        if depth_toggled || current.depth.compare != state.depth.compare {
            context.depth_func(gl_compare_function(state.depth.compare));
        }
        if current.depth.write_enabled != state.depth.write_enabled {
            let write: bool = state.depth.write_enabled;
            context.depth_mask(write);
        }
        let blend_toggled: bool = current.blend.enabled != state.blend.enabled;
        if blend_toggled {
            WebGl2Backend::set_capability(
                context,
                WebGl2RenderingContext::BLEND,
                state.blend.enabled,
            );
        }
        if blend_toggled
            || current.blend.color != state.blend.color
            || current.blend.alpha != state.blend.alpha
        {
            WebGl2Backend::apply_blend(context, state);
        }
        if current.cull.mode != state.cull.mode {
            let culling: bool = state.cull.mode != CullMode::None;
            WebGl2Backend::set_capability(context, WebGl2RenderingContext::CULL_FACE, culling);
            if culling {
                context.cull_face(gl_cull_face(state.cull.mode));
            }
        }
        if current.cull.front_face != state.cull.front_face {
            context.front_face(gl_front_face(state.cull.front_face));
        }
        if current.color_mask.bits != state.color_mask.bits {
            let bits: u32 = state.color_mask.bits;
            context.color_mask(
                bits & GL_COLOR_CHANNEL_RED != 0,
                bits & GL_COLOR_CHANNEL_GREEN != 0,
                bits & GL_COLOR_CHANNEL_BLUE != 0,
                bits & GL_COLOR_CHANNEL_ALPHA != 0,
            );
        }
        if current.scissor != state.scissor {
            match state.scissor {
                Some(rectangle) => {
                    WebGl2Backend::set_capability(
                        context,
                        WebGl2RenderingContext::SCISSOR_TEST,
                        true,
                    );
                    context.scissor(
                        *rectangle.get_x(),
                        *rectangle.get_y(),
                        *rectangle.get_width(),
                        *rectangle.get_height(),
                    );
                }
                None => {
                    WebGl2Backend::set_capability(
                        context,
                        WebGl2RenderingContext::SCISSOR_TEST,
                        false,
                    );
                }
            }
        }
        if current.viewport != state.viewport {
            context.viewport(
                *state.viewport.get_x(),
                *state.viewport.get_y(),
                *state.viewport.get_width(),
                *state.viewport.get_height(),
            );
        }
        self.set_shadow(*state);
    }

    /// Issues the pair of calls that establish a blend state.
    ///
    /// Split out of [`WebGl2Backend::apply_state`] so the diff block
    /// above reads as a table of "what changed" rather than as a wall of
    /// GL calls.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to write through.
    /// - `&GlRenderState` - The state whose blend is to be applied.
    fn apply_blend(context: &WebGl2RenderingContext, state: &GlRenderState) {
        context.blend_equation_separate(
            gl_blend_equation(state.blend.color.get_operation()),
            gl_blend_equation(state.blend.alpha.get_operation()),
        );
        context.blend_func_separate(
            gl_blend_factor(state.blend.color.get_source()),
            gl_blend_factor(state.blend.color.get_destination()),
            gl_blend_factor(state.blend.alpha.get_source()),
            gl_blend_factor(state.blend.alpha.get_destination()),
        );
    }

    /// Enables or disables one GL capability, used by the state diff.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to write through.
    /// - `u32` - The capability enum to toggle.
    /// - `bool` - `true` to enable, `false` to disable.
    fn set_capability(context: &WebGl2RenderingContext, capability: u32, enabled: bool) {
        if enabled {
            context.enable(capability);
        } else {
            context.disable(capability);
        }
    }

    /// Resets the shadow to the context's own initial state, which
    /// forces the next apply to re-assert everything.
    ///
    /// Needed after anything that can change GL state behind the
    /// backend's back: a context restore, a debug extension, or a
    /// driver's own reset. Without it the shadow would claim a state the
    /// driver no longer holds and every diffed call would be skipped.
    ///
    /// # Arguments
    ///
    /// - `u32` - The viewport width to seed the shadow with.
    /// - `u32` - The viewport height to seed the shadow with.
    pub fn invalidate_state(&mut self, width: u32, height: u32) {
        let fresh: GlRenderState = GlRenderState::context_defaults(width as i32, height as i32);
        self.set_shadow(fresh);
        self.set_active_unit(GL_TEXTURE_UNIT_NONE);
        self.set_bound_program(JsValue::UNDEFINED);
    }

    /// Sets the color [`WebGl2Backend::begin_frame`] clears to.
    ///
    /// # Arguments
    ///
    /// - `Color` - The clear color, with channels in `0.0..=1.0`.
    pub fn set_clear_color(&mut self, color: Color) {
        self.set_clear_color_field(color);
    }

    /// Clears the bound framebuffer and folds a full-size viewport into
    /// the shadow.
    ///
    /// The clear is issued through `clearColor` plus `clear` rather than
    /// `clearBufferfv` because the former is a two-call pair the driver
    /// folds into a single tile-clear, while the latter is the WebGL 2
    /// entry point that most drivers route through a slower generic
    /// path. The viewport is written into the shadow as well, so a
    /// caller that set a smaller viewport for a pass does not have it
    /// silently left in place by the next frame.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to clear through.
    /// - `u32` - The width of the bound framebuffer, in pixels.
    /// - `u32` - The height of the bound framebuffer, in pixels.
    pub fn begin_frame(&mut self, context: &WebGl2RenderingContext, width: u32, height: u32) {
        let color: Color = self.get_clear_color_field();
        context.clear_color(
            color.get_red() as f32,
            color.get_green() as f32,
            color.get_blue() as f32,
            color.get_alpha() as f32,
        );
        context.clear(
            WebGl2RenderingContext::COLOR_BUFFER_BIT | WebGl2RenderingContext::DEPTH_BUFFER_BIT,
        );
        let mut state: GlRenderState = *self.get_shadow();
        state.viewport = GlViewport {
            x: 0,
            y: 0,
            width: width as i32,
            height: height as i32,
        };
        self.set_shadow(state);
    }

    /// Clears only the depth buffer, leaving color untouched.
    ///
    /// Split out because a second pass that needs a fresh depth range
    /// without disturbing the color underneath is common in deferred
    /// and post-process work, and a full clear here would throw away
    /// results the pass is about to read.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to clear through.
    pub fn clear_depth(&self, context: &WebGl2RenderingContext) {
        context.clear(WebGl2RenderingContext::DEPTH_BUFFER_BIT);
    }

    /// Renders a complete frame: clears to `color`, binds `program`, and
    /// issues one triangle-list draw.
    ///
    /// The one-call frame shape a demo loop needs, built from the same
    /// primitives the multi-pass API exposes. Geometry is generated
    /// inside the vertex shader from `gl_VertexID`, so there are no
    /// vertex buffers to bind; `vertex_count` is the number of vertices
    /// the shader is expected to emit.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to draw through.
    /// - `&GlProgram` - The program to draw with.
    /// - `Color` - The color to clear to before drawing.
    /// - `u32` - The number of vertices to draw.
    pub fn render_frame(
        &mut self,
        context: &WebGl2RenderingContext,
        program: &GlProgram,
        color: Color,
        vertex_count: u32,
    ) {
        self.set_clear_color(color);
        self.begin_frame(context, self.canvas_width(), self.canvas_height());
        self.use_program(context, program);
        let args: DrawArgs = DrawArgs::whole_stream(vertex_count, 1);
        self.draw_arrays(context, PrimitiveTopology::TriangleList, &args);
    }

    /// Returns the canvas backing-store width in physical pixels.
    ///
    /// # Returns
    ///
    /// - `u32` - The canvas `width` attribute.
    fn canvas_width(&self) -> u32 {
        self.get_canvas().width()
    }

    /// Returns the canvas backing-store height in physical pixels.
    ///
    /// # Returns
    ///
    /// - `u32` - The canvas `height` attribute.
    fn canvas_height(&self) -> u32 {
        self.get_canvas().height()
    }
}

/// Implements every draw entry point, plus binding helpers and readback.
impl WebGl2Backend {
    /// Binds a program, issuing `useProgram` only when it is not already
    /// current.
    ///
    /// The identity test is `Object.is` rather than a pointer compare,
    /// because a `WebGlProgram` is a JS handle whose Rust address is a
    /// stack slot a later handle may reuse; comparing addresses would let
    /// two different programs compare equal and skip a needed rebind.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to bind against.
    /// - `&GlProgram` - The program to make current.
    pub fn use_program(&mut self, context: &WebGl2RenderingContext, program: &GlProgram) {
        let candidate: &JsValue = program.get_program().as_ref();
        if !Object::is(self.get_bound_program(), candidate) {
            context.use_program(Some(program.get_program()));
            let owned: JsValue = candidate.clone();
            self.set_bound_program(owned);
        }
    }

    /// Binds a texture to a unit, issuing `activeTexture` only when the
    /// unit is not already current.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to bind against.
    /// - `u32` - The texture unit index, as passed to
    ///   [`gl_texture_unit`].
    /// - `&GlTexture` - The texture to bind to that unit.
    pub fn bind_texture_unit(
        &mut self,
        context: &WebGl2RenderingContext,
        unit: u32,
        texture: &GlTexture,
    ) {
        if self.get_active_unit() != unit {
            context.active_texture(gl_texture_unit(unit));
            self.set_active_unit(unit);
        }
        context.bind_texture(gl_texture_target_2d(), Some(texture.get_texture()));
    }

    /// Draws `args.vertex_count` vertices from the bound vertex streams.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to draw through.
    /// - `PrimitiveTopology` - How the vertices are assembled.
    /// - `&DrawArgs` - The counts and offsets to draw.
    pub fn draw_arrays(
        &self,
        context: &WebGl2RenderingContext,
        topology: PrimitiveTopology,
        args: &DrawArgs,
    ) {
        let mode: u32 = gl_primitive_mode(topology);
        let first: i32 = args.get_first_vertex() as i32;
        let count: i32 = args.get_vertex_count() as i32;
        if args.get_instance_count() <= 1 {
            context.draw_arrays(mode, first, count);
        } else {
            context.draw_arrays_instanced(mode, first, count, args.get_instance_count() as i32);
        }
    }

    /// Draws `args.index_count` indices from the bound index buffer.
    ///
    /// WebGL 2 has no `baseVertex`, the way WebGPU's
    /// [`DrawIndexedArgs`] does: the element offset reaches the vertex
    /// fetch through the attribute pointers recorded in the vertex array
    /// object instead. `base_vertex` is therefore only meaningful as an
    /// assertion here, and a non-zero value is reported as `false` rather
    /// than silently ignored, because silently ignoring it would draw
    /// the wrong mesh with no diagnostic at all.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to draw through.
    /// - `PrimitiveTopology` - How the vertices are assembled.
    /// - `&DrawIndexedArgs` - The counts and offsets to draw.
    /// - `IndexFormat` - The element width of the bound index buffer.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the draw was issued, `false` when
    ///   `base_vertex` is non-zero and therefore cannot be honored.
    pub fn draw_elements(
        &self,
        context: &WebGl2RenderingContext,
        topology: PrimitiveTopology,
        args: &DrawIndexedArgs,
        index_format: IndexFormat,
    ) -> bool {
        if args.get_base_vertex() != 0 {
            return false;
        }
        let (index_type, stride): (u32, u32) = gl_index_type(index_format);
        let mode: u32 = gl_primitive_mode(topology);
        let count: i32 = args.get_index_count() as i32;
        let offset: i32 = (args.get_first_index() * stride) as i32;
        if args.get_instance_count() <= 1 {
            context.draw_elements_with_i32(mode, count, index_type, offset);
        } else {
            context.draw_elements_instanced_with_i32(
                mode,
                count,
                index_type,
                offset,
                args.get_instance_count() as i32,
            );
        }
        true
    }

    /// Draws a contiguous index range without disturbing the rest of the
    /// index buffer.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to draw through.
    /// - `PrimitiveTopology` - How the vertices are assembled.
    /// - `u32` - The first index to read.
    /// - `u32` - The last index to read, inclusive.
    /// - `IndexFormat` - The element width of the bound index buffer.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the range was non-empty and was drawn.
    pub fn draw_element_range(
        &self,
        context: &WebGl2RenderingContext,
        topology: PrimitiveTopology,
        start: u32,
        end: u32,
        index_format: IndexFormat,
    ) -> bool {
        if end < start {
            return false;
        }
        let (index_type, _stride): (u32, u32) = gl_index_type(index_format);
        context.draw_range_elements_with_i32(
            gl_primitive_mode(topology),
            start,
            end,
            (end - start + 1) as i32,
            index_type,
            0,
        );
        true
    }

    /// Draws an instanced vertex range, for geometry generated entirely
    /// on the GPU.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to draw through.
    /// - `PrimitiveTopology` - How the vertices are assembled.
    /// - `&DrawArgs` - The counts and offsets to draw.
    pub fn draw_arrays_instanced(
        &self,
        context: &WebGl2RenderingContext,
        topology: PrimitiveTopology,
        args: &DrawArgs,
    ) {
        let instances: i32 = args.get_instance_count().max(1) as i32;
        context.draw_arrays_instanced(
            gl_primitive_mode(topology),
            args.get_first_vertex() as i32,
            args.get_vertex_count() as i32,
            instances,
        );
    }

    /// Draws an instanced index range, the form a mesh rendered many
    /// times per frame takes.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to draw through.
    /// - `PrimitiveTopology` - How the vertices are assembled.
    /// - `&DrawIndexedArgs` - The counts and offsets to draw.
    /// - `IndexFormat` - The element width of the bound index buffer.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the draw was issued, `false` when
    ///   `base_vertex` is non-zero and therefore cannot be honored.
    pub fn draw_elements_instanced(
        &self,
        context: &WebGl2RenderingContext,
        topology: PrimitiveTopology,
        args: &DrawIndexedArgs,
        index_format: IndexFormat,
    ) -> bool {
        self.draw_elements(context, topology, args, index_format)
    }

    /// Reads a rectangle of the bound framebuffer back into Rust memory.
    ///
    /// The destination buffer is owned by the backend and reused across
    /// calls, so a steady-state readback, a picking query or a screenshot
    /// say, allocates nothing after the first time it runs at that size.
    /// The result is returned as an owned `Vec`: a caller that needs the
    /// bytes past the next readback keeps its own copy, which is one
    /// explicit clone rather than a borrow held across a driver call.
    ///
    /// Reading from the default framebuffer is legal but slow on most
    /// drivers, because the contents may already have been discarded by
    /// the compositor; reading from a bound
    /// [`GlFramebuffer`](super::GlFramebuffer) is the supported path.
    ///
    /// # Arguments
    ///
    /// - `&WebGl2RenderingContext` - The context to read through.
    /// - `i32` - The left edge in pixels.
    /// - `i32` - The bottom edge in pixels.
    /// - `u32` - The width in pixels.
    /// - `u32` - The height in pixels.
    ///
    /// # Returns
    ///
    /// - `Vec<u8>` - The tightly packed RGBA bytes, row-major from the
    ///   bottom edge, or empty when the rectangle has no area.
    pub fn read_pixels(
        &mut self,
        context: &WebGl2RenderingContext,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> Vec<u8> {
        if width == 0 || height == 0 {
            return Vec::new();
        }
        let needed: usize = (width as usize) * (height as usize) * GL_RGBA_TEXEL_SIZE;
        if self.get_readback().len() < needed {
            self.set_readback(vec![0; needed]);
        }
        let mut storage: Vec<u8> = mem::take(self.get_mut_readback());
        let _: Result<(), JsValue> = context.read_pixels_with_opt_u8_array(
            x,
            y,
            width as i32,
            height as i32,
            WebGl2RenderingContext::RGBA,
            WebGl2RenderingContext::UNSIGNED_BYTE,
            Some(&mut storage[..needed]),
        );
        let bytes: Vec<u8> = storage[..needed].to_vec();
        self.set_readback(storage);
        bytes
    }
}