burin 0.1.2

A Rust GUI framework built on the auralis reactive signal kernel
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
//! wgpu GPU rendering backend.

pub mod glyphon_bridge;
mod pipeline;

use std::collections::HashMap;

use crate::core::error::{push_error, GpuErrorKind, UiError};
use crate::render::ClipInfo;
use glam::Affine2;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;

use pipeline::{
    create_atlas_bind_group, create_blend_bind_group_layout, create_blend_pipeline,
    create_blit_bind_group_layout, create_blit_pipeline, create_blur_bind_group_layout,
    create_blur_pipeline, create_composite_pipeline, create_gradient_pipeline,
    create_path_pipeline, create_rect_pipeline, create_text_bind_group_layout,
    create_text_pipeline, BlitVertex, BlurUniforms, GradientVertex, PathVertex, RectVertex,
    TextVertex,
};

use crate::render::painter::DrawCommand;
use crate::render::path::{bezpath_bounds, bezpath_to_lyon, hash_bezpath};
use crate::style::Brush;
use crate::style::{Color, Rect, Size};
use lyon::tessellation::{
    BuffersBuilder, FillOptions, FillTessellator, FillVertex, StrokeOptions, StrokeTessellator,
    VertexBuffers,
};

pub use glyphon_bridge::{create_buffer, GlyphonBridge, TextAreaDesc};

const MAX_IMAGE_DIM: u32 = 4096;

/// Pre-computed mip chain for a single image.
pub(crate) struct CpuImageMips {
    pub width: u32,
    pub height: u32,
    /// mip 0 = full resolution, each subsequent level halves both dims.
    pub levels: Vec<Rc<Vec<u8>>>,
}

fn mip_level_count(width: u32, height: u32) -> u32 {
    let max_dim = width.max(height) as f64;
    (max_dim.log2().floor() as u32).max(1)
}

/// Box-filter downsample RGBA pixels from src_w×src_h to dst_w×dst_h.
fn box_filter(src: &[u8], src_w: u32, src_h: u32, dst_w: u32, dst_h: u32) -> Vec<u8> {
    let mut dst = vec![0u8; (dst_w * dst_h * 4) as usize];
    let x_ratio = src_w as f64 / dst_w as f64;
    let y_ratio = src_h as f64 / dst_h as f64;
    for dy in 0..dst_h {
        let y0 = (dy as f64 * y_ratio).floor() as u32;
        let y1 = (((dy as f64 + 1.0) * y_ratio).ceil() as u32).min(src_h);
        for dx in 0..dst_w {
            let x0 = (dx as f64 * x_ratio).floor() as u32;
            let x1 = (((dx as f64 + 1.0) * x_ratio).ceil() as u32).min(src_w);
            let mut r = 0u64;
            let mut g = 0u64;
            let mut b = 0u64;
            let mut a = 0u64;
            let mut cnt = 0u64;
            for sy in y0..y1 {
                for sx in x0..x1 {
                    let si = (sy * src_w + sx) as usize * 4;
                    if si + 4 <= src.len() {
                        r += src[si] as u64;
                        g += src[si + 1] as u64;
                        b += src[si + 2] as u64;
                        a += src[si + 3] as u64;
                        cnt += 1;
                    }
                }
            }
            if cnt > 0 {
                let di = (dy * dst_w + dx) as usize * 4;
                dst[di] = (r / cnt) as u8;
                dst[di + 1] = (g / cnt) as u8;
                dst[di + 2] = (b / cnt) as u8;
                dst[di + 3] = (a / cnt) as u8;
            }
        }
    }
    dst
}

fn build_mip_chain(pixels: &[u8], width: u32, height: u32) -> Vec<Rc<Vec<u8>>> {
    let count = mip_level_count(width, height);
    let mut levels = Vec::with_capacity(count as usize);
    levels.push(Rc::new(pixels.to_vec()));
    let mut mw = width;
    let mut mh = height;
    for _ in 1..count {
        let nw = (mw / 2).max(1);
        let nh = (mh / 2).max(1);
        let mip = box_filter(levels.last().unwrap(), mw, mh, nw, nh);
        levels.push(Rc::new(mip));
        mw = nw;
        mh = nh;
    }
    levels
}

struct ImageEntry {
    width: u32,
    height: u32,
    full_res: Rc<Vec<u8>>,
    mips: Option<Rc<CpuImageMips>>,
}

// Thread-local registry mapping image hashes to their data.
// NOTE: intentionally NOT migrated to AppContext. `ImageEntry`/`CpuImageMips`
// hold `Rc<Vec<u8>>` (render-layer, !Send/!Sync) so this cannot become a
// process-level singleton without an Rc->Arc rewrite of the whole image path.
// More importantly it is a content-addressed IMMUTABLE cache (hash -> pixels):
// sharing the same image across windows is correct de-duplication, not state
// that needs per-window isolation. Kept in the render layer by design.
thread_local! {
static IMAGE_REGISTRY: RefCell<HashMap<u64, ImageEntry>> = RefCell::new(HashMap::new());
/// hash → (element refcount, pinned). Pinned entries (registered without an
/// owning element via `register_image`) are never evicted — old behavior.
/// Element-owned entries (`register_image_for`) are freed when the last
/// referencing element is torn down (audit 2026-07-17 round 3, Finding E).
static IMAGE_REFS: RefCell<HashMap<u64, (usize, bool)>> = RefCell::new(HashMap::new());
/// element → image hashes it registered (usually 1).
static ELEMENT_IMAGES: RefCell<HashMap<crate::core::ElementId, Vec<u64>>> = RefCell::new(HashMap::new());
}

/// Register image data for GPU/CPU rendering. Called from widget mount.
///
/// If the image exceeds MAX_IMAGE_DIM (4096) in either dimension it is
/// box-filtered down on registration.  Mip chain generation is deferred
/// to `lookup_image_mips` (first render time), so images that are
/// registered but never rendered incur no mip cost.
///
/// Entries registered through this anonymous form are PINNED (never evicted).
/// Prefer `register_image_for` from widget mounts so pixel data is freed
/// when the last referencing element is torn down.
pub fn register_image(hash: u64, width: u32, height: u32, pixels: Rc<Vec<u8>>) {
    IMAGE_REFS.with(|r| r.borrow_mut().entry(hash).or_insert((0, false)).1 = true);
    register_image_inner(hash, width, height, pixels);
}

/// Register image data owned by `eid`: a per-hash refcount tracks how many
/// live elements reference the pixels; the core teardown protocol decrements
/// it and frees the entry (pixels + mips) when it reaches zero.
pub(crate) fn register_image_for(
    eid: crate::core::ElementId,
    hash: u64,
    width: u32,
    height: u32,
    pixels: Rc<Vec<u8>>,
) {
    crate::core::dirty_registry::register_teardown_hook(image_teardown_cleanup);
    let newly_referenced = ELEMENT_IMAGES.with(|m| {
        let mut m = m.borrow_mut();
        let hashes = m.entry(eid).or_default();
        if hashes.contains(&hash) {
            false
        } else {
            hashes.push(hash);
            true
        }
    });
    if newly_referenced {
        IMAGE_REFS.with(|r| r.borrow_mut().entry(hash).or_insert((0, false)).0 += 1);
    }
    register_image_inner(hash, width, height, pixels);
}

fn register_image_inner(hash: u64, width: u32, height: u32, pixels: Rc<Vec<u8>>) {
    // Content-addressed: an existing entry for this hash is already correct
    // (and may carry a built mip chain) — skip the re-insert and, for
    // oversized images, the redundant box_filter.
    if IMAGE_REGISTRY.with(|reg| reg.borrow().contains_key(&hash)) {
        return;
    }
    let (w, h, data) = if width > MAX_IMAGE_DIM || height > MAX_IMAGE_DIM {
        let scale = (MAX_IMAGE_DIM as f64 / width.max(height) as f64).min(1.0);
        let nw = (width as f64 * scale) as u32;
        let nh = (height as f64 * scale) as u32;
        (nw, nh, Rc::new(box_filter(&pixels, width, height, nw, nh)))
    } else {
        (width, height, pixels)
    };
    IMAGE_REGISTRY.with(|reg| {
        reg.borrow_mut().insert(
            hash,
            ImageEntry {
                width: w,
                height: h,
                full_res: data,
                mips: None,
            },
        );
    });
}

fn image_teardown_cleanup(id: crate::core::ElementId) {
    let hashes = ELEMENT_IMAGES.with(|m| m.borrow_mut().remove(&id));
    let Some(hashes) = hashes else { return };
    for hash in hashes {
        let evict = IMAGE_REFS.with(|r| {
            let mut r = r.borrow_mut();
            if let Some(entry) = r.get_mut(&hash) {
                entry.0 = entry.0.saturating_sub(1);
                if entry.0 == 0 && !entry.1 {
                    r.remove(&hash);
                    return true;
                }
            }
            false
        });
        if evict {
            IMAGE_REGISTRY.with(|reg| {
                reg.borrow_mut().remove(&hash);
            });
        }
    }
}

/// Test-only introspection: (registry entries, ref entries, element links).
#[doc(hidden)]
pub fn debug_image_registry_sizes() -> (usize, usize, usize) {
    (
        IMAGE_REGISTRY.with(|r| r.borrow().len()),
        IMAGE_REFS.with(|r| r.borrow().len()),
        ELEMENT_IMAGES.with(|m| m.borrow().len()),
    )
}

/// Look up full-resolution image data by hash.  Used by CPU ImageCache.
pub fn lookup_image(hash: u64) -> Option<(u32, u32, Rc<Vec<u8>>)> {
    IMAGE_REGISTRY.with(|reg| {
        let reg = reg.borrow();
        reg.get(&hash)
            .map(|e| (e.width, e.height, e.full_res.clone()))
    })
}

/// Look up (or lazily generate) the mip chain for an image.
pub(crate) fn lookup_image_mips(hash: u64) -> Option<Rc<CpuImageMips>> {
    IMAGE_REGISTRY.with(|reg| {
        let mut reg = reg.borrow_mut();
        let entry = reg.get_mut(&hash)?;
        match &entry.mips {
            Some(mips) => Some(Rc::clone(mips)),
            None => {
                let levels = build_mip_chain(&entry.full_res, entry.width, entry.height);
                let mips = Rc::new(CpuImageMips {
                    width: entry.width,
                    height: entry.height,
                    levels,
                });
                entry.mips = Some(Rc::clone(&mips));
                Some(mips)
            }
        }
    })
}

pub struct WgpuRenderer {
    instance: wgpu::Instance,
    adapter: wgpu::Adapter,
    device: wgpu::Device,
    queue: wgpu::Queue,
    surface: Option<wgpu::Surface<'static>>,
    config: Option<wgpu::SurfaceConfiguration>,
    rect_pipeline: wgpu::RenderPipeline,
    gradient_pipeline: wgpu::RenderPipeline,
    text_pipeline: wgpu::RenderPipeline,
    text_bind_group_layout: wgpu::BindGroupLayout,
    pub glyphon: GlyphonBridge,
    /// Separate glyphon instance for overlay/z>0 text.  Two independent
    /// atlases prevent prepare() conflicts within a single render pass.
    glyphon_overlay: Option<GlyphonBridge>,
    /// Image texture cache: (hash → texture, view, bind_group, w, h, last_used_frame)
    image_cache: HashMap<
        u64,
        (
            wgpu::Texture,
            wgpu::TextureView,
            wgpu::BindGroup,
            f32,
            f32,
            u64,
        ),
    >,
    image_cache_frame: u64,
    screen_size: Size,
    scale_factor: f32,
    clear_color: crate::style::Color,
    /// Persistent retained framebuffer — avoids swapchain LoadOp::Load unreliability.
    persistent_tex: Option<wgpu::Texture>,
    persistent_view: Option<wgpu::TextureView>,
    persistent_bind_group: Option<wgpu::BindGroup>,
    msaa_tex: Option<wgpu::Texture>,
    msaa_view: Option<wgpu::TextureView>,
    /// Sample count for MSAA (same value used for pipelines and glyphon).
    sample_count: u32,
    persistent_dirty: bool,
    blit_pipeline: wgpu::RenderPipeline,
    blit_bind_group_layout: wgpu::BindGroupLayout,
    image_sampler: wgpu::Sampler,
    blit_sampler: wgpu::Sampler,
    belt: wgpu::util::StagingBelt,
    rect_vbuf: Option<(wgpu::Buffer, u64)>,
    rect_ibuf: Option<(wgpu::Buffer, u64)>,
    effect_vbuf: Option<(wgpu::Buffer, u64)>,
    effect_ibuf: Option<(wgpu::Buffer, u64)>,
    grad_vbuf: Option<(wgpu::Buffer, u64)>,
    grad_ibuf: Option<(wgpu::Buffer, u64)>,
    image_vbuf: Option<(wgpu::Buffer, u64)>,
    image_ibuf: Option<(wgpu::Buffer, u64)>,
    blit_vbuf: Option<wgpu::Buffer>,
    blit_ibuf: Option<wgpu::Buffer>,
    path_pipeline: wgpu::RenderPipeline,
    path_cache: rustc_hash::FxHashMap<PathCacheKey, CachedPathMesh>,
    path_cache_frame: u64,
    path_vbuf: Option<(wgpu::Buffer, u64)>,
    path_ibuf: Option<(wgpu::Buffer, u64)>,
    // ── Effect compositing (blend modes; P3 adds backdrop blur) ──
    // snapshot_tex is a copy of the persistent (resolved) texture taken right
    // before an effect pass, so the effect shader can read the backdrop (dst)
    // without a feedback loop on the live attachment.
    snapshot_tex: Option<wgpu::Texture>,
    snapshot_view: Option<wgpu::TextureView>,
    snapshot_bind_group: Option<wgpu::BindGroup>,
    blend_pipeline: wgpu::RenderPipeline,
    blend_bind_group_layout: wgpu::BindGroupLayout,
    blend_sampler: wgpu::Sampler,
    // ── Backdrop blur (P3) ──
    blur_pipeline: wgpu::RenderPipeline,
    blur_bind_group_layout: wgpu::BindGroupLayout,
    composite_pipeline: wgpu::RenderPipeline,
    effect_h_tex: Option<wgpu::Texture>,
    effect_h_view: Option<wgpu::TextureView>,
    effect_v_tex: Option<wgpu::Texture>,
    effect_v_view: Option<wgpu::TextureView>,
    effect_v_bind_group: Option<wgpu::BindGroup>,
    /// Persistent blur uniform buffers + bind groups (audit 2026-07-17
    /// round 5, B5): previously re-created per blur region per frame
    /// (2 create_buffer + 2 create_bind_group each). The bind groups
    /// reference the persistent snapshot/effect views, so they are only
    /// invalidated on resize (views recreated → set to None).
    blur_h_res: Option<(wgpu::Buffer, wgpu::BindGroup)>,
    blur_v_res: Option<(wgpu::Buffer, wgpu::BindGroup)>,
    backdrop_vbuf: Option<(wgpu::Buffer, u64)>,
    backdrop_ibuf: Option<(wgpu::Buffer, u64)>,
}

