roxlap-gpu 0.30.0

GPU compute-shader renderer for the roxlap voxel engine (WGPU + WGSL DDA marcher). Sibling to roxlap-core's CPU DDA renderer.
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
//! GPU.11.0 gate — headless scene-DDA render.
//!
//! Stands up a real GPU device, uploads a one-grid scene whose every
//! column has a textured floor voxel, and renders it through the
//! `scene_dda.wgsl` compute pipeline that now carries the full mip
//! ladder per slot (GPU.11.0). The shader still marches mip-0, so a
//! correct render proves:
//!   1. `scene_dda.wgsl` compiles with the grown `GridStaticMeta`.
//!   2. The 112-byte std430 struct layout matches the Rust upload.
//!   3. The new per-slot occupancy / color_offsets *strides* still
//!      address mip-0 byte-identically (a floor voxel reads its
//!      colour back through the strided layout).
//!
//! Skips silently if no Vulkan/Metal/DX12 adapter is reachable.

#![allow(
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::doc_markdown,
    clippy::many_single_char_names,
    clippy::redundant_closure_for_method_calls
)]

use std::sync::Mutex;

use roxlap_formats::vxl::Vxl;
use roxlap_gpu::{
    decompress_chunk, Camera, GpuInitError, GpuLight, GpuRendererSettings, GpuSceneResident,
    GpuViewCutout, GridUpload, GridWorldTransform, HeadlessGpu, HeadlessSceneRenderer, SceneLights,
    SceneUpload,
};

static GPU_TEST_LOCK: Mutex<()> = Mutex::new(());

fn try_init() -> Option<(HeadlessGpu, std::sync::MutexGuard<'static, ()>)> {
    let guard = GPU_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    match HeadlessGpu::new_blocking(GpuRendererSettings::default()) {
        Ok(gpu) => Some((gpu, guard)),
        Err(GpuInitError::NoAdapter) => {
            eprintln!("[skip] no GPU adapter reachable");
            None
        }
        Err(e) => {
            eprintln!("[skip] GPU init failed ({e})");
            None
        }
    }
}

/// `vsid × vsid` chunk: one textured floor voxel per column at
/// `z = 100`, colour `0x80ff_8000` (A=0x80 → brightness 1.0,
/// R=0xff, G=0x80, B=0x00). decompress_chunk builds its mip ladder.
fn floor_chunk(vsid: u32) -> Vxl {
    let n_cols = (vsid as usize) * (vsid as usize);
    let mut data: Vec<u8> = Vec::with_capacity(n_cols * 8);
    let mut column_offset: Vec<u32> = Vec::with_capacity(n_cols + 1);
    let bgra = [0x00u8, 0x80, 0xff, 0x80];
    for _ in 0..n_cols {
        column_offset.push(u32::try_from(data.len()).expect("offset fits"));
        data.extend_from_slice(&[0, 100, 100, 0]); // nextptr=0, z1=100, z1c=100, z0=0
        data.extend_from_slice(&bgra);
    }
    column_offset.push(u32::try_from(data.len()).expect("offset fits"));
    Vxl {
        vsid,
        ipo: [0.0; 3],
        ist: [1.0, 0.0, 0.0],
        ihe: [0.0, 0.0, 1.0],
        ifo: [0.0, 1.0, 0.0],
        data: data.into_boxed_slice(),
        column_offset: column_offset.into_boxed_slice(),
        mip_base_offsets: Box::new([0, n_cols + 1]),
        vbit: Box::new([]),
        vbiti: 0,
    }
}

/// `vsid × vsid` chunk: every column solid over `z ∈ [top, bot]`
/// (a wall/block facing a horizontal ray), colour `0x80ff_8000`.
fn block_chunk(vsid: u32, top: u8, bot: u8) -> Vxl {
    let n_cols = (vsid as usize) * (vsid as usize);
    let n_vox = (bot - top + 1) as usize;
    let mut data: Vec<u8> = Vec::with_capacity(n_cols * (4 + n_vox * 4));
    let mut column_offset: Vec<u32> = Vec::with_capacity(n_cols + 1);
    let bgra = [0x00u8, 0x80, 0xff, 0x80];
    for _ in 0..n_cols {
        column_offset.push(u32::try_from(data.len()).expect("offset fits"));
        data.extend_from_slice(&[0, top, bot, 0]); // nextptr=0, z1=top, z1c=bot, z0=0
        for _ in 0..n_vox {
            data.extend_from_slice(&bgra);
        }
    }
    column_offset.push(u32::try_from(data.len()).expect("offset fits"));
    Vxl {
        vsid,
        ipo: [0.0; 3],
        ist: [1.0, 0.0, 0.0],
        ihe: [0.0, 0.0, 1.0],
        ifo: [0.0, 1.0, 0.0],
        data: data.into_boxed_slice(),
        column_offset: column_offset.into_boxed_slice(),
        mip_base_offsets: Box::new([0, n_cols + 1]),
        vbit: Box::new([]),
        vbiti: 0,
    }
}

/// [`block_chunk`] with an explicit BGRA colour (SC.4 — two colours so a
/// two-grid composite test can tell which grid won the min-t depth test).
fn block_chunk_bgra(vsid: u32, top: u8, bot: u8, bgra: [u8; 4]) -> Vxl {
    let n_cols = (vsid as usize) * (vsid as usize);
    let n_vox = (bot - top + 1) as usize;
    let mut data: Vec<u8> = Vec::with_capacity(n_cols * (4 + n_vox * 4));
    let mut column_offset: Vec<u32> = Vec::with_capacity(n_cols + 1);
    for _ in 0..n_cols {
        column_offset.push(u32::try_from(data.len()).expect("offset fits"));
        data.extend_from_slice(&[0, top, bot, 0]);
        for _ in 0..n_vox {
            data.extend_from_slice(&bgra);
        }
    }
    column_offset.push(u32::try_from(data.len()).expect("offset fits"));
    Vxl {
        vsid,
        ipo: [0.0; 3],
        ist: [1.0, 0.0, 0.0],
        ihe: [0.0, 0.0, 1.0],
        ifo: [0.0, 1.0, 0.0],
        data: data.into_boxed_slice(),
        column_offset: column_offset.into_boxed_slice(),
        mip_base_offsets: Box::new([0, n_cols + 1]),
        vbit: Box::new([]),
        vbiti: 0,
    }
}

/// Recognisably the orange block (`0x80ff_8000` at brightness 1.0 →
/// ~(255,128,0)), not the bluish sky (~(120,150,220)). Loose because
/// coarse mips average the (uniform) block colour.
fn is_block_color(p: u32) -> bool {
    let (r, g, b) = (p & 0xff, (p >> 8) & 0xff, (p >> 16) & 0xff);
    r > 180 && (80..=175).contains(&g) && b < 70
}

/// DL.3 — floor at z=100 in every column, plus a short wall standing on it
/// at `x ∈ [wx0, wx1)` (all y), solid `z ∈ [wtop, 100]` (rising `100-wtop`
/// voxels above the floor). Used to cast a sun shadow onto the floor next to
/// the wall. Colour `0x80ff_8000`.
fn floor_with_wall_chunk(vsid: u32, wx0: u32, wx1: u32, wtop: u8) -> Vxl {
    let n_cols = (vsid as usize) * (vsid as usize);
    let mut data: Vec<u8> = Vec::new();
    let mut column_offset: Vec<u32> = Vec::with_capacity(n_cols + 1);
    let bgra = [0x00u8, 0x80, 0xff, 0x80];
    for i in 0..n_cols {
        let x = (i as u32) % vsid;
        column_offset.push(u32::try_from(data.len()).expect("offset fits"));
        if x >= wx0 && x < wx1 {
            let n_vox = (100 - wtop + 1) as usize;
            data.extend_from_slice(&[0, wtop, 100, 0]); // z1=wtop..z1c=100
            for _ in 0..n_vox {
                data.extend_from_slice(&bgra);
            }
        } else {
            data.extend_from_slice(&[0, 100, 100, 0]); // floor voxel at z=100
            data.extend_from_slice(&bgra);
        }
    }
    column_offset.push(u32::try_from(data.len()).expect("offset fits"));
    Vxl {
        vsid,
        ipo: [0.0; 3],
        ist: [1.0, 0.0, 0.0],
        ihe: [0.0, 0.0, 1.0],
        ifo: [0.0, 1.0, 0.0],
        data: data.into_boxed_slice(),
        column_offset: column_offset.into_boxed_slice(),
        mip_base_offsets: Box::new([0, n_cols + 1]),
        vbit: Box::new([]),
        vbiti: 0,
    }
}

/// `vsid × vsid` chunk: one textured floor voxel per column at `z =
/// surf`, with implicit voxlap **bedrock** solid below it to z=255.
/// Models a cliff/wall: only the top is coloured; the face below is
/// bedrock. Pre-fix the GPU treated bedrock as air → the face showed
/// sky. Slab `[nextptr=0, z1=surf, z1c=surf, z0=0]` + 1 colour.
fn wall_chunk(vsid: u32, surf: u8) -> Vxl {
    let n_cols = (vsid as usize) * (vsid as usize);
    let mut data: Vec<u8> = Vec::with_capacity(n_cols * 8);
    let mut column_offset: Vec<u32> = Vec::with_capacity(n_cols + 1);
    let bgra = [0x00u8, 0x80, 0xff, 0x80];
    for _ in 0..n_cols {
        column_offset.push(u32::try_from(data.len()).expect("offset fits"));
        data.extend_from_slice(&[0, surf, surf, 0]);
        data.extend_from_slice(&bgra);
    }
    column_offset.push(u32::try_from(data.len()).expect("offset fits"));
    Vxl {
        vsid,
        ipo: [0.0; 3],
        ist: [1.0, 0.0, 0.0],
        ihe: [0.0, 0.0, 1.0],
        ifo: [0.0, 1.0, 0.0],
        data: data.into_boxed_slice(),
        column_offset: column_offset.into_boxed_slice(),
        mip_base_offsets: Box::new([0, n_cols + 1]),
        vbit: Box::new([]),
        vbiti: 0,
    }
}

