cranpose-render-wgpu 0.21.0

GPU renderer for Cranpose scenes through wgpu
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
use std::{
    cell::Cell,
    hash::{Hash, Hasher},
    rc::Rc,
    sync::{Arc, mpsc},
    time::Duration,
};

use bytemuck::{Pod, Zeroable};
use cranpose_core::collections::{bounded_lru::BoundedLruCache, map::HashMap};
use cranpose_render_common::{geometry::blur_reach, graph::quad_bounds};
use cranpose_ui_graphics::{
    BlendMode, FRAGMENT_KIND_ARC, ImageSampling, Point, RecordLane, Rect, RenderHash, TileMode,
};
use web_time::Instant;

use crate::{
    DebugCpuAllocationStats,
    ablation::Ablation,
    arc_trig_fill::ArcTrigFill,
    collect::LayerScene,
    draw_pass::{
        PassSegment, PassTarget, ResolvedComposite, ResolvedCompositeKind, RunCalls, SegmentLayout,
        SourceContent, TrackedPass,
    },
    effect_renderer::{BlurDest, BlurSpec, CompositeSampleMode, EffectRenderer},
    fixed_pipeline::{FixedPipeline, FixedPipelineJob},
    frame::{
        ClearedImages, Damage, DeviceRect, FrameExecutor, FrameRecording, FrameWindow,
        ImageContent, KeptWindow, PresentedImage, Repaint, StageSideScratch, Summaries,
        SwapchainHistory, Window, WindowDamage,
    },
    frame_graph::{
        BufferUpload, FrameCommandRecorder, FrameCommandStats, FrameTextureDescriptor,
        FrameUploadAllocators, TextureRegionCopy, UniformUpload, UploadAllocatorId,
        UploadAllocatorSpec, UploadMode, WgpuFrameGraph, WgpuFrameGraphExecutor,
    },
    frame_packet::{CancelReason, FramePacket, FrameSceneStorage, PresentOutcome, RenderReturns},
    geometry::{
        DevicePixelBounds, SegmentTransform, anchored_device_rect, canonicalize_device_coordinate,
        snap_delta_for_anchor, snapped_anchor_device_origin, translate_quad,
        translation_stable_anchored_device_pixel_bounds,
    },
    glyph_run_arena::GlyphRunArena,
    gpu_stats::{self, gpu_stats_enabled},
    layer_cache::LayerCache,
    lazy_resource::Pending,
    offscreen::{COMPOSITION_BYTES_PER_PIXEL, OffscreenTarget},
    output_conversion::OutputConverter,
    pipeline_compiler::{CompileLane, CompilerSend, PipelineCompilation, PipelineCompiler},
    rrect_shadow::{SHADOW_QUAD_CORNERS, ShadowInstance},
    run_store::{ArenaBinding, PlacementData, RunBufferMode, RunDrawCall, RunStore, StoredTables},
    scene::{ImageDraw, RunDraw, ShadowDraw, SnapAnchor, TextDraw},
    scissor::intersect_target_rects,
    shape_pipelines::{PipelineDemand, ShapePipelineFactory, ShapePipelines},
    shared_shader::SharedShader,
};
const MAX_TRANSPARENT_SOURCES: usize = 16;

pub(crate) const CLEAR_COLOR: wgpu::Color = wgpu::Color {
    r: cranpose_render_common::FRAME_CLEAR_COLOR[0] as f64,
    g: cranpose_render_common::FRAME_CLEAR_COLOR[1] as f64,
    b: cranpose_render_common::FRAME_CLEAR_COLOR[2] as f64,
    a: cranpose_render_common::FRAME_CLEAR_COLOR[3] as f64,
};
const MAX_TEXTURE_CACHE_ITEMS: usize = 256;

type DeviceRect4 = (f32, f32, f32, f32);
/// A rect of whole target pixels: x, y, width, height.
pub(crate) type TargetRect = (u32, u32, u32, u32);

/// The scissor that keeps a draw under `scissor`, or under none, within
/// `slot`: empty when they share no pixel.
pub(crate) fn confined_scissor(scissor: Option<TargetRect>, slot: TargetRect) -> TargetRect {
    scissor
        .map_or(Some(slot), |scissor| intersect_target_rects(scissor, slot))
        .unwrap_or((slot.0, slot.1, 0, 0))
}

/// A draw's scissor cut down to the pixels its pass segment may touch;
/// `None` when nothing of it remains.
pub(crate) fn bounded_scissor(
    scissor: TargetRect,
    bound: Option<TargetRect>,
) -> Option<TargetRect> {
    match bound {
        Some(bound) => intersect_target_rects(scissor, bound),
        None => (scissor.2 > 0 && scissor.3 > 0).then_some(scissor),
    }
}

fn intersect_device_rects(a: DeviceRect4, b: DeviceRect4) -> Option<DeviceRect4> {
    let left = a.0.max(b.0);
    let top = a.1.max(b.1);
    let right = (a.0 + a.2).min(b.0 + b.2);
    let bottom = (a.1 + a.3).min(b.1 + b.3);
    (right > left && bottom > top).then_some((left, top, right - left, bottom - top))
}

pub(crate) fn anchored_rect_to_device(
    rect: Rect,
    snap_anchor: Option<SnapAnchor>,
    root_scale: f32,
) -> DeviceRect4 {
    let device = anchored_device_rect(rect, snap_anchor, root_scale);
    (device.x, device.y, device.width, device.height)
}

fn mask_rect(rect: Rect) -> [f32; 4] {
    [rect.x, rect.y, rect.width, rect.height]
}

#[derive(Default)]
struct DeviceErrorSentry {
    errors: std::sync::atomic::AtomicU64,
    poisoned: std::sync::atomic::AtomicBool,
}

impl DeviceErrorSentry {
    fn record(&self, error: &wgpu::Error) {
        use std::sync::atomic::Ordering;
        self.poisoned.store(true, Ordering::Release);
        let count = self.errors.fetch_add(1, Ordering::Relaxed) + 1;
        if count.is_power_of_two() {
            log::error!("[gpu-device] uncaptured wgpu error #{count}: {error}");
        }
    }

    fn take_poison(&self) -> bool {
        self.poisoned
            .swap(false, std::sync::atomic::Ordering::AcqRel)
    }

    fn error_count(&self) -> u64 {
        self.errors.load(std::sync::atomic::Ordering::Relaxed)
    }
}

fn is_blend_mode_supported(mode: BlendMode) -> bool {
    matches!(
        mode,
        BlendMode::Src | BlendMode::SrcOver | BlendMode::DstOut
    )
}

pub(crate) fn blend_state_for_mode(mode: BlendMode) -> wgpu::BlendState {
    match mode {
        BlendMode::Src => wgpu::BlendState::REPLACE,
        BlendMode::DstOut => wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::Zero,
                dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::Zero,
                dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                operation: wgpu::BlendOperation::Add,
            },
        },
        _ => wgpu::BlendState::ALPHA_BLENDING,
    }
}

pub(crate) fn supported_blend_mode(mode: BlendMode) -> BlendMode {
    if is_blend_mode_supported(mode) {
        return mode;
    }

    BlendMode::SrcOver
}

pub(crate) fn hash_f32_for_cache<H: Hasher>(value: f32, state: &mut H) {
    value.to_bits().hash(state);
}

fn hash_text_raster_geometry_for_cache<H: Hasher>(
    rect: Rect,
    static_text_motion: bool,
    state: &mut H,
) {
    hash_f32_for_cache(rect.width, state);
    hash_f32_for_cache(rect.height, state);
    static_text_motion.hash(state);
    if !static_text_motion {
        hash_f32_for_cache(rect.x.fract(), state);
        hash_f32_for_cache(rect.y.fract(), state);
    }
}

/// Hashes what a text draws apart from where and how large its rect is.
fn hash_text_draw_for_cache<H: Hasher>(
    text_draw: &TextDraw,
    raster_rect: Rect,
    text_scale: f32,
    state: &mut H,
) {
    text_draw.text.render_hash().hash(state);
    text_draw.layout.id().hash(state);
    text_draw.style_hash.hash(state);
    text_draw.color.render_hash().hash(state);
    hash_text_gradient_phase_for_cache(text_draw, raster_rect, state);
    text_draw.font_size.to_bits().hash(state);
    text_scale.to_bits().hash(state);
}

pub(crate) fn hash_text_gradient_phase_for_cache<H: Hasher>(
    text_draw: &TextDraw,
    raster_rect: Rect,
    state: &mut H,
) {
    if text_draw.text.gradient() {
        for coordinate in [raster_rect.x, raster_rect.y] {
            let phase = if coordinate >= 0.0 {
                coordinate.rem_euclid(4.0)
            } else {
                coordinate
            };
            phase.to_bits().hash(state);
        }
    }
}

/// Where a text draws in the scene, snapped, and the scale its glyphs
/// raster at; `None` for a text that draws nothing.
pub(crate) fn text_logical_geometry_for_draw(
    text_draw: &TextDraw,
    root_scale: f32,
) -> Option<(Rect, f32)> {
    if text_draw.text.is_empty()
        || text_draw.rect.width <= 0.0
        || text_draw.rect.height <= 0.0
        || !root_scale.is_finite()
        || root_scale <= 0.0
    {
        return None;
    }

    let text_scale = text_draw.scale * root_scale;
    if !text_scale.is_finite() || text_scale <= 0.0 {
        return None;
    }

    let snap_delta = text_draw
        .snap_anchor
        .map(|anchor| snap_delta_for_anchor(anchor, root_scale))
        .unwrap_or_default();
    let logical_rect = text_draw.rect.translate(snap_delta.x, snap_delta.y);
    Some((logical_rect, text_scale))
}

pub(crate) fn text_raster_geometry_for_draw(
    text_draw: &TextDraw,
    root_scale: f32,
) -> Option<(Rect, Rect, Option<Rect>, f32, bool)> {
    text_logical_geometry_for_draw(text_draw, root_scale)
        .map(|logical| text_raster_geometry_at(text_draw, logical, root_scale))
}

/// The raster geometry of a text whose logical geometry is
/// `(logical_rect, text_scale)`: see [`text_raster_geometry_for_draw`].
fn text_raster_geometry_at(
    text_draw: &TextDraw,
    (logical_rect, text_scale): (Rect, f32),
    root_scale: f32,
) -> (Rect, Rect, Option<Rect>, f32, bool) {
    let static_text_motion = text_draw.text.static_motion();
    let clip = text_draw.clip;
    let mut raster_rect = Rect {
        x: logical_rect.x * root_scale,
        y: logical_rect.y * root_scale,
        width: logical_rect.width * root_scale,
        height: logical_rect.height * root_scale,
    };
    if text_draw.snap_anchor.is_some() {
        raster_rect.x = canonicalize_device_coordinate(raster_rect.x);
        raster_rect.y = canonicalize_device_coordinate(raster_rect.y);
    }
    if static_text_motion {
        raster_rect.x = raster_rect.x.round();
        raster_rect.y = raster_rect.y.round();
    }
    raster_rect.width = raster_rect.width.ceil().max(1.0);
    raster_rect.height = raster_rect.height.ceil().max(1.0);
    (
        logical_rect,
        raster_rect,
        clip,
        text_scale,
        static_text_motion,
    )
}

fn expand_rect(rect: Rect, margin_x: f32, margin_y: f32) -> Rect {
    Rect {
        x: rect.x - margin_x,
        y: rect.y - margin_y,
        width: rect.width + margin_x * 2.0,
        height: rect.height + margin_y * 2.0,
    }
}

fn draw_rect_is_visible_in_viewport(
    rect: Rect,
    clip: Option<Rect>,
    viewport: ViewportUniformParams,
    root_scale: f32,
) -> bool {
    if !root_scale.is_finite() || root_scale <= 0.0 {
        return false;
    }
    rect_is_visible_in_rect(rect, clip, viewport.scene_rect(root_scale))
}

fn rect_is_visible_in_rect(rect: Rect, clip: Option<Rect>, viewport_rect: Rect) -> bool {
    let visible_rect = match clip {
        Some(clip) => clip.intersect(viewport_rect),
        None => Some(viewport_rect),
    };
    visible_rect.is_some_and(|visible| rect.intersect(visible).is_some())
}

fn snapped_quad_bounds(quad: [[f32; 2]; 4], anchor: Option<SnapAnchor>, root_scale: f32) -> Rect {
    let snap_delta = anchor
        .map(|anchor| snap_delta_for_anchor(anchor, root_scale))
        .unwrap_or_default();
    quad_bounds(translate_quad(quad, snap_delta))
}

/// The logical rect a draw may touch: its snapped bounds within its clip,
/// `None` when the clip leaves nothing.
fn clipped_bounds(rect: Rect, clip: Option<Rect>) -> Option<Rect> {
    match clip {
        Some(clip) => rect.intersect(clip),
        None => Some(rect),
    }
}

pub(crate) fn text_draw_bounds(text: &TextDraw, root_scale: f32) -> Option<Rect> {
    text_logical_geometry_for_draw(text, root_scale)
        .and_then(|(logical_rect, _)| text_reach_bounds(text, logical_rect))
}

// Where `text`, laid in `logical_rect`, draws its glyphs within its clip.
fn text_reach_bounds(text: &TextDraw, logical_rect: Rect) -> Option<Rect> {
    clipped_bounds(text.glyph_reach(logical_rect, text.scale), text.clip)
}

pub(crate) fn image_draw_bounds(image: &ImageDraw, root_scale: f32) -> Option<Rect> {
    clipped_bounds(
        snapped_quad_bounds(image.quad, image.snap_anchor, root_scale),
        image.clip,
    )
}

pub(crate) fn run_draw_bounds(run: &RunDraw, root_scale: f32) -> Option<Rect> {
    let snap_delta = run
        .placement
        .snap_anchor
        .map(|anchor| snap_delta_for_anchor(anchor, root_scale))
        .unwrap_or_default();
    clipped_bounds(
        run.bounds.translate(snap_delta.x, snap_delta.y),
        run.placement.clip,
    )
}

/// The logical geometry of `text` (see [`text_logical_geometry_for_draw`])
/// when it draws any pixel inside `viewport_rect`.
pub(crate) fn text_draw_geometry_in_rect(
    text: &TextDraw,
    viewport_rect: Rect,
    root_scale: f32,
) -> Option<(Rect, f32)> {
    text_logical_geometry_for_draw(text, root_scale).filter(|(logical_rect, _)| {
        text_reach_bounds(text, *logical_rect)
            .is_some_and(|bounds| bounds.intersect(viewport_rect).is_some())
    })
}

pub(crate) fn hash_device_offset<H: Hasher>(
    value: f32,
    origin: f32,
    root_scale: f32,
    state: &mut H,
) {
    hash_f32_for_cache((value - origin) * root_scale, state);
}

pub(crate) fn hash_device_rect<H: Hasher>(
    rect: Rect,
    origin_x: f32,
    origin_y: f32,
    root_scale: f32,
    state: &mut H,
) {
    hash_device_offset(rect.x, origin_x, root_scale, state);
    hash_device_offset(rect.y, origin_y, root_scale, state);
    hash_device_offset(rect.width, 0.0, root_scale, state);
    hash_device_offset(rect.height, 0.0, root_scale, state);
}

/// Hashes the rect `placement` is clipped to, `clip`, and its rounding, in
/// device pixels from `origin`.
fn hash_placement_clips<H: Hasher>(
    placement: &crate::scene::Placement,
    clip: Option<Rect>,
    (origin_x, origin_y): (f32, f32),
    root_scale: f32,
    state: &mut H,
) {
    let rounding = placement.rounding;
    for (rect, radius) in [
        (clip, 0.0),
        (
            rounding.map(|rounding| rounding.rect),
            rounding.map_or(0.0, |rounding| rounding.radius),
        ),
    ] {
        rect.is_some().hash(state);
        if let Some(rect) = rect {
            let [x, y, width, height] = placement.device_rect(rect, root_scale);
            for value in [
                x - origin_x,
                y - origin_y,
                width,
                height,
                radius * root_scale,
            ] {
                hash_f32_for_cache(value, state);
            }
        }
    }
}