impl WgpuRenderer {
    pub async fn new(window: Arc<dyn winit::window::Window>) -> Result<Self, RenderError> {
        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        let t_total = std::time::Instant::now();
        let size = window.surface_size();
        let sf = window.scale_factor() as f32;
        let screen_size = Size::new(size.width as f32, size.height as f32);

        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        let t0 = std::time::Instant::now();
        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
            backends: wgpu::Backends::PRIMARY,
            flags: wgpu::InstanceFlags::default(),
            backend_options: wgpu::BackendOptions::default(),
            memory_budget_thresholds: Default::default(),
            display: None,
        });
        let surface = match instance.create_surface(Arc::clone(&window)) {
            Ok(s) => s,
            Err(e) => {
                push_error(UiError::GpuInit(crate::core::error::GpuErrorKind::Other(
                    e.to_string(),
                )));
                return Err(RenderError::Surface);
            }
        };
        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        tracing::debug!(target: "wgpu", "instance+surface: {:?}", t0.elapsed());

        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        let t1 = std::time::Instant::now();
        let adapter = match instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: Some(&surface),
                ..Default::default()
            })
            .await
        {
            Ok(a) => a,
            Err(e) => {
                push_error(UiError::GpuInit(crate::core::error::GpuErrorKind::Other(
                    e.to_string(),
                )));
                return Err(RenderError::NoAdapter);
            }
        };
        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        tracing::debug!(target: "wgpu", "request_adapter: {:?}", t1.elapsed());

        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        let t2 = std::time::Instant::now();
        let (device, queue) = match adapter
            .request_device(&wgpu::DeviceDescriptor::default())
            .await
        {
            Ok(dq) => dq,
            Err(e) => {
                push_error(UiError::GpuInit(crate::core::error::GpuErrorKind::Other(
                    e.to_string(),
                )));
                return Err(RenderError::Device);
            }
        };
        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        tracing::debug!(target: "wgpu", "request_device: {:?}", t2.elapsed());

        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        let t3 = std::time::Instant::now();
        let config = match surface.get_default_config(&adapter, size.width, size.height) {
            Some(c) => c,
            None => {
                push_error(UiError::GpuInit(crate::core::error::GpuErrorKind::Surface));
                return Err(RenderError::Surface);
            }
        };
        surface.configure(&device, &config);

        let sample_count = 4u32;
        let rect_pipeline = create_rect_pipeline(&device, config.format, sample_count);
        let gradient_pipeline = create_gradient_pipeline(&device, config.format, sample_count);
        let text_bind_group_layout = create_text_bind_group_layout(&device);
        let text_pipeline = create_text_pipeline(
            &device,
            config.format,
            &text_bind_group_layout,
            sample_count,
        );
        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        tracing::debug!(target: "wgpu", "pipelines: {:?}", t3.elapsed());

        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        let t4 = std::time::Instant::now();
        let glyphon = GlyphonBridge::new(
            &device,
            &queue,
            config.format,
            size.width,
            size.height,
            wgpu::MultisampleState {
                count: sample_count,
                mask: !0,
                alpha_to_coverage_enabled: false,
            },
        );
        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        tracing::debug!(target: "wgpu", "glyphon bridge: {:?}", t4.elapsed());

        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        let t5 = std::time::Instant::now();
        let blit_bind_group_layout = create_blit_bind_group_layout(&device);
        let blit_pipeline =
            create_blit_pipeline(&device, config.format, &blit_bind_group_layout, 1);
        let path_pipeline = create_path_pipeline(&device, config.format, sample_count);
        let image_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("image sampler"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::MipmapFilterMode::Linear,
            ..Default::default()
        });
        let blit_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("blit sampler"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            ..Default::default()
        });
        // ── Effect (blend) infrastructure ──
        let blend_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("blend sampler"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            ..Default::default()
        });
        let blend_bind_group_layout = create_blend_bind_group_layout(&device);
        let blend_pipeline = create_blend_pipeline(
            &device,
            config.format,
            &blend_bind_group_layout,
            sample_count,
        );
        let blur_bind_group_layout = create_blur_bind_group_layout(&device);
        let blur_pipeline = create_blur_pipeline(&device, config.format, &blur_bind_group_layout);
        // Composite samples a single texture+sampler (same layout shape as blit/blend tex+sampler).
        let composite_pipeline = create_composite_pipeline(
            &device,
            config.format,
            &blend_bind_group_layout,
            sample_count,
        );
        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        tracing::debug!(target: "wgpu", "blit pipeline: {:?}", t5.elapsed());

        #[cfg(any(feature = "devtools", feature = "file-logging"))]
        tracing::debug!(target: "wgpu", "total: {:?}", t_total.elapsed());

        let blit_vbuf = {
            let verts: [BlitVertex; 4] = [
                BlitVertex {
                    position: [-1.0, -1.0],
                    uv: [0.0, 1.0],
                },
                BlitVertex {
                    position: [1.0, -1.0],
                    uv: [1.0, 1.0],
                },
                BlitVertex {
                    position: [1.0, 1.0],
                    uv: [1.0, 0.0],
                },
                BlitVertex {
                    position: [-1.0, 1.0],
                    uv: [0.0, 0.0],
                },
            ];
            let idx: [u32; 6] = [0, 1, 2, 0, 2, 3];
            let v = device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("blit vbuf"),
                size: std::mem::size_of_val(&verts) as u64,
                usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: true,
            });
            if let Ok(mut mapped) = v.slice(..).get_mapped_range_mut() {
                mapped.copy_from_slice(bytemuck::cast_slice(&verts));
            } else {
                crate::core::error::push_error(crate::core::error::UiError::GpuRender(
                    "Failed to map blit vertex buffer".into(),
                ));
            }
            v.unmap();
            let i = device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("blit ibuf"),
                size: std::mem::size_of_val(&idx) as u64,
                usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: true,
            });
            if let Ok(mut mapped) = i.slice(..).get_mapped_range_mut() {
                mapped.copy_from_slice(bytemuck::cast_slice(&idx));
            } else {
                crate::core::error::push_error(crate::core::error::UiError::GpuRender(
                    "Failed to map blit index buffer".into(),
                ));
            }
            i.unmap();
            (v, i)
        };

        let belt = wgpu::util::StagingBelt::new(device.clone(), 1_000_000);

        Ok(Self {
            instance,
            adapter,
            device,
            queue,
            surface: Some(surface),
            config: Some(config),
            rect_pipeline,
            gradient_pipeline,
            text_pipeline,
            text_bind_group_layout,
            glyphon,
            glyphon_overlay: None,
            image_cache: HashMap::new(),
            image_cache_frame: 0,
            screen_size,
            scale_factor: sf,
            clear_color: crate::style::Color::rgba8(26, 26, 31, 255),
            persistent_tex: None,
            persistent_view: None,
            persistent_bind_group: None,
            msaa_tex: None,
            msaa_view: None,
            persistent_dirty: true,
            sample_count,
            blit_pipeline,
            blit_bind_group_layout,
            blit_sampler,
            image_sampler,
            belt,
            rect_vbuf: None,
            rect_ibuf: None,
            effect_vbuf: None,
            effect_ibuf: None,
            grad_vbuf: None,
            grad_ibuf: None,
            image_vbuf: None,
            image_ibuf: None,
            blit_vbuf: Some(blit_vbuf.0),
            blit_ibuf: Some(blit_vbuf.1),
            path_pipeline,
            path_cache: rustc_hash::FxHashMap::default(),
            path_cache_frame: 0,
            path_vbuf: None,
            path_ibuf: None,
            snapshot_tex: None,
            snapshot_view: None,
            snapshot_bind_group: None,
            blend_pipeline,
            blend_bind_group_layout,
            blend_sampler,
            blur_pipeline,
            blur_bind_group_layout,
            composite_pipeline,
            effect_h_tex: None,
            effect_h_view: None,
            effect_v_tex: None,
            effect_v_view: None,
            effect_v_bind_group: None,
            blur_h_res: None,
            blur_v_res: None,
            backdrop_vbuf: None,
            backdrop_ibuf: None,
        })
    }

    pub fn set_clear_color(&mut self, c: crate::style::Color) {
        self.clear_color = c;
    }

    #[allow(dead_code)]
    fn surface_config(&self) -> Option<&wgpu::SurfaceConfiguration> {
        self.config.as_ref()
    }

    pub fn has_surface(&self) -> bool {
        self.surface.is_some()
    }

    pub fn begin_frame(&mut self) -> Result<Frame, RenderError> {
        let surface = self.surface.as_ref().ok_or(RenderError::Surface)?;
        let (texture, view) = match surface.get_current_texture() {
            wgpu::CurrentSurfaceTexture::Success(t) => {
                let v = t
                    .texture
                    .create_view(&wgpu::TextureViewDescriptor::default());
                (t, v)
            }
            wgpu::CurrentSurfaceTexture::Suboptimal(t) => {
                let v = t
                    .texture
                    .create_view(&wgpu::TextureViewDescriptor::default());
                (t, v)
            }
            _ => return Err(RenderError::Surface),
        };
        let encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("burin frame"),
            });
        Ok(Frame {
            texture,
            view,
            encoder,
        })
    }

    fn ensure_persistent_texture(&mut self) {
        let cfg = match &self.config {
            Some(c) => c,
            None => return,
        };
        let w = cfg.width;
        let h = cfg.height;
        if w == 0 || h == 0 {
            return;
        }
        let fmt = cfg.format;
        let msaa = 4u32;
        if let Some(ref tex) = self.persistent_tex {
            if tex.width() == w && tex.height() == h {
                return;
            }
        }

        let msaa_tex = self.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("msaa persistent"),
            size: wgpu::Extent3d {
                width: w,
                height: h,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: msaa,
            dimension: wgpu::TextureDimension::D2,
            format: fmt,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
            view_formats: &[],
        });
        let msaa_view = msaa_tex.create_view(&wgpu::TextureViewDescriptor::default());

        let resolve_tex = self.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("resolve persistent"),
            size: wgpu::Extent3d {
                width: w,
                height: h,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: fmt,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT
                | wgpu::TextureUsages::TEXTURE_BINDING
                | wgpu::TextureUsages::COPY_DST
                | wgpu::TextureUsages::COPY_SRC,
            view_formats: &[],
        });
        let resolve_view = resolve_tex.create_view(&wgpu::TextureViewDescriptor::default());
        let bg = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("blit bind group"),
            layout: &self.blit_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&resolve_view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&self.blit_sampler),
                },
            ],
        });

        // Snapshot texture: a COPY_DST target holding a copy of the persistent
        // texture, sampled by effect shaders as the backdrop (dst).
        let snapshot_tex = self.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("effect snapshot"),
            size: wgpu::Extent3d {
                width: w,
                height: h,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: fmt,
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
            view_formats: &[],
        });
        let snapshot_view = snapshot_tex.create_view(&wgpu::TextureViewDescriptor::default());
        let snapshot_bg = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("effect snapshot bind group"),
            layout: &self.blend_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&snapshot_view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&self.blend_sampler),
                },
            ],
        });

        // Effect ping-pong textures for separable Gaussian blur (backdrop).
        let effect_desc = wgpu::TextureDescriptor {
            label: Some("effect blur"),
            size: wgpu::Extent3d {
                width: w,
                height: h,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: fmt,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        };
        let effect_h_tex = self.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("effect blur h"),
            ..effect_desc.clone()
        });
        let effect_h_view = effect_h_tex.create_view(&wgpu::TextureViewDescriptor::default());
        let effect_v_tex = self.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("effect blur v"),
            ..effect_desc
        });
        let effect_v_view = effect_v_tex.create_view(&wgpu::TextureViewDescriptor::default());
        // Bind group for the composite pass: samples the final (vertical) blur result.
        let effect_v_bg = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("effect blur v bind group"),
            layout: &self.blend_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&effect_v_view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&self.blend_sampler),
                },
            ],
        });

        self.msaa_tex = Some(msaa_tex);
        self.msaa_view = Some(msaa_view);
        self.persistent_tex = Some(resolve_tex);
        self.persistent_view = Some(resolve_view);
        self.persistent_bind_group = Some(bg);
        self.snapshot_tex = Some(snapshot_tex);
        self.snapshot_view = Some(snapshot_view);
        self.snapshot_bind_group = Some(snapshot_bg);
        self.effect_h_tex = Some(effect_h_tex);
        self.effect_h_view = Some(effect_h_view);
        self.effect_v_tex = Some(effect_v_tex);
        self.effect_v_view = Some(effect_v_view);
        self.effect_v_bind_group = Some(effect_v_bg);
        // Blur uniforms/bind groups reference the OLD views — rebuild lazily (B5).
        self.blur_h_res = None;
        self.blur_v_res = None;
        self.persistent_dirty = true;
    }

    pub fn draw_commands(
        &mut self,
        frame: &mut Frame,
        commands: &[DrawCommand],
        text_areas: &[TextAreaDesc],
        backdrop_regions: &[crate::render::BackdropRegion],
        _hint: Size,
    ) {
        self.ensure_persistent_texture();

        let (cfg_w, cfg_h, cfg_fmt) = {
            let Some(cfg) = self.config.as_ref() else {
                crate::core::error::push_error(crate::core::error::UiError::GpuRender(
                    "draw_commands: no active surface".into(),
                ));
                return;
            };
            (cfg.width, cfg.height, cfg.format)
        };

        let screen = Size::new(
            self.screen_size.width / self.scale_factor,
            self.screen_size.height / self.scale_factor,
        );

        // ── Build per-z-layer vertex data ──
        // Commands and text areas are grouped by z_index so that each
        // layer's surfaces can be drawn before its text, and higher-z
        // layers naturally occlude lower-z layers (no z-culling needed).

        // Sort text_areas by z_index so we can iterate with a cursor
        // inside the z-layer loop instead of allocating a per-frame
        // HashMap<i32, Vec<TextAreaDesc>> (audit 2026-07-18).
        // `commands` is already sorted by z_index so input order is
        // preserved for the caller; we sort a local clone for per-layer
        // consumption.
        let mut text_by_z: Vec<TextAreaDesc> = text_areas.to_vec();
        text_by_z.sort_unstable_by_key(|t| t.z_index);

        self.image_cache_frame += 1;
        let max_images: usize = 64;

        // Helper: compute padded bytes_per_row aligned to 256 (WebGPU requirement)
        let padded_bpr = |width: u32| -> u32 {
            let bpr = width * 4;
            (bpr + 255) & !255
        };

        // Phase 1: Image upload (with lazy mip chain)
        for cmd in commands {
            if let DrawCommand::DrawImage { hash, .. } = cmd {
                if !self.image_cache.contains_key(hash) {
                    if self.image_cache.len() >= max_images {
                        let oldest_hash = match self.image_cache.iter().min_by_key(|(_, v)| v.5) {
                            Some((&h, _)) => h,
                            None => continue,
                        };
                        self.image_cache.remove(&oldest_hash);
                    }
                    if let Some(mips) = lookup_image_mips(*hash) {
                        let mip_count = mips.levels.len() as u32;
                        let tex_size = wgpu::Extent3d {
                            width: mips.width,
                            height: mips.height,
                            depth_or_array_layers: 1,
                        };
                        let tex = self.device.create_texture(&wgpu::TextureDescriptor {
                            label: Some("image"),
                            size: tex_size,
                            mip_level_count: mip_count,
                            sample_count: 1,
                            dimension: wgpu::TextureDimension::D2,
                            format: wgpu::TextureFormat::Rgba8UnormSrgb,
                            usage: wgpu::TextureUsages::TEXTURE_BINDING
                                | wgpu::TextureUsages::COPY_DST,
                            view_formats: &[],
                        });
                        for (i, level_data) in mips.levels.iter().enumerate() {
                            let lw = (mips.width >> i).max(1);
                            let lh = (mips.height >> i).max(1);
                            let src_bpr = lw * 4;
                            let dst_bpr = padded_bpr(lw);
                            if src_bpr == dst_bpr {
                                self.queue.write_texture(
                                    wgpu::TexelCopyTextureInfo {
                                        texture: &tex,
                                        mip_level: i as u32,
                                        origin: wgpu::Origin3d::ZERO,
                                        aspect: wgpu::TextureAspect::All,
                                    },
                                    level_data,
                                    wgpu::TexelCopyBufferLayout {
                                        offset: 0,
                                        bytes_per_row: Some(src_bpr),
                                        rows_per_image: Some(lh),
                                    },
                                    wgpu::Extent3d {
                                        width: lw,
                                        height: lh,
                                        depth_or_array_layers: 1,
                                    },
                                );
                            } else {
                                let mut padded = Vec::with_capacity((dst_bpr * lh) as usize);
                                for row in 0..lh {
                                    let start = (row * src_bpr) as usize;
                                    padded.extend_from_slice(
                                        &level_data[start..start + src_bpr as usize],
                                    );
                                    padded.resize(padded.len() + (dst_bpr - src_bpr) as usize, 0);
                                }
                                self.queue.write_texture(
                                    wgpu::TexelCopyTextureInfo {
                                        texture: &tex,
                                        mip_level: i as u32,
                                        origin: wgpu::Origin3d::ZERO,
                                        aspect: wgpu::TextureAspect::All,
                                    },
                                    &padded,
                                    wgpu::TexelCopyBufferLayout {
                                        offset: 0,
                                        bytes_per_row: Some(dst_bpr),
                                        rows_per_image: Some(lh),
                                    },
                                    wgpu::Extent3d {
                                        width: lw,
                                        height: lh,
                                        depth_or_array_layers: 1,
                                    },
                                );
                            }
                        }
                        let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
                        let bg = create_atlas_bind_group(
                            &self.device,
                            &self.text_bind_group_layout,
                            &view,
                            Some(&self.image_sampler),
                        );
                        self.image_cache.insert(
                            *hash,
                            (
                                tex,
                                view,
                                bg,
                                mips.width as f32,
                                mips.height as f32,
                                self.image_cache_frame,
                            ),
                        );
                    }
                }
            }
        }

        // ── Pass 1: Batch collect + single render pass ──
        let mut accessed_image_hashes: Vec<u64> = Vec::new();
        {
            let mut rect_verts: Vec<RectVertex> = Vec::with_capacity(commands.len() * 4);
            let mut rect_indices: Vec<u32> = Vec::with_capacity(commands.len() * 6);
            // Effect rects (blend_mode != 0): collected separately so they can be
            // drawn in their own render pass, inserted at the correct z-boundary
            // between segments. P1 draws them as normal rects (proves segmentation
            // + z-order); P2 swaps in the blend pipeline sampling a backdrop snapshot.
            let mut effect_verts: Vec<RectVertex> = Vec::new();
            let mut effect_indices: Vec<u32> = Vec::new();
            struct EffectOp {
                z: i32,
                i_start: u32,
                i_count: u32,
                #[allow(dead_code)]
                blend_mode: u8,
            }
            let mut effect_ops: Vec<EffectOp> = Vec::new();
            // Backdrop-blur regions become composite quads (RectVertex) drawn after
            // a per-region separable Gaussian blur of the backdrop snapshot.
            let mut backdrop_verts: Vec<RectVertex> = Vec::new();
            let mut backdrop_indices: Vec<u32> = Vec::new();
            struct BackdropOp {
                z: i32,
                i_start: u32,
                i_count: u32,
                blur_radius: f32,
            }
            let mut backdrop_ops: Vec<BackdropOp> = Vec::new();
            let mut grad_verts: Vec<GradientVertex> = Vec::with_capacity(16);
            let mut grad_indices: Vec<u32> = Vec::with_capacity(24);
            let mut image_verts: Vec<TextVertex> = Vec::with_capacity(8);
            let mut image_indices: Vec<u32> = Vec::with_capacity(12);
            let mut image_bind_groups: Vec<&wgpu::BindGroup> = Vec::new();
            let mut path_verts: Vec<PathVertex> = Vec::with_capacity(256);
            let mut path_indices: Vec<u32> = Vec::with_capacity(512);

            let mut cmd_indices: Vec<usize> = (0..commands.len()).collect();
            cmd_indices.sort_by_key(|&i| commands[i].z_index());
            let mut cur_z: Option<i32> = None;
            let (
                mut rv0,
                mut ris0,
                mut gv0,
                mut gis0,
                mut iv0,
                mut iis0,
                mut ibo0,
                mut pv0,
                mut pis0,
            ) = (0u32, 0u32, 0u32, 0u32, 0u32, 0u32, 0u32, 0u32, 0u32);
            #[allow(dead_code)] // per-primitive counters; only the vertex/index starts are consumed today
            struct ZRange {
                z: i32,
                rv: u32,
                rc: u32,
                ris: u32,
                ric: u32,
                gv: u32,
                gc: u32,
                gis: u32,
                gic: u32,
                iv: u32,
                ic: u32,
                iis: u32,
                iic: u32,
                ibo: u32,
                pv: u32,
                pc: u32,
                pis: u32,
                pic: u32,
            }
            let mut z_layers: Vec<ZRange> = Vec::new();
            for &i in &cmd_indices {
                let cmd = &commands[i];
                let z = cmd.z_index();
                if Some(z) != cur_z {
                    if let Some(pz) = cur_z {
                        z_layers.push(ZRange {
                            z: pz,
                            rv: rv0,
                            rc: rect_verts.len() as u32 - rv0,
                            ris: ris0,
                            ric: rect_indices.len() as u32 - ris0,
                            gv: gv0,
                            gc: grad_verts.len() as u32 - gv0,
                            gis: gis0,
                            gic: grad_indices.len() as u32 - gis0,
                            iv: iv0,
                            ic: image_verts.len() as u32 - iv0,
                            iis: iis0,
                            iic: image_indices.len() as u32 - iis0,
                            ibo: ibo0,
                            pv: pv0,
                            pc: path_verts.len() as u32 - pv0,
                            pis: pis0,
                            pic: path_indices.len() as u32 - pis0,
                        });
                    }
                    rv0 = rect_verts.len() as u32;
                    ris0 = rect_indices.len() as u32;
                    gv0 = grad_verts.len() as u32;
                    gis0 = grad_indices.len() as u32;
                    iv0 = image_verts.len() as u32;
                    iis0 = image_indices.len() as u32;
                    ibo0 = image_bind_groups.len() as u32;
                    pv0 = path_verts.len() as u32;
                    pis0 = path_indices.len() as u32;
                    cur_z = Some(z);
                }
                let clip = match cmd {
                    DrawCommand::FillRect { clip, .. } => clip,
                    DrawCommand::StrokeRect { clip, .. } => clip,
                    DrawCommand::FillShadow { clip, .. } => clip,
                    DrawCommand::FillLinearGradient { clip, .. } => clip,
                    DrawCommand::DrawImage { clip, .. } => clip,
                    DrawCommand::FillPath { clip, .. } => clip,
                    DrawCommand::StrokePath { clip, .. } => clip,
                };
                let xform = match cmd {
                    DrawCommand::FillRect { transform, .. } => transform,
                    DrawCommand::StrokeRect { transform, .. } => transform,
                    DrawCommand::FillShadow { transform, .. } => transform,
                    DrawCommand::FillLinearGradient { transform, .. } => transform,
                    DrawCommand::DrawImage { transform, .. } => transform,
                    DrawCommand::FillPath { transform, .. } => transform,
                    DrawCommand::StrokePath { transform, .. } => transform,
                };
                let (_sx, _sy, sw, sh) = clip_to_scissor(*clip, screen, self.scale_factor);
                if sw == 0 || sh == 0 {
                    continue;
                }

                match cmd {
                    DrawCommand::FillRect {
                        rect,
                        color,
                        radius,
                        clip,
                        blend_mode,
                        ..
                    } => {
                        if *blend_mode == 0 {
                            let base = rect_verts.len() as u32;
                            let v = rect_to_vertices(
                                *rect, screen, *color, *radius, 0.0, *xform, *clip, [0.0; 2],
                                [0.0; 4], 0.0, 0.0,
                            );
                            rect_verts.extend_from_slice(&v);
                            rect_indices.extend_from_slice(&[
                                base,
                                base + 1,
                                base + 2,
                                base,
                                base + 2,
                                base + 3,
                            ]);
                        } else {
                            let base = effect_verts.len() as u32;
                            let i_start = effect_indices.len() as u32;
                            let mut v = rect_to_vertices(
                                *rect, screen, *color, *radius, 0.0, *xform, *clip, [0.0; 2],
                                [0.0; 4], 0.0, 0.0,
                            );
                            // Bake blend mode into is_shadow (see BLEND_SHADER field
                            // repurposing). Snapshot UV is derived from NDC in the shader.
                            for vert in v.iter_mut() {
                                vert.is_shadow = *blend_mode as f32;
                            }
                            effect_verts.extend_from_slice(&v);
                            effect_indices.extend_from_slice(&[
                                base,
                                base + 1,
                                base + 2,
                                base,
                                base + 2,
                                base + 3,
                            ]);
                            effect_ops.push(EffectOp {
                                z,
                                i_start,
                                i_count: 6,
                                blend_mode: *blend_mode,
                            });
                        }
                    }
                    DrawCommand::StrokeRect {
                        rect,
                        color,
                        width,
                        radius,
                        clip,
                        blend_mode,
                        ..
                    } => {
                        if *blend_mode == 0 {
                            let base = rect_verts.len() as u32;
                            let v = rect_to_vertices(
                                *rect, screen, *color, *radius, *width, *xform, *clip, [0.0; 2],
                                [0.0; 4], 0.0, 0.0,
                            );
                            rect_verts.extend_from_slice(&v);
                            rect_indices.extend_from_slice(&[
                                base,
                                base + 1,
                                base + 2,
                                base,
                                base + 2,
                                base + 3,
                            ]);
                        } else {
                            let base = effect_verts.len() as u32;
                            let i_start = effect_indices.len() as u32;
                            let mut v = rect_to_vertices(
                                *rect, screen, *color, *radius, *width, *xform, *clip, [0.0; 2],
                                [0.0; 4], 0.0, 0.0,
                            );
                            for vert in v.iter_mut() {
                                vert.is_shadow = *blend_mode as f32;
                            }
                            effect_verts.extend_from_slice(&v);
                            effect_indices.extend_from_slice(&[
                                base,
                                base + 1,
                                base + 2,
                                base,
                                base + 2,
                                base + 3,
                            ]);
                            effect_ops.push(EffectOp {
                                z,
                                i_start,
                                i_count: 6,
                                blend_mode: *blend_mode,
                            });
                        }
                    }
                    DrawCommand::FillShadow {
                        rect,
                        color,
                        radius,
                        shadow,
                        elem_size: _,
                        clip,
                        ..
                    } => {
                        let base = rect_verts.len() as u32;
                        let center: [f32; 2] =
                            [rect.x + rect.width * 0.5, rect.y + rect.height * 0.5];
                        let sc = shadow.color.to_linear_array();
                        let v = rect_to_vertices(
                            *rect,
                            screen,
                            *color,
                            *radius,
                            0.0,
                            *xform,
                            *clip,
                            center,
                            sc,
                            shadow.blur,
                            1.0,
                        );
                        rect_verts.extend_from_slice(&v);
                        rect_indices.extend_from_slice(&[
                            base,
                            base + 1,
                            base + 2,
                            base,
                            base + 2,
                            base + 3,
                        ]);
                    }
                    DrawCommand::FillLinearGradient {
                        rect,
                        gradient,
                        radius,
                        stroke_width,
                        clip,
                        ..
                    } => {
                        let base = grad_verts.len() as u32;
                        let v = gradient_to_vertices(
                            *rect,
                            screen,
                            *gradient,
                            *radius,
                            *stroke_width,
                            *xform,
                            *clip,
                        );
                        grad_verts.extend_from_slice(&v);
                        grad_indices.extend_from_slice(&[
                            base,
                            base + 1,
                            base + 2,
                            base,
                            base + 2,
                            base + 3,
                        ]);
                    }
                    DrawCommand::DrawImage {
                        hash,
                        rect,
                        opacity,
                        content_fit,
                        clip,
                        ..
                    } => {
                        if *opacity <= 0.0 {
                            continue;
                        }
                        accessed_image_hashes.push(*hash);
                        if let Some(&(_, _, ref bg, iw, ih, _)) = self.image_cache.get(hash) {
                            let render_rect = content_fit_rect(*content_fit, *rect, iw, ih);
                            let tp = xform
                                .transform_point2(glam::Vec2::new(render_rect.x, render_rect.y));
                            let base = image_verts.len() as u32;
                            let image_clip_rect: [f32; 4] = [
                                clip.rect.x,
                                clip.rect.y,
                                clip.rect.x + clip.rect.width,
                                clip.rect.y + clip.rect.height,
                            ];
                            let image_clip_radius: [f32; 4] = [
                                clip.radius.top_left,
                                clip.radius.top_right,
                                clip.radius.bottom_right,
                                clip.radius.bottom_left,
                            ];
                            let v = image_quad_vertices(
                                tp.x,
                                tp.y,
                                render_rect.width,
                                render_rect.height,
                                screen,
                                image_clip_rect,
                                image_clip_radius,
                                *opacity,
                            );
                            image_verts.extend_from_slice(&v);
                            image_indices.extend_from_slice(&[
                                base,
                                base + 1,
                                base + 2,
                                base,
                                base + 2,
                                base + 3,
                            ]);
                            while image_bind_groups.len() < (image_verts.len() / 4) {
                                image_bind_groups.push(bg);
                            }
                        }
                    }
                    DrawCommand::FillPath {
                        path,
                        brush,
                        clip,
                        transform,
                        ..
                    } => {
                        let ph = hash_bezpath(path);
                        let bh = hash_brush(brush);
                        let xh = hash_linear_transform(*transform);
                        let key = PathCacheKey {
                            path_hash: ph,
                            brush_hash: bh,
                            stroke_hash: 0,
                            xform_hash: xh,
                        };

                        let ka = glam_to_kurbo_affine(*transform);
                        let (tx, ty) = (transform.translation.x, transform.translation.y);

                        let (gs, ge) = match brush {
                            Brush::Gradient(g) => {
                                let bbox = match bezpath_bounds(path) {
                                    Some(b) => b,
                                    None => continue,
                                };
                                let sx = (bbox.x + g.start.0 * bbox.width) as f64;
                                let sy = (bbox.y + g.start.1 * bbox.height) as f64;
                                let ex = (bbox.x + g.end.0 * bbox.width) as f64;
                                let ey = (bbox.y + g.end.1 * bbox.height) as f64;
                                let sp = ka * kurbo::Point::new(sx, sy);
                                let ep = ka * kurbo::Point::new(ex, ey);
                                ((sp.x, sp.y), (ep.x, ep.y))
                            }
                            Brush::Solid(_) => ((0.0, 0.0), (0.0, 0.0)),
                        };

                        let sx_calc = 2.0 / screen.width;
                        let sy_calc = -2.0 / screen.height;

                        if !self.path_cache.contains_key(&key) {
                            let mut linear = *transform;
                            linear.translation = glam::Vec2::ZERO;
                            let transformed = glam_to_kurbo_affine(linear) * (**path).clone();
                            let lyon_p = bezpath_to_lyon(&transformed);

                            let mut buffers = VertexBuffers::<[f32; 2], u32>::new();
                            let options = FillOptions::default();
                            if FillTessellator::new()
                                .tessellate_path(
                                    &lyon_p,
                                    &options,
                                    &mut BuffersBuilder::new(&mut buffers, |v: FillVertex| {
                                        let pos = v.position();
                                        [pos.x, pos.y]
                                    }),
                                )
                                .is_ok()
                            {
                                self.path_cache.insert(
                                    key,
                                    CachedPathMesh {
                                        logical_vertices: buffers.vertices,
                                        indices: buffers.indices,
                                        last_frame: self.path_cache_frame,
                                    },
                                );
                            }
                        }
                        if let Some(cached) = self.path_cache.get_mut(&key) {
                            cached.last_frame = self.path_cache_frame;
                            let base = path_verts.len() as u32;
                            let cr = clip.rect;
                            let clip_r = [cr.x, cr.y, cr.x + cr.width, cr.y + cr.height];
                            let clip_rad = [
                                clip.radius.top_left,
                                clip.radius.top_right,
                                clip.radius.bottom_right,
                                clip.radius.bottom_left,
                            ];
                            for &v in &cached.logical_vertices {
                                let px = v[0] + tx;
                                let py = v[1] + ty;
                                let ndc_x = px * sx_calc - 1.0;
                                let ndc_y = py * sy_calc + 1.0;
                                let color = brush_color_at_pos(brush, px as f64, py as f64, gs, ge);
                                path_verts.push(PathVertex {
                                    position: [ndc_x, ndc_y],
                                    color,
                                    world_pos: [px, py],
                                    clip_rect: clip_r,
                                    clip_radius: clip_rad,
                                });
                            }
                            for &i in &cached.indices {
                                path_indices.push(base + i);
                            }
                        }
                    }
                    DrawCommand::StrokePath {
                        path,
                        stroke,
                        brush,
                        clip,
                        transform,
                        ..
                    } => {
                        let ph = hash_bezpath(path);
                        let bh = hash_brush(brush);
                        let sh = hash_stroke(stroke);
                        // Translation-invariant key (B1) — same as FillPath.
                        let xh = hash_linear_transform(*transform);
                        let key = PathCacheKey {
                            path_hash: ph,
                            brush_hash: bh,
                            stroke_hash: sh,
                            xform_hash: xh,
                        };

                        let ka = glam_to_kurbo_affine(*transform);
                        let (tx, ty) = (transform.translation.x, transform.translation.y);

                        let (gs, ge) = match brush {
                            Brush::Gradient(g) => {
                                let bbox = match bezpath_bounds(path) {
                                    Some(b) => b,
                                    None => continue,
                                };
                                let sx = (bbox.x + g.start.0 * bbox.width) as f64;
                                let sy = (bbox.y + g.start.1 * bbox.height) as f64;
                                let ex = (bbox.x + g.end.0 * bbox.width) as f64;
                                let ey = (bbox.y + g.end.1 * bbox.height) as f64;
                                let sp = ka * kurbo::Point::new(sx, sy);
                                let ep = ka * kurbo::Point::new(ex, ey);
                                ((sp.x, sp.y), (ep.x, ep.y))
                            }
                            Brush::Solid(_) => ((0.0, 0.0), (0.0, 0.0)),
                        };

                        let sx_calc = 2.0 / screen.width;
                        let sy_calc = -2.0 / screen.height;

                        if !self.path_cache.contains_key(&key) {
                            let mut linear = *transform;
                            linear.translation = glam::Vec2::ZERO;
                            let transformed = glam_to_kurbo_affine(linear) * (**path).clone();
                            let lyon_p = bezpath_to_lyon(&transformed);

                            let mut buffers = VertexBuffers::<[f32; 2], u32>::new();
                            let line_width = stroke.width as f32;
                            let options = StrokeOptions::default()
                                .with_line_width(line_width.max(0.5))
                                .with_line_cap(match stroke.start_cap {
                                    kurbo::Cap::Butt => lyon::tessellation::LineCap::Butt,
                                    kurbo::Cap::Round => lyon::tessellation::LineCap::Round,
                                    kurbo::Cap::Square => lyon::tessellation::LineCap::Square,
                                })
                                .with_line_join(match stroke.join {
                                    kurbo::Join::Bevel => lyon::tessellation::LineJoin::Bevel,
                                    kurbo::Join::Round => lyon::tessellation::LineJoin::Round,
                                    kurbo::Join::Miter => lyon::tessellation::LineJoin::Miter,
                                });
                            if StrokeTessellator::new()
                                .tessellate_path(
                                    &lyon_p,
                                    &options,
                                    &mut BuffersBuilder::new(
                                        &mut buffers,
                                        |v: lyon::tessellation::StrokeVertex| {
                                            let pos = v.position();
                                            [pos.x, pos.y]
                                        },
                                    ),
                                )
                                .is_ok()
                            {
                                self.path_cache.insert(
                                    key,
                                    CachedPathMesh {
                                        logical_vertices: buffers.vertices,
                                        indices: buffers.indices,
                                        last_frame: self.path_cache_frame,
                                    },
                                );
                            }
                        }
                        if let Some(cached) = self.path_cache.get_mut(&key) {
                            cached.last_frame = self.path_cache_frame;
                            let base = path_verts.len() as u32;
                            let cr = clip.rect;
                            let clip_r = [cr.x, cr.y, cr.x + cr.width, cr.y + cr.height];
                            let clip_rad = [
                                clip.radius.top_left,
                                clip.radius.top_right,
                                clip.radius.bottom_right,
                                clip.radius.bottom_left,
                            ];
                            for &v in &cached.logical_vertices {
                                let px = v[0] + tx;
                                let py = v[1] + ty;
                                let ndc_x = px * sx_calc - 1.0;
                                let ndc_y = py * sy_calc + 1.0;
                                let color = brush_color_at_pos(brush, px as f64, py as f64, gs, ge);
                                path_verts.push(PathVertex {
                                    position: [ndc_x, ndc_y],
                                    color,
                                    world_pos: [px, py],
                                    clip_rect: clip_r,
                                    clip_radius: clip_rad,
                                });
                            }
                            for &i in &cached.indices {
                                path_indices.push(base + i);
                            }
                        }
                    }
                }
            }

            if let Some(z) = cur_z {
                z_layers.push(ZRange {
                    z,
                    rv: rv0,
                    rc: rect_verts.len() as u32 - rv0,
                    ris: ris0,
                    ric: rect_indices.len() as u32 - ris0,
                    gv: gv0,
                    gc: grad_verts.len() as u32 - gv0,
                    gis: gis0,
                    gic: grad_indices.len() as u32 - gis0,
                    iv: iv0,
                    ic: image_verts.len() as u32 - iv0,
                    iis: iis0,
                    iic: image_indices.len() as u32 - iis0,
                    ibo: ibo0,
                    pv: pv0,
                    pc: path_verts.len() as u32 - pv0,
                    pis: pis0,
                    pic: path_indices.len() as u32 - pis0,
                });
            }

            // ── Backdrop-blur composite quads ──
            // Each region → a rounded RectVertex quad (tint baked into color, tint
            // strength in color.a). Drawn after a per-region separable Gaussian blur
            // of the backdrop snapshot; sampled in the composite shader via NDC UV.
            for br in backdrop_regions {
                let i_start = backdrop_indices.len() as u32;
                let base = backdrop_verts.len() as u32;
                let (tint_rgb, tint_a) = match br.tint {
                    Some(c) => ([c.r, c.g, c.b], c.a),
                    None => ([0.0, 0.0, 0.0], 0.0),
                };
                let tint = crate::style::Color {
                    r: tint_rgb[0],
                    g: tint_rgb[1],
                    b: tint_rgb[2],
                    a: tint_a,
                };
                let mut v = rect_to_vertices(
                    br.rect,
                    screen,
                    tint,
                    br.corner_radius,
                    0.0,
                    br.transform,
                    br.rect.into(),
                    [0.0; 2],
                    [0.0; 4],
                    0.0,
                    0.0,
                );
                for vert in v.iter_mut() {
                    vert.is_shadow = 0.0;
                }
                backdrop_verts.extend_from_slice(&v);
                backdrop_indices.extend_from_slice(&[
                    base,
                    base + 1,
                    base + 2,
                    base,
                    base + 2,
                    base + 3,
                ]);
                backdrop_ops.push(BackdropOp {
                    z: br.z_index,
                    i_start,
                    i_count: 6,
                    blur_radius: br.blur_radius,
                });
            }

            // ── Belt uploads: encode copy_buffer_to_buffer before render pass ──
            let rect_vbuf: Option<&wgpu::Buffer> = if !rect_verts.is_empty() {
                let data = bytemuck::cast_slice(&rect_verts);
                let vbuf = Self::ensure_buffer(
                    &mut self.rect_vbuf,
                    data.len() as u64,
                    wgpu::BufferUsages::VERTEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        vbuf,
                        0,
                        wgpu::BufferSize::new(data.len() as u64).unwrap(),
                    )
                    .copy_from_slice(data);
                let idata = bytemuck::cast_slice(&rect_indices);
                let ibuf = Self::ensure_buffer(
                    &mut self.rect_ibuf,
                    idata.len() as u64,
                    wgpu::BufferUsages::INDEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        ibuf,
                        0,
                        wgpu::BufferSize::new(idata.len() as u64).unwrap(),
                    )
                    .copy_from_slice(idata);
                Some(vbuf)
            } else {
                None
            };
            let rect_ibuf_ref: Option<&wgpu::Buffer> = if !rect_verts.is_empty() {
                self.rect_ibuf.as_ref().map(|(b, _)| b)
            } else {
                None
            };

            let grad_vbuf_ref: Option<&wgpu::Buffer> = if !grad_verts.is_empty() {
                let data = bytemuck::cast_slice(&grad_verts);
                let vbuf = Self::ensure_buffer(
                    &mut self.grad_vbuf,
                    data.len() as u64,
                    wgpu::BufferUsages::VERTEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        vbuf,
                        0,
                        wgpu::BufferSize::new(data.len() as u64).unwrap(),
                    )
                    .copy_from_slice(data);
                let idata = bytemuck::cast_slice(&grad_indices);
                let ibuf = Self::ensure_buffer(
                    &mut self.grad_ibuf,
                    idata.len() as u64,
                    wgpu::BufferUsages::INDEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        ibuf,
                        0,
                        wgpu::BufferSize::new(idata.len() as u64).unwrap(),
                    )
                    .copy_from_slice(idata);
                Some(vbuf)
            } else {
                None
            };
            let grad_ibuf_ref: Option<&wgpu::Buffer> = if !grad_verts.is_empty() {
                self.grad_ibuf.as_ref().map(|(b, _)| b)
            } else {
                None
            };

            let image_vbuf_ref: Option<&wgpu::Buffer> = if !image_verts.is_empty() {
                let data = bytemuck::cast_slice(&image_verts);
                let vbuf = Self::ensure_buffer(
                    &mut self.image_vbuf,
                    data.len() as u64,
                    wgpu::BufferUsages::VERTEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        vbuf,
                        0,
                        wgpu::BufferSize::new(data.len() as u64).unwrap(),
                    )
                    .copy_from_slice(data);
                let idata = bytemuck::cast_slice(&image_indices);
                let ibuf = Self::ensure_buffer(
                    &mut self.image_ibuf,
                    idata.len() as u64,
                    wgpu::BufferUsages::INDEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        ibuf,
                        0,
                        wgpu::BufferSize::new(idata.len() as u64).unwrap(),
                    )
                    .copy_from_slice(idata);
                Some(vbuf)
            } else {
                None
            };
            let image_ibuf_ref: Option<&wgpu::Buffer> = if !image_verts.is_empty() {
                self.image_ibuf.as_ref().map(|(b, _)| b)
            } else {
                None
            };

            let path_vbuf_ref: Option<&wgpu::Buffer> = if !path_verts.is_empty() {
                let pvdata = bytemuck::cast_slice(&path_verts);
                let pvbuf = Self::ensure_buffer(
                    &mut self.path_vbuf,
                    pvdata.len() as u64,
                    wgpu::BufferUsages::VERTEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        pvbuf,
                        0,
                        wgpu::BufferSize::new(pvdata.len() as u64).unwrap(),
                    )
                    .copy_from_slice(pvdata);
                let pidata = bytemuck::cast_slice(&path_indices);
                let pibuf = Self::ensure_buffer(
                    &mut self.path_ibuf,
                    pidata.len() as u64,
                    wgpu::BufferUsages::INDEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        pibuf,
                        0,
                        wgpu::BufferSize::new(pidata.len() as u64).unwrap(),
                    )
                    .copy_from_slice(pidata);
                Some(pvbuf)
            } else {
                None
            };
            let path_ibuf_ref: Option<&wgpu::Buffer> = if !path_verts.is_empty() {
                self.path_ibuf.as_ref().map(|(b, _)| b)
            } else {
                None
            };

            let effect_vbuf_ref: Option<&wgpu::Buffer> = if !effect_verts.is_empty() {
                let data = bytemuck::cast_slice(&effect_verts);
                let vbuf = Self::ensure_buffer(
                    &mut self.effect_vbuf,
                    data.len() as u64,
                    wgpu::BufferUsages::VERTEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        vbuf,
                        0,
                        wgpu::BufferSize::new(data.len() as u64).unwrap(),
                    )
                    .copy_from_slice(data);
                let idata = bytemuck::cast_slice(&effect_indices);
                let ibuf = Self::ensure_buffer(
                    &mut self.effect_ibuf,
                    idata.len() as u64,
                    wgpu::BufferUsages::INDEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        ibuf,
                        0,
                        wgpu::BufferSize::new(idata.len() as u64).unwrap(),
                    )
                    .copy_from_slice(idata);
                Some(vbuf)
            } else {
                None
            };
            let effect_ibuf_ref: Option<&wgpu::Buffer> = if !effect_verts.is_empty() {
                self.effect_ibuf.as_ref().map(|(b, _)| b)
            } else {
                None
            };

            let backdrop_vbuf_ref: Option<&wgpu::Buffer> = if !backdrop_verts.is_empty() {
                let data = bytemuck::cast_slice(&backdrop_verts);
                let vbuf = Self::ensure_buffer(
                    &mut self.backdrop_vbuf,
                    data.len() as u64,
                    wgpu::BufferUsages::VERTEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        vbuf,
                        0,
                        wgpu::BufferSize::new(data.len() as u64).unwrap(),
                    )
                    .copy_from_slice(data);
                let idata = bytemuck::cast_slice(&backdrop_indices);
                let ibuf = Self::ensure_buffer(
                    &mut self.backdrop_ibuf,
                    idata.len() as u64,
                    wgpu::BufferUsages::INDEX,
                    &self.device,
                );
                self.belt
                    .write_buffer(
                        &mut frame.encoder,
                        ibuf,
                        0,
                        wgpu::BufferSize::new(idata.len() as u64).unwrap(),
                    )
                    .copy_from_slice(idata);
                Some(vbuf)
            } else {
                None
            };
            let backdrop_ibuf_ref: Option<&wgpu::Buffer> = if !backdrop_verts.is_empty() {
                self.backdrop_ibuf.as_ref().map(|(b, _)| b)
            } else {
                None
            };

            // ── Z-layered persistent render pass(es) ──
            // Segmented rendering: when blend/backdrop effects are present, the
            // single pass is split at effect z-boundaries so each effect can be
            // composited in its own pass (P2/P3 sample a backdrop snapshot of the
            // persistent texture). MSAA is preserved across segments via
            // LoadOp::Load + StoreOp::Store. With NO effects this reduces to exactly
            // one pass with StoreOp::Discard — bit-identical to the pre-segmentation
            // path (zero regression on the common case).
            {
                let Some(pv) = self.persistent_view.as_ref() else {
                    push_error(UiError::GpuRender("missing persistent view".into()));
                    return;
                };
                let Some(mv) = self.msaa_view.as_ref() else {
                    push_error(UiError::GpuRender("missing msaa view".into()));
                    return;
                };
                // Effect compositing resources (only needed on the segmented path).
                let snapshot_bg = self.snapshot_bind_group.as_ref();
                let snapshot_tex = self.snapshot_tex.as_ref();
                let snapshot_view = self.snapshot_view.as_ref();
                let persistent_tex = self.persistent_tex.as_ref();
                let effect_h_view = self.effect_h_view.as_ref();
                let effect_v_view = self.effect_v_view.as_ref();
                let effect_v_bg = self.effect_v_bind_group.as_ref();
                if !rect_verts.is_empty()
                    || !grad_verts.is_empty()
                    || !image_verts.is_empty()
                    || !path_verts.is_empty()
                    || !text_areas.is_empty()
                    || !effect_verts.is_empty()
                    || !backdrop_verts.is_empty()
                {
                    self.persistent_dirty = true;
                }
                let has_effects = !effect_ops.is_empty() || !backdrop_ops.is_empty();
                let first_load = if self.persistent_dirty {
                    self.persistent_dirty = false;
                    let c = self.clear_color.to_linear();
                    wgpu::LoadOp::Clear(wgpu::Color {
                        r: c.r as f64,
                        g: c.g as f64,
                        b: c.b as f64,
                        a: c.a as f64,
                    })
                } else {
                    wgpu::LoadOp::Load
                };
                let scr_w = self.screen_size.width as u32;
                let scr_h = self.screen_size.height as u32;

                // Draws one z-layer's surfaces (rect/grad/image/path/text) into the
                // given pass. Kept as a macro (not a method) so disjoint-field
                // borrows survive alongside the &mut self.glyphon text call.
                macro_rules! draw_surface_layer {
                    ($pass:expr, $layer:expr, $texts:expr) => {{
                        let layer = $layer;
                        let texts: &[TextAreaDesc] = $texts;
                        if layer.ric > 0 {
                            $pass.set_pipeline(&self.rect_pipeline);
                            $pass.set_vertex_buffer(0, rect_vbuf.unwrap().slice(..));
                            $pass.set_index_buffer(
                                rect_ibuf_ref.unwrap().slice(..),
                                wgpu::IndexFormat::Uint32,
                            );
                            $pass.set_scissor_rect(0, 0, scr_w, scr_h);
                            $pass.draw_indexed(layer.ris..layer.ris + layer.ric, 0, 0..1);
                        }
                        if let Some(vbuf) = grad_vbuf_ref {
                            if layer.gic > 0 {
                                $pass.set_pipeline(&self.gradient_pipeline);
                                $pass.set_vertex_buffer(0, vbuf.slice(..));
                                $pass.set_index_buffer(
                                    grad_ibuf_ref.unwrap().slice(..),
                                    wgpu::IndexFormat::Uint32,
                                );
                                $pass.set_scissor_rect(0, 0, scr_w, scr_h);
                                $pass.draw_indexed(layer.gis..layer.gis + layer.gic, 0, 0..1);
                            }
                        }
                        if let Some(vbuf) = image_vbuf_ref {
                            if layer.iic > 0 {
                                $pass.set_pipeline(&self.text_pipeline);
                                $pass.set_vertex_buffer(0, vbuf.slice(..));
                                $pass.set_index_buffer(
                                    image_ibuf_ref.unwrap().slice(..),
                                    wgpu::IndexFormat::Uint32,
                                );
                                let quads = layer.ic as usize / 4;
                                for q in 0..quads {
                                    let bg_idx = layer.ibo as usize + q;
                                    if bg_idx < image_bind_groups.len() {
                                        $pass.set_bind_group(
                                            0,
                                            Some(image_bind_groups[bg_idx]),
                                            &[],
                                        );
                                        let base = layer.iis + (q * 6) as u32;
                                        $pass.draw_indexed(base..base + 6, 0, 0..1);
                                    }
                                }
                            }
                        }
                        if let Some(vbuf) = path_vbuf_ref {
                            if layer.pic > 0 {
                                $pass.set_pipeline(&self.path_pipeline);
                                $pass.set_vertex_buffer(0, vbuf.slice(..));
                                $pass.set_index_buffer(
                                    path_ibuf_ref.unwrap().slice(..),
                                    wgpu::IndexFormat::Uint32,
                                );
                                $pass.set_scissor_rect(0, 0, scr_w, scr_h);
                                $pass.draw_indexed(layer.pis..layer.pis + layer.pic, 0, 0..1);
                            }
                        }
                        if !texts.is_empty() {
                            if layer.z == 0 {
                                if let Some(idx) =
                                    self.glyphon.prepare_layer(&self.device, &self.queue, texts)
                                {
                                    self.glyphon.render_layer(idx, &mut $pass);
                                }
                            } else {
                                let go = self.glyphon_overlay.get_or_insert_with(|| {
                                    GlyphonBridge::new(
                                        &self.device,
                                        &self.queue,
                                        cfg_fmt,
                                        self.screen_size.width as u32,
                                        self.screen_size.height as u32,
                                        wgpu::MultisampleState {
                                            count: self.sample_count,
                                            mask: !0,
                                            alpha_to_coverage_enabled: false,
                                        },
                                    )
                                });
                                if let Some(idx) =
                                    go.prepare_layer(&self.device, &self.queue, texts)
                                {
                                    go.render_layer(idx, &mut $pass);
                                }
                            }
                        }
                    }};
                }

                // Draws one effect (blend rect) into the given pass using the blend
                // pipeline, which samples the pre-copied backdrop snapshot bound at
                // group 0. Blend mode + snapshot UV are baked into the vertex data.
                // (P3 will extend this for backdrop blur.)
                macro_rules! draw_effect {
                    ($pass:expr, $op:expr, $snap_bg:expr) => {{
                        let op = $op;
                        if let (Some(vbuf), Some(ibuf)) = (effect_vbuf_ref, effect_ibuf_ref) {
                            $pass.set_pipeline(&self.blend_pipeline);
                            $pass.set_bind_group(0, $snap_bg, &[]);
                            $pass.set_vertex_buffer(0, vbuf.slice(..));
                            $pass.set_index_buffer(ibuf.slice(..), wgpu::IndexFormat::Uint32);
                            $pass.set_scissor_rect(0, 0, scr_w, scr_h);
                            $pass.draw_indexed(op.i_start..op.i_start + op.i_count, 0, 0..1);
                        }
                    }};
                }

                // Runs the two separable-Gaussian blur passes for one backdrop op:
                // snapshot → effect_h (horizontal) → effect_v (vertical). Assumes the
                // snapshot already holds the current backdrop. Leaves the result in
                // effect_v (sampled later by the composite pass via effect_v_bg).
                //
                // Uniform buffers + bind groups are persistent (B5) — created once,
                // re-written per region via queue.write_buffer. They reference the
                // persistent snapshot/effect views (invalidated only on resize).
                macro_rules! run_backdrop_blur {
                    ($radius:expr) => {{
                        let radius = ($radius).max(0.5);
                        let texel = [1.0 / cfg_w.max(1) as f32, 1.0 / cfg_h.max(1) as f32];
                        if let (Some(snap_view), Some(eh_view), Some(ev_view)) =
                            (snapshot_view, effect_h_view, effect_v_view)
                        {
                            if self.blur_h_res.is_none() {
                                let buf = self.device.create_buffer(&wgpu::BufferDescriptor {
                                    label: Some("blur uniform h"),
                                    size: std::mem::size_of::<BlurUniforms>() as u64,
                                    usage: wgpu::BufferUsages::UNIFORM
                                        | wgpu::BufferUsages::COPY_DST,
                                    mapped_at_creation: false,
                                });
                                let bg =
                                    self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                        label: Some("blur bg h"),
                                        layout: &self.blur_bind_group_layout,
                                        entries: &[
                                            wgpu::BindGroupEntry {
                                                binding: 0,
                                                resource: wgpu::BindingResource::TextureView(
                                                    snap_view,
                                                ),
                                            },
                                            wgpu::BindGroupEntry {
                                                binding: 1,
                                                resource: wgpu::BindingResource::Sampler(
                                                    &self.blend_sampler,
                                                ),
                                            },
                                            wgpu::BindGroupEntry {
                                                binding: 2,
                                                resource: buf.as_entire_binding(),
                                            },
                                        ],
                                    });
                                self.blur_h_res = Some((buf, bg));
                            }
                            if self.blur_v_res.is_none() {
                                let buf = self.device.create_buffer(&wgpu::BufferDescriptor {
                                    label: Some("blur uniform v"),
                                    size: std::mem::size_of::<BlurUniforms>() as u64,
                                    usage: wgpu::BufferUsages::UNIFORM
                                        | wgpu::BufferUsages::COPY_DST,
                                    mapped_at_creation: false,
                                });
                                let bg =
                                    self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                        label: Some("blur bg v"),
                                        layout: &self.blur_bind_group_layout,
                                        entries: &[
                                            wgpu::BindGroupEntry {
                                                binding: 0,
                                                resource: wgpu::BindingResource::TextureView(
                                                    eh_view,
                                                ),
                                            },
                                            wgpu::BindGroupEntry {
                                                binding: 1,
                                                resource: wgpu::BindingResource::Sampler(
                                                    &self.blend_sampler,
                                                ),
                                            },
                                            wgpu::BindGroupEntry {
                                                binding: 2,
                                                resource: buf.as_entire_binding(),
                                            },
                                        ],
                                    });
                                self.blur_v_res = Some((buf, bg));
                            }
                            // Horizontal: snapshot → effect_h
                            let u_h = BlurUniforms {
                                direction: [1.0, 0.0],
                                texel,
                                radius,
                                _pad: [0.0; 3],
                            };
                            let (ref buf_h, ref bg_h) = *self.blur_h_res.as_ref().unwrap();
                            self.queue
                                .write_buffer(buf_h, 0, bytemuck::cast_slice(&[u_h]));
                            {
                                let mut bp =
                                    frame
                                        .encoder
                                        .begin_render_pass(&wgpu::RenderPassDescriptor {
                                            label: Some("blur h"),
                                            color_attachments: &[Some(
                                                wgpu::RenderPassColorAttachment {
                                                    view: eh_view,
                                                    resolve_target: None,
                                                    ops: wgpu::Operations {
                                                        load: wgpu::LoadOp::Clear(
                                                            wgpu::Color::TRANSPARENT,
                                                        ),
                                                        store: wgpu::StoreOp::Store,
                                                    },
                                                    depth_slice: None,
                                                },
                                            )],
                                            depth_stencil_attachment: None,
                                            timestamp_writes: None,
                                            occlusion_query_set: None,
                                            multiview_mask: None,
                                        });
                                bp.set_pipeline(&self.blur_pipeline);
                                bp.set_bind_group(0, bg_h, &[]);
                                bp.set_vertex_buffer(0, self.blit_vbuf.as_ref().unwrap().slice(..));
                                bp.set_index_buffer(
                                    self.blit_ibuf.as_ref().unwrap().slice(..),
                                    wgpu::IndexFormat::Uint32,
                                );
                                bp.draw_indexed(0..6, 0, 0..1);
                            }
                            // Vertical: effect_h → effect_v
                            let u_v = BlurUniforms {
                                direction: [0.0, 1.0],
                                texel,
                                radius,
                                _pad: [0.0; 3],
                            };
                            let (ref buf_v, ref bg_v) = *self.blur_v_res.as_ref().unwrap();
                            self.queue
                                .write_buffer(buf_v, 0, bytemuck::cast_slice(&[u_v]));
                            {
                                let mut bp =
                                    frame
                                        .encoder
                                        .begin_render_pass(&wgpu::RenderPassDescriptor {
                                            label: Some("blur v"),
                                            color_attachments: &[Some(
                                                wgpu::RenderPassColorAttachment {
                                                    view: ev_view,
                                                    resolve_target: None,
                                                    ops: wgpu::Operations {
                                                        load: wgpu::LoadOp::Clear(
                                                            wgpu::Color::TRANSPARENT,
                                                        ),
                                                        store: wgpu::StoreOp::Store,
                                                    },
                                                    depth_slice: None,
                                                },
                                            )],
                                            depth_stencil_attachment: None,
                                            timestamp_writes: None,
                                            occlusion_query_set: None,
                                            multiview_mask: None,
                                        });
                                bp.set_pipeline(&self.blur_pipeline);
                                bp.set_bind_group(0, bg_v, &[]);
                                bp.set_vertex_buffer(0, self.blit_vbuf.as_ref().unwrap().slice(..));
                                bp.set_index_buffer(
                                    self.blit_ibuf.as_ref().unwrap().slice(..),
                                    wgpu::IndexFormat::Uint32,
                                );
                                bp.draw_indexed(0..6, 0, 0..1);
                            }
                        }
                    }};
                }

                // Draws one backdrop composite quad into the current MSAA pass,
                // sampling the blurred result (effect_v) with a rounded mask + tint.
                macro_rules! draw_backdrop {
                    ($pass:expr, $op:expr, $ev_bg:expr) => {{
                        let op = $op;
                        if let (Some(vbuf), Some(ibuf)) = (backdrop_vbuf_ref, backdrop_ibuf_ref) {
                            $pass.set_pipeline(&self.composite_pipeline);
                            $pass.set_bind_group(0, $ev_bg, &[]);
                            $pass.set_vertex_buffer(0, vbuf.slice(..));
                            $pass.set_index_buffer(ibuf.slice(..), wgpu::IndexFormat::Uint32);
                            $pass.set_scissor_rect(0, 0, scr_w, scr_h);
                            $pass.draw_indexed(op.i_start..op.i_start + op.i_count, 0, 0..1);
                        }
                    }};
                }

                if !has_effects {
                    // ── Fast path: single pass (StoreOp::Discard) ──
                    let mut pass = frame
                        .encoder
                        .begin_render_pass(&wgpu::RenderPassDescriptor {
                            label: Some("persistent"),
                            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                                view: mv,
                                resolve_target: Some(pv),
                                ops: wgpu::Operations {
                                    load: first_load,
                                    store: wgpu::StoreOp::Discard,
                                },
                                depth_slice: None,
                            })],
                            depth_stencil_attachment: None,
                            timestamp_writes: None,
                            occlusion_query_set: None,
                            multiview_mask: None,
                        });
                    let mut text_cursor = 0usize;
                    for layer in &z_layers {
                        let count = text_by_z[text_cursor..]
                            .iter()
                            .take_while(|t| t.z_index == layer.z)
                            .count();
                        let texts = &text_by_z[text_cursor..text_cursor + count];
                        text_cursor += count;
                        draw_surface_layer!(pass, layer, texts);
                    }
                } else {
                    // ── Segmented path: interleave surface layers, blend effects,
                    // and backdrop-blur composites by z. Surfaces at z <= boundary
                    // composite first; then effects/backdrops at that z composite over
                    // them. MSAA persists across passes (Load + Store).
                    let mut effect_i = 0usize;
                    let mut backdrop_i = 0usize;
                    let mut layer_i = 0usize;
                    let mut text_cursor = 0usize;
                    let mut first = true;
                    let snapshot_copy = |enc: &mut wgpu::CommandEncoder, cfg_w: u32, cfg_h: u32| {
                        if let (Some(src), Some(dst)) = (persistent_tex, snapshot_tex) {
                            enc.copy_texture_to_texture(
                                wgpu::TexelCopyTextureInfo {
                                    texture: src,
                                    mip_level: 0,
                                    origin: wgpu::Origin3d::ZERO,
                                    aspect: wgpu::TextureAspect::All,
                                },
                                wgpu::TexelCopyTextureInfo {
                                    texture: dst,
                                    mip_level: 0,
                                    origin: wgpu::Origin3d::ZERO,
                                    aspect: wgpu::TextureAspect::All,
                                },
                                wgpu::Extent3d {
                                    width: cfg_w,
                                    height: cfg_h,
                                    depth_or_array_layers: 1,
                                },
                            );
                        }
                    };
                    loop {
                        let next_e = effect_ops.get(effect_i).map(|e| e.z);
                        let next_b = backdrop_ops.get(backdrop_i).map(|b| b.z);
                        let boundary_z = match (next_e, next_b) {
                            (Some(a), Some(b)) => Some(a.min(b)),
                            (Some(a), None) => Some(a),
                            (None, Some(b)) => Some(b),
                            (None, None) => None,
                        };
                        if boundary_z.is_none() && layer_i >= z_layers.len() {
                            break;
                        }

                        // Segment: layers with z <= boundary_z (or all remaining).
                        let seg_start = layer_i;
                        while layer_i < z_layers.len() {
                            match boundary_z {
                                Some(bz) if z_layers[layer_i].z > bz => break,
                                _ => layer_i += 1,
                            }
                        }
                        if layer_i > seg_start {
                            let load = if first {
                                first_load
                            } else {
                                wgpu::LoadOp::Load
                            };
                            first = false;
                            let mut pass =
                                frame
                                    .encoder
                                    .begin_render_pass(&wgpu::RenderPassDescriptor {
                                        label: Some("persistent-seg"),
                                        color_attachments: &[Some(
                                            wgpu::RenderPassColorAttachment {
                                                view: mv,
                                                resolve_target: Some(pv),
                                                ops: wgpu::Operations {
                                                    load,
                                                    store: wgpu::StoreOp::Store,
                                                },
                                                depth_slice: None,
                                            },
                                        )],
                                        depth_stencil_attachment: None,
                                        timestamp_writes: None,
                                        occlusion_query_set: None,
                                        multiview_mask: None,
                                    });
                            for li in seg_start..layer_i {
                                let layer = &z_layers[li];
                                let count = text_by_z[text_cursor..]
                                    .iter()
                                    .take_while(|t| t.z_index == layer.z)
                                    .count();
                                let texts = &text_by_z[text_cursor..text_cursor + count];
                                text_cursor += count;
                                draw_surface_layer!(pass, layer, texts);
                            }
                        }

                        let bz = match boundary_z {
                            Some(z) => z,
                            None => break,
                        };

                        // ── Backdrop composites at bz (blur first, then composite) ──
                        while backdrop_i < backdrop_ops.len() && backdrop_ops[backdrop_i].z == bz {
                            let radius = backdrop_ops[backdrop_i].blur_radius;
                            // Snapshot current backdrop, blur it (separate passes), then
                            // composite the rounded region into the MSAA target.
                            snapshot_copy(&mut frame.encoder, cfg_w, cfg_h);
                            run_backdrop_blur!(radius);
                            if let Some(ev_bg) = effect_v_bg {
                                let load = if first {
                                    first_load
                                } else {
                                    wgpu::LoadOp::Load
                                };
                                first = false;
                                let mut pass =
                                    frame
                                        .encoder
                                        .begin_render_pass(&wgpu::RenderPassDescriptor {
                                            label: Some("backdrop-composite"),
                                            color_attachments: &[Some(
                                                wgpu::RenderPassColorAttachment {
                                                    view: mv,
                                                    resolve_target: Some(pv),
                                                    ops: wgpu::Operations {
                                                        load,
                                                        store: wgpu::StoreOp::Store,
                                                    },
                                                    depth_slice: None,
                                                },
                                            )],
                                            depth_stencil_attachment: None,
                                            timestamp_writes: None,
                                            occlusion_query_set: None,
                                            multiview_mask: None,
                                        });
                                draw_backdrop!(pass, &backdrop_ops[backdrop_i], ev_bg);
                            }
                            backdrop_i += 1;
                        }

                        // ── Blend effects at bz ──
                        if effect_i < effect_ops.len() && effect_ops[effect_i].z == bz {
                            snapshot_copy(&mut frame.encoder, cfg_w, cfg_h);
                            let load = if first {
                                first_load
                            } else {
                                wgpu::LoadOp::Load
                            };
                            first = false;
                            let mut pass =
                                frame
                                    .encoder
                                    .begin_render_pass(&wgpu::RenderPassDescriptor {
                                        label: Some("effect-seg"),
                                        color_attachments: &[Some(
                                            wgpu::RenderPassColorAttachment {
                                                view: mv,
                                                resolve_target: Some(pv),
                                                ops: wgpu::Operations {
                                                    load,
                                                    store: wgpu::StoreOp::Store,
                                                },
                                                depth_slice: None,
                                            },
                                        )],
                                        depth_stencil_attachment: None,
                                        timestamp_writes: None,
                                        occlusion_query_set: None,
                                        multiview_mask: None,
                                    });
                            if let Some(snap_bg) = snapshot_bg {
                                while effect_i < effect_ops.len() && effect_ops[effect_i].z == bz {
                                    draw_effect!(pass, &effect_ops[effect_i], snap_bg);
                                    effect_i += 1;
                                }
                            } else {
                                while effect_i < effect_ops.len() && effect_ops[effect_i].z == bz {
                                    effect_i += 1;
                                }
                            }
                        }
                    }
                }
            }
        }

        for &hash in &accessed_image_hashes {
            if let Some(entry) = self.image_cache.get_mut(&hash) {
                entry.5 = self.image_cache_frame;
            }
        }

        // ── Pass 2: Blit persistent texture to swapchain ──
        let Some(persistent_bg) = self.persistent_bind_group.as_ref() else {
            push_error(UiError::GpuRender("missing persistent bind group".into()));
            return;
        };
        let Some(blit_vbuf) = self.blit_vbuf.as_ref() else {
            push_error(UiError::GpuRender("missing blit vertex buffer".into()));
            return;
        };
        let Some(blit_ibuf) = self.blit_ibuf.as_ref() else {
            push_error(UiError::GpuRender("missing blit index buffer".into()));
            return;
        };
        {
            let c = self.clear_color.to_linear();
            let mut pass = frame
                .encoder
                .begin_render_pass(&wgpu::RenderPassDescriptor {
                    label: Some("blit"),
                    color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                        view: &frame.view,
                        resolve_target: None,
                        ops: wgpu::Operations {
                            load: wgpu::LoadOp::Clear(wgpu::Color {
                                r: c.r as f64,
                                g: c.g as f64,
                                b: c.b as f64,
                                a: c.a as f64,
                            }),
                            store: wgpu::StoreOp::Store,
                        },
                        depth_slice: None,
                    })],
                    depth_stencil_attachment: None,
                    timestamp_writes: None,
                    occlusion_query_set: None,
                    multiview_mask: None,
                });

            pass.set_pipeline(&self.blit_pipeline);
            pass.set_bind_group(0, persistent_bg, &[]);
            pass.set_vertex_buffer(0, blit_vbuf.slice(..));
            pass.set_index_buffer(blit_ibuf.slice(..), wgpu::IndexFormat::Uint32);
            pass.draw_indexed(0..6, 0, 0..1);
        }
    }

    pub fn end_frame(&mut self, frame: Frame) {
        self.belt.finish();
        self.queue.submit([frame.encoder.finish()]);
        self.belt.recall();
        self.queue.present(frame.texture);
        // Trim path cache (LRU)
        self.path_cache_frame += 1;
        // Trim stale entries every 30 frames
        if self.path_cache_frame.is_multiple_of(30) {
            let threshold = self.path_cache_frame.saturating_sub(60);
            self.path_cache.retain(|_, v| v.last_frame >= threshold);
        }
        // Hard limit to prevent unbounded growth
        const MAX_PATH_CACHE: usize = 256;
        if self.path_cache.len() > MAX_PATH_CACHE {
            let threshold = self.path_cache_frame.saturating_sub(30);
            self.path_cache.retain(|_, v| v.last_frame >= threshold);
        }
        self.glyphon.trim();
        if let Some(ref mut go) = self.glyphon_overlay {
            go.trim();
        }
    }

    pub fn resize(&mut self, w: u32, h: u32) {
        if w > 0 && h > 0 {
            if let (Some(surface), Some(config)) = (self.surface.as_ref(), self.config.as_mut()) {
                config.width = w;
                config.height = h;
                self.screen_size = Size::new(w as f32, h as f32);
                surface.configure(&self.device, config);
                self.glyphon.resize(&self.queue, w, h);
                if let Some(ref mut go) = self.glyphon_overlay {
                    go.resize(&self.queue, w, h);
                }
            }
            self.persistent_tex = None;
            self.persistent_view = None;
            self.persistent_bind_group = None;
            self.msaa_tex = None;
            self.msaa_view = None;
        }
    }

    pub fn set_scale_factor(&mut self, sf: f64) {
        self.scale_factor = sf as f32;
    }

    pub fn create_surface(
        &mut self,
        window: &Arc<dyn winit::window::Window>,
    ) -> Result<(), RenderError> {
        let size = window.surface_size();
        let surface = self
            .instance
            .create_surface(Arc::clone(window))
            .map_err(|e| {
                push_error(UiError::GpuInit(GpuErrorKind::Other(e.to_string())));
                RenderError::Surface
            })?;
        let config = match surface.get_default_config(&self.adapter, size.width, size.height) {
            Some(c) => c,
            None => {
                push_error(UiError::GpuInit(GpuErrorKind::Surface));
                return Err(RenderError::Surface);
            }
        };
        surface.configure(&self.device, &config);
        self.screen_size = Size::new(size.width as f32, size.height as f32);
        self.surface = Some(surface);
        self.config = Some(config);
        self.persistent_dirty = true;
        Ok(())
    }

    pub fn destroy_surface(&mut self) {
        self.surface = None;
        self.config = None;
        self.persistent_tex = None;
        self.persistent_view = None;
        self.persistent_bind_group = None;
        self.msaa_tex = None;
        self.msaa_view = None;
        self.snapshot_tex = None;
        self.snapshot_view = None;
        self.snapshot_bind_group = None;
        self.effect_h_tex = None;
        self.effect_h_view = None;
        self.effect_v_tex = None;
        self.effect_v_view = None;
        self.effect_v_bind_group = None;
        self.blur_h_res = None;
        self.blur_v_res = None;
        self.glyphon.invalidate_cache();
        if let Some(ref mut go) = self.glyphon_overlay {
            go.invalidate_cache();
        }
    }

    fn ensure_buffer<'a>(
        buf: &'a mut Option<(wgpu::Buffer, u64)>,
        required: u64,
        usage: wgpu::BufferUsages,
        device: &wgpu::Device,
    ) -> &'a wgpu::Buffer {
        let cap = buf.as_ref().map_or(0, |(_, c)| *c);
        if cap >= required {
            return &buf.as_ref().unwrap().0;
        }
        let new_size = required.next_power_of_two().max(256);
        let new = device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: new_size,
            usage: usage | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        *buf = Some((new, new_size));
        &buf.as_ref().unwrap().0
    }
}