#[test]
fn scene_dda_marches_coarse_mip_for_distant_chunk() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    eprintln!("mip_render: adapter = {}", gpu.adapter_info);

    // One solid block chunk placed FAR along +y (chunk index 4) so a
    // horizontal ray enters it at t ≈ 128 — past several octaves of
    // mip_scan_dist, forcing a deep mip. The camera sits in the empty
    // chunk (0,0,0).
    let vsid = 32u32;
    let chunk = decompress_chunk(&block_chunk(vsid, 0, 31));
    assert!(chunk.mips.len() >= 5, "need a deep ladder for mip-4");

    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 8, 1],
        pool_dims: [1, 8, 1],
        chunks: vec![([0, 4, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Camera in the empty near chunk, looking +y at the block; z=16
    // lands inside the block's z=0..31 band. right × down == forward.
    let cam = Camera {
        position: [vsid as f32 * 0.5, 0.0, 16.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, 1.0, 0.0],
        fov_y_rad: 30f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;

    // mip-0 baseline (LOD off): the block renders.
    let fb0 = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        cam.fov_y_rad,
        64,
        0.0,
    );
    assert!(
        is_block_color(fb0[centre]),
        "mip-0 centre should be the block, got {:#08x}",
        fb0[centre],
    );

    // Force a deep mip: msd=8 at t≈128 → mip-4. If mip-N occupancy /
    // colour addressing were wrong the block would vanish (sky) or
    // render a garbage colour.
    let fb4 = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        cam.fov_y_rad,
        64,
        8.0,
    );
    eprintln!(
        "mip_render: centre mip0={:#08x} mip4={:#08x}",
        fb0[centre], fb4[centre]
    );
    assert!(
        is_block_color(fb4[centre]),
        "coarse-mip centre should still be the block, got {:#08x}",
        fb4[centre],
    );

    // The coarse render should broadly agree with mip-0 (same block
    // fills the view) — most pixels classify the same way.
    let agree = fb0
        .iter()
        .zip(&fb4)
        .filter(|(a, b)| is_block_color(**a) == is_block_color(**b))
        .count();
    let frac = agree as f32 / fb0.len() as f32;
    eprintln!("mip_render: block/sky agreement = {frac:.3}");
    assert!(
        frac > 0.9,
        "mip-0 vs mip-4 block coverage diverged: {frac:.3}"
    );
}

#[test]
fn scene_dda_aabb_early_out_away_is_sky() {
    // GPU.13.0 — the occupied chunk-AABB early-out must terminate a
    // ray the moment it has left the box along its travel direction.
    // One block chunk at +y (chunk 4); the camera sits in the near
    // chunk (0,0,0) but looks the OTHER way (−y). Every ray starts
    // already past the AABB's near slab (p.y=0 < aabb_min.y=4 with
    // step.y<0) → instant early-out → pure sky, no block pixels.
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&block_chunk(vsid, 0, 31));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 8, 1],
        pool_dims: [1, 8, 1],
        chunks: vec![([0, 4, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });
    // Sanity: the upload computed the occupied AABB at chunk y=4.
    assert_eq!(scene.static_meta[0].aabb_min, [0, 4, 0]);
    assert_eq!(scene.static_meta[0].aabb_max, [0, 4, 0]);

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [vsid as f32 * 0.5, 0.0, 16.0],
        right: [-1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, -1.0, 0.0], // look AWAY from the block
        fov_y_rad: 30f32.to_radians(),
    };
    let fb = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        cam.fov_y_rad,
        64,
        0.0,
    );
    let block_px = fb.iter().filter(|&&p| is_block_color(p)).count();
    assert_eq!(
        block_px, 0,
        "looking away from the only chunk must be all sky, got {block_px} block pixels",
    );
}

#[test]
fn scene_dda_zero_grids_renders_sky() {
    // Regression for the sprite-only / empty-scene GPU path: a scene
    // with ZERO grids must still render a valid frame — the scene pass
    // fills the flat sky everywhere (+ far depth), giving the sprite
    // pass a background to composite over. Pre-fix the render facade
    // short-circuited a grid-less scene to a bare clear and never ran
    // `render_scene`, so a sprite-only viewer (no voxel grids) showed
    // only the clear colour with the model invisible. This exercises
    // the engine half the facade fix newly relies on: grid_count == 0
    // with zero cameras renders the uniform sky without panicking.
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![] });
    assert_eq!(
        scene.grid_count, 0,
        "empty SceneUpload → zero-grid resident"
    );

    let (w, h) = (32u32, 32u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Zero cameras matches grid_count == 0 (render_scene asserts equal).
    // Pre-fix this path was never dispatched (the facade short-circuited
    // to a clear); the win here is it runs end-to-end without panic and
    // produces a clean, consistent background for the sprite pass.
    let fb = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[],
        30f32.to_radians(),
        64,
        0.0,
    );
    assert_eq!(fb.len(), (w * h) as usize);

    // No grids → no per-pixel grid hits → every pixel is the flat sky
    // sample (headless default sky [120, 150, 220]). Uniform proves the
    // zero-grid path reads no garbage grid data; bluish (b > r) + not
    // black proves the sky direction is well-formed — the pre-fix
    // (0,0,1) default fed atan2(0,0) → NaN → a black sample.
    let first = fb[0];
    assert!(
        fb.iter().all(|&p| p == first),
        "zero-grid frame must be a uniform sky, got varied pixels (first={first:#08x})",
    );
    assert_ne!(
        first & 0x00ff_ffff,
        0,
        "sky must not be black, got {first:#08x}"
    );
    let (r, _g, b) = (first & 0xff, (first >> 8) & 0xff, (first >> 16) & 0xff);
    assert!(b > r, "headless sky is bluish (b>r), got {first:#08x}");
}

/// GPU side-shade (voxlap setsideshades) darkens a grid face. Camera
/// looks straight down at a floor plane; the floor is hit via a +z
/// step, so its shade comes from the `bot` lane. Rendering with
/// `bot = 64` must darken the floor ~half vs the unshaded baseline,
/// proving the scene-DDA face detection + brightness reduction work.
/// (Exact CPU parity needs visual inspection; this guards the
/// mechanism + uniform plumbing against regressions.)
#[test]
fn scene_dda_side_shades_darken_floor() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&floor_chunk(vsid)); // floor voxel at z=100
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Above the floor (z=50 < 100), looking straight down (+z, voxlap
    // z-down). right × down == forward.
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);

    let fb0 = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        cam.fov_y_rad,
        64,
        0.0,
    );
    let base = fb0[centre];
    assert!(
        is_block_color(base),
        "centre should be the lit floor, got {base:#08x}",
    );

    // Darken the floor face (bot lane = side_shades[1]).
    renderer.set_side_shades([0, 64, 0, 0, 0, 0]);
    let fb1 = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        cam.fov_y_rad,
        64,
        0.0,
    );
    let shaded = fb1[centre];
    assert!(
        lum(shaded) < lum(base),
        "side-shade should darken the floor: {base:#08x} -> {shaded:#08x}",
    );
    // bot=64 of 128 → ~half brightness, still textured (not black sky).
    assert_ne!(
        shaded & 0x00ff_ffff,
        0,
        "half-shaded floor must not be black"
    );
}

/// FW.3 — a `fog_mask` for one 1-deck grid (slot 0, band z∈[0,255]) over
/// cells `[0, vsid)²` at origin `(0, 0)`, every cell set to `state` (2 =
/// Visible, 1 = Memory, 0 = Unseen) at `intensity` (0..=63), styled by
/// `memory_dim`. Built via the SHARED `roxlap_gpu::fow::pack_fog_mask`
/// (one header owner — the same packer the production path uses).
fn fog_mask_uniform(vsid: u32, state: u8, intensity: u8, memory_dim: f32) -> Vec<u32> {
    let byte = (state << 6) | (intensity & 63);
    let cells = vec![byte; (vsid * vsid) as usize];
    roxlap_gpu::fow::pack_fog_mask(
        0,
        [0, 0],
        vsid,
        vsid,
        &[[0, 255]],
        0,
        memory_dim,
        0.0,
        &cells,
    )
}

/// FW.3 — the GPU fog mask hides Unseen cells (renders sky) and shows
/// Visible cells (renders the floor, dim=1), mirroring the CPU verdict.
#[test]
fn scene_dda_fog_mask_hides_unseen_shows_visible() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&floor_chunk(vsid)); // floor voxel at z=100
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });
    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };

    // No fog → the floor draws.
    let base = render(&mut renderer);
    assert!(is_block_color(base), "baseline floor, got {base:#08x}");

    // All-Visible fog at full intensity → still the floor (dim=1 identity).
    renderer.set_fog_mask(&fog_mask_uniform(vsid, 2, 63, 1.0));
    let visible = render(&mut renderer);
    assert!(is_block_color(visible), "visible floor, got {visible:#08x}");

    // All-Unseen fog → the floor is Hidden (treated as air) → sky.
    renderer.set_fog_mask(&fog_mask_uniform(vsid, 0, 0, 1.0));
    let unseen = render(&mut renderer);
    assert!(
        !is_block_color(unseen),
        "unseen cell must render as sky, got {unseen:#08x}"
    );

    // Clearing the mask restores the floor (byte-identical to baseline).
    renderer.set_fog_mask(&[]);
    let cleared = render(&mut renderer);
    assert_eq!(cleared, base, "disabled fog is byte-identical");
}

/// FW.3 — a Memory cell at low intensity renders dimmer than the live
/// floor (the `memory_dim` taper), mirroring the CPU `fow_style`.
#[test]
fn scene_dda_fog_memory_dims_floor() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&floor_chunk(vsid));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });
    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };

    // Visible at full intensity (dim 1).
    renderer.set_fog_mask(&fog_mask_uniform(vsid, 2, 63, 0.4));
    let visible = render(&mut renderer);
    // Memory at intensity 0 → dim = memory_dim = 0.4.
    renderer.set_fog_mask(&fog_mask_uniform(vsid, 1, 0, 0.4));
    let memory = render(&mut renderer);
    assert!(is_block_color(visible), "visible floor, got {visible:#08x}");
    assert!(
        lum(memory) < lum(visible),
        "memory floor must be dimmer: {visible:#08x} -> {memory:#08x}"
    );
    assert_ne!(memory & 0x00ff_ffff, 0, "dimmed memory is not black");
}

/// DL.1 — the directional sun (N·L diffuse) lights a grid face by its
/// facing. Camera looks straight down a floor (hit via +z step ⇒ top-face
/// normal = -z = up). A sun coming from above (to-sun = up = -z) gives
/// N·L = 1 → the floor is brighter than the baked-only baseline; a sun
/// from below (to-sun = +z) gives N·L = 0 → no sun term. Proves the
/// albedo/ambient split, face-normal, sun_dir plumbing, and `sun_flags`
/// gate end-to-end through `scene_dda.wgsl`.
#[test]
fn scene_dda_sun_lights_floor_by_facing() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&floor_chunk(vsid)); // floor voxel at z=100
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };

    // Baseline: no lights (baked-only path).
    let baked = render(&mut renderer);
    assert!(
        is_block_color(baked),
        "centre should be the floor: {baked:#08x}"
    );

    // A single white grid is identity-aligned, so grid-local == world. Sun
    // from above: to-sun direction is up (-z, voxlap z-down).
    let sun = |to_sun: [f32; 3]| SceneLights {
        enabled: true,
        grid_sun_dirs: vec![to_sun],
        sun_color: [1.0; 3],
        sun_intensity: 1.0,
        ambient: [1.0; 3],
        ..SceneLights::default()
    };

    renderer.set_scene_lights(sun([0.0, 0.0, -1.0]));
    let lit_above = render(&mut renderer);
    renderer.set_scene_lights(sun([0.0, 0.0, 1.0]));
    let lit_below = render(&mut renderer);

    assert!(
        lum(lit_above) > lum(baked),
        "sun from above must brighten the floor: baked {baked:#08x} -> {lit_above:#08x}",
    );
    assert!(
        lum(lit_above) > lum(lit_below),
        "sun facing the surface must beat a back-facing sun: {lit_above:#08x} vs {lit_below:#08x}",
    );
}