pub(crate) fn hash_run_item_with_clip<H: Hasher>(
    run: &RunDraw,
    clip: Option<Rect>,
    origin_x: f32,
    origin_y: f32,
    root_scale: f32,
    state: &mut H,
) {
    let tables = run.tables();
    tables.fingerprint().hash(state);
    run.segments.start.hash(state);
    run.segments.end.hash(state);
    let placement = &run.placement;
    let snap = placement
        .snap_anchor
        .map(|anchor| snap_delta_for_anchor(anchor, root_scale))
        .unwrap_or_default();
    let offset = Point::new(placement.offset.x + snap.x, placement.offset.y + snap.y);
    let canonicalize = placement.snap_anchor.is_some();
    canonicalize.hash(state);
    for segment in &tables.segments[run.segments.start as usize..run.segments.end as usize] {
        if segment.lane != RecordLane::Shapes {
            continue;
        }
        let exact_placement =
            !canonicalize || segment.kinds & ((1u8 << FRAGMENT_KIND_ARC) | LINE_KINDS) != 0;
        exact_placement.hash(state);
        if exact_placement {
            for value in [offset.x, offset.y, origin_x, origin_y, root_scale] {
                hash_f32_for_cache(value, state);
            }
            continue;
        }
        for index in segment.start..segment.start + segment.count {
            let record = tables
                .shapes
                .get(index as usize)
                .expect("shape segment record");
            let half_width = if record.is_stroked() {
                record.stroke_width * 0.5
            } else {
                0.0
            };
            let [left, top, right, bottom] = crate::run_geometry::device_shape_edges(
                record.rect,
                half_width,
                offset,
                root_scale,
                canonicalize,
            );
            for value in [left - origin_x, top - origin_y, right - left, bottom - top] {
                hash_f32_for_cache(value, state);
            }
        }
    }
    hash_placement_clips(placement, clip, (origin_x, origin_y), root_scale, state);
    if tables.segments[run.segments.start as usize..run.segments.end as usize]
        .iter()
        .any(|segment| segment.gradient || segment.vertex_gradient)
    {
        let dither = placement
            .snap_anchor
            .map(|anchor| snapped_anchor_device_origin(anchor, root_scale))
            .unwrap_or_default();
        hash_f32_for_cache(dither.x - origin_x, state);
        hash_f32_for_cache(dither.y - origin_y, state);
    }
    hash_f32_for_cache(placement.alpha, state);
    match &placement.color_filter {
        Some(filter) => {
            1u8.hash(state);
            filter.render_hash().hash(state);
        }
        None => 0u8.hash(state),
    }
}

/// The vertex entry point of every fullscreen strip.
pub(crate) const FULLSCREEN_VS: &str = "fullscreen_vs";

/// What every draw of one pass shares: its target's size and whether the
/// pass has a depth buffer its opaque interiors fill first.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PassFrame {
    pub(crate) size: (u32, u32),
    pub(crate) depth: PassDepth,
}

impl PassFrame {
    /// The scissor a draw bounded by `scissor`, or by nothing, sets.
    pub(crate) fn scissor(self, scissor: Option<(u32, u32, u32, u32)>) -> (u32, u32, u32, u32) {
        scissor.unwrap_or((0, 0, self.size.0, self.size.1))
    }
}

/// Which of a pass's two stages a shape draw records: the front-to-back
/// opaque interiors, or the paint in draw order.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum RunStage {
    Interiors,
    Paint,
}

/// The depth buffer of a pass that lays opaque interiors down first.
pub(crate) const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;

/// Depth steps one draw apart in a pass's order, as the shaders'
/// `DEPTH_STEP`.
const PASS_DEPTH_STEP: f32 = 1.0 / 1_048_576.0;

/// The depth of the draw at `index` in its pass's order, as the shaders'
/// `record_depth` and `batch_depth` place it: later draws nearer.
pub(crate) fn pass_order_depth(index: u32) -> f32 {
    (1.0 - (index as f32 + 1.0) * PASS_DEPTH_STEP).max(0.0)
}

fn survive_gpu_errors_enabled() -> bool {
    !cranpose_core::DIAGNOSTICS.survive_gpu_errors.equals("0")
}

#[derive(Default)]
pub(crate) struct ViewportUniforms {
    uploads: FrameUploadAllocators,
    slots: Vec<UniformUpload>,
}

impl ViewportUniforms {
    fn begin_frame(&mut self) {
        self.slots.clear();
        self.uploads.reset();
    }

    fn claim(
        &mut self,
        device: &wgpu::Device,
        layout: &wgpu::BindGroupLayout,
        uniforms: &Uniforms,
    ) -> usize {
        let slot = self.slots.len();
        self.slots.push(self.uploads.upload_uniform(
            UploadAllocatorId::Viewport,
            UploadAllocatorSpec::uniform(
                "Viewport Uniform Buffer",
                "Viewport Uniform Bind Group",
                size_of::<Uniforms>() as u64,
            ),
            device,
            layout,
            bytemuck::bytes_of(uniforms),
        ));
        slot
    }

    // Claims the frame's next slot for a viewport of `params`.
    pub(crate) fn claim_viewport(
        &mut self,
        pipelines: &RendererPipelines,
        params: ViewportUniformParams,
    ) -> usize {
        let uniforms = Uniforms::of(params, PlacementData::zeroed(), RunTier::Arena);
        self.claim(
            &pipelines.device,
            &pipelines.uniform_bind_group_layout,
            &uniforms,
        )
    }

    pub(crate) fn bind(&self, pass: &mut TrackedPass<'_, '_>, slot: usize) -> Result<(), String> {
        let uniform = self
            .slots
            .get(slot)
            .ok_or_else(|| "viewport uniform slot was never claimed this frame".to_string())?;
        pass.set_bind_group(0, &uniform.bind_group, &[uniform.offset]);
        Ok(())
    }

    fn stage_pending(
        &mut self,
        device: &wgpu::Device,
        recorder: &mut impl FrameCommandRecorder,
    ) -> FrameCommandStats {
        self.uploads.stage_pending(|buffer, offset, bytes| {
            recorder.stage_frame_buffer_copy(device, buffer, offset, bytes)
        })
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct ViewportUniformParams {
    pub(crate) width: u32,
    pub(crate) height: u32,
    pub(crate) offset: [f32; 2],
    pub(crate) transform: SegmentTransform,
    /// Where the drawn vertices' origin sits in the segment's device space,
    /// added before the transform: zero, except for a retained glyph run
    /// drawn under a transform, whose vertices sit at its raster origin. The
    /// sum is the one the shared path writes, so both draw the same pixels.
    pub(crate) origin: [f32; 2],
    /// The pass-order index of the batch's first shape record, from which
    /// each record's depth counts.
    pub(crate) depth_base: f32,
}

impl ViewportUniformParams {
    // The fallback meshes' target in this viewport, counting depth from its
    // base in a pass with the depth buffer.
    fn mesh_target(self, depth: PassDepth) -> crate::mesh_fallback::MeshTarget {
        crate::mesh_fallback::MeshTarget {
            offset: self.offset,
            size: [self.width as f32, self.height as f32],
            depth_base: depth.is_on().then_some(self.depth_base),
        }
    }

    /// The logical rect of the drawn scene that the target shows.
    pub(crate) fn scene_rect(self, root_scale: f32) -> Rect {
        segment_scene_rect(
            self.transform,
            Rect {
                x: self.offset[0],
                y: self.offset[1],
                width: self.width as f32,
                height: self.height as f32,
            },
            root_scale,
        )
    }
}

/// The logical rect of a scene drawn under `transform` that the device rect
/// `device` of the target's scene space shows: the rect itself when the
/// scene is not transformed, else the bounds it maps back to.
pub(crate) fn segment_scene_rect(
    transform: SegmentTransform,
    device: Rect,
    root_scale: f32,
) -> Rect {
    let device = if transform.is_identity() {
        device
    } else {
        transform.segment_bounds(device)
    };
    Rect {
        x: device.x / root_scale,
        y: device.y / root_scale,
        width: device.width / root_scale,
        height: device.height / root_scale,
    }
}

/// A stored run's draws for one pass: the tables current when it was
/// prepared, the uniform slot holding its placement, and the range of the
/// pass's run draws with the pipeline and vertex range of each segment's
/// quads and bands.
pub(crate) struct StoreRunBatch {
    pub(crate) tables: StoredTables,
    pub(crate) uniform_slot: usize,
    pub(crate) draws: std::ops::Range<usize>,
    /// The scissor an unturned run's clip puts on its paint: see
    /// [`clip_scissor`].
    pub(crate) clip: Option<TargetRect>,
}

struct CompositionTarget {
    target: Rc<OffscreenTarget>,
    output_bind_group: wgpu::BindGroup,
    /// The frame the target's pixels show.
    kept: KeptWindow,
}

/// Where a frame renders: straight into the presentable image, or into the
/// reusable composition target that then goes into the image.
enum FrameRoot {
    /// Straight into the presentable image `image`; copied into the
    /// composition target `keep` too when that target is to keep the frame
    /// for images that come cleared.
    Surface {
        image: Rc<OffscreenTarget>,
        keep: Option<CompositionTarget>,
    },
    /// Into the composition target, which is copied whole into an image
    /// that comes `cleared`, and otherwise drawn into the image by the
    /// output conversion.
    Composition {
        composition: CompositionTarget,
        cleared: Option<Rc<OffscreenTarget>>,
    },
}

/// What goes into the presentable image or out of it once the frame is
/// drawn.
#[derive(Clone, Copy)]
enum OutputStep<'a> {
    /// Nothing: the frame rendered into the presentable image.
    None,
    /// The output conversion of `mode` into the view, from the bind group.
    Convert {
        view: &'a wgpu::TextureView,
        bind_group: &'a wgpu::BindGroup,
        mode: OutputMode,
    },
    /// A whole copy, between the composition target and an image that
    /// comes cleared.
    Copy {
        from: &'a OffscreenTarget,
        to: &'a OffscreenTarget,
    },
}

impl FrameRoot {
    fn target(&self) -> &Rc<OffscreenTarget> {
        match self {
            Self::Surface { image, .. } => image,
            Self::Composition { composition, .. } => &composition.target,
        }
    }

    /// What the frame ends with between its root and the presentable image.
    fn output<'a>(
        &'a self,
        output_view: Option<&'a wgpu::TextureView>,
        screenshot_bind_group: Option<&'a wgpu::BindGroup>,
    ) -> OutputStep<'a> {
        match self {
            Self::Surface { keep: None, .. } => OutputStep::None,
            Self::Surface {
                image,
                keep: Some(keep),
            } => OutputStep::Copy {
                from: image,
                to: &keep.target,
            },
            Self::Composition {
                composition,
                cleared: Some(image),
            } => OutputStep::Copy {
                from: &composition.target,
                to: image,
            },
            Self::Composition {
                composition,
                cleared: None,
            } => output_view.map_or(OutputStep::None, |view| match screenshot_bind_group {
                Some(bind_group) => OutputStep::Convert {
                    view,
                    bind_group,
                    mode: OutputMode::Screenshot,
                },
                None => OutputStep::Convert {
                    view,
                    bind_group: &composition.output_bind_group,
                    mode: OutputMode::Display,
                },
            }),
        }
    }

    /// The composition target the frame drew into or copied into, which
    /// the renderer keeps for the next frame.
    fn into_composition(self) -> Option<CompositionTarget> {
        match self {
            Self::Surface { keep, .. } => keep,
            Self::Composition { composition, .. } => Some(composition),
        }
    }
}

/// What a presentable image must allow to be the frame's root target:
/// rendering, and the copies a capture and the opaque-prefix cache take of
/// the page and put back.
const DIRECT_SURFACE_ROOT_USAGES: wgpu::TextureUsages = wgpu::TextureUsages::RENDER_ATTACHMENT
    .union(wgpu::TextureUsages::COPY_SRC)
    .union(wgpu::TextureUsages::COPY_DST);

/// The usages to ask of a presentable image: rendering, and whichever of
/// the copies and sampling the surface offers. Sampling lets a frame whose
/// backdrops read the page draw straight into the image too.
pub fn presentable_root_usages(supported: wgpu::TextureUsages) -> wgpu::TextureUsages {
    wgpu::TextureUsages::RENDER_ATTACHMENT
        | (supported & (DIRECT_SURFACE_ROOT_USAGES | wgpu::TextureUsages::TEXTURE_BINDING))
}

/// Whether the presented image can be the frame's root target: its bytes
/// are the composition format (so the 8-bit output conversion would be an
/// identity), it is the viewport's size, it can be copied as the page is,
/// and it can be sampled when the frame `reads_page` through a backdrop.
/// Metal's images cannot be sampled, so only a frame without backdrops
/// draws straight into one there.
fn surface_is_direct_root(
    texture: &wgpu::Texture,
    composition_format: wgpu::TextureFormat,
    viewport: (u32, u32),
    reads_page: bool,
) -> bool {
    let usage = texture.usage();
    texture.format().remove_srgb_suffix() == composition_format
        && usage.contains(DIRECT_SURFACE_ROOT_USAGES)
        && (!reads_page || usage.contains(wgpu::TextureUsages::TEXTURE_BINDING))
        && (texture.width(), texture.height()) == viewport
}

/// What a frame draws into its page: the root and overlay scenes at the
/// root's scale over the clear colour, all of the page or a part of it.
#[derive(Clone, Copy)]
struct FrameDraw<'a> {
    root: &'a LayerScene,
    overlay: Option<&'a LayerScene>,
    root_scale: f32,
    clear: wgpu::Color,
    repaint: Repaint,
    scene_motion: crate::frame_packet::SceneMotion,
}

#[derive(Clone, Copy)]
enum OutputMode {
    Display,
    Screenshot,
}

/// Where a frame goes: displayed in a presentable image, or read back as a
/// screenshot.
#[derive(Clone, Copy)]
pub(crate) struct FrameOutput<'a> {
    mode: OutputMode,
    view: Option<&'a wgpu::TextureView>,
    texture: Option<&'a wgpu::Texture>,
    /// What `texture` holds as the frame draws into it.
    image: ImageContent,
}

impl<'a> FrameOutput<'a> {
    // A frame displayed in `texture` through `view`, which holds `image`.
    pub(crate) fn display(
        texture: &'a wgpu::Texture,
        view: &'a wgpu::TextureView,
        image: ImageContent,
    ) -> Self {
        Self {
            mode: OutputMode::Display,
            view: Some(view),
            texture: Some(texture),
            image,
        }
    }
}

/// The kept frame a frame's damage is measured against.
#[derive(Clone, Copy)]
enum Keeper {
    /// The renderer's composition target.
    Composition,
    /// A swapchain image.
    Image(PresentedImage),
    /// The frame drawn last into an image that comes cleared; `keeping`
    /// when the composition target takes a copy of this frame to keep.
    Cleared { keeping: bool },
}

/// What a frame changes against the frame its target kept, what it draws
/// for that, and which target kept it.
#[derive(Clone, Copy)]
struct FrameDamagePlan {
    damage: Damage,
    repaint: Repaint,
    keeper: Keeper,
}