fn brush_color_at_pos(brush: &Brush, x: f64, y: f64, gs: (f64, f64), ge: (f64, f64)) -> [f32; 4] {
    match brush {
        Brush::Solid(c) => c.to_linear_array(),
        Brush::Gradient(g) => {
            let n = g.stop_count.min(4) as usize;
            if n < 2 {
                return [1.0, 1.0, 1.0, 1.0];
            }
            let dx = ge.0 - gs.0;
            let dy = ge.1 - gs.1;
            let len_sq = dx * dx + dy * dy;
            if len_sq < 0.0001 {
                return g.stops[0].color.to_linear_array();
            }
            let t = ((x - gs.0) * dx + (y - gs.1) * dy) / len_sq;
            let t = t.clamp(0.0, 1.0) as f32;
            for i in 0..n - 1 {
                let o0 = g.stops[i].offset;
                let o1 = g.stops[i + 1].offset;
                if t >= o0 && t <= o1 {
                    let local_t = if o1 > o0 { (t - o0) / (o1 - o0) } else { 0.0 };
                    return g.stops[i]
                        .color
                        .lerp(&g.stops[i + 1].color, local_t)
                        .to_linear_array();
                }
            }
            g.stops[n - 1].color.to_linear_array()
        }
    }
}

fn hash_brush(brush: &Brush) -> u64 {
    use std::hash::{Hash, Hasher};
    let mut h = rustc_hash::FxHasher::default();
    match brush {
        Brush::Solid(c) => {
            0u64.hash(&mut h);
            c.r.to_bits().hash(&mut h);
            c.g.to_bits().hash(&mut h);
            c.b.to_bits().hash(&mut h);
            c.a.to_bits().hash(&mut h);
        }
        Brush::Gradient(g) => {
            1u64.hash(&mut h);
            g.start.0.to_bits().hash(&mut h);
            g.start.1.to_bits().hash(&mut h);
            g.end.0.to_bits().hash(&mut h);
            g.end.1.to_bits().hash(&mut h);
        }
    }
    h.finish()
}