/// DL.6 — stylized cel banding terraces the sun's diffuse. Two sun
/// directions giving distinct smooth N·L (0.8 vs 0.9 on the flat floor)
/// land on the **same** band at `bands = 2` (both round to the top level),
/// so the stylized floor renders identically while the smooth floor differs.
#[test]
fn scene_dda_cel_banding_terraces_sun() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&floor_chunk(vsid));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };

    // Floor top normal = up (-z); N·L = -to_sun.z. Two sun elevations:
    // ndl = 0.8 and 0.9 (distinct), both rounding to the top band at bands=2.
    let a = [0.6_f32, 0.0, -0.8]; // ndl 0.8
    let b = [0.435_89_f32, 0.0, -0.9]; // ndl 0.9
    let rig = |to_sun: [f32; 3], bands: u32| SceneLights {
        enabled: true,
        grid_sun_dirs: vec![to_sun],
        sun_color: [1.0; 3],
        sun_intensity: 1.0,
        ambient: [0.1; 3],
        style_bands: bands,
        shadow_tint: [0.1, 0.1, 0.2],
        ..SceneLights::default()
    };

    // Smooth (bands = 0): the two elevations differ.
    renderer.set_scene_lights(rig(a, 0));
    let smooth_a = render(&mut renderer);
    renderer.set_scene_lights(rig(b, 0));
    let smooth_b = render(&mut renderer);
    assert_ne!(
        smooth_a, smooth_b,
        "smooth diffuse must vary with N·L: {smooth_a:#08x} vs {smooth_b:#08x}",
    );

    // Stylized (bands = 2): both N·L round to the same band ⇒ identical.
    renderer.set_scene_lights(rig(a, 2));
    let cel_a = render(&mut renderer);
    renderer.set_scene_lights(rig(b, 2));
    let cel_b = render(&mut renderer);
    assert_eq!(
        cel_a, cel_b,
        "cel banding must terrace both N·L to one level: {cel_a:#08x} vs {cel_b:#08x}",
    );
}

/// DL.2 — point lights: N·L diffuse + distance falloff + hard radius cut.
/// Floor viewed straight down (top-face normal = up = -z). A point light
/// hovering just above the floor centre brightens it vs the baked baseline;
/// a light below the top face contributes nothing (back-facing); a distant
/// light (still above) is dimmer than a near one (falloff).
#[test]
fn scene_dda_point_light_brightens_by_distance_and_facing() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&floor_chunk(vsid)); // floor at z=100
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };

    // Baseline: no lights (ambient only via the baked path).
    let baked = render(&mut renderer);
    assert!(
        is_block_color(baked),
        "centre should be the floor: {baked:#08x}"
    );

    // Identity grid ⇒ grid-local == world. The floor centre is ~(16,16,100).
    let one_point = |pos: [f32; 3]| SceneLights {
        enabled: true,
        ambient: [1.0; 3],
        grid_point_lights: vec![vec![GpuLight {
            position: pos,
            radius: 64.0,
            color: [1.0; 3],
            intensity: 2.0,
            casts_shadow: false,
            // SL — `-1.0` outer cosine ⇒ a pure point light (no cone mask).
            spot_dir: [0.0, 0.0, 1.0],
            cos_inner: -1.0,
            cos_outer: -1.0,
        }]],
        ..SceneLights::default()
    };

    renderer.set_scene_lights(one_point([16.0, 16.0, 98.0])); // 2 above the top
    let near_above = render(&mut renderer);
    renderer.set_scene_lights(one_point([16.0, 16.0, 60.0])); // 40 above the top
    let far_above = render(&mut renderer);
    renderer.set_scene_lights(one_point([16.0, 16.0, 110.0])); // below the top face
    let below = render(&mut renderer);

    assert!(
        lum(near_above) > lum(baked),
        "a near point light must brighten the floor: baked {baked:#08x} -> {near_above:#08x}",
    );
    assert!(
        lum(near_above) > lum(far_above),
        "distance falloff: near must beat far: {near_above:#08x} vs {far_above:#08x}",
    );
    assert!(
        lum(below) <= lum(baked) + 2,
        "a back-facing point light must not light the top face: {below:#08x} vs baked {baked:#08x}",
    );
}

/// SL — spot (cone) lights. Same floor-viewed-straight-down setup as the
/// point-light test. A spot hovering just above the centre and aimed straight
/// down (on-axis, `cd == 1`, inside the inner cone) must render **identically**
/// to the equivalent point light — proving the fold passes colour/intensity
/// and that the cone factor saturates to 1 inside the inner half-angle. The
/// same spot aimed sideways puts the centre outside the cone, so it masks to
/// zero and matches the unlit baked baseline (masking + the early-out).
#[test]
fn scene_dda_spot_cone_matches_point_on_axis_and_masks_off_axis() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&floor_chunk(vsid)); // floor at z=100
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };

    // Light 2 units above the floor top (z=98 < 100), over the centre (16,16).
    let pos = [16.0, 16.0, 98.0];
    // A wide cone: the light sits close to the surface, so the centre pixel's
    // exact hit is a few degrees off the axis — well inside a 40° inner angle
    // (cone == 1), while a sideways-aimed copy leaves it ~90° off (masked).
    let cos_inner = 40f32.to_radians().cos();
    let cos_outer = 60f32.to_radians().cos();
    let light = |spot_dir: [f32; 3], ci: f32, co: f32| SceneLights {
        enabled: true,
        ambient: [1.0; 3],
        grid_point_lights: vec![vec![GpuLight {
            position: pos,
            radius: 64.0,
            color: [1.0; 3],
            intensity: 2.0,
            casts_shadow: false,
            spot_dir,
            cos_inner: ci,
            cos_outer: co,
        }]],
        ..SceneLights::default()
    };

    let baked = render(&mut renderer); // no lights set yet ⇒ ambient/baked only
                                       // A pure point light (cone disabled, cos_outer = -1).
    renderer.set_scene_lights(light([0.0, 0.0, 1.0], -1.0, -1.0));
    let point = render(&mut renderer);
    // A real cone aimed straight down (+z) at the floor ⇒ centre on-axis.
    renderer.set_scene_lights(light([0.0, 0.0, 1.0], cos_inner, cos_outer));
    let spot_on = render(&mut renderer);
    // The same cone aimed sideways ⇒ the centre is outside it.
    renderer.set_scene_lights(light([1.0, 0.0, 0.0], cos_inner, cos_outer));
    let spot_off = render(&mut renderer);

    assert!(
        lum(point) > lum(baked),
        "sanity: the point light must brighten the floor: {baked:#08x} -> {point:#08x}",
    );
    assert_eq!(
        spot_on, point,
        "on-axis spot (cd=1, inside inner cone) must equal the point light: \
         {spot_on:#08x} vs {point:#08x}",
    );
    assert_eq!(
        spot_off, baked,
        "off-cone spot must contribute nothing (masked to zero): \
         {spot_off:#08x} vs baked {baked:#08x}",
    );
}

/// DL.3 — stylized hard shadows. A short wall stands on the floor at x≈16;
/// the camera looks straight down at the floor point (14,16) — which the
/// wall does NOT block from above. An angled sun (toward +x and up) is
/// occluded by the wall on its way to that point, so with shadow-casting ON
/// the point is darker than with the same sun and shadows OFF. Proves the
/// `shadow_occluded` DDA, the normal bias, and the `casts_shadow` gate.
#[test]
fn scene_dda_sun_shadow_darkens_occluded_floor() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // Wall at x ∈ [16,18), 10 voxels tall above the floor (z 90..100).
    let chunk = decompress_chunk(&floor_with_wall_chunk(vsid, 16, 18, 90));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Directly above the floor point (14,16): the centre pixel shows it, and
    // the wall at x=16 doesn't block this straight-down view.
    let cam = Camera {
        position: [14.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };

    // Sun toward +x and up (to-sun = normalize(1,0,-1)): it reaches (14,16)
    // only by passing through the wall at x=16 → occluded when shadows cast.
    let s = std::f32::consts::FRAC_1_SQRT_2;
    let sun = |casts_shadow: bool| SceneLights {
        enabled: true,
        grid_sun_dirs: vec![[s, 0.0, -s]],
        sun_color: [1.0; 3],
        sun_intensity: 3.0,
        sun_casts_shadow: casts_shadow,
        ambient: [0.5; 3],
        shadow_strength: 1.0, // full black shadow
        shadow_bias: 1.5,
        shadow_max_dist: 512.0,
        shadow_max_steps: 256,
        ..SceneLights::default()
    };

    renderer.set_scene_lights(sun(false));
    let lit = render(&mut renderer); // sun reaches the floor (no shadow test)
    renderer.set_scene_lights(sun(true));
    let shadowed = render(&mut renderer); // wall occludes the sun → darker

    // Both are the floor (low blue), not the bluish sky — even at intensity 3
    // the floor's blue stays 0 while sky is ~0xdc.
    let blue = |p: u32| (p >> 16) & 0xff;
    assert!(
        blue(lit) < 70 && blue(shadowed) < 70,
        "expected floor, not sky"
    );
    assert!(
        lum(shadowed) < lum(lit),
        "wall must cast a sun shadow on the floor: lit {lit:#08x} -> shadowed {shadowed:#08x}",
    );
}

/// XS.3 — cross-grid sun shadow: a wall in grid **B** (placed at a world
/// offset) shadows the floor of grid **A**. The shadow only appears if the
/// shadow ray crosses from A into B in world space, so shadows-on must be
/// darker than shadows-off. Exercises the per-grid world transform packing +
/// `shadow_occluded_world`.
#[test]
fn scene_dda_cross_grid_sun_shadow() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // Grid A: plain floor at z=100, world origin (0,0,0).
    let grid_a = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], decompress_chunk(&floor_chunk(vsid)))],
    };
    // Grid B: a wall (x∈[16,18), z 90..100) — moved to world (+2,0,0) below, so
    // its wall sits at world x∈[18,20), on the to-sun ray from A's floor point
    // (the ray reaches it at z≈96, inside the wall's z-span).
    let grid_b = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![(
            [0, 0, 0],
            decompress_chunk(&floor_with_wall_chunk(vsid, 16, 18, 90)),
        )],
    };
    let scene = GpuSceneResident::upload(
        &gpu.device,
        &SceneUpload {
            grids: vec![grid_a, grid_b],
        },
    );

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Straight down over A's floor point (14,16). World camera.
    let cam = Camera {
        position: [14.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    // Per-grid local cameras: A is identity; B is offset by world (2,0,0).
    let cam_b = Camera {
        position: [14.0 - 2.0, 16.0, 50.0],
        ..cam
    };
    // Per-grid world transforms: A identity, B translated +2 in x.
    let xf_a = GridWorldTransform::default();
    let xf_b = GridWorldTransform {
        origin: [2.0, 0.0, 0.0],
        ..GridWorldTransform::default()
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam, cam_b],
            &[xf_a, xf_b],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };

    // Sun toward +x and up: reaches A's floor (14,16,100) only by crossing
    // grid B's wall at world x≈24 → occluded when shadows cast.
    let s = std::f32::consts::FRAC_1_SQRT_2;
    let sun = |casts_shadow: bool| SceneLights {
        enabled: true,
        grid_sun_dirs: vec![[s, 0.0, -s], [s, 0.0, -s]], // both grids identity-rot
        sun_color: [1.0; 3],
        sun_intensity: 3.0,
        sun_casts_shadow: casts_shadow,
        ambient: [0.5; 3],
        shadow_strength: 1.0,
        shadow_bias: 1.5,
        shadow_max_dist: 512.0,
        shadow_max_steps: 256,
        ..SceneLights::default()
    };

    renderer.set_scene_lights(sun(false));
    let lit = render(&mut renderer);
    renderer.set_scene_lights(sun(true));
    let shadowed = render(&mut renderer);

    let blue = |p: u32| (p >> 16) & 0xff;
    assert!(
        blue(lit) < 70 && blue(shadowed) < 70,
        "expected A's floor, not sky: lit={lit:#08x} shadowed={shadowed:#08x}"
    );
    assert!(
        lum(shadowed) < lum(lit),
        "grid B's wall must cast a cross-grid sun shadow on grid A: lit {lit:#08x} -> shadowed {shadowed:#08x}",
    );
}