pub(crate) struct GpuRenderer {
    pub(crate) device: Arc<wgpu::Device>,
    pub(crate) queue: Arc<wgpu::Queue>,
    device_errors: Arc<DeviceErrorSentry>,
    renderer_epoch: u64,
    #[cfg(not(target_arch = "wasm32"))]
    display_format: wgpu::TextureFormat,
    composition_target: Option<CompositionTarget>,
    output_converter: OutputConverter,
    screenshot_converter: OutputConverter,
    pub(crate) pipelines: RendererPipelines,
    pub(crate) depth_targets: DepthTargets,
    /// The pipeline and mesh buffers of the 3D scenes; nothing in it is made
    /// until a scene draws.
    pub(crate) scene_pass: crate::scene_pass::ScenePass,
    pub(crate) depth_gate: crate::depth_gate::DepthGate,
    /// The size of the atlas the layer surfaces drawn again go into.
    pub(crate) surface_atlas_sizes: crate::frame::SurfaceAtlasSizes,
    text: TextResources,
    pub(crate) viewport_uniforms: ViewportUniforms,
    run_store: RunStore,
    images: ImageTextures,
    /// How run tables and retained glyph runs reach the GPU, and whether
    /// kept layer surfaces stay in their atlas: see [`UploadMode::copy_free`].
    pub(crate) copy_free: UploadMode,
    /// Text runs the frame has drawn so far, cached or not.
    pub(crate) pass_scratch: crate::draw_pass::PassScratch,
    pub(crate) stage_side_scratch: StageSideScratch,
    deferred_offscreen_releases: Vec<OffscreenTarget>,
    pub(crate) effect_renderer: EffectRenderer,
    pub(crate) layer_cache: LayerCache,
    pub(crate) ablation: Ablation,
    pub(crate) ablation_frames: u32,
    pub(crate) nesting_overflow_reported: bool,
    window_damage: WindowDamage,
    /// The frames the swapchain's images hold.
    swapchain: SwapchainHistory,
    /// The frames drawn into images that come cleared.
    cleared: ClearedImages,
    /// The colour a frame drawn in a window last filled it with, and a
    /// pixel of it.
    pub(crate) window_fill: Option<(wgpu::Color, Rc<OffscreenTarget>)>,
    pub(crate) gates: admission_gate::AdmissionGates,
    transparent_sources: HashMap<(u32, u32), Rc<OffscreenTarget>>,
    shadow_surfaces: ShadowSurfaces,
    pub(crate) frame_stats: gpu_stats::FrameStats,
    history: FrameHistory,
    /// Whether the pipeline cache file holds the last launch's first screen,
    /// which builds ahead of this renderer's first frame.
    #[cfg(not(target_arch = "wasm32"))]
    first_screen_recorded: bool,
    /// How many requested shader warm-ups this renderer has queued.
    shader_warm_ups_queued: usize,
    // The scene update of the frame drawn last.
    drawn_scene_update: Option<u64>,
}

/// What a frame clears to before it draws: nothing for a transparent
/// window, the framework's background for every other surface.
pub fn frame_clear_color(transparent: bool) -> wgpu::Color {
    if transparent {
        wgpu::Color::TRANSPARENT
    } else {
        CLEAR_COLOR
    }
}

/// What a [`GpuRenderer`] is built from. It crosses to the present thread
/// whole, so it holds only owned, `Send` parts.
pub(crate) struct GpuRendererInit {
    pub(crate) device: Arc<wgpu::Device>,
    pub(crate) queue: Arc<wgpu::Queue>,
    pub(crate) surface_format: wgpu::TextureFormat,
    pub(crate) adapter_backend: wgpu::Backend,
    pub(crate) adapter_downlevel: wgpu::DownlevelFlags,
    pub(crate) renderer_epoch: u64,
    pub(crate) pipeline_compilation: PipelineCompilation,
    pub(crate) on_device_lost: Option<crate::DeviceLostHandler>,
}