fn hash_stroke(stroke: &kurbo::Stroke) -> u64 {
    use std::hash::{Hash, Hasher};
    let mut h = rustc_hash::FxHasher::default();
    stroke.width.to_bits().hash(&mut h);
    (match stroke.start_cap {
        kurbo::Cap::Butt => 0u64,
        kurbo::Cap::Round => 1u64,
        kurbo::Cap::Square => 2u64,
    })
    .hash(&mut h);
    (match stroke.join {
        kurbo::Join::Bevel => 0u64,
        kurbo::Join::Round => 1u64,
        kurbo::Join::Miter => 2u64,
    })
    .hash(&mut h);
    h.finish()
}

/// Hash only the LINEAR part (scale/rotate/shear) of a transform.
///
/// Path-cache keys deliberately exclude the translation (audit 2026-07-17
/// round 5, B1): tessellated geometry is translation-invariant, and keying
/// on the full transform meant every scrolled icon re-tessellated every
/// frame (measured 2–10 µs per icon per miss). The translation is applied
/// at vertex-fill time instead.
fn hash_linear_transform(xf: glam::Affine2) -> u64 {
    use std::hash::{Hash, Hasher};
    let mut h = rustc_hash::FxHasher::default();
    xf.matrix2.x_axis.x.to_bits().hash(&mut h);
    xf.matrix2.x_axis.y.to_bits().hash(&mut h);
    xf.matrix2.y_axis.x.to_bits().hash(&mut h);
    xf.matrix2.y_axis.y.to_bits().hash(&mut h);
    h.finish()
}

