three-rs 0.3.0

A port of three.js core and its WebGPURenderer to Rust on wgpu, graded against three.js's own end-to-end pixel comparison.
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
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
//! Port of `three.js/src/nodes/core/` — the node graph itself.
//!
//! Three's nodes are mutable JS objects that grow builder state as they are
//! visited. Here a node is an immutable `Rc<Node>`; `Rc::as_ptr` gives it a
//! stable identity, and every piece of per-build state the builder would have
//! hung off the node (usage count, property name, varying slot, uniform slot)
//! lives in the builder keyed by that identity. See `docs/nodes.md` §1.

use std::cell::{Cell, RefCell};
use std::hash::Hash;
use std::rc::Rc;

use crate::math::{Color, Matrix4, Vector2, Vector3};
use crate::textures::{
    CubeDepthTexture, CubeTexture, Data3DTexture, DataArrayTexture, DataTexture, DepthTexture,
    Texture,
};

/// A WGSL value type. Three carries these as strings (`'vec3'`); the closed set
/// is the part of `NodeBuilder`'s type vocabulary the ladder has reached.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Type {
    /// No value — a statement's type, or a `fn` with no return.
    Void,
    /// `bool`.
    Bool,
    /// `f32`.
    F32,
    /// `i32`.
    I32,
    /// `u32`.
    U32,
    /// `vec2<f32>`.
    Vec2,
    /// `vec3<f32>`.
    Vec3,
    /// `vec4<f32>`.
    Vec4,
    /// `vec2<u32>`.
    UVec2,
    /// `vec2<i32>`.
    IVec2,
    /// `vec3<u32>` — MaterialX's `mx_hash_vec3` packs its three byte hashes
    /// into one.
    UVec3,
    /// `vec4<u32>` — the `skinIndex` attribute, which three.js declares
    /// `attribute( 'skinIndex', 'uvec4' )` and uploads as a `Uint32Array`.
    UVec4,
    /// `vec3<i32>` / `vec4<i32>` — TSL's `ivec3()` / `ivec4()`.
    IVec3,
    /// `vec4<i32>` — TSL's `ivec4()`.
    IVec4,
    /// `vec2<bool>` / `vec3<bool>` / `vec4<bool>` — TSL's `bvec2()` … `bvec4()`,
    /// and what a component-wise comparison of two vectors is.
    BVec2,
    /// `vec3<bool>`.
    BVec3,
    /// `vec4<bool>`.
    BVec4,
    /// `mat2` — `RotateNode`'s vec2 path emits `mat2x2<f32>( cos, sin, -sin, cos )`.
    Mat2,
    /// `mat3x3<f32>`.
    Mat3,
    /// `mat4x4<f32>`.
    Mat4,
}

impl Type {
    /// `NodeBuilder.getTypeLength()`.
    pub fn components(self) -> usize {
        match self {
            Type::Void => 0,
            Type::Bool | Type::F32 | Type::I32 | Type::U32 => 1,
            Type::Vec2 | Type::UVec2 | Type::IVec2 | Type::BVec2 => 2,
            Type::Vec3 | Type::UVec3 | Type::IVec3 | Type::BVec3 => 3,
            Type::Vec4 | Type::UVec4 | Type::IVec4 | Type::BVec4 => 4,
            Type::Mat2 => 4,
            Type::Mat3 => 9,
            Type::Mat4 => 16,
        }
    }

    /// `NodeBuilder.getComponentType()`.
    pub fn component_type(self) -> Type {
        match self {
            Type::UVec2 | Type::UVec3 | Type::UVec4 => Type::U32,
            Type::IVec2 | Type::IVec3 | Type::IVec4 => Type::I32,
            Type::BVec2 | Type::BVec3 | Type::BVec4 => Type::Bool,
            Type::Vec2 | Type::Vec3 | Type::Vec4 | Type::Mat2 | Type::Mat3 | Type::Mat4 => {
                Type::F32
            }
            other => other,
        }
    }

    /// The vector type with `self`'s component type and `n` components.
    pub fn vector_of(component: Type, n: usize) -> Type {
        match (component, n) {
            (_, 1) => component,
            (Type::U32, 2) => Type::UVec2,
            (Type::I32, 2) => Type::IVec2,
            (Type::U32, 3) => Type::UVec3,
            (Type::I32, 3) => Type::IVec3,
            (Type::I32, 4) => Type::IVec4,
            (Type::Bool, 2) => Type::BVec2,
            (Type::Bool, 3) => Type::BVec3,
            (Type::Bool, 4) => Type::BVec4,
            (Type::F32, 2) => Type::Vec2,
            (Type::F32, 3) => Type::Vec3,
            (Type::F32, 4) => Type::Vec4,
            (Type::U32, 4) => Type::UVec4,
            other => panic!("three-rs: the node builder only makes vectors of {other:?}"),
        }
    }

    /// Whether this is one of the matrix types.
    pub fn is_matrix(self) -> bool {
        matches!(self, Type::Mat2 | Type::Mat3 | Type::Mat4)
    }
}

/// Which of the generated shader's uniform blocks a uniform lives in.
///
/// Three has a `UniformGroupNode` per uniform; the ladder uses `renderGroup`
/// (per render call — camera, time, viewport), `objectGroup` (per render
/// object — model matrices, material values) and, under an `ArrayCamera`,
/// `sharedUniformGroup( 'cameraIndex' )` — the one `u32` the backend swaps
/// per sub-camera (`docs/nodes.md` §40).
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum UniformGroup {
    /// `renderGroup` — per render call: camera, time, viewport.
    Render,
    /// `cameraIndex`'s group, which three sorts between the two others
    /// (`@group( 1 )` in `webgpu_camera_array`'s dump).
    CameraIndex,
    /// `objectGroup` — per render object: model matrices, material values.
    Object,
}

impl UniformGroup {
    /// The order the groups take their `@group( n )` indices in, each one
    /// only when it has a binding.
    pub(crate) const ORDER: [UniformGroup; 3] = [
        UniformGroup::Render,
        UniformGroup::CameraIndex,
        UniformGroup::Object,
    ];

    pub(crate) fn struct_name(self) -> &'static str {
        match self {
            UniformGroup::Render => "render",
            UniformGroup::CameraIndex => "cameraIndex",
            UniformGroup::Object => "object",
        }
    }
}

/// How often a uniform block has to be rewritten — `Node.updateType`.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum UpdateType {
    /// Written once per render call.
    Render,
    /// Written once per render object.
    Object,
}

/// Where a uniform's bytes come from. The renderer owns the values; the node
/// graph only names them. This is the whole of `NodeUpdateType` /
/// `UniformNode.update()` for the ladder so far.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum UniformSource {
    /// `cameraProjectionMatrix` — `camera.projectionMatrix`.
    CameraProjectionMatrix,
    /// `cameraViewMatrix` — `camera.matrixWorldInverse`.
    CameraViewMatrix,
    /// `cameraIndex` — `uniform( 0, 'uint' ).setName( 'u_cameraIndex' )`, the
    /// sub-camera an `ArrayCamera` draw is for. The backend binds one group
    /// per sub-camera rather than rewriting it, so the value written here is
    /// never read; see `Draw::sub_cameras` in the renderer.
    CameraIndex,
    /// `cameraWorldMatrix` — `camera.matrixWorld`.
    CameraWorldMatrix,
    /// `cameraPosition` — `camera.matrixWorld`'s translation, which
    /// `getIBLVolumeRefraction` takes the world-space view vector from.
    CameraPosition,
    /// `modelWorldMatrix` — `object.matrixWorld`.
    ModelWorldMatrix,
    /// `modelNormalMatrix` — `object.normalMatrix`.
    ModelNormalMatrix,
    /// `cameraProjectionMatrixInverse` — `camera.projectionMatrixInverse`.
    /// `Line2NodeMaterial.setupPosition()` needs it to push a clip-space
    /// position back through the camera.
    CameraProjectionMatrixInverse,
    /// `modelWorldMatrixInverse` — `uniform( new Matrix4() ).onObjectUpdate( (
    /// { object }, self ) => self.value.copy( object.matrixWorld ).invert() )`.
    ModelWorldMatrixInverse,
    /// `materialColor` — `MeshBasicMaterial.color` in the working space.
    MaterialColor,
    /// `materialOpacity` — `Material.opacity`.
    MaterialOpacity,
    /// `materialAlphaTest` — `Material.alphaTest`.
    MaterialAlphaTest,
    /// `materialReflectivity` — `MeshLambertMaterial.reflectivity` and
    /// friends.
    MaterialReflectivity,
    /// `materialEnvIntensity` — `MeshStandardMaterial.envMapIntensity`, the
    /// scale `EnvironmentNode` puts on both IBL terms.
    MaterialEnvIntensity,
    /// `materialRotation` — `SpriteMaterial.rotation`.
    MaterialRotation,
    /// `MeshPhongMaterial.shininess` / `.specular` / `.emissive` /
    /// `.emissiveIntensity`.
    MaterialShininess,
    /// `MeshPhongMaterial.specular`.
    MaterialSpecular,
    /// `MeshPhongMaterial.emissive` / `MeshStandardMaterial.emissive`.
    MaterialEmissive,
    /// `emissiveIntensity` — the scale on [`UniformSource::MaterialEmissive`].
    MaterialEmissiveIntensity,
    /// `materialAnisotropyVector` — `MeshPhysicalMaterial.anisotropy` and
    /// `.anisotropyRotation` as one `vec2( a * cos( r ), a * sin( r ) )`.
    MaterialAnisotropyVector,
    /// `MeshPhysicalMaterial.clearcoat` / `.clearcoatRoughness` /
    /// `.clearcoatNormalScale`.
    MaterialClearcoat,
    /// `MeshPhysicalMaterial.clearcoatRoughness`.
    MaterialClearcoatRoughness,
    /// `MeshPhysicalMaterial.clearcoatNormalScale`.
    MaterialClearcoatNormalScale,
    /// `MeshPhysicalMaterial.transmission` / `.thickness` /
    /// `.attenuationDistance` / `.attenuationColor` — `KHR_materials_transmission`
    /// and `KHR_materials_volume`.
    MaterialTransmission,
    /// `MeshPhysicalMaterial.thickness`.
    MaterialThickness,
    /// `MeshPhysicalMaterial.attenuationDistance`.
    MaterialAttenuationDistance,
    /// `MeshPhysicalMaterial.attenuationColor`, in the working space.
    MaterialAttenuationColor,
    /// `materialEnvRotation` — the env map's rotation matrix.
    EnvRotationMatrix,
    /// `backgroundRotation` — `scene.backgroundRotation`.
    BackgroundRotation,
    /// `backgroundBlurriness` — `scene.backgroundBlurriness`.
    BackgroundBlurriness,
    /// `backgroundIntensity` — `scene.backgroundIntensity`.
    BackgroundIntensity,
    /// `reference( 'color' | 'near' | 'far' | 'density', …, scene.fog )
    /// .setGroup( renderGroup )` — the classic `scene.fog`'s parameters, which
    /// `NodeManager.updateFog()` binds as render-group uniforms so that a fog
    /// whose values change never rebuilds a program. The colour is in the
    /// working space.
    FogColor,
    /// `scene.fog.near` — `Fog`'s linear near distance.
    FogNear,
    /// `scene.fog.far` — `Fog`'s linear far distance.
    FogFar,
    /// `scene.fog.density` — `FogExp2`'s density.
    FogDensity,
    /// `time` — `TimerNode.GLOBAL`, seconds since the renderer started.
    Time,
    /// `deltaTime` — `TimerNode.DELTA`, `frame.deltaTime`: the seconds since
    /// the previous `NodeFrame.update()`.
    DeltaTime,
    /// `frameId` — `TimerNode.FRAME`, `uniform( 0, 'uint' )` updated from
    /// `frame.frameId`: the count of `NodeFrame.update()` calls.
    FrameId,
    /// `viewportSize` — the render target's pixel dimensions.
    ViewportSize,
    /// `viewport` — `ScreenNode.VIEWPORT`, the whole rectangle as
    /// `( x, y, width, height )` in physical pixels. `ScreenNode.update()`
    /// takes it from the bound render target, or from
    /// `renderer.getViewport()` times the pixel ratio.
    Viewport,
    /// `screenDPR` — `uniform( 1 ).onRenderUpdate( ( { renderer } ) =>
    /// renderer.getPixelRatio() )`.
    ScreenDpr,
    /// `cameraNear` — `uniform( 'float' ).setName( 'cameraNear' )
    /// .setGroup( renderGroup ).onRenderUpdate( ( { camera } ) => camera.near )`.
    CameraNear,
    /// `cameraFar` — as [`UniformSource::CameraNear`], from `camera.far`.
    CameraFar,
    /// `LightsNode`'s per-light members, by index into the renderer's light
    /// list for the pass. The dumps put all four in the **render** group:
    /// `light.color * light.intensity` (linear), the cutoff distance, the decay
    /// exponent, and the light's position through the camera view matrix.
    LightColorIntensity(usize),
    /// The cutoff distance — see [`UniformSource::LightColorIntensity`].
    LightCutoffDistance(usize),
    /// The decay exponent — see [`UniformSource::LightColorIntensity`].
    LightDecay(usize),
    /// The light's position through the camera view matrix — see
    /// [`UniformSource::LightColorIntensity`].
    LightViewPosition(usize),
    /// `Morph.js`' `base = uniform( 1 )`, updated per object to
    /// `1 - Σ morphTargetInfluences` (or 1 when the targets are relative).
    MorphBase,
    /// `lightPosition( light )` / `lightTargetPosition( light )` — the world
    /// positions `lightTargetDirection` differences.
    LightWorldPosition(usize),
    /// `lightTargetPosition( light )` — see [`UniformSource::LightWorldPosition`].
    LightTargetPosition(usize),
    /// `HemisphereLightNode`: `light.groundColor * light.intensity` (linear).
    LightGroundColor(usize),
    /// `SpotLightNode`'s `coneCosNode` / `penumbraCosNode`.
    LightConeCos(usize),
    /// `SpotLightNode.penumbraCosNode` — see [`UniformSource::LightConeCos`].
    LightPenumbraCos(usize),
    /// `ShadowNode`'s per-shadow references: `lightShadowMatrix( light )`, the
    /// shadow camera's near and far planes (`PointShadowNode`) and
    /// `reference( …, shadow )` for the five scalars.
    ShadowMatrix(usize),
    /// The shadow camera's near plane — see [`UniformSource::ShadowMatrix`].
    ShadowCameraNear(usize),
    /// The shadow camera's far plane — see [`UniformSource::ShadowMatrix`].
    ShadowCameraFar(usize),
    /// `reference( 'bias', 'float', shadow )`.
    ShadowBias(usize),
    /// `reference( 'normalBias', 'float', shadow )`.
    ShadowNormalBias(usize),
    /// `reference( 'radius', 'float', shadow )` — the blur radius.
    ShadowRadius(usize),
    /// `reference( 'blurSamples', 'float', shadow )` — the VSM blur passes'
    /// tap count.
    ShadowBlurSamples(usize),
    /// `reference( 'mapSize', 'vec2', shadow )` — the shadow map's resolution.
    ShadowMapSize(usize),
    /// `reference( 'intensity', 'float', shadow )`.
    ShadowIntensity(usize),
    /// `materialLineWidth` — `MaterialNode.LINE_WIDTH`, i.e.
    /// `material.linewidth`. Only a fat-line material reads it; a hairline
    /// `Line` ignores it, as WebGL and WebGPU both do.
    MaterialLineWidth,
    /// `materialMetalness` / `materialRoughness` / `materialBumpScale`.
    MaterialMetalness,
    /// `MeshStandardMaterial.roughness`.
    MaterialRoughness,
    /// `MeshStandardMaterial.bumpScale`.
    MaterialBumpScale,
    /// `MeshPhysicalMaterial`'s `ior` / `specularIntensity` / `specularColor`,
    /// and `MeshStandardMaterial.normalScale`.
    MaterialIor,
    /// `MeshPhysicalMaterial.specularIntensity`.
    MaterialSpecularIntensity,
    /// `MeshPhysicalMaterial.specularColor`, in the working space.
    MaterialSpecularColor,
    /// `MeshPhysicalMaterial.sheen` / `.sheenColor` / `.sheenRoughness`.
    MaterialSheen,
    /// `MeshPhysicalMaterial.sheenColor`, in the working space.
    MaterialSheenColor,
    /// `MeshPhysicalMaterial.sheenRoughness`.
    MaterialSheenRoughness,
    /// `MeshPhysicalMaterial.diffuseRoughness`.
    MaterialDiffuseRoughness,
    /// `MeshPhysicalMaterial.iridescence` — `KHR_materials_iridescence`.
    MaterialIridescence,
    /// `MeshPhysicalMaterial.iridescenceIOR`.
    MaterialIridescenceIor,
    /// `iridescenceThicknessRange[ 1 ]` — three reaches the range through
    /// `reference( '1', 'float', material.iridescenceThicknessRange )` and
    /// `reference( '0', … )`, two separate float uniforms, the maximum built
    /// first.
    MaterialIridescenceThicknessMax,
    /// `iridescenceThicknessRange[ 0 ]`.
    MaterialIridescenceThicknessMin,
    /// `MeshStandardMaterial.normalScale`.
    MaterialNormalScale,
    /// `MeshStandardMaterial.aoMapIntensity` — the scale in `materialAO`'s
    /// `tex.r.sub( 1 ).mul( aoMapIntensity ).add( 1 )`.
    MaterialAoMapIntensity,
    /// `material.lightMapIntensity` (`MeshBasicMaterial`, `MeshLambertMaterial`,
    /// `MeshPhongMaterial`, `MeshStandardMaterial`, `MeshToonMaterial`) — the
    /// scale in `materialLightMap`'s `tex.rgb.mul( lightMapIntensity )`.
    MaterialLightMapIntensity,
    /// `PointsMaterial.size` — `materialPointSize`.
    MaterialPointSize,
    /// `toneMappingExposure` — `renderer.toneMappingExposure`.
    ToneMappingExposure,
    /// `reference( 'bindMatrix', 'mat4' )` / `reference( 'bindMatrixInverse',
    /// 'mat4' )` — `SkinnedMesh`'s two bind matrices, in the object group.
    BindMatrix,
    /// `bindMatrixInverse` — see [`UniformSource::BindMatrix`].
    BindMatrixInverse,
    /// `reference( 'center', 'vec2', object )` — `Sprite.center`, read by
    /// `SpriteNodeMaterial.setupPositionView()`, in the object group.
    ObjectCenter,
    /// `VelocityNode.previousModelWorldMatrix` — the object's `matrixWorld`
    /// as the last velocity draw of it left it (`getPreviousMatrix( object
    /// )`), in the object group. See [`crate::nodes::velocity`].
    PreviousModelWorldMatrix,
    /// `VelocityNode.currentProjectionMatrix` — the camera's projection, or
    /// the unjittered one `setProjectionMatrix()` handed the node, in the
    /// render group.
    VelocityProjectionMatrix,
    /// `VelocityNode.previousProjectionMatrix` — last frame's
    /// `currentProjectionMatrix` for this camera.
    PreviousProjectionMatrix,
    /// `VelocityNode.previousCameraViewMatrix` — last frame's
    /// `camera.matrixWorldInverse` for this camera.
    PreviousCameraViewMatrix,
    /// A plain `uniform( value )` the example supplies.
    Value(Vec<f64>),
    /// `uniform( value )` whose `.value` is written between draws — three.js'
    /// `UniformNode` is always this; [`UniformSource::Value`] is the special
    /// case of one that never moves. `SSAAPassNode.sampleWeight` is the port's
    /// first: one node, one program, eight different values across the eight
    /// accumulation draws of a frame.
    Settable(SettableValue),
    /// A `uniform()` inside a node whose `updateType` is
    /// `NodeUpdateType.OBJECT`: three calls `node.update( frame )` with
    /// `frame.object` set to the render object about to be drawn, and the
    /// callback writes `uniformNode.value` before that object's bindings are
    /// built. `webgpu_instance_uniform`'s `InstanceUniformNode` is the ladder's
    /// first — twelve meshes share one material, one program and one pipeline,
    /// and differ only in the three floats this uniform resolves to.
    ///
    /// The port has no place to hang a `mesh.color` the way the page does, so
    /// the callback receives the object itself and answers from whatever the
    /// application keyed to it. See `docs/nodes.md` §18.
    ObjectUpdate(ObjectUpdate),
    /// A uniform that holds a *reference* to an application object and reads
    /// it when the buffer is written: three's `uniform( skinnedMesh.bindMatrix
    /// )` (the `Matrix4` itself, not a copy of its elements) and
    /// `objectWorldMatrix( object3d )` (`Object3DNode` with an explicit object,
    /// whose `OBJECT` update reads `object3d.matrixWorld`). Unlike
    /// [`UniformSource::ObjectUpdate`] it does not need a render object, so a
    /// compute kernel can read it: `computeSkinning()` and
    /// `webgpu_skinning_points` are the first. See `docs/nodes.md` §44.
    Live(LiveValue),
    /// `cameraNormalMatrix` — `uniform( camera.normalMatrix )`. Nothing in
    /// `WebGPURenderer` ever writes a camera's `normalMatrix` (only
    /// `WebGLRenderer` updates it, and only for the objects it draws), so
    /// three uploads the identity it was constructed with, and so does this.
    CameraNormalMatrix,
    /// `materialRefractionRatio` — `uniform( 0 ).onObjectUpdate( ( { material
    /// } ) => material.refractionRatio )`.
    MaterialRefractionRatio,
    /// `highpModelViewMatrix` — `uniform( 'mat4' ).onObjectUpdate( … )`:
    /// `camera.matrixWorldInverse * object.matrixWorld`, multiplied on the CPU
    /// in double precision.
    HighpModelViewMatrix,
    /// `highpModelNormalViewMatrix` — the normal matrix of
    /// [`UniformSource::HighpModelViewMatrix`]'s product, per object.
    HighpModelNormalViewMatrix,
    /// `Object3DNode`'s vector and scalar scopes: [`Object3DScope`] of the
    /// object drawn (`ModelNode`, `object` `None`) or of an explicit one
    /// (`objectPosition( object3d )` and friends), whose `matrixWorld` the
    /// [`LiveValue`] reads when the buffer is written.
    Object3D {
        /// Which value of the object.
        scope: Object3DScope,
        /// The explicit object's `matrixWorld`, or `None` for the object the
        /// draw is for. For [`Object3DScope::Direction`] it is instead the
        /// world direction itself, three components, since
        /// `getWorldDirection()` refreshes the object's world matrix and
        /// negates for a camera, which only the object can tell.
        object: Option<LiveValue>,
    },
}

/// `Object3DNode`'s scopes other than `WORLD_MATRIX`, which has a uniform
/// source of its own ([`UniformSource::ModelWorldMatrix`], or
/// [`UniformSource::Live`] for an explicit object).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Object3DScope {
    /// `POSITION` — `setFromMatrixPosition( matrixWorld )`, a `vec3`.
    Position,
    /// `SCALE` — `setFromMatrixScale( matrixWorld )`, a `vec3`.
    Scale,
    /// `DIRECTION` — `getWorldDirection()`, the normalised `+z` column of
    /// `matrixWorld`, a `vec3`.
    Direction,
    /// `VIEW_POSITION` — the world position through the rendering camera's
    /// `matrixWorldInverse`, a `vec3`.
    ViewPosition,
    /// `RADIUS` — the bounding sphere of the *drawn* object's geometry,
    /// through the scoped object's `matrixWorld`, a `float`. three reads
    /// `frame.object.geometry` even for an explicit object, and so does this.
    Radius,
}

/// The reader behind [`UniformSource::Live`]. Compares and hashes by
/// identity, like [`SettableValue`]: the value moves, the program does not.
#[derive(Clone)]
pub struct LiveValue(Rc<dyn Fn() -> Vec<f64>>);

impl LiveValue {
    pub(crate) fn new(read: impl Fn() -> Vec<f64> + 'static) -> Self {
        Self(Rc::new(read))
    }

    /// The value now.
    pub(crate) fn get(&self) -> Vec<f64> {
        (self.0)()
    }
}

impl std::fmt::Debug for LiveValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("LiveValue")
            .field(&Rc::as_ptr(&self.0).cast::<u8>())
            .finish()
    }
}

impl PartialEq for LiveValue {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl std::hash::Hash for LiveValue {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        (Rc::as_ptr(&self.0).cast::<u8>() as usize).hash(state);
    }
}

/// The callback behind [`UniformSource::ObjectUpdate`] — `Node.update( frame )`
/// narrowed to what an `OBJECT` update can read: `frame.object`, and the one
/// question a node on the ladder asks `frame.renderer`, `isOccluded()`.
///
/// Two callbacks with the same behaviour are still two uniforms, so, like
/// [`SettableValue`], this compares by identity: the program cache must not
/// merge two per-object uniforms that happen to be spelled alike.
#[derive(Clone)]
pub struct ObjectUpdate(Rc<ObjectUpdateFn>);