#[expect(clippy::multiple_inherent_impl, reason = "draw passes in draw_pass")]
impl GpuRenderer {
    pub(crate) fn new(init: GpuRendererInit) -> Self {
        let GpuRendererInit {
            device,
            queue,
            surface_format: display_format,
            adapter_backend,
            adapter_downlevel,
            renderer_epoch,
            pipeline_compilation,
            on_device_lost,
        } = init;
        let construction_started = Instant::now();
        let device_errors = Arc::new(DeviceErrorSentry::default());
        if survive_gpu_errors_enabled() {
            let sentry = Arc::clone(&device_errors);
            device.on_uncaptured_error(Arc::new(move |error| sentry.record(&error)));
        }
        let composition_format = crate::offscreen::composition_format(display_format);
        device.set_device_lost_callback(move |reason, message| {
            log::error!("[gpu-device] device lost ({reason:?}): {message}");
            if let Some(on_device_lost) = &on_device_lost {
                on_device_lost();
            }
        });
        let copy_free = UploadMode::copy_free(&device, adapter_backend);
        let mut run_store = RunStore::new(
            &device,
            RunBufferMode::for_device(&device, adapter_downlevel),
            copy_free,
        );
        let uniform_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Viewport Uniform Bind Group Layout"),
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: true,
                        min_binding_size: wgpu::BufferSize::new(size_of::<Uniforms>() as u64),
                    },
                    count: None,
                }],
            });
        let image_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Image Texture Bind Group Layout"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Texture {
                            multisampled: false,
                            view_dimension: wgpu::TextureViewDimension::D2,
                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                ],
            });
        let image_nearest_sampler =
            device.create_sampler(&image_sampler_descriptor(ImageSampling::Nearest));
        let image_linear_sampler =
            device.create_sampler(&image_sampler_descriptor(ImageSampling::Linear));
        let text_glyph_atlas = TextGlyphAtlas::new(
            &device,
            &image_bind_group_layout,
            GlyphSamplers {
                nearest: &image_nearest_sampler,
                linear: &image_linear_sampler,
            },
            TEXT_GLYPH_ATLAS_MIN_SIZE,
        );
        let viewport_uniforms = ViewportUniforms::default();

        if cranpose_core::DIAGNOSTICS.glass_material_folds.equals("1") {
            cranpose_ui_graphics::set_glass_material_folds(true);
        } else if cranpose_core::DIAGNOSTICS.glass_material_folds.equals("0") {
            cranpose_ui_graphics::set_glass_material_folds(false);
        }
        log::info!(
            "[gpu-init] liquid glass material folds {}",
            if cranpose_ui_graphics::glass_material_folds_enabled() {
                "on: a pipeline per material's feature set"
            } else {
                "off: one pipeline per blend mode"
            }
        );

        #[cfg(not(target_arch = "wasm32"))]
        let crate::pipeline_disk_cache::Loaded {
            cache: pipeline_cache,
            records,
            updated,
            first_screen: first_screen_pipelines,
        } = crate::pipeline_disk_cache::load(&device);
        #[cfg(not(target_arch = "wasm32"))]
        let first_screen: Vec<u64> = records
            .shapes
            .iter()
            .filter(|shape| shape.first_screen)
            .map(|shape| shape.entry)
            .collect();
        #[cfg(target_arch = "wasm32")]
        let (pipeline_cache, first_screen): (Option<wgpu::PipelineCache>, Vec<u64>) =
            (None, Vec::new());
        let recorder = crate::pipeline_recorder::PipelineRecorder::default();
        #[cfg(not(target_arch = "wasm32"))]
        let pipeline_cache_watcher = crate::pipeline_disk_cache::spawn_persist_watcher(
            pipeline_cache.clone(),
            Arc::clone(&records),
            recorder.clone(),
        );

        let effects_started = Instant::now();
        let pipeline_compiler =
            PipelineCompiler::for_compilation(pipeline_compilation, adapter_backend);
        let depth_gate = crate::depth_gate::DepthGate::new(
            crate::draw_pass::lays_interiors_first(adapter_backend),
            pipeline_compiler.is_active(),
        );
        let effect_renderer = EffectRenderer::new(
            &device,
            pipeline_compiler.clone(),
            pipeline_cache.clone(),
            composition_format,
            adapter_backend,
            recorder.clone(),
        );
        let output_converter = OutputConverter::new(&device, adapter_backend, display_format);
        let screenshot_converter =
            OutputConverter::new(&device, adapter_backend, wgpu::TextureFormat::Rgba8Unorm);
        let effects_ms = instant_ms(effects_started, Instant::now());
        let shape_shader = SharedShader::new(
            &device,
            adapter_backend,
            "Shape Shader",
            shape_shader_code(run_store.mode()),
            &[Some(&uniform_bind_group_layout), Some(run_store.layout())],
        );
        if run_store.mode().trig_fill {
            let fill = ArcTrigFill::new(
                &device,
                adapter_backend,
                pipeline_cache.clone(),
                shape_shader.clone(),
                recorder.clone(),
            );
            if first_screen.contains(&crate::arc_trig_fill::FIRST_SCREEN_KEY) {
                fill.warm(&pipeline_compiler);
            }
            run_store.attach_trig_fill(fill);
        }
        let shape_pipelines = ShapePipelines::new(
            ShapePipelineFactory {
                device: Arc::clone(&device),
                cache: pipeline_cache.clone(),
                format: composition_format,
                shader: shape_shader,
                mode: run_store.mode(),
            },
            adapter_backend,
            &pipeline_compiler,
            first_screen
                .into_iter()
                .filter(|&bits| bits != crate::arc_trig_fill::FIRST_SCREEN_KEY)
                .filter_map(ShapePipelineKey::from_bits),
            recorder.clone(),
        );
        let layouts = (&uniform_bind_group_layout, &image_bind_group_layout);
        let [image_shader, glyph_atlas_shader, rrect_shadow_shader] =
            renderer_pipelines::overlay_shaders(&device, adapter_backend, layouts);

        let mut renderer = Self {
            queue,
            device_errors,
            renderer_epoch,
            #[cfg(not(target_arch = "wasm32"))]
            display_format,
            composition_target: None,
            output_converter,
            screenshot_converter,
            pipelines: RendererPipelines {
                device: Arc::clone(&device),
                composition_format,
                adapter_backend,
                pipeline_cache,
                #[cfg(not(target_arch = "wasm32"))]
                _pipeline_cache_watcher: pipeline_cache_watcher,
                #[cfg(not(target_arch = "wasm32"))]
                recent_after_update: updated.then(|| Arc::clone(&records)),
                recorder,
                #[cfg(not(target_arch = "wasm32"))]
                landing: pipeline_compiler.landing(),
                compiler: pipeline_compiler,
                #[cfg(not(target_arch = "wasm32"))]
                built_at_frame_start: 0,
                drew_placeholder: Cell::new(false),
                shape_pipelines,
                image_pipeline: IMAGE_SRC_OVER_PIPELINES.map(FixedPipeline::new),
                image_pipeline_dst_out: IMAGE_DST_OUT_PIPELINES.map(FixedPipeline::new),
                glyph_atlas_pipeline: GLYPH_ATLAS_PIPELINES.map(FixedPipeline::new),
                rrect_shadow_pipeline: RRECT_SHADOW_PIPELINES.map(FixedPipeline::new),
                #[cfg(not(target_arch = "wasm32"))]
                mesh_pipelines: MESH_FALLBACK_PIPELINES.map(FixedPipeline::new),
                #[cfg(not(target_arch = "wasm32"))]
                instant: crate::instant_triangles::InstantPipelines::new(
                    &device,
                    adapter_backend,
                    layouts,
                ),
                image_shader,
                glyph_atlas_shader,
                rrect_shadow_shader,
                uniform_bind_group_layout,
                image_bind_group_layout,
                image_nearest_sampler,
                image_linear_sampler,
            },
            device,
            depth_targets: DepthTargets::default(),
            scene_pass: crate::scene_pass::ScenePass::default(),
            depth_gate,
            surface_atlas_sizes: crate::frame::SurfaceAtlasSizes::for_backend(adapter_backend),
            text: TextResources::new(text_glyph_atlas, copy_free),
            viewport_uniforms,
            run_store,
            images: ImageTextures::new(),
            copy_free,
            pass_scratch: crate::draw_pass::PassScratch::default(),
            stage_side_scratch: StageSideScratch::default(),
            deferred_offscreen_releases: Vec::new(),
            effect_renderer,
            layer_cache: LayerCache::new(),
            ablation: Ablation::default(),
            ablation_frames: 0,
            nesting_overflow_reported: false,
            window_damage: WindowDamage::default(),
            swapchain: SwapchainHistory::default(),
            cleared: ClearedImages::default(),
            window_fill: None,
            gates: admission_gate::AdmissionGates::default(),
            transparent_sources: HashMap::default(),
            shadow_surfaces: ShadowSurfaces::new(),
            frame_stats: gpu_stats::FrameStats::default(),
            history: FrameHistory::default(),
            #[cfg(not(target_arch = "wasm32"))]
            first_screen_recorded: records.shapes.iter().any(|shape| shape.first_screen),
            shader_warm_ups_queued: 0,
            drawn_scene_update: None,
        };
        #[cfg(not(target_arch = "wasm32"))]
        renderer.warm_first_screen(&records, first_screen_pipelines);
        renderer.warm_requested_shaders();
        log::info!(
            "[gpu-init] {:?} renderer ready in {:.1} ms (effects {:.1} ms)",
            adapter_backend,
            instant_ms(construction_started, Instant::now()),
            effects_ms,
        );
        renderer
    }

    /// Prepares the first screen the last launches recorded: its pipelines
    /// queue behind the stand-ins, and its first frame waits for none of
    /// them; draws still compiling take stand-ins or placeholders, or are left
    /// out until theirs land. A driver cache that holds the recorded pipelines
    /// builds them as cache hits, which need no stand-in.
    #[cfg(not(target_arch = "wasm32"))]
    fn warm_first_screen(
        &mut self,
        records: &crate::pipeline_records::PipelineRecords,
        first_screen: crate::pipeline_disk_cache::FirstScreenPipelines,
    ) {
        self.pipelines.warm_instant_tier(first_screen);
        self.warm_fixed_pipelines(&|label| {
            records
                .fixed
                .iter()
                .any(|fixed| fixed.first_screen && fixed.entry == label)
        });
        if first_screen == crate::pipeline_disk_cache::FirstScreenPipelines::Cached {
            self.effect_renderer.trust_cached(records);
        }
        self.effect_renderer.warm_first_screen_shaders(
            records
                .shaders
                .iter()
                .filter(|shader| shader.first_screen)
                .map(|shader| &shader.entry),
        );
        if !records.shapes.iter().any(|shape| shape.first_screen) {
            self.warm_every_screen();
        }
    }

    /// Queues, for a launch with no record of its first screen, the
    /// pipelines most screens draw with that take no fallback triangles:
    /// text on whole pixels and turned, layer composites, rounded images
    /// and shadows, then flat solid rects. They build while the first
    /// screen composes, when the compiler's threads have nothing else, so
    /// its first frame draws them. On a Mate 20 X the gauntlet's first
    /// frame then drew its whole screen, its shapes as fallback triangles,
    /// where without them its layers, turned text, avatars and shadows
    /// joined over the next 150 ms.
    #[cfg(not(target_arch = "wasm32"))]
    fn warm_every_screen(&mut self) {
        let lane = CompileLane::WarmUp;
        let (compiler, backend) = (&self.pipelines.compiler, self.pipelines.adapter_backend);
        for quads in [GlyphQuads::Aligned, GlyphQuads::Turned] {
            self.pipelines
                .glyph_atlas_pipeline_resource(PassDepth::Off, quads)
                .queue(
                    compiler,
                    lane,
                    backend,
                    self.pipelines
                        .glyph_atlas_pipeline_job(PassDepth::Off, quads),
                );
        }
        self.effect_renderer.warm_composites(&self.device);
        let rounded = ImageVariant {
            depth: PassDepth::Off,
            alpha_mask: false,
            rounded: true,
        };
        self.pipelines
            .image_pipeline_resource(BlendMode::SrcOver, rounded)
            .queue(
                compiler,
                lane,
                backend,
                self.pipelines
                    .image_pipeline_job(BlendMode::SrcOver, rounded),
            );
        self.pipelines.rrect_shadow_pipeline[PassDepth::Off.index()].queue(
            compiler,
            lane,
            backend,
            self.pipelines.rrect_shadow_pipeline_job(PassDepth::Off),
        );
        self.pipelines
            .shape_pipelines
            .warm(ShapePipelineKey::SOLID_RECT);
    }

    /// Queues the shader warm-ups requested since the last call, each at the
    /// target it draws to (`cranpose_ui_graphics::request_shader_warm_ups`).
    fn warm_requested_shaders(&mut self) {
        let requested = cranpose_ui_graphics::shader_warm_ups_after(self.shader_warm_ups_queued);
        if requested.is_empty() {
            return;
        }
        self.shader_warm_ups_queued += requested.len();
        self.effect_renderer.warm_shaders(&requested);
    }

    /// Queues every pipeline recent launches of another build drew with:
    /// after an update their compiled pipelines are gone, and each would
    /// otherwise compile inside the first frame that draws with it.
    #[cfg(not(target_arch = "wasm32"))]
    fn warm_recent(&mut self, records: &crate::pipeline_records::PipelineRecords) {
        self.warm_fixed_pipelines(&|label| records.fixed.iter().any(|fixed| fixed.entry == label));
        let shapes = records
            .shapes
            .iter()
            .map(|shape| shape.entry)
            .filter(|&bits| bits != crate::arc_trig_fill::FIRST_SCREEN_KEY)
            .filter_map(ShapePipelineKey::from_bits);
        for key in shapes {
            self.pipelines.shape_pipelines.warm(key);
        }
        self.effect_renderer
            .warm_recorded_shaders(records.shaders.iter().map(|shader| &shader.entry));
    }

    /// Queues the fixed pipelines whose labels `wanted` accepts.
    #[cfg(not(target_arch = "wasm32"))]
    fn warm_fixed_pipelines(&self, wanted: &dyn Fn(&str) -> bool) {
        let compiler = self.effect_renderer.compiler();
        self.for_each_fixed_pipeline(&mut |pipeline, job| {
            if wanted(pipeline.label()) {
                pipeline.queue(
                    compiler,
                    CompileLane::WarmUp,
                    self.pipelines.adapter_backend,
                    job(),
                );
            }
        });
    }

    /// Builds here, ahead of the first frame, the pipelines named by the
    /// first screens in `records` that this renderer's backend drew (see
    /// [`crate::WgpuRenderer::build_first_screen_pipelines`]).
    pub(crate) fn build_first_screen(&mut self, records: &[u8], viewport: (u32, u32)) {
        let records = crate::pipeline_records::decode_first_screen(
            records,
            self.pipelines.adapter_backend,
            viewport,
        );
        for shape in &records.shapes {
            if let Some(key) = ShapePipelineKey::from_bits(shape.entry) {
                self.pipelines.shape_pipelines.build_ahead(key);
            }
        }
        self.for_each_fixed_pipeline(&mut |pipeline, job| {
            if records
                .fixed
                .iter()
                .any(|fixed| fixed.entry == pipeline.label())
            {
                pipeline.build_ahead(
                    &self.pipelines.recorder,
                    self.pipelines.adapter_backend,
                    job(),
                );
            }
        });
    }

    /// See [`crate::WgpuRenderer::first_screen_recorded`].
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn first_screen_recorded(&self) -> bool {
        self.first_screen_recorded
    }

    /// Hands `visit` every fixed pipeline, the effect renderer's too, with
    /// the job that builds it.
    fn for_each_fixed_pipeline(
        &self,
        visit: &mut dyn FnMut(&FixedPipeline, &dyn Fn() -> FixedPipelineJob),
    ) {
        self.effect_renderer.for_each_fixed(&self.device, visit);
        for depth in PassDepth::BOTH {
            for (alpha_mask, rounded) in
                [(false, false), (true, false), (false, true), (true, true)]
            {
                let variant = ImageVariant {
                    depth,
                    alpha_mask,
                    rounded,
                };
                for blend_mode in [BlendMode::SrcOver, BlendMode::DstOut] {
                    visit(
                        self.pipelines.image_pipeline_resource(blend_mode, variant),
                        &|| Box::new(self.pipelines.image_pipeline_job(blend_mode, variant)),
                    );
                }
            }
            for quads in [GlyphQuads::Plain, GlyphQuads::Aligned, GlyphQuads::Turned] {
                visit(
                    self.pipelines.glyph_atlas_pipeline_resource(depth, quads),
                    &|| Box::new(self.pipelines.glyph_atlas_pipeline_job(depth, quads)),
                );
            }
            visit(
                &self.pipelines.rrect_shadow_pipeline[depth.index()],
                &|| Box::new(self.pipelines.rrect_shadow_pipeline_job(depth)),
            );
        }
        for kind in crate::scene_pass::ScenePipeline::ALL {
            visit(self.scene_pipeline_resource(kind), &|| {
                Box::new(self.scene_surface_pipeline_job(kind))
            });
        }
    }

    fn scene_pipeline_resource(&self, kind: crate::scene_pass::ScenePipeline) -> &FixedPipeline {
        self.scene_pass.pipeline(kind)
    }

    /// A scene pipeline for a scene's pass, or `None` while it compiles:
    /// the draws it takes are left out, and the frame after it lands draws
    /// them.
    pub(crate) fn scene_surface_pipeline(
        &self,
        kind: crate::scene_pass::ScenePipeline,
    ) -> Option<&wgpu::RenderPipeline> {
        self.pipelines.ready_or_left_out(
            &self.frame_stats,
            self.scene_pipeline_resource(kind),
            || self.scene_surface_pipeline_job(kind),
        )
    }

    /// The pipeline of `variant` of a volume shader a frame noted; `None`
    /// while it compiles, when the frame after it lands draws the volumes,
    /// or when the shader does not build.
    pub(crate) fn scene_volume_pipeline(
        &self,
        shader: &Arc<str>,
        variant: crate::scene_pass::VolumeVariant,
    ) -> Option<&wgpu::RenderPipeline> {
        let kept = self.scene_pass.volume(shader)?;
        self.pipelines.app_shader_pipeline(
            &self.frame_stats,
            kept.pipeline(variant as usize),
            || {
                crate::scene_pass::volume_pipeline_job(
                    &self.device,
                    self.pipelines.pipeline_cache.clone(),
                    (
                        self.pipelines.composition_format,
                        self.pipelines.adapter_backend,
                        variant,
                    ),
                    (self.scene_pipeline_parts(), kept),
                )
            },
        )
    }

    /// The pipeline of `variant` of a shaded surface's shader a frame
    /// noted; `None` while it compiles, when the frame after it lands draws
    /// the surfaces with it, or when the shader does not build.
    pub(crate) fn scene_shaded_pipeline(
        &self,
        shader: &Arc<str>,
        variant: crate::scene_pass::ShadedVariant,
    ) -> Option<&wgpu::RenderPipeline> {
        let kept = self.scene_pass.shaded(shader)?;
        self.pipelines.app_shader_pipeline(
            &self.frame_stats,
            kept.pipeline(variant as usize),
            || {
                crate::scene_pass::shaded_pipeline_job(
                    &self.device,
                    self.pipelines.pipeline_cache.clone(),
                    (
                        self.pipelines.composition_format,
                        self.pipelines.adapter_backend,
                        variant,
                    ),
                    (self.scene_pipeline_parts(), kept),
                )
            },
        )
    }

    /// The bind group layout of a scene surface's uniforms.
    pub(crate) fn scene_uniform_layout(&self) -> &wgpu::BindGroupLayout {
        &self.scene_pipeline_parts().uniform_layout
    }

    /// What a scene binds in its shadow map's place when it casts none.
    pub(crate) fn scene_no_shadow(&self) -> &wgpu::BindGroup {
        &self.scene_pipeline_parts().no_shadow
    }

    fn scene_pipeline_parts(&self) -> &crate::scene_pass::ScenePipelineParts {
        self.scene_pass.parts(
            &self.device,
            self.pipelines.adapter_backend,
            &self.effect_renderer.effect_texture_bind_group_layout,
        )
    }

    fn scene_surface_pipeline_job(
        &self,
        kind: crate::scene_pass::ScenePipeline,
    ) -> impl FnOnce() -> wgpu::RenderPipeline + CompilerSend + 'static {
        crate::scene_pass::scene_pipeline_job(
            &self.device,
            self.pipelines.pipeline_cache.clone(),
            (self.pipelines.composition_format, kind),
            self.scene_pipeline_parts(),
        )
    }

    /// Lays out as fallback triangles a stored run's draws in `range` of
    /// `draws` whose shape pipelines still compile.
    pub(crate) fn mesh_store_run(
        &self,
        run: &RunDraw,
        space: DrawSpace,
        range: std::ops::Range<usize>,
        draws: &[RunDrawCall],
        depth: PassDepth,
        meshes: &mut crate::mesh_fallback::PassMeshes,
    ) {
        if self.pipelines.mesh_pipeline(depth).is_none()
            || !self.pipelines.awaits_shape_pipeline(draws, range.clone())
        {
            return;
        }
        let tables = run.tables();
        let records = crate::mesh_fallback::MeshRecords {
            bodies: tables.shapes.bodies(),
            curves: tables.shapes.curves(),
            brushes: &tables.brushes,
            stops: &tables.stops,
        };
        let placement = PlacementData::of(
            &run.placement,
            space.scale,
            space.viewport.transform,
            [0.0; 2],
        )
        .mesh();
        let target = space.viewport.mesh_target(depth);
        self.pipelines
            .mesh_unready(draws, range, records, &|_| Some(placement), target, meshes);
    }

    /// Lays out as fallback triangles the arena draws in `range` of `draws`,
    /// just cut or closed, whose shape pipelines still compile.
    pub(crate) fn mesh_arena_draws(
        &self,
        viewport: ViewportUniformParams,
        range: std::ops::Range<usize>,
        draws: &[RunDrawCall],
        depth: PassDepth,
        meshes: &mut crate::mesh_fallback::PassMeshes,
    ) {
        if self.pipelines.mesh_pipeline(depth).is_none()
            || !self.pipelines.awaits_shape_pipeline(draws, range.clone())
        {
            return;
        }
        let (records, placements) = self.run_store.arena_mesh_records();
        self.pipelines.mesh_unready(
            draws,
            range,
            records,
            &|body| {
                placements
                    .get(body.placement as usize)
                    .map(PlacementData::mesh)
            },
            viewport.mesh_target(depth),
            meshes,
        );
    }

    /// Draws `instances` of the pass's uploaded shadow instances, bound with
    /// `uniform_slot` under `scissor`.
    pub(crate) fn draw_shadow_instances(
        &self,
        pass: &mut TrackedPass<'_, '_>,
        buffer: &BufferUpload,
        uniform_slot: usize,
        instances: std::ops::Range<u32>,
        scissor: Option<(u32, u32, u32, u32)>,
        frame: PassFrame,
    ) -> Result<(), String> {
        let depth = frame.depth;
        let Some(pipeline) = self.pipelines.ready_or_left_out(
            &self.frame_stats,
            &self.pipelines.rrect_shadow_pipeline[depth.index()],
            || self.pipelines.rrect_shadow_pipeline_job(depth),
        ) else {
            return Ok(());
        };
        let (x, y, width, height) = frame.scissor(scissor);
        pass.set_scissor_rect(x, y, width, height);
        pass.set_pipeline(pipeline);
        self.viewport_uniforms.bind(pass, uniform_slot)?;
        pass.set_vertex_buffer(0, buffer.slice());
        pass.draw(0..SHADOW_QUAD_CORNERS, instances);
        self.frame_stats.add_draw_calls(1);
        Ok(())
    }

    pub(crate) fn upload_shadow_instances<C: FrameCommandRecorder>(
        &self,
        recorder: &mut C,
        instances: &[ShadowInstance],
    ) -> BufferUpload {
        recorder.upload_buffer(
            UploadAllocatorSpec::vertex(
                "Shadow Instance Buffer",
                size_of::<ShadowInstance>() as u64,
            ),
            &self.device,
            bytemuck::cast_slice(instances),
        )
    }

    pub(crate) fn max_texture_dim(&self) -> u32 {
        self.effect_renderer.max_texture_dim()
    }

    /// A pooled texture that outlives the frame: layer cache entries and
    /// cached shadow surfaces.
    pub(crate) fn acquire_retained_surface(&mut self, width: u32, height: u32) -> OffscreenTarget {
        self.effect_renderer
            .acquire_offscreen(&self.device, width, height, Some(&self.frame_stats))
    }

    /// Where the frame renders: straight into an image that can take it,
    /// unless the image comes cleared and the composition target keeps the
    /// frame for it; else into the composition target.
    fn frame_root(
        &mut self,
        output: FrameOutput<'_>,
        viewport: (u32, u32),
        reads_page: bool,
    ) -> FrameRoot {
        if let (OutputMode::Display, Some(view), Some(texture)) =
            (output.mode, output.view, output.texture)
            && surface_is_direct_root(
                texture,
                self.pipelines.composition_format,
                viewport,
                reads_page,
            )
        {
            let image = Rc::new(OffscreenTarget::from_surface(texture.clone(), view.clone()));
            if output.image == ImageContent::Cleared && self.cleared.copied {
                return FrameRoot::Composition {
                    composition: self.take_composition_target(viewport.0, viewport.1),
                    cleared: Some(image),
                };
            }
            return FrameRoot::Surface { image, keep: None };
        }
        FrameRoot::Composition {
            composition: self.take_composition_target(viewport.0.max(1), viewport.1.max(1)),
            cleared: None,
        }
    }

    fn take_composition_target(&mut self, width: u32, height: u32) -> CompositionTarget {
        if let Some(target) = self.composition_target.take()
            && target.target.width == width
            && target.target.height == height
        {
            return target;
        }
        let target = Rc::new(OffscreenTarget::new(
            &self.device,
            self.pipelines.composition_format,
            width,
            height,
        ));
        let output_bind_group = self.output_converter.bind_group(&self.device, &target.view);
        CompositionTarget {
            target,
            output_bind_group,
            kept: KeptWindow::default(),
        }
    }

    fn transient_offscreen_descriptor(
        &self,
        label: &'static str,
        width: u32,
        height: u32,
    ) -> FrameTextureDescriptor {
        let max_texture_dim = self.max_texture_dim();
        FrameTextureDescriptor::render_attachment(
            label,
            width.min(max_texture_dim),
            height.min(max_texture_dim),
            self.pipelines.composition_format,
        )
    }

    /// A texture of the given size that stays transparent: the input of a
    /// runtime shader whose layer draws nothing itself, so the shader needs
    /// no surface pass and reads the same empty content every frame.
    #[inline(never)]
    pub(crate) fn transparent_source<C: FrameCommandRecorder>(
        &mut self,
        recorder: &mut C,
        width: u32,
        height: u32,
    ) -> Rc<OffscreenTarget> {
        if let Some(source) = self.transparent_sources.get(&(width, height)) {
            return Rc::clone(source);
        }
        if self.transparent_sources.len() >= MAX_TRANSPARENT_SOURCES {
            for (_, source) in self.transparent_sources.drain() {
                if let Ok(target) = Rc::try_unwrap(source) {
                    self.deferred_offscreen_releases.push(target);
                }
            }
        }
        let source = Rc::new(self.acquire_retained_surface(width, height));
        self.clear_target(
            recorder,
            &source.view,
            wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
        );
        self.transparent_sources
            .insert((width, height), Rc::clone(&source));
        source
    }

    fn defer_offscreen_release(&mut self, target: OffscreenTarget) {
        self.deferred_offscreen_releases.push(target);
    }

    fn flush_deferred_offscreen_releases(&mut self, executor: &mut WgpuFrameGraphExecutor) {
        self.gates.end_frame(&mut self.layer_cache);
        for target in self.deferred_offscreen_releases.drain(..) {
            self.effect_renderer.release_offscreen(target);
        }
        self.layer_cache.end_frame();
        for (transient, target) in self.layer_cache.take_released() {
            match transient {
                Some(descriptor) => executor.return_cached_transient(descriptor, target),
                None => self.effect_renderer.release_offscreen(target),
            }
        }
        self.effect_renderer.end_offscreen_frame();
        executor.end_transient_frame();
        self.depth_targets.end_frame();
        self.scene_pass.end_frame();
        self.surface_atlas_sizes.end_frame();
    }

    pub(crate) fn render(
        &mut self,
        submit: &mut FrameSubmit,
        viewport: (u32, u32),
        packet: FramePacket,
        surface_epoch: u64,
        returns: &mut RenderReturns,
        output: FrameOutput<'_>,
    ) -> Result<(), String> {
        let (width, height) = viewport;
        let FrameOutput {
            mode: output_mode,
            view: output_view,
            image,
            ..
        } = output;
        let cancel_reason = if packet.renderer_epoch != self.renderer_epoch {
            Some(CancelReason::RendererEpoch)
        } else if packet.surface_epoch != surface_epoch {
            Some(CancelReason::SurfaceEpoch)
        } else if packet.viewport != (width, height) {
            Some(CancelReason::Viewport)
        } else {
            None
        };
        if let Some(reason) = cancel_reason {
            return Self::cancel_packet(packet, reason, returns);
        }
        if self.device_errors.take_poison() {
            return Self::cancel_packet(packet, CancelReason::DeviceError, returns);
        }
        returns.frame_id = packet.frame_id;
        let render_start = Instant::now();
        #[cfg(not(target_arch = "wasm32"))]
        let builds_before = thread_builds();
        self.warm_requested_shaders();
        self.pipelines.shape_pipelines.begin_frame();
        self.pipelines.recorder.begin_frame((width, height));
        self.begin_placeholder_frame();
        self.viewport_uniforms.begin_frame();
        self.run_store.begin_frame(gpu_stats_enabled());
        self.text.begin_text_glyph_run_frame();
        self.pass_scratch.begin_frame();

        let reads_page = packet.root.contains_backdrop()
            || packet
                .overlay
                .as_ref()
                .is_some_and(|overlay| overlay.contains_backdrop());
        let mut frame_root = self.frame_root(output, (width, height), reads_page);
        let plan = self.damage_of(
            &packet,
            &mut frame_root,
            output_mode,
            (width, height),
            (surface_epoch, image),
        );
        let repaint = self.count_damage(plan, (width, height));
        let root = frame_root.target();
        let screenshot_bind_group = output_view.and_then(|_| {
            matches!(output_mode, OutputMode::Screenshot).then(|| {
                self.screenshot_converter
                    .bind_group(&self.device, &root.view)
            })
        });
        let output = frame_root.output(output_view, screenshot_bind_group.as_ref());
        let submitted = self.render_graph(submit, root, packet, repaint, returns, output);
        #[cfg(not(target_arch = "wasm32"))]
        self.effect_renderer
            .compiler()
            .spread_demand(thread_builds() == builds_before);
        if let Some(composition) = frame_root.into_composition() {
            self.composition_target = Some(composition);
        }
        let after_graph = Instant::now();
        if self.effect_renderer.take_skipped_draw() {
            self.pipelines.note_placeholder(&self.frame_stats);
        }
        self.flush_deferred_offscreen_releases(&mut submit.executor);
        self.pass_scratch.glyph_instances.end_frame();

        self.record_pool_sizes(&submit.executor);
        self.effect_renderer
            .merge_and_reset_debug_counters(&self.frame_stats);
        self.keep_window(plan, &submitted);
        let result = submitted.map(drop);
        submit.executor.reset_upload_allocators();
        self.close_frame_stats(submit);
        let after_stats = Instant::now();
        if let Some(total_ms) = should_log_wgpu_render_stage(render_start, after_stats) {
            log::warn!(
                "[wgpu-render-stage:render] total_ms={total_ms:.2} graph_ms={:.2} cleanup_stats_ms={:.2}",
                instant_ms(render_start, after_graph),
                instant_ms(after_graph, after_stats),
            );
        }
        if result.is_ok() {
            returns.outcome = PresentOutcome::Presented;
        }
        result
    }

    // Notes the sizes of the caches and pools the frame left.
    fn record_pool_sizes(&self, executor: &WgpuFrameGraphExecutor) {
        self.frame_stats
            .layer_cache_size
            .set(self.layer_cache.len() as u32);
        self.frame_stats
            .layer_cache_bytes
            .set(self.layer_cache.bytes());
        self.frame_stats.offscreen_pool_size.set(
            self.effect_renderer
                .retained_offscreen_count()
                .saturating_add(executor.retained_texture_count())
                .saturating_add(usize::from(self.composition_target.is_some())) as u32,
        );
        self.frame_stats.offscreen_pool_bytes.set(
            (self.effect_renderer.retained_offscreen_bytes() as u64)
                .saturating_add(executor.retained_texture_bytes())
                .saturating_add(self.composition_target.as_ref().map_or(0, |target| {
                    u64::from(target.target.width)
                        .saturating_mul(u64::from(target.target.height))
                        .saturating_mul(COMPOSITION_BYTES_PER_PIXEL)
                })),
        );
        self.frame_stats
            .text_pool_size
            .set(self.text.image_cache.len() as u32);
        self.frame_stats
            .image_cache_size
            .set(self.images.len() as u32);
    }

    // Takes the frame's stats, logs and prints them as asked, and resets
    // them for the next frame.
    fn close_frame_stats(&mut self, submit: &FrameSubmit) {
        self.history.close(&self.frame_stats, &self.device);
        submit.end_pass_timing_frame();
        self.frame_stats.reset();
    }

    /// Returns a packet unrendered, handing its scene back for recycling.
    pub(crate) fn cancel_packet(
        packet: FramePacket,
        reason: CancelReason,
        returns: &mut RenderReturns,
    ) -> Result<(), String> {
        returns.scene = Some(FrameSceneStorage {
            root: packet.root,
            overlay: packet.overlay,
        });
        returns.frame_id = packet.frame_id;
        returns.outcome = PresentOutcome::Cancelled(reason);
        Ok(())
    }

    // The parts a draw of text or an image borrows apart.
    pub(crate) fn draw_parts(&mut self) -> DrawParts<'_> {
        DrawParts {
            text: &mut self.text,
            images: &mut self.images,
            upload: FrameUploads {
                pipelines: &self.pipelines,
                uniforms: &mut self.viewport_uniforms,
                queue: &self.queue,
                stats: &mut self.frame_stats,
            },
        }
    }

    pub(crate) fn last_frame_stats(&self) -> Option<gpu_stats::FrameStatsSnapshot> {
        self.history.last()
    }

    pub(crate) fn needs_frame_warmup(&self) -> bool {
        self.history.warming_up() || self.pipelines.placeholder_awaits_frame()
    }

    /// Starts a frame: once a pipeline a placeholder waited for has landed,
    /// the retained layers, which may hold that placeholder, are dropped.
    fn begin_placeholder_frame(&mut self) {
        self.pipelines.drew_placeholder.set(false);
        #[cfg(not(target_arch = "wasm32"))]
        if let Some(landing) = &self.pipelines.landing {
            self.pipelines.built_at_frame_start = landing.built();
            if landing.take_landed() {
                self.drop_kept_drawing();
            }
        }
    }

    /// Drops what earlier frames drew and kept, the layers and the blurred
    /// shadows: pipelines the browser compiled in the background landed, and
    /// those were drawn while the draws that use them were left out.
    #[cfg(target_arch = "wasm32")]
    pub(crate) fn background_pipelines_landed(&mut self) {
        self.drop_kept_drawing();
    }

    /// Drops the layers, the blurred shadows and the window earlier frames
    /// drew and kept.
    fn drop_kept_drawing(&mut self) {
        self.layer_cache.clear();
        self.shadow_surfaces.clear();
        if let Some(composition) = self.composition_target.as_mut() {
            composition.kept.forget();
        }
        self.swapchain.forget();
    }

    /// What `packet` changes against the frame `frame_root` kept: a frame
    /// displayed through the composition target, or into a swapchain `image`
    /// that holds an earlier frame, draws only that, while a screenshot is
    /// drawn whole and so is a frame into any other presentable image. An
    /// image that comes cleared takes the frame from the composition target
    /// while the frames change a small part of a heavy window.
    fn damage_of(
        &mut self,
        packet: &FramePacket,
        frame_root: &mut FrameRoot,
        output_mode: OutputMode,
        viewport: (u32, u32),
        (surface_epoch, image): (u64, ImageContent),
    ) -> Option<FrameDamagePlan> {
        match frame_root {
            FrameRoot::Composition { composition, .. }
                if matches!(output_mode, OutputMode::Screenshot) =>
            {
                composition.kept.forget();
                None
            }
            FrameRoot::Composition {
                composition,
                cleared: Some(cleared),
            } => {
                let frame = self.frame_window(packet, viewport);
                let damage =
                    self.window_damage
                        .begin_as(&frame, &mut composition.kept, Summaries::COPIED);
                if matches!(damage, Damage::Part(_)) {
                    return Some(FrameDamagePlan {
                        damage,
                        repaint: damage.repaint(),
                        keeper: Keeper::Composition,
                    });
                }
                // The frame changes most of the window, or costs less drawn
                // whole than copied: it draws straight into the image, and
                // the composition target lets the frame go.
                self.cleared.copied = false;
                *frame_root = FrameRoot::Surface {
                    image: Rc::clone(cleared),
                    keep: None,
                };
                Some(FrameDamagePlan {
                    damage,
                    repaint: Repaint::Whole,
                    keeper: Keeper::Cleared { keeping: false },
                })
            }
            FrameRoot::Composition { composition, .. } => {
                // The swapchain image the frame is copied into holds a frame
                // the history does not follow.
                self.swapchain.forget();
                let frame = self.frame_window(packet, viewport);
                let damage = self.window_damage.begin(&frame, &mut composition.kept);
                Some(FrameDamagePlan {
                    damage,
                    repaint: damage.repaint(),
                    keeper: Keeper::Composition,
                })
            }
            FrameRoot::Surface { image: target, .. } => {
                let image = match image {
                    ImageContent::Untracked => return None,
                    ImageContent::Numbered(image) => image,
                    ImageContent::Cleared => {
                        let frame = self.frame_window(packet, viewport);
                        let (damage, keeping) =
                            self.cleared.damage(&mut self.window_damage, &frame);
                        if keeping {
                            *frame_root = FrameRoot::Surface {
                                image: Rc::clone(target),
                                keep: Some(self.take_composition_target(viewport.0, viewport.1)),
                            };
                        }
                        return Some(FrameDamagePlan {
                            damage,
                            repaint: Repaint::Whole,
                            keeper: Keeper::Cleared { keeping },
                        });
                    }
                };
                self.swapchain.begin(surface_epoch);
                if !image.kept {
                    // Drawn whole, and only a frame a lap of images later
                    // could draw over it, so the first frames of a surface
                    // take no summary.
                    self.swapchain.drop_image(image);
                    return None;
                }
                let frame = self.frame_window(packet, viewport);
                let damage = self.window_damage.begin(&frame, &mut self.swapchain.kept);
                let repaint = self
                    .swapchain
                    .changed_since(image, damage)
                    .map_or(Repaint::Whole, |changed| {
                        self.window_damage.repaint(changed, viewport)
                    });
                Some(FrameDamagePlan {
                    damage,
                    repaint,
                    keeper: Keeper::Image(image),
                })
            }
        }
    }

    // Whether `packet` draws what the surface shows, so it takes no image and
    // no present; the frame then closes as drawn for the renderer's warm-up.
    pub(crate) fn shows(&mut self, packet: &FramePacket, viewport: (u32, u32)) -> bool {
        let frame = self.frame_window(packet, viewport);
        let shown = self.swapchain.shows(&frame, &mut self.window_damage);
        if shown {
            self.history.skip_frame();
        }
        shown
    }

    fn frame_window<'p>(&self, packet: &'p FramePacket, viewport: (u32, u32)) -> FrameWindow<'p> {
        FrameWindow {
            packet,
            viewport,
            setting: self.text.frame_setting(packet.clear),
        }
    }

    /// Counts what a frame of `plan` changes and draws of a page of
    /// `viewport` in the frame's stats, and returns what it draws.
    fn count_damage(&self, plan: Option<FrameDamagePlan>, viewport: (u32, u32)) -> Repaint {
        self.frame_stats
            .damage
            .set(plan.map_or(gpu_stats::FrameDamage::Untracked, |plan| {
                plan.damage.stats()
            }));
        let repaint = plan.map_or(Repaint::Whole, |plan| plan.repaint);
        self.frame_stats
            .repainted_pixels
            .set(repaint.pixels(viewport));
        repaint
    }

    /// Keeps the summary of the frame of `plan` just drawn into its target
    /// when it was drawn whole, with no stand-in. A frame that draws nothing
    /// into a presentable image submits nothing, and is drawn all the same.
    fn keep_window(&mut self, plan: Option<FrameDamagePlan>, submitted: &Result<bool, String>) {
        let Some(plan) = plan else {
            return;
        };
        let drawn = match submitted {
            Ok(submitted) => *submitted || plan.repaint == Repaint::Nothing,
            Err(_) => false,
        } && !self.frame_stats.drew_stand_in();
        match plan.keeper {
            Keeper::Composition => {
                if let Some(composition) = self.composition_target.as_mut() {
                    self.window_damage.end(&mut composition.kept, drawn);
                }
            }
            Keeper::Image(image) => {
                self.swapchain.record(image, plan.damage, drawn);
                self.window_damage.end(&mut self.swapchain.kept, drawn);
            }
            Keeper::Cleared { keeping } => {
                self.window_damage.end(&mut self.cleared.kept, drawn);
                if keeping && let Some(composition) = self.composition_target.as_mut() {
                    self.cleared.hand_over(&mut composition.kept);
                }
            }
        }
    }

    /// Lets go of the copy of the last frame the renderer keeps for a
    /// surface whose images come cleared, as a WebGPU canvas's do: for a
    /// runtime whose app holds still, so the copy's window of memory goes.
    /// The next frame draws whole into its image.
    pub(crate) fn release_kept_copy(&mut self) {
        if !self.cleared.copied {
            return;
        }
        match self.composition_target.take() {
            Some(mut composition) => self.cleared.take_back(&mut composition.kept),
            None => self.cleared.copied = false,
        }
    }

    /// Queues every pipeline the cache file records, as the first frame of
    /// an updated build does, and returns whether any is left to build: for
    /// a renderer that prepares an app's pipelines without drawing.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn prepare_recorded(&mut self) -> bool {
        self.effect_renderer.warm_after_first_frame();
        if let Some(records) = self.pipelines.recent_after_update.take() {
            self.warm_recent(&records);
        }
        self.pipelines.compiling()
    }

    pub(crate) fn debug_cpu_allocation_stats(&self) -> DebugCpuAllocationStats {
        DebugCpuAllocationStats {
            scene_graph_node_count: 0,
            scene_graph_heap_bytes: 0,
            scene_hits: cranpose_render_common::graph_scene::HitStorage::default(),
            text_renderer_pool_len: self.text.image_cache.len(),
            text_renderer_pool_cap: self.text.image_cache.cap().get(),
            image_texture_cache_len: self.images.len(),
            image_texture_cache_cap: self.images.capacity(),
            run_arena_staging_bytes: self.run_store.arena_staging_bytes(),
            run_store_bytes: self.run_store.stored_bytes(),
            run_store_runs: self.run_store.stored_count(),
            scratch_image_vertices_cap: self.pass_scratch.images.vertices.capacity(),
            scratch_image_indices_cap: self.pass_scratch.images.indices.capacity(),
            scratch_image_cmds_cap: self.pass_scratch.images.cmds.capacity(),
            scratch_glyph_instances_cap: self.pass_scratch.glyph_instances.plain.capacity()
                + self.pass_scratch.glyph_instances.turned.capacity(),
            layer_cache_len: self.layer_cache.len(),
            layer_cache_bytes: self.layer_cache.bytes(),
        }
    }
    pub(crate) fn render_to_rgba_pixels(
        &mut self,
        submit: &mut FrameSubmit,
        viewport: (u32, u32),
        packet: FramePacket,
        surface_epoch: u64,
        returns: &mut RenderReturns,
    ) -> Result<Vec<u8>, String> {
        let (width, height) = viewport;
        if width == 0 || height == 0 {
            return Err("Screenshot size must be non-zero".to_string());
        }
        let errors_before = self.device_errors.error_count();

        let output_texture = crate::offscreen::create_2d_texture(
            &self.device,
            wgpu::TextureFormat::Rgba8Unorm,
            width,
            height,
            wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
            Some("Screenshot Output Texture"),
        );
        let output_view = output_texture.create_view(&wgpu::TextureViewDescriptor::default());
        let output = FrameOutput {
            mode: OutputMode::Screenshot,
            view: Some(&output_view),
            texture: None,
            image: ImageContent::Untracked,
        };
        self.render(submit, viewport, packet, surface_epoch, returns, output)?;
        if returns.outcome != PresentOutcome::Presented {
            return Err(format!(
                "Screenshot rendering did not complete: {:?}",
                returns.outcome
            ));
        }
        self.read_screenshot_pixels(submit, &output_texture, errors_before)
    }

    fn read_screenshot_pixels(
        &mut self,
        submit: &mut FrameSubmit,
        output_texture: &wgpu::Texture,
        errors_before: u64,
    ) -> Result<Vec<u8>, String> {
        let width = output_texture.width();
        let height = output_texture.height();
        let bytes_per_pixel = 4u32;
        let unpadded_bytes_per_row = width
            .checked_mul(bytes_per_pixel)
            .ok_or_else(|| "Screenshot row byte size overflow".to_string())?;
        let padded_bytes_per_row =
            align_to(unpadded_bytes_per_row, wgpu::COPY_BYTES_PER_ROW_ALIGNMENT);
        let output_buffer_size = padded_bytes_per_row as u64 * height as u64;

        let output_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Screenshot Readback Buffer"),
            size: output_buffer_size,
            usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });

        let mut graph = WgpuFrameGraph::new(Some("Screenshot Copy Encoder"));
        let source = graph.import_surface("screenshot-copy-source");
        graph.add_fallible_command_pass(Some("Screenshot Copy Pass"), &[source], &[], |context| {
            context.encoder.copy_texture_to_buffer(
                wgpu::TexelCopyTextureInfo {
                    texture: output_texture,
                    mip_level: 0,
                    origin: wgpu::Origin3d::ZERO,
                    aspect: wgpu::TextureAspect::All,
                },
                wgpu::TexelCopyBufferInfo {
                    buffer: &output_buffer,
                    layout: wgpu::TexelCopyBufferLayout {
                        offset: 0,
                        bytes_per_row: Some(padded_bytes_per_row),
                        rows_per_image: Some(height),
                    },
                },
                wgpu::Extent3d {
                    width,
                    height,
                    depth_or_array_layers: 1,
                },
            );
            Ok(())
        });
        let execution = submit.execute(graph);
        self.recall_frame_uploads();
        let execution = execution.map_err(|error| error.to_string())?;
        let submission_index = execution.submission;
        let copy_stats = execution.stats;
        self.history.add_command_stats(copy_stats);

        let buffer_slice = output_buffer.slice(..);
        let (tx, rx) = mpsc::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = tx.send(result);
        });
        self.device
            .poll(wgpu::PollType::Wait {
                submission_index: Some(submission_index),
                timeout: None,
            })
            .map_err(|error| format!("Screenshot GPU wait failed: {error}"))?;

        match rx.recv_timeout(Duration::from_secs(3)) {
            Ok(Ok(())) => {}
            Ok(Err(err)) => return Err(format!("Screenshot map_async failed: {err:?}")),
            Err(err) => return Err(format!("Screenshot readback timed out: {err}")),
        }
        if self.device_errors.error_count() != errors_before {
            output_buffer.unmap();
            return Err("GPU errors occurred while capturing the screenshot".to_string());
        }

        let mapped = buffer_slice
            .get_mapped_range()
            .map_err(|err| format!("Screenshot readback could not be read: {err}"))?;
        let mut pixels = vec![0u8; (width as usize) * (height as usize) * 4];

        let src_row_len = padded_bytes_per_row as usize;
        let dst_row_len = unpadded_bytes_per_row as usize;
        for row in 0..height as usize {
            let src_offset = row * src_row_len;
            let dst_offset = row * dst_row_len;
            pixels[dst_offset..dst_offset + dst_row_len]
                .copy_from_slice(&mapped[src_offset..src_offset + dst_row_len]);
        }
        drop(mapped);
        output_buffer.unmap();

        self.convert_surface_pixels_to_rgba(&pixels)
    }

    /// Asks the buffers the submitted frame unmapped back for later frames.
    fn recall_frame_uploads(&mut self) {
        self.viewport_uniforms.uploads.recall();
        self.run_store.recall();
    }

    /// Records and submits the frame: whether it submitted anything.
    fn render_graph(
        &mut self,
        submit: &mut FrameSubmit,
        root_target: &Rc<OffscreenTarget>,
        packet: FramePacket,
        repaint: Repaint,
        returns: &mut RenderReturns,
        output: OutputStep<'_>,
    ) -> Result<bool, String> {
        #[cfg(target_arch = "wasm32")]
        let graph_start = Instant::now();
        let scene_motion =
            packet.scene_motion(self.drawn_scene_update.replace(packet.scene_update));
        let FramePacket {
            root,
            overlay,
            root_scale,
            clear,
            ..
        } = packet;
        let page = Rc::clone(root_target);
        let draw = FrameDraw {
            root: &root,
            overlay: overlay.as_deref(),
            root_scale,
            clear,
            repaint,
            scene_motion,
        };

        #[cfg(not(target_arch = "wasm32"))]
        let (result, submitted) = self.execute_frame_graph(submit, draw, &page, output);

        #[cfg(target_arch = "wasm32")]
        let (result, submitted) = {
            let (result, execution) = {
                let mut frame_encoder = submit.begin(Some("Renderer Frame Encoder"));
                let initial_pass_count = frame_encoder.recorded_pass_count();
                let result = self.encode_frame(&mut frame_encoder, draw, Rc::clone(&page), output);
                let execution =
                    if result.is_ok() && frame_encoder.recorded_pass_count() > initial_pass_count {
                        Some(frame_encoder.finish())
                    } else {
                        None
                    };
                (result, execution)
            };
            let after_execute = Instant::now();
            if let Some(total_ms) = should_log_wgpu_render_stage(graph_start, after_execute) {
                log::warn!("[wgpu-render-stage:graph] total_ms={total_ms:.2}",);
            }
            if let Some(execution) = &execution {
                self.frame_stats.record_command_stats(execution.stats);
                self.pipelines.compiler.watch_paced_landing(&self.queue);
            }
            (result, execution.is_some())
        };
        #[cfg(not(target_arch = "wasm32"))]
        if submitted {
            self.pipelines.recorder.note_frame_drawn();
            self.effect_renderer.warm_after_first_frame();
            if let Some(records) = self.pipelines.recent_after_update.take() {
                self.warm_recent(&records);
            }
        }
        if !submitted {
            self.run_store.invalidate_uploads();
            self.text.glyph_gpu_run_cache.clear();
            self.text.glyph_run_arena = GlyphRunArena::new(self.copy_free);
        }
        returns.scene = Some(FrameSceneStorage { root, overlay });
        result.map(|()| submitted)
    }

    /// Records and submits the frame through the frame graph: whether it
    /// recorded without error, and whether it submitted anything.
    #[cfg(not(target_arch = "wasm32"))]
    fn execute_frame_graph(
        &mut self,
        submit: &mut FrameSubmit,
        draw: FrameDraw<'_>,
        page: &Rc<OffscreenTarget>,
        output: OutputStep<'_>,
    ) -> (Result<(), String>, bool) {
        let graph_start = Instant::now();
        let mut frame_graph = WgpuFrameGraph::new(Some("Renderer Frame Graph"));
        let surface = frame_graph.import_surface("renderer-surface");
        frame_graph.add_fallible_recorded_command_pass(
            Some("Renderer Frame Pass"),
            &[],
            &[surface],
            |frame_encoder| self.encode_frame(frame_encoder, draw, Rc::clone(page), output),
        );
        let after_build = Instant::now();
        let execution = submit.execute(frame_graph);
        let after_execute = Instant::now();
        self.recall_frame_uploads();
        if let Some(total_ms) = should_log_wgpu_render_stage(graph_start, after_execute) {
            log::warn!(
                "[wgpu-render-stage:graph] total_ms={total_ms:.2} build_ms={:.2} execute_ms={:.2}",
                instant_ms(graph_start, after_build),
                instant_ms(after_build, after_execute),
            );
        }
        match execution {
            Ok(execution) => {
                if execution.stats.pass_count > 0 {
                    self.frame_stats.record_command_stats(execution.stats);
                }
                (Ok(()), true)
            }
            Err(crate::frame_graph::FrameGraphError::NoDeclaredPasses) => (Ok(()), false),
            Err(error) => (Err(error.to_string()), false),
        }
    }

    /// Records the frame: the root and overlay layer scenes into the frame's
    /// target, the output conversion when the target is not the presented
    /// image, and the viewport uniforms the recorded passes claimed.
    fn encode_frame<C: FrameCommandRecorder>(
        &mut self,
        recorder: &mut C,
        draw: FrameDraw<'_>,
        page: Rc<OffscreenTarget>,
        output: OutputStep<'_>,
    ) -> Result<(), String> {
        let scene_motion = draw.scene_motion;
        FrameExecutor::new(self, recorder).render_frame(
            draw.root,
            draw.overlay,
            page,
            draw.root_scale,
            wgpu::LoadOp::Clear(draw.clear),
            draw.repaint,
        )?;
        match output {
            OutputStep::None => {}
            OutputStep::Convert {
                view: output_view,
                bind_group,
                mode,
            } => {
                match mode {
                    OutputMode::Display => &self.output_converter,
                    OutputMode::Screenshot => &self.screenshot_converter,
                }
                .encode(
                    &self.device,
                    recorder,
                    output_view,
                    bind_group,
                    self.pipelines.adapter_backend,
                    &self.pipelines.recorder,
                );
                recorder.record_pass();
            }
            OutputStep::Copy { from, to } => {
                recorder.copy_texture_region(TextureRegionCopy {
                    source: from,
                    source_origin: [0, 0],
                    dest: to,
                    dest_origin: [0, 0],
                    size: [to.width, to.height],
                });
                // The image takes the copy even when the frame drew nothing.
                recorder.record_pass();
            }
        }
        self.stage_frame_uploads(recorder);
        self.pipelines.shape_pipelines.build_wanted();
        self.depth_gate.end_frame();
        self.pipelines.compiler.end_frame(scene_motion);
        self.viewport_uniforms.uploads.finish_frame();
        self.run_store.finish_frame();
        Ok(())
    }

    pub(crate) fn stage_frame_uploads(&mut self, recorder: &mut impl FrameCommandRecorder) {
        let mut upload = self.viewport_uniforms.stage_pending(&self.device, recorder);
        upload += self.run_store.stage_pending(&self.device, recorder);
        upload += self
            .text
            .glyph_run_arena
            .stage_pending(&self.device, recorder);
        self.run_store.fill_arena_trig(&self.device, recorder);
        self.frame_stats.record_command_stats(upload);
    }
    /// Resolves a blurred shadow at `z` into a texture and queues its
    /// composites. The shadow's shapes and texts render into a source the
    /// size of their blur footprint, blur in place and take the post-blur
    /// cutouts; the source is then blitted over the rect the shadow covers.
    /// Shape-only shadows live in the shadow cache, keyed by their content
    /// and device placement, so a scrolling card re-blits its cached blur.
    /// The blurred shadow texture and whether the cache held it: a
    /// shape-only shadow is cached by content and placement, a shadow with
    /// text renders every frame.
    fn blurred_shadow_source<C: FrameCommandRecorder>(
        &mut self,
        frame: &mut FrameRecording<'_, C>,
        shadow: &ShadowDraw,
        source_device: DevicePixelBounds,
        pixel_radius: f32,
        root_scale: f32,
    ) -> Option<(Rc<OffscreenTarget>, bool, SourceContent)> {
        let shape_only = shadow.texts.is_empty();
        let key = if shape_only {
            shape_shadow_surface_cache_key(shadow, source_device, pixel_radius, root_scale)
        } else {
            None
        };
        let content = key.map_or(SourceContent::Transient, |key| {
            SourceContent::retained(&key)
        });
        if let Some(target) = key.and_then(|key| self.shadow_surfaces.get(&key)) {
            return Some((Rc::clone(target), true, content));
        }
        // A shadow whose blur compiles is left out until the blur lands.
        if !self.effect_renderer.blur_draws_now(
            &self.device,
            TileMode::Decal,
            (source_device.width, source_device.height),
            pixel_radius,
            pixel_radius,
        ) {
            self.pipelines.note_placeholder(&self.frame_stats);
            return None;
        }
        if !shape_only {
            self.frame_stats.record_shadow_text_blur_fallback();
        }
        let source = self.render_shadow_source(
            frame,
            shadow,
            source_device,
            pixel_radius,
            root_scale,
            key.is_some(),
        )?;
        if let Some(key) = key {
            self.frame_stats
                .record_shadow_shape_cache_miss(source_device.width, source_device.height);
            self.frame_stats.maybe_print_shadow_shape_cache_miss(
                source_device,
                key.content_hash,
                pixel_radius,
                shadow.shapes.as_ref().map_or(0, RunDraw::record_count) as usize,
                shadow.clip,
            );
            self.shadow_surfaces.insert(key, Rc::clone(&source));
        }
        Some((source, false, content))
    }

    pub(crate) fn resolve_blurred_shadow<C: FrameCommandRecorder>(
        &mut self,
        frame: &mut FrameRecording<'_, C>,
        shadow: &ShadowDraw,
        z: usize,
        root_scale: f32,
        window: &Window,
        resolved: &mut Vec<ResolvedComposite>,
    ) {
        if !shadow.requires_surface()
            || skip_shadow_draws()
            || !root_scale.is_finite()
            || root_scale <= 0.0
        {
            return;
        }
        let Some(bounds) = shadow_draw_bounds(shadow) else {
            return;
        };
        let margin = blur_reach(shadow.blur_radius, root_scale);
        let source_bounds = expand_rect(bounds, margin, margin);
        let mut visible = source_bounds;
        if let Some(clip) = shadow.clip {
            let Some(clipped) = visible.intersect(expand_rect(clip, margin, margin)) else {
                return;
            };
            visible = clipped;
        }
        let Some(drawn) = window.cut_near(
            DeviceRect::from_logical(visible, root_scale),
            margin * root_scale,
        ) else {
            return;
        };
        let visible = Rect {
            x: drawn.x / root_scale,
            y: drawn.y / root_scale,
            width: drawn.width / root_scale,
            height: drawn.height / root_scale,
        };
        let max_texture_dim = self.max_texture_dim();
        let shape_only = shadow.texts.is_empty();
        let anchor = shadow
            .shapes
            .as_ref()
            .and_then(|run| run.placement.snap_anchor);
        let source_device = shape_only
            .then(|| {
                translation_stable_anchored_device_pixel_bounds(
                    source_bounds,
                    anchor,
                    root_scale,
                    max_texture_dim,
                )
            })
            .flatten()
            .or_else(|| device_pixel_bounds(visible, root_scale, max_texture_dim));
        let Some(source_device) = source_device else {
            return;
        };
        let pixel_radius = shadow.blur_radius * root_scale;
        let Some((source, hit, content)) =
            self.blurred_shadow_source(frame, shadow, source_device, pixel_radius, root_scale)
        else {
            return;
        };
        let dest = (
            source_device.x,
            source_device.y,
            source_device.width as f32,
            source_device.height as f32,
        );
        let mut coverage = window
            .cut(DeviceRect {
                x: dest.0,
                y: dest.1,
                width: dest.2,
                height: dest.3,
            })
            .map(|cut| (cut.x, cut.y, cut.width, cut.height));
        if let Some(clip) = shadow.clip {
            coverage = coverage.and_then(|coverage| {
                intersect_device_rects(coverage, anchored_rect_to_device(clip, anchor, root_scale))
            });
        }
        let Some(coverage) = coverage else {
            return;
        };
        if hit {
            self.frame_stats.record_shadow_shape_cache_hit(
                (coverage.2 as u64).saturating_mul(coverage.3 as u64),
            );
        }
        let rounded_mask = shadow_composite_mask(shadow, anchor, root_scale);
        let downscaled =
            (source.width, source.height) != (source_device.width, source_device.height);
        let (sample_mode, source_viewport) = if downscaled {
            (
                CompositeSampleMode::Linear,
                Some((0.0, 0.0, source.width as f32, source.height as f32)),
            )
        } else {
            (CompositeSampleMode::Nearest, None)
        };
        resolved.push(ResolvedComposite {
            z_index: z,
            source,
            content,
            dest,
            scissor: Some(coverage),
            kind: ResolvedCompositeKind::Blit {
                alpha: 1.0,
                blend_mode: BlendMode::SrcOver,
                rounded_mask,
                sample_mode,
                source_viewport,
            },
        });
    }

    /// Draws a shadow's shapes and texts into a surface covering `bounds`
    /// and blurs it. A wide blur runs at its scratch size and its result
    /// stays there, read bilinearly by the composite; a post-blur cutout
    /// needs the surface's full size, so the blurred result is interpolated
    /// back into it first and the cutout drawn at that size. A retained
    /// result feeds the shadow cache; a transient one is registered with
    /// the frame's transients and released with them. `None` when the
    /// shadow draws nothing.
    fn render_shadow_source<C: FrameCommandRecorder>(
        &mut self,
        frame: &mut FrameRecording<'_, C>,
        shadow: &ShadowDraw,
        bounds: DevicePixelBounds,
        pixel_radius: f32,
        root_scale: f32,
        retained: bool,
    ) -> Option<Rc<OffscreenTarget>> {
        let (width, height) = (bounds.width, bounds.height);
        let (scratch_width, scratch_height) =
            crate::effect_renderer::blur_scratch_size(pixel_radius, pixel_radius, width, height);
        let full_size_result = shadow.post_blur_cutouts.is_some()
            || (scratch_width, scratch_height) == (width, height);
        let (result_width, result_height) = if full_size_result {
            (width, height)
        } else {
            (scratch_width, scratch_height)
        };
        let result = if retained {
            Rc::new(self.acquire_retained_surface(result_width, result_height))
        } else {
            self.shadow_transient(frame, "Shadow Result", result_width, result_height)
        };
        let source = if full_size_result {
            Rc::clone(&result)
        } else {
            self.shadow_transient(frame, "Shadow Source", width, height)
        };
        let offset = [bounds.x, bounds.y];
        let target = PassTarget {
            view: &source.view,
            width,
            height,
        };
        let scene = shadow_scene(
            shadow.shapes.as_ref(),
            &shadow.texts,
            blur_reach(shadow.blur_radius, root_scale),
        );
        let segment = PassSegment::plain(&scene, offset, root_scale);
        let drew = self.encode_pass(
            frame.recorder,
            target,
            (std::slice::from_ref(&segment), SegmentLayout::Stacked),
            wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
            "Shadow Source Pass",
        );
        match drew {
            Ok(true) => {}
            Ok(false) => {
                drop(source);
                if retained && let Ok(target) = Rc::try_unwrap(result) {
                    self.defer_offscreen_release(target);
                }
                return None;
            }
            Err(error) => {
                log::error!("shadow source pass failed: {error}");
                return None;
            }
        }
        if pixel_radius > 0.0 {
            let scratch_descriptor = self.transient_offscreen_descriptor(
                "Shadow Blur Scratch",
                scratch_width,
                scratch_height,
            );
            let scratch = frame
                .recorder
                .acquire_transient_offscreen(&self.device, scratch_descriptor);
            let blurred = if full_size_result && (scratch_width, scratch_height) != (width, height)
            {
                Some(self.shadow_transient(
                    frame,
                    "Shadow Blur Result",
                    scratch_width,
                    scratch_height,
                ))
            } else {
                None
            };
            let (view, size) = match &blurred {
                Some(blurred) => (&blurred.view, (scratch_width, scratch_height)),
                None => (&result.view, (result_width, result_height)),
            };
            let passes = self.effect_renderer.encode_blur_scissored_ping_pong_passes(
                frame.recorder,
                &self.device,
                &source,
                &scratch,
                BlurDest {
                    view,
                    size,
                    scissor: None,
                },
                BlurSpec::uniform(pixel_radius, TileMode::Decal),
            );
            frame.recorder.record_passes(passes);
            self.effect_renderer.record_blur_pass();
            frame
                .recorder
                .release_transient_offscreen(scratch_descriptor, scratch);
            if let Some(blurred) = &blurred {
                self.effect_renderer.encode_upscale_pass(
                    frame.recorder,
                    &self.device,
                    blurred,
                    &result.view,
                );
                frame.recorder.record_pass();
            }
        }
        if let Some(cutout_run) = &shadow.post_blur_cutouts {
            let cutouts = shadow_scene(Some(cutout_run), &[], 0.0);
            let segment = PassSegment::plain(&cutouts, offset, root_scale);
            if let Err(error) = self.encode_pass(
                frame.recorder,
                target,
                (std::slice::from_ref(&segment), SegmentLayout::Stacked),
                wgpu::LoadOp::Load,
                "Shadow Cutout Pass",
            ) {
                log::error!("shadow cutout pass failed: {error}");
            }
        }
        Some(result)
    }

    /// A transient surface of a shadow's frame, released with the frame's
    /// transients.
    fn shadow_transient<C: FrameCommandRecorder>(
        &self,
        frame: &mut FrameRecording<'_, C>,
        label: &'static str,
        width: u32,
        height: u32,
    ) -> Rc<OffscreenTarget> {
        let descriptor = self.transient_offscreen_descriptor(label, width, height);
        let target = Rc::new(
            frame
                .recorder
                .acquire_transient_offscreen(&self.device, descriptor),
        );
        frame.transients.push((descriptor, Rc::clone(&target)));
        target
    }

    /// Whether `run` draws from retained buffers keyed by its command.
    pub(crate) fn run_is_stored(&self, run: &RunDraw) -> bool {
        self.run_store.is_stored(run)
    }

    /// Brings a stored run's tables up to date and appends its draws, under
    /// a placement uniform of its own, to the pass's run draws `draws`, and
    /// the pipelines they draw with to `pipelines`.
    pub(crate) fn prepare_store_run<C: FrameCommandRecorder>(
        &mut self,
        recorder: &mut C,
        run: &RunDraw,
        (space, clip_scissors): (DrawSpace, &mut clip_scissor::Last),
        window: &std::ops::Range<u32>,
        (depth, mut depth_keys): (PassDepth, Option<&mut PipelineDemand>),
        (draws, pipelines): (&mut Vec<RunDrawCall>, &mut PipelineDemand),
    ) -> StoreRunBatch {
        let (viewport, root_scale) = (space.viewport, space.scale);
        let placement = &run.placement;
        let ablation = self.ablation.shape;
        let turns = ShapeTurns::of(viewport.transform, false);
        let scissor = clip_scissors.stored(placement, root_scale, &viewport);
        let clip = SegmentClip::of(placement, scissor.is_some());
        let start = draws.len();
        self.run_store.stored_run_draws(
            &self.device,
            run,
            &mut |segment| {
                let key = |depth| {
                    ShapePipelineKey::of_run(
                        segment,
                        placement,
                        RunTier::Store,
                        ablation,
                        turns,
                        (depth, clip),
                        viewport,
                    )
                };
                if let Some(keys) = depth_keys.as_deref_mut() {
                    keys.add(key(PassDepth::On));
                }
                key(depth)
            },
            draws,
        );
        window_draws(draws, start, window);
        let run_draws = &draws[start..];
        let upload_start = Instant::now();
        let (upload, fill, tables) = self.run_store.upload_stored(
            &self.device,
            recorder,
            run,
            root_scale,
            window,
            run_draws,
        );
        if let Some(total_ms) = should_log_wgpu_render_stage(upload_start, Instant::now()) {
            log::warn!(
                "[wgpu-render-stage:run-upload] total_ms={total_ms:.2} bytes={} records={}",
                upload.upload_bytes,
                run.tables().shapes.len()
            );
        }
        self.frame_stats.record_command_stats(upload);
        if let Some(fill) = fill {
            self.frame_stats.add_shape_fill(fill);
        }
        let uniforms = Uniforms::of(
            viewport,
            PlacementData::of(&run.placement, root_scale, viewport.transform, [0.0; 2]),
            RunTier::Store,
        );
        let uniform_slot = self.viewport_uniforms.claim(
            &self.device,
            &self.pipelines.uniform_bind_group_layout,
            &uniforms,
        );
        for draw in run_draws {
            pipelines.add(draw.key);
        }
        StoreRunBatch {
            tables,
            uniform_slot,
            draws: start..draws.len(),
            clip: scissor,
        }
    }

    pub(crate) fn open_arena(&mut self) -> usize {
        self.run_store.open_arena()
    }

    /// The records the open arena chunk holds so far.
    pub(crate) fn open_arena_records(&self) -> u32 {
        self.run_store.open_arena_records()
    }

    pub(crate) fn arena_accepts(&self, chunk: usize, run: &RunDraw) -> bool {
        self.run_store.arena_accepts(chunk, run)
    }

    /// Appends `window` of `run`'s records to the open arena chunk, their
    /// placement under `turn`, moved by `shift` and `depth_skip` places on (see
    /// [`PlacementData::of`]), keyed for a pass whose flat and turned
    /// records alternate often when `mixed_turns`, and the pipelines they
    /// draw with to `pipelines`. `clip` says how its variant tests its clip:
    /// an unturned run leaves a rect clip to its paint's scissor, never a
    /// rounded one.
    pub(crate) fn append_arena_run(
        &mut self,
        chunk: usize,
        run: &RunDraw,
        window: std::ops::Range<u32>,
        (at, depth_skip): (ArenaPlacement, u32),
        clip: SegmentClip,
        (pipelines, mut depth_keys): (&mut PipelineDemand, Option<&mut PipelineDemand>),
    ) -> u32 {
        let placement = &run.placement;
        let ablation = self.ablation.shape;
        let (viewport, depth) = (at.space.viewport, at.depth);
        let turn = viewport.transform;
        let turns = ShapeTurns::of(turn, at.mixed_turns);
        self.run_store.append_arena(
            chunk,
            run,
            window,
            (at.space.scale, turn, at.shift, depth_skip),
            &mut |segment| {
                let key = |depth| {
                    ShapePipelineKey::of_run(
                        segment,
                        placement,
                        RunTier::Arena,
                        ablation,
                        turns,
                        (depth, clip),
                        viewport,
                    )
                };
                if let Some(keys) = depth_keys.as_deref_mut() {
                    keys.add(key(PassDepth::On));
                }
                key(depth)
            },
            pipelines,
        )
    }

    /// Moves the open chunk's draws since its last cut onto `out`, keeping
    /// it open.
    pub(crate) fn cut_arena(&mut self, chunk: usize, out: &mut Vec<RunDrawCall>) {
        self.run_store.cut_arena(&self.device, chunk, out);
    }

    /// Uploads the open chunk and moves its draws since its last cut onto
    /// `out`.
    pub(crate) fn close_arena(&mut self, chunk: usize, out: &mut Vec<RunDrawCall>) {
        if let Some(fill) = self.run_store.close_arena(&self.device, chunk, out) {
            self.frame_stats.add_shape_fill(fill);
        }
    }

    pub(crate) fn draw_run_calls(
        &self,
        pass: &mut TrackedPass<'_, '_>,
        tables: ArenaBinding<'_>,
        calls: RunCalls<'_>,
    ) -> Result<(), String> {
        let RunCalls {
            uniform_slot,
            draws,
            scissor,
            stage,
            meshes,
        } = calls;
        match stage {
            RunStage::Paint => {
                if draws.is_empty() {
                    return Ok(());
                }
                self.frame_stats.bump_shapes();
                self.record_run_draws(
                    pass,
                    tables,
                    uniform_slot,
                    scissor,
                    draws.iter().map(PlannedRunDraw::paint),
                    meshes,
                )
            }
            RunStage::Interiors => {
                let planned = interior_run_draws(draws);
                if planned.is_empty() {
                    return Ok(());
                }
                self.frame_stats
                    .shape_interior_draws
                    .set(self.frame_stats.shape_interior_draws.get() + planned.len() as u32);
                self.record_run_draws(
                    pass,
                    tables,
                    uniform_slot,
                    scissor,
                    planned.into_iter(),
                    None,
                )
            }
        }
    }

    /// Records `planned` against one batch's tables, uniform and scissor. A
    /// draw whose pipeline still compiles draws its fallback triangles from
    /// `meshes`, with the depth buffer or not, else it is left out.
    fn record_run_draws(
        &self,
        pass: &mut TrackedPass<'_, '_>,
        tables: ArenaBinding<'_>,
        uniform_slot: usize,
        scissor: (u32, u32, u32, u32),
        planned: impl ExactSizeIterator<Item = PlannedRunDraw>,
        meshes: Option<(crate::mesh_fallback::RunMeshes<'_>, PassDepth)>,
    ) -> Result<(), String> {
        self.frame_stats.add_draw_calls(planned.len() as u32);
        let (x, y, width, height) = scissor;
        pass.set_scissor_rect(x, y, width, height);
        self.viewport_uniforms.bind(pass, uniform_slot)?;
        pass.set_bind_group(1, tables.bind_group, &tables.offsets[2..]);
        let bind_records = |pass: &mut TrackedPass<'_, '_>| {
            for (slot, buffer) in tables.records.into_iter().enumerate() {
                pass.set_vertex_buffer(
                    slot as u32,
                    buffer.slice(u64::from(tables.offsets[slot])..),
                );
            }
        };
        bind_records(pass);
        let mut bound_class = None;
        let mut records_bound = true;
        for (position, draw) in planned.enumerate() {
            let key = draw.key;
            let Some((pipeline, fallback)) = self.pipelines.shape_pipelines.get(key) else {
                // Its pipeline compiles: the draw takes its fallback
                // triangles, or is left out, until it lands.
                let mesh = meshes.and_then(|(meshes, depth)| {
                    let range = meshes.ranges.get(position)?.clone()?;
                    Some((range, meshes, self.pipelines.mesh_pipeline(depth)?))
                });
                let Some((range, meshes, mesh_pipeline)) = mesh else {
                    self.pipelines.note_placeholder(&self.frame_stats);
                    continue;
                };
                self.pipelines.note_mesh_fallback(&self.frame_stats);
                pass.set_pipeline(mesh_pipeline);
                meshes.draw(pass, range);
                bound_class = None;
                records_bound = false;
                continue;
            };
            if !records_bound {
                bind_records(pass);
                records_bound = true;
            }
            if fallback {
                self.frame_stats
                    .shape_pipeline_fallback_draws
                    .set(self.frame_stats.shape_pipeline_fallback_draws.get() + 1);
            } else if !key.is_general() {
                self.frame_stats
                    .shape_specialized_draws
                    .set(self.frame_stats.shape_specialized_draws.get() + 1);
            }
            if bound_class != Some(draw.band_class) {
                pass.set_index_buffer(
                    self.run_store.strip_index_buffer(draw.band_class).slice(..),
                    wgpu::IndexFormat::Uint32,
                );
                bound_class = Some(draw.band_class);
            }
            pass.set_pipeline(pipeline);
            pass.draw_indexed(draw.indices, 0, draw.records);
        }
        Ok(())
    }

    /// Draws a stored-run batch, whose draws are its range of `draws`.
    pub(crate) fn draw_store_run(
        &self,
        pass: &mut TrackedPass<'_, '_>,
        batch: &StoreRunBatch,
        draws: &[RunDrawCall],
        scissor: (u32, u32, u32, u32),
        stage: RunStage,
        meshes: Option<(crate::mesh_fallback::RunMeshes<'_>, PassDepth)>,
    ) -> Result<(), String> {
        let calls = RunCalls {
            uniform_slot: batch.uniform_slot,
            draws: &draws[batch.draws.clone()],
            scissor,
            stage,
            meshes: meshes.map(|(meshes, depth)| (meshes.of(batch.draws.clone()), depth)),
        };
        self.draw_run_calls(pass, batch.tables.binding(), calls)
    }

    pub(crate) fn draw_arena(
        &self,
        pass: &mut TrackedPass<'_, '_>,
        chunk: usize,
        calls: RunCalls<'_>,
    ) -> Result<(), String> {
        self.draw_run_calls(pass, self.run_store.arena_binding(chunk), calls)
    }
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn surface_format(&self) -> wgpu::TextureFormat {
        self.display_format
    }

    pub(crate) fn device_error_count(&self) -> u64 {
        self.device_errors.error_count()
    }

    /// A pass that only applies `load_op` to the target, for a scene with
    /// nothing to draw that still needs its clear.
    pub(crate) fn clear_target<C: FrameCommandRecorder>(
        &self,
        recorder: &mut C,
        view: &wgpu::TextureView,
        load_op: wgpu::LoadOp<wgpu::Color>,
    ) {
        self.empty_pass(recorder, "Clear Pass", view, load_op);
    }

    pub(crate) fn empty_pass<C: FrameCommandRecorder>(
        &self,
        recorder: &mut C,
        label: &'static str,
        view: &wgpu::TextureView,
        load_op: wgpu::LoadOp<wgpu::Color>,
    ) {
        let pass = recorder.begin_color_pass(label, view, load_op);
        drop(pass);
        recorder.record_pass();
    }
    pub(crate) fn draw_image_cmds(
        &self,
        pass: &mut TrackedPass<'_, '_>,
        image_slot: &ImageSlot,
        uniform_slot: usize,
        cmds: &[ImageDrawCmd],
        (blend_mode, depth): (BlendMode, PassDepth),
        bound: Option<(u32, u32, u32, u32)>,
    ) -> Result<(), String> {
        if cmds.is_empty() {
            return Ok(());
        }
        self.frame_stats.bump_images();
        self.frame_stats.add_draw_calls(cmds.len() as u32);
        let mut bound_pipeline = None;
        self.viewport_uniforms.bind(pass, uniform_slot)?;
        pass.set_index_buffer(image_slot.indices.slice(), wgpu::IndexFormat::Uint32);
        pass.set_vertex_buffer(0, image_slot.vertices.slice());
        let mut clips_bound = false;
        for cmd in cmds {
            let Some((x, y, width, height)) = bounded_scissor(cmd.scissor, bound) else {
                continue;
            };
            pass.set_scissor_rect(x, y, width, height);
            let variant = ImageVariant {
                depth,
                alpha_mask: cmd.image.alpha_mask,
                rounded: cmd.clip.is_some(),
            };
            if bound_pipeline != Some(variant) {
                match self
                    .pipelines
                    .image_pipeline(&self.frame_stats, blend_mode, variant)
                {
                    FixedDraw::Own(pipeline) => pass.set_pipeline(pipeline),
                    FixedDraw::Instant(stand_in) => {
                        stand_in.bind(pass, variant.texels());
                    }
                    FixedDraw::LeftOut => continue,
                }
                bound_pipeline = Some(variant);
            }
            pass.set_bind_group(1, &cmd.image.bind_group, &[]);
            let instance = match cmd.clip {
                Some(clip) => {
                    if !clips_bound {
                        let clips = image_slot
                            .clips
                            .as_ref()
                            .ok_or_else(|| "rounded image without clip instances".to_string())?;
                        pass.set_vertex_buffer(1, clips.slice());
                        clips_bound = true;
                    }
                    clip
                }
                None => 0,
            };
            pass.draw_indexed(
                cmd.index_start..(cmd.index_start + 6),
                0,
                instance..instance + 1,
            );
        }
        Ok(())
    }

    pub(crate) fn draw_glyph_cmds(
        &self,
        pass: &mut TrackedPass<'_, '_>,
        (glyph_slot, turned_slot): (Option<&BufferUpload>, TurnedGlyphs<'_>),
        uniform_slot: usize,
        cmds: &[GlyphDrawCmd],
        bound: Option<(u32, u32, u32, u32)>,
        frame: PassFrame,
    ) -> Result<(), String> {
        if cmds.is_empty() {
            return Ok(());
        }
        let whole_target = frame.scissor(bound);
        self.frame_stats.bump_text();
        let mut bound_atlas = None;
        // Which shared instances are bound, turned or plain, and which
        // pipeline; retained runs draw with a plain one.
        let mut shared_bound: Option<GlyphKind> = None;
        let mut bound_pipeline: Option<GlyphQuads> = None;
        let mut bound_run_instances: Option<&wgpu::Buffer> = None;
        let mut draws = 0u32;
        for draw in GlyphDraws::new(cmds) {
            let scissor = draw.scissor.map_or(Some(whole_target), |scissor| {
                bounded_scissor(scissor, bound)
            });
            let Some((x, y, width, height)) = scissor else {
                continue;
            };
            pass.set_scissor_rect(x, y, width, height);
            if !bound_atlas.is_some_and(|atlas| Rc::ptr_eq(atlas, draw.atlas)) {
                pass.set_bind_group(1, draw.atlas.as_ref(), &[]);
                bound_atlas = Some(draw.atlas);
            }
            draws += 1;
            if let Some((laid, instances)) = turned_slot.laid(&draw.step) {
                self.viewport_uniforms.bind(pass, uniform_slot)?;
                laid.draw(pass, instances);
                self.pipelines.note_instant(&self.frame_stats);
                (bound_pipeline, shared_bound, bound_run_instances) = (None, None, None);
                continue;
            }
            let quads = draw.step.quads();
            if bound_pipeline != Some(quads) {
                let pipeline =
                    self.pipelines
                        .glyph_atlas_pipeline(&self.frame_stats, frame.depth, quads);
                let Some(pipeline) = pipeline else {
                    continue;
                };
                pass.set_pipeline(pipeline);
                bound_pipeline = Some(quads);
            }
            match draw.step {
                GlyphDrawStep::Shared(instances, quads) => {
                    let kind = quads.kind();
                    if shared_bound != Some(kind) {
                        let slot = match kind {
                            GlyphKind::Plain => glyph_slot,
                            GlyphKind::Turned => turned_slot.instances(),
                        }
                        .ok_or_else(|| "shared glyph draw without glyph instances".to_string())?;
                        self.viewport_uniforms.bind(pass, uniform_slot)?;
                        pass.set_vertex_buffer(0, slot.slice());
                        shared_bound = Some(kind);
                        bound_run_instances = None;
                    }
                    pass.draw(0..GLYPH_QUAD_CORNERS, instances);
                }
                GlyphDrawStep::Retained {
                    run,
                    uniform_slot: retained_slot,
                    ..
                } => {
                    shared_bound = None;
                    self.viewport_uniforms.bind(pass, retained_slot)?;
                    let instances = run.span.instance_buffer();
                    if bound_run_instances != Some(instances) {
                        pass.set_vertex_buffer(0, instances.slice(..));
                        bound_run_instances = Some(instances);
                    }
                    pass.draw(0..GLYPH_QUAD_CORNERS, run.span.instances());
                }
            }
        }
        self.frame_stats.add_draw_calls(draws);
        Ok(())
    }
    pub(crate) fn upload_glyph_instances<C: FrameCommandRecorder>(
        &self,
        recorder: &mut C,
        instances: &[GlyphInstance],
    ) -> BufferUpload {
        recorder.upload_buffer(
            glyph_instance_spec(),
            &self.device,
            bytemuck::cast_slice(instances),
        )
    }

    pub(crate) fn upload_turned_glyphs<C: FrameCommandRecorder>(
        &self,
        recorder: &mut C,
        glyphs: &[TurnedGlyph],
    ) -> BufferUpload {
        recorder.upload_buffer(
            turned_glyph_spec(),
            &self.device,
            bytemuck::cast_slice(glyphs),
        )
    }

    fn convert_surface_pixels_to_rgba(&self, pixels: &[u8]) -> Result<Vec<u8>, String> {
        if !pixels.len().is_multiple_of(4) {
            return Err("Screenshot readback has an incomplete pixel".to_string());
        }
        Ok(pixels.to_vec())
    }
}

fn align_to(value: u32, alignment: u32) -> u32 {
    debug_assert!(alignment > 0);
    value.div_ceil(alignment) * alignment
}

/// The scissor of a logical rect in a target whose origin sits at
/// `viewport.offset` of the scene's device space, clamped to the target.
/// `None` when nothing of the rect lands in the target.
pub(crate) fn scissor_rect_for_rect(
    rect: Rect,
    root_scale: f32,
    viewport: ViewportUniformParams,
) -> Option<(u32, u32, u32, u32)> {
    let width = viewport.width as f32;
    let height = viewport.height as f32;
    let mut left = canonicalize_device_coordinate(rect.x * root_scale);
    let mut top = canonicalize_device_coordinate(rect.y * root_scale);
    let mut right = canonicalize_device_coordinate((rect.x + rect.width) * root_scale);
    let mut bottom = canonicalize_device_coordinate((rect.y + rect.height) * root_scale);
    if !viewport.transform.is_identity() {
        let bounds = viewport.transform.target_bounds(Rect {
            x: left,
            y: top,
            width: right - left,
            height: bottom - top,
        });
        (left, top) = (bounds.x, bounds.y);
        (right, bottom) = (bounds.x + bounds.width, bounds.y + bounds.height);
    }
    let left = (left - viewport.offset[0]).clamp(0.0, width).floor();
    let top = (top - viewport.offset[1]).clamp(0.0, height).floor();
    let right = (right - viewport.offset[0]).clamp(0.0, width).ceil();
    let bottom = (bottom - viewport.offset[1]).clamp(0.0, height).ceil();
    if right <= left || bottom <= top {
        return None;
    }
    Some((
        left as u32,
        top as u32,
        (right - left) as u32,
        (bottom - top) as u32,
    ))
}

fn scissor_rect_for_layer(
    rect: Rect,
    clip: Option<Rect>,
    root_scale: f32,
    viewport: ViewportUniformParams,
) -> Option<(u32, u32, u32, u32)> {
    let clipped_rect = match clip {
        Some(clip_rect) => rect.intersect(clip_rect)?,
        None => rect,
    };
    scissor_rect_for_rect(clipped_rect, root_scale, viewport)
}

/// The rounded clip an image takes in place, in device pixels: one instance
/// its draw reads, beside the quad's vertices.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub(crate) struct DeviceRoundedClip {
    rect: [f32; 4],
    radius: f32,
}

impl DeviceRoundedClip {
    const ATTRIBS: [wgpu::VertexAttribute; 2] = wgpu::vertex_attr_array![
        4 => Float32x4,
        5 => Float32,
    ];

    fn desc() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: size_of::<Self>() as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Instance,
            attributes: &Self::ATTRIBS,
        }
    }

    /// The rounded clip `image` takes at `root_scale`, scaled as a run's
    /// placement scales its clip.
    fn of(image: &ImageDraw, root_scale: f32) -> Option<Self> {
        let rounding = image.rounding.filter(|_| image.clip.is_some())?;
        let device =
            crate::geometry::device_rect(rounding.rect, root_scale, image.snap_anchor.is_some());
        Some(Self {
            rect: [device.x, device.y, device.width, device.height],
            radius: rounding.radius * root_scale,
        })
    }
}