fn glam_to_kurbo_affine(xform: glam::Affine2) -> kurbo::Affine {
    kurbo::Affine::new([
        xform.matrix2.x_axis.x as f64,
        xform.matrix2.x_axis.y as f64,
        xform.matrix2.y_axis.x as f64,
        xform.matrix2.y_axis.y as f64,
        xform.translation.x as f64,
        xform.translation.y as f64,
    ])
}

#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug)]
struct PathCacheKey {
    path_hash: u64,
    brush_hash: u64,
    stroke_hash: u64,
    xform_hash: u64,
}

/// Tessellated path geometry in *untranslated* logical space: the linear
/// part of the draw transform is baked in, the translation is NOT (B1).
struct CachedPathMesh {
    logical_vertices: Vec<[f32; 2]>,
    indices: Vec<u32>,
    last_frame: u64,
}

pub struct Frame {
    pub texture: wgpu::SurfaceTexture,
    pub view: wgpu::TextureView,
    pub encoder: wgpu::CommandEncoder,
}

#[derive(Debug)]
pub enum RenderError {
    NoAdapter,
    Surface,
    Device,
}
impl std::fmt::Display for RenderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "render error")
    }
}
impl std::error::Error for RenderError {}

fn rect_to_vertices(
    rect: Rect,
    screen: Size,
    color: Color,
    radii: crate::style::CornerRadii,
    stroke_width: f32,
    xform: Affine2,
    clip: ClipInfo,
    shadow_offset: [f32; 2],
    shadow_color: [f32; 4],
    shadow_blur: f32,
    is_shadow: f32,
) -> [RectVertex; 4] {
    let sx = 2.0 / screen.width;
    let sy = -2.0 / screen.height;
    // Linearize for the sRGB render target (see Color::to_linear). Text (glyphon
    // ColorMode::Accurate) and images (sRGB texture auto-decode) handle this
    // themselves, so only geometry shaders linearize here.
    let c = color.to_linear_array();
    let rs = [
        radii.top_left,
        radii.top_right,
        radii.bottom_right,
        radii.bottom_left,
    ];
    let so = clip.scroll_offset;
    let cr = [
        clip.rect.x + so[0],
        clip.rect.y + so[1],
        clip.rect.x + clip.rect.width + so[0],
        clip.rect.y + clip.rect.height + so[1],
    ];
    let cra = [
        clip.radius.top_left,
        clip.radius.top_right,
        clip.radius.bottom_right,
        clip.radius.bottom_left,
    ];

    let raw00 = glam::Vec2::new(rect.x, rect.y);
    let raw10 = glam::Vec2::new(rect.x + rect.width, rect.y);
    let raw11 = glam::Vec2::new(rect.x + rect.width, rect.y + rect.height);
    let raw01 = glam::Vec2::new(rect.x, rect.y + rect.height);
    let tp00 = xform.transform_point2(raw00);
    let tp10 = xform.transform_point2(raw10);
    let tp11 = xform.transform_point2(raw11);
    let tp01 = xform.transform_point2(raw01);

    // Shadow uses document-space world_pos + center (same space as normal rects
    // and as clip_rect), so clip_alpha compares consistently. Position NDC still
    // uses the transformed corners (tp00..) below. Using transformed world_pos
    // here would break clipping under scroll (clip_rect is document space).
    let (wp00, wp10, wp11, wp01): ([f32; 2], [f32; 2], [f32; 2], [f32; 2]) = (
        [rect.x, rect.y],
        [rect.x + rect.width, rect.y],
        [rect.x + rect.width, rect.y + rect.height],
        [rect.x, rect.y + rect.height],
    );

    let (so_val, sz, rs_val): ([f32; 2], [f32; 2], [f32; 4]) = if is_shadow > 0.0 {
        let center = [rect.x + rect.width * 0.5, rect.y + rect.height * 0.5];
        (center, [rect.width, rect.height], rs)
    } else {
        (shadow_offset, [rect.width, rect.height], rs)
    };

    [
        RectVertex {
            position: [tp00.x * sx - 1.0, tp00.y * sy + 1.0],
            color: c,
            size: sz,
            radii: rs_val,
            local: [0.0, 0.0],
            stroke_width,
            world_pos: wp00,
            shadow_offset: so_val,
            shadow_color,
            shadow_blur_radius: shadow_blur,
            is_shadow,
            clip_rect: cr,
            clip_radius: cra,
        },
        RectVertex {
            position: [tp10.x * sx - 1.0, tp10.y * sy + 1.0],
            color: c,
            size: sz,
            radii: rs_val,
            local: [rect.width, 0.0],
            stroke_width,
            world_pos: wp10,
            shadow_offset: so_val,
            shadow_color,
            shadow_blur_radius: shadow_blur,
            is_shadow,
            clip_rect: cr,
            clip_radius: cra,
        },
        RectVertex {
            position: [tp11.x * sx - 1.0, tp11.y * sy + 1.0],
            color: c,
            size: sz,
            radii: rs_val,
            local: [rect.width, rect.height],
            stroke_width,
            world_pos: wp11,
            shadow_offset: so_val,
            shadow_color,
            shadow_blur_radius: shadow_blur,
            is_shadow,
            clip_rect: cr,
            clip_radius: cra,
        },
        RectVertex {
            position: [tp01.x * sx - 1.0, tp01.y * sy + 1.0],
            color: c,
            size: sz,
            radii: rs_val,
            local: [0.0, rect.height],
            stroke_width,
            world_pos: wp01,
            shadow_offset: so_val,
            shadow_color,
            shadow_blur_radius: shadow_blur,
            is_shadow,
            clip_rect: cr,
            clip_radius: cra,
        },
    ]
}

fn gradient_to_vertices(
    rect: Rect,
    screen: Size,
    gradient: crate::style::LinearGradient,
    radii: crate::style::CornerRadii,
    stroke_width: f32,
    xform: Affine2,
    clip: ClipInfo,
) -> [GradientVertex; 4] {
    let sx = 2.0 / screen.width;
    let sy = -2.0 / screen.height;
    let p00 = xform.transform_point2(glam::Vec2::new(rect.x, rect.y));
    let p10 = xform.transform_point2(glam::Vec2::new(rect.x + rect.width, rect.y));
    let p11 = xform.transform_point2(glam::Vec2::new(rect.x + rect.width, rect.y + rect.height));
    let p01 = xform.transform_point2(glam::Vec2::new(rect.x, rect.y + rect.height));
    let s = [rect.width, rect.height];
    let so = clip.scroll_offset;
    let cr = [
        clip.rect.x + so[0],
        clip.rect.y + so[1],
        clip.rect.x + clip.rect.width + so[0],
        clip.rect.y + clip.rect.height + so[1],
    ];
    let cra = [
        clip.radius.top_left,
        clip.radius.top_right,
        clip.radius.bottom_right,
        clip.radius.bottom_left,
    ];
    let n = gradient.stop_count.min(4) as usize;
    let c0 = gradient
        .stops
        .first()
        .map_or([0.0; 4], |s| s.color.to_linear_array());
    let c1 = gradient
        .stops
        .get(1)
        .map_or(c0, |s| s.color.to_linear_array());
    let c2 = gradient
        .stops
        .get(2)
        .map_or(c1, |s| s.color.to_linear_array());
    let c3 = gradient
        .stops
        .get(3)
        .map_or(c2, |s| s.color.to_linear_array());
    let off1 = gradient.stops.get(1).map_or(0.0, |s| s.offset);
    let off2 = gradient.stops.get(2).map_or(1.0, |s| s.offset);
    let off3 = gradient.stops.get(3).map_or(1.0, |s| s.offset);
    let count_offsets: [f32; 4] = [n as f32, off1, off2, off3];
    // Endpoint semantics depend on kind (all mapped to document space here);
    // the shader's gradient_t interprets them per-kind. Kind is baked into the
    // (otherwise unused) shadow_offset.x of each vertex.
    let dir_start: [f32; 2] = [
        rect.x + gradient.start.0 * rect.width,
        rect.y + gradient.start.1 * rect.height,
    ];
    let dir_end: [f32; 2] = [
        rect.x + gradient.end.0 * rect.width,
        rect.y + gradient.end.1 * rect.height,
    ];
    let kind_f = match gradient.kind {
        crate::style::GradientKind::Linear => 0.0,
        crate::style::GradientKind::Radial => 1.0,
        crate::style::GradientKind::Conic => 2.0,
    };
    GradientVertex::gen_quad(
        [p00, p10, p11, p01],
        sx,
        sy,
        s,
        radii,
        stroke_width,
        cr,
        cra,
        c0,
        c1,
        c2,
        c3,
        count_offsets,
        dir_start,
        dir_end,
        rect,
        kind_f,
    )
}

impl GradientVertex {
    #[allow(clippy::too_many_arguments)]
    fn gen_quad(
        corners: [glam::Vec2; 4],
        sx: f32,
        sy: f32,
        size: [f32; 2],
        radii: crate::style::CornerRadii,
        stroke_width: f32,
        clip_rect: [f32; 4],
        clip_radius: [f32; 4],
        c0: [f32; 4],
        c1: [f32; 4],
        c2: [f32; 4],
        c3: [f32; 4],
        count_offsets: [f32; 4],
        dir_start: [f32; 2],
        dir_end: [f32; 2],
        rect: Rect,
        kind: f32,
    ) -> [GradientVertex; 4] {
        let rs = [
            radii.top_left,
            radii.top_right,
            radii.bottom_right,
            radii.bottom_left,
        ];
        let locals: [[f32; 2]; 4] = [
            [0.0, 0.0],
            [rect.width, 0.0],
            [rect.width, rect.height],
            [0.0, rect.height],
        ];
        let world_ps: [[f32; 2]; 4] = [
            [rect.x, rect.y],
            [rect.x + rect.width, rect.y],
            [rect.x + rect.width, rect.y + rect.height],
            [rect.x, rect.y + rect.height],
        ];
        // shadow_offset is unused by the gradient shader for shadows; we bake the
        // gradient kind into .x (0=Linear, 1=Radial, 2=Conic).
        let kv: [f32; 2] = [kind, 0.0];
        [
            GradientVertex {
                position: [corners[0].x * sx - 1.0, corners[0].y * sy + 1.0],
                size,
                radii: rs,
                local: locals[0],
                stroke_width,
                world_pos: world_ps[0],
                shadow_offset: kv,
                clip_rect,
                clip_radius,
                color_0: c0,
                color_1: c1,
                color_2: c2,
                color_3: c3,
                count_offsets,
                dir_start,
                dir_end,
            },
            GradientVertex {
                position: [corners[1].x * sx - 1.0, corners[1].y * sy + 1.0],
                size,
                radii: rs,
                local: locals[1],
                stroke_width,
                world_pos: world_ps[1],
                shadow_offset: kv,
                clip_rect,
                clip_radius,
                color_0: c0,
                color_1: c1,
                color_2: c2,
                color_3: c3,
                count_offsets,
                dir_start,
                dir_end,
            },
            GradientVertex {
                position: [corners[2].x * sx - 1.0, corners[2].y * sy + 1.0],
                size,
                radii: rs,
                local: locals[2],
                stroke_width,
                world_pos: world_ps[2],
                shadow_offset: kv,
                clip_rect,
                clip_radius,
                color_0: c0,
                color_1: c1,
                color_2: c2,
                color_3: c3,
                count_offsets,
                dir_start,
                dir_end,
            },
            GradientVertex {
                position: [corners[3].x * sx - 1.0, corners[3].y * sy + 1.0],
                size,
                radii: rs,
                local: locals[3],
                stroke_width,
                world_pos: world_ps[3],
                shadow_offset: kv,
                clip_rect,
                clip_radius,
                color_0: c0,
                color_1: c1,
                color_2: c2,
                color_3: c3,
                count_offsets,
                dir_start,
                dir_end,
            },
        ]
    }
}