#[test]
fn scene_dda_fine_occluder_cross_grid_shadow_survives_lod() {
    // SC.4 bug: on the GPU a fine (vws < 1) grid casts NO cross-grid sun
    // shadow when LOD is on (the demo: a mini ship over a coarse planet). The
    // shadow ray marches the occluder at pick_mip(receiver world-t), and a
    // coarse mip of a small fine grid mis-resolves → the shadow vanishes.
    // Repro: an unscaled floor + a fine (vws=0.25) slab above it, rendered
    // with LOD OFF (mip_scan_dist=0) and ON (>0); the shadow must survive.
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let grid_a = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], decompress_chunk(&floor_chunk(vsid)))],
    };
    // Fine slab low in its chunk (local z 8..12); decompress_chunk builds its
    // mip ladder, so LOD can pick a coarse mip for the shadow march.
    let grid_b = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], decompress_chunk(&block_chunk(vsid, 8, 12)))],
    };
    let scene = GpuSceneResident::upload(
        &gpu.device,
        &SceneUpload {
            grids: vec![grid_a, grid_b],
        },
    );

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [14.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let b_origin = [10.0f32, 12.0, 30.0];
    let cam_b = Camera {
        position: [
            cam.position[0] - b_origin[0],
            cam.position[1] - b_origin[1],
            cam.position[2] - b_origin[2],
        ],
        ..cam
    };
    let xf_a = GridWorldTransform::default();
    let xf_b = GridWorldTransform {
        origin: b_origin,
        voxel_world_size: 0.25,
        ..GridWorldTransform::default()
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer, msd: f32| -> u32 {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam, cam_b],
            &[xf_a, xf_b],
            cam.fov_y_rad,
            64,
            msd,
        )[centre]
    };

    let sd = glam::Vec3::new(0.12, 0.0, -0.99).normalize();
    let sun = |casts_shadow: bool| SceneLights {
        enabled: true,
        grid_sun_dirs: vec![[sd.x, sd.y, sd.z], [sd.x, sd.y, sd.z]],
        sun_color: [1.0; 3],
        sun_intensity: 3.0,
        sun_casts_shadow: casts_shadow,
        ambient: [0.5; 3],
        shadow_strength: 1.0,
        shadow_bias: 1.5,
        shadow_max_dist: 512.0,
        shadow_max_steps: 768,
        ..SceneLights::default()
    };

    renderer.set_scene_lights(sun(false));
    let lit = render(&mut renderer, 0.0);
    renderer.set_scene_lights(sun(true));
    let shadowed_lod_off = render(&mut renderer, 0.0);
    let shadowed_lod_on = render(&mut renderer, 8.0);
    eprintln!(
        "fine occluder LOD: lit={}({}) lod_off={}({}) lod_on={}({})",
        lit,
        lum(lit),
        shadowed_lod_off,
        lum(shadowed_lod_off),
        shadowed_lod_on,
        lum(shadowed_lod_on),
    );

    // The fine occluder must cast a cross-grid shadow with LOD off…
    assert!(
        lum(shadowed_lod_off) < lum(lit),
        "fine occluder must cast a shadow (LOD off): lit {lit:#08x} -> {shadowed_lod_off:#08x}"
    );
    // …and it must NOT vanish when LOD is on (the reported bug).
    assert!(
        lum(shadowed_lod_on) < lum(lit),
        "fine occluder shadow must survive LOD (the ship-over-planet bug): \
         lit {lit:#08x} -> lod_on {shadowed_lod_on:#08x}"
    );
}

#[test]
fn scene_dda_scaled_grid_composites_by_world_depth() {
    // SC.4 — GPU per-grid voxel_world_size. Two grids at the same origin,
    // both blocks on the camera column but different scale, so the world and
    // voxel-frame depth metrics DISAGREE:
    //  - Grid A (vws 1.0): RED block at chunk y=4 → world y-near 128.
    //  - Grid B (vws 2.0): BLUE block at chunk y=3 → world y-near 3·32·2 = 192
    //    (FARTHER in world), but voxel-near 96 (< A's 128).
    // Correct (world depth): A (128) occludes B (192) → RED wins the min-t
    // composite. Without the shader's chunk_dim/vsize × vws the marcher would
    // put B at t≈96 < 128 → BLUE would win.
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let red = [0x00u8, 0x00, 0xff, 0x80]; // BGRA → R=0xff
    let blue = [0xffu8, 0x00, 0x00, 0x80]; // BGRA → B=0xff
    let grid_a = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 8, 1],
        pool_dims: [1, 8, 1],
        chunks: vec![(
            [0, 4, 0],
            decompress_chunk(&block_chunk_bgra(vsid, 0, 31, red)),
        )],
    };
    let grid_b = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 8, 1],
        pool_dims: [1, 8, 1],
        chunks: vec![(
            [0, 3, 0],
            decompress_chunk(&block_chunk_bgra(vsid, 0, 31, blue)),
        )],
    };
    let scene = GpuSceneResident::upload(
        &gpu.device,
        &SceneUpload {
            grids: vec![grid_a, grid_b],
        },
    );

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // World camera at (16,0,16) looking +y. Both grids sit at origin (0,0,0),
    // so each per-grid (world-local) camera equals the world camera.
    let cam = Camera {
        position: [16.0, 0.0, 16.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, 1.0, 0.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let xf_a = GridWorldTransform::default(); // vws 1.0
    let xf_b = GridWorldTransform {
        voxel_world_size: 2.0,
        ..GridWorldTransform::default()
    };
    let fb = renderer.render_with_transforms(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam, cam],
        &[xf_a, xf_b],
        cam.fov_y_rad,
        64,
        0.0,
    );
    let centre = (h / 2 * w + w / 2) as usize;
    let p = fb[centre];
    let (r, b) = (p & 0xff, (p >> 16) & 0xff);
    // Shading scales all channels uniformly, so the R>B ratio is preserved.
    assert!(
        r > b,
        "the world-nearer vws=1 RED grid must win the min-t composite; BLUE \
         here means grid B's vws=2 depth (chunk_dim/vsize × vws) wasn't applied: {p:#08x}"
    );
}

#[test]
fn scene_dda_scaled_grid_depth_is_world() {
    // SC.4 — the GPU depth buffer stores `best_t` in WORLD units even for a
    // scaled grid, so it agrees with the CPU compose depth. This is the GPU
    // half of the CPU-vs-GPU depth parity: the CPU test
    // `sc1_scaled_grid_depth_is_world` (roxlap-scene) asserts its zbuffer at a
    // world VALUE, and this asserts the GPU depth at a world value too — both
    // world ⇒ they agree. A vws=2 grid whose block near-face is at world
    // y=128 must read depth 128, not the voxel-frame 64.
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // Block chunk at index y=2 → voxel y 64.. → world y 128.. (× vws 2).
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 4, 1],
        pool_dims: [1, 4, 1],
        chunks: vec![([0, 2, 0], decompress_chunk(&block_chunk(vsid, 0, 31)))],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // World camera at (32,0,32) looking +y; centre ray hits the block interior
    // (voxel x=16, z=16 under vws=2). Grid at origin ⇒ per-grid cam == world.
    let cam = Camera {
        position: [32.0, 0.0, 32.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, 1.0, 0.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let xf = GridWorldTransform {
        voxel_world_size: 2.0,
        ..GridWorldTransform::default()
    };
    let depth = renderer.render_depth_with_transforms(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        &[xf],
        cam.fov_y_rad,
        64,
        0.0,
    );
    let centre = (h / 2 * w + w / 2) as usize;
    let d = depth[centre];
    assert!(
        d.is_finite(),
        "centre ray must hit the scaled block, got {d}"
    );
    // Near face: chunk 2 · vsid 32 · vws 2 = world y 128; camera at y=0.
    assert!(
        (d - 128.0).abs() <= 4.0,
        "GPU depth must be WORLD (128); {d} ≈ 64 would mean the voxel-frame \
         t wasn't scaled by vws"
    );
}

#[test]
fn scene_dda_fine_scaled_grid_composites_by_world_depth() {
    // SC.4 — the vws<1 (fine grid) mirror of the coarse test, for symmetry.
    //  - Grid A (vws 1.0): RED block at chunk y=3 → world y-near 96.
    //  - Grid B (vws 0.5): BLUE block at chunk y=4 → world y-near 4·32·0.5 = 64
    //    (NEARER in world), but voxel-near 128 (> A's 96).
    // Correct (world depth): B (64) occludes A (96) → BLUE wins. Without the
    // × vws the marcher would put B at t≈128 > 96 → RED would win.
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let red = [0x00u8, 0x00, 0xff, 0x80];
    let blue = [0xffu8, 0x00, 0x00, 0x80];
    let grid_a = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 8, 1],
        pool_dims: [1, 8, 1],
        chunks: vec![(
            [0, 3, 0],
            decompress_chunk(&block_chunk_bgra(vsid, 0, 31, red)),
        )],
    };
    let grid_b = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 8, 1],
        pool_dims: [1, 8, 1],
        chunks: vec![(
            [0, 4, 0],
            decompress_chunk(&block_chunk_bgra(vsid, 0, 31, blue)),
        )],
    };
    let scene = GpuSceneResident::upload(
        &gpu.device,
        &SceneUpload {
            grids: vec![grid_a, grid_b],
        },
    );

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Camera at (8,0,8): inside both blocks' world x/z spans (grid B's
    // vws=0.5 block only reaches world 16 in x and z).
    let cam = Camera {
        position: [8.0, 0.0, 8.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, 1.0, 0.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let xf_a = GridWorldTransform::default(); // vws 1.0
    let xf_b = GridWorldTransform {
        voxel_world_size: 0.5,
        ..GridWorldTransform::default()
    };
    let fb = renderer.render_with_transforms(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam, cam],
        &[xf_a, xf_b],
        cam.fov_y_rad,
        64,
        0.0,
    );
    let centre = (h / 2 * w + w / 2) as usize;
    let p = fb[centre];
    let (r, b) = (p & 0xff, (p >> 16) & 0xff);
    assert!(
        b > r,
        "the world-nearer vws=0.5 BLUE grid must win the min-t composite; RED \
         here means grid B's vws=0.5 depth wasn't applied: {p:#08x}"
    );
}