/// One draw call of a run batch's stage: its pipeline, the strip index
/// buffer it binds and the indices and records it instances.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PlannedRunDraw {
    pub(crate) key: ShapePipelineKey,
    pub(crate) band_class: u8,
    pub(crate) indices: std::ops::Range<u32>,
    pub(crate) records: std::ops::Range<u32>,
}

impl PlannedRunDraw {
    /// A draw of the paint, as it was recorded.
    fn paint(draw: &RunDrawCall) -> Self {
        Self {
            key: draw.key,
            band_class: draw.band_class,
            indices: draw.indices(),
            records: draw.records.clone(),
        }
    }
}

/// Drops the entries of `cache` no frame drew for more than `idle_frames`
/// before `frame`: the least recently used first, which is the order they
/// were last drawn in.
fn evict_idle<K: Clone + Eq + Hash, V>(
    cache: &mut BoundedLruCache<K, V>,
    frame: u64,
    idle_frames: u64,
    last_frame: impl Fn(&V) -> u64,
) {
    while cache
        .peek_lru()
        .is_some_and(|(_, value)| frame.saturating_sub(last_frame(value)) > idle_frames)
    {
        cache.pop_lru();
    }
}

/// Cuts the run draws from `start` on to `window` of their records, counted
/// across them in order, and drops the draws left with none.
fn window_draws(draws: &mut Vec<RunDrawCall>, start: usize, window: &std::ops::Range<u32>) {
    let mut relative = 0u32;
    let mut kept = start;
    for index in start..draws.len() {
        let draw = &mut draws[index];
        let count = draw.records.end - draw.records.start;
        let first = relative;
        relative += count;
        let keep_start = window.start.max(first).min(first + count);
        let keep_end = window.end.min(first + count).max(keep_start);
        draw.records =
            draw.records.start + (keep_start - first)..draw.records.start + (keep_end - first);
        if draw.records.start < draw.records.end {
            draws.swap(kept, index);
            kept += 1;
        }
    }
    draws.truncate(kept);
}