/// Image quad with rounded-rect clip support.
fn image_quad_vertices(
    x: f32,
    y: f32,
    w: f32,
    h: f32,
    screen: Size,
    clip_rect: [f32; 4],
    clip_radius: [f32; 4],
    opacity: f32,
) -> [TextVertex; 4] {
    let sx = 2.0 / screen.width;
    let sy = -2.0 / screen.height;
    [
        TextVertex {
            position: [x * sx - 1.0, y * sy + 1.0],
            uv: [0.0, 0.0],
            clip_rect,
            clip_radius,
            world_pos: [x, y],
            opacity,
        },
        TextVertex {
            position: [(x + w) * sx - 1.0, y * sy + 1.0],
            uv: [1.0, 0.0],
            clip_rect,
            clip_radius,
            world_pos: [x + w, y],
            opacity,
        },
        TextVertex {
            position: [(x + w) * sx - 1.0, (y + h) * sy + 1.0],
            uv: [1.0, 1.0],
            clip_rect,
            clip_radius,
            world_pos: [x + w, y + h],
            opacity,
        },
        TextVertex {
            position: [x * sx - 1.0, (y + h) * sy + 1.0],
            uv: [0.0, 1.0],
            clip_rect,
            clip_radius,
            world_pos: [x, y + h],
            opacity,
        },
    ]
}

fn clip_to_scissor(clip: ClipInfo, screen: Size, scale_factor: f32) -> (u32, u32, u32, u32) {
    let sf = scale_factor;
    let c = clip.rect;
    let x = (c.x * sf).round().max(0.0) as u32;
    let y = (c.y * sf).round().max(0.0) as u32;
    let r = ((c.x + c.width) * sf).round().min(screen.width * sf) as u32;
    let b = ((c.y + c.height) * sf).round().min(screen.height * sf) as u32;
    (x.min(r), y.min(b), r.saturating_sub(x), b.saturating_sub(y))
}

fn content_fit_rect(
    fit: crate::widgets::display::ContentFit,
    dest: crate::style::Rect,
    img_w: f32,
    img_h: f32,
) -> crate::style::Rect {
    if img_w <= 0.0 || img_h <= 0.0 {
        return dest;
    }
    match fit {
        crate::widgets::display::ContentFit::Fill => dest,
        crate::widgets::display::ContentFit::Contain => {
            let scale = (dest.width / img_w).min(dest.height / img_h);
            let rw = img_w * scale;
            let rh = img_h * scale;
            crate::style::Rect::new(
                dest.x + (dest.width - rw) * 0.5,
                dest.y + (dest.height - rh) * 0.5,
                rw,
                rh,
            )
        }
        crate::widgets::display::ContentFit::Cover => {
            let scale = (dest.width / img_w).max(dest.height / img_h);
            let rw = img_w * scale;
            let rh = img_h * scale;
            crate::style::Rect::new(
                dest.x + (dest.width - rw) * 0.5,
                dest.y + (dest.height - rh) * 0.5,
                rw,
                rh,
            )
        }
        crate::widgets::display::ContentFit::None => crate::style::Rect::new(
            dest.x,
            dest.y,
            img_w.min(dest.width),
            img_h.min(dest.height),
        ),
        crate::widgets::display::ContentFit::ScaleDown => {
            let scale = (dest.width / img_w).min(dest.height / img_h).min(1.0);
            let rw = img_w * scale;
            let rh = img_h * scale;
            crate::style::Rect::new(
                dest.x + (dest.width - rw) * 0.5,
                dest.y + (dest.height - rh) * 0.5,
                rw,
                rh,
            )
        }
    }
}