/// `vsid × vsid` chunk: every column carries an `nvox`-tall coloured
/// slab whose top sits at `surf` — so the chunk's mip-0 colour count is
/// `vsid² · nvox`. With `vsid = 128, nvox = 8` that's 131072 colours,
/// 2× the per-chunk GPU colour stride — the dense-chunk case the cave
/// demo hits (a fully solid 128×128×256 cave chunk).
fn dense_floor_chunk(vsid: u32, surf: u8, nvox: u8) -> Vxl {
    let n_cols = (vsid as usize) * (vsid as usize);
    let bot = surf + nvox - 1;
    let mut data: Vec<u8> = Vec::with_capacity(n_cols * (4 + nvox as usize * 4));
    let mut column_offset: Vec<u32> = Vec::with_capacity(n_cols + 1);
    let bgra = [0x00u8, 0x80, 0xff, 0x80];
    for _ in 0..n_cols {
        column_offset.push(u32::try_from(data.len()).expect("offset fits"));
        data.extend_from_slice(&[0, surf, bot, 0]);
        for _ in 0..nvox {
            data.extend_from_slice(&bgra);
        }
    }
    column_offset.push(u32::try_from(data.len()).expect("offset fits"));
    Vxl {
        vsid,
        ipo: [0.0; 3],
        ist: [1.0, 0.0, 0.0],
        ihe: [0.0, 0.0, 1.0],
        ifo: [0.0, 1.0, 0.0],
        data: data.into_boxed_slice(),
        column_offset: column_offset.into_boxed_slice(),
        mip_base_offsets: Box::new([0, n_cols + 1]),
        vbit: Box::new([]),
        vbiti: 0,
    }
}

/// Regression for the cave-demo "half the map is black" report: a chunk
/// whose colour count exceeds the per-chunk GPU stride must NOT have its
/// colours truncated. Columns are stored in `y·vsid + x` order, so a
/// truncated tail blacks out the high-`y` spatial half of the chunk.
///
/// A dense top-faced floor is viewed straight down; with the bug the
/// bottom half of the frame (world `y ≥ vsid/2`, the truncated columns)
/// renders black instead of the floor colour.
#[test]
fn scene_dda_dense_chunk_colours_not_truncated() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 128u32;
    let chunk = decompress_chunk(&dense_floor_chunk(vsid, 100, 8));
    // Precondition: this chunk really does exceed the per-chunk stride,
    // so the test exercises the truncation path (not a no-op).
    assert!(
        chunk.mips[0].colors.len() > roxlap_gpu::scene::COLORS_PER_CHUNK_WORDS as usize,
        "test chunk must exceed the colour stride to exercise truncation \
         ({} colours)",
        chunk.mips[0].colors.len()
    );
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Inside the chunk, above the floor (z=2 < 100), looking straight
    // down (+z). Screen-down maps to world +y, so the high-y (truncated)
    // columns land in the bottom half of the frame.
    let cam = Camera {
        position: [vsid as f32 * 0.5, vsid as f32 * 0.5, 2.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let fb = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        cam.fov_y_rad,
        64,
        0.0,
    );

    // Count floor-colour pixels in the top half (low world-y, never
    // truncated) vs the bottom half (high world-y, truncated by the bug).
    let half = (h / 2) as usize;
    let count = |rows: std::ops::Range<usize>| {
        let mut n = 0;
        for y in rows {
            for x in 0..w as usize {
                if is_block_color(fb[y * w as usize + x]) {
                    n += 1;
                }
            }
        }
        n
    };
    let top = count(0..half);
    let bottom = count(half..h as usize);
    eprintln!("dense_chunk: top-half floor px {top}, bottom-half floor px {bottom}");
    assert!(top > 0, "top half should show the floor, got {top}");
    // The bottom half is the truncated spatial half — it must still
    // render the floor, not black.
    assert!(
        bottom * 2 > top,
        "bottom (high-y) half is mostly black — colours were truncated \
         (top {top} vs bottom {bottom})",
    );
}

/// Lifting the 16-grid cap moved the per-grid cameras out of a fixed
/// `array<…, 16>` uniform and into a runtime-sized storage buffer
/// (binding 15). This guards that grid `g` marches with **its own**
/// `grid_cameras[g]`, not `cameras[0]` for every grid. Two identical
/// floor grids get OPPOSITE cameras: grid 0 looks up (away → sky), grid
/// 1 looks down (at the floor). The floor can only appear if grid 1's
/// own camera was used; a control with both cameras up must be pure sky.
#[test]
fn scene_dda_per_grid_cameras_are_independent() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let mk_floor = || GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], decompress_chunk(&floor_chunk(vsid)))],
    };
    let scene = GpuSceneResident::upload(
        &gpu.device,
        &SceneUpload {
            grids: vec![mk_floor(), mk_floor()],
        },
    );
    assert_eq!(scene.grid_count, 2, "two-grid scene");

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Above the floor (z=50 < 100). Down = +z (voxlap z-down) hits it;
    // up = −z looks at empty space → sky.
    let cam_down = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let cam_up = Camera {
        forward: [0.0, 0.0, -1.0],
        ..cam_down
    };
    let fov = cam_down.fov_y_rad;

    // grid 0 looks away (sky), grid 1 looks at its floor.
    let fb = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam_up, cam_down],
        fov,
        64,
        0.0,
    );
    let floor_px = fb.iter().filter(|&&p| is_block_color(p)).count();
    assert!(
        floor_px > 0,
        "grid 1's floor must be visible via grid_cameras[1] — got {floor_px} floor px \
         (per-grid camera indexing broken?)",
    );

    // Control: BOTH grids look away → pure sky. If the shader read a
    // stale/shared camera this would disagree with the result above.
    let fb2 = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam_up, cam_up],
        fov,
        64,
        0.0,
    );
    let floor_px2 = fb2.iter().filter(|&&p| is_block_color(p)).count();
    assert_eq!(
        floor_px2, 0,
        "both grids looking away must be all sky — got {floor_px2} floor px",
    );
}

#[test]
fn aabb_tracks_streaming_refresh_and_evict() {
    // GPU.13.0 — the early-out box is maintained live: installing a
    // chunk at a new index must GROW the AABB (so the shader never
    // skips streamed-in terrain), and evicting it must SHRINK it back.
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [8, 8, 1], // room for streamed indices
        chunks: vec![([0, 0, 0], decompress_chunk(&block_chunk(vsid, 0, 31)))],
    };
    let mut scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });
    assert_eq!(scene.static_meta[0].aabb_min, [0, 0, 0]);
    assert_eq!(scene.static_meta[0].aabb_max, [0, 0, 0]);

    // Stream in a far chunk at (3, 2, 0) → AABB grows to cover it.
    let far = decompress_chunk(&block_chunk(vsid, 0, 31));
    scene.refresh_chunk(&gpu.queue, 0, [3, 2, 0], &far);
    assert_eq!(scene.static_meta[0].aabb_min, [0, 0, 0]);
    assert_eq!(scene.static_meta[0].aabb_max, [3, 2, 0]);

    // Evict it → AABB shrinks back to the lone origin chunk.
    scene.evict_chunk(&gpu.queue, 0, [3, 2, 0]);
    assert_eq!(scene.static_meta[0].aabb_min, [0, 0, 0]);
    assert_eq!(scene.static_meta[0].aabb_max, [0, 0, 0]);
}

#[test]
fn scene_dda_renders_bedrock_wall_face_solid() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    eprintln!("wall_render: adapter = {}", gpu.adapter_info);

    // A wall chunk: textured top at z=40, bedrock 41..255 below. Place
    // it far along +y; the camera looks at its face from BELOW the
    // textured top (z=128, deep in the bedrock region) — exactly the
    // cliff-face view that pre-fix showed sky through.
    let vsid = 32u32;
    let chunk = decompress_chunk(&wall_chunk(vsid, 40));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 8, 1],
        pool_dims: [1, 8, 1],
        chunks: vec![([0, 4, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [vsid as f32 * 0.5, 0.0, 128.0], // z=128 = bedrock region
        right: [1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, 1.0, 0.0],
        fov_y_rad: 30f32.to_radians(),
    };
    let fb = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        cam.fov_y_rad,
        64,
        0.0,
    );
    let centre = fb[(h / 2 * w + w / 2) as usize];
    eprintln!("wall_render: centre pixel = {centre:#08x}");
    // The bedrock face must be SOLID and inherit the surface colour
    // (was sky before the bedrock-as-solid fix).
    assert!(
        is_block_color(centre),
        "bedrock wall face should be solid surface colour, got {centre:#08x} (sky = regression)",
    );
}

#[test]
fn scene_dda_renders_floor_through_mip_layout() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    eprintln!("scene_render: adapter = {}", gpu.adapter_info);

    let vsid = 64u32;
    let chunk = decompress_chunk(&floor_chunk(vsid));
    // Sanity: the mip ladder was built (GPU.11.0 plumbing).
    assert!(chunk.mips.len() >= 2, "expected a mip ladder");

    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });
    eprintln!("scene_render: resident {} bytes", scene.resident_bytes());

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);

    // Camera at the chunk's XY centre, above the floor (small z),
    // looking straight down (+z). right × down == forward (RH).
    let cam = Camera {
        position: [vsid as f32 * 0.5, vsid as f32 * 0.5, 20.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 30f32.to_radians(),
    };
    let fb = renderer.render(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        30f32.to_radians(),
        64,
        0.0, // mip_scan_dist=0 → always mip-0
    );
    assert_eq!(fb.len(), (w * h) as usize);

    // Centre pixel: the down-ray hits the z=100 floor voxel of the
    // centre column. Expected colour = (R,G,B) * (alpha/128) / 255
    // → (255,128,0) at brightness 1.0 → rgba8 ≈ (255,128,0).
    let centre = fb[(h / 2 * w + w / 2) as usize];
    let (r, g, b) = (centre & 0xff, (centre >> 8) & 0xff, (centre >> 16) & 0xff);
    eprintln!("scene_render: centre pixel = ({r}, {g}, {b})");
    assert!(r > 200, "floor R should be ~255, got {r}");
    assert!((100..=160).contains(&g), "floor G should be ~128, got {g}");
    assert!(b < 40, "floor B should be ~0, got {b}");

    // The floor fills the frame at this near-vertical view; assert a
    // solid majority of pixels are floor-coloured (not sky / clear),
    // proving the strided mip-0 lookup works across the whole image.
    let floor_px = fb
        .iter()
        .filter(|&&p| {
            let (r, g, b) = (p & 0xff, (p >> 8) & 0xff, (p >> 16) & 0xff);
            r > 200 && (90..=170).contains(&g) && b < 50
        })
        .count();
    let frac = floor_px as f32 / fb.len() as f32;
    eprintln!("scene_render: floor fraction = {frac:.3}");
    assert!(frac > 0.6, "expected floor to fill the view, got {frac:.3}");
}