pub(crate) mod clip_scissor;
mod glyph_draws;
mod pipeline_builds;
mod stage_telemetry;
pub(crate) use glyph_draws::{
    GlyphDrawCmd, GlyphInstance, GlyphInstances, TurnedGlyphs, defer_retained_glyph_runs,
    group_glyph_kinds,
};
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use pipeline_builds::mark_thread_off_frame;
#[cfg(not(target_arch = "wasm32"))]
use pipeline_builds::thread_builds;
pub(crate) use pipeline_builds::{build_pipeline_logged, create_render_pipeline_logged};
pub use pipeline_builds::{pipelines_created, pipelines_created_off_frame};
pub(crate) use stage_telemetry::{instant_ms, should_log_wgpu_render_stage};
use text_resources::TextResources;
pub(crate) mod admission_gate;
mod glyph_atlas;
mod renderer_pipelines;
#[cfg(not(target_arch = "wasm32"))]
use renderer_pipelines::MESH_FALLBACK_PIPELINES;
use renderer_pipelines::{
    FixedDraw, GLYPH_ATLAS_PIPELINES, IMAGE_DST_OUT_PIPELINES, IMAGE_SRC_OVER_PIPELINES,
    RRECT_SHADOW_PIPELINES, RendererPipelines,
};
mod draw_space;
mod interior_run_draws;
pub(crate) use interior_run_draws::interior_run_draws;
mod image_draws;
pub(crate) use draw_space::{ArenaPlacement, DrawSpace};
mod image_textures;
use image_textures::ImageTextures;
mod text_glyph_cache;
mod text_resources;
pub(crate) use image_draws::{
    ImageBatch, ImageDrawCmd, ImageSlot, Vertex, place_image_draws, resolve_image_geometry,
};
mod depth_targets;
mod pass_depth;
mod viewport_uniform;
use depth_targets::DepthTargets;
pub(crate) use pass_depth::PassDepth;
use viewport_uniform::Uniforms;
mod frame_history;
mod frame_submit;
mod frame_uploads;
use frame_history::FrameHistory;
pub use frame_history::frames_presented;
pub(crate) use frame_submit::FrameSubmit;
pub(crate) use frame_uploads::{DrawParts, FrameUploads};
mod shadow_cache;
pub(crate) use shadow_cache::{shadow_composite_mask, shadow_draw_bounds};
mod shape_pipeline;
use glyph_atlas::{
    GlyphAtlasEntry, GlyphAtlasSlotKey, GlyphSamplers, RunAtlasEntries, TEXT_GLYPH_ATLAS_MIN_SIZE,
    TextGlyphAtlas, create_glyph_atlas_pipeline, glyph_atlas_uv_rect,
};
use glyph_draws::{
    GLYPH_QUAD_CORNERS, GlyphDrawStep, GlyphDraws, GlyphExtent, GlyphKind, GlyphQuads, TurnedGlyph,
    for_each_visible_glyph, glyph_cut_edges, glyph_instance_spec, glyph_rect_aligned,
    shared_glyph_clip, turned_glyph_spec,
};
use image_draws::{
    ImageBindGroups, ImageBinding, ImageUvRect, ImageVariant, create_image_pipeline,
    image_sampler_descriptor, sampling_under,
};
use shadow_cache::{
    ShadowSurfaces, device_pixel_bounds, shadow_scene, shape_shadow_surface_cache_key,
    skip_shadow_draws,
};
#[cfg(test)]
pub(crate) use shape_pipeline::ShapeVariant;
use shape_pipeline::{LINE_KINDS, shape_shader_code};
pub(crate) use shape_pipeline::{
    RunTier, SegmentClip, ShapeDepth, ShapePipelineKey, ShapeTurns, create_shape_pipeline,
};
use text_glyph_cache::{
    CachedGpuTextGlyphRun, GlyphRunBounds, GlyphRunQuads, MAX_TEXT_GLYPH_GPU_RUN_CACHE_ITEMS,
    MAX_TEXT_GLYPH_MASK_CACHE_ITEMS, MAX_TEXT_IMAGE_CACHE_ITEMS, MIN_TEXT_GLYPH_RUN_CACHE_ITEMS,
    RETAINED_TEXT_GLYPH_RUN_MIN_QUADS, TEXT_GLYPH_GPU_RUN_IDLE_FRAMES, TEXT_GLYPH_RUN_IDLE_FRAMES,
    TextGlyphRunDraw, TextGlyphRunLookup, cached_text_glyph_instance,
    cached_text_glyph_quad_is_visible_in_viewport, log_text_atlas_fallback,
};

#[cfg(test)]
#[path = "tests/render_text_bounds_tests.rs"]
mod text_bounds_tests;

#[cfg(test)]
#[path = "tests/render_retained_glyph_tests.rs"]
mod retained_glyph_tests;

#[cfg(test)]
#[path = "tests/frame_clear_tests.rs"]
mod frame_clear_tests;