/// The body of an [`ObjectUpdate`]: three's `update( frame )`.
pub(crate) type ObjectUpdateFn = dyn Fn(&NodeFrame) -> Vec<f64>;

/// `NodeFrame` as a node's `update( frame )` sees it, narrowed to what the
/// port's object-update uniforms read.
///
/// three hands the node the whole frame, renderer included; the port hands it
/// the render object and the renderer's occlusion results for the render
/// context being drawn, which is all `frame.renderer.isOccluded( object )`
/// reads (`webgpu_occlusion`, `docs/nodes.md` §39).
///
/// This is the per-object view of the frame. The frame itself — `frameId`,
/// `renderId`, the clock and the update maps — is
/// [`NodeFrameState`](crate::nodes::NodeFrameState), which the renderer owns
/// (`docs/nodes.md` §57).
#[derive(Clone, Copy)]
pub struct NodeFrame<'a> {
    /// `frame.object` — the render object about to be drawn.
    pub object: &'a crate::core::Object3D,
    /// `renderContextData.occluded` for the current render context: the ids of
    /// the objects whose last resolved occlusion query drew no samples. `None`
    /// until a query has resolved, as three's is `undefined`.
    pub(crate) occluded: Option<&'a std::collections::HashSet<u32>>,
}

impl<'a> NodeFrame<'a> {
    /// `frame.renderer.isOccluded( object )`: whether the last occlusion query
    /// the current render context resolved for `object` drew no samples.
    /// Results arrive asynchronously, a frame or more after the draw, so this
    /// is `false` until then, as it is in three.
    pub fn is_occluded(&self, object: &crate::core::Object3D) -> bool {
        self.occluded.is_some_and(|set| set.contains(&object.id))
    }
}

impl ObjectUpdate {
    pub(crate) fn new(update: impl Fn(&crate::core::Object3D) -> Vec<f64> + 'static) -> Self {
        Self(Rc::new(move |frame: &NodeFrame| update(frame.object)))
    }

    /// An update that reads more of the frame than its object — see
    /// [`NodeFrame`].
    pub(crate) fn with_frame(update: impl Fn(&NodeFrame) -> Vec<f64> + 'static) -> Self {
        Self(Rc::new(update))
    }

    /// `node.update( frame )` — the value for one render object.
    pub(crate) fn value(&self, frame: &NodeFrame) -> Vec<f64> {
        (self.0)(frame)
    }
}

impl std::fmt::Debug for ObjectUpdate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ObjectUpdate")
            .field(&Rc::as_ptr(&self.0).cast::<u8>())
            .finish()
    }
}

impl PartialEq for ObjectUpdate {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl std::hash::Hash for ObjectUpdate {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        (Rc::as_ptr(&self.0).cast::<u8>() as usize).hash(state);
    }
}

/// The cell behind [`UniformSource::Settable`]. Two cells with equal contents
/// are still two uniforms, so this compares and hashes by identity — a value
/// that changes must not be part of any cache key.
#[derive(Clone, Debug)]
pub struct SettableValue(Rc<RefCell<Vec<f64>>>);

impl SettableValue {
    /// Wraps a starting value.
    pub fn new(values: Vec<f64>) -> Self {
        Self(Rc::new(RefCell::new(values)))
    }

    /// `uniformNode.value = …`.
    pub fn set(&self, values: Vec<f64>) {
        *self.0.borrow_mut() = values;
    }

    /// `uniformNode.value`.
    pub fn get(&self) -> Vec<f64> {
        self.0.borrow().clone()
    }
}

impl PartialEq for SettableValue {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl std::hash::Hash for SettableValue {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        (Rc::as_ptr(&self.0) as *const u8 as usize).hash(state);
    }
}

impl UniformSource {
    pub(crate) fn update_type(&self) -> UpdateType {
        match self {
            UniformSource::ModelWorldMatrix
            | UniformSource::ModelNormalMatrix
            | UniformSource::MaterialColor
            | UniformSource::MaterialOpacity
            | UniformSource::MaterialAlphaTest
            | UniformSource::MaterialReflectivity
            | UniformSource::MaterialEnvIntensity
            | UniformSource::MaterialShininess
            | UniformSource::MaterialSpecular
            | UniformSource::MaterialEmissive
            | UniformSource::MaterialEmissiveIntensity
            | UniformSource::ModelWorldMatrixInverse
            | UniformSource::MaterialLineWidth
            | UniformSource::MaterialMetalness
            | UniformSource::MaterialRoughness
            | UniformSource::MaterialBumpScale
            | UniformSource::MaterialIor
            | UniformSource::MaterialSpecularIntensity
            | UniformSource::MaterialSpecularColor
            | UniformSource::MaterialSheen
            | UniformSource::MaterialSheenColor
            | UniformSource::MaterialSheenRoughness
            | UniformSource::MaterialDiffuseRoughness
            | UniformSource::MaterialIridescence
            | UniformSource::MaterialIridescenceIor
            | UniformSource::MaterialIridescenceThicknessMax
            | UniformSource::MaterialIridescenceThicknessMin
            | UniformSource::MaterialNormalScale
            | UniformSource::MaterialAoMapIntensity
            | UniformSource::MaterialLightMapIntensity
            | UniformSource::MaterialPointSize
            | UniformSource::EnvRotationMatrix
            | UniformSource::MorphBase
            | UniformSource::BindMatrix
            | UniformSource::BindMatrixInverse
            | UniformSource::ObjectCenter
            | UniformSource::PreviousModelWorldMatrix
            | UniformSource::Value(_)
            | UniformSource::Settable(_)
            | UniformSource::ObjectUpdate(_)
            | UniformSource::Live(_) => UpdateType::Object,
            _ => UpdateType::Render,
        }
    }
}

/// `UniformNode`.
#[derive(Debug)]
pub struct UniformNode {
    /// Where the uniform's bytes come from.
    pub source: UniformSource,
    /// The uniform's WGSL type.
    pub ty: Type,
    /// Which uniform block the uniform is bound in.
    pub group: UniformGroup,
    /// Three names camera uniforms explicitly and numbers the rest
    /// `nodeUniformN`. A `Cell` because three's `UniformNode.setName()`
    /// renames the node in place, so every reference to it sees the new name
    /// ([`NodeRef::set_name`] on a uniform).
    pub name: std::cell::Cell<Option<&'static str>>,
}

/// Where an array-typed uniform buffer's contents come from — `BufferNode`.
#[derive(Clone, PartialEq)]
#[non_exhaustive]
pub enum BufferSource {
    /// `InstancedMesh.instanceMatrix`.
    InstanceMatrix,
    /// `InstancedMesh.instanceColor` — `setColorAt()`'s three floats per
    /// instance, always a `stepMode: 'instance'` vertex buffer (three.js wraps
    /// it in a fresh `InstancedBufferAttribute( colors.array, 3 )` and never
    /// takes the uniform branch for it).
    InstanceColor,
    /// `RangeNode` resolved per instance:
    /// `lerp( min[c], max[c], Math.random() )`. `min`/`max` are the `Vector4`s
    /// `RangeNode.setup()` builds out of the min/max values: a scalar splats
    /// into all four components, a `Color` fills `xyz` and leaves `w` at 1, and
    /// any other vector takes `x`, `y`, `z || 0`, `w || 0` — so a `vec3` range
    /// has `w` **0** at both ends, not 1.
    Range {
        /// The low end of the range, one component per channel.
        min: [f64; 4],
        /// The high end of the range, one component per channel.
        max: [f64; 4],
    },
    /// `Morph.js`' `uniformArray( mesh.morphTargetInfluences, 'float' )` — one
    /// `vec4` per morph target with the influence in `.x`.
    MorphInfluences,
    /// `uniformArray( values )` — a constant array the application supplies,
    /// already padded to one `vec4` per element the way
    /// `UniformArrayNode.updateBuffer()` pads it. `BloomNode.bloomTintColors`
    /// is the ladder's first; `GodraysNode`, `SSAONode`, `GTAONode` and
    /// `SSRNode` want the same node.
    ///
    /// The values travel in an `Rc` so the buffer can be cached on the node's
    /// identity and uploaded once, like [`BufferSource::Attribute`].
    UniformArray(Rc<Vec<f32>>),
    /// `Camera.js`' `uniformArray( matrices ).setGroup( renderGroup )
    /// .setName( 'cameraViewMatrices' )` — every `ArrayCamera` sub-camera's
    /// `matrixWorldInverse`, rewritten per render. A render-group buffer, the
    /// only one.
    CameraViewMatrices,
    /// The same for `cameraProjectionMatrices` — the sub-cameras'
    /// `projectionMatrix`.
    CameraProjectionMatrices,
    /// `ClippingNode`'s `uniformArray( intersectionPlanes ).setGroup(
    /// renderGroup )` — the clipping context's intersection planes in view
    /// space, one `vec4` each, written per draw from the draw's context.
    ClippingIntersection,
    /// The same for the context's `unionPlanes`.
    ClippingUnion,
    /// `buffer( previousBoneMatrices, 'mat4', bones )` — `Skinning.js`'
    /// `getPreviousSkinnedPosition()`: the skeleton's bone matrices as they
    /// were before this frame's `skeleton.update()`, for the
    /// `positionPrevious` of a draw into a `velocity` MRT.
    PreviousBoneMatrices,
    /// `referenceBuffer( 'skeleton.boneMatrices', 'mat4', bones )` — the
    /// skeleton's bone matrices as one `array< mat4x4<f32>, N >`. Three falls
    /// back to a bone *texture* when `bones * 64` passes the uniform buffer
    /// limit; the ladder's skeletons fit (Michelle is 65 bones, 4160 bytes).
    BoneMatrices,
    /// A per-instance attribute the caller fills itself — three.js'
    /// `new InstancedBufferAttribute( array, itemSize )` on the geometry, e.g.
    /// `BatchedText`'s `aGlyphUV` / `aGlyphBounds` / `aColor` / `aOpacity`.
    /// Only ever a vertex buffer: the uniform path has no equivalent.
    Attribute(Rc<Vec<f32>>),
    /// `instancedArray( count, type )` — a GPU-only *storage* buffer
    /// (`StorageBufferNode`, `StorageInstancedBufferAttribute` with no array
    /// behind it). Zero-filled once by the renderer and never re-uploaded: the
    /// only thing that ever writes it is a compute pass.
    ///
    /// Declared `array< T >` with **no** element count, because
    /// `WGSLNodeBuilder.getStorageAccess()` emits a runtime-sized array; the
    /// `count` on the [`BufferNode`] is only what the renderer allocates.
    Storage,
    /// `instancedArray( count, 'uint' ).toAtomic()` — [`BufferSource::Storage`]
    /// whose elements are declared `atomic< T >`
    /// (`WGSLNodeBuilder.getUniforms()`'s `bufferNode.isAtomic` arm), so the
    /// only way to touch one is an [`atomic function`](Node::Atomic).
    AtomicStorage,
    /// `storage( indirectAttribute, struct( … ), count )` — an
    /// `IndirectStorageBufferAttribute` read and written through a named WGSL
    /// struct (`StructTypeNode`). Declared as the struct itself rather than
    /// wrapped in `{ value : array< … > }`, which is
    /// `WGSLNodeBuilder.isCustomStruct()`'s single-struct case. `init` is the
    /// attribute's `Uint32Array`, uploaded once when the GPU buffer is made;
    /// see [`crate::core::IndirectStorageBufferAttribute`].
    Struct {
        /// The struct's name and member list.
        layout: Rc<StructLayout>,
        /// The attribute's initial contents, uploaded once.
        init: Rc<Vec<u32>>,
    },
    /// `storage( attribute, type, count )` over an attribute that *has* a CPU
    /// array — `computeSkinning()`'s `storage( new InstancedBufferAttribute(
    /// position.array, 3 ), 'vec3' )`, or a `StorageInstancedBufferAttribute(
    /// array, itemSize )`. Uploaded once, when the GPU buffer is made, then
    /// left to the kernels. `init` is the array's bits already laid out at the
    /// storage stride (a `vec3` padded to 16 bytes, as
    /// `WebGPUAttributeUtils.createAttribute()` pads it).
    ///
    /// `read_only` is `.toReadOnly()`: `var<storage, read>` in a kernel too.
    StorageData {
        /// The array's bits, already laid out at the storage stride.
        init: Rc<Vec<u32>>,
        /// `.toReadOnly()` — `var<storage, read>` rather than `read_write`.
        read_only: bool,
    },
    /// `buffer( skeleton.boneMatrices, 'mat4', bones )` — `computeSkinning()`'s
    /// bone matrices: a plain uniform `BufferNode` over the skeleton's own
    /// array rather than `SkinningNode`'s `referenceBuffer`, so it is resolved
    /// from the skeleton it names and not from a render object. The
    /// skeleton's `OnObjectUpdate` (`skeleton.update()`, once per frame) runs
    /// when the buffer is written.
    SkeletonBoneMatrices(SkeletonRef),
    /// `LightProbeNode.lightProbe` — `uniformArray( 9 × Vector3 )` holding the
    /// probe at this index of the renderer's light list, each coefficient
    /// already multiplied by `light.intensity` (`LightProbeNode.update()`).
    /// One `vec4` per coefficient, rewritten per draw like
    /// [`BufferSource::MorphInfluences`], because the probe can change between
    /// frames while the program does not.
    LightProbe(usize),
    /// `uniformArray( values )` over an array the application keeps changing
    /// — `LightProbeHelper`'s `uniformArray( lightProbe.sh.coefficients )`,
    /// which shares the probe's own `Vector3`s. The reader returns the
    /// elements already padded to four floats each; the buffer is rewritten
    /// per draw. Compares by identity, as [`UniformSource::Live`] does.
    Live(LiveValue),
}

impl BufferSource {
    /// A `var<storage>` binding rather than a `var<uniform>` one — every
    /// `StorageBufferNode` shape the port has.
    pub(crate) fn is_storage(&self) -> bool {
        matches!(
            self,
            BufferSource::Storage
                | BufferSource::AtomicStorage
                | BufferSource::Struct { .. }
                | BufferSource::StorageData { .. }
        )
    }

    /// The group the binding joins and the name three gives it: the two
    /// camera arrays are `renderGroup` buffers named by `setName()`, the
    /// clipping planes are unnamed `renderGroup` buffers, and everything else
    /// is an object-group `NodeBuffer_N` (`None`).
    pub(crate) fn group_and_name(&self) -> (UniformGroup, Option<&'static str>) {
        match self {
            BufferSource::CameraViewMatrices => (UniformGroup::Render, Some("cameraViewMatrices")),
            BufferSource::CameraProjectionMatrices => {
                (UniformGroup::Render, Some("cameraProjectionMatrices"))
            }
            // `setGroup( renderGroup )` without a name: `NodeBuffer_N`.
            BufferSource::ClippingIntersection | BufferSource::ClippingUnion => {
                (UniformGroup::Render, None)
            }
            _ => (UniformGroup::Object, None),
        }
    }
}

/// The skeleton behind [`BufferSource::SkeletonBoneMatrices`], compared by
/// identity: one skeleton is one buffer however many kernels read it.
#[derive(Clone)]
pub struct SkeletonRef(pub(crate) Rc<RefCell<crate::objects::Skeleton>>);

impl PartialEq for SkeletonRef {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

/// One member of a [`StructLayout`] — an entry of `struct( { … } )`'s object.
#[derive(Clone, Debug, PartialEq)]
pub struct StructMember {
    /// The member's name in WGSL.
    pub name: &'static str,
    /// The member's WGSL type.
    pub ty: Type,
    /// `{ type: 'uint', atomic: true }` — declared `atomic< u32 >`.
    pub atomic: bool,
}

/// `struct( members, name )` — `StructTypeNode`: a named WGSL struct whose
/// members keep their declaration order.
#[derive(Clone, Debug, PartialEq)]
pub struct StructLayout {
    /// The struct's name in WGSL.
    pub name: &'static str,
    /// The struct's members, in declaration order.
    pub members: Vec<StructMember>,
}

impl StructLayout {
    /// `struct DrawBuffer {\n\tvertexCount : u32,\n … };` —
    /// `WGSLNodeBuilder.getStructMembers()`'s spelling, `atomic< u32 >` with
    /// the spaces three puts inside it.
    pub(crate) fn wgsl(&self) -> String {
        let members: Vec<String> = self
            .members
            .iter()
            .map(|m| {
                let ty = crate::nodes::wgsl::type_name(m.ty);
                if m.atomic {
                    format!("\t{} : atomic< {ty} >", m.name)
                } else {
                    format!("\t{} : {ty}", m.name)
                }
            })
            .collect();
        format!("struct {} {{\n{}\n}};", self.name, members.join(",\n"))
    }

    /// The member's index, by name — `.get( name )`.
    pub(crate) fn member(&self, name: &str) -> usize {
        self.members
            .iter()
            .position(|m| m.name == name)
            .unwrap_or_else(|| panic!("three-rs: struct {} has no member {name}", self.name))
    }
}

/// `workgroupArray( type, count )` — `WorkgroupInfoNode`: a `var<workgroup>`
/// array every invocation of one workgroup shares.
///
/// Its identity is the array: two `workgroupArray()` calls are two arrays,
/// each named `WorkgroupArray_N` by the builder in first-use order.
#[derive(Debug)]
pub struct WorkgroupArrayDef {
    /// The array's element type.
    pub element_ty: Type,
    /// The number of elements.
    pub count: usize,
    /// `.toAtomic()`.
    pub atomic: bool,
}

/// The identity of one `BufferNode` / `InstanceBuffer`, from a never-reused
/// counter — the same shape as `BufferGeometry.id` and `Material.id`.
///
/// The renderer caches a `range()` buffer's one-and-only random fill under
/// this. It used to be `Rc::as_ptr( &buffer )`, which a *later* buffer
/// inherits the moment this one's material is dropped, and which would then be
/// served the dead buffer's fill: the same freed-address bug as issue #58's
/// geometry cache, silent rather than loud because the contents are random
/// either way.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct BufferId(usize);

impl BufferId {
    pub(crate) fn next() -> Self {
        thread_local! {
            static BUFFER_ID: Cell<usize> = const { Cell::new(0) };
        }
        BUFFER_ID.with(|id| {
            let next = id.get();
            id.set(next + 1);
            BufferId(next)
        })
    }

    /// The number itself, for keying on.
    pub fn get(&self) -> usize {
        self.0
    }
}

/// The CPU-side buffer behind one or more *instanced vertex attributes* —
/// three.js' `InstancedBufferAttribute` / `InstancedInterleavedBuffer`.
///
/// `RangeNode.setup()` and `createInstanceMatrixNode()` both branch on
/// `uniformBufferSize <= builder.getUniformBufferLimit()`: under the limit the
/// data is a uniform buffer indexed by `instanceIndex` ([`BufferNode`]), over it
/// an instanced attribute, which is what lifts the ~1024-instance cap the
/// 64 KiB uniform binding imposes. `Rc` identity is the buffer's identity, so
/// two `range( 0, 1 )` calls are two buffers with two different random fills,
/// exactly as two `RangeNode`s are in three.js.
#[derive(Debug)]
pub struct InstanceBuffer {
    /// This buffer's identity — see [`BufferId`].
    pub id: BufferId,
    /// Where the buffer's contents come from.
    pub source: BufferSource,
    /// The instance count — `InstancedBufferAttribute.count`.
    pub count: usize,
    /// Floats per instance, i.e. the attribute stride in components: 16 for the
    /// interleaved instance matrix (`new InstancedInterleavedBuffer( array, 16,
    /// 1 )`), 4 for a `range()`.
    pub item_size: usize,
    /// The buffer steps once per *vertex*: `attributeArray()`'s
    /// `StorageBufferAttribute` read through `.toAttribute()`, which is not an
    /// `InstancedBufferAttribute`. `false` for everything else here.
    pub per_vertex: bool,
}

/// `BufferNode` — `buffer( array, type, count )`.
#[derive(Debug)]
pub struct BufferNode {
    /// This buffer's identity — see [`BufferId`].
    pub id: BufferId,
    /// Where the buffer's contents come from.
    pub source: BufferSource,
    /// The type of one element.
    pub element_ty: Type,
    /// The number of elements.
    pub count: usize,
}

/// The texture a `TextureNode` / `CubeTextureNode` samples.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum TextureSource {
    /// `texture( Texture )` — a sampled `texture_2d<f32>`.
    Texture2D(Texture),
    /// `texture( DepthTexture )` — a sampled `texture_depth_2d`.
    Depth(DepthTexture),
    /// A `DepthTexture` with `compareFunction` set, bound as
    /// `texture_depth_2d` + `sampler_comparison` and read with
    /// `textureSampleCompare` — `ShadowNode`'s shadow map.
    ShadowMap(DepthTexture),
    /// `cubeTexture( CubeTexture )` — a sampled `texture_cube<f32>`.
    Cube(CubeTexture),
    /// `DataArrayTexture` — the morph-target data texture.
    DataArray(DataArrayTexture),
    /// `DataTexture` — `BatchedMesh`' matrices / colours / indirect tables.
    Data(DataTexture),
    /// A point light's shadow map — `cubeTexture( CubeDepthTexture )`.
    CubeDepth(CubeDepthTexture),
    /// `texture3D( Data3DTexture | Storage3DTexture )` — a sampled
    /// `texture_3d<f32>`.
    Texture3D(Data3DTexture),
    /// `storageTexture( StorageTexture )` — a `texture_storage_2d<format,
    /// access>`, what `textureStore()` writes and `.load()` reads.
    Storage(Texture, StorageAccess),
    /// `storageTexture( Storage3DTexture )` — `texture_storage_3d`.
    Storage3D(Data3DTexture, StorageAccess),
}

impl TextureSource {
    /// The texture's id — `Texture.id`, whichever class it is.
    pub(crate) fn id(&self) -> usize {
        match self {
            TextureSource::Texture2D(t) | TextureSource::Storage(t, _) => t.id(),
            TextureSource::Depth(t) | TextureSource::ShadowMap(t) => t.id(),
            TextureSource::Cube(t) => t.id(),
            TextureSource::DataArray(t) => t.id(),
            TextureSource::Data(t) => t.id(),
            TextureSource::CubeDepth(t) => t.id(),
            TextureSource::Texture3D(t) | TextureSource::Storage3D(t, _) => t.id(),
        }
    }

    /// Bound as a storage texture rather than a sampled one. A texture a
    /// kernel stores into and a material samples is two bindings of one
    /// texture, so the builder keys its binding names on this as well as on
    /// [`id`](Self::id).
    pub(crate) fn is_storage_binding(&self) -> bool {
        matches!(
            self,
            TextureSource::Storage(..) | TextureSource::Storage3D(..)
        )
    }
}

/// `NodeAccess` for a `StorageTextureNode` — the `access` of its
/// `texture_storage_*` declaration.
///
/// `StorageTextureNode`'s default is `WRITE_ONLY`; `WGSLNodeBuilder
/// .getStorageAccess()` forces `READ_ONLY` outside the compute stage, which the
/// builder applies when it declares the binding.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StorageAccess {
    /// `texture_storage_2d<format, write>` — `StorageTextureNode`'s default.
    #[default]
    WriteOnly,
    /// `texture_storage_2d<format, read>` — forced outside the compute stage.
    ReadOnly,
    /// `texture_storage_2d<format, read_write>`.
    ReadWrite,
}

impl StorageAccess {
    /// `WGSLNodeBuilder`'s `accessNames`.
    pub(crate) fn wgsl(self) -> &'static str {
        match self {
            StorageAccess::WriteOnly => "write",
            StorageAccess::ReadOnly => "read",
            StorageAccess::ReadWrite => "read_write",
        }
    }

    /// The `wgpu` spelling, for the bind group layout.
    pub(crate) fn wgpu(self) -> wgpu::StorageTextureAccess {
        match self {
            StorageAccess::WriteOnly => wgpu::StorageTextureAccess::WriteOnly,
            StorageAccess::ReadOnly => wgpu::StorageTextureAccess::ReadOnly,
            StorageAccess::ReadWrite => wgpu::StorageTextureAccess::ReadWrite,
        }
    }
}

impl TextureSource {
    /// The texture's liveness, for the renderer's view cache to sweep on; see
    /// [`TextureOwner`](crate::textures::TextureOwner).
    pub(crate) fn owner(&self) -> crate::textures::TextureOwner {
        match self {
            TextureSource::Texture2D(texture) => texture.owner(),
            TextureSource::Depth(depth) | TextureSource::ShadowMap(depth) => depth.owner(),
            TextureSource::Cube(cube) => cube.owner(),
            TextureSource::DataArray(data) => data.owner(),
            TextureSource::Data(data) => data.owner(),
            TextureSource::CubeDepth(cube) => cube.owner(),
            TextureSource::Texture3D(data) | TextureSource::Storage3D(data, _) => data.owner(),
            TextureSource::Storage(texture, _) => texture.owner(),
        }
    }
}

/// How a `TextureNode` reads its texture — `WGSLNodeBuilder.generateTexture*`.
#[derive(Clone, Debug)]
pub enum SampleMode {
    /// `textureSample( t, t_sampler, uv )`.
    Sample,
    /// `textureSampleLevel( t, t_sampler, uv, level )`.
    Level(NodeRef),
    /// `textureSampleBias( t, t_sampler, uv, bias )` — `textureNode.bias(
    /// value )`. `FXAANode` samples its input at a bias of `-100`, pinning
    /// every tap to the top mip.
    Bias(NodeRef),
    /// `textureSampleGrad( t, t_sampler, uv, gradX, gradY )` —
    /// `textureNode.grad( gradX, gradY )` (`generateTextureGrad()`), each
    /// gradient built as a `vec2`.
    Grad(NodeRef, NodeRef),
    /// `textureGather( component, t, t_sampler, uv[, offset] )` —
    /// `textureNode.gather( component )`, optionally `.offset( ivec2 )`
    /// (`WGSLNodeBuilder.generateTextureGather()`): one channel of the four
    /// texels a bilinear tap would read, from mip level 0.
    Gather {
        /// Which channel to gather (0–3).
        component: NodeRef,
        /// An optional texel offset — `.offset( ivec2 )`.
        offset: Option<NodeRef>,
    },
    /// `textureGatherCompare( t, t_sampler, uv, depth[, offset] )` —
    /// `depthNode.gather().compare( depth )` on a depth texture with a
    /// comparison sampler (`generateTextureGatherCompare()`): the four
    /// texels' comparison results.
    GatherCompare {
        /// The depth value each of the four texels is compared against.
        compare: NodeRef,
        /// An optional texel offset — `.offset( ivec2 )`.
        offset: Option<NodeRef>,
    },
    /// The non-filterable path: `textureLoad` against `textureDimensions`,
    /// with no sampler binding at all. What Three emits for a depth texture.
    Load,
    /// `textureLoad( t, coord, layer, u32( 0u ) )` on a 2-D-array texture —
    /// `textureLoad( … ).depth( layer )`, with no clamping and no sampler.
    LoadLayer(NodeRef),
    /// `textureSample( t, t_sampler, uv, i32( layer ) )` on a 2-D-array
    /// texture — `texture( map, uv ).depth( layer )`. Three builds the depth
    /// node as `'int'`, so a float layer arrives truncated by `i32()`.
    SampleLayer(NodeRef),
    /// `textureSampleCompare( t, t_sampler, uv, depth )` — the depth-compare
    /// read `ShadowFilterNode`'s `depthCompare` lowers to.
    Compare(NodeRef),
    /// `textureLoad( t, coord, u32( 0u ) )` — an unclamped texel fetch at an
    /// integer coordinate, what `Batch.js` reads its data textures with. When
    /// the node's type is a scalar the snippet is swizzled (`.x`), which is
    /// how Three's `textureLoad( … ).x` on the `r32uint` indirect table lands
    /// in one `u32` property.
    LoadTexel,
    /// `storageTexture( t ).load( coord )` — `textureLoad( t, coord )` on a
    /// `texture_storage_*`, which takes no level argument
    /// (`WGSLNodeBuilder.generateStorageTextureLoad()`).
    StorageLoad,
}

/// A WGSL builtin input.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Builtin {
    /// `@builtin( vertex_index )`.
    VertexIndex,
    /// `@builtin( instance_index )`.
    InstanceIndex,
    /// `@builtin( position )` in the fragment stage.
    FragCoord,
    /// `@builtin( front_facing )` — `FrontFacingNode`.
    FrontFacing,
    /// `invocationLocalIndex` — `@builtin( local_invocation_index )`, which
    /// `WGSLNodeBuilder.getInvocationLocalIndex()` adds to the compute entry
    /// point's parameters only when a kernel reads it.
    InvocationLocalIndex,
    /// `workgroupId` / `localId` / `globalId` / `numWorkgroups` —
    /// `ComputeBuiltinNode`. The compute entry point always declares these four
    /// (`WGSLNodeBuilder.getAttributes( 'compute' )`), so reading one only names
    /// the parameter.
    WorkgroupId,
    /// `@builtin( local_invocation_id )` — see [`Builtin::WorkgroupId`].
    LocalId,
    /// `@builtin( global_invocation_id )` — see [`Builtin::WorkgroupId`].
    GlobalId,
    /// `@builtin( num_workgroups )` — see [`Builtin::WorkgroupId`].
    NumWorkgroups,
    /// `subgroupSize` — `@builtin( subgroup_size )`. Three's
    /// `computeBuiltin( 'subgroupSize', 'uint' )` only writes the name: the
    /// parameter is the one `WGSLNodeBuilder.getAttributes( 'compute' )`
    /// appends after the four above when the device has `subgroups`. The port
    /// declares it, and `enable subgroups;`, when a kernel reads it or calls a
    /// subgroup function (`docs/nodes.md` §84).
    SubgroupSize,
    /// `invocationSubgroupIndex` — `@builtin( subgroup_invocation_id )`,
    /// registered by `WGSLNodeBuilder.getInvocationSubgroupIndex()` when the
    /// flow reads it, so it comes before the four fixed parameters, like
    /// [`Builtin::InvocationLocalIndex`].
    InvocationSubgroupIndex,
    /// `subgroupIndex` — `@builtin( subgroup_id )`, registered by
    /// `WGSLNodeBuilder.getSubgroupIndex()`; see
    /// [`Builtin::InvocationSubgroupIndex`].
    SubgroupIndex,
}

impl Builtin {
    pub(crate) fn name(self) -> &'static str {
        match self {
            Builtin::VertexIndex => "vertexIndex",
            Builtin::InstanceIndex => "instanceIndex",
            Builtin::FragCoord => "fragCoord",
            Builtin::FrontFacing => "isFront",
            Builtin::InvocationLocalIndex => "invocationLocalIndex",
            Builtin::WorkgroupId => "workgroupId",
            Builtin::LocalId => "localId",
            Builtin::GlobalId => "globalId",
            Builtin::NumWorkgroups => "numWorkgroups",
            Builtin::SubgroupSize => "subgroupSize",
            Builtin::InvocationSubgroupIndex => "invocationSubgroupIndex",
            Builtin::SubgroupIndex => "subgroupIndex",
        }
    }

    pub(crate) fn ty(self) -> Type {
        match self {
            Builtin::VertexIndex
            | Builtin::InstanceIndex
            | Builtin::InvocationLocalIndex
            | Builtin::SubgroupSize
            | Builtin::InvocationSubgroupIndex
            | Builtin::SubgroupIndex => Type::U32,
            Builtin::WorkgroupId
            | Builtin::LocalId
            | Builtin::GlobalId
            | Builtin::NumWorkgroups => Type::UVec3,
            Builtin::FragCoord => Type::Vec4,
            Builtin::FrontFacing => Type::Bool,
        }
    }
}

/// `SubgroupFunctionNode.getInputType()`: the longer of the two inputs'
/// types, a matrix counting as length 0, and `b`'s on a tie.
pub(crate) fn subgroup_input_type(a: Option<&NodeRef>, b: Option<&NodeRef>) -> Type {
    let length = |node: Option<&NodeRef>| match node.map(NodeRef::ty) {
        Some(ty) if ty.is_matrix() => 0,
        Some(ty) => ty.components(),
        None => 0,
    };
    if length(a) > length(b) {
        a.map(NodeRef::ty)
    } else {
        b.map(NodeRef::ty)
    }
    // Three returns `bType`, `null` for a one-input function whose input is a
    // matrix; the port's `Type` has no null, and a matrix is its own type.
    .or_else(|| a.map(NodeRef::ty))
    .unwrap_or(Type::Void)
}

/// `SubgroupFunctionNode.generateNodeType()`: `subgroupElect` is a `bool`,
/// `subgroupBallot` a `uvec4`, everything else its input type.
fn subgroup_type(method: &str, a: Option<&NodeRef>, b: Option<&NodeRef>) -> Type {
    match method {
        "subgroupElect" => Type::Bool,
        "subgroupBallot" => Type::UVec4,
        _ => subgroup_input_type(a, b),
    }
}

/// `VarNode` — a `var<private>` that caches its value's first use.
#[derive(Debug)]
pub struct VarDef {
    /// `toVar( 'name' )`; `None` numbers it `nodeVarN`. A `String` because a
    /// sub-build layer prefixes the name with its own (`NORMAL_normalView`) —
    /// see `docs/nodes.md` §7.
    pub name: Option<String>,
    /// The node whose value is cached.
    pub value: NodeRef,
    /// The variable's WGSL type.
    pub ty: Type,
}

/// `VaryingNode` — a value computed in the vertex stage and interpolated.
#[derive(Debug)]
pub struct VaryingDef {
    /// `toVarying( 'name' )`; `None` numbers it `nodeVaryingN`.
    pub name: Option<&'static str>,
    /// The node whose value is carried across stages.
    pub value: NodeRef,
    /// The varying's WGSL type.
    pub ty: Type,
    /// u32 varyings need `@interpolate(flat, either)`.
    pub flat: bool,
}

/// `ShaderNode` / `Fn()`. With `layout` set, a real WGSL `fn` is emitted and
/// called; without it the body is inlined at every call site — which is what
/// Three does for `saturation`/`hue`, and why their expressions appear
/// expanded three times in the dumped shader.
pub struct FnDef {
    /// The `fn`'s name in WGSL when `layout` is set; `None` numbers it.
    pub name: Option<&'static str>,
    /// The parameters' names and types, in declaration order.
    pub params: Vec<(&'static str, Type)>,
    /// The return type.
    pub ret: Type,
    /// Whether a real WGSL `fn` is emitted, rather than inlining the body.
    pub layout: bool,
    /// Builds the call's result node from the argument nodes.
    #[allow(clippy::type_complexity)]
    pub body: Box<dyn Fn(&[NodeRef]) -> NodeRef>,
}

impl std::fmt::Debug for FnDef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FnDef").field("name", &self.name).finish()
    }
}

/// A node type defined outside the crate: the port of `class MyNode extends
/// Node` with a `setup( builder )` override, the shape every addon in
/// `examples/jsm/tsl/` has.
///
/// **`setup` composes; it never emits.** It returns a graph of the existing
/// [`Node`] variants, and the builder builds that graph in its place. It
/// cannot write a WGSL statement of its own, declare a binding, or change how
/// a variant is generated. Three's addons never override `generate` either:
/// they override `setup` (and `updateBefore`, which is #162). A node that needs
/// a statement shape nothing can compose into belongs in the crate as a
/// variant, where the exhaustive `match` in the builder and the dump gates see
/// it (#155 decision 2).
///
/// The builder calls `setup` once per build, the first time it reaches the
/// node, and keeps the result as three keeps `nodeProperties.outputNode`: in
/// the builder's per-build data, keyed on the node's identity and scoped by
/// [`isolate`](crate::nodes::tsl::isolate). Two reaches of one
/// `Node::Custom` share one expansion; two `Node::Custom`s over equal structs
/// are two nodes. See `docs/nodes.md` §45.
///
/// ```ignore
/// struct Double(NodeRef);
///
/// impl CustomNode for Double {
///     fn type_name(&self) -> &'static str { "DoubleNode" }
///     fn node_type(&self) -> Type { self.0.ty() }
///     fn setup(&self, _: &NodeBuilder) -> NodeRef { self.0.clone().mul(2.0) }
/// }
///
/// let doubled = custom(Double(uv().x()));
/// ```
pub trait CustomNode {
    /// three's `static get type()`: the class name, `'RGBShiftNode'`. Only
    /// `Debug` output and panic messages read it.
    fn type_name(&self) -> &'static str;

    /// `Node.getNodeType( builder )`: the WGSL type of the value `setup`
    /// returns. The port needs it before the build, because the TSL methods
    /// that wrap this node (`.mul()`, `.x()`, …) type their result eagerly.
    fn node_type(&self) -> Type;

    /// `Node.isCacheable( builder )`: whether a node reached more than once
    /// is built into a var once and read from there. r187dev's `Node.build()`
    /// does that for every cacheable node with a value (`cacheResult`), not
    /// only for `TempNode`s, so the default is `true`, as three's is.
    /// `false` builds the output again at every reach.
    fn is_cacheable(&self) -> bool {
        true
    }

    /// `Node.setup( builder )`: the graph this node stands for.
    ///
    /// `builder` is shared, not mutable: the only thing to ask it is
    /// [`context`](crate::nodes::NodeBuilder::context), three's
    /// `builder.context`, as installed by an enclosing
    /// [`context`](crate::nodes::tsl::context) node. The TSL functions called
    /// here see the same context through the crate's own accessors.
    fn setup(&self, builder: &crate::nodes::NodeBuilder) -> NodeRef;

    /// `updateBeforeType`. Anything but `None` puts the node in the
    /// material's update-before list; see `docs/nodes.md` §57.
    fn update_before_type(&self) -> crate::nodes::NodeUpdateType {
        crate::nodes::NodeUpdateType::None
    }
    /// `updateType`.
    fn update_type(&self) -> crate::nodes::NodeUpdateType {
        crate::nodes::NodeUpdateType::None
    }
    /// `updateAfterType`.
    fn update_after_type(&self) -> crate::nodes::NodeUpdateType {
        crate::nodes::NodeUpdateType::None
    }
    /// `updateBefore( frame )`. `false` leaves the guard untouched, as
    /// three's `=== false` does. See [`NodeUpdate`](crate::nodes::NodeUpdate).
    fn update_before(&self, _renderer: &mut crate::renderer::Renderer) -> bool {
        true
    }
    /// `update( frame )`.
    fn update(&self, _renderer: &mut crate::renderer::Renderer) -> bool {
        true
    }
    /// `updateAfter( frame )`.
    fn update_after(&self, _renderer: &mut crate::renderer::Renderer) -> bool {
        true
    }
}

/// A [`CustomNode`] seen through [`NodeUpdate`](crate::nodes::NodeUpdate), so
/// the renderer drives it like any other updating node.
pub(crate) struct CustomUpdate(pub(crate) Rc<dyn CustomNode>);

impl crate::nodes::NodeUpdate for CustomUpdate {
    fn update_before_type(&self) -> crate::nodes::NodeUpdateType {
        self.0.update_before_type()
    }
    fn update_type(&self) -> crate::nodes::NodeUpdateType {
        self.0.update_type()
    }
    fn update_after_type(&self) -> crate::nodes::NodeUpdateType {
        self.0.update_after_type()
    }
    fn update_before(&self, renderer: &mut crate::renderer::Renderer) -> bool {
        self.0.update_before(renderer)
    }
    fn update(&self, renderer: &mut crate::renderer::Renderer) -> bool {
        self.0.update(renderer)
    }
    fn update_after(&self, renderer: &mut crate::renderer::Renderer) -> bool {
        self.0.update_after(renderer)
    }
}

impl std::fmt::Debug for dyn CustomNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.type_name())
    }
}

/// `context( node, { … } )`'s value: the `builder.context` keys a
/// [`Node::Context`] installs for its subgraph, merged over the ones already
/// in force (`builder.addContext()`).
///
/// Three's values are arbitrary JS; the port's are nodes (`getViewZ: () =>
/// scenePassViewZ` is `.set( "getViewZ", scene_pass_view_z )`), held in
/// `BuildContext`'s string-keyed `extra` map, which is where #155 decision 6
/// put addon keys. The typed core keys (`setupNormal`, the material side, …)
/// are installed by the material's own setup and cannot be set from here.
#[derive(Clone, Debug, Default)]
pub struct ContextValue {
    pub(crate) entries: Vec<(&'static str, NodeRef)>,
    /// `{ uniformFlow: … }`, which is not a node: see
    /// [`uniform_flow`](Self::uniform_flow).
    pub(crate) uniform_flow: Option<bool>,
    /// `{ nodeName: … }`, which is not a node: see
    /// [`node_name`](Self::node_name).
    pub(crate) node_name: Option<&'static str>,
}

impl ContextValue {
    /// An empty set of context entries.
    pub fn new() -> Self {
        Self::default()
    }

    /// `{ …, key: value }`. A later `set` of the same key wins, as the later
    /// of two properties does in a JS object literal.
    pub fn set(mut self, key: &'static str, value: impl Into<NodeRef>) -> Self {
        self.entries.retain(|(k, _)| *k != key);
        self.entries.push((key, value.into()));
        self
    }

    /// `{ …, uniformFlow: on }` — what [`uniform_flow`] installs. Under it a
    /// `select()` with both branches is WGSL's `select( f, t, cond )` rather
    /// than an `if`/`else` over a var (`ConditionalNode.generate()`), so it
    /// stays in uniform control flow.
    ///
    /// [`uniform_flow`]: crate::nodes::tsl::uniform_flow
    pub fn uniform_flow(mut self, on: bool) -> Self {
        self.uniform_flow = Some(on);
        self
    }

    /// `{ …, nodeName: name }` — what [`set_name`] installs. The first
    /// `uniform()` built inside clears it, and takes the name unless it has
    /// one of its own.
    ///
    /// [`set_name`]: crate::nodes::tsl::set_name
    pub fn node_name(mut self, name: &'static str) -> Self {
        self.node_name = Some(name);
        self
    }
}

/// What [`debug`](crate::nodes::tsl::debug)'s callback is handed: three's
/// `callback( builder, snippet )`, with the two things of the builder a
/// debugging callback reads.
#[derive(Clone, Copy, Debug)]
pub struct DebugInfo<'a> {
    /// `builder.shaderStage`: `"vertex"`, `"fragment"` or `"compute"`.
    pub stage: &'static str,
    /// `builder.flow.code`: the statements emitted so far in the current
    /// scope (the stage's `main`, or the `Fn()` being built), one per line,
    /// with one level of indentation removed.
    pub flow: &'a str,
    /// The debugged node's snippet, which is also the debug node's own.
    pub snippet: &'a str,
}

/// A [`debug`](crate::nodes::tsl::debug) callback.
#[derive(Clone)]
pub struct DebugCallback(pub(crate) Rc<dyn Fn(&DebugInfo<'_>)>);

impl DebugCallback {
    /// Wrap `callback`.
    pub fn new(callback: impl Fn(&DebugInfo<'_>) + 'static) -> Self {
        Self(Rc::new(callback))
    }
}

impl std::fmt::Debug for DebugCallback {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("DebugCallback")
    }
}

/// The node set. Closed on purpose: an exhaustive `match` in the builder is
/// what tells the next rung it has added something the generator cannot emit.
/// [`Node::Custom`] is the one opening, and it can only compose the others.
#[derive(Debug)]
#[non_exhaustive]
pub enum Node {
    /// A literal. `values` holds one entry per component.
    Const {
        /// The literal's WGSL type.
        ty: Type,
        /// One entry per component.
        values: Vec<f64>,
    },
    /// `array< f32, N >( … )` — `QuadMesh`'s `vertexNode`.
    ConstArray {
        /// The array's element type.
        element_ty: Type,
        /// The array's contents, one entry per component of each element.
        values: Vec<f64>,
    },
    /// The same literal array held in a `var<private> nodeVarN : array< T, N >`
    /// rather than written out at each use — three.js' `TempNode` promotion of
    /// an `array()` that the flow reads more than once, which `BloomNode`'s
    /// composite does five times (`dump/m11`).
    ArrayVar {
        /// The array's element type.
        element_ty: Type,
        /// The array's contents, one entry per component of each element.
        values: Vec<f64>,
    },
    /// `UniformNode`.
    Uniform(Rc<UniformNode>),
    /// `BufferNode` element access: `NodeBuffer_N.value[ index ]`.
    BufferElement {
        /// The buffer being indexed.
        buffer: Rc<BufferNode>,
        /// The element index.
        index: NodeRef,
    },
    /// A geometry attribute.
    Attribute {
        /// The attribute's name in WGSL.
        name: &'static str,
        /// The attribute's WGSL type.
        ty: Type,
    },
    /// `instancedBufferAttribute( buffer, type, stride, offset )`: a vertex
    /// attribute whose buffer steps once per instance. `offset` is in floats
    /// from the start of the instance; the stride is the buffer's `item_size`,
    /// which is all three.js' own call sites use.
    InstancedAttribute {
        /// The per-instance buffer.
        buffer: Rc<InstanceBuffer>,
        /// The offset into one instance's data, in floats.
        offset: usize,
        /// The attribute's WGSL type.
        ty: Type,
    },
    /// A WGSL builtin input.
    Builtin(Builtin),
    /// `ComputeNode` used as a value — `Fn( () => { …; return x } )().compute(
    /// count )` set as a material's `positionNode`. Outside the compute stage
    /// it generates `output` (`properties.outputComputeNode`); the kernel
    /// itself runs from `updateBefore()` (`NodeUpdateType.FRAME`), which the
    /// builder records in
    /// [`NodeProgram::update_before`](crate::nodes::NodeProgram) for the
    /// renderer to dispatch. See `docs/nodes.md` §44 and §57.
    Compute {
        /// The compute kernel's statements and dispatch size.
        flow: Rc<crate::nodes::ComputeFlow>,
        /// The value read outside the compute stage.
        output: NodeRef,
    },
    /// `VarNode` — a cached `var<private>`.
    Var(Rc<VarDef>),
    /// `VarNode` with `intent` set, as it ends up when something assigns to
    /// it — see [`to_var_intent`](crate::nodes::tsl::to_var_intent). A WGSL
    /// function-scope `var`, declared with its value where it is first
    /// built (`var nodeVar0 : vec3<f32> = …;`) instead of hoisted into the
    /// `// vars` block.
    VarIntent(Rc<VarDef>),
    /// `VarNode` with `readOnly` set — `node.toConst()`. A WGSL `let`, so it is
    /// declared where it is assigned and, unlike a `var<private>`, cannot be
    /// written again.
    Let(Rc<VarDef>),
    /// `VaryingNode`.
    Varying(Rc<VaryingDef>),
    /// A `var<private>` with a fixed name that the setup code assigns
    /// explicitly — `PropertyNode` (`DiffuseColor`, `Output`, …).
    Property {
        /// The property's name in WGSL.
        name: &'static str,
        /// The property's WGSL type.
        ty: Type,
    },
    /// A parameter of an emitted `fn` — a name that is already in scope.
    Param {
        /// The parameter's name in WGSL.
        name: &'static str,
        /// The parameter's WGSL type.
        ty: Type,
    },
    /// A statement: `target = value`.
    Assign {
        /// The assignment's left-hand side.
        target: NodeRef,
        /// The assignment's right-hand side.
        value: NodeRef,
    },
    /// `OperatorNode`.
    Op {
        /// The WGSL operator, e.g. `"+"`.
        op: &'static str,
        /// The left operand.
        a: NodeRef,
        /// The right operand.
        b: NodeRef,
        /// The result's WGSL type.
        ty: Type,
    },
    /// `MathNode` — a WGSL builtin call.
    Math {
        /// The WGSL builtin's name, e.g. `"sin"`.
        name: &'static str,
        /// The call's arguments.
        args: Vec<NodeRef>,
        /// The result's WGSL type.
        ty: Type,
    },
    /// `SplitNode`.
    Swizzle {
        /// The node being swizzled.
        node: NodeRef,
        /// The swizzle mask, e.g. `"xyz"`.
        components: &'static str,
        /// The result's WGSL type.
        ty: Type,
    },
    /// `ConvertNode` / a single-argument constructor: `vec4<f32>( x )`.
    Cast {
        /// The node being converted.
        node: NodeRef,
        /// The target WGSL type.
        ty: Type,
    },
    /// `OperatorNode` with one operand: `( - x )`.
    Neg {
        /// The negated node.
        node: NodeRef,
        /// The result's WGSL type.
        ty: Type,
    },
    /// `JoinNode` — `vec4<f32>( a, b, c, d )`.
    Join {
        /// The joined components.
        args: Vec<NodeRef>,
        /// The result's WGSL type.
        ty: Type,
    },
    /// `ArrayElementNode`: `m[ 3u ]`, `array< f32, 3 >( … )[ vertexIndex ]`.
    Element {
        /// The array or vector being indexed.
        node: NodeRef,
        /// The element index.
        index: NodeRef,
        /// The element's WGSL type.
        ty: Type,
    },
    /// `TextureNode` — a texture sample.
    Texture {
        /// The texture being sampled.
        texture: Rc<TextureSource>,
        /// The sample coordinate.
        uv: NodeRef,
        /// How the texture is read.
        mode: SampleMode,
        /// The result's WGSL type.
        ty: Type,
    },
    /// `TextureSizeNode` — `textureDimensions( t, level )`, a `vec2<u32>`.
    TextureSize {
        /// The texture being measured.
        texture: Rc<TextureSource>,
        /// The mip level.
        level: NodeRef,
    },
    /// `varyingProperty( type, name )` — a *named* varying assigned to by
    /// statement rather than built from a value. Unlike [`Node::Varying`] it
    /// is declared as soon as either stage mentions it, so a varying the
    /// fragment stage never reads still appears in `VaryingsStruct`, exactly
    /// as `vBatchIndirectId` does in Three's dump.
    VaryingProperty {
        /// The varying's name in WGSL.
        name: &'static str,
        /// The varying's WGSL type.
        ty: Type,
        /// Whether the varying needs `@interpolate(flat, either)`.
        flat: bool,
    },
    /// A call into a [`Node::Code`] / inlined `Fn()`.
    Call {
        /// The function being called.
        def: Rc<FnDef>,
        /// The call's arguments.
        args: Vec<NodeRef>,
    },
    /// `CodeNode` itself — a `wgslFn()` as a node rather than as a call, which
    /// is the shape another `wgslFn`'s `includes` list needs it in. Building
    /// one emits its declaration into `// codes` and nothing else; it has no
    /// value, so nothing but an `includes` list may hold one.
    Code(Rc<crate::nodes::code::CodeDef>),
    /// `FunctionCallNode` over a `wgslFn()` — a call into hand-written WGSL
    /// the node system copies through verbatim. See [`crate::nodes::code`].
    CodeCall {
        /// The hand-written WGSL function being called.
        def: Rc<crate::nodes::code::CodeDef>,
        /// The call's arguments.
        args: Vec<NodeRef>,
    },
    /// A sequence of statements followed by the value they produce — the shape
    /// an inlined `Fn()` body with `toVar()` statements has. Three has no node
    /// for it: its `ShaderNode` call simply flows its body's statements into the
    /// current stage and returns the last expression, which is what this does.
    Block {
        /// The statements built ahead of `result`.
        statements: Vec<NodeRef>,
        /// The block's value.
        result: NodeRef,
    },
    /// `Loop( count, ( { i } ) => { … } )` — `for ( var i : i32 = 0; i < n; i ++ )`.
    Loop {
        /// `Loop( { start, end }, … )`'s `start`. `None` is three.js'
        /// `Loop( count, … )` shorthand, whose start is the literal `0`; a
        /// node start is written out as it stands, so
        /// `webgpu_postprocessing_anamorphic`'s negated half-sample count
        /// reaches the loop header as `i32( ( - nodeVar1 ) )`.
        start: Option<NodeRef>,
        /// `Loop( { start, end }, … )`'s `end`, or `Loop( count, … )`'s
        /// count — the loop's exclusive upper bound.
        count: NodeRef,
        /// The loop index, as it appears inside `body` (`Node::Param`). Its
        /// type is `Loop( { type } )`: `i32`, or `f32` for `hashBlur`'s
        /// `type: 'float'`, which also changes the step to `i += 1.`.
        index: NodeRef,
        /// `Loop( { condition } )` — `"<"` unless the caller asked for
        /// another comparison (`boxBlur`'s `"<="`).
        condition: &'static str,
        /// `Loop( { update } )` — `i += update` in place of the default step.
        update: Option<NodeRef>,
        /// The loop body's statements.
        body: Vec<NodeRef>,
    },
    /// `Break()` — a bare `break;` out of the innermost `Loop`.
    Break,
    /// `Continue()` — a bare `continue;` to the next iteration of the
    /// innermost `Loop`.
    Continue,
    /// `textureStore( storageTexture, coord, value )` — a statement.
    ///
    /// `StorageTextureNode.generateStore()` writes the coordinate as
    /// `vec2<u32>( … )` (`vec3<u32>` for a 3D texture) around whatever the
    /// caller passed, so a `uvec2` coordinate is wrapped once more, exactly as
    /// three's dump shows.
    TextureStore {
        /// The storage texture being written.
        texture: Rc<TextureSource>,
        /// The texel coordinate.
        coord: NodeRef,
        /// The value stored.
        value: NodeRef,
    },
    /// `If( cond, () => { … } )` as a bare statement (`setupDiscard`),
    /// optionally with the `.Else( … )` / `.ElseIf( … )` arm `StackNode` adds.
    ///
    /// `ElseIf` is not a shape of its own: `StackNode.ElseIf()` puts a whole
    /// nested `If` inside the else block, so `If( a ).ElseIf( b )` is
    /// `else_body: vec![ if_then( b, … ) ]` and generates as a nested
    /// `if`/`else`, which is exactly what three emits.
    If {
        /// The branch condition.
        cond: NodeRef,
        /// The `if` block's statements.
        body: Vec<NodeRef>,
        /// Empty for a one-armed `If`, in which case no `else` is emitted and
        /// the generated text is byte-identical to what it was before the arm
        /// existed.
        else_body: Vec<NodeRef>,
    },
    /// `If( cond, () => { … } )` — a one-armed conditional over a result var
    /// that was initialised before it. `pre` holds the statements three.js
    /// emits ahead of the result var (its `toConst` lines), `result` is the var
    /// itself and `body` the statements inside the block, the last of which
    /// assigns `result`. The node's value is the result var.
    IfVar {
        /// The statements built ahead of `result`.
        pre: Vec<NodeRef>,
        /// The initialised result variable.
        result: NodeRef,
        /// The branch condition.
        cond: NodeRef,
        /// The block's statements, the last of which assigns `result`.
        body: Vec<NodeRef>,
    },
    /// `Discard()` — a bare `discard;`.
    Discard,
    /// `return value;` inside an emitted `fn` — what an `If( cond, () => {
    /// return x; } )` in a `Fn()` body compiles to (`neutralToneMapping`'s
    /// early out).
    Return {
        /// The returned value.
        value: NodeRef,
    },
    /// `x.not()` — `( ! x )`. A `bool`, or a `bvecN` for an `N`-vector
    /// operand (`OperatorNode.getNodeType()`'s `'!'` arm).
    Not {
        /// The negated node.
        node: NodeRef,
    },
    /// `x.bitNot()` — `( ~ x )`, typed `getIntegerType( typeA )`.
    BitNot {
        /// The negated node.
        node: NodeRef,
        /// The result's WGSL type.
        ty: Type,
    },
    /// `cond.select( a, b )` — lowered to an `if`/`else` writing a result var,
    /// exactly as Three does.
    Select {
        /// The branch condition.
        cond: NodeRef,
        /// The value when `cond` is true.
        a: NodeRef,
        /// The value when `cond` is false.
        b: NodeRef,
        /// The result's WGSL type.
        ty: Type,
    },
    /// `storageStruct.get( 'member' )` — `MemberNode` on a
    /// [`BufferSource::Struct`] storage buffer: `NodeBuffer_N.member`.
    StructMember {
        /// The struct buffer.
        buffer: Rc<BufferNode>,
        /// The member's index in the struct's layout.
        member: usize,
    },
    /// `AtomicFunctionNode` — `atomicStore( &pointer, value )` and the rest of
    /// the family. `pointer` is a struct member or buffer element declared
    /// `atomic< T >`. As a bare statement it is one `atomicX( … );` line; read
    /// as a value as well, its result is held in a `let` first.
    Atomic {
        /// The WGSL atomic function's name, e.g. `"atomicAdd"`.
        method: &'static str,
        /// The atomic value being operated on.
        pointer: NodeRef,
        /// The operand, when the method takes one.
        value: Option<NodeRef>,
    },
    /// `WorkgroupInfoNode` — the array itself. Read through
    /// [`Node::Element`], which is how `.element( i )` reaches it.
    Workgroup(Rc<WorkgroupArrayDef>),
    /// `BarrierNode` — `workgroupBarrier()` / `storageBarrier()` /
    /// `textureBarrier()`, `scope` being the prefix.
    Barrier {
        /// The barrier's WGSL prefix, e.g. `"workgroup"`.
        scope: &'static str,
    },
    /// `SubgroupFunctionNode` — `subgroupAdd( e )`, `subgroupShuffle( e, id )`,
    /// `quadSwapX( e )` and the rest of the family
    /// (`src/nodes/gpgpu/SubgroupFunctionNode.js`). Reaching one enables
    /// `subgroups` in the stage that builds it.
    Subgroup {
        /// The WGSL builtin's name, e.g. `"subgroupAdd"` — three's
        /// `SubgroupFunctionNode.SUBGROUP_ADD` and friends.
        method: &'static str,
        /// `aNode`; `None` only for `subgroupElect()`.
        a: Option<NodeRef>,
        /// `bNode`: the lane id, shuffle mask or delta of the two-input forms.
        b: Option<NodeRef>,
    },
    /// A node type defined outside the crate; see [`CustomNode`]. Built by
    /// building what its `setup` returns.
    Custom(Rc<dyn CustomNode>),
    /// `ContextNode` — `node.context( { … } )`. Builds `node` with `value`'s
    /// keys merged into `builder.context`, and restores the previous context
    /// afterwards.
    Context {
        /// The node built under the merged context.
        node: NodeRef,
        /// The context entries merged in.
        value: Rc<ContextValue>,
    },
    /// `IsolateNode` — `isolate( node )`. Builds `node` in a `NodeCache` of
    /// its own, whose parent is the cache in force where the isolate is
    /// built: what `node`'s subgraph sets up, counts or declares for the first
    /// time stays inside it.
    Isolate {
        /// The node built in its own cache.
        node: NodeRef,
    },
    /// `ExpressionNode` — `expression( snippet, type )`: raw WGSL. A value
    /// type prints `snippet` where the node is read; `void` writes it to the
    /// flow as a statement. Not cacheable: each read prints it again.
    Expression {
        /// The WGSL, verbatim.
        snippet: Rc<str>,
        /// Its type, `Void` for a statement.
        ty: Type,
    },
    /// `DebugNode` — `debug( node, callback )`: `node`'s snippet, passed
    /// through after `callback` (or a log of the flow so far) has seen it.
    Debug {
        /// The node debugged.
        node: NodeRef,
        /// `null` logs the stage's flow code to stderr, as three's `log()`
        /// does to the console.
        callback: Option<DebugCallback>,
    },
    /// `structType( values )` — `StructNode`: a value of a [`struct_type`]
    /// (`StructTypeNode`) built from one value per member, in member order.
    /// Always held in a var of the struct's type, which is where three's
    /// `StructNode` lands too (`nodeVar31 = StructType0( … )` in the TRAA
    /// dump); its [`ty`](NodeRef::ty) is [`Type::Void`] because the port's
    /// `Type` has no struct case, and only [`Node::StructGet`] reads it.
    ///
    /// [`struct_type`]: crate::nodes::tsl::struct_type
    StructNew {
        /// The struct's layout.
        layout: Rc<StructLayout>,
        /// One value per member.
        values: Vec<NodeRef>,
    },
    /// `outputStruct( ...members )` — `OutputStructNode`, a fragment stage's
    /// whole result as one `@location( i )` member per value, each of its own
    /// type. It is only ever a material's `outputNode`
    /// (`DepthOfFieldNode`'s CoC pass writes its near and far fields this
    /// way); the builder turns it into the `OutputType` struct and its
    /// `output.mN = …` lines, as it does an [`MrtNode`]'s members, and it
    /// has no value of its own.
    ///
    /// [`MrtNode`]: crate::nodes::MrtNode
    OutputStruct {
        /// The members, by location.
        members: Vec<NodeRef>,
    },
    /// `structNode.get( name )` — `MemberNode` over a [`Node::StructNew`]
    /// (or over a block or `Fn()` whose result is one): `{ var }.{ member }`.
    StructGet {
        /// The struct value read.
        value: NodeRef,
        /// Its layout.
        layout: Rc<StructLayout>,
        /// The member's index in the layout.
        member: usize,
    },
}

/// A handle on a node. Fluent TSL methods hang off this; see `tsl.rs`.
#[derive(Clone, Debug)]
pub struct NodeRef(Rc<Node>);

impl NodeRef {
    /// Wraps a node in a new handle.
    pub fn new(node: Node) -> Self {
        NodeRef(Rc::new(node))
    }

    /// The `Rc<Node>` this handle wraps.
    pub fn as_rc(&self) -> &Rc<Node> {
        &self.0
    }

    /// The `Node` this handle wraps.
    pub fn node(&self) -> &Node {
        &self.0
    }

    /// The identity the builder keys its per-node state on.
    pub fn key(&self) -> usize {
        Rc::as_ptr(&self.0) as *const u8 as usize
    }

    /// `Node.getNodeType( builder )`.
    pub fn ty(&self) -> Type {
        match &*self.0 {
            Node::Const { ty, .. } => *ty,
            Node::ConstArray { element_ty, .. } => *element_ty,
            Node::ArrayVar { element_ty, .. } => *element_ty,
            Node::Uniform(u) => u.ty,
            Node::BufferElement { buffer, .. } => buffer.element_ty,
            Node::Attribute { ty, .. } => *ty,
            Node::InstancedAttribute { ty, .. } => *ty,
            Node::Builtin(b) => b.ty(),
            Node::Var(v) => v.ty,
            Node::VarIntent(v) => v.ty,
            Node::Let(v) => v.ty,
            Node::Varying(v) => v.ty,
            Node::TextureSize { .. } => Type::UVec2,
            Node::VaryingProperty { ty, .. } => *ty,
            Node::Property { ty, .. } => *ty,
            Node::Param { ty, .. } => *ty,
            Node::Assign { target, .. } => target.ty(),
            Node::Op { ty, .. } => *ty,
            Node::Math { ty, .. } => *ty,
            Node::Swizzle { ty, .. } => *ty,
            Node::Cast { ty, .. } => *ty,
            Node::Neg { ty, .. } => *ty,
            Node::Join { ty, .. } => *ty,
            Node::Element { ty, .. } => *ty,
            Node::Texture { ty, .. } => *ty,
            Node::Call { def, .. } => def.ret,
            Node::Code(_) => Type::Void,
            Node::CodeCall { def, .. } => def.ret,
            Node::IfVar { result, .. } => result.ty(),
            Node::Select { ty, .. } => *ty,
            Node::Block { result, .. } => result.ty(),
            Node::Loop { .. }
            | Node::If { .. }
            | Node::Discard
            | Node::Break
            | Node::Continue
            | Node::TextureStore { .. }
            | Node::Return { .. } => Type::Void,
            Node::Not { node } => Type::vector_of(Type::Bool, node.ty().components().max(1)),
            Node::BitNot { ty, .. } => *ty,
            Node::StructMember { buffer, member } => match &buffer.source {
                BufferSource::Struct { layout, .. } => layout.members[*member].ty,
                _ => unreachable!("three-rs: a struct member is only built on a struct buffer"),
            },
            // `AtomicFunctionNode.getNodeType()` is the pointer's type.
            Node::Atomic { pointer, .. } => pointer.ty(),
            Node::Workgroup(def) => def.element_ty,
            Node::Barrier { .. } => Type::Void,
            Node::Subgroup { method, a, b } => subgroup_type(method, a.as_ref(), b.as_ref()),
            Node::Compute { output, .. } => output.ty(),
            Node::Custom(custom) => custom.node_type(),
            Node::Context { node, .. } | Node::Isolate { node } | Node::Debug { node, .. } => {
                node.ty()
            }
            Node::Expression { ty, .. } => *ty,
            Node::StructNew { .. } | Node::OutputStruct { .. } => Type::Void,
            Node::StructGet { layout, member, .. } => layout.members[*member].ty,
        }
    }
}

impl From<&NodeRef> for NodeRef {
    fn from(n: &NodeRef) -> Self {
        n.clone()
    }
}

/// `float( x )` from a Rust number — the `From` impls are what let TSL methods
/// take `impl Into<NodeRef>` and read like the JS.
macro_rules! const_from {
    ($t:ty, $ty:expr) => {
        impl From<$t> for NodeRef {
            fn from(v: $t) -> Self {
                NodeRef::new(Node::Const {
                    ty: $ty,
                    values: vec![v as f64],
                })
            }
        }
    };
}

const_from!(f64, Type::F32);
const_from!(f32, Type::F32);
const_from!(i32, Type::I32);
const_from!(u32, Type::U32);

impl From<bool> for NodeRef {
    fn from(v: bool) -> Self {
        NodeRef::new(Node::Const {
            ty: Type::Bool,
            values: vec![if v { 1.0 } else { 0.0 }],
        })
    }
}

impl From<Color> for NodeRef {
    fn from(c: Color) -> Self {
        NodeRef::new(Node::Const {
            ty: Type::Vec3,
            values: vec![c.r, c.g, c.b],
        })
    }
}

impl From<Vector2> for NodeRef {
    fn from(v: Vector2) -> Self {
        NodeRef::new(Node::Const {
            ty: Type::Vec2,
            values: vec![v.x, v.y],
        })
    }
}

impl From<Vector3> for NodeRef {
    fn from(v: Vector3) -> Self {
        NodeRef::new(Node::Const {
            ty: Type::Vec3,
            values: vec![v.x, v.y, v.z],
        })
    }
}

impl From<Matrix4> for NodeRef {
    fn from(m: Matrix4) -> Self {
        NodeRef::new(Node::Const {
            ty: Type::Mat4,
            values: m.elements.to_vec(),
        })
    }
}

/// A lazily-initialised accessor singleton. Three's `positionLocal` etc. are
/// module-level node objects, and their object identity is what makes the
/// builder emit one `var` shared by every reference; `thread_local!` + `Rc`
/// clone reproduces that.
pub(crate) struct Lazy<T: 'static> {
    cell: RefCell<Option<T>>,
}

impl<T: Clone + 'static> Default for Lazy<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Clone + 'static> Lazy<T> {
    pub const fn new() -> Self {
        Lazy {
            cell: RefCell::new(None),
        }
    }

    pub fn get(&self, init: impl FnOnce() -> T) -> T {
        let mut slot = self.cell.borrow_mut();
        if slot.is_none() {
            *slot = Some(init());
        }
        slot.as_ref()
            .expect("three-rs: the lazy slot was filled in just above")
            .clone()
    }
}

// The program cache key hashes the binding descriptions, which bottom out in
// these three types. Their `Hash` impls are written by hand rather than derived
// so that they reach every field the *generated program* depends on and no
// field that is only a value: a texture's pixels, an instanced attribute's
// array. Three.js draws the same line in `Node.getCacheKey()`, where a texture
// contributes its uuid and never its image.

impl std::hash::Hash for UniformSource {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        match self {
            UniformSource::LightColorIntensity(i)
            | UniformSource::LightCutoffDistance(i)
            | UniformSource::LightDecay(i)
            | UniformSource::LightViewPosition(i)
            | UniformSource::LightWorldPosition(i)
            | UniformSource::LightTargetPosition(i)
            | UniformSource::LightGroundColor(i)
            | UniformSource::LightConeCos(i)
            | UniformSource::LightPenumbraCos(i)
            | UniformSource::ShadowMatrix(i)
            | UniformSource::ShadowCameraNear(i)
            | UniformSource::ShadowCameraFar(i)
            | UniformSource::ShadowBias(i)
            | UniformSource::ShadowNormalBias(i)
            | UniformSource::ShadowRadius(i)
            | UniformSource::ShadowBlurSamples(i)
            | UniformSource::ShadowMapSize(i)
            | UniformSource::ShadowIntensity(i) => i.hash(state),
            // A baked `uniform( value )`: two materials can generate identical
            // WGSL and differ only here (a texture's uv matrix, say), so the
            // bits are part of the key.
            UniformSource::Value(values) => {
                values.len().hash(state);
                for value in values {
                    value.to_bits().hash(state);
                }
            }
            // Identity, never contents: the whole point is that the value
            // moves between draws while the program stays one program.
            UniformSource::Settable(cell) => cell.hash(state),
            UniformSource::Live(value) => value.hash(state),
            _ => {}
        }
    }
}

impl std::hash::Hash for BufferSource {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        match self {
            BufferSource::Range { min, max } => {
                for value in min.iter().chain(max.iter()) {
                    value.to_bits().hash(state);
                }
            }
            // Identity, never contents — the array behind an instanced
            // attribute is megabytes and is resolved per draw anyway.
            BufferSource::Attribute(data) | BufferSource::UniformArray(data) => {
                (Rc::as_ptr(data) as *const u8 as usize).hash(state)
            }
            // The layout is spelled into the WGSL, which the key already
            // hashes; the initial contents are a value and stay out.
            BufferSource::Struct { layout, .. } => {
                layout.name.hash(state);
                for member in &layout.members {
                    member.name.hash(state);
                    member.ty.hash(state);
                    member.atomic.hash(state);
                }
            }
            // The array is a value; `.toReadOnly()` is spelt into the WGSL.
            BufferSource::StorageData { read_only, .. } => read_only.hash(state),
            BufferSource::SkeletonBoneMatrices(skeleton) => {
                (Rc::as_ptr(&skeleton.0) as *const u8 as usize).hash(state)
            }
            BufferSource::LightProbe(index) => index.hash(state),
            BufferSource::Live(value) => value.hash(state),
            BufferSource::InstanceMatrix
            | BufferSource::InstanceColor
            | BufferSource::MorphInfluences
            | BufferSource::BoneMatrices
            | BufferSource::PreviousBoneMatrices
            | BufferSource::CameraViewMatrices
            | BufferSource::CameraProjectionMatrices
            | BufferSource::ClippingIntersection
            | BufferSource::ClippingUnion
            | BufferSource::Storage
            | BufferSource::AtomicStorage => {}
        }
    }
}

impl std::hash::Hash for TextureSource {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        match self {
            // Identity only, as `Node.getCacheKey()` takes a texture's uuid.
            // The format, colour space, filters, wrapping and anisotropy are
            // *not* here: the compiled program's layout reads only the
            // binding's `kind` and `visibility` (`programs::layout_entry`),
            // and the view and sampler built from those fields are resolved
            // per draw. Neither the image data nor the `gpu` handle either —
            // the key must not move when the texture is uploaded.
            TextureSource::Texture2D(texture) => texture.id().hash(state),
            TextureSource::Depth(texture) | TextureSource::ShadowMap(texture) => {
                texture.id().hash(state)
            }
            TextureSource::Cube(texture) => texture.id().hash(state),
            TextureSource::DataArray(texture) => texture.id().hash(state),
            TextureSource::Data(texture) => texture.id().hash(state),
            TextureSource::CubeDepth(texture) => texture.id().hash(state),
            TextureSource::Texture3D(texture) => texture.id().hash(state),
            // The access is part of the declaration, so it is part of the key.
            TextureSource::Storage(texture, access) => {
                texture.id().hash(state);
                access.hash(state);
            }
            TextureSource::Storage3D(texture, access) => {
                texture.id().hash(state);
                access.hash(state);
            }
        }
    }
}

/// Derived but for `Attribute`, whose `Rc<Vec<f32>>` is the caller's whole
/// per-instance array — `BatchedText` hands it four floats per glyph.
impl std::fmt::Debug for BufferSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BufferSource::InstanceMatrix => f.write_str("InstanceMatrix"),
            BufferSource::InstanceColor => f.write_str("InstanceColor"),
            BufferSource::Storage => f.write_str("Storage"),
            BufferSource::AtomicStorage => f.write_str("AtomicStorage"),
            BufferSource::Struct { layout, init } => f
                .debug_struct("Struct")
                .field("layout", &layout.name)
                .field("init", &format_args!("{} words", init.len()))
                .finish(),
            BufferSource::Range { min, max } => f
                .debug_struct("Range")
                .field("min", min)
                .field("max", max)
                .finish(),
            BufferSource::MorphInfluences => f.write_str("MorphInfluences"),
            BufferSource::BoneMatrices => f.write_str("BoneMatrices"),
            BufferSource::PreviousBoneMatrices => f.write_str("PreviousBoneMatrices"),
            BufferSource::CameraViewMatrices => f.write_str("CameraViewMatrices"),
            BufferSource::CameraProjectionMatrices => f.write_str("CameraProjectionMatrices"),
            BufferSource::ClippingIntersection => f.write_str("ClippingIntersection"),
            BufferSource::ClippingUnion => f.write_str("ClippingUnion"),
            BufferSource::Attribute(data) => f
                .debug_tuple("Attribute")
                .field(&format_args!("{} floats", data.len()))
                .finish(),
            BufferSource::UniformArray(data) => f
                .debug_tuple("UniformArray")
                .field(&format_args!("{} floats", data.len()))
                .finish(),
            BufferSource::StorageData { init, read_only } => f
                .debug_struct("StorageData")
                .field("init", &format_args!("{} words", init.len()))
                .field("read_only", read_only)
                .finish(),
            BufferSource::SkeletonBoneMatrices(skeleton) => f
                .debug_tuple("SkeletonBoneMatrices")
                .field(&format_args!("{} bones", skeleton.0.borrow().bones.len()))
                .finish(),
            BufferSource::LightProbe(index) => f.debug_tuple("LightProbe").field(index).finish(),
            BufferSource::Live(value) => f.debug_tuple("Live").field(value).finish(),
        }
    }
}