threers 0.0.7

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
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
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
//! Scene traversal and draw encoding.

use std::collections::HashMap;
use std::ffi::c_void;
use std::sync::Arc;

use super::device::{depth_stencil_state, MetalDevice, MetalError};
use super::enums::*;
use super::objc::{
    class, msg0, msg1, msg2, msg3, msg4, msg6, sel, AutoreleasePool, Id, Owned, NIL,
};
use super::resources::{
    new_sampler, render_pipeline, upload_texture_as, white_texture, PipelineKey, SamplerKey,
};
use crate::cameras::Camera;
use crate::core::{BufferGeometry, Layers, ObjectKind};
use crate::lights::Light;
use crate::materials::{Material, MaterialKind};
use crate::math::{Matrix4, Vector3};
use crate::renderer::viewport::{ClearMode, PixelRect};
use crate::scene::Scene;
use crate::textures::Texture;

/// Views one frame can carry, matching `MAX_VIEWS` in `shaders.metal`.
/// A plain camera uses one, a headset two (the eyes), a CAD quad view four
/// (one per viewport — see [`crate::helpers::QuadView::metal_views`]).
pub const MAX_VIEWS: usize = 4;

/// Light counts, matching the array sizes in `shaders.metal`.
use crate::lights::shadow::{Caster, MAX_SHADOW_RECORDS};

const MAX_DIR_LIGHTS: usize = 4;
/// Each face of a point light's shadow cube, texels — as the wgpu path.
const POINT_SHADOW_SIZE: u32 = 512;
const MAX_POINT_LIGHTS: usize = 8;
const MAX_SPOT_LIGHTS: usize = 4;
const MAX_HEMI_LIGHTS: usize = 2;

/// Buffer indices, matching the `[[buffer(n)]]` attributes in `shaders.metal`.
const VB_VERTICES: NSUInteger = 0;
const VB_FRAME: NSUInteger = 1;
const VB_DRAW: NSUInteger = 2;
const VB_INSTANCES: NSUInteger = 3;
const VB_SKIN: NSUInteger = 4;
const VB_BONES: NSUInteger = 5;
/// The morph-target buffer and its per-draw `MorphInfo` uniform.
const VB_MORPH_DATA: NSUInteger = 6;
const VB_MORPH: NSUInteger = 7;
/// The geometry's per-vertex extras: three `float4`s a vertex — `(uv1,
/// uv2)`, `(uv3, 0, 0)` and its `tangent` (xyz, handedness in w) — present
/// when it has any of them (`Draw::phys_flags.z`: 1 uv sets, 2 tangents).
const VB_UV_SETS: NSUInteger = 8;
/// Shell fur's sixteen `float4` parameter slots (`vs_fur`, and `fs_fur` at
/// `FB_FUR`).
const VB_FUR: NSUInteger = 9;
const FB_FUR: NSUInteger = 2;
const FB_FRAME: NSUInteger = 0;
const FB_DRAW: NSUInteger = 1;

/// How many frames of transient (per-frame) GPU buffers to keep alive.
///
/// `render` does not block on the GPU, so a buffer referenced by an in-flight
/// command buffer must outlive the call that made it. Three frames covers
/// triple buffering, which is the deepest a `CAMetalLayer` goes.
const FRAMES_IN_FLIGHT: usize = 3;

/// Frames a cached geometry or texture survives without being drawn.
///
/// Two seconds at 60 fps: long enough that an object leaving and re-entering
/// the view is not re-uploaded, short enough that a scene rebuilt every frame
/// does not accumulate. See [`MetalRenderer::set_cache_retention`].
pub const DEFAULT_CACHE_RETENTION: u64 = 120;

// --------------------------------------------------------------- GPU structs
//
// Twins of the structs in `shaders.metal`. Members are 16-byte aligned there,
// so they are here; `tests/metal_backend.rs` asserts the sizes.

#[repr(C)]
#[derive(Clone, Copy, Default, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct Vertex {
    pub position: [f32; 3],
    pub normal: [f32; 3],
    pub uv: [f32; 2],
    pub color: [f32; 3],
    /// The colour attribute's alpha (1 without one): three.js multiplies a
    /// 4-component vertex colour into opacity.
    pub alpha: f32,
}

/// Set `THREERS_NO_SLIM_LINES=1` to upload lines as full `Vertex`es again.
///
/// An escape hatch for a change that alters how every line in a scene reaches
/// the GPU: the two paths are meant to be pixel-identical, and this is what
/// lets that be measured rather than asserted.
fn slim_lines_enabled() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var_os("THREERS_NO_SLIM_LINES").is_none())
}

/// The slim vertex a line draw reads: 24 bytes against `Vertex`'s 48.
///
/// Lines are unlit and untextured, so normal, uv and pad are dead weight. At
/// connectome scale that halves the resident geometry — see `LineVertex` in
/// `shaders.metal`, whose layout this must match exactly.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct LineVertex {
    pub position: [f32; 3],
    pub color: [f32; 3],
}

#[repr(C)]
#[derive(Clone, Copy, Default)]
struct DirLightGpu {
    direction: [f32; 4],
    color: [f32; 4],
}

#[repr(C)]
#[derive(Clone, Copy, Default)]
struct PointLightGpu {
    position: [f32; 4],
    color: [f32; 4],
    params: [f32; 4],
}

#[repr(C)]
#[derive(Clone, Copy, Default)]
struct SpotLightGpu {
    position: [f32; 4],
    direction: [f32; 4],
    color: [f32; 4],
    params: [f32; 4],
}

#[repr(C)]
#[derive(Clone, Copy, Default)]
struct HemiLightGpu {
    sky: [f32; 4],
    ground: [f32; 4],
    up: [f32; 4],
}

#[repr(C, align(16))]
#[derive(Clone, Copy)]
pub(crate) struct FrameUniforms {
    view_proj: [[f32; 16]; MAX_VIEWS],
    view: [[f32; 16]; MAX_VIEWS],
    camera_pos: [[f32; 4]; MAX_VIEWS],
    ambient: [f32; 4],
    fog_color: [f32; 4],
    fog_params: [f32; 4],
    viewport: [f32; 4],
    counts: [u32; 4],
    /// `[view_count, 0, 0, 0]`.
    view_info: [u32; 4],
    dir: [DirLightGpu; MAX_DIR_LIGHTS],
    points: [PointLightGpu; MAX_POINT_LIGHTS],
    spots: [SpotLightGpu; MAX_SPOT_LIGHTS],
    hemis: [HemiLightGpu; MAX_HEMI_LIGHTS],
    /// `[rect-area light count, transmission capture mips, reflective
    /// transparency (0/1), the capture slice of view 0]` — w is the view's
    /// own index when a per-view pass has promoted it into slot 0.
    rect_counts: [u32; 4],
    rects: [RectLightGpu; MAX_RECT_LIGHTS],
    /// x: shadow records in use.
    shadow_info: [u32; 4],
    /// One per shadow-casting light, which names it in its own `color[3]`
    /// (record + 1) — `ShadowRecord` in `shaders.metal`.
    shadows: [ShadowRecordGpu; MAX_SHADOW_RECORDS],
}

/// three.js's transmission render target: the opaque scene drawn linear and
/// untone-mapped into `target`, then copied into `mipped` and its mips
/// generated, which the transmissive materials read.
struct MetalCapture {
    target: crate::metal::MetalRenderTarget,
    mipped: Owned,
    size: (u32, u32),
    levels: u32,
    /// One array slice per view, each the scene from that view.
    slices: u32,
}

/// One `RectAreaLight`: its centre, colour × intensity, and half width and
/// half height as vectors (three.js's `halfWidth`, `halfHeight`).
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct RectLightGpu {
    position: [f32; 4],
    color: [f32; 4],
    right: [f32; 4],
    up: [f32; 4],
}

const MAX_RECT_LIGHTS: usize = 4;

impl Default for FrameUniforms {
    fn default() -> Self {
        Self {
            view_proj: [Matrix4::identity().elements; MAX_VIEWS],
            view: [Matrix4::identity().elements; MAX_VIEWS],
            camera_pos: [[0.0; 4]; MAX_VIEWS],
            view_info: [1, 0, 0, 0],
            ambient: [0.0; 4],
            fog_color: [0.0; 4],
            fog_params: [0.0; 4],
            viewport: [1.0, 1.0, 1.0, 1.0],
            counts: [0; 4],
            dir: [DirLightGpu::default(); MAX_DIR_LIGHTS],
            points: [PointLightGpu::default(); MAX_POINT_LIGHTS],
            spots: [SpotLightGpu::default(); MAX_SPOT_LIGHTS],
            hemis: [HemiLightGpu::default(); MAX_HEMI_LIGHTS],
            rect_counts: [0; 4],
            rects: [RectLightGpu::default(); MAX_RECT_LIGHTS],
            shadow_info: [0; 4],
            shadows: [ShadowRecordGpu::default(); MAX_SHADOW_RECORDS],
        }
    }
}

impl FrameUniforms {
    /// Move view `index` into slot 0 and declare a single view.
    ///
    /// The non-layered shaders always read view 0, so a per-view pass hands
    /// them the view it is drawing.
    fn promote_view(&mut self, index: usize) {
        if index != 0 && index < MAX_VIEWS {
            self.view_proj[0] = self.view_proj[index];
            self.view[0] = self.view[index];
            self.camera_pos[0] = self.camera_pos[index];
        }
        self.view_info[0] = 1;
    }
}

/// An environment cube on the GPU: its faces (mipmapped), and its PMREM
/// CubeUV atlas with that atlas's lookup parameters (texel width and height,
/// top mip, 1) where it has one.
struct EnvGpu {
    cube: Owned,
    atlas: Option<Owned>,
    params: [f32; 4],
}

impl EnvGpu {
    fn upload(device: &MetalDevice, cube: &crate::textures::CubeTexture) -> Result<Self, MetalError> {
        let tex = crate::metal::resources::upload_cube(device, cube)?;
        let (atlas, params) = match &cube.cube_uv_atlas {
            Some(a) => (
                Some(crate::metal::resources::upload_cube_uv_atlas(
                    device,
                    a,
                    cube.format == crate::TextureFormat::Rgba8UnormSrgb,
                )?),
                [a.texel_width, a.texel_height, a.lod_max as f32, 1.0],
            ),
            None => (None, [0.0; 4]),
        };
        Ok(Self { cube: tex, atlas, params })
    }
}

/// Twin of `MorphInfo` in `shaders.metal`: the per-draw morph state.
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct MorphUniforms {
    /// Target count (0 = off), stride in float4s, flags (1 normal, 2 colour,
    /// 4 position), vertex count.
    info: [u32; 4],
    /// x: three.js's `morphTargetBaseInfluence`.
    base: [f32; 4],
    weights: [[f32; 4]; 16],
}

/// A geometry's morph targets on the GPU, laid out like three.js's morph
/// texture — see `apply_morph` in `shaders.metal`.
struct MorphBuffer {
    buffer: Owned,
    count: u32,
    stride: u32,
    flags: u32,
    vertex_count: u32,
}

/// What `vs_skinned` reads per vertex, alongside the ordinary `Vertex`.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct SkinVertex {
    joints: [f32; 4],
    weights: [f32; 4],
}

#[repr(C, align(16))]
#[derive(Clone, Copy)]
pub(crate) struct DrawUniforms {
    model: [f32; 16],
    normal_mat: [f32; 16],
    base_color: [f32; 4],
    emissive: [f32; 4],
    specular: [f32; 4],
    pbr: [f32; 4],
    misc: [f32; 4],
    flags: [u32; 4],
    /// Which of the extra texture slots are bound, as 0/1: emissive, normal,
    /// metallic-roughness, ambient occlusion. `flags.y` already carries the
    /// base-colour map and stays where it is so nothing else has to move.
    maps: [u32; 4],
    /// Scalars the maps modulate: normal-map scale, AO strength, then the
    /// environment's intensity and whether one is bound at all.
    map_params: [f32; 4],
    /// `[receives_shadow, is_sprite, 0, 0]`. Per-object rather than
    /// per-material, which is where `Object3D` keeps it.
    object: [u32; 4],
    /// Which variant-only maps are bound: alpha, light, bump, specular.
    extra: [u32; 4],
    /// `Material::map_uniforms()[0]`: `[lightMapIntensity, bumpScale, envMap
    /// bound, 0]`.
    map_scalars: [f32; 4],
    /// `Material::map_uniforms()[1]`: the `envMap`'s combine, reflectivity,
    /// refraction ratio and mapping.
    env_params: [f32; 4],
    /// `Material::map_uniforms()[2..5]`: its lookup rotation, flip folded in.
    env_dir: [[f32; 4]; 3],
    /// Each map's sampler as an index into the bound sampler pool, four to a
    /// vector in [`MAP_COUNT`] order — see `SAMPLER_POOL`.
    map_samplers: [[u32; 4]; 6],
    /// MeshPhysicalMaterial's scalars (zero for anything else):
    /// `[ior, specularIntensity, clearcoat, clearcoatRoughness]`,
    /// `[specularColor, sheen]`, `[sheenColor, sheenRoughness]`,
    /// `[iridescence, iridescenceIOR, iridescenceThicknessRange]`,
    /// `[anisotropy, anisotropyRotation, clearcoatNormalScale]`,
    /// `[transmission, thickness, attenuationDistance (0 = infinite),
    /// dispersion]`, `[attenuationColor, 0]`.
    physical: [[f32; 4]; 7],
    /// `[bitmask of the bound physical maps, specular antialiasing, 0, 0]`.
    phys_flags: [u32; 4],
    /// Each map's uv transform, two rows of three.js's `Texture.matrix`, in
    /// [`MAP_COUNT`] order: base, emissive, normal, metallic-roughness,
    /// ambient occlusion, alpha, light, bump, specular, matcap, then the
    /// physical maps ([`physical_maps`]).
    map_xform: [[f32; 4]; 2 * MAP_COUNT],
}

/// How many material maps a draw can bind, `MAP_*` in `shaders.metal`.
const MAP_COUNT: usize = 22;
/// Where the physical maps start, in that order (`MAP_PHYS0`), and at which
/// texture slot they are bound.
const MAP_PHYS0: usize = 10;
const PHYS_TEXTURE0: usize = 16;
/// Sampler slots 0–9: one per distinct filter-and-wrap among a draw's maps.
const SAMPLER_POOL: usize = 10;

/// MeshPhysicalMaterial's own maps, in `MAP_PHYS0` order, and whether each is
/// data (linear) rather than colour.
fn physical_maps(slots: &crate::materials::MaterialTextureSlots) -> [(Option<&Arc<Texture>>, bool); 12] {
    [
        (slots.specular_intensity_map.as_ref(), true),
        (slots.specular_color_map.as_ref(), false),
        (slots.clearcoat_map.as_ref(), true),
        (slots.clearcoat_roughness_map.as_ref(), true),
        (slots.clearcoat_normal_map.as_ref(), true),
        (slots.iridescence_map.as_ref(), true),
        (slots.iridescence_thickness_map.as_ref(), true),
        (slots.sheen_color_map.as_ref(), false),
        (slots.sheen_roughness_map.as_ref(), true),
        (slots.transmission_map.as_ref(), true),
        (slots.thickness_map.as_ref(), true),
        (slots.anisotropy_map.as_ref(), true),
    ]
}

/// The identity uv transform's two rows, for every map.
const IDENTITY_XFORMS: [[f32; 4]; 2 * MAP_COUNT] = {
    let mut x = [[0.0; 4]; 2 * MAP_COUNT];
    let mut i = 0;
    while i < MAP_COUNT {
        x[2 * i] = [1.0, 0.0, 0.0, 0.0];
        x[2 * i + 1] = [0.0, 1.0, 0.0, 0.0];
        i += 1;
    }
    x
};

/// [`DrawUniforms::map_xform`] for a material: each map's own transform, as
/// three.js gives every texture its own `matrix`.
fn map_xforms(slots: &crate::materials::MaterialTextureSlots) -> [[f32; 4]; 2 * MAP_COUNT] {
    let mut out = IDENTITY_XFORMS;
    let mut maps = [None; MAP_COUNT];
    maps[..10].copy_from_slice(&[
        slots.map.as_ref(),
        slots.emissive_map.as_ref(),
        slots.normal_map.as_ref(),
        slots.roughness_map.as_ref().or(slots.metalness_map.as_ref()),
        slots.ao_map.as_ref(),
        slots.alpha_map.as_ref(),
        slots.light_map.as_ref(),
        slots.bump_map.as_ref(),
        slots.specular_map.as_ref(),
        // The matcap is looked up by normal, not uv.
        None,
    ]);
    for (i, (t, _)) in physical_maps(slots).into_iter().enumerate() {
        maps[MAP_PHYS0 + i] = t;
    }
    for (i, t) in maps.into_iter().enumerate() {
        if let Some(t) = t {
            let [r0, r1] = t.uv_transform_rows();
            // Row 0's w: the uv set the map reads (`Texture.channel`).
            out[2 * i] = [r0[0], r0[1], r0[2], t.channel.min(3) as f32];
            out[2 * i + 1] = [r1[0], r1[1], r1[2], 0.0];
        }
    }
    out
}

impl Default for DrawUniforms {
    fn default() -> Self {
        Self {
            model: Matrix4::identity().elements,
            normal_mat: Matrix4::identity().elements,
            base_color: [1.0, 1.0, 1.0, 1.0],
            emissive: [0.0; 4],
            specular: [0.0, 0.0, 0.0, 30.0],
            pbr: [1.0, 0.0, 0.0, 0.0],
            misc: [0.0, 1.0, 0.1, 1000.0],
            flags: [0; 4],
            maps: [0; 4],
            map_params: [1.0, 1.0, 1.0, 0.0],
            object: [0; 4],
            extra: [0; 4],
            map_scalars: [1.0, 1.0, 0.0, 0.0],
            env_params: [0.0, 1.0, 0.98, 0.0],
            env_dir: [[-1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]],
            map_samplers: [[0; 4]; 6],
            physical: [[0.0; 4]; 7],
            phys_flags: [0; 4],
            map_xform: IDENTITY_XFORMS,
        }
    }
}

// ------------------------------------------------------------------ pass I/O

/// The attachments of one render pass.
///
/// Produced by [`MetalRenderTarget::attachments`](super::MetalRenderTarget::attachments)
/// for offscreen work and by [`SurfaceFrame`](super::SurfaceFrame) for a window;
/// [`crate::metal::renderer::PassAttachments::from_raw`] covers integrating with an app that already owns
/// its own Metal textures.
#[derive(Clone, Copy)]
pub struct PassAttachments {
    pub(crate) color: Id,
    pub(crate) resolve: Id,
    pub(crate) depth: Id,
    /// Where a multisampled `depth` is resolved when the pass stores depth;
    /// nil for none.
    pub(crate) depth_resolve: Id,
    pub(crate) drawable: Id,
    pub(crate) width: u32,
    pub(crate) height: u32,
    pub(crate) color_format: NSUInteger,
    pub(crate) depth_format: NSUInteger,
    pub(crate) sample_count: u32,
    pub(crate) reverse_z: bool,
}

impl PassAttachments {
    /// Wrap textures this crate did not create.
    ///
    /// # Safety
    /// `color`, `resolve`, `depth` and `drawable` must be `nil` or live objects
    /// of the corresponding Metal type, valid until the next `render` returns.
    /// `color_format` and `sample_count` must describe `color`, or pipeline
    /// creation fails at draw time.
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn from_raw(
        color: Id,
        resolve: Id,
        depth: Id,
        drawable: Id,
        width: u32,
        height: u32,
        color_format: NSUInteger,
        depth_format: NSUInteger,
        sample_count: u32,
    ) -> Self {
        Self {
            color,
            resolve,
            depth,
            depth_resolve: NIL,
            drawable,
            width,
            height,
            color_format,
            depth_format,
            sample_count,
            reverse_z: false,
        }
    }

    /// Resolve a multisampled depth into `texture` — single-sampled,
    /// `DEPTH32_FLOAT`, the attachments' size and slices — whenever the pass
    /// stores depth (`MetalRenderer::set_store_depth`), so it can be sampled
    /// as a `depth2d` afterwards.
    ///
    /// # Safety
    /// `texture` must be nil or a live `MTLTexture` as described, valid until
    /// the next `render` returns.
    pub unsafe fn with_depth_resolve(mut self, texture: Id) -> Self {
        self.depth_resolve = texture;
        self
    }

    /// Invert the depth convention: near at 1, far at 0, and a `greater` depth
    /// test. The depth clear value follows.
    ///
    /// Required on visionOS — the compositor accepts nothing else (`drawable.h`:
    /// "It only supports reverse-Z depth"). Worth having anyway: floating-point
    /// depth has most of its precision near 0, and reverse-Z puts that where the
    /// far plane is, which is where z-fighting otherwise shows up.
    ///
    /// The projection matrix has to agree — `RenderView::projection_matrix` must
    /// map the near plane to 1. A compositor-supplied projection already does.
    pub fn with_reverse_z(mut self, reverse: bool) -> Self {
        self.reverse_z = reverse;
        self
    }

    /// Attachment size in pixels.
    pub fn size(&self) -> (u32, u32) {
        (self.width, self.height)
    }

    /// Whether depth runs near-at-1.
    pub fn reverse_z(&self) -> bool {
        self.reverse_z
    }
}

/// One view of a frame: a camera, and where its pixels land.
///
/// A window renders one of these. A headset renders two — the eyes — which is
/// what [`crate::metal::renderer::MetalRenderer::render_views`] takes and what the visionOS compositor
/// hands over per frame.
#[derive(Clone, Debug)]
pub struct RenderView {
    /// World → view.
    pub view_matrix: Matrix4,
    /// View → clip. Depth must land in `[0, 1]`; reverse-Z (near at 1) is fine
    /// as long as the pass is marked
    /// [`with_reverse_z`](PassAttachments::with_reverse_z).
    pub projection_matrix: Matrix4,
    /// Eye position in world space, for specular and fog.
    pub position: Vector3,
    /// Layer mask; an object is drawn when it intersects this.
    pub layers: Layers,
    /// Array slice of the colour and depth attachments to render into.
    pub slice: u32,
    /// Sub-rectangle of the attachment, or `None` for all of it.
    pub viewport: Option<MTLViewport>,
    /// Colour texture for this view alone, or `nil` to use the pass's. Set for
    /// a compositor layout that gives each eye its own texture.
    pub color: Id,
    /// Depth texture for this view alone, or `nil` to use the pass's.
    pub depth: Id,
}

impl RenderView {
    /// A view from any [`Camera`], filling the whole attachment.
    pub fn from_camera(camera: &dyn Camera) -> Self {
        Self {
            view_matrix: camera.view_matrix(),
            projection_matrix: camera.projection_matrix(),
            position: camera.position(),
            layers: camera.layers(),
            slice: 0,
            viewport: None,
            color: NIL,
            depth: NIL,
        }
    }

    /// A view from explicit matrices — what an XR compositor provides.
    pub fn from_matrices(view_matrix: Matrix4, projection_matrix: Matrix4) -> Self {
        let inverse = view_matrix.invert().elements;
        Self {
            view_matrix,
            projection_matrix,
            position: Vector3::new(inverse[12], inverse[13], inverse[14]),
            layers: Layers::default(),
            slice: 0,
            viewport: None,
            color: NIL,
            depth: NIL,
        }
    }

    /// Render into array slice `slice` of the attachment.
    pub fn with_slice(mut self, slice: u32) -> Self {
        self.slice = slice;
        self
    }

    /// Render into a sub-rectangle of the attachment.
    pub fn with_viewport(mut self, viewport: MTLViewport) -> Self {
        self.viewport = Some(viewport);
        self
    }

    /// Render into textures of this view's own, rather than the pass's.
    ///
    /// # Safety
    /// Both must be `nil` or live `MTLTexture`s matching the pass's format and
    /// sample count, valid until `render_views` returns.
    pub unsafe fn with_textures(mut self, color: Id, depth: Id) -> Self {
        self.color = color;
        self.depth = depth;
        self
    }
}

/// What one [`MetalRenderer::render`] call did.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MetalRenderStats {
    /// Draw calls encoded.
    pub draw_calls: u32,
    /// Triangles submitted, counting instances.
    pub triangles: u32,
    /// Objects skipped because their geometry had no `position` attribute.
    pub skipped: u32,
    /// Items outside a view's frustum, not encoded (counted per view).
    pub culled: u32,
    /// Geometries uploaded this frame (a cache miss or a version bump).
    pub geometry_uploads: u32,
    /// Textures uploaded this frame.
    pub texture_uploads: u32,
    /// Bytes currently held in cached vertex + index buffers. The figure that
    /// decides whether a large scene fits in memory or pages, and the one the
    /// slim line layout exists to halve.
    pub geometry_bytes: u64,
}

// -------------------------------------------------------------------- caches

struct CachedGeometry {
    /// Held to pin the allocation: the cache key is the `Arc`'s address, and a
    /// dropped geometry's address can otherwise be handed to a new one.
    _geometry: Arc<BufferGeometry>,
    version: u32,
    vertices: Owned,
    /// Joint indices and weights, built only when the geometry carries them.
    /// A parallel buffer rather than a wider `Vertex`: skinned meshes are a
    /// minority and 32 bytes a vertex on every other mesh is not a trade worth
    /// making.
    skin: Option<Owned>,
    indices: Option<Owned>,
    index_count: usize,
    vertex_count: usize,
    /// Line-list indices for `wireframe: true` materials, from
    /// [`BufferGeometry::wireframe_indices`]: polygon outlines when the
    /// geometry knows its polygons, so a quad draws without its diagonal.
    /// Built on first wireframe use rather than with the rest, since most
    /// meshes are never drawn that way; a version bump replaces the whole
    /// entry, which drops this with it.
    wire: Option<(Owned, usize)>,
    /// Morph targets, when the geometry has any. Rebuilt with the rest on a
    /// version bump.
    morph: Option<MorphBuffer>,
    /// The `uv1`/`uv2`/`uv3` sets and tangents ([`VB_UV_SETS`]), when it
    /// has any, and which (bit 1 uv sets, bit 2 tangents).
    uv_sets: Option<(Owned, u32)>,
    /// Size of the GPU buffers this entry holds, for `geometry_bytes`.
    bytes: u64,
    /// Object-space bounds, for frustum culling (`item_sphere`); `None` with
    /// no positions, which is never culled.
    sphere: Option<crate::math::Sphere>,
    /// Which layout `vertices` holds. A geometry drawn as lines and then as a
    /// mesh (or the reverse) has to be re-uploaded, not reinterpreted.
    slim: bool,
    last_used: u64,
}

struct CachedTexture {
    _texture: Arc<Texture>,
    texture: Owned,
    sampler_key: SamplerKey,
    last_used: u64,
}

/// One item collected from the scene graph, ready to encode.
/// A shadow-casting light's shadow — `ShadowRecord` in `shaders.metal`, and
/// the wgpu renderer's `ShadowRecordGpu`, field for field.
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct ShadowRecordGpu {
    info: [u32; 4],
    params: [f32; 4],
    extra: [f32; 4],
    pcss: [f32; 4],
    vp: [[f32; 16]; 4],
}

/// What one shadow pass draws with — `ShadowPass` in `shaders.metal`.
#[repr(C)]
#[derive(Clone, Copy)]
struct ShadowPassGpu {
    shadow_vp: [f32; 16],
    point_shadow_pos: [f32; 4],
}

/// The OIT accumulation's targets: the resolved `accum` and `reveal` the
/// composite reads, and their multisampled twins under MSAA.
struct OitTargets {
    size: (u32, u32, u32),
    accum: Owned,
    reveal: Owned,
    accum_ms: Option<Owned>,
    reveal_ms: Option<Owned>,
}

/// Every shadow-casting light this frame, in the order its record takes.
type Casters = Vec<Caster>;

struct DrawItem {
    /// Shell fur's parameters (`FurMaterial::data`) when the material is fur.
    fur: Option<[f32; 64]>,
    geometry: Arc<BufferGeometry>,
    material: Arc<Material>,
    world: Matrix4,
    layers: Layers,
    primitive: NSUInteger,
    /// Per-instance matrices, until they are uploaded — then the index of the
    /// buffer holding them, so a skipped item cannot shift another item's
    /// buffer out from under it.
    instances: Option<Vec<f32>>,
    instance_slot: Option<usize>,
    instance_count: usize,
    /// An `InstancedMesh`'s morph texture (three.js `morphTexture`): one row
    /// of `row_len` floats per instance, until uploaded after the matrices in
    /// the instance buffer — then `(row_len, offset in floats)`, which is
    /// where `apply_morph` reads them.
    instance_morph: Option<(Vec<f32>, usize)>,
    instance_morph_layout: Option<(u32, u32)>,
    /// `InstancedMesh.colors` (three.js `instanceColor`), four floats an
    /// instance, until uploaded after the matrices and morph rows — then
    /// where they start in the instance buffer, in float4s (0: none).
    instance_colors: Option<Vec<f32>>,
    instance_color_offset: u32,
    view_depth: f32,
    render_order: i32,
    transparent: bool,
    /// three.js's transmissive material (`Material::transmissive`): drawn
    /// after the opaque items, reading them from the transmission capture.
    transmissive: bool,
    /// Glass blended over what is behind it, tinting it by channel
    /// (`TransparencyMode::Glass` and `Oit`; `phys_flags.w` in the shader).
    blended_glass: bool,
    /// Order-independent transparency (`TransparencyMode::Oit`).
    oit: bool,
    /// Left out of the capture pass running now (see `capture_transmission`).
    capture_skip: bool,
    /// The side the item is drawn with in place of its material's — the
    /// capture's back-face pass draws double-sided glass as BackSide.
    side_override: Option<u32>,
    /// three.js `depthTest` / `depthWrite` / `depthFunc` / `colorWrite`.
    state: crate::materials::RenderState,
    /// A skinned mesh's bone matrices, until they are uploaded — then the
    /// index of the transient buffer holding them, for the same reason
    /// `instances` works that way.
    bones: Option<Vec<f32>>,
    bone_slot: Option<usize>,
    /// Whether this object draws into the shadow map. Off by default, as it is
    /// on [`Object3D`] — a scene where everything casts is rarely what anyone
    /// means, and it costs a full depth pass over geometry that never shows.
    cast_shadow: bool,
    /// Whether this object is darkened by the shadow map. Also per-object and
    /// also off by default, and for a sharper reason than casting: a surface
    /// that receives without anything casting is unchanged, but one that casts
    /// without receiving shadows itself through acne.
    receive_shadow: bool,
    /// The span of the index (vertices, unindexed) this item draws —
    /// three.js's draw range cut to the item's group. Always set; a full span
    /// is `0..draw_count`.
    range: (u32, u32),
    /// The object's `morph_influences` (empty when it has none).
    morph_influences: Vec<f32>,
    /// A wireframe drawn over a partial span: its line list, in this frame's
    /// transient buffers, and the index count.
    wire_span: Option<(usize, usize)>,
    /// For a `Sprite`: three.js's `sprite_vert` parameters — centre minus
    /// one half (x, y), rotation, and 1 when the quad scales with view depth
    /// (size attenuation off under a perspective view). `vs_mesh` billboards
    /// the draw's unit quad with them; see `sprite_vertex` in the shader.
    sprite: Option<[f32; 4]>,
}

// ------------------------------------------------------------------ renderer

/// Draws a [`Scene`] with Metal.
///
/// Owns the caches (geometry, textures, samplers, pipeline states) and nothing
/// about *where* the pixels go — pass [`PassAttachments`] for that, so one
/// renderer can serve a window and an offscreen target at once.
///
/// # Coverage
///
/// Meshes, instanced meshes, line segments and points, with `Basic`, `Lambert`,
/// `Phong`, `Standard`, `Physical`, `Normal`, `Depth`, `Toon`, `Matcap`, `Line`,
/// `Points` and `Sprite` materials, one base-colour map, and directional, point,
/// spot, hemisphere and ambient lights. Materials outside that set draw unlit in
/// their base colour rather than failing.
///
/// Environment/IBL and the normal, roughness, metalness, AO and emissive map
/// slots are implemented here, and `tests/backend_parity.rs` holds them to the
/// wgpu renderer: emissive maps agree to 0.0024 rms over a luma grid and
/// environment reflections to 0.0009 at mirror roughness.
///
/// Skinning is here too: a `SkinnedMesh` blends through its skeleton's bone
/// matrices in `vs_skinned`, four influences a vertex, and measures identical
/// to the wgpu path in the same pose.
///
/// Shadows are here from every casting light, as on the wgpu path: a
/// depth-only pass per light into a layer of its kind's map array, filtered
/// over the same twelve-tap disc with normal bias, and reused while nothing
/// they show changes (`ShadowMap`). `cast_shadow` and `receive_shadow` are
/// read per object, as the wgpu path reads them, and `tests/shadow_fixtures.rs`
/// and `tests/multi_light_shadows.rs` hold the two to the same placement.
///
/// A point light's map is six layers, its cube faces, written by the only
/// shadow pass here with a fragment stage, because they store *radial*
/// distance rather than projected depth, and looked up by direction with the
/// face chosen by hand. A point light and a spot light in the same place
/// throw the same shadow, 0.0785 of darkening over eight tiles against 0.0786
/// over eight.
///
/// Instanced meshes with non-uniform per-instance scale shade with skewed
/// normals.
///
/// Morph targets are here: `morphAttributes` position, normal and colour,
/// blended in the vertex stage from one float4 buffer per geometry laid out
/// like three.js's morph texture (see `apply_morph` in the shader), before
/// skinning, in every mesh pass including the shadow ones. Groups with a
/// material array draw one item per group, and the draw range cuts every
/// draw — three.js's render list, item for item.
///
/// The direct-light BRDF matches too, as of the `1/pi` diffuse normalisation
/// and height-correlated Smith visibility: every map slot measures within
/// 0.016 rms of the wgpu path. What is still missing there is the roughness
/// floor and the geometric specular antialiasing it applies.
pub struct MetalRenderer {
    device: MetalDevice,
    pipelines: HashMap<PipelineKey, Owned>,
    samplers: HashMap<SamplerKey, Owned>,
    geometries: HashMap<usize, CachedGeometry>,
    textures: HashMap<usize, CachedTexture>,
    /// Depth-stencil states by (`MTLCompareFunction`, write), made on first
    /// use: every material picks its own from `depthTest` / `depthWrite` /
    /// `depthFunc`.
    depth_states: HashMap<(NSUInteger, bool), Owned>,
    white: Owned,
    /// three.js's `RectAreaLight` tables, at fragment texture 28.
    ltc: Owned,
    /// three.js's transmission render target — see [`MetalCapture`].
    transmission: Option<MetalCapture>,
    /// The capture the colour pass binds at fragment texture 29
    /// (`capture_dummy` when `None`), and its trilinear sampler at 14.
    capture_bound: Option<Id>,
    /// A 1×1 black array in the capture's shape, for frames with nothing
    /// transmissive.
    capture_dummy: Owned,
    transmission_sampler: Owned,
    /// Bound at the cube slot when a scene has no environment.
    black_cube: Owned,
    /// The shadow maps: a depth array per kind, a layer per casting light
    /// (six, its cube faces, per point light), with the directional and spot
    /// arrays' sizes and every array's layer count. Allocated on the first
    /// frame a light asks to cast, because the size comes from the light and
    /// no light exists when the renderer is built.
    shadow_map: Option<Owned>,
    shadow_size: u32,
    spot_shadow_map: Option<Owned>,
    spot_shadow_size: u32,
    point_shadow_map: Option<Owned>,
    shadow_layers: [usize; 3],
    /// What the maps were last drawn from (see `shadow_fingerprint`).
    /// Each shadow pass's content hash when last drawn, so an unchanged pass
    /// is skipped.
    shadow_pass_hashes: Vec<Option<u64>>,
    /// Depth-only pipeline and comparison sampler for that map.
    shadow_pipeline: Option<Owned>,
    /// The point variant, which writes radial distance from a fragment stage.
    point_shadow_pipeline: Option<Owned>,
    /// Directional/spot shadows for casters cut by `alphaTest` and a map.
    cut_shadow_pipeline: Option<Owned>,
    shadow_sampler: Option<Owned>,
    /// Bound at the shadow slot when nothing casts. Metal requires every
    /// resource a function declares to be bound, and a 1x1 depth texture is
    /// cheaper than a second fragment function.
    empty_shadow: Owned,
    /// The uploaded environment, keyed by the `CubeTexture` it came from so a
    /// scene that keeps the same environment across frames uploads it once.
    /// Environments are large — six faces and a mip chain — and rebuilding one
    /// per frame would cost more than everything else this backend does.
    env_cube: Option<(crate::renderer::gpu_texture::CubeCacheKey, EnvGpu)>,
    /// Materials' own `envMap`s, keyed by the cube's upload
    /// (`cube_cache_key`: its stable id, format and size).
    material_envs: HashMap<crate::renderer::gpu_texture::CubeCacheKey, EnvGpu>,
    /// three.js `WebGLCubeUVMaps`: each cube texture lighting a
    /// Standard/Physical material, prefiltered through PMREM once, by its
    /// source's key — as the wgpu renderer does.
    pmrem_cache: HashMap<crate::renderer::gpu_texture::CubeCacheKey, Arc<crate::textures::CubeTexture>>,
    /// The scene environment's intensity and lookup rows, for the draws that
    /// read it.
    env_light: (f32, [[f32; 4]; 3]),
    identity_instance: Owned,
    transients: Vec<Vec<Owned>>,
    frame: u64,
    cache_retention: u64,
    /// Keep the depth attachment after the pass, for readers like SSAO.
    store_depth: bool,
    /// This frame has OIT items and draws them in their own passes; and which
    /// of `encode_pass`'s two jobs is running — 0 everything else, 1 the OIT
    /// accumulation (`encode_oit`).
    oit_active: bool,
    oit_phase: u8,
    oit_targets: Option<OitTargets>,
    /// The composite's two halves, by colour format and sample count.
    oit_composite: HashMap<(NSUInteger, u32), (Owned, Owned)>,
    /// Line width in pixels; 0 keeps one-pixel hardware lines.
    line_width: f32,
    /// three.js `renderer.toneMapping` / `toneMappingExposure`, applied in
    /// the fragment stage to every material that is `tone_mapped`.
    tone_mapping: (crate::ToneMapping, f32),
    /// See [`set_specular_antialiasing`](MetalRenderer::set_specular_antialiasing).
    specular_antialiasing: bool,
    /// See [`set_reflective_transparency`](MetalRenderer::set_reflective_transparency).
    reflective_transparency: bool,
    /// three.js `renderer.shadowMap.type`; a light's `ShadowSettings::filter`
    /// overrides it.
    shadow_type: crate::lights::ShadowType,
    /// Last `geometry_bytes` reported, so the log fires on change not per frame.
    /// Atomic because the frame build runs behind `&self`.
    logged_geometry_bytes: std::sync::atomic::AtomicU64,
    /// three.js viewport / scissor / auto-clear state. See
    /// [`crate::metal::viewport`].
    pub(super) viewport_state: super::viewport::MetalViewportState,
    /// three.js `shadowMap.autoUpdate` / `needsUpdate`.
    shadow_flags: crate::renderer::ShadowMap,
    /// Caster kinds and directional map size the maps were last drawn for.
    shadow_sig: Option<(usize, usize, usize, u32, u32)>,
    shadow_updates: u64,
}

impl MetalRenderer {
    /// Acquire the default device and build the renderer.
    pub fn new() -> Result<Self, MetalError> {
        Self::with_device(MetalDevice::new()?)
    }

    /// Build on an existing device — use this to share one device (and one
    /// compiled shader library) between a window and an offscreen target.
    pub fn with_device(device: MetalDevice) -> Result<Self, MetalError> {
        let mut depth_states = HashMap::new();
        // The shadow passes' state, forward-Z with writes.
        depth_states.insert(
            (compare::LESS, true),
            depth_stencil_state(&device, compare::LESS, true)?,
        );
        let white = white_texture(&device)?;
        let ltc = crate::metal::resources::upload_ltc(&device)?;
        let capture_dummy = crate::metal::resources::black_texture_array(&device)?;
        let transmission_sampler = new_sampler(
            &device,
            SamplerKey { mag_linear: true, min_linear: true, mipmapped: true, mip_linear: true, wrap_s: 0, wrap_t: 0 },
        )?;
        let black_cube = crate::metal::resources::black_cube(&device)?;
        let empty_shadow = crate::metal::resources::empty_shadow_map(&device)?;
        let identity_instance =
            device.new_buffer(bytemuck::cast_slice(&[Matrix4::identity().elements]))?;
        let viewport_state = super::viewport::MetalViewportState::new(device.clone());
        // The samplers every draw falls back to, made up front: a draw with no
        // texture would otherwise find neither in the cache and bind nil in
        // every sampler slot, which Metal's validation layer rejects.
        let mut samplers = HashMap::new();
        for key in [SamplerKey::default_linear(), SamplerKey::environment()] {
            samplers.insert(key, new_sampler(&device, key)?);
        }
        Ok(Self {
            device,
            pipelines: HashMap::new(),
            samplers,
            geometries: HashMap::new(),
            textures: HashMap::new(),
            depth_states,
            white,
            ltc,
            capture_dummy,
            transmission: None,
            capture_bound: None,
            transmission_sampler,
            black_cube,
            material_envs: HashMap::new(),
            pmrem_cache: HashMap::new(),
            env_light: (1.0, crate::materials::environment_lookup_rows(Default::default(), true)),
            env_cube: None,
            shadow_map: None,
            shadow_size: 0,
            spot_shadow_map: None,
            spot_shadow_size: 0,
            point_shadow_map: None,
            shadow_layers: [0; 3],
            shadow_pass_hashes: Vec::new(),
            shadow_pipeline: None,
            point_shadow_pipeline: None,
            cut_shadow_pipeline: None,
            shadow_sampler: None,
            empty_shadow,
            identity_instance,
            transients: (0..FRAMES_IN_FLIGHT).map(|_| Vec::new()).collect(),
            frame: 0,
            cache_retention: DEFAULT_CACHE_RETENTION,
            store_depth: false,
            oit_active: false,
            oit_phase: 0,
            oit_targets: None,
            oit_composite: HashMap::new(),
            line_width: 0.0,
            tone_mapping: (crate::ToneMapping::None, 1.0),
            specular_antialiasing: false,
            reflective_transparency: false,
            shadow_type: crate::lights::ShadowType::default(),
            logged_geometry_bytes: std::sync::atomic::AtomicU64::new(0),
            viewport_state,
            shadow_flags: Default::default(),
            shadow_sig: None,
            shadow_updates: 0,
        })
    }

    /// three.js `renderer.shadowMap`, as on the wgpu
    /// [`Renderer::shadow_map`](crate::Renderer::shadow_map): with
    /// `auto_update` off, a render redraws the shadow maps only when
    /// `needs_update` is set (and clears it). See
    /// [`ShadowMap`](crate::renderer::ShadowMap).
    pub fn shadow_map(&self) -> &crate::renderer::ShadowMap {
        &self.shadow_flags
    }

    pub fn shadow_map_mut(&mut self) -> &mut crate::renderer::ShadowMap {
        &mut self.shadow_flags
    }

    /// How many renders so far drew the shadow maps.
    pub fn shadow_map_updates(&self) -> u64 {
        self.shadow_updates
    }

    /// The device this renderer draws with.
    pub fn device(&self) -> &MetalDevice {
        &self.device
    }

    /// Drop every cached GPU resource. The next frame re-uploads what it needs.
    pub fn clear_caches(&mut self) {
        self.geometries.clear();
        self.textures.clear();
    }

    /// Geometries and textures currently cached on the GPU.
    pub fn cache_sizes(&self) -> (usize, usize) {
        (self.geometries.len(), self.textures.len())
    }

    /// How many frames an unused geometry or texture stays on the GPU before it
    /// is dropped. Default [`DEFAULT_CACHE_RETENTION`].
    ///
    /// The cache holds an `Arc` to everything it has uploaded, so without an
    /// upper bound a scene rebuilt each frame would pin every geometry it has
    /// ever drawn — in GPU memory *and* in system memory. Raise it if objects
    /// come and go over a longer cycle than the default; `u64::MAX` never
    /// evicts.
    /// Draw lines as coverage-weighted screen-space quads of this pixel width.
    /// 0 (the default) keeps hardware lines, which are all-or-nothing at one
    /// pixel and so flicker for anything thinner.
    pub fn set_line_width(&mut self, px: f32) {
        self.line_width = px.max(0.0);
    }

    /// three.js `renderer.toneMapping` and `toneMappingExposure`, the same
    /// curves as the wgpu backend's [`crate::Renderer::set_tone_mapping`]:
    /// applied per material, before the sRGB encode, to every material whose
    /// `tone_mapped` is set (normal and depth materials never are).
    pub fn set_tone_mapping(&mut self, mode: crate::ToneMapping, exposure: f32) {
        self.tone_mapping = (mode, exposure);
    }

    /// The tone mapping set by [`set_tone_mapping`](Self::set_tone_mapping).
    pub fn tone_mapping(&self) -> (crate::ToneMapping, f32) {
        self.tone_mapping
    }

    /// Geometric specular antialiasing on Standard and Physical materials in
    /// place of three.js's `geometryRoughness`, as
    /// [`crate::Renderer::set_specular_antialiasing`]. Off by default.
    pub fn set_specular_antialiasing(&mut self, on: bool) {
        self.specular_antialiasing = on;
    }

    pub fn specular_antialiasing(&self) -> bool {
        self.specular_antialiasing
    }

    /// threers's reflective transparency on Standard and Physical, as
    /// [`crate::Renderer::set_reflective_transparency`]: a transparent
    /// surface's alpha raised by its specular reflectance. Off by default —
    /// three.js blends the whole colour by `opacity`.
    /// How shadow maps are filtered — three.js's `renderer.shadowMap.type`,
    /// as `Renderer::set_shadow_type`.
    pub fn set_shadow_type(&mut self, kind: crate::lights::ShadowType) {
        self.shadow_type = kind;
    }

    pub fn shadow_type(&self) -> crate::lights::ShadowType {
        self.shadow_type
    }

    pub fn set_reflective_transparency(&mut self, on: bool) {
        self.reflective_transparency = on;
    }

    pub fn reflective_transparency(&self) -> bool {
        self.reflective_transparency
    }

    /// Keep the depth attachment after each pass so a later pass can sample it.
    /// Off by default: it costs bandwidth that nothing otherwise reads.
    pub fn set_store_depth(&mut self, on: bool) {
        self.store_depth = on;
    }

    pub fn set_cache_retention(&mut self, frames: u64) {
        self.cache_retention = frames;
    }

    /// Draw `scene` from `camera` into `pass`, on a command buffer of its own.
    ///
    /// Does not block: the command buffer is committed and the call returns. A
    /// readback or a present sequences behind it on the same queue.
    pub fn render(
        &mut self,
        scene: &mut Scene,
        camera: &dyn Camera,
        pass: &PassAttachments,
    ) -> Result<MetalRenderStats, MetalError> {
        self.render_views(scene, &[RenderView::from_camera(camera)], pass)
    }

    /// Draw `scene` once for each view — one for a window, two for the eyes of
    /// a headset.
    ///
    /// Both eyes go through the scene graph, the sort and the encoder **once**
    /// when the views are *layered*: same colour texture, same viewport,
    /// different array slices. The vertex stage then reads its eye out of the
    /// instance id and writes `render_target_array_index`, so the second eye
    /// costs pixels and not CPU. Anything else — a texture or a viewport per
    /// eye — is one pass per view, and the clear happens once per distinct
    /// target so the second pass cannot wipe the first.
    ///
    /// At most [`MAX_VIEWS`] views; extra ones are ignored.
    pub fn render_views(
        &mut self,
        scene: &mut Scene,
        views: &[RenderView],
        pass: &PassAttachments,
    ) -> Result<MetalRenderStats, MetalError> {
        let _pool = AutoreleasePool::new();
        if views.is_empty() {
            return Err(MetalError::Unsupported("render with no views".into()));
        }
        let views = &views[..views.len().min(MAX_VIEWS)];
        // The viewport / scissor / auto-clear state. The default state plans
        // as `legacy` and takes the path below untouched.
        self.viewport_state.buffer_size = (pass.width, pass.height);
        let staged = match self.viewport_state.settings.plan(pass.width, pass.height) {
            None => {
                // Nothing visible: draw nothing, but still present a frame.
                self.present(pass)?;
                return Ok(MetalRenderStats::default());
            }
            Some(p) if p.legacy => None,
            Some(p) => Some(self.viewport_state.stage(&p, views, pass)?),
        };
        // A pane drawn into its own target is copied back afterwards.
        let composite = staged.as_ref().and_then(|s| s.composite);
        let (views, pass): (&[RenderView], &PassAttachments) = match &staged {
            Some(s) => (&s.views, &s.pass),
            None => (views, pass),
        };
        self.frame = self.frame.wrapping_add(1);
        let slot = (self.frame % FRAMES_IN_FLIGHT as u64) as usize;
        self.transients[slot].clear();

        scene.update_world();
        self.ensure_environment(scene);
        let (mut frame_u, mut items, mut stats, casters) = self.collect(scene, views, pass);
        // Each casting light's record — layers, matrices, biases, filter —
        // and the passes that draw its maps, as the wgpu renderer builds them.
        self.ensure_shadow_arrays(&casters)?;
        // `(kind: 0 directional, 1 spot, 2 point; layer; matrix; point light)`.
        let mut shadow_passes: Vec<(u8, usize, [f32; 16], [f32; 4])> = Vec::new();
        let mut next_layer = [0usize; 3];
        for (r, caster) in casters.iter().enumerate() {
            let settings = caster.settings();
            let filter = settings.filter.unwrap_or(self.shadow_type).code();
            let (bias, normal_bias) = (settings.bias.max(0.0), settings.normal_bias.max(0.0));
            let mut rec = ShadowRecordGpu::default();
            match *caster {
                Caster::Dir { dir, settings } => {
                    let texel = 1.0 / self.shadow_size.max(1) as f32;
                    let layer = next_layer[0];
                    let cascades = caster.layers();
                    next_layer[0] += cascades;
                    let angle = if settings.light_size > 0.0 { settings.light_size } else { 0.02 };
                    let (depth_range, width) = if cascades > 1 {
                        (12.0, 2.0)
                    } else {
                        (settings.camera_far - settings.camera_near, 2.0 * settings.camera_size)
                    };
                    let scale = angle * depth_range / width.max(1e-6);
                    rec.info = [0, layer as u32, cascades as u32, filter];
                    rec.params = [bias, normal_bias, settings.radius.max(0.0) * texel, texel];
                    rec.pcss = [(scale * 0.25).clamp(2.0 * texel, 0.1), 0.0, 0.0, scale];
                    if cascades > 1 {
                        let view = &views[0];
                        for (c, (vp, far)) in crate::lights::shadow::cascade_frusta(
                            &view.view_matrix,
                            &view.projection_matrix,
                            dir,
                            &settings,
                            self.shadow_size,
                        )
                        .into_iter()
                        .enumerate()
                        {
                            rec.vp[c] = vp;
                            rec.extra[c] = far;
                            shadow_passes.push((0, layer + c, vp, [0.0; 4]));
                        }
                    } else {
                        let s = settings.camera_size;
                        // A fixed cube on the origin, matching the wgpu path.
                        let eye = dir.normalize() * (-s * 1.5);
                        let view = Matrix4::look_at(eye, Vector3::ZERO, Vector3::UP);
                        let proj =
                            Matrix4::orthographic(-s, s, s, -s, settings.camera_near, settings.camera_far);
                        let vp = proj.multiply(&view).elements;
                        rec.vp[0] = vp;
                        rec.extra = [f32::MAX; 4];
                        shadow_passes.push((0, layer, vp, [0.0; 4]));
                    }
                }
                Caster::Spot { pos, dir, angle, settings } => {
                    let texel = 1.0 / self.spot_shadow_size.max(1) as f32;
                    let layer = next_layer[1];
                    next_layer[1] += 1;
                    let view = Matrix4::look_at(pos, pos + dir.normalize(), Vector3::UP);
                    // fov = 2 × the outer angle, so the frustum covers the whole cone.
                    let fov = (angle * 2.0).clamp(0.05, std::f32::consts::PI - 0.05);
                    let proj =
                        Matrix4::perspective(fov, 1.0, settings.camera_near, settings.camera_far);
                    let vp = proj.multiply(&view).elements;
                    let size = if settings.light_size > 0.0 { settings.light_size } else { 0.5 };
                    let scale = size / (2.0 * (fov * 0.5).tan());
                    rec.info = [1, layer as u32, 1, filter];
                    rec.params = [bias, normal_bias, settings.radius.max(0.0) * texel, texel];
                    rec.pcss = [(scale * 0.05).clamp(2.0 * texel, 0.1), settings.camera_near, settings.camera_far, scale];
                    rec.vp[0] = vp;
                    shadow_passes.push((1, layer, vp, [0.0; 4]));
                }
                Caster::Point { pos, radius, settings } => {
                    // A face texel (`2 / size` of a unit cube face) per unit
                    // of radius, across the direction to the light.
                    let texel = 2.0 / POINT_SHADOW_SIZE as f32;
                    let k = next_layer[2];
                    next_layer[2] += 6;
                    let at = [pos.x, pos.y, pos.z, radius];
                    let size = if settings.light_size > 0.0 { settings.light_size } else { 0.5 };
                    rec.info = [2, k as u32, 1, filter];
                    rec.params = [bias, normal_bias, settings.radius.max(0.0) * texel, texel];
                    rec.extra = at;
                    rec.pcss = [(size * 0.05).clamp(2.0 * texel, 0.2), 0.0, 0.0, size];
                    // Six faces, +X −X +Y −Y +Z −Z, each a 90° frustum. Metal's
                    // cube convention, which is not the GL one the wgpu path
                    // uses: +Y is up on the side faces rather than −Y. Getting
                    // this wrong leaves each face individually plausible and
                    // the shadow landing in the wrong place.
                    let faces: [(Vector3, Vector3); 6] = [
                        (Vector3::new(1.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 0.0)),
                        (Vector3::new(-1.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 0.0)),
                        (Vector3::new(0.0, 1.0, 0.0), Vector3::new(0.0, 0.0, -1.0)),
                        (Vector3::new(0.0, -1.0, 0.0), Vector3::new(0.0, 0.0, 1.0)),
                        (Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 1.0, 0.0)),
                        (Vector3::new(0.0, 0.0, -1.0), Vector3::new(0.0, 1.0, 0.0)),
                    ];
                    let proj = Matrix4::perspective(std::f32::consts::FRAC_PI_2, 1.0, 0.1, radius.max(1.0));
                    for (face, &(forward, up)) in faces.iter().enumerate() {
                        let view = Matrix4::look_at(pos, pos + forward, up);
                        let vp = mirror_clip_x(proj.multiply(&view)).elements;
                        shadow_passes.push((2, k + face, vp, at));
                    }
                }
            }
            frame_u.shadows[r] = rec;
        }
        frame_u.shadow_info = [casters.len() as u32, 0, 0, 0];
        // What the maps show, for reusing them (`ShadowMap`): taken here,
        // before the uploads below move the instance and bone data out.
        let shadow_content = (!shadow_passes.is_empty()).then(|| shadow_fingerprint(&items));

        // Opaque front-to-back (early-z rejects the overdraw), transparent
        // back-to-front (blending is order-dependent). `render_order` wins over
        // both, as in three.js.
        items.sort_by(|a, b| {
            a.transparent
                .cmp(&b.transparent)
                // three.js's transmissive list sits between the two.
                .then(a.transmissive.cmp(&b.transmissive))
                .then(a.render_order.cmp(&b.render_order))
                .then_with(|| {
                    let (x, y) = if a.transparent {
                        (b.view_depth, a.view_depth)
                    } else {
                        (a.view_depth, b.view_depth)
                    };
                    x.partial_cmp(&y).unwrap_or(std::cmp::Ordering::Equal)
                })
        });

        // Upload before encoding: both touch `self`, and the encoder holds a
        // command buffer that a mipmap blit would otherwise interleave with.
        for item in &mut items {
            let slim = item.primitive == primitive::LINE && slim_lines_enabled();
            if self.ensure_geometry(&item.geometry, slim)? {
                stats.geometry_uploads += 1;
            }
            if item.primitive == primitive::TRIANGLE && item.material.wireframe() {
                let full = item.range == (0, item.geometry.draw_count() as u32);
                if full {
                    self.ensure_wire_indices(&item.geometry)?;
                } else {
                    // A group or a draw range: the line list of that span
                    // alone, for this frame.
                    let flat = item.geometry.wireframe_indices_in(item.range.0, item.range.1);
                    if !flat.is_empty() {
                        let buf = self.device.new_buffer(bytemuck::cast_slice(&flat))?;
                        item.wire_span = Some((self.transients[slot].len(), flat.len()));
                        self.transients[slot].push(buf);
                    }
                }
            }
            let slots = item.material.texture_slots();
            // Every slot the fragment shader can bind. Uploading only the ones
            // it used to sample is how the extra maps stayed invisible after
            // the shader learned to read them: the binding asked the cache for
            // a texture nobody had put there.
            let phys: Vec<(Arc<Texture>, bool)> = physical_maps(&slots)
                .into_iter()
                .filter_map(|(m, l)| m.map(|m| (m.clone(), l)))
                .collect();
            for (map, linear) in [
                (slots.map, false),
                (slots.matcap_map, false),
                (slots.gradient_map, false),
                (slots.emissive_map, false),
                (slots.normal_map, true),
                (slots.roughness_map, true),
                (slots.metalness_map, true),
                (slots.ao_map, true),
                (slots.alpha_map, true),
                // Colour, decoded by its texture's colour space, as three.js does.
                (slots.light_map, false),
                (slots.bump_map, true),
                (slots.specular_map, true),
            ]
            .into_iter()
            .filter_map(|(m, l)| m.map(|m| (m, l)))
            .chain(phys)
            {
                if self.ensure_texture_as(&map, linear)? {
                    stats.texture_uploads += 1;
                }
            }
            // A Standard/Physical material's own `envMap` prefiltered, as the
            // scene's is; a Basic/Lambert/Phong one sampled as it is.
            let pbr = slots.pbr_env_map.as_ref().map(|c| self.prefiltered(c));
            for cube in [slots.env_map.as_ref(), pbr.as_ref()].into_iter().flatten() {
                let key = crate::renderer::gpu_texture::cube_cache_key(cube);
                if !self.material_envs.contains_key(&key) {
                    if let Ok(env) = EnvGpu::upload(&self.device, cube) {
                        self.material_envs.insert(key, env);
                        stats.texture_uploads += 1;
                    }
                    let sk = SamplerKey::from_cube(cube);
                    if !self.samplers.contains_key(&sk) {
                        if let Ok(smp) = new_sampler(&self.device, sk) {
                            self.samplers.insert(sk, smp);
                        }
                    }
                }
            }
            if let Some(mut data) = item.instances.take() {
                item.instance_count = data.len() / 16;
                if let Some((rows, row_len)) = item.instance_morph.take() {
                    item.instance_morph_layout = Some((row_len as u32, data.len() as u32));
                    data.extend_from_slice(&rows);
                }
                if let Some(colors) = item.instance_colors.take() {
                    // float4-aligned, after everything else.
                    data.resize(data.len().div_ceil(4) * 4, 0.0);
                    item.instance_color_offset = (data.len() / 4) as u32;
                    data.extend_from_slice(&colors);
                }
                let buf = self.device.new_buffer(bytemuck::cast_slice(&data))?;
                item.instance_slot = Some(self.transients[slot].len());
                self.transients[slot].push(buf);
            }
            if let Some(data) = item.bones.take() {
                // Transient rather than cached: a pose changes every frame, so
                // there is nothing to reuse and a cache would only be a thing
                // to invalidate.
                if !data.is_empty() {
                    let buf = self.device.new_buffer(bytemuck::cast_slice(&data))?;
                    item.bone_slot = Some(self.transients[slot].len());
                    self.transients[slot].push(buf);
                }
            }
        }

        // Whether this render redraws them — see `ShadowMap`. They depend on
        // lights and casters, not the camera, so a render that skips them
        // samples what an identical redraw would have written: with
        // `auto_update` on, a render whose casters and lights match the last
        // one's keeps its maps.
        // Each pass's own content: the casters and its light's matrix. Only
        // the passes whose changed are redrawn — a cascade following the
        // camera does not take a still point light's faces with it.
        let sig = (next_layer[0], next_layer[1], next_layer[2], self.shadow_size, self.spot_shadow_size);
        let valid = self.shadow_sig == Some(sig);
        let pass_hashes: Vec<Option<u64>> = shadow_passes
            .iter()
            .map(|(kind, layer, vp, at)| {
                shadow_content.map(|c| {
                    let mut h = (c.rotate_left(5) ^ (*layer as u64 | (*kind as u64) << 32)).wrapping_mul(0x517c_c1b7_2722_0a95);
                    for v in vp.iter().chain(at) {
                        h = (h.rotate_left(5) ^ v.to_bits() as u64).wrapping_mul(0x517c_c1b7_2722_0a95);
                    }
                    h
                })
            })
            .collect();
        let changed: Vec<bool> = pass_hashes
            .iter()
            .enumerate()
            .map(|(k, h)| !valid || self.shadow_pass_hashes.get(k) != Some(h))
            .collect();
        let update_shadows = !shadow_passes.is_empty() && {
            self.shadow_sig = Some(sig);
            self.shadow_flags.take_update(valid, !changed.iter().any(|&c| c))
        };
        // A forced update (`needs_update` with `auto_update` off) redraws
        // every pass; an automatic one, the ones that changed.
        let draw_pass: Vec<bool> = changed
            .iter()
            .map(|&c| update_shadows && (!self.shadow_flags.auto_update || c))
            .collect();
        if update_shadows {
            self.shadow_pass_hashes = pass_hashes;
        }
        if draw_pass.iter().any(|&d| d) {
            self.shadow_updates += 1;
        }
        // Shadow maps first: the main pass samples them, so they have to be
        // filled before anything reads them. Each pass writes its own matrix
        // into `shadow_vp` and shares one depth-only vertex function, rather
        // than having three that differ only in which matrix they read.
        for (&(kind, layer, vp, at), _) in shadow_passes.iter().zip(&draw_pass).filter(|(_, &d)| d) {
            let map = match kind {
                0 => self.shadow_map.as_ref(),
                1 => self.spot_shadow_map.as_ref(),
                _ => self.point_shadow_map.as_ref(),
            };
            let Some(map) = map.map(|t| t.id()) else { continue };
            let pass_u = ShadowPassGpu { shadow_vp: vp, point_shadow_pos: at };
            // Casters outside this light's frustum are not drawn into it.
            let frustum = crate::math::Frustum::from_projection_matrix(&Matrix4 { elements: vp });
            unsafe { self.encode_shadow(&pass_u, &items, slot, map, layer, kind == 2, &frustum) }?;
        }
        // three.js's `renderTransmissionPass`, when anything is transmissive.
        if items.iter().any(|i| i.transmissive) {
            self.capture_transmission(pass, views, &frame_u, &mut items, scene, slot, &mut stats)?;
        } else {
            self.transmission = None;
            self.capture_bound = None;
        }
        frame_u.rect_counts[1] = self.transmission.as_ref().map_or(0, |c| c.levels);
        let vp = staged.as_ref().map(|s| (s.clear, s.scissor));
        unsafe { self.encode(pass, views, &frame_u, &items, scene, slot, &mut stats, vp) }?;
        if let Some(c) = composite {
            self.viewport_state.composite(&c)?;
            // The pane pass had the drawable stripped; present after the copy.
            self.present(&c.dest)?;
        }
        self.evict_unused();
        Ok(stats)
    }

    /// three.js r165's `renderTransmissionPass`: the scene without its
    /// transmissive items drawn linear and untone-mapped into a half-float
    /// target the size of this pass, from the first view, copied into a
    /// mipmapped texture whose mips Metal generates; then the back faces of
    /// double-sided transmissive items drawn over it, reading that copy, and
    /// copied again. The colour pass binds the result (`capture_bound`).
    #[allow(clippy::too_many_arguments)]
    fn capture_transmission(
        &mut self,
        pass: &PassAttachments,
        views: &[RenderView],
        frame_u: &FrameUniforms,
        items: &mut [DrawItem],
        scene: &Scene,
        slot: usize,
        stats: &mut MetalRenderStats,
    ) -> Result<(), MetalError> {
        // The size the views draw at: the pass, or the largest of their
        // viewports — three.js sizes the capture to the drawing buffer a view
        // renders into, and a side-by-side eye renders into half of one.
        let (w, h) = views.iter().fold((0u32, 0u32), |(w, h), v| match v.viewport {
            Some(vp) => (w.max(vp.width.round() as u32), h.max(vp.height.round() as u32)),
            None => (w.max(pass.width), h.max(pass.height)),
        });
        let (w, h) = (w.clamp(1, pass.width.max(1)), h.clamp(1, pass.height.max(1)));
        let slices = views.len().clamp(1, MAX_VIEWS) as u32;
        if self.transmission.as_ref().map(|c| (c.size, c.slices)) != Some(((w, h), slices)) {
            let levels = 32 - w.max(h).leading_zeros();
            let target = if slices > 1 {
                crate::metal::MetalRenderTarget::layered(&self.device, w, h, pixel_format::RGBA16_FLOAT, slices)?
            } else {
                crate::metal::MetalRenderTarget::new(&self.device, w, h, pixel_format::RGBA16_FLOAT, 1)?
            };
            let mipped = crate::metal::resources::new_texture_array_levels(
                &self.device,
                w,
                h,
                pixel_format::RGBA16_FLOAT,
                texture_usage::SHADER_READ | texture_usage::RENDER_TARGET,
                slices,
                levels,
            )?;
            self.transmission = Some(MetalCapture { target, mipped, size: (w, h), levels, slices });
        }
        let attachments = self.transmission.as_ref().expect("just made").target.attachments();
        // Every view captures the scene from where it stands, into its own
        // slice, over the whole capture — a view drawn into a viewport reads
        // its slice through its own projection, so where its rectangle sits
        // does not matter here.
        let capture_views: Vec<RenderView> = views[..slices as usize]
            .iter()
            .enumerate()
            .map(|(i, v)| {
                let mut view = v.clone();
                view.color = NIL;
                view.depth = NIL;
                view.slice = i as u32;
                view.viewport = None;
                view
            })
            .collect();
        // Linear, untone-mapped, as three.js draws the transmission target.
        let mut fu = *frame_u;
        fu.view_info[0] = slices;
        fu.view_info[1] = 0;
        fu.view_info[2] = 0;
        fu.rect_counts[1] = 1;
        fu.viewport = [w as f32, h as f32, 1.0 / w as f32, 1.0 / h as f32];

        for it in items.iter_mut() {
            it.capture_skip = it.transmissive;
        }
        self.capture_bound = None;
        unsafe { self.encode(&attachments, &capture_views, &fu, items, scene, slot, stats, None) }?;
        self.copy_capture()?;

        if items.iter().any(|i| i.transmissive && i.material.side() == 2) {
            for it in items.iter_mut() {
                let back = it.transmissive && it.material.side() == 2;
                it.capture_skip = !back;
                it.side_override = back.then_some(1);
            }
            let cap = self.transmission.as_ref().expect("made above");
            self.capture_bound = Some(cap.mipped.id());
            fu.rect_counts[1] = cap.levels;
            let keep = Some((crate::renderer::viewport::ClearMode::None, None));
            unsafe { self.encode(&attachments, &capture_views, &fu, items, scene, slot, stats, keep) }?;
            self.copy_capture()?;
        }
        for it in items.iter_mut() {
            it.capture_skip = false;
            it.side_override = None;
        }
        self.capture_bound = self.transmission.as_ref().map(|c| c.mipped.id());
        Ok(())
    }

    /// The capture target's colour into level 0 of `mipped`, then its mips.
    fn copy_capture(&self) -> Result<(), MetalError> {
        let Some(cap) = &self.transmission else { return Ok(()) };
        let _pool = AutoreleasePool::new();
        unsafe {
            let cmd = self.device.command_buffer();
            if cmd.is_null() {
                return Err(MetalError::NoCommandQueue);
            }
            let blit: Id = msg0(cmd, sel!("blitCommandEncoder"));
            let (w, h) = cap.size;
            for slice in 0..cap.slices as NSUInteger {
                let _: () = super::objc::msg9(
                    blit,
                    sel!("copyFromTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:toTexture:destinationSlice:destinationLevel:destinationOrigin:"),
                    cap.target.color_texture(),
                    slice,
                    0usize,
                    MTLOrigin { x: 0, y: 0, z: 0 },
                    MTLSize { width: w as NSUInteger, height: h as NSUInteger, depth: 1 },
                    cap.mipped.id(),
                    slice,
                    0usize,
                    MTLOrigin { x: 0, y: 0, z: 0 },
                );
            }
            let _: () = msg1(blit, sel!("generateMipmapsForTexture:"), cap.mipped.id());
            let _: () = msg0(blit, sel!("endEncoding"));
            let _: () = msg0(cmd, sel!("commit"));
        }
        Ok(())
    }

    /// Drop cache entries no frame has touched in [`cache_retention`](Self::set_cache_retention)
    /// frames.
    fn evict_unused(&mut self) {
        if self.cache_retention == u64::MAX {
            return;
        }
        let cutoff = self.frame.saturating_sub(self.cache_retention);
        self.geometries.retain(|_, g| g.last_used >= cutoff);
        self.textures.retain(|_, t| t.last_used >= cutoff);
    }

    // ------------------------------------------------------------ collection

    fn collect(
        &self,
        scene: &Scene,
        views: &[RenderView],
        pass: &PassAttachments,
    ) -> (FrameUniforms, Vec<DrawItem>, MetalRenderStats, Casters) {
        // View 0 is the one the traversal uses: its layer mask decides what is
        // drawn and its position orders the transparent items. Two eyes a few
        // centimetres apart do not disagree about either.
        let primary = &views[0];
        let eye = primary.position;
        // A perspective projection's last column is (0, 0, -1, 0) — the test
        // the raycaster and the wgpu sprite shader use too.
        let primary_perspective = primary.projection_matrix.elements[15] == 0.0;
        let mut view_proj = [Matrix4::identity().elements; MAX_VIEWS];
        let mut view_matrices = [Matrix4::identity().elements; MAX_VIEWS];
        let mut camera_pos = [[0.0f32; 4]; MAX_VIEWS];
        for (i, v) in views.iter().enumerate() {
            view_proj[i] = v.projection_matrix.multiply(&v.view_matrix).elements;
            view_matrices[i] = v.view_matrix.elements;
            camera_pos[i] = [v.position.x, v.position.y, v.position.z, 1.0];
        }
        let mut frame_u = FrameUniforms {
            view_proj,
            view: view_matrices,
            camera_pos,
            // y: the target encodes sRGB on write, which data outputs (the
            // normal material) undo so the stored byte is the value.
            // z, w: the tone mapping mode and its exposure's bits.
            view_info: [
                views.len() as u32,
                pixel_format::is_srgb(pass.color_format) as u32,
                self.tone_mapping.0.shader_mode() as u32,
                self.tone_mapping.1.to_bits(),
            ],
            viewport: [
                pass.width as f32,
                pass.height as f32,
                1.0 / pass.width.max(1) as f32,
                1.0 / pass.height.max(1) as f32,
            ],
            fog_color: [
                scene.fog.color.r,
                scene.fog.color.g,
                scene.fog.color.b,
                scene.fog.mode as f32,
            ],
            fog_params: [scene.fog.near, scene.fog.far, scene.fog.density, 0.0],
            ..Default::default()
        };
        frame_u.rect_counts[2] = self.reflective_transparency as u32;

        let mut items = Vec::new();
        let mut stats = MetalRenderStats::default();
        let (mut n_dir, mut n_point, mut n_spot, mut n_hemi) = (0usize, 0, 0, 0);
        // Every light that casts, each owning a layer of its kind's maps.
        let mut casters = Casters::new();
        // Union every view's layer mask so split-screen / multi-viewport passes
        // collect both sides; each pass filters in [`encode_pass`].
        let cam_layers = views.iter().fold(Layers::with_mask(0), |mut acc, v| {
            acc.mask |= v.layers.mask;
            acc
        });
        let cam_layers = if cam_layers.mask == 0 {
            primary.layers
        } else {
            cam_layers
        };

        scene.arena.traverse_visible(scene.root, &mut |_id, obj| {
            if !cam_layers.test(&obj.layers) {
                return;
            }
            let world = obj.matrix_world;
            let obj_layers = obj.layers;
            // The instance colours for the next item pushed (instanced meshes
            // only), taken by `push_skinned`.
            let pending_colors: std::cell::RefCell<Option<Vec<f32>>> =
                std::cell::RefCell::new(None);
            #[allow(clippy::too_many_arguments)]
            let mut push_skinned = |geometry: &Arc<BufferGeometry>,
                            material: &Arc<Material>,
                            primitive: NSUInteger,
                            instances: Option<Vec<f32>>,
                            bones: Option<Vec<f32>>,
                            sprite: Option<[f32; 4]>,
                            range: Option<(u32, u32)>,
                            influences: &[f32],
                            instance_morph: Option<(Vec<f32>, usize)>| {
                if !geometry.attributes.contains_key("position") {
                    stats.skipped += 1;
                    return;
                }
                // An empty span draws nothing, as three.js's renderBufferDirect.
                let Some(range) = range else {
                    return;
                };
                let center =
                    Vector3::new(world.elements[12], world.elements[13], world.elements[14]);
                // three.js sorts on `transparent` alone: an opaque
                // `depthTest: false` material draws in the opaque list, in
                // `render_order`, like any other.
                let transparent = is_transparent(material);
                // A transmissive physical material's glass mode.
                let glass_mode = match &**material {
                    Material::Physical(m) if m.transmission > 0.0 => Some(m.transparency),
                    _ => None,
                };
                items.push(DrawItem {
                    fur: match &**material {
                        Material::Shader(m) if crate::fur::is_fur(m) => {
                            let mut d = [0.0f32; 64];
                            for (slot, v) in m.data.iter().take(16).enumerate() {
                                d[slot * 4..slot * 4 + 4].copy_from_slice(v);
                            }
                            Some(d)
                        }
                        _ => None,
                    },
                    range,
                    morph_influences: influences.to_vec(),
                    wire_span: None,
                    bones,
                    bone_slot: None,
                    // three.js's shadow pass renders meshes, lines and points;
                    // a sprite casts nothing.
                    cast_shadow: obj.cast_shadow && sprite.is_none(),
                    sprite,
                    receive_shadow: obj.receive_shadow,
                    geometry: geometry.clone(),
                    material: material.clone(),
                    world,
                    layers: obj_layers,
                    primitive,
                    instance_count: instances.as_ref().map(|v| v.len() / 16).unwrap_or(1),
                    instances,
                    instance_slot: None,
                    instance_morph,
                    instance_morph_layout: None,
                    instance_colors: pending_colors.borrow_mut().take(),
                    instance_color_offset: 0,
                    view_depth: center.distance_to(eye),
                    render_order: obj.render_order,
                    // `Refract` draws as three.js's transmission here: the same
                    // volume model over the capture, which is what that mode
                    // refines on the wgpu path.
                    transparent: transparent && glass_mode != Some(crate::materials::TransparencyMode::Refract),
                    transmissive: material.transmissive()
                        || glass_mode == Some(crate::materials::TransparencyMode::Refract),
                    // `Glass` and `Oit`: glass blended over what is behind,
                    // tinted by channel.
                    blended_glass: matches!(
                        glass_mode,
                        Some(crate::materials::TransparencyMode::Glass | crate::materials::TransparencyMode::Oit)
                    ),
                    // Order-independent: drawn in the accumulation pass rather
                    // than sorted with the transparent draws (`encode_oit`).
                    oit: transparent
                        && matches!(&**material, Material::Physical(m) if m.transparency == crate::materials::TransparencyMode::Oit),
                    capture_skip: false,
                    side_override: None,
                    state: material.render_state(),
                });
            };
            // Everything that is not a skinned mesh, which is everything
            // else: same closure, no pose.
            // The whole draw range, one material: lines, points, instances.
            let mut push =
                |g: &Arc<BufferGeometry>,
                 m: &Arc<Material>,
                 p: NSUInteger,
                 inst: Option<Vec<f32>>| {
                    push_skinned(g, m, p, inst, None, None, g.draw_span(None), &[], None)
                };

            match &obj.kind {
                // One item per draw — per group with a material array — each
                // with its own material, transparency and sort, as three.js's
                // render list holds them.
                ObjectKind::Mesh(m) => {
                    // No positions: counted once, as any undrawable item is
                    // (its draw list is empty, so the loop would not see it).
                    if !m.geometry.attributes.contains_key("position") {
                        push_skinned(&m.geometry, &m.material, primitive::TRIANGLE, None, None, None, None, &[], None);
                        return;
                    }
                    for d in m.draws() {
                        push_skinned(
                            &m.geometry,
                            d.material,
                            primitive::TRIANGLE,
                            None,
                            None,
                            None,
                            Some((d.start, d.end)),
                            &m.morph_influences,
                            None,
                        );
                    }
                }
                ObjectKind::SkinnedMesh(sm) => {
                    // `bone_matrices` is world × inverse-bind per bone, already
                    // composed by `Skeleton::update`, so the vertex stage only
                    // has to blend them.
                    //
                    // Exactly `MAX_BONES` of them, padded with identities, as
                    // the wgpu backend uploads them: `vs_skinned` clamps joint
                    // indices to the last, so every index reads the matrix it
                    // reads there (and in the raycaster) — never past the end.
                    let identity = Matrix4::identity();
                    let bones: Vec<f32> = (0..crate::core::MAX_BONES)
                        .flat_map(|i| {
                            sm.skeleton
                                .bone_matrices
                                .get(i)
                                .unwrap_or(&identity)
                                .elements
                        })
                        .collect();
                    for d in sm.draws() {
                        push_skinned(
                            &sm.geometry,
                            d.material,
                            primitive::TRIANGLE,
                            None,
                            Some(bones.clone()),
                            None,
                            Some((d.start, d.end)),
                            &sm.morph_influences,
                            None,
                        );
                    }
                }
                ObjectKind::LineSegments(ls) => {
                    push(&ls.geometry, &ls.material, primitive::LINE, None)
                }
                ObjectKind::Points(p) => push(&p.geometry, &p.material, primitive::POINT, None),
                ObjectKind::Sprite(s) => {
                    if let Some(g) = sprite_geometry(s) {
                        let (rotation, size_attenuation) = match &*s.material {
                            Material::Sprite(m) => (m.rotation, m.size_attenuation),
                            _ => (0.0, true),
                        };
                        // The billboard faces view 0; a pass per view
                        // promotes its own view to slot 0, so each faces its
                        // own camera. Whether the size follows depth is read
                        // off the primary view's projection.
                        let by_depth = !size_attenuation && primary_perspective;
                        let params = [
                            s.center.x - 0.5,
                            s.center.y - 0.5,
                            rotation,
                            if by_depth { 1.0 } else { 0.0 },
                        ];
                        push_skinned(
                            &g,
                            &s.material,
                            primitive::TRIANGLE,
                            None,
                            None,
                            Some(params),
                            g.draw_span(None),
                            &[],
                            None,
                        );
                    }
                }
                ObjectKind::InstancedMesh(im) => {
                    if im.transforms.is_empty() {
                        return;
                    }
                    let mut data = Vec::with_capacity(im.transforms.len() * 16);
                    for m in &im.transforms {
                        data.extend_from_slice(&m.elements);
                    }
                    // Per-instance morph influences (`set_morph_at`), when the
                    // mesh has them for every instance.
                    let row_len = im.morph_row_len();
                    let rows = (!im.morph_texture.is_empty()
                        && im.morph_texture.len() == row_len * im.transforms.len())
                    .then(|| im.morph_texture.clone());
                    if !im.geometry.attributes.contains_key("position") {
                        push(&im.geometry, &im.material, primitive::TRIANGLE, Some(data));
                        return;
                    }
                    // three.js `instanceColor`: white past the end of `colors`.
                    let colors = (!im.colors.is_empty()).then(|| {
                        (0..im.transforms.len())
                            .flat_map(|i| {
                                let c = im.colors.get(i).copied().unwrap_or(crate::math::Color::WHITE);
                                [c.r, c.g, c.b, 1.0]
                            })
                            .collect::<Vec<f32>>()
                    });
                    // One instanced item per draw — per group with a material
                    // array — as three.js's render list holds them.
                    for d in im.draws() {
                        *pending_colors.borrow_mut() = colors.clone();
                        push_skinned(
                            &im.geometry,
                            d.material,
                            primitive::TRIANGLE,
                            Some(data.clone()),
                            None,
                            None,
                            Some((d.start, d.end)),
                            &im.morph_influences,
                            rows.clone().map(|r| (r, row_len)),
                        );
                    }
                }
                ObjectKind::Light(light) => match light {
                    Light::Ambient(l) => {
                        frame_u.ambient[0] += l.color.r * l.intensity;
                        frame_u.ambient[1] += l.color.g * l.intensity;
                        frame_u.ambient[2] += l.color.b * l.intensity;
                    }
                    Light::Directional(l) => {
                        if n_dir >= MAX_DIR_LIGHTS {
                            return;
                        }
                        // three.js: direction = normalize(target - position),
                        // default target the origin — matched to the wgpu path.
                        let pos = obj.world_position();
                        let d = Vector3::new(-pos.x, -pos.y, -pos.z);
                        let dir = if d.length_sq() > 1e-8 {
                            d.normalize()
                        } else {
                            transform_direction(&world, l.direction).normalize()
                        };
                        frame_u.dir[n_dir] = DirLightGpu {
                            direction: [dir.x, dir.y, dir.z, 0.0],
                            color: scaled(l.color, l.intensity),
                        };
                        // Every casting light gets a record and layers of
                        // its kind's map array, and names the record.
                        if l.cast_shadow && casters.len() < MAX_SHADOW_RECORDS {
                            casters.push(Caster::Dir { dir, settings: l.shadow });
                            frame_u.dir[n_dir].color[3] = casters.len() as f32;
                        }
                        n_dir += 1;
                    }
                    Light::Point(l) => {
                        if n_point >= MAX_POINT_LIGHTS {
                            return;
                        }
                        let p = obj.world_position();
                        frame_u.points[n_point] = PointLightGpu {
                            position: [p.x, p.y, p.z, 1.0],
                            color: scaled(l.color, l.intensity),
                            params: [l.distance, l.decay, 0.0, 0.0],
                        };
                        if l.cast_shadow && casters.len() < MAX_SHADOW_RECORDS {
                            // The cube's depth is normalised by the light's
                            // falloff `distance`, or — with none, as three.js's
                            // `PointLightShadow` does (`light.distance ||
                            // camera.far`) — by its shadow camera's far plane.
                            let radius = if l.distance > 0.0 { l.distance } else { l.shadow.camera_far };
                            casters.push(Caster::Point { pos: p, radius, settings: l.shadow });
                            frame_u.points[n_point].color[3] = casters.len() as f32;
                        }
                        n_point += 1;
                    }
                    Light::Spot(l) => {
                        if n_spot >= MAX_SPOT_LIGHTS {
                            return;
                        }
                        let p = obj.world_position();
                        let dir = transform_direction(&world, l.direction).normalize();
                        let casts = l.cast_shadow && casters.len() < MAX_SHADOW_RECORDS;
                        if casts {
                            casters.push(Caster::Spot { pos: p, dir, angle: l.angle, settings: l.shadow });
                        }
                        frame_u.spots[n_spot] = SpotLightGpu {
                            position: [p.x, p.y, p.z, 1.0],
                            direction: [dir.x, dir.y, dir.z, 0.0],
                            color: scaled(l.color, l.intensity),
                            params: [
                                l.distance,
                                l.decay,
                                l.angle.cos(),
                                (l.angle * (1.0 - l.penumbra)).cos(),
                            ],
                        };
                        if casts {
                            frame_u.spots[n_spot].color[3] = casters.len() as f32;
                        }
                        n_spot += 1;
                    }
                    Light::Hemisphere(l) => {
                        if n_hemi >= MAX_HEMI_LIGHTS {
                            return;
                        }
                        let up = transform_direction(&world, Vector3::UP).normalize();
                        frame_u.hemis[n_hemi] = HemiLightGpu {
                            sky: scaled(l.sky_color, l.intensity),
                            ground: scaled(l.ground_color, l.intensity),
                            up: [up.x, up.y, up.z, 0.0],
                        };
                        n_hemi += 1;
                    }
                    // three.js orients it by its world matrix's rotation
                    // alone (`extractRotation`): width along local +x,
                    // height along +y, emitting toward local −z.
                    Light::RectArea(l) => {
                        let n = frame_u.rect_counts[0] as usize;
                        if n >= MAX_RECT_LIGHTS {
                            return;
                        }
                        let p = obj.world_position();
                        let ax = transform_direction(&world, Vector3::new(1.0, 0.0, 0.0)).normalize();
                        let ay = transform_direction(&world, Vector3::new(0.0, 1.0, 0.0)).normalize();
                        let (hw, hh) = (l.width * 0.5, l.height * 0.5);
                        frame_u.rects[n] = RectLightGpu {
                            position: [p.x, p.y, p.z, 1.0],
                            color: scaled(l.color, l.intensity),
                            right: [ax.x * hw, ax.y * hw, ax.z * hw, 0.0],
                            up: [ay.x * hh, ay.y * hh, ay.z * hh, 0.0],
                        };
                        frame_u.rect_counts[0] += 1;
                    }
                },
                ObjectKind::Group => {}
            }
        });

        frame_u.counts = [n_dir as u32, n_point as u32, n_spot as u32, n_hemi as u32];
        stats.geometry_bytes = self.geometries.values().map(|g| g.bytes).sum();
        // Logged on meaningful change rather than per frame: on a scene that
        // grows as it animates this is the number that predicts a stall, and
        // it is invisible otherwise.
        let last = self
            .logged_geometry_bytes
            .load(std::sync::atomic::Ordering::Relaxed);
        if stats.geometry_bytes.abs_diff(last) > (last / 8).max(64 << 20) {
            self.logged_geometry_bytes
                .store(stats.geometry_bytes, std::sync::atomic::Ordering::Relaxed);
            log::info!(
                "geometry buffers: {:.2} GB across {} cached ({})",
                stats.geometry_bytes as f64 / (1u64 << 30) as f64,
                self.geometries.len(),
                if slim_lines_enabled() { "slim lines" } else { "full vertices" },
            );
        }
        (frame_u, items, stats, casters)
    }

    // --------------------------------------------------------------- uploads

    /// Ensure `geometry` has current GPU buffers. `true` if it uploaded.
    fn ensure_geometry(
        &mut self,
        geometry: &Arc<BufferGeometry>,
        slim: bool,
    ) -> Result<bool, MetalError> {
        let key = Arc::as_ptr(geometry) as usize;
        let frame = self.frame;
        if let Some(cached) = self.geometries.get_mut(&key) {
            if cached.version == geometry.geometry_version && cached.slim == slim {
                cached.last_used = frame;
                return Ok(false);
            }
        }
        // Built as one or the other, never both: the slim path also skips
        // `derive_normals`, whose scratch vector is the size of the geometry.
        let (vbuf, vertex_count) = if slim {
            let vertices = build_line_vertices(geometry);
            if vertices.is_empty() {
                return Ok(false);
            }
            let n = vertices.len();
            (self.device.new_buffer(bytemuck::cast_slice(&vertices))?, n)
        } else {
            let vertices = build_vertices(geometry);
            if vertices.is_empty() {
                return Ok(false);
            }
            let n = vertices.len();
            (self.device.new_buffer(bytemuck::cast_slice(&vertices))?, n)
        };
        // Skinning attributes, if this geometry has them. Named as three.js
        // names them.
        let skin = if !slim
            && geometry.attributes.contains_key("joint")
            && geometry.attributes.contains_key("weight")
        {
            let data = build_skin(geometry, vertex_count);
            Some(self.device.new_buffer(bytemuck::cast_slice(&data))?)
        } else {
            None
        };
        let (ibuf, index_count) = match &geometry.index {
            Some(idx) if !idx.is_empty() => (
                Some(self.device.new_buffer(bytemuck::cast_slice(idx))?),
                idx.len(),
            ),
            _ => (None, 0),
        };
        // Morph targets in three.js's morph-texture layout, built by the
        // same helper the wgpu backend uploads from.
        let morph = match (!slim)
            .then(|| crate::core::MorphTexels::build(geometry))
            .flatten()
            .filter(|t| t.targets > 0 && t.vertex_count as usize == vertex_count)
        {
            Some(t) => {
                let has_position = geometry
                    .morph_attributes
                    .get("position")
                    .is_some_and(|v| !v.is_empty());
                Some(MorphBuffer {
                    buffer: self.device.new_buffer(bytemuck::cast_slice(&t.data))?,
                    count: t.targets,
                    stride: t.stride,
                    // Normal and colour as the helper says; position only
                    // with position targets, as `Morph` blends it; colour
                    // alpha (all four channels) for a 4-component colour.
                    flags: (t.flags & 3)
                        | (has_position as u32) << 2
                        | (((t.flags & 4) != 0) as u32) << 3,
                    vertex_count: t.vertex_count,
                })
            }
            None => None,
        };
        let morph_bytes = morph
            .as_ref()
            .map(|m| (m.count * m.stride * m.vertex_count) as u64 * 16)
            .unwrap_or(0);
        let sets = ["uv1", "uv2", "uv3"].map(|n| geometry.get_attribute(n).filter(|a| a.item_size >= 2));
        let tangents = geometry.get_attribute("tangent").filter(|a| a.item_size >= 3);
        let has_sets = sets.iter().any(Option::is_some);
        let uv_sets = if !slim && (has_sets || tangents.is_some()) {
            let mut data = vec![0.0f32; vertex_count * 12];
            for (set, attr) in sets.iter().enumerate() {
                let Some(a) = attr else { continue };
                for v in 0..vertex_count.min(a.count()) {
                    let at = v * 12 + (set / 2) * 4 + (set % 2) * 2;
                    data[at] = a.array[v * a.item_size];
                    data[at + 1] = a.array[v * a.item_size + 1];
                }
            }
            if let Some(t) = tangents {
                for v in 0..vertex_count.min(t.count()) {
                    let at = v * 12 + 8;
                    data[at..at + 3].copy_from_slice(&t.array[v * t.item_size..v * t.item_size + 3]);
                    data[at + 3] = if t.item_size >= 4 { t.array[v * t.item_size + 3] } else { 1.0 };
                }
            }
            let flags = has_sets as u32 | (tangents.is_some() as u32) << 1;
            Some((self.device.new_buffer(bytemuck::cast_slice(&data))?, flags))
        } else {
            None
        };
        self.geometries.insert(
            key,
            CachedGeometry {
                morph,
                uv_sets,
                bytes: morph_bytes + (vertex_count * if slim {
                    std::mem::size_of::<LineVertex>()
                } else {
                    std::mem::size_of::<Vertex>()
                } + index_count * std::mem::size_of::<u32>()) as u64,
                sphere: geometry_sphere(geometry),
                _geometry: geometry.clone(),
                version: geometry.geometry_version,
                vertices: vbuf,
                skin,
                indices: ibuf,
                index_count,
                vertex_count,
                wire: None,
                slim,
                last_used: frame,
            },
        );
        Ok(true)
    }

    /// Ensure the cached `geometry` has its wireframe line-list indices.
    ///
    /// Call after [`ensure_geometry`](Self::ensure_geometry): a stale entry
    /// is rebuilt there without them, so they follow `geometry_version`.
    fn ensure_wire_indices(&mut self, geometry: &Arc<BufferGeometry>) -> Result<(), MetalError> {
        let key = Arc::as_ptr(geometry) as usize;
        let Some(cached) = self.geometries.get_mut(&key) else {
            return Ok(());
        };
        if cached.wire.is_some() || cached.slim {
            return Ok(());
        }
        let flat = geometry.wireframe_indices();
        if flat.is_empty() {
            return Ok(());
        }
        let buf = self.device.new_buffer(bytemuck::cast_slice(&flat))?;
        cached.bytes += (flat.len() * std::mem::size_of::<u32>()) as u64;
        cached.wire = Some((buf, flat.len()));
        Ok(())
    }

    /// Upload `texture`, as colour or as data.
    ///
    /// `linear` asks for the bytes as they are, with no sRGB decode on sample.
    /// Normal, metallic-roughness and occlusion maps are *quantities*, not
    /// colours — the glTF spec says so in §3.9.2 — and decoding them through a
    /// transfer curve corrupts them: the 0.5 that means "flat" in a normal map
    /// comes out as 0.214, a tangent-space x of −0.57, so every flat texel
    /// arrives steeply tilted. The wgpu loader learned this the hard way and
    /// keeps two views of each image; this keeps two uploads, because a Metal
    /// texture's pixel format is fixed when it is created.
    ///
    /// The two are cached separately, so an image used as both a colour map and
    /// a data map gets one of each rather than whichever role asked first.
    fn ensure_texture_as(
        &mut self,
        texture: &Arc<Texture>,
        linear: bool,
    ) -> Result<bool, MetalError> {
        let key = texture_key(texture, linear);
        let sampler_key = SamplerKey::from_texture(texture);
        if !self.samplers.contains_key(&sampler_key) {
            let sampler = new_sampler(&self.device, sampler_key)?;
            self.samplers.insert(sampler_key, sampler);
        }
        let frame = self.frame;
        if let Some(cached) = self.textures.get_mut(&key) {
            cached.last_used = frame;
            return Ok(false);
        }
        // An unsupported format is not fatal: the material falls back to its
        // flat colour, which is a great deal more useful than no frame at all.
        let Ok(gpu) = upload_texture_as(&self.device, texture, linear) else {
            return Ok(false);
        };
        self.textures.insert(
            key,
            CachedTexture {
                _texture: texture.clone(),
                texture: gpu,
                sampler_key,
                last_used: frame,
            },
        );
        Ok(true)
    }

    /// The `MTLDepthStencilState` for one compare function and write flag,
    /// made on first use.
    fn depth_state(&mut self, compare: NSUInteger, write: bool) -> Result<Id, MetalError> {
        if !self.depth_states.contains_key(&(compare, write)) {
            let state = depth_stencil_state(&self.device, compare, write)?;
            self.depth_states.insert((compare, write), state);
        }
        Ok(self.depth_states[&(compare, write)].id())
    }

    fn pipeline(&mut self, key: PipelineKey) -> Result<Id, MetalError> {
        if !self.pipelines.contains_key(&key) {
            let state = render_pipeline(&self.device, key)?;
            self.pipelines.insert(key, state);
        }
        Ok(self.pipelines[&key].id())
    }

    // -------------------------------------------------------------- encoding

    #[allow(clippy::too_many_arguments)]
    unsafe fn encode(
        &mut self,
        pass: &PassAttachments,
        views: &[RenderView],
        frame_u: &FrameUniforms,
        items: &[DrawItem],
        scene: &Scene,
        slot: usize,
        stats: &mut MetalRenderStats,
        vp: Option<(ClearMode, Option<PixelRect>)>,
    ) -> Result<(), MetalError> {
        let cmd = self.device.command_buffer();
        if cmd.is_null() {
            return Err(MetalError::NoCommandQueue);
        }

        // Order-independent transparency in passes of its own after each
        // view's — not in a layered (stereo) pass, where `Oit` draws sorted
        // with the other transparent surfaces, as `Glass` does. The depth it
        // tests against has to outlive the main pass for it.
        let layered_pass = vp.is_none() && layered(views, pass);
        self.oit_active = !layered_pass && !pass.depth.is_null() && items.iter().any(|i| i.oit && !i.capture_skip);
        let store_depth = self.store_depth;
        self.store_depth |= self.oit_active;
        let result = self.encode_views(cmd, pass, views, frame_u, items, scene, slot, stats, vp, layered_pass);
        self.store_depth = store_depth;
        self.oit_active = false;
        result?;

        if !pass.drawable.is_null() {
            let _: () = msg1(cmd, sel!("presentDrawable:"), pass.drawable);
        }
        let _: () = msg0(cmd, sel!("commit"));
        Ok(())
    }

    /// `encode`'s passes, one per view (or one layered pass for them all).
    #[allow(clippy::too_many_arguments)]
    unsafe fn encode_views(
        &mut self,
        cmd: Id,
        pass: &PassAttachments,
        views: &[RenderView],
        frame_u: &FrameUniforms,
        items: &[DrawItem],
        scene: &Scene,
        slot: usize,
        stats: &mut MetalRenderStats,
        vp: Option<(ClearMode, Option<PixelRect>)>,
        layered_pass: bool,
    ) -> Result<(), MetalError> {
        if layered_pass {
            let descriptor =
                self.render_pass_descriptor(pass, scene, &views[0], true, false, views.len() as u32);
            self.encode_pass(
                cmd,
                descriptor,
                pass,
                frame_u,
                views[0].viewport,
                None,
                items,
                slot,
                stats,
                views.len() as u32,
                None,
                None,
            )?;
        } else {
            // One pass per view. The clear happens the first time a given
            // (texture, slice) is targeted; a second view sharing it loads
            // instead, or it would erase the first.
            let mut cleared: Vec<(Id, u32)> = Vec::with_capacity(views.len());
            for (index, view) in views.iter().enumerate() {
                let target = (
                    if view.color.is_null() {
                        pass.color
                    } else {
                        view.color
                    },
                    view.slice,
                );
                let first = !cleared.contains(&target);
                cleared.push(target);
                // Under the viewport state the first touch clears only when
                // the clear is the whole target; a scissored clear loads and
                // fills its rectangle, and auto-clear off just loads.
                let (clear, fill) = match vp {
                    None => (first, None),
                    Some((ClearMode::Full, _)) => (first, None),
                    Some((ClearMode::Rect(r), _)) if first => (
                        false,
                        Some((r, clear_color(scene.background, scene.background_alpha))),
                    ),
                    Some(_) => (false, None),
                };
                // A later view loads this target, so a multisampled colour has
                // to survive the resolve — `MULTISAMPLE_RESOLVE` alone discards
                // it and the next pane would load garbage.
                let keep = views[index + 1..].iter().any(|v| {
                    (if v.color.is_null() { pass.color } else { v.color }, v.slice) == target
                });

                // The mono shaders read view 0, so the view being drawn moves
                // into that slot for this pass.
                let mut per_view = *frame_u;
                per_view.promote_view(index);
                // The view now reads as view 0; its capture slice is `index`.
                per_view.rect_counts[3] = index as u32;
                // Pixel-sized effects (wide lines) convert through the viewport
                // they are drawn in, not the whole attachment.
                if let Some(v) = view.viewport {
                    let (w, h) = (v.width.max(1.0) as f32, v.height.max(1.0) as f32);
                    per_view.viewport = [w, h, 1.0 / w, 1.0 / h];
                }
                let descriptor =
                    // Kept multisampled when the OIT composite loads it next.
                    self.render_pass_descriptor(pass, scene, view, clear, keep || self.oit_active, 1);
                self.encode_pass(
                    cmd,
                    descriptor,
                    pass,
                    &per_view,
                    view.viewport,
                    Some(view.layers),
                    items,
                    slot,
                    stats,
                    1,
                    fill,
                    vp.and_then(|(_, s)| s),
                )?;
                if self.oit_active {
                    self.encode_oit(cmd, pass, view, &per_view, items, slot, stats, keep)?;
                }
            }
        }
        Ok(())
    }

    /// An item's world bounding sphere, where its geometry's bounds bound
    /// what it draws: not instanced, skinned, morphed or a sprite (each
    /// draws somewhere its object-space bounds do not say).
    fn item_sphere(&self, item: &DrawItem) -> Option<crate::math::Sphere> {
        if item.instance_count > 1
            || item.instance_slot.is_some()
            || item.bone_slot.is_some()
            || item.sprite.is_some()
            || item.morph_influences.iter().any(|&w| w != 0.0)
        {
            return None;
        }
        let g = self.geometries.get(&(Arc::as_ptr(&item.geometry) as usize))?;
        Some(g.sphere?.apply_matrix4(&item.world))
    }

    /// The OIT surfaces of one view: accumulated against the depth its main
    /// pass left — weighted light added up, transmittance multiplied down —
    /// then composited over its colour, multiplied by the transmittance and
    /// plus the light. Into the same (multisampled) colour the main pass drew
    /// and resolved again, so a later view's resolve keeps the composite.
    #[allow(clippy::too_many_arguments)]
    unsafe fn encode_oit(
        &mut self,
        cmd: Id,
        pass: &PassAttachments,
        view: &RenderView,
        frame_u: &FrameUniforms,
        items: &[DrawItem],
        slot: usize,
        stats: &mut MetalRenderStats,
        keep_multisampled: bool,
    ) -> Result<(), MetalError> {
        if !items.iter().any(|i| i.oit && !i.capture_skip && view.layers.test(&i.layers)) {
            return Ok(());
        }
        let samples = pass.sample_count.max(1);
        let (w, h) = (pass.width.max(1), pass.height.max(1));
        if self.oit_targets.as_ref().map(|t| t.size) != Some((w, h, samples)) {
            let usage = texture_usage::RENDER_TARGET | texture_usage::SHADER_READ;
            let mk = |n| {
                crate::metal::resources::new_texture_2d(
                    &self.device,
                    w,
                    h,
                    pixel_format::RGBA16_FLOAT,
                    usage,
                    storage_mode::PRIVATE,
                    n,
                    1,
                )
            };
            let (accum, reveal) = (mk(1)?, mk(1)?);
            let (accum_ms, reveal_ms) = if samples > 1 { (Some(mk(samples)?), Some(mk(samples)?)) } else { (None, None) };
            self.oit_targets = Some(OitTargets { size: (w, h, samples), accum, reveal, accum_ms, reveal_ms });
        }
        let key = (pass.color_format, samples);
        if !self.oit_composite.contains_key(&key) {
            let tint = crate::metal::resources::oit_composite_pipeline(&self.device, pass.color_format, samples, false)?;
            let light = crate::metal::resources::oit_composite_pipeline(&self.device, pass.color_format, samples, true)?;
            self.oit_composite.insert(key, (tint, light));
        }
        let t = self.oit_targets.as_ref().expect("made above");
        let (accum, reveal) = (t.accum.id(), t.reveal.id());
        let (accum_ms, reveal_ms) = (t.accum_ms.as_ref().map(|t| t.id()), t.reveal_ms.as_ref().map(|t| t.id()));

        // 1) Accumulation, against the main pass's depth.
        let rpd: Id = msg0(class!("MTLRenderPassDescriptor"), sel!("renderPassDescriptor"));
        let attachments: Id = msg0(rpd, sel!("colorAttachments"));
        for (index, (single, multi, clear)) in
            [(accum, accum_ms, MTLClearColor { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }),
             (reveal, reveal_ms, MTLClearColor { red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0 })]
            .into_iter()
            .enumerate()
        {
            let a: Id = msg1(attachments, sel!("objectAtIndexedSubscript:"), index);
            let _: () = msg1(a, sel!("setLoadAction:"), load_action::CLEAR);
            let _: () = msg1(a, sel!("setClearColor:"), clear);
            match multi {
                Some(m) => {
                    let _: () = msg1(a, sel!("setTexture:"), m);
                    let _: () = msg1(a, sel!("setResolveTexture:"), single);
                    let _: () = msg1(a, sel!("setStoreAction:"), store_action::MULTISAMPLE_RESOLVE);
                }
                None => {
                    let _: () = msg1(a, sel!("setTexture:"), single);
                    let _: () = msg1(a, sel!("setStoreAction:"), store_action::STORE);
                }
            }
        }
        let depth_texture = if view.depth.is_null() { pass.depth } else { view.depth };
        let depth: Id = msg0(rpd, sel!("depthAttachment"));
        let _: () = msg1(depth, sel!("setTexture:"), depth_texture);
        let _: () = msg1(depth, sel!("setSlice:"), view.slice as NSUInteger);
        let _: () = msg1(depth, sel!("setLoadAction:"), load_action::LOAD);
        let _: () = msg1(depth, sel!("setStoreAction:"), store_action::DONT_CARE);
        self.oit_phase = 1;
        let accumulated = self.encode_pass(cmd, rpd, pass, frame_u, view.viewport, Some(view.layers), items, slot, stats, 1, None, None);
        self.oit_phase = 0;
        accumulated?;

        // 2) The composite, over the colour the main pass drew.
        let rpd: Id = msg0(class!("MTLRenderPassDescriptor"), sel!("renderPassDescriptor"));
        let attachments: Id = msg0(rpd, sel!("colorAttachments"));
        let color: Id = msg1(attachments, sel!("objectAtIndexedSubscript:"), 0usize);
        let color_texture = if view.color.is_null() { pass.color } else { view.color };
        let _: () = msg1(color, sel!("setTexture:"), color_texture);
        let _: () = msg1(color, sel!("setSlice:"), view.slice as NSUInteger);
        let _: () = msg1(color, sel!("setLoadAction:"), load_action::LOAD);
        if pass.resolve.is_null() {
            let _: () = msg1(color, sel!("setStoreAction:"), store_action::STORE);
        } else {
            let _: () = msg1(color, sel!("setResolveTexture:"), pass.resolve);
            let _: () = msg1(color, sel!("setResolveSlice:"), view.slice as NSUInteger);
            let _: () = msg1(
                color,
                sel!("setStoreAction:"),
                if keep_multisampled { store_action::STORE_AND_MULTISAMPLE_RESOLVE } else { store_action::MULTISAMPLE_RESOLVE },
            );
        }
        let encoder: Id = msg1(cmd, sel!("renderCommandEncoderWithDescriptor:"), rpd);
        if encoder.is_null() {
            return Err(MetalError::NoCommandQueue);
        }
        if let Some(v) = view.viewport {
            let _: () = msg1(encoder, sel!("setViewport:"), v);
        }
        let (tint, light) = &self.oit_composite[&key];
        let _: () = msg2(encoder, sel!("setFragmentTexture:atIndex:"), accum, 0usize);
        let _: () = msg2(encoder, sel!("setFragmentTexture:atIndex:"), reveal, 1usize);
        for pipe in [tint.id(), light.id()] {
            let _: () = msg1(encoder, sel!("setRenderPipelineState:"), pipe);
            let _: () = msg3(encoder, sel!("drawPrimitives:vertexStart:vertexCount:"), primitive::TRIANGLE, 0usize, 3usize);
        }
        let _: () = msg0(encoder, sel!("endEncoding"));
        Ok(())
    }

    /// Encode every item into one render pass.
    ///
    /// `views_per_draw` is 1 for an ordinary pass and the view count for a
    /// layered one, where the instance count is multiplied by it and the vertex
    /// stage splits the id back into (view, instance).
    #[allow(clippy::too_many_arguments)]
    unsafe fn encode_pass(
        &mut self,
        cmd: Id,
        descriptor: Id,
        pass: &PassAttachments,
        frame_u: &FrameUniforms,
        viewport: Option<MTLViewport>,
        view_layers: Option<Layers>,
        items: &[DrawItem],
        slot: usize,
        stats: &mut MetalRenderStats,
        views_per_draw: u32,
        fill: Option<(PixelRect, MTLClearColor)>,
        scissor_test: Option<PixelRect>,
    ) -> Result<(), MetalError> {
        // Built before the encoder exists: an error after it would have to
        // end the encoder first.
        let fill_pipeline = match fill {
            Some(_) => Some(self.viewport_state.fill_pipeline(
                pass.color_format,
                pass.depth_format,
                pass.sample_count.max(1),
            )?),
            None => None,
        };
        let encoder: Id = msg1(cmd, sel!("renderCommandEncoderWithDescriptor:"), descriptor);
        if encoder.is_null() {
            return Err(MetalError::Unsupported(
                "render command encoder could not be created for these attachments".into(),
            ));
        }

        let _: () = msg1(
            encoder,
            sel!("setFrontFacingWinding:"),
            winding::COUNTER_CLOCKWISE,
        );
        // The scissored clear, before anything draws: the background over the
        // scissor rectangle and nowhere else.
        if let (Some((rect, color)), Some(pipeline)) = (fill, fill_pipeline) {
            super::viewport::encode_fill(encoder, pipeline, rect, color);
        }
        // With the scissor test on, nothing lands outside its rectangle.
        let mut items = items;
        if let Some(viewport) = viewport {
            let _: () = msg1(encoder, sel!("setViewport:"), viewport);
            // Scissor to the viewport, clamped to the attachment (Metal
            // rejects a scissor outside it), so nothing a pane draws — a wide
            // line, a point sprite — can land in the pane next to it.
            use super::viewport::{intersect, scissor_for_viewport, scissor_rect};
            let rect = scissor_for_viewport(&viewport, pass.width, pass.height).and_then(|r| {
                match scissor_test {
                    Some(s) => intersect(r, scissor_rect(s)),
                    None => Some(r),
                }
            });
            match rect {
                Some(rect) => {
                    let _: () = msg1(encoder, sel!("setScissorRect:"), rect);
                }
                // Viewport and scissor miss each other: nothing to draw.
                None => items = &[],
            }
        }
        // The frame uniforms outgrow `setVertexBytes`'s 4 KB with the shadow
        // records, so they go in a buffer of this frame's, kept until the
        // frame in flight is done with it.
        let frame_bytes = std::slice::from_raw_parts(
            frame_u as *const FrameUniforms as *const u8,
            std::mem::size_of::<FrameUniforms>(),
        );
        let frame_buf = match self.device.new_buffer(frame_bytes) {
            Ok(b) => b,
            Err(e) => {
                let _: () = msg0(encoder, sel!("endEncoding"));
                return Err(e);
            }
        };
        let _: () = msg3(encoder, sel!("setVertexBuffer:offset:atIndex:"), frame_buf.id(), 0usize, VB_FRAME);
        let _: () = msg3(encoder, sel!("setFragmentBuffer:offset:atIndex:"), frame_buf.id(), 0usize, FB_FRAME);
        self.transients[slot].push(frame_buf);

        let mut last_pipeline = NIL;
        let mut last_cull = usize::MAX;
        let mut last_fill = usize::MAX;
        let mut last_depth = NIL;
        let mut last_bias = (0.0f32, 0.0f32);
        // A failure inside the loop still has to close the encoder: Metal
        // aborts the process if one is released without `endEncoding`.
        let mut failure = None;

        // Frustum culling, against this view's view-projection: an item whose
        // bounds are outside it is not encoded at all. Not in a layered pass,
        // where one draw serves both eyes and either could see it.
        let cull = (views_per_draw == 1)
            .then(|| crate::math::Frustum::from_projection_matrix(&Matrix4 { elements: frame_u.view_proj[0] }));
        for item in items {
            if let Some(f) = &cull {
                if self.item_sphere(item).is_some_and(|s| !f.intersects_sphere(&s)) {
                    stats.culled += 1;
                    continue;
                }
            }
            // With an OIT pass this frame, its items draw there and nowhere
            // else.
            if self.oit_active && item.oit != (self.oit_phase == 1) {
                continue;
            }
            if item.capture_skip {
                continue;
            }
            if let Some(layers) = view_layers {
                if !layers.test(&item.layers) {
                    continue;
                }
            }
            let key = Arc::as_ptr(&item.geometry) as usize;
            // Nothing uploaded for this geometry — an attribute set the vertex
            // builder could make nothing of. Counted, not drawn, not fatal.
            let Some(geom) = self.geometries.get(&key) else {
                stats.skipped += 1;
                continue;
            };
            // Everything needed from the cache entry, read out before anything
            // borrows `self` mutably again.
            let (vertices, indices, index_count, vertex_count, skin_buffer) = (
                geom.vertices.id(),
                geom.indices.as_ref().map(|b| b.id()),
                geom.index_count,
                geom.vertex_count,
                geom.skin.as_ref().map(|b| b.id()),
            );
            let morph_u = item_morph_uniforms(item, geom.morph.as_ref());
            let morph_buffer = geom
                .morph
                .as_ref()
                .filter(|_| morph_u.info[0] > 0)
                .map(|m| m.buffer.id())
                .unwrap_or(self.identity_instance.id());
            let uv_sets = geom.uv_sets.as_ref().map(|(b, f)| (b.id(), *f));
            // A wireframe mesh draws its line-list (polygon outlines, 1px
            // hardware lines, as the wgpu `TriangleWire` path does) rather
            // than its triangles in line fill mode, which would show every
            // fan diagonal. Fill-mode LINES stays only as the fallback for a
            // wireframe with no line list (nothing to outline). A partial
            // span draws the line list built for it.
            let wire = if item.primitive == primitive::TRIANGLE && item.material.wireframe() {
                match item.wire_span {
                    Some((i, n)) => self.transients[slot].get(i).map(|b| (b.id(), n)),
                    None if item.range == (0, item.geometry.draw_count() as u32) => {
                        geom.wire.as_ref().map(|(b, n)| (b.id(), *n))
                    }
                    None => None,
                }
            } else {
                None
            };
            let draw_primitive = if wire.is_some() {
                primitive::LINE
            } else {
                item.primitive
            };

            let material = &item.material;
            let point_sprites = item.primitive == primitive::POINT;
            let layered_draw = views_per_draw > 1;
            let wide = item.primitive == primitive::LINE
                && self.line_width > 0.0
                && !layered_draw
                && slim_lines_enabled();
            // Both halves have to be true: a mesh with a skeleton whose
            // geometry carries no weights has nothing to blend, and a geometry
            // with weights and no skeleton has nothing to blend *through*.
            // Read before `self.pipeline`, which borrows `self` mutably.
            let is_skinned = item.bone_slot.is_some() && skin_buffer.is_some() && !layered_draw;
            let fur_draw = item.fur.is_some() && !layered_draw && !point_sprites && item.primitive == primitive::TRIANGLE;
            let pipeline = match self.pipeline(PipelineKey {
                point_sprites,
                fur: fur_draw,
                wide_lines: wide,
                // Must agree with what `ensure_geometry` uploaded, or the
                // vertex shader reads the wrong stride and the draw is garbage.
                slim_lines: item.primitive == primitive::LINE
                    && !layered_draw
                    && slim_lines_enabled(),
                blend: item.transparent,
                layered: layered_draw,
                // Both halves have to be true: a mesh with a skeleton whose
                // geometry carries no weights has nothing to blend, and a
                // geometry with weights and no skeleton has nothing to blend
                // *through*.
                skinned: is_skinned,
                // Only the layered path needs this, and naming it there costs a
                // pipeline per topology — so leave it unspecified elsewhere.
                topology: if layered_draw {
                    topology_class::of(draw_primitive)
                } else {
                    topology_class::UNSPECIFIED
                },
                color_format: pass.color_format,
                depth_format: pass.depth_format,
                sample_count: pass.sample_count.max(1),
                color_write: item.state.color_write,
                oit_accum: self.oit_phase == 1,
            }) {
                Ok(pipeline) => pipeline,
                Err(e) => {
                    failure = Some(e);
                    break;
                }
            };
            if pipeline != last_pipeline {
                let _: () = msg1(encoder, sel!("setRenderPipelineState:"), pipeline);
                last_pipeline = pipeline;
            }

            // The material's `depthTest` / `depthWrite` / `depthFunc`. By
            // default transparent surfaces test depth but do not write it, so
            // two blended layers both survive rather than the nearer one
            // erasing the further one's contribution; with the test off
            // nothing is written, as in WebGL. A pass with no depth attachment
            // gets no depth state at all: Metal rejects one that would write.
            if !pass.depth.is_null() {
                let mut func = item.state.effective_func();
                if pass.reverse_z {
                    func = func.reversed();
                }
                // The accumulation reads the opaque depth and writes none.
                let write = item.state.effective_write(!item.transparent) && self.oit_phase == 0;
                let depth_state = match self.depth_state(metal_compare(func), write) {
                    Ok(s) => s,
                    Err(e) => {
                        failure = Some(e);
                        break;
                    }
                };
                if depth_state != last_depth {
                    let _: () = msg1(encoder, sel!("setDepthStencilState:"), depth_state);
                    last_depth = depth_state;
                }
                // three.js `polygonOffset`: Metal's depth bias is
                // `glPolygonOffset`'s formula — `slopeScale · m + bias · r` —
                // so factor and units carry over, negated under reverse-Z
                // where nearer is larger.
                let sign = if pass.reverse_z { -1.0 } else { 1.0 };
                let bias = material
                    .polygon_offset()
                    .map(|o| (o.units * sign, o.factor * sign))
                    .unwrap_or((0.0, 0.0));
                if bias != last_bias {
                    let _: () = msg3(
                        encoder,
                        sel!("setDepthBias:slopeScale:clamp:"),
                        bias.0,
                        bias.1,
                        0.0f32,
                    );
                    last_bias = bias;
                }
            }

            let cull = match item.side_override.unwrap_or(material.side()) {
                1 => cull::FRONT,
                2 => cull::NONE,
                _ => cull::BACK,
            };
            if cull != last_cull {
                let _: () = msg1(encoder, sel!("setCullMode:"), cull);
                last_cull = cull;
            }
            let fill = if material.wireframe() && wire.is_none() {
                fill_mode::LINES
            } else {
                fill_mode::FILL
            };
            if fill != last_fill {
                let _: () = msg1(encoder, sel!("setTriangleFillMode:"), fill);
                last_fill = fill;
            }

            let _: () = msg3(
                encoder,
                sel!("setVertexBuffer:offset:atIndex:"),
                vertices,
                0usize,
                VB_VERTICES,
            );

            // Instanced draws read their matrices from this frame's transient
            // buffer; everything else still binds the 1x1 identity, because the
            // shader declares the buffer and Metal requires bound what is
            // declared.
            let instanced = item
                .instance_slot
                .and_then(|i| self.transients[slot].get(i))
                .map(|b| b.id());
            let instance_buffer = instanced.unwrap_or(self.identity_instance.id());
            let _: () = msg3(
                encoder,
                sel!("setVertexBuffer:offset:atIndex:"),
                instance_buffer,
                0usize,
                VB_INSTANCES,
            );
            // Metal requires every resource a function declares to be bound,
            // and `vs_skinned` declares both — so bind them whenever they
            // exist, and fall back to the vertex buffer itself when they do
            // not. The non-skinned vertex functions never read either index.
            let skin_buffer = skin_buffer.unwrap_or(vertices);
            let _: () = msg3(
                encoder,
                sel!("setVertexBuffer:offset:atIndex:"),
                skin_buffer,
                0usize,
                VB_SKIN,
            );
            let bone_buffer = item
                .bone_slot
                .and_then(|i| self.transients[slot].get(i))
                .map(|b| b.id())
                .unwrap_or(self.identity_instance.id());
            let _: () = msg3(
                encoder,
                sel!("setVertexBuffer:offset:atIndex:"),
                bone_buffer,
                0usize,
                VB_BONES,
            );
            // Morph targets: the geometry's buffer, or a stand-in with count
            // 0 — the mesh functions declare both.
            let _: () = msg3(
                encoder,
                sel!("setVertexBuffer:offset:atIndex:"),
                morph_buffer,
                0usize,
                VB_MORPH_DATA,
            );
            set_bytes(encoder, true, &morph_u, VB_MORPH);
            // The extra uv sets, or a stand-in the shader never reads
            // (`phys_flags.z` is 0 without them).
            let _: () = msg3(
                encoder,
                sel!("setVertexBuffer:offset:atIndex:"),
                uv_sets.map_or(self.identity_instance.id(), |(b, _)| b),
                0usize,
                VB_UV_SETS,
            );
            if let (true, Some(fur)) = (fur_draw, &item.fur) {
                set_bytes(encoder, true, fur, VB_FUR);
                set_bytes(encoder, false, fur, FB_FUR);
            }

            let (texture, sampler) = self.material_texture(material);
            let _: () = msg2(
                encoder,
                sel!("setFragmentTexture:atIndex:"),
                texture,
                0usize,
            );
            // three.js gives every texture its own filter and wrap: each map
            // takes its sampler's place in the pool, the base map's first.
            let mut pool = SamplerPool::new(sampler);
            let (extra, present) = self.material_maps(material);
            for (i, slot) in extra.iter().enumerate() {
                let _: () = msg2(
                    encoder,
                    sel!("setFragmentTexture:atIndex:"),
                    slot.map_or(self.white.id(), |(t, _)| t),
                    i + 1,
                );
                // Emissive, normal, metallic-roughness, ambient occlusion.
                pool.set(1 + i, slot.map_or(sampler, |(_, s)| s));
            }
            // A Standard/Physical material's own `envMap` in place of the
            // scene's, with its own intensity and rotation.
            let own_env = material.pbr_env().and_then(|(c, intensity, rotation)| {
                let c = self.prefiltered_cached(c);
                self.material_envs
                    .get(&crate::renderer::gpu_texture::cube_cache_key(&c))
                    .map(|e| {
                        (e, intensity, crate::materials::environment_lookup_rows(rotation, e.atlas.is_none()))
                    })
            });
            let pbr_env = own_env.map(|e| e.0).or(self.env_cube.as_ref().map(|(_, e)| e));
            let env = pbr_env.map(|e| e.cube.id());
            // Its PMREM atlas, which `ibl` reads (texture 15, sampler 13).
            let _: () = msg2(
                encoder,
                sel!("setFragmentTexture:atIndex:"),
                pbr_env.and_then(|e| e.atlas.as_ref()).map_or(self.white.id(), |a| a.id()),
                15usize,
            );
            let _: () = msg2(encoder, sel!("setFragmentTexture:atIndex:"), self.ltc.id(), 28usize);
            // The transmission capture (three.js `transmissionSamplerMap`).
            let _: () = msg2(
                encoder,
                sel!("setFragmentTexture:atIndex:"),
                self.capture_bound.unwrap_or(self.capture_dummy.id()),
                29usize,
            );
            let _: () = msg2(encoder, sel!("setFragmentSamplerState:atIndex:"), self.transmission_sampler.id(), 14usize);
            let atlas_sampler = self
                .samplers
                .get(&SamplerKey::default_linear())
                .map_or(sampler, |s| s.id());
            let _: () = msg2(encoder, sel!("setFragmentSamplerState:atIndex:"), atlas_sampler, 13usize);
            let env_atlas_params = pbr_env.map_or([0.0; 4], |e| e.params);
            let _: () = msg2(
                encoder,
                sel!("setFragmentTexture:atIndex:"),
                env.unwrap_or(self.black_cube.id()),
                5usize,
            );
            // A mipmapped sampler for the cube: the environment is sampled by
            // roughness across its own chain, and a sampler that cannot see
            // past level 0 makes every surface a mirror.
            let env_sampler = self
                .samplers
                .get(&SamplerKey::environment())
                .map(|s| s.id())
                .unwrap_or(sampler);
            let _: () = msg2(
                encoder,
                sel!("setFragmentSamplerState:atIndex:"),
                env_sampler,
                10usize,
            );
            // The shadow map and its comparison sampler. Bound even when
            // nothing casts, because Metal requires every declared resource to
            // be bound and `shadow_factor` returns early on the flag anyway.
            let shadow_tex = self
                .shadow_map
                .as_ref()
                .map(|t| t.id())
                .unwrap_or(self.empty_shadow.id());
            let _: () = msg2(
                encoder,
                sel!("setFragmentTexture:atIndex:"),
                shadow_tex,
                6usize,
            );
            let shadow_samp = self
                .shadow_sampler
                .as_ref()
                .map(|s| s.id())
                .unwrap_or(env_sampler);
            let _: () = msg2(
                encoder,
                sel!("setFragmentSamplerState:atIndex:"),
                shadow_samp,
                11usize,
            );
            let spot_tex = self
                .spot_shadow_map
                .as_ref()
                .map(|t| t.id())
                .unwrap_or(self.empty_shadow.id());
            let _: () = msg2(encoder, sel!("setFragmentTexture:atIndex:"), spot_tex, 7usize);
            let point_tex = self
                .point_shadow_map
                .as_ref()
                .map(|t| t.id())
                .unwrap_or(self.empty_shadow.id());
            let _: () = msg2(encoder, sel!("setFragmentTexture:atIndex:"), point_tex, 8usize);
            // The variant-only maps: alpha, light, bump, specular at textures
            // 9–12, their samplers in the pool.
            let slots = material.texture_slots();
            // The light map is colour, the rest data.
            let extras = [
                (&slots.alpha_map, true),
                (&slots.light_map, false),
                (&slots.bump_map, true),
                (&slots.specular_map, true),
            ]
            .map(|(m, linear)| self.slot_texture(m.as_ref(), linear));
            for (i, slot) in extras.iter().enumerate() {
                let _: () = msg2(
                    encoder,
                    sel!("setFragmentTexture:atIndex:"),
                    slot.map_or(self.white.id(), |(t, _)| t),
                    9 + i,
                );
                pool.set(5 + i, slot.map_or(sampler, |(_, s)| s));
            }
            // MeshPhysicalMaterial's maps at textures 16–27.
            let mut phys_bits = 0u32;
            for (i, (map, linear)) in physical_maps(&slots).into_iter().enumerate() {
                let slot = self.slot_texture(map, linear);
                let _: () = msg2(
                    encoder,
                    sel!("setFragmentTexture:atIndex:"),
                    slot.map_or(self.white.id(), |(t, _)| t),
                    PHYS_TEXTURE0 + i,
                );
                if let Some((_, smp)) = slot {
                    pool.set(MAP_PHYS0 + i, smp);
                    phys_bits |= 1 << i;
                }
            }

            let mut draw_u = draw_uniforms(
                item,
                texture != self.white.id(),
                instanced.is_some(),
                &item.geometry,
            );
            draw_u.maps = present;
            draw_u.extra = extras.map(|e| e.is_some() as u32);
            // The material's own `envMap`, mipmapped like the scene's.
            let mat_env = slots
                .env_map
                .as_ref()
                .and_then(|c| self.material_envs.get(&crate::renderer::gpu_texture::cube_cache_key(c)))
                .map(|e| e.cube.id());
            draw_u.map_scalars[2] = mat_env.is_some() as u32 as f32;
            // A matcap's texture, sRGB like a colour map — or, on a toon
            // material, its `gradientMap`, decoded by its own colour space as
            // three.js does. No material has both.
            let matcap = self.slot_texture(slots.matcap_map.as_ref().or(slots.gradient_map.as_ref()), false);
            draw_u.map_scalars[3] = matcap.is_some() as u32 as f32;
            let _: () = msg2(
                encoder,
                sel!("setFragmentTexture:atIndex:"),
                matcap.map_or(self.white.id(), |(t, _)| t),
                14usize,
            );
            pool.set(9, matcap.map_or(sampler, |(_, s)| s));
            let _: () = msg2(
                encoder,
                sel!("setFragmentTexture:atIndex:"),
                mat_env.unwrap_or(self.black_cube.id()),
                13usize,
            );
            let mat_env_sampler = slots
                .env_map
                .as_ref()
                .and_then(|c| self.samplers.get(&SamplerKey::from_cube(c)))
                .map_or(env_sampler, |s| s.id());
            let _: () = msg2(encoder, sel!("setFragmentSamplerState:atIndex:"), mat_env_sampler, 12usize);
            for (i, smp) in pool.slots().into_iter().enumerate() {
                let _: () = msg2(encoder, sel!("setFragmentSamplerState:atIndex:"), smp, i);
            }
            draw_u.map_samplers = pool.packed();
            draw_u.phys_flags = [
                phys_bits,
                self.specular_antialiasing as u32,
                uv_sets.map_or(0, |(_, f)| f),
                item.blended_glass as u32,
            ];
            // three.js `envMapIntensity` with the material's own environment,
            // `scene.environmentIntensity` with the scene's.
            let (intensity, rows) = own_env.map_or(self.env_light, |e| (e.1, e.2));
            draw_u.map_params[2] = intensity;
            draw_u.map_params[3] = if env.is_some() { 1.0 } else { 0.0 };
            if matches!(material.kind(), MaterialKind::Standard | MaterialKind::Physical) {
                draw_u.env_dir = rows;
                draw_u.env_params = env_atlas_params;
            }
            if wide {
                // misc.y is the point size, which a line never uses — the quad
                // path reads its width in pixels from the same slot.
                draw_u.misc[1] = self.line_width;
            }
            set_bytes(encoder, true, &draw_u, VB_DRAW);
            set_bytes(encoder, false, &draw_u, FB_DRAW);

            // Without a matrix buffer there is exactly one instance to draw,
            // whatever the object claimed — the alternative is the shader
            // indexing past the identity matrix.
            let instances = if instanced.is_some() {
                item.instance_count.max(1)
            } else {
                1
            };
            // A layered pass draws every instance once per view; the vertex
            // stage divides the id back out.
            let instances = instances * views_per_draw as usize;
            // The item's span: three.js's draw range cut to its group. The
            // wireframe line list replaces the triangle indices for the draw
            // only (it was built for the span, or is the whole list for a
            // whole span); the triangle count below reports the span.
            let (start, end) = (item.range.0 as usize, item.range.1 as usize);
            let (draw_indices, draw_first, draw_index_count) = match wire {
                Some((buf, n)) => (Some(buf), 0usize, n),
                None => (indices, start, end.min(index_count).saturating_sub(start)),
            };
            match draw_indices {
                Some(ibuf) if index_count > 0 || wire.is_some() => {
                    if draw_index_count > 0 {
                        let _: () = msg6(
                            encoder,
                            sel!(
                                "drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount:"
                            ),
                            draw_primitive,
                            draw_index_count as NSUInteger,
                            index_type::UINT32,
                            ibuf,
                            draw_first * std::mem::size_of::<u32>(),
                            instances as NSUInteger,
                        );
                    }
                }
                _ => {
                    let end = end.min(vertex_count);
                    // Six vertices per segment instead of two, as triangles —
                    // the vertex buffer is unchanged, only how it is read.
                    let (prim, first, count) = if wide {
                        (primitive::TRIANGLE, (start / 2) * 6, (end.saturating_sub(start) / 2) * 6)
                    } else {
                        (item.primitive, start, end.saturating_sub(start))
                    };
                    if count > 0 {
                        let _: () = msg4(
                            encoder,
                            sel!("drawPrimitives:vertexStart:vertexCount:instanceCount:"),
                            prim,
                            first,
                            count as NSUInteger,
                            instances as NSUInteger,
                        );
                    }
                }
            }

            stats.draw_calls += 1;
            if item.primitive == primitive::TRIANGLE {
                let verts = end.min(if index_count > 0 { index_count } else { vertex_count })
                    .saturating_sub(start);
                stats.triangles += (verts / 3 * instances) as u32;
            }
        }

        let _: () = msg0(encoder, sel!("endEncoding"));
        match failure {
            Some(e) => Err(e),
            None => Ok(()),
        }
    }

    /// The texture + sampler to bind for `material`, falling back to the 1x1
    /// white texture so the shader can sample unconditionally.
    fn material_texture(&self, material: &Material) -> (Id, Id) {
        let slots = material.texture_slots();
        // A matcap's own texture has its own slot (`fs_mesh`'s `matcap_tex`).
        let map = slots.map.as_ref();
        if let Some(map) = map {
            if let Some(cached) = self.textures.get(&(Arc::as_ptr(map) as usize)) {
                if let Some(sampler) = self.samplers.get(&cached.sampler_key) {
                    return (cached.texture.id(), sampler.id());
                }
            }
        }
        // No colour map: the white stand-in, sampled with the first map
        // present's settings (`MaterialTextureSlots::sampler_source`), which
        // the other slots share.
        let from_maps = slots
            .sampler_source()
            // Cached under its role: sRGB for colour slots, linear for data.
            .and_then(|t| {
                self.textures
                    .get(&texture_key(t, false))
                    .or_else(|| self.textures.get(&texture_key(t, true)))
            })
            .and_then(|cached| self.samplers.get(&cached.sampler_key));
        let sampler = from_maps
            .or_else(|| self.samplers.get(&SamplerKey::default_linear()))
            .map(|s| s.id())
            .unwrap_or(NIL);
        (self.white.id(), sampler)
    }

    /// One uploaded texture and its sampler, or `None` if it has not been
    /// uploaded yet.
    ///
    /// Separate from [`material_texture`](Self::material_texture) because the
    /// extra slots have no fallback worth binding: a shader that is told the
    /// slot is empty never samples it, so binding white there would only hide
    /// an upload that did not happen.
    fn slot_texture(&self, map: Option<&Arc<Texture>>, linear: bool) -> Option<(Id, Id)> {
        let map = map?;
        let cached = self.textures.get(&texture_key(map, linear))?;
        let sampler = self.samplers.get(&cached.sampler_key)?;
        Some((cached.texture.id(), sampler.id()))
    }

    /// Draw every opaque triangle mesh into the shadow map, from the light.
    ///
    /// Transparent items are skipped: a depth buffer cannot represent partial
    /// occlusion, so a glass panel would cast the shadow of a solid one. That
    /// is three.js's behaviour too, and the alternative — a second pass with
    /// alpha testing — is a lot of machinery for a case that usually wants no
    /// shadow at all.
    unsafe fn encode_shadow(
        &mut self,
        pass_u: &ShadowPassGpu,
        items: &[DrawItem],
        slot: usize,
        map: Id,
        slice: usize,
        radial: bool,
        frustum: &crate::math::Frustum,
    ) -> Result<(), MetalError> {
        let _pool = AutoreleasePool::new();
        let chosen = if radial {
            self.point_shadow_pipeline.as_ref()
        } else {
            self.shadow_pipeline.as_ref()
        };
        let Some(pipeline) = chosen else {
            return Ok(());
        };
        let pipeline = pipeline.id();
        let cmd = unsafe { self.device.command_buffer() };
        if cmd.is_null() {
            return Err(MetalError::NoCommandQueue);
        }
        let desc: Id = unsafe { msg0(class!("MTLRenderPassDescriptor"), sel!("renderPassDescriptor")) };
        unsafe {
            let depth: Id = msg0(desc, sel!("depthAttachment"));
            let _: () = msg1(depth, sel!("setTexture:"), map);
            let _: () = msg1(depth, sel!("setLoadAction:"), load_action::CLEAR);
            let _: () = msg1(depth, sel!("setStoreAction:"), store_action::STORE);
            let _: () = msg1(depth, sel!("setClearDepth:"), 1.0f64);
            // Which cube face, for a point light. Zero for the flat maps.
            let _: () = msg1(depth, sel!("setSlice:"), slice as NSUInteger);
        }
        let encoder: Id = unsafe { msg1(cmd, sel!("renderCommandEncoderWithDescriptor:"), desc) };
        if encoder.is_null() {
            return Err(MetalError::NoCommandQueue);
        }
        let cut_pipeline = self.cut_shadow_pipeline.as_ref().map(|p| p.id());
        let mut current = pipeline;
        let mut last_cull = NSUInteger::MAX;
        unsafe {
            let _: () = msg1(encoder, sel!("setRenderPipelineState:"), pipeline);
            // Counter-clockwise front faces, as the colour pass draws them, so
            // a caster's `side` can choose which faces its shadow records.
            let _: () = msg1(encoder, sel!("setFrontFacingWinding:"), winding::COUNTER_CLOCKWISE);
            // Forward-Z `Less`, write on: what a shadow map wants (made in
            // `with_device`, so the lookup cannot miss).
            let _: () = msg1(
                encoder,
                sel!("setDepthStencilState:"),
                self.depth_states[&(compare::LESS, true)].id(),
            );
            set_bytes(encoder, true, pass_u, VB_FRAME);
            if radial {
                // `fs_shadow_point` reads the light's position and radius from
                // the same uniform, and the fragment stage has its own binding
                // table — binding it for the vertex stage alone leaves the
                // fragment reading zeros, which writes a depth of 0 everywhere
                // and shadows nothing.
                set_bytes(encoder, false, pass_u, VB_FRAME);
            }
        }

        for item in items {
            // Per-object, matching the wgpu path: `Object3D::cast_shadow` is
            // off by default and a caster has to say so. Shadowing everything
            // opaque instead looks right in a test scene and wrong in a real
            // one, where the floor casts onto itself.
            // A transparent caster casts too: three.js's shadow map ignores
            // blending (only `alphaTest` cuts a shadow).
            if !item.cast_shadow {
                continue;
            }
            if item.primitive != primitive::TRIANGLE {
                continue;
            }
            if self.item_sphere(item).is_some_and(|s| !frustum.intersects_sphere(&s)) {
                continue;
            }
            let key = Arc::as_ptr(&item.geometry) as usize;
            let Some(geom) = self.geometries.get(&key) else {
                continue;
            };
            let (vertices, indices, index_count, vertex_count, skin) = (
                geom.vertices.id(),
                geom.indices.as_ref().map(|b| b.id()),
                geom.index_count,
                geom.vertex_count,
                geom.skin.as_ref().map(|b| b.id()),
            );
            let instances = item
                .instance_slot
                .and_then(|i| self.transients[slot].get(i))
                .map(|b| b.id())
                .unwrap_or(self.identity_instance.id());
            let bones = item
                .bone_slot
                .and_then(|i| self.transients[slot].get(i))
                .map(|b| b.id())
                .unwrap_or(self.identity_instance.id());
            let morph_u = item_morph_uniforms(item, geom.morph.as_ref());
            let morph_buffer = geom
                .morph
                .as_ref()
                .filter(|_| morph_u.info[0] > 0)
                .map(|m| m.buffer.id())
                .unwrap_or(self.identity_instance.id());
            let (start, end) = (item.range.0 as usize, item.range.1 as usize);
            // The caster's map, for a cut-out test: `has_map` as the colour
            // pass computes it (anything but the white stand-in).
            let (map_texture, map_sampler) = self.material_texture(&item.material);
            let mut draw_u = draw_uniforms(
                item,
                map_texture != self.white.id(),
                item.instance_slot.is_some(),
                &item.geometry,
            );
            // `maps.w == 2` is how `vs_shadow` is told to skin; the fragment
            // stage never sees this uniform, so the slot is free.
            if item.bone_slot.is_some() && skin.is_some() {
                draw_u.maps[3] = 2;
            }
            // three.js's shadow depth material takes the caster's `map`,
            // `alphaMap` and `alphaTest`: a cut-out caster runs
            // `fs_shadow_cut` (the point pass's fragment stage tests it
            // already).
            let alpha = self.slot_texture(item.material.texture_slots().alpha_map.as_ref(), true);
            draw_u.extra[0] = alpha.is_some() as u32;
            let cut = item.material.alpha_test() > 0.0 && (draw_u.flags[1] == 1 || alpha.is_some());
            let want = match (radial, cut, cut_pipeline) {
                (false, true, Some(p)) => p,
                _ => pipeline,
            };
            // three.js's `shadowSide`: a front-sided caster records its back
            // faces, a back-sided one its front faces, a double-sided one both.
            let cull = shadow_cull(item.material.side(), radial);
            unsafe {
                if want != current {
                    let _: () = msg1(encoder, sel!("setRenderPipelineState:"), want);
                    current = want;
                }
                if cull != last_cull {
                    let _: () = msg1(encoder, sel!("setCullMode:"), cull);
                    last_cull = cull;
                }
                if cut || radial {
                    let _: () = msg2(encoder, sel!("setFragmentTexture:atIndex:"), map_texture, 0usize);
                    let _: () = msg2(encoder, sel!("setFragmentSamplerState:atIndex:"), map_sampler, 0usize);
                    let (alpha_tex, alpha_smp) = alpha.unwrap_or((self.white.id(), map_sampler));
                    let _: () = msg2(encoder, sel!("setFragmentTexture:atIndex:"), alpha_tex, 1usize);
                    let _: () = msg2(encoder, sel!("setFragmentSamplerState:atIndex:"), alpha_smp, 1usize);
                    set_bytes(encoder, false, &draw_u, VB_DRAW);
                }
                let _: () = msg3(encoder, sel!("setVertexBuffer:offset:atIndex:"), vertices, 0usize, 0usize);
                let _: () = msg3(encoder, sel!("setVertexBuffer:offset:atIndex:"), instances, 0usize, VB_INSTANCES);
                let _: () = msg3(encoder, sel!("setVertexBuffer:offset:atIndex:"), skin.unwrap_or(vertices), 0usize, VB_SKIN);
                let _: () = msg3(encoder, sel!("setVertexBuffer:offset:atIndex:"), bones, 0usize, VB_BONES);
                let _: () = msg3(encoder, sel!("setVertexBuffer:offset:atIndex:"), morph_buffer, 0usize, VB_MORPH_DATA);
                set_bytes(encoder, true, &morph_u, VB_MORPH);
                set_bytes(encoder, true, &draw_u, VB_DRAW);
                let n = item.instance_count.max(1) as NSUInteger;
                match indices {
                    Some(ibuf) if index_count > 0 => {
                        let count = end.min(index_count).saturating_sub(start);
                        if count > 0 {
                            let _: () = msg6(
                                encoder,
                                sel!(
                                    "drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount:"
                                ),
                                primitive::TRIANGLE,
                                count as NSUInteger,
                                index_type::UINT32,
                                ibuf,
                                start * std::mem::size_of::<u32>(),
                                n,
                            );
                        }
                    }
                    _ => {
                        let count = end.min(vertex_count).saturating_sub(start);
                        if count > 0 {
                            let _: () = msg4(
                                encoder,
                                sel!("drawPrimitives:vertexStart:vertexCount:instanceCount:"),
                                primitive::TRIANGLE,
                                start,
                                count as NSUInteger,
                                n,
                            );
                        }
                    }
                }
            }
        }
        unsafe {
            let _: () = msg0(encoder, sel!("endEncoding"));
            let _: () = msg0(cmd, sel!("commit"));
        }
        Ok(())
    }

    /// Size the shadow arrays for this frame's casters, with their sampler
    /// and pipelines: a layer per directional and spot light, six per point
    /// light, at the size the lights of each kind ask for
    /// (`ShadowSettings::map_size`, the largest), shrunk when several share
    /// a kind (`shadow_layer_size`). Reallocated only to grow or resize.
    ///
    /// `map_size` used to be a field the wgpu path read and discarded; the
    /// failure mode there is worse than not offering it, since the caller sets
    /// 4096, sees no error and gets whatever was allocated. It is read here.
    fn ensure_shadow_arrays(&mut self, casters: &Casters) -> Result<(), MetalError> {
        use crate::lights::shadow::shadow_layer_size;
        let count = |want: fn(&Caster) -> bool| casters.iter().filter(|c| want(c)).map(Caster::layers).sum::<usize>();
        let nd = count(|c| matches!(c, Caster::Dir { .. }));
        let ns = count(|c| matches!(c, Caster::Spot { .. }));
        let np = count(|c| matches!(c, Caster::Point { .. })) / 6;
        let max_size = |want: fn(&Caster) -> bool| {
            casters.iter().filter(|c| want(c)).map(|c| c.settings().map_size).max().unwrap_or(0)
        };
        if nd + ns + np == 0 {
            return Ok(());
        }
        let array = |device: &MetalDevice, size: u32, layers: usize| {
            crate::metal::resources::new_texture_array_levels(
                device,
                size,
                size,
                pixel_format::DEPTH32_FLOAT,
                texture_usage::RENDER_TARGET | texture_usage::SHADER_READ,
                layers as u32,
                1,
            )
        };
        if nd > 0 {
            let size = shadow_layer_size(max_size(|c| matches!(c, Caster::Dir { .. })), nd);
            if self.shadow_map.is_none() || self.shadow_size != size || self.shadow_layers[0] < nd {
                self.shadow_map = Some(array(&self.device, size, nd)?);
                self.shadow_size = size;
                self.shadow_layers[0] = nd;
            }
        }
        if ns > 0 {
            let size = shadow_layer_size(max_size(|c| matches!(c, Caster::Spot { .. })), ns);
            if self.spot_shadow_map.is_none() || self.spot_shadow_size != size || self.shadow_layers[1] < ns {
                self.spot_shadow_map = Some(array(&self.device, size, ns)?);
                self.spot_shadow_size = size;
                self.shadow_layers[1] = ns;
            }
        }
        if np > 0 {
            // 512 a face, matching the wgpu path. Six faces make a cube
            // expensive to grow — 1024 is four times the memory and four times
            // the fill — and a point light's shadow is usually small in frame.
            if self.point_shadow_map.is_none() || self.shadow_layers[2] < 6 * np {
                self.point_shadow_map = Some(array(&self.device, POINT_SHADOW_SIZE, 6 * np)?);
                self.shadow_layers[2] = 6 * np;
            }
            if self.point_shadow_pipeline.is_none() {
                self.point_shadow_pipeline =
                    Some(crate::metal::resources::point_shadow_pipeline(&self.device)?);
            }
        }
        self.ensure_shadow_support()
    }

    /// The pipeline and comparison sampler every shadow pass shares.
    fn ensure_shadow_support(&mut self) -> Result<(), MetalError> {
        if self.shadow_sampler.is_none() {
            self.shadow_sampler = Some(crate::metal::resources::shadow_sampler(&self.device)?);
        }
        if self.shadow_pipeline.is_none() {
            self.shadow_pipeline = Some(crate::metal::resources::shadow_pipeline(&self.device)?);
        }
        if self.cut_shadow_pipeline.is_none() {
            self.cut_shadow_pipeline =
                Some(crate::metal::resources::cut_shadow_pipeline(&self.device)?);
        }
        Ok(())
    }

    /// Upload `scene.environment` if it is not the one already held.
    ///
    /// `scene.environment_cube_rt` — a live cube render target, fed by a
    /// `CubeCamera` — is not handled: it lives in the wgpu renderer's own
    /// resource table and this backend has no view of it. A scene using one
    /// gets no environment here rather than a wrong one.
    fn ensure_environment(&mut self, scene: &Scene) {
        // Prefiltered, as three.js's `WebGLCubeUVMaps` does for every
        // Standard/Physical environment.
        let cube = scene.environment.as_ref().map(|c| self.prefiltered(c));
        // three.js `scene.environmentIntensity` and `environmentRotation`.
        self.env_light = (
            scene.environment_intensity,
            crate::materials::environment_lookup_rows(
                scene.environment_rotation,
                cube.as_ref().is_some_and(|c| c.cube_uv_atlas.is_none()),
            ),
        );
        let Some(cube) = cube.as_ref() else {
            self.env_cube = None;
            return;
        };
        let key = crate::renderer::gpu_texture::cube_cache_key(cube);
        if matches!(&self.env_cube, Some((k, _)) if *k == key) {
            return;
        }
        let sk = SamplerKey::environment();
        if !self.samplers.contains_key(&sk) {
            if let Ok(smp) = new_sampler(&self.device, sk) {
                self.samplers.insert(sk, smp);
            }
        }
        let sk = SamplerKey::default_linear();
        if !self.samplers.contains_key(&sk) {
            if let Ok(smp) = new_sampler(&self.device, sk) {
                self.samplers.insert(sk, smp);
            }
        }
        match EnvGpu::upload(&self.device, cube) {
            Ok(env) => self.env_cube = Some((key, env)),
            Err(e) => {
                // A scene that cannot have its environment uploaded is better
                // rendered without one than not at all, but silently is how
                // this backend earned a reputation for being broken.
                eprintln!("threers: metal environment upload failed: {e:?}");
                self.env_cube = None;
            }
        }
    }

    /// three.js `WebGLCubeUVMaps`: a cube texture lighting a Standard or
    /// Physical material is read through its PMREM — prefiltered once and
    /// kept — unless it already carries one.
    fn prefiltered(&mut self, cube: &Arc<crate::textures::CubeTexture>) -> Arc<crate::textures::CubeTexture> {
        if cube.cube_uv_atlas.is_some() {
            return cube.clone();
        }
        self.pmrem_cache
            .entry(crate::renderer::gpu_texture::cube_cache_key(cube))
            .or_insert_with(|| crate::PmremGenerator::from_cube(cube.clone()))
            .clone()
    }

    /// [`prefiltered`](Self::prefiltered) once it has run for `cube`.
    fn prefiltered_cached(&self, cube: &Arc<crate::textures::CubeTexture>) -> Arc<crate::textures::CubeTexture> {
        self.pmrem_cache
            .get(&crate::renderer::gpu_texture::cube_cache_key(cube))
            .cloned()
            .unwrap_or_else(|| cube.clone())
    }

    /// The extra material maps, in the order the shader binds them:
    /// emissive, normal, metallic-roughness, ambient occlusion.
    ///
    /// glTF packs roughness and metalness into one image — roughness in green,
    /// metalness in blue — and `threers` keeps them as separate slots because
    /// three.js does. When both point at the same texture, which is what a glTF
    /// load produces, one binding carries both. When they differ, roughness
    /// wins the slot and metalness falls back to its scalar, because there is
    /// no third channel to put it in and the alternative is a sixth binding for
    /// a case no real asset produces.
    ///
    /// Each with its own sampler, from the texture's own filter and wrap.
    fn material_maps(&self, material: &Material) -> ([Option<(Id, Id)>; 4], [u32; 4]) {
        let slots = material.texture_slots();
        let mr = slots.roughness_map.as_ref().or(slots.metalness_map.as_ref());
        let ids = [
            self.slot_texture(slots.emissive_map.as_ref(), false),
            self.slot_texture(slots.normal_map.as_ref(), true),
            self.slot_texture(mr, true),
            self.slot_texture(slots.ao_map.as_ref(), true),
        ];
        let present = std::array::from_fn(|i| ids[i].is_some() as u32);
        (ids, present)
    }

    /// Build the `MTLRenderPassDescriptor` for one view. Autoreleased.
    ///
    /// `clear` is false for a second view sharing a target with the first —
    /// side-by-side eyes in one texture, where clearing again would erase the
    /// eye already drawn.
    unsafe fn render_pass_descriptor(
        &self,
        pass: &PassAttachments,
        scene: &Scene,
        view: &RenderView,
        clear: bool,
        keep_multisampled: bool,
        array_length: u32,
    ) -> Id {
        let rpd: Id = msg0(
            class!("MTLRenderPassDescriptor"),
            sel!("renderPassDescriptor"),
        );
        // A layered pass renders into every slice at once, so it names how many
        // there are and starts at slice 0 — the shader's
        // `render_target_array_index` picks between them. Without this the
        // attachment is one slice deep and both eyes land on top of each other.
        if array_length > 1 {
            let _: () = msg1(
                rpd,
                sel!("setRenderTargetArrayLength:"),
                array_length as NSUInteger,
            );
        }
        let slice = if array_length > 1 { 0 } else { view.slice };
        let color_attachments: Id = msg0(rpd, sel!("colorAttachments"));
        let color: Id = msg1(color_attachments, sel!("objectAtIndexedSubscript:"), 0usize);
        let color_texture = if view.color.is_null() {
            pass.color
        } else {
            view.color
        };
        let _: () = msg1(color, sel!("setTexture:"), color_texture);
        let _: () = msg1(color, sel!("setSlice:"), slice as NSUInteger);
        let _: () = msg1(
            color,
            sel!("setLoadAction:"),
            if clear {
                load_action::CLEAR
            } else {
                load_action::LOAD
            },
        );
        let _: () = msg1(
            color,
            sel!("setClearColor:"),
            clear_color(scene.background, scene.background_alpha),
        );
        if pass.resolve.is_null() {
            let _: () = msg1(color, sel!("setStoreAction:"), store_action::STORE);
        } else {
            let _: () = msg1(color, sel!("setResolveTexture:"), pass.resolve);
            let _: () = msg1(color, sel!("setResolveSlice:"), slice as NSUInteger);
            let _: () = msg1(
                color,
                sel!("setStoreAction:"),
                if keep_multisampled {
                    store_action::STORE_AND_MULTISAMPLE_RESOLVE
                } else {
                    store_action::MULTISAMPLE_RESOLVE
                },
            );
        }

        let depth_texture = if view.depth.is_null() {
            pass.depth
        } else {
            view.depth
        };
        if !depth_texture.is_null() {
            let depth: Id = msg0(rpd, sel!("depthAttachment"));
            let _: () = msg1(depth, sel!("setTexture:"), depth_texture);
            let _: () = msg1(depth, sel!("setSlice:"), slice as NSUInteger);
            // Always cleared, even for a view that loads its colour: depth is
            // usually not stored (below), so loading it reads undefined values
            // across the second view's whole viewport. Views sharing a target
            // do not overlap — or, if they do, the later one is an overlay —
            // so nothing needs the earlier view's depth.
            let _: () = msg1(depth, sel!("setLoadAction:"), load_action::CLEAR);
            // Normally discarded: depth is scratch for the pass that draws it,
            // and storing it costs bandwidth nothing reads. SSAO does read it
            // afterwards, and a discarded depth texture samples as the clear
            // value — which sends every pixel down the far-plane early-out and
            // produces occlusion that is uniformly, silently absent.
            // Under MSAA, stored depth is also resolved where the target
            // asks (`PassAttachments::with_depth_resolve`) — the multisampled
            // one kept as well while the OIT pass still has to test against
            // it.
            let resolve_depth =
                self.store_depth && view.depth.is_null() && !pass.depth_resolve.is_null() && pass.sample_count > 1;
            if resolve_depth {
                let _: () = msg1(depth, sel!("setResolveTexture:"), pass.depth_resolve);
                let _: () = msg1(depth, sel!("setResolveSlice:"), slice as NSUInteger);
            }
            let _: () = msg1(
                depth,
                sel!("setStoreAction:"),
                match (self.store_depth, resolve_depth, self.oit_active) {
                    (false, ..) => store_action::DONT_CARE,
                    (true, false, _) => store_action::STORE,
                    (true, true, false) => store_action::MULTISAMPLE_RESOLVE,
                    (true, true, true) => store_action::STORE_AND_MULTISAMPLE_RESOLVE,
                },
            );
            // Reverse-Z clears to the far plane, which is 0.
            let _: () = msg1(
                depth,
                sel!("setClearDepth:"),
                if pass.reverse_z { 0.0f64 } else { 1.0f64 },
            );
        }
        rpd
    }
}

/// The clear colour for `color` — every colour clear goes through here: the
/// pass's own (`loadAction` clear), the scissored fill a partial clear draws,
/// and [`MetalRenderer::clear`]. [`Color`](crate::math::Color) is linear, and
/// so is `MTLClearColor`; an sRGB attachment encodes on write, for the clear
/// and for the fill's fragment output alike.
pub(crate) fn clear_color(color: crate::math::Color, alpha: f32) -> MTLClearColor {
    MTLClearColor {
        red: color.r as f64,
        green: color.g as f64,
        blue: color.b as f64,
        alpha: alpha as f64,
    }
}

/// Whether these views can share one layered pass: more than one of them, all
/// into the pass's own texture, same viewport, distinct array slices, and no
/// MSAA resolve (a pass resolves one slice, so two eyes need two passes).
fn layered(views: &[RenderView], pass: &PassAttachments) -> bool {
    if views.len() < 2 || !pass.resolve.is_null() {
        return false;
    }
    let first = &views[0];
    let same_viewport = |a: &Option<MTLViewport>, b: &Option<MTLViewport>| match (a, b) {
        (None, None) => true,
        (Some(a), Some(b)) => {
            a.origin_x == b.origin_x
                && a.origin_y == b.origin_y
                && a.width == b.width
                && a.height == b.height
        }
        _ => false,
    };
    views.iter().all(|v| {
        v.color.is_null() && v.depth.is_null() && same_viewport(&v.viewport, &first.viewport)
    }) && views
        .iter()
        .enumerate()
        .all(|(i, v)| views[..i].iter().all(|w| w.slice != v.slice))
}

// ------------------------------------------------------------------- helpers

/// `setVertexBytes:length:atIndex:` / `setFragmentBytes:length:atIndex:`.
///
/// Metal copies the bytes into the command buffer, so `T` needs to live only
/// for the call. Capped at 4 KB by the API — both uniform structs are far under.
unsafe fn set_bytes<T>(encoder: Id, vertex_stage: bool, value: &T, index: NSUInteger) {
    debug_assert!(std::mem::size_of::<T>() <= 4096);
    let sel = if vertex_stage {
        sel!("setVertexBytes:length:atIndex:")
    } else {
        sel!("setFragmentBytes:length:atIndex:")
    };
    let _: () = msg3(
        encoder,
        sel,
        value as *const T as *const c_void,
        std::mem::size_of::<T>() as NSUInteger,
        index,
    );
}

fn scaled(color: crate::math::Color, intensity: f32) -> [f32; 4] {
    [
        color.r * intensity,
        color.g * intensity,
        color.b * intensity,
        0.0,
    ]
}

/// three.js's `depthFunc` as an `MTLCompareFunction` (already flipped for a
/// reverse-Z pass by the caller).
fn metal_compare(func: crate::materials::DepthFunc) -> NSUInteger {
    use crate::materials::DepthFunc;
    match func {
        DepthFunc::Never => compare::NEVER,
        DepthFunc::Always => compare::ALWAYS,
        DepthFunc::Less => compare::LESS,
        DepthFunc::LessEqual => compare::LESS_EQUAL,
        DepthFunc::Equal => compare::EQUAL,
        DepthFunc::GreaterEqual => compare::GREATER_EQUAL,
        DepthFunc::Greater => compare::GREATER,
        DepthFunc::NotEqual => compare::NOT_EQUAL,
    }
}

/// The cull mode for a caster of three.js `side` in a shadow pass. three.js's
/// `shadowSide` records a front-sided caster's back faces, a back-sided one's
/// front faces and a double-sided one's both. The point pass's cube faces
/// mirror one axis, which reverses winding, so the faces to cull swap there.
fn shadow_cull(side: u32, radial: bool) -> NSUInteger {
    let (front, back) = if radial { (cull::BACK, cull::FRONT) } else { (cull::FRONT, cull::BACK) };
    match side {
        1 => back,
        2 => cull::NONE,
        _ => front,
    }
}

/// A material draws blended when it is transparent by three.js's rule
/// ([`Material::transparent`]): its `transparent` flag, or — for a material
/// that never set one — opacity below 1.
fn is_transparent(material: &Material) -> bool {
    material.transparent()
}

/// Rotate + scale a direction by a world matrix, ignoring translation.
fn transform_direction(m: &Matrix4, v: Vector3) -> Vector3 {
    let e = &m.elements;
    Vector3::new(
        e[0] * v.x + e[4] * v.y + e[8] * v.z,
        e[1] * v.x + e[5] * v.y + e[9] * v.z,
        e[2] * v.x + e[6] * v.y + e[10] * v.z,
    )
}

/// The inverse transpose of `m`, for transforming normals under non-uniform
/// scale. Returned as a 4x4 so the shader can read its columns directly.
pub(crate) fn normal_matrix(m: &Matrix4) -> [f32; 16] {
    let inv = m.invert();
    let e = inv.elements;
    let mut t = [0.0f32; 16];
    for col in 0..4 {
        for row in 0..4 {
            t[col * 4 + row] = e[row * 4 + col];
        }
    }
    t
}

/// A geometry's bounding sphere: its own when it has computed one, else
/// from its positions (around their box's centre, as three.js's).
fn geometry_sphere(g: &BufferGeometry) -> Option<crate::math::Sphere> {
    if let Some(s) = g.bounding_sphere {
        return Some(s);
    }
    let (mut lo, mut hi) = (Vector3::new(f32::MAX, f32::MAX, f32::MAX), Vector3::new(f32::MIN, f32::MIN, f32::MIN));
    let mut any = false;
    for p in g.positions()? {
        lo = Vector3::new(lo.x.min(p.x), lo.y.min(p.y), lo.z.min(p.z));
        hi = Vector3::new(hi.x.max(p.x), hi.y.max(p.y), hi.z.max(p.z));
        any = true;
    }
    if !any {
        return None;
    }
    let centre = (lo + hi) * 0.5;
    let r = g.positions()?.map(|p| (p - centre).length_sq()).fold(0.0f32, f32::max).sqrt();
    Some(crate::math::Sphere::new(centre, r))
}

/// What a frame's shadow maps show, hashed: the passes' matrices and every
/// caster's geometry, transform, side, cut-out, pose, instances and morph —
/// all `encode_shadow` reads. Equal from one render to the next, the maps are
/// reused (`ShadowMap`), as the wgpu path does. A cheap word mix (FxHash's):
/// the inputs are trusted, and this runs over every caster each frame.
fn shadow_fingerprint(items: &[DrawItem]) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    let mut mix = |x: u64| h = (h.rotate_left(5) ^ x).wrapping_mul(0x517c_c1b7_2722_0a95);
    let floats = |mix: &mut dyn FnMut(u64), v: &[f32]| {
        mix(v.len() as u64);
        v.iter().for_each(|x| mix(x.to_bits() as u64));
    };
    for item in items.iter().filter(|i| i.cast_shadow && i.primitive == primitive::TRIANGLE) {
        mix(Arc::as_ptr(&item.geometry) as usize as u64);
        mix(item.geometry.geometry_version as u64);
        item.world.elements.iter().for_each(|v| mix(v.to_bits() as u64));
        let m = &item.material;
        mix(m.side() as u64);
        mix(m.alpha_test().to_bits() as u64);
        let slots = m.texture_slots();
        mix(slots.map.as_ref().map_or(0, |t| Arc::as_ptr(t) as usize as u64));
        mix(slots.alpha_map.as_ref().map_or(0, |t| Arc::as_ptr(t) as usize as u64));
        mix(((item.range.0 as u64) << 32) | item.range.1 as u64);
        mix(item.instance_count as u64);
        floats(&mut mix, item.instances.as_deref().unwrap_or(&[]));
        floats(&mut mix, item.instance_morph.as_ref().map_or(&[][..], |(rows, _)| rows));
        floats(&mut mix, item.bones.as_deref().unwrap_or(&[]));
        floats(&mut mix, &item.morph_influences);
    }
    h
}

/// Per-draw uniforms for one item.
/// Negate clip-space x.
///
/// Cube maps are defined left-handed in every graphics API, and
/// [`Matrix4::look_at`] is right-handed — so rendering a face with it lays the
/// texels down mirrored against what the sampler will later expect. Each face
/// still looks perfectly reasonable on its own, which is what makes this hard
/// to see: the shadow simply lands on the wrong side.
///
/// *Which* axis is not a guess. `tests/shadow_fixtures.rs` measures how far the
/// shadow lands from where the geometry puts it, in pixels:
///
/// ```text
///            none      x       y      xy
///   wgpu     79.2   133.1     2.1   107.0
///   metal   107.0     2.1   133.1    79.2
/// ```
///
/// This path wants x and the wgpu one wants y, and the two tables are each
/// other's shifted by one flip — their framebuffer-y conventions are opposite,
/// so the correction is too. Anything but the 2.1 is a shadow of something
/// that is not there.
fn mirror_clip_x(m: Matrix4) -> Matrix4 {
    let mut e = m.elements;
    // Column-major: row 0 is elements 0, 4, 8, 12.
    e[0] = -e[0];
    e[4] = -e[4];
    e[8] = -e[8];
    e[12] = -e[12];
    Matrix4 { elements: e }
}

/// Cache key for an uploaded texture: the image, and which transfer curve it
/// was uploaded under. See [`MetalRenderer::ensure_texture_as`].
/// Per-vertex joint indices and weights, as the shader wants them.
///
/// Both attributes are four floats a vertex in `BufferGeometry` — three.js
/// stores joint *indices* as floats too — so this is a repack rather than a
/// conversion. Vertices past the end of either attribute are bound rigidly to
/// bone 0, which is what an unweighted vertex means.
fn build_skin(geometry: &BufferGeometry, vertex_count: usize) -> Vec<SkinVertex> {
    use crate::core::BufferAttribute;
    let joints = geometry.get_attribute("joint");
    let weights = geometry.get_attribute("weight");
    (0..vertex_count)
        .map(|i| {
            let take = |a: Option<&BufferAttribute>, fallback: [f32; 4]| -> [f32; 4] {
                match a {
                    Some(a) if (i + 1) * 4 <= a.array.len() => {
                        [a.array[i * 4], a.array[i * 4 + 1], a.array[i * 4 + 2], a.array[i * 4 + 3]]
                    }
                    _ => fallback,
                }
            };
            SkinVertex {
                joints: take(joints, [0.0; 4]),
                weights: take(weights, [1.0, 0.0, 0.0, 0.0]),
            }
        })
        .collect()
}

/// The `MorphInfo` uniform for drawing `geometry` (whose GPU morph buffer is
/// `morph`) under `influences`: count, base influence and weights from
/// [`crate::core::morph_uniforms`] — the numbers the wgpu backend uploads —
/// with this shader's flags (1 normal, 2 colour, 4 position). Count 0, no
/// blending, when nothing blends.
fn morph_uniforms(
    geometry: &BufferGeometry,
    morph: Option<&MorphBuffer>,
    influences: &[f32],
) -> MorphUniforms {
    let (Some(m), Some((header, weights))) =
        (morph, crate::core::morph_uniforms(geometry, influences))
    else {
        return MorphUniforms::default();
    };
    let mut u = MorphUniforms {
        info: [header[0] as u32, m.stride, m.flags, m.vertex_count],
        base: [header[1], 0.0, 0.0, 0.0],
        weights: [[0.0; 4]; 16],
    };
    for (i, w) in weights.iter().enumerate() {
        u.weights[i / 4][i % 4] = *w;
    }
    u
}

/// [`morph_uniforms`] for a draw item: an `InstancedMesh` with per-instance
/// rows reads its influences from them (flag 16; `base.y` the row length,
/// `base.z` where the rows start in the instance buffer, in floats).
fn item_morph_uniforms(item: &DrawItem, morph: Option<&MorphBuffer>) -> MorphUniforms {
    let Some((row_len, offset)) = item.instance_morph_layout else {
        return morph_uniforms(&item.geometry, morph, &item.morph_influences);
    };
    let (Some(m), Some(header)) = (morph, crate::core::morph_header(&item.geometry)) else {
        return MorphUniforms::default();
    };
    MorphUniforms {
        info: [header[0] as u32, m.stride, m.flags | 16, m.vertex_count],
        base: [0.0, row_len as f32, offset as f32, 0.0],
        weights: [[0.0; 4]; 16],
    }
}

fn texture_key(texture: &Arc<Texture>, linear: bool) -> usize {
    // The pointer is 16-byte aligned, so the low bit is free to carry the role.
    (Arc::as_ptr(texture) as usize) | linear as usize
}

/// The distinct samplers one draw's maps use, bound at slots 0 to
/// `SAMPLER_POOL − 1`, and each map's index among them. The base map's is
/// always slot 0, which the point and cut-out shaders read directly. A draw
/// whose maps want more distinct filter-and-wrap states than there are slots
/// reads the extra ones through the base map's.
struct SamplerPool {
    ids: Vec<Id>,
    index: [u32; MAP_COUNT],
}

impl SamplerPool {
    fn new(base: Id) -> Self {
        Self { ids: vec![base], index: [0; MAP_COUNT] }
    }

    fn set(&mut self, map: usize, sampler: Id) {
        let i = match self.ids.iter().position(|&s| s == sampler) {
            Some(i) => i,
            None if self.ids.len() < SAMPLER_POOL => {
                self.ids.push(sampler);
                self.ids.len() - 1
            }
            None => 0,
        };
        self.index[map] = i as u32;
    }

    /// Every slot, the unused ones filled with the base map's sampler:
    /// Metal requires each declared sampler to be bound.
    fn slots(&self) -> [Id; SAMPLER_POOL] {
        std::array::from_fn(|i| self.ids.get(i).copied().unwrap_or(self.ids[0]))
    }

    fn packed(&self) -> [[u32; 4]; 6] {
        std::array::from_fn(|v| std::array::from_fn(|k| self.index.get(4 * v + k).copied().unwrap_or(0)))
    }
}

fn draw_uniforms(
    item: &DrawItem,
    has_map: bool,
    instanced: bool,
    geometry: &BufferGeometry,
) -> DrawUniforms {
    let material = &item.material;
    let color = material.color();
    let emissive = material.emissive();
    let specular = material.specular();
    let (depth_near, depth_far) = material.depth_range();
    let slots = material.texture_slots();
    // A sprite's normal matrix slot carries its billboard parameters instead
    // (see `sprite_vertex` in the shader); `object.y` says so.
    let normal_mat = match item.sprite {
        Some(p) => {
            let mut m = [0.0f32; 16];
            m[..4].copy_from_slice(&p);
            m
        }
        None => normal_matrix(&item.world),
    };
    let is_sprite = item.sprite.is_some() as u32;
    DrawUniforms {
        model: item.world.elements,
        normal_mat,
        base_color: [color.r, color.g, color.b, material.opacity()],
        emissive: [emissive.r, emissive.g, emissive.b, 0.0],
        specular: [specular.r, specular.g, specular.b, material.shininess()],
        pbr: [
            material.roughness(),
            material.metalness(),
            material.alpha_test(),
            // A toon `gradientMap` rides the matcap slot (see `encode_pass`).
            material.texture_slots().gradient_map.is_some() as u32 as f32,
        ],
        extra: [0; 4],
        map_scalars: material.map_uniforms()[0],
        env_params: material.map_uniforms()[1],
        env_dir: {
            let u = material.map_uniforms();
            [u[2], u[3], u[4]]
        },
        map_xform: map_xforms(&slots),
        // Filled in at bind time, where the pool and the uploaded set are known.
        map_samplers: [[0; 4]; 6],
        phys_flags: [0; 4],
        physical: match &**material {
            Material::Physical(m) => [
                [m.ior, m.specular_intensity, m.clearcoat, m.clearcoat_roughness],
                [m.specular_color.r, m.specular_color.g, m.specular_color.b, m.sheen],
                [m.sheen_color.r, m.sheen_color.g, m.sheen_color.b, m.sheen_roughness],
                [
                    m.iridescence,
                    m.iridescence_ior,
                    m.iridescence_thickness_range[0],
                    m.iridescence_thickness_range[1],
                ],
                [m.anisotropy, m.anisotropy_rotation, m.clearcoat_normal_scale.x, m.clearcoat_normal_scale.y],
                [
                    m.transmission,
                    m.thickness,
                    if m.attenuation_distance.is_finite() { m.attenuation_distance.max(0.0) } else { 0.0 },
                    m.dispersion,
                ],
                [m.attenuation_color.r, m.attenuation_color.g, m.attenuation_color.b, 0.0],
            ],
            _ => [[0.0; 4]; 7],
        },
        // `maps` is filled in at bind time, where the uploaded set is known.
        maps: [0; 4],
        object: [
            item.receive_shadow as u32,
            is_sprite,
            material.shadow_only() as u32,
            material.tone_mapped() as u32,
        ],
        map_params: [
            material.normal_scale().x,
            material.ao_intensity(),
            1.0,
            0.0,
        ],
        misc: [
            0.0,
            material.point_size(),
            if material.point_size_attenuation() {
                1.0
            } else {
                0.0
            },
            0.0,
        ],
        flags: [
            material.kind() as u32,
            has_map as u32,
            geometry.attributes.contains_key("color") as u32,
            // Bit 0: instanced; above it, where the instance colours start.
            if instanced { 1 | (item.instance_color_offset << 1) } else { 0 },
        ],
    }
    .with_depth_range(depth_near, depth_far)
}

impl DrawUniforms {
    /// `misc.z/w` carry the point-size attenuation flag for points and the
    /// depth range for `MeshDepthMaterial` — the two never apply at once.
    fn with_depth_range(mut self, near: f32, far: f32) -> Self {
        if self.flags[0] == MaterialKind::Depth as u32
            || self.flags[0] == MaterialKind::Distance as u32
        {
            self.misc[2] = near;
            self.misc[3] = far;
        }
        self
    }
}

/// Interleave a geometry's attributes into the shader's `Vertex` layout.
pub(crate) fn build_vertices(geometry: &BufferGeometry) -> Vec<Vertex> {
    let Some(positions) = geometry.attributes.get("position") else {
        return Vec::new();
    };
    if positions.item_size < 3 {
        return Vec::new();
    }
    let count = positions.array.len() / positions.item_size;
    let normals = geometry
        .attributes
        .get("normal")
        .filter(|a| a.item_size >= 3);
    let uvs = geometry.attributes.get("uv").filter(|a| a.item_size >= 2);
    let colors = geometry
        .attributes
        .get("color")
        .filter(|a| a.item_size >= 3);
    let derived = if normals.is_none() {
        // A mesh with no normals would light to black. Deriving them costs one
        // pass over the index buffer at upload and nothing per frame.
        Some(derive_normals(geometry, count))
    } else {
        None
    };

    let mut out = Vec::with_capacity(count);
    for i in 0..count {
        let p = positions.item_size * i;
        let normal = match (normals, &derived) {
            (Some(n), _) => {
                let j = n.item_size * i;
                [n.array[j], n.array[j + 1], n.array[j + 2]]
            }
            (None, Some(d)) => d[i],
            (None, None) => [0.0, 0.0, 1.0],
        };
        let uv = uvs
            .map(|a| {
                let j = a.item_size * i;
                [a.array[j], a.array[j + 1]]
            })
            .unwrap_or([0.0, 0.0]);
        let color = colors
            .map(|a| {
                let j = a.item_size * i;
                [a.array[j], a.array[j + 1], a.array[j + 2]]
            })
            .unwrap_or([1.0, 1.0, 1.0]);
        let alpha = colors
            .filter(|a| a.item_size >= 4)
            .map(|a| a.array[a.item_size * i + 3])
            .unwrap_or(1.0);
        out.push(Vertex {
            position: [
                positions.array[p],
                positions.array[p + 1],
                positions.array[p + 2],
            ],
            normal,
            uv,
            color,
            alpha,
        });
    }
    out
}

/// Interleave a line geometry's attributes into the slim `LineVertex` layout.
///
/// Deliberately does not derive normals: lines are unlit, and `derive_normals`
/// would allocate a scratch vector the size of the geometry to compute
/// something nothing reads.
pub(crate) fn build_line_vertices(geometry: &BufferGeometry) -> Vec<LineVertex> {
    let Some(positions) = geometry.attributes.get("position") else {
        return Vec::new();
    };
    if positions.item_size < 3 {
        return Vec::new();
    }
    let count = positions.array.len() / positions.item_size;
    let colors = geometry
        .attributes
        .get("color")
        .filter(|a| a.item_size >= 3);

    let mut out = Vec::with_capacity(count);
    for i in 0..count {
        let p = positions.item_size * i;
        let color = colors
            .map(|a| {
                let j = a.item_size * i;
                [a.array[j], a.array[j + 1], a.array[j + 2]]
            })
            .unwrap_or([1.0, 1.0, 1.0]);
        out.push(LineVertex {
            position: [
                positions.array[p],
                positions.array[p + 1],
                positions.array[p + 2],
            ],
            color,
        });
    }
    out
}

/// Area-weighted vertex normals, for geometry that arrived without any.
fn derive_normals(geometry: &BufferGeometry, count: usize) -> Vec<[f32; 3]> {
    let mut normals = vec![[0.0f32; 3]; count];
    let positions = match geometry.attributes.get("position") {
        Some(p) => p,
        None => return normals,
    };
    let at = |i: usize| {
        let j = positions.item_size * i;
        Vector3::new(
            positions.array[j],
            positions.array[j + 1],
            positions.array[j + 2],
        )
    };
    let mut accumulate = |a: usize, b: usize, c: usize| {
        if a >= count || b >= count || c >= count {
            return;
        }
        // Un-normalised cross product: its length is twice the triangle's
        // area, which is exactly the weight a smooth normal wants.
        let n = (at(b) - at(a)).cross(at(c) - at(a));
        for i in [a, b, c] {
            normals[i][0] += n.x;
            normals[i][1] += n.y;
            normals[i][2] += n.z;
        }
    };
    match &geometry.index {
        Some(idx) => {
            for tri in idx.chunks_exact(3) {
                accumulate(tri[0] as usize, tri[1] as usize, tri[2] as usize);
            }
        }
        None => {
            for t in 0..count / 3 {
                accumulate(t * 3, t * 3 + 1, t * 3 + 2);
            }
        }
    }
    for n in &mut normals {
        let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt();
        if len > 1e-12 {
            *n = [n[0] / len, n[1] / len, n[2] / len];
        } else {
            *n = [0.0, 0.0, 1.0];
        }
    }
    normals
}

/// The quad a `Sprite` draws.
///
/// A `Sprite` carries a material and no geometry, so the backend supplies one:
/// a shared unit quad on the XY plane, corners at ±0.5 with three.js's sprite
/// uvs. `vs_mesh` turns it into the camera-facing quad three.js's
/// `sprite_vert` draws, from the draw's `sprite` parameters.
fn sprite_geometry(_sprite: &crate::core::Sprite) -> Option<Arc<BufferGeometry>> {
    static QUAD: std::sync::OnceLock<Arc<BufferGeometry>> = std::sync::OnceLock::new();
    Some(
        QUAD.get_or_init(|| Arc::new(crate::geometries::PlaneGeometry::new(1.0, 1.0)))
            .clone(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::BufferAttribute;

    #[test]
    fn uniform_structs_match_the_shader() {
        // Sizes are the ABI contract with `shaders.metal`.
        assert_eq!(std::mem::size_of::<Vertex>(), 48);
        assert_eq!(std::mem::size_of::<DirLightGpu>(), 32);
        assert_eq!(std::mem::size_of::<PointLightGpu>(), 48);
        assert_eq!(std::mem::size_of::<SpotLightGpu>(), 64);
        assert_eq!(std::mem::size_of::<HemiLightGpu>(), 48);
        assert_eq!(
            std::mem::size_of::<DrawUniforms>(),
            // `model` and `normal_mat`; nine float4/uint4 rows; `extra`,
            // `map_scalars`, `env_params` and the three `env_dir` rows; the
            // six `map_samplers`, seven `physical` and one `phys_flags` rows;
            // and two `map_xform` rows a map — the members `struct Draw`
            // declares in `shaders.metal`.
            64 + 64 + 9 * 16 + 6 * 16 + 6 * 16 + 7 * 16 + 16 + 2 * MAP_COUNT * 16
        );
        assert_eq!(
            std::mem::size_of::<FrameUniforms>(),
            // Per view: view_proj, view, camera_pos. Then 6 float4s of frame
            // state, the shadow pass's matrices and point light, the light
            // arrays, the rect-area lights, and each light's shadow.
            MAX_VIEWS * (64 + 64 + 16)
                + 16 * 6
                + MAX_DIR_LIGHTS * 32
                + MAX_POINT_LIGHTS * 48
                + MAX_SPOT_LIGHTS * 64
                + MAX_HEMI_LIGHTS * 48
                + 16 + MAX_RECT_LIGHTS * 64 // rect_counts, rects
                + 16 // shadow_info
                + MAX_SHADOW_RECORDS * (4 * 16 + 4 * 64) // shadow records
        );
        assert_eq!(std::mem::size_of::<ShadowPassGpu>(), 64 + 16);
        // The frame goes in a buffer (it is past `setVertexBytes`'s 4 KB);
        // the draw uniforms still go inline.
        assert!(std::mem::size_of::<DrawUniforms>() <= 4096);
    }

    fn triangle() -> BufferGeometry {
        let mut g = BufferGeometry::new();
        g.set_attribute(
            "position",
            BufferAttribute::new(vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0], 3),
        );
        g
    }

    #[test]
    fn missing_normals_are_derived() {
        let verts = build_vertices(&triangle());
        assert_eq!(verts.len(), 3);
        // A CCW triangle on the XY plane faces +Z.
        for v in &verts {
            assert!((v.normal[2] - 1.0).abs() < 1e-5, "{:?}", v.normal);
        }
    }

    #[test]
    fn attributes_interleave_in_order() {
        let mut g = triangle();
        g.set_attribute(
            "normal",
            BufferAttribute::new(vec![0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0], 3),
        );
        g.set_attribute(
            "uv",
            BufferAttribute::new(vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0], 2),
        );
        g.set_attribute(
            "color",
            BufferAttribute::new(vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0], 3),
        );
        let verts = build_vertices(&g);
        assert_eq!(verts[1].position, [1.0, 0.0, 0.0]);
        assert_eq!(verts[1].normal, [0.0, 1.0, 0.0]);
        assert_eq!(verts[1].uv, [1.0, 0.0]);
        assert_eq!(verts[2].color, [0.0, 0.0, 1.0]);
    }

    #[test]
    fn geometry_without_positions_is_skipped() {
        assert!(build_vertices(&BufferGeometry::new()).is_empty());
        assert!(build_line_vertices(&BufferGeometry::new()).is_empty());
    }

    #[test]
    fn line_vertices_are_half_the_size_and_carry_position_and_colour() {
        // The whole point of the slim layout. If this ever stops holding, the
        // connectome renders go back to paging.
        assert_eq!(std::mem::size_of::<LineVertex>(), 24);
        assert_eq!(std::mem::size_of::<Vertex>(), 48);

        let mut g = triangle();
        g.set_attribute(
            "color",
            BufferAttribute::new(vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0], 3),
        );
        let verts = build_line_vertices(&g);
        assert_eq!(verts.len(), 3);
        assert_eq!(verts[1].position, [1.0, 0.0, 0.0]);
        assert_eq!(verts[2].color, [0.0, 0.0, 1.0]);
    }

    #[test]
    fn line_vertices_default_to_white_without_a_colour_attribute() {
        // Matches `build_vertices`, whose shader multiplies by this when the
        // material does not enable vertex colours.
        let verts = build_line_vertices(&triangle());
        assert_eq!(verts[0].color, [1.0, 1.0, 1.0]);
    }

    #[test]
    fn normal_matrix_is_inverse_transpose() {
        let m = Matrix4::compose(
            Vector3::new(1.0, 2.0, 3.0),
            crate::math::Quaternion::identity(),
            Vector3::new(2.0, 1.0, 0.5),
        );
        let n = normal_matrix(&m);
        // Under a non-uniform scale, a normal along x scales by 1/2, not 2.
        assert!((n[0] - 0.5).abs() < 1e-5, "{}", n[0]);
        assert!((n[5] - 1.0).abs() < 1e-5, "{}", n[5]);
        assert!((n[10] - 2.0).abs() < 1e-5, "{}", n[10]);
    }

    #[test]
    fn transparency_follows_three_js() {
        use crate::materials::{BasicMaterial, StandardMaterial};
        use crate::math::Color;
        let opaque = Material::Basic(BasicMaterial::default());
        assert!(!is_transparent(&opaque));
        // three.js blends only a material marked `transparent`: a Basic
        // material at opacity 0.5 without the flag draws opaquely, as wgpu
        // draws it.
        let faded = Material::Basic(BasicMaterial::default().with_opacity(0.5));
        assert!(!is_transparent(&faded));
        let flagged = Material::Basic(BasicMaterial::default().with_transparent(true));
        assert!(is_transparent(&flagged));
        // A native material that never set the flag keeps blending below
        // opacity 1 (`transparent: None`); setting it decides.
        let mut lit = StandardMaterial::new(Color::WHITE);
        lit.opacity = 0.5;
        assert!(is_transparent(&Material::Standard(lit.clone())));
        lit.transparent = Some(false);
        assert!(!is_transparent(&Material::Standard(lit)));
    }
}