#[test]
fn hierarchical_skip_hits_far_chunk_and_tracks_evict() {
    // GPU.13.1 — a ray crossing a long run of EMPTY chunks (stepped
    // read-free under the chunk-occupancy pyramid) must still enter
    // the far occupied chunk exactly — the integer block test cannot
    // overshoot it. Evicting the chunk must clear the pyramid (same
    // view → pure sky), and re-installing must set it again.
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&block_chunk(vsid, 0, 31));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 8, 1],
        pool_dims: [1, 8, 1],
        chunks: vec![([0, 7, 0], chunk.clone())], // 7 empty chunks in front
    };
    let mut scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });
    // Pool [1,8,1] → 3 pyramid levels above L0.
    assert_eq!(scene.static_meta[0].chunk_occ_levels, 3);

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [vsid as f32 * 0.5, 8.0, 16.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, 1.0, 0.0], // toward the far chunk at +y
        fov_y_rad: 30f32.to_radians(),
    };
    let render = |scene: &GpuSceneResident| {
        let fb = renderer.render(
            &gpu.device,
            &gpu.queue,
            scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        );
        fb.iter().filter(|&&p| is_block_color(p)).count()
    };

    let visible = render(&scene);
    assert!(
        visible > 0,
        "far chunk must render through the skipped empty run"
    );

    scene.evict_chunk(&gpu.queue, 0, [0, 7, 0]);
    // The maintenance path re-ORed the ancestors down to all-empty.
    assert!(
        scene.chunk_occ_pyramid_shadow()[0]
            .iter()
            .all(|lvl| lvl.iter().all(|&w| w == 0)),
        "evicting the only chunk empties every pyramid level"
    );
    assert_eq!(render(&scene), 0, "evicted chunk leaves pure sky");

    scene.refresh_chunk(&gpu.queue, 0, [0, 7, 0], &chunk);
    assert_eq!(
        render(&scene),
        visible,
        "re-installed chunk renders exactly as before"
    );
}

/// EV — headless material plumbing + GPU emissive parity: an emissive
/// terrain mapping renders full-bright through the real `scene_dda.wgsl`
/// pipeline, matching the CPU `emissive_shade` ladder exactly, and is
/// independent of both the baked byte and a hostile dynamic rig. Also
/// gates: an empty map re-renders byte-identically to the pre-material
/// baseline (the fast path stays byte-exact).
#[test]
fn scene_dda_emissive_ignores_lighting() {
    use roxlap_formats::material::{Material, MaterialTable};
    use roxlap_formats::Rgb;

    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // A **dim** floor at z=100: baked byte 0x40 ⇒ the baked path renders at
    // half albedo, so full-bright emissive is unmistakable. BGRA, R=0xff.
    let chunk = decompress_chunk(&block_chunk_bgra(vsid, 100, 100, [0x00, 0x80, 0xff, 0x40]));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            cam.fov_y_rad,
            64,
            0.0,
        )
    };
    // Readback is 0xAABBGGRR — R in the low byte.
    let rgb = |p: u32| (p & 0xff, (p >> 8) & 0xff, (p >> 16) & 0xff);

    // Baseline: the dim baked floor (≈ half of R=0xff / G=0x80).
    let baked_fb = render(&mut renderer);
    let (br, bg, _) = rgb(baked_fb[centre]);
    assert!(
        (100..=160).contains(&br) && bg < 90,
        "baked floor should be dim orange: {:#010x}",
        baked_fb[centre]
    );

    // Map the floor colour to an opaque **emissive** material: the centre
    // must hit the exact CPU ladder value — emissive_shade(0xff8000, 255)
    // = (255, 255, 0) — ignoring the dim baked byte.
    let mut table = MaterialTable::new();
    table.set(1, Material::OPAQUE.with_emissive(255));
    renderer.set_terrain_materials(&table, &[(Rgb(0x00ff_8000), 1)]);
    let glow = render(&mut renderer)[centre];
    assert_eq!(
        rgb(glow),
        (255, 255, 0),
        "emissive must ignore the baked byte and match the CPU ladder: {glow:#010x}"
    );

    // A hostile rig (zero ambient, back-facing sun) blacks out a plain
    // floor but must not touch the emissive one.
    renderer.set_scene_lights(SceneLights {
        enabled: true,
        grid_sun_dirs: vec![[0.0, 0.0, 1.0]], // to-sun below: N·L = 0 on top
        sun_color: [1.0; 3],
        sun_intensity: 1.0,
        ambient: [0.0; 3],
        ..SceneLights::default()
    });
    let glow_lit = render(&mut renderer)[centre];
    assert_eq!(
        rgb(glow_lit),
        (255, 255, 0),
        "emissive must outrank the dynamic rig: {glow_lit:#010x}"
    );
    renderer.set_terrain_materials(&table, &[]); // gate off again
    let (dr, dg, db) = rgb(render(&mut renderer)[centre]);
    assert!(
        dr == 0 && dg == 0 && db == 0,
        "zero rig must black out the plain floor: ({dr},{dg},{db})"
    );

    // Byte-exactness gate: with the rig off and an empty map the whole
    // frame is identical to the pre-material baseline.
    renderer.set_scene_lights(SceneLights::default());
    assert_eq!(
        render(&mut renderer),
        baked_fb,
        "empty material map must re-render byte-identically"
    );
}

/// CA.3 — z-graded [`block_chunk`]: solid over `z ∈ [top, bot]`, each
/// voxel's stored BLUE byte = its z (R=0xff, G=0x00, brightness 0x80 →
/// exact colour passthrough), so a render pins EXACTLY which voxel
/// layer produced a pixel.
fn graded_block_chunk(vsid: u32, top: u8, bot: u8) -> Vxl {
    let n_cols = (vsid as usize) * (vsid as usize);
    let n_vox = (bot - top + 1) as usize;
    let mut data: Vec<u8> = Vec::with_capacity(n_cols * (4 + n_vox * 4));
    let mut column_offset: Vec<u32> = Vec::with_capacity(n_cols + 1);
    for _ in 0..n_cols {
        column_offset.push(u32::try_from(data.len()).expect("offset fits"));
        data.extend_from_slice(&[0, top, bot, 0]);
        for z in top..=bot {
            data.extend_from_slice(&[z, 0x00, 0xff, 0x80]); // BGRA, B = z
        }
    }
    column_offset.push(u32::try_from(data.len()).expect("offset fits"));
    Vxl {
        vsid,
        ipo: [0.0; 3],
        ist: [1.0, 0.0, 0.0],
        ihe: [0.0, 0.0, 1.0],
        ifo: [0.0, 1.0, 0.0],
        data: data.into_boxed_slice(),
        column_offset: column_offset.into_boxed_slice(),
        mip_base_offsets: Box::new([0, n_cols + 1]),
        vbit: Box::new([]),
        vbiti: 0,
    }
}

/// CA.3 — cutaway parity gate, primary rays: the per-grid clip lane
/// hides `z < z_clip` through the real `scene_dda.wgsl` pipeline, the
/// cut face shows EXACTLY the voxel layer at the plane (matching the
/// CPU sampler's stored-colour/run-top rule — the fixture stores a
/// z-graded colour per voxel, so any off-by-one or wrong-layer fetch
/// changes the byte), a fully-clipped grid is all sky, and a `None`
/// clip re-renders byte-identically to the unclipped baseline.
#[test]
fn scene_dda_cutaway_clips_and_pins_cut_face_colour() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // Solid block z ∈ [100, 140], blue byte = z.
    let chunk = decompress_chunk(&graded_block_chunk(vsid, 100, 140));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let render = |r: &mut HeadlessSceneRenderer, clip: Option<i32>| {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            &[GridWorldTransform {
                z_clip: clip,
                ..GridWorldTransform::default()
            }],
            cam.fov_y_rad,
            64,
            0.0,
        )
    };
    // Readback is 0xAABBGGRR — R low byte, B bits 16..23.
    let rgb = |p: u32| (p & 0xff, (p >> 8) & 0xff, (p >> 16) & 0xff);

    // Unclipped: the top surface at z=100 (blue byte 100).
    let base = render(&mut renderer, None);
    assert_eq!(
        rgb(base[centre]),
        (255, 0, 100),
        "unclipped top face must be the z=100 layer: {:#010x}",
        base[centre]
    );
    // Clip mid-run: the cut face is EXACTLY the z=120 layer.
    let cut = render(&mut renderer, Some(120));
    assert_eq!(
        rgb(cut[centre]),
        (255, 0, 120),
        "cut face must be the z=120 layer: {:#010x}",
        cut[centre]
    );
    // Pixel classification: clipping only REMOVES geometry, so every
    // block pixel of the cut render must be a block pixel of the base
    // render too (edge rays that grazed the block's side above the
    // plane legitimately become sky — the reverse never happens).
    for (i, (&b, &c)) in base.iter().zip(cut.iter()).enumerate() {
        assert!(
            (b & 0xff) > 180 || (c & 0xff) <= 180,
            "pixel {i} was sky and became block: base={b:#010x} cut={c:#010x}"
        );
    }
    // Clip just past the block's bottom: voxlap columns are BEDROCK
    // below the last slab, so the cut face is the (colourless) bedrock
    // layer at z=141 — the colour fetch falls back to the nearest
    // stored colour, the run's z=140 byte. Pins the interior-fallback
    // rule the CPU sampler uses (`surface_color_mip` run-top/bottom).
    let bedrock = render(&mut renderer, Some(141));
    assert_eq!(
        rgb(bedrock[centre]),
        (255, 0, 140),
        "bedrock cut face must fall back to the run's stored colour: {:#010x}",
        bedrock[centre]
    );
    // Clip past the chunk's full depth: nothing left — all sky.
    let gone = render(&mut renderer, Some(256));
    assert!(
        gone.iter().all(|&p| (p & 0xff) <= 180),
        "clip=256 must hide the entire chunk"
    );
    // Standing gate: clip=None is byte-identical to the baseline.
    let none_again = render(&mut renderer, None);
    assert_eq!(
        none_again, base,
        "z_clip=None must re-render byte-identically"
    );
}

/// CA.3 — the GPU clip uses the CPU's exact `z_clip >> mip` FLOOR
/// formula. Two 1-voxel plates with real air between them (P at z=100,
/// Q at z=140) and `z_clip = 101`: at mip 0 plate P (z=100 < 101) is
/// hidden and the ray reaches Q; at any coarse mip the odd plane
/// floors onto P's cell (`100 >> m == 101 >> m` for m ≥ 1) so P pokes
/// through — the accepted coarse-mip bleed. A round-up formula would
/// show Q at BOTH mips; no `>> mip` at all would hide P at mip 0 too.
/// The plates' blue bytes (100 vs 140) name the winning layer exactly.
#[test]
fn scene_dda_cutaway_mip_formula_floors() {
    use roxlap_formats::color::VoxColor;

    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // B = z for each plate (BGRA upload order is handled by from_dense).
    let vxl = Vxl::from_dense(vsid, |_, _, z| match z {
        100 => Some(VoxColor(0x80ff_0064)), // P: R=0xff, B=100
        140 => Some(VoxColor(0x80ff_008c)), // Q: R=0xff, B=140
        _ => None,
    });
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], decompress_chunk(&vxl))],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let centre = (h / 2 * w + w / 2) as usize;
    let xf = GridWorldTransform {
        z_clip: Some(101),
        ..GridWorldTransform::default()
    };
    // Camera OUTSIDE the chunk (z = -200), so the chunk is entered at
    // t ≈ 200 and `mip_scan_dist` alone dictates the marched mip.
    let cam = Camera {
        position: [16.0, 16.0, -200.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 20f32.to_radians(),
    };
    let render_at = |mip_scan_dist: f32| {
        renderer.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            &[xf],
            cam.fov_y_rad,
            64,
            mip_scan_dist,
        )[centre]
    };
    let blue = |p: u32| (p >> 16) & 0xff;
    // LOD off → mip 0 → P hidden, the ray lands on Q (blue 140).
    let p_mip0 = render_at(0.0);
    assert_eq!(
        blue(p_mip0),
        140,
        "mip 0: clip=101 must hide plate P and hit Q: {p_mip0:#010x}"
    );
    // mip ≥ 1 (t_enter ≈ 200 ≥ 2·mip_scan_dist): the floored plane
    // exposes P's cell — blue must come from P's layer, far below Q's.
    let p_coarse = render_at(100.0);
    assert!(
        blue(p_coarse) < 120,
        "coarse mip: floor formula must expose plate P: {p_coarse:#010x}"
    );
}

/// CA.3 — cutaway shadow parity: a clipped-away wall stops casting sun
/// shadow on the floor next to it (the shadow march applies the same
/// per-grid clip as the primary rays — "world as if removed").
#[test]
fn scene_dda_cutaway_hidden_wall_stops_shadowing() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // Floor at z=100 + wall x ∈ [16,18) rising z ∈ [90, 100].
    let chunk = decompress_chunk(&floor_with_wall_chunk(vsid, 16, 18, 90));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [14.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer, clip: Option<i32>| {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            &[GridWorldTransform {
                z_clip: clip,
                ..GridWorldTransform::default()
            }],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };
    // Sun toward +x and up: the wall at x ∈ [16,18) occludes the floor
    // point (14,16,100) — the to-sun ray crosses it at z ≈ 96..94.
    let s = std::f32::consts::FRAC_1_SQRT_2;
    renderer.set_scene_lights(SceneLights {
        enabled: true,
        grid_sun_dirs: vec![[s, 0.0, -s]],
        sun_color: [1.0; 3],
        sun_intensity: 3.0,
        sun_casts_shadow: true,
        ambient: [0.5; 3],
        shadow_strength: 1.0,
        shadow_bias: 1.5,
        shadow_max_dist: 512.0,
        shadow_max_steps: 256,
        ..SceneLights::default()
    });
    let shadowed = render(&mut renderer, None);
    // Clip at z=100: the wall body (z 90..99) vanishes, the floor layer
    // itself (z=100) stays visible AND sun-lit.
    let unshadowed = render(&mut renderer, Some(100));
    let blue = |p: u32| (p >> 16) & 0xff;
    assert!(
        blue(shadowed) < 70 && blue(unshadowed) < 70,
        "both renders must show the floor, not sky: {shadowed:#010x} / {unshadowed:#010x}"
    );
    assert!(
        lum(unshadowed) > lum(shadowed),
        "a clipped-away wall must stop shadowing: shadowed {shadowed:#010x} -> clipped {unshadowed:#010x}",
    );
}

/// CA follow-up — cross-grid sun shadow under a TELE camera (the deck
/// view): a hull grid must darken the ground grid beside it even when
/// the receiver sits ~1150 world units from the eye. Regression for
/// the Decks report "GPU shows no hull shadow" — the CPU backend
/// renders it, so a miss here is a backend divergence.
#[test]
fn scene_dda_cross_grid_shadow_survives_tele_distance() {
    use roxlap_formats::color::VoxColor;

    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 128u32;
    // Ground grid: plate z ∈ [420, 440] (deep in chunk z=1 → the
    // upload uses chunk (0,0,1) like the demo).
    let ground_vxl = Vxl::from_dense(vsid, |_, _, z| {
        (164..=184).contains(&z).then_some(VoxColor(0x80_4A_5E_3C))
    });
    // The demo's ground plate lives at z 420..440 = chunk 1, local 164..184.
    let ground = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [2, 1, 2],
        pool_dims: [2, 1, 2],
        chunks: vec![
            ([0, 0, 1], decompress_chunk(&ground_vxl)),
            ([1, 0, 1], decompress_chunk(&ground_vxl)),
        ],
    };
    // Hull grid: a HOLLOW box (roof plate + perimeter walls + deck
    // floors, like the demo shiplet) x 24..104, y 32..96, z 240..313,
    // split over stacked chunks (0,0,0) + (0,0,1), world origin z=106.
    let hull_at = |x: u32, y: u32, z: i32| -> bool {
        let inside = (24..104).contains(&x) && (32..96).contains(&y) && (240..=313).contains(&z);
        if !inside {
            return false;
        }
        let roof = z <= 241;
        let wall = x <= 25 || x >= 102 || y <= 33 || y >= 94;
        let floor = matches!(z, 259..=262 | 283..=286 | 307..=310 | 311..=313);
        roof || wall || floor
    };
    let hull_c0 = Vxl::from_dense(vsid, |x, y, z| {
        hull_at(x, y, i32::try_from(z).unwrap()).then_some(VoxColor(0x80_62_66_70))
    });
    let hull_c1 = Vxl::from_dense(vsid, |x, y, z| {
        hull_at(x, y, i32::try_from(z).unwrap() + 256).then_some(VoxColor(0x80_62_66_70))
    });
    let ship = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 2],
        pool_dims: [1, 1, 2],
        chunks: vec![
            ([0, 0, 0], decompress_chunk(&hull_c0)),
            ([0, 0, 1], decompress_chunk(&hull_c1)),
        ],
    };
    let scene = GpuSceneResident::upload(
        &gpu.device,
        &SceneUpload {
            grids: vec![ground, ship],
        },
    );

    let (w, h) = (128u32, 128u32);
    let renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Tele camera: straight down from ~1150 world units, framing the
    // shadow band EAST of the hull (sun travel [0.45, 0.35, 0.82] ⇒
    // the hull's shadow falls on its +x/+y side, extending ~40 voxels
    // past the wall from the 74-voxel drop to the ground).
    let cam = Camera {
        position: [120.0, 70.0, 420.0 - 1150.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 0.15,
    };
    let cam_ship = Camera {
        position: [cam.position[0], cam.position[1], cam.position[2] - 106.0],
        ..cam
    };
    let xf_ground = GridWorldTransform::default();
    let xf_ship = GridWorldTransform {
        origin: [0.0, 0.0, 106.0],
        ..GridWorldTransform::default()
    };
    let mut r = renderer;
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam, cam_ship],
            &[xf_ground, xf_ship],
            cam.fov_y_rad,
            64,
            640.0, // the demo's tele LOD override
        )
    };
    let s = |casts: bool| SceneLights {
        enabled: true,
        grid_sun_dirs: vec![[-0.45, -0.35, -0.82], [-0.45, -0.35, -0.82]],
        sun_color: [1.0, 0.95, 0.85],
        sun_intensity: 1.1,
        sun_casts_shadow: casts,
        ambient: [0.4, 0.42, 0.48],
        shadow_strength: 0.8,
        shadow_bias: 1.5,
        shadow_max_dist: 200.0,
        shadow_max_steps: 768,
        ..SceneLights::default()
    };
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let sum = |fb: &[u32]| fb.iter().map(|&p| u64::from(lum(p))).sum::<u64>();
    r.set_scene_lights(s(false));
    let lit = render(&mut r);
    r.set_scene_lights(s(true));
    let shadowed = render(&mut r);
    // Compare total luminance over the frame: with the hull shadow
    // present a visible patch of ground darkens.
    let (l0, l1) = (sum(&lit), sum(&shadowed));
    assert!(
        l1 < l0 && (l0 - l1) * 100 > l0,
        "hull must cast a visible cross-grid shadow at tele distance: \
         lit {l0} shadowed {l1}"
    );
}

/// OC.2 — keyhole parity gate, primary rays: the uniform cone +
/// per-grid focus-plane lane cut the front wall through the real
/// `scene_dda.wgsl` pipeline exactly like the CPU keyhole — cone
/// centre revealed, outside-cone column intact, cells at/below the
/// focus plane intact — and both "off" encodings re-render
/// byte-identically.
#[test]
fn scene_dda_cutout_reveals_wall_inside_window_only() {
    use roxlap_formats::color::VoxColor;

    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // Front wall at y=8, room back wall at y=24, both spanning x/z.
    const WALL: (u32, u32, u32) = (0xC0, 0x40, 0x40);
    const BACK: (u32, u32, u32) = (0x40, 0xC0, 0x40);
    let vxl = Vxl::from_dense(vsid, |_, y, _| match y {
        8 => Some(VoxColor(0x80_C0_40_40)),
        24 => Some(VoxColor(0x80_40_C0_40)),
        _ => None,
    });
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], decompress_chunk(&vxl))],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Camera at y=2 looking +y (right × down == forward): wall at
    // world-t ≈ 6, back wall at ≈ 22, centre ray at grid z = 16.
    let cam = Camera {
        position: [16.0, 2.0, 16.0],
        right: [-1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, 1.0, 0.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let outside = (h / 2 * w + 4) as usize; // 28.5 px from the centre
    let render = |r: &mut HeadlessSceneRenderer, focus_z: i32| {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            &[GridWorldTransform {
                cutout_focus_z: focus_z,
                cutout_focus_local: [16.0, 25.0, 16.0],
                ..GridWorldTransform::default()
            }],
            cam.fov_y_rad,
            64,
            0.0,
        )
    };
    let rgb = |p: u32| (p & 0xff, (p >> 8) & 0xff, (p >> 16) & 0xff);

    // Baseline: no cutout → the front wall everywhere.
    let base = render(&mut renderer, i32::MIN);
    assert_eq!(
        rgb(base[centre]),
        WALL,
        "base centre: {:#010x}",
        base[centre]
    );

    // Cone down the +y view axis, hard edge, reveal past the wall
    // (cell dist ≈ 6.5) but short of the back wall (≈ 22.5); focus
    // plane below the centre ray's grid z (16 < 100).
    renderer.set_view_cutout(Some(GpuViewCutout {
        tan_outer: 0.2,
        tan_inner: 0.2,
        margin: 2.0,
    }));
    let cut = render(&mut renderer, 100);
    assert_eq!(
        rgb(cut[centre]),
        BACK,
        "cone centre must see through the wall: {:#010x}",
        cut[centre]
    );
    assert_eq!(
        rgb(cut[outside]),
        WALL,
        "outside the cone the wall must stay: {:#010x}",
        cut[outside]
    );
    // Focus plane above the centre ray's z (16 ≮ 10): the wall stays
    // even inside the cone — the floor-in-front rule.
    let below = render(&mut renderer, 10);
    assert_eq!(
        rgb(below[centre]),
        WALL,
        "cells at/below the focus plane must stay: {:#010x}",
        below[centre]
    );
    // Off encodings: a cleared cutout AND a margin larger than the
    // scene are both byte-identical to the baseline (decision 8).
    renderer.set_view_cutout(None);
    assert_eq!(
        render(&mut renderer, i32::MIN),
        base,
        "cleared cutout must be byte-identical"
    );
    renderer.set_view_cutout(Some(GpuViewCutout {
        tan_outer: 10.0,
        tan_inner: 10.0,
        margin: 1.0e6,
    }));
    assert_eq!(
        render(&mut renderer, 100),
        base,
        "a scene-sized margin must be byte-identical"
    );
    renderer.set_view_cutout(None);
}

/// OC.2 — the GPU feather tapers the reveal distance across the cone
/// band by the SAME per-cell rule the CPU test pins (visual-pass
/// redesign — no dither): the cut edge is spatially coherent (the
/// axis row's revealed pixels form one contiguous run — no teeth),
/// deterministic frame-to-frame, and strictly narrower than the same
/// cone with a hard edge.
#[test]
fn scene_dda_cutout_feather_tapers_reveal_radially() {
    use roxlap_formats::color::VoxColor;

    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let vxl = Vxl::from_dense(vsid, |_, y, _| match y {
        8 => Some(VoxColor(0x80_C0_40_40)),
        24 => Some(VoxColor(0x80_40_C0_40)),
        _ => None,
    });
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], decompress_chunk(&vxl))],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 2.0, 16.0],
        right: [-1.0, 0.0, 0.0],
        down: [0.0, 0.0, 1.0],
        forward: [0.0, 1.0, 0.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let tapered = GpuViewCutout {
        tan_outer: 0.5,
        tan_inner: 0.1,
        margin: 2.0,
    };
    let xf = [GridWorldTransform {
        cutout_focus_local: [16.0, 25.0, 16.0],
        cutout_focus_z: 100,
        ..GridWorldTransform::default()
    }];
    let render = |r: &mut HeadlessSceneRenderer| {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            &xf,
            cam.fov_y_rad,
            64,
            0.0,
        )
    };
    renderer.set_view_cutout(Some(tapered));
    let fb = render(&mut renderer);
    let fb2 = render(&mut renderer);
    assert_eq!(fb, fb2, "the feather taper must be deterministic");
    const BACK: (u32, u32, u32) = (0x40, 0xC0, 0x40);
    let rgb = |p: u32| (p & 0xff, (p >> 8) & 0xff, (p >> 16) & 0xff);
    // Per-row coherence on the axis row: BACK pixels form ONE
    // contiguous run (whole-cell classification — no teeth).
    let row = (h / 2) as usize * w as usize;
    let flags: Vec<bool> = (0..w as usize)
        .map(|px| rgb(fb[row + px]) == BACK)
        .collect();
    let first = flags.iter().position(|&b| b);
    let last = flags.iter().rposition(|&b| b);
    let (Some(first), Some(last)) = (first, last) else {
        panic!("axis row must contain revealed pixels");
    };
    assert!(
        flags[first..=last].iter().all(|&b| b),
        "revealed run must be contiguous (no teeth): {flags:?}"
    );
    assert!(
        flags.iter().any(|&b| !b),
        "the taper must keep wall pixels on the axis row"
    );
    // The taper cuts a strictly SMALLER hole than a hard edge at the
    // same outer cone.
    renderer.set_view_cutout(Some(GpuViewCutout {
        tan_inner: 0.5,
        ..tapered
    }));
    let fb_hard = render(&mut renderer);
    let count = |fb: &[u32]| fb.iter().filter(|&&p| rgb(p) == BACK).count();
    let (n_taper, n_hard) = (count(&fb), count(&fb_hard));
    assert!(
        0 < n_taper && n_taper < n_hard,
        "taper must shrink the hole: tapered {n_taper} vs hard {n_hard}"
    );
}

/// OC.2 — cut faces through the keyhole use the stored-colour /
/// run-top fallback (decision 4): cutting mid-run into a z-graded
/// block shows EXACTLY the voxel layer at the focus plane.
#[test]
fn scene_dda_cutout_cut_face_pins_layer_colour() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let chunk = decompress_chunk(&graded_block_chunk(vsid, 100, 140));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let cam = Camera {
        position: [16.0, 16.0, 50.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    renderer.set_view_cutout(Some(GpuViewCutout {
        tan_outer: 4.0,
        tan_inner: 4.0,
        margin: 0.0,
    }));
    let fb = renderer.render_with_transforms(
        &gpu.device,
        &gpu.queue,
        &scene,
        &[cam],
        &[GridWorldTransform {
            cutout_focus_local: [16.0, 16.0, 130.0],
            cutout_focus_z: 120,
            ..GridWorldTransform::default()
        }],
        cam.fov_y_rad,
        64,
        0.0,
    );
    let rgb = |p: u32| (p & 0xff, (p >> 8) & 0xff, (p >> 16) & 0xff);
    assert_eq!(
        rgb(fb[centre]),
        (255, 0, 120),
        "keyhole cut face must be the z=120 layer: {:#010x}",
        fb[centre]
    );
}

/// OC.2 — the GPU keyhole uses the CPU's exact `focus_z >> mip` FLOOR
/// formula (the CA.3 mip gate, cutout edition): plates P (z=100) and
/// Q (z=140) with the focus plane at 101 — mip 0 hides P and lands on
/// Q; a coarse mip floors the plane onto P's cell so P pokes through.
#[test]
fn scene_dda_cutout_mip_formula_floors() {
    use roxlap_formats::color::VoxColor;

    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    let vxl = Vxl::from_dense(vsid, |_, _, z| match z {
        100 => Some(VoxColor(0x80ff_0064)), // P: R=0xff, B=100
        140 => Some(VoxColor(0x80ff_008c)), // Q: R=0xff, B=140
        _ => None,
    });
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], decompress_chunk(&vxl))],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    let centre = (h / 2 * w + w / 2) as usize;
    renderer.set_view_cutout(Some(GpuViewCutout {
        tan_outer: 1.0,
        tan_inner: 1.0,
        margin: 0.0,
    }));
    let xf = GridWorldTransform {
        cutout_focus_local: [16.0, 16.0, 120.0],
        cutout_focus_z: 101,
        ..GridWorldTransform::default()
    };
    // Camera OUTSIDE the chunk (t_enter ≈ 200) so `mip_scan_dist`
    // alone dictates the marched mip.
    let cam = Camera {
        position: [16.0, 16.0, -200.0],
        right: [1.0, 0.0, 0.0],
        down: [0.0, 1.0, 0.0],
        forward: [0.0, 0.0, 1.0],
        fov_y_rad: 20f32.to_radians(),
    };
    let render_at = |r: &mut HeadlessSceneRenderer, mip_scan_dist: f32| {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            &[xf],
            cam.fov_y_rad,
            64,
            mip_scan_dist,
        )[centre]
    };
    let blue = |p: u32| (p >> 16) & 0xff;
    // LOD off → mip 0 → P hidden by the keyhole, the ray lands on Q.
    let p_mip0 = render_at(&mut renderer, 0.0);
    assert_eq!(
        blue(p_mip0),
        140,
        "mip 0: focus plane 101 must hide plate P and hit Q: {p_mip0:#010x}"
    );
    // mip ≥ 1: the floored plane exposes P's cell (100 >> m == 101 >> m).
    let p_coarse = render_at(&mut renderer, 100.0);
    assert!(
        blue(p_coarse) < 120,
        "coarse mip: floor formula must expose plate P: {p_coarse:#010x}"
    );
}

/// OC.2 — the keyhole is a VIEW aid (entry-doc non-goal): a wall
/// hidden by the cutout still casts its sun shadow (the shadow march
/// never sees the cutout) — the exact opposite of the CA clip's
/// "world as if removed" gate, pinned side-by-side against it.
#[test]
fn scene_dda_cutout_hidden_wall_still_shadows() {
    let Some((gpu, _lock)) = try_init() else {
        return;
    };
    let vsid = 32u32;
    // Floor at z=100 + wall x ∈ [16,18) rising z ∈ [90, 100).
    let chunk = decompress_chunk(&floor_with_wall_chunk(vsid, 16, 18, 90));
    let grid = GridUpload {
        vsid,
        origin_chunk: [0, 0, 0],
        chunks_dims: [1, 1, 1],
        pool_dims: [1, 1, 1],
        chunks: vec![([0, 0, 0], chunk)],
    };
    let scene = GpuSceneResident::upload(&gpu.device, &SceneUpload { grids: vec![grid] });

    let (w, h) = (64u32, 64u32);
    let mut renderer = HeadlessSceneRenderer::new(&gpu.device, &gpu.queue, w, h);
    // Shallow shoulder view from −x: the wall stands BETWEEN the eye
    // and the character column at (20, 16); the centre ray crosses it
    // at z ≈ 92 and, once the keyhole melts it, lands on the floor
    // beyond at x ≈ 24 — inside the wall's sun shadow.
    let (fx, fz) = (0.747_41_f32, 0.664_36_f32); // normalize(18, 0, 16)
    let cam = Camera {
        position: [2.0, 16.0, 80.0],
        right: [fz, 0.0, -fx],
        down: [0.0, 1.0, 0.0],
        forward: [fx, 0.0, fz],
        fov_y_rad: 60f32.to_radians(),
    };
    let centre = (h / 2 * w + w / 2) as usize;
    let lum = |p: u32| (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff);
    let render = |r: &mut HeadlessSceneRenderer, clip: Option<i32>, focus_z: i32| {
        r.render_with_transforms(
            &gpu.device,
            &gpu.queue,
            &scene,
            &[cam],
            &[GridWorldTransform {
                z_clip: clip,
                cutout_focus_z: focus_z,
                cutout_focus_local: [20.0, 16.0, 96.0],
                ..GridWorldTransform::default()
            }],
            cam.fov_y_rad,
            64,
            0.0,
        )[centre]
    };
    // Sun toward −x and up: the wall shadows the floor strip BEYOND
    // it (x ≳ 18) — exactly what the cut ray lands on.
    let s = std::f32::consts::FRAC_1_SQRT_2;
    renderer.set_scene_lights(SceneLights {
        enabled: true,
        grid_sun_dirs: vec![[-s, 0.0, -s]],
        sun_color: [1.0; 3],
        sun_intensity: 3.0,
        sun_casts_shadow: true,
        ambient: [0.5; 3],
        shadow_strength: 1.0,
        shadow_bias: 1.5,
        shadow_max_dist: 512.0,
        shadow_max_steps: 256,
        ..SceneLights::default()
    });
    // Uncut baseline: the centre pixel is the (sun-lit) wall face.
    let base = render(&mut renderer, None, i32::MIN);
    // Keyhole around the column behind the wall: the wall melts, the
    // revealed floor beyond stays in the hidden wall's shadow.
    renderer.set_view_cutout(Some(GpuViewCutout {
        tan_outer: 10.0,
        tan_inner: 10.0,
        margin: 1.0,
    }));
    let cut = render(&mut renderer, None, 100);
    assert_ne!(cut, base, "the keyhole must melt the wall at the centre");
    // Contrast: the CA clip REMOVES the wall from the world — the same
    // floor point brightens (no occluder left to shadow it).
    renderer.set_view_cutout(None);
    let clipped = render(&mut renderer, Some(100), i32::MIN);
    assert!(
        lum(clipped) > lum(cut),
        "world-removal must unshadow what the view cutout keeps dark: \
         cut {cut:#010x} clipped {clipped:#010x}"
    );
}