euv-core 0.57.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
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
/// Wayland `wl_shm` 32-bit ARGB8888 pixel format code (little-endian `[B, G, R, A]`).
pub const WL_SHM_FORMAT_ARGB8888: u32 = 0;

/// Wayland `wl_shm` 32-bit XRGB8888 pixel format code (little-endian `[B, G, R, 0xFF]`).
pub const WL_SHM_FORMAT_XRGB8888: u32 = 1;

/// Shared library name for Linux X11 client runtime.
pub(crate) const LINUX_LIB_X11: &str = "libX11.so.6\0";

/// Shared library name for Linux Wayland client runtime.
pub(crate) const LINUX_LIB_WAYLAND: &str = "libwayland-client.so.0\0";

/// Shared library name for Linux XRandR extension runtime.
pub(crate) const LINUX_LIB_XRANDR: &str = "libXrandr.so.2\0";

/// Shared library name for Linux MIT-SHM Xext extension runtime.
pub(crate) const LINUX_LIB_XEXT: &str = "libXext.so.6\0";

/// Shared library name for Linux Vulkan loader (probed for `VK_KHR_wayland_surface` WSI).
pub(crate) const LINUX_LIB_VULKAN: &str = "libvulkan.so.1\0";

/// Environment variable detecting an active native Wayland compositor socket.
pub(crate) const LINUX_WAYLAND_DISPLAY_ENV: &str = "WAYLAND_DISPLAY";

/// Optional environment variable forcing legacy X11 over native Wayland when set to `"1"`.
pub(crate) const LINUX_FORCE_X11_ENV: &str = "EUV_LINUX_FORCE_X11";

/// Dynamic symbol for `XShmQueryExtension`.
pub(crate) const SYM_XSHM_QUERY_EXTENSION: &str = "XShmQueryExtension\0";

/// Dynamic symbol for `XShmCreateImage`.
pub(crate) const SYM_XSHM_CREATE_IMAGE: &str = "XShmCreateImage\0";

/// Dynamic symbol for `XShmAttach`.
pub(crate) const SYM_XSHM_ATTACH: &str = "XShmAttach\0";

/// Dynamic symbol for `XShmDetach`.
pub(crate) const SYM_XSHM_DETACH: &str = "XShmDetach\0";

/// Dynamic symbol for `XShmPutImage`.
pub(crate) const SYM_XSHM_PUT_IMAGE: &str = "XShmPutImage\0";

/// Dynamic symbol for `wl_display_dispatch_pending`.
pub(crate) const SYM_WL_DISPLAY_DISPATCH_PENDING: &str = "wl_display_dispatch_pending\0";

/// Dynamic symbol for `wl_display_dispatch`.
pub(crate) const SYM_WL_DISPLAY_DISPATCH: &str = "wl_display_dispatch\0";

/// Dynamic symbol for `wl_display_flush`.
pub(crate) const SYM_WL_DISPLAY_FLUSH: &str = "wl_display_flush\0";

/// Dynamic symbol for `wl_display_roundtrip`.
pub(crate) const SYM_WL_DISPLAY_ROUNDTRIP: &str = "wl_display_roundtrip\0";

/// Dynamic symbol for `wl_display_disconnect`.
pub(crate) const SYM_WL_DISPLAY_DISCONNECT: &str = "wl_display_disconnect\0";

/// Dynamic symbol for `vkCreateWaylandSurfaceKHR` in `libvulkan.so.1`.
pub(crate) const SYM_VK_CREATE_WAYLAND_SURFACE: &str = "vkCreateWaylandSurfaceKHR\0";

/// Dynamic symbol for `XRRGetScreenInfo`.
pub(crate) const SYM_XRR_GET_SCREEN_INFO: &str = "XRRGetScreenInfo\0";

/// Dynamic symbol for `XRRConfigCurrentRate`.
pub(crate) const SYM_XRR_CONFIG_CURRENT_RATE: &str = "XRRConfigCurrentRate\0";

/// Dynamic symbol for `XRRFreeScreenConfigInfo`.
pub(crate) const SYM_XRR_FREE_SCREEN_CONFIG_INFO: &str = "XRRFreeScreenConfigInfo\0";

/// Environment variable overriding detected display refresh rate in Hertz on Linux.
pub(crate) const LINUX_REFRESH_ENV_VAR: &str = "EUV_REFRESH_RATE_HZ";

/// Dynamic symbol for `XOpenDisplay`.
pub(crate) const SYM_X_OPEN_DISPLAY: &str = "XOpenDisplay\0";

/// Dynamic symbol for `XDefaultScreen`.
pub(crate) const SYM_X_DEFAULT_SCREEN: &str = "XDefaultScreen\0";

/// Dynamic symbol for `XRootWindow`.
pub(crate) const SYM_X_ROOT_WINDOW: &str = "XRootWindow\0";

/// Dynamic symbol for `XCreateSimpleWindow`.
pub(crate) const SYM_X_CREATE_SIMPLE_WINDOW: &str = "XCreateSimpleWindow\0";

/// Dynamic symbol for `XStoreName`.
pub(crate) const SYM_X_STORE_NAME: &str = "XStoreName\0";

/// Dynamic symbol for `XMapWindow`.
pub(crate) const SYM_X_MAP_WINDOW: &str = "XMapWindow\0";

/// Dynamic symbol for `XResizeWindow`.
pub(crate) const SYM_X_RESIZE_WINDOW: &str = "XResizeWindow\0";

/// Dynamic symbol for `XDestroyWindow`.
pub(crate) const SYM_X_DESTROY_WINDOW: &str = "XDestroyWindow\0";

/// Dynamic symbol for `XFlush`.
pub(crate) const SYM_X_FLUSH: &str = "XFlush\0";

/// Dynamic symbol for `XDefaultGC`.
pub(crate) const SYM_X_DEFAULT_GC: &str = "XDefaultGC\0";

/// Dynamic symbol for `XDefaultVisual`.
pub(crate) const SYM_X_DEFAULT_VISUAL: &str = "XDefaultVisual\0";

/// Dynamic symbol for `XDefaultDepth`.
pub(crate) const SYM_X_DEFAULT_DEPTH: &str = "XDefaultDepth\0";

/// Dynamic symbol for `XCreateImage`.
pub(crate) const SYM_X_CREATE_IMAGE: &str = "XCreateImage\0";

/// Dynamic symbol for `XPutImage`.
pub(crate) const SYM_X_PUT_IMAGE: &str = "XPutImage\0";

/// Dynamic symbol for `XFree`.
pub(crate) const SYM_X_FREE: &str = "XFree\0";

/// Dynamic symbol for `XSelectInput`.
pub(crate) const SYM_X_SELECT_INPUT: &str = "XSelectInput\0";

/// Dynamic symbol for `XInternAtom`.
pub(crate) const SYM_X_INTERN_ATOM: &str = "XInternAtom\0";

/// Dynamic symbol for `XSetWMProtocols`.
pub(crate) const SYM_X_SET_WM_PROTOCOLS: &str = "XSetWMProtocols\0";

/// Dynamic symbol for `XPending`.
pub(crate) const SYM_X_PENDING: &str = "XPending\0";

/// Dynamic symbol for `XNextEvent`.
pub(crate) const SYM_X_NEXT_EVENT: &str = "XNextEvent\0";

/// Dynamic symbol for `XLookupString`.
pub(crate) const SYM_X_LOOKUP_STRING: &str = "XLookupString\0";

/// Dynamic symbol for `XResourceManagerString`.
pub(crate) const SYM_X_RESOURCE_MANAGER_STRING: &str = "XResourceManagerString\0";

/// Dynamic symbol for `wl_display_connect`.
pub(crate) const SYM_WL_DISPLAY_CONNECT: &str = "wl_display_connect\0";

/// ICCCM atom name for window manager close requests.
pub(crate) const ATOM_WM_DELETE_WINDOW: &str = "WM_DELETE_WINDOW\0";

/// Environment variable names inspected for HiDPI scale overrides on Linux.
pub(crate) const LINUX_DPI_ENV_VARS: [&str; 3] =
    ["EUV_SCALE_FACTOR", "GDK_SCALE", "QT_SCALE_FACTOR"];

/// Xrdb resource key specifying logical DPI on Linux X11 desktops.
pub(crate) const XFT_DPI_PREFIX: &str = "Xft.dpi:";

/// Standard 96 DPI baseline for X11 scale conversion.
pub(crate) const X11_DEFAULT_DPI: f32 = 96.0;

/// POSIX `RTLD_LAZY` flag for `dlopen`.
pub(crate) const RTLD_LAZY: i32 = 1;

/// X11 `ZPixmap` image format identifier.
pub(crate) const X11_Z_PIXMAP: i32 = 2;

/// Combined X11 input mask (`KeyPress | KeyRelease | ButtonPress | ButtonRelease | PointerMotion | Exposure | VisibilityChange | StructureNotify | FocusChange`).
pub(crate) const X11_EVENT_MASK: isize = 0x0023_804F;

/// X11 `KeyPress` event type.
pub(crate) const X11_KEY_PRESS: i32 = 2;

/// X11 `KeyRelease` event type.
pub(crate) const X11_KEY_RELEASE: i32 = 3;

/// X11 `ButtonPress` event type.
pub(crate) const X11_BUTTON_PRESS: i32 = 4;

/// X11 `ButtonRelease` event type.
pub(crate) const X11_BUTTON_RELEASE: i32 = 5;

/// X11 `MotionNotify` event type.
pub(crate) const X11_MOTION_NOTIFY: i32 = 6;

/// X11 `FocusIn` event type.
pub(crate) const X11_FOCUS_IN: i32 = 9;

/// X11 `FocusOut` event type.
pub(crate) const X11_FOCUS_OUT: i32 = 10;

/// X11 `Expose` event type.
pub(crate) const X11_EXPOSE: i32 = 12;

/// X11 `DestroyNotify` event type.
pub(crate) const X11_DESTROY_NOTIFY: i32 = 17;

/// X11 `UnmapNotify` event type.
pub(crate) const X11_UNMAP_NOTIFY: i32 = 18;

/// X11 `MapNotify` event type.
pub(crate) const X11_MAP_NOTIFY: i32 = 19;

/// X11 `ConfigureNotify` event type.
pub(crate) const X11_CONFIGURE_NOTIFY: i32 = 22;

/// X11 `ClientMessage` event type.
pub(crate) const X11_CLIENT_MESSAGE: i32 = 33;

/// X11 modifier mask for Shift.
pub(crate) const X11_SHIFT_MASK: u32 = 1;

/// X11 modifier mask for Control.
pub(crate) const X11_CTRL_MASK: u32 = 4;

/// X11 modifier mask for Alt (`Mod1`).
pub(crate) const X11_ALT_MASK: u32 = 8;

/// X11 modifier mask for Super/Meta (`Mod4`).
pub(crate) const X11_META_MASK: u32 = 64;

/// Error message when neither Wayland nor X11 display server is reachable.
pub(crate) const ERR_LINUX_NO_DISPLAY: &str = "neither X11 nor Wayland display server available";

/// Dynamic symbol for `wl_display_get_error`.
pub(crate) const SYM_WL_DISPLAY_GET_ERROR: &str = "wl_display_get_error\0";

/// Dynamic symbol for `wl_proxy_marshal_flags`.
pub(crate) const SYM_WL_PROXY_MARSHAL_FLAGS: &str = "wl_proxy_marshal_flags\0";

/// Dynamic symbol for `wl_proxy_add_listener`.
pub(crate) const SYM_WL_PROXY_ADD_LISTENER: &str = "wl_proxy_add_listener\0";

/// Dynamic symbol for `wl_proxy_destroy`.
pub(crate) const SYM_WL_PROXY_DESTROY: &str = "wl_proxy_destroy\0";

/// Dynamic symbol for `wl_proxy_get_version`.
pub(crate) const SYM_WL_PROXY_GET_VERSION: &str = "wl_proxy_get_version\0";

/// Exported Wayland protocol descriptor symbol for `wl_registry_interface`.
pub(crate) const SYM_WL_REGISTRY_INTERFACE: &str = "wl_registry_interface\0";

/// Exported Wayland protocol descriptor symbol for `wl_compositor_interface`.
pub(crate) const SYM_WL_COMPOSITOR_INTERFACE: &str = "wl_compositor_interface\0";

/// Exported Wayland protocol descriptor symbol for `wl_surface_interface`.
pub(crate) const SYM_WL_SURFACE_INTERFACE: &str = "wl_surface_interface\0";

/// Exported Wayland protocol descriptor symbol for `wl_shm_interface`.
pub(crate) const SYM_WL_SHM_INTERFACE: &str = "wl_shm_interface\0";

/// Exported Wayland protocol descriptor symbol for `wl_shm_pool_interface`.
pub(crate) const SYM_WL_SHM_POOL_INTERFACE: &str = "wl_shm_pool_interface\0";

/// Exported Wayland protocol descriptor symbol for `wl_buffer_interface`.
pub(crate) const SYM_WL_BUFFER_INTERFACE: &str = "wl_buffer_interface\0";

/// Dynamic symbol for `vkCreateXlibSurfaceKHR` in `libvulkan.so.1`.
pub(crate) const SYM_VK_CREATE_XLIB_SURFACE: &str = "vkCreateXlibSurfaceKHR\0";

/// Dynamic libc symbol for anonymous Linux shared memory (`memfd_create`).
pub(crate) const SYM_MEMFD_CREATE: &str = "memfd_create\0";

/// Wayland registry global interface name for `wl_compositor`.
pub(crate) const WL_IFACE_COMPOSITOR: &str = "wl_compositor";

/// Wayland registry global interface name for `wl_shm`.
pub(crate) const WL_IFACE_SHM: &str = "wl_shm";

/// Anonymous `memfd_create` buffer name for Wayland software presentation.
pub(crate) const WL_MEMFD_NAME: &str = "euv-wayland-shm\0";

/// Fallback `/dev/shm` directory prefix when `memfd_create` is unavailable.
pub(crate) const LINUX_DEV_SHM_PREFIX: &str = "/dev/shm/euv-wl-shm";

/// Wayland `wl_display.get_registry` request opcode.
pub(crate) const WL_OPCODE_DISPLAY_GET_REGISTRY: u32 = 1;

/// Wayland `wl_registry.bind` request opcode.
pub(crate) const WL_OPCODE_REGISTRY_BIND: u32 = 0;

/// Wayland `wl_compositor.create_surface` request opcode.
pub(crate) const WL_OPCODE_COMPOSITOR_CREATE_SURFACE: u32 = 0;

/// Wayland `wl_shm.create_pool` request opcode.
pub(crate) const WL_OPCODE_SHM_CREATE_POOL: u32 = 0;

/// Wayland `wl_shm_pool.create_buffer` request opcode.
pub(crate) const WL_OPCODE_SHM_POOL_CREATE_BUFFER: u32 = 0;

/// Wayland `wl_shm_pool.destroy` request opcode.
pub(crate) const WL_OPCODE_SHM_POOL_DESTROY: u32 = 1;

/// Wayland `wl_buffer.destroy` request opcode.
pub(crate) const WL_OPCODE_BUFFER_DESTROY: u32 = 0;

/// Wayland `wl_surface.attach` request opcode.
pub(crate) const WL_OPCODE_SURFACE_ATTACH: u32 = 1;

/// Wayland `wl_surface.damage` request opcode (surface version < 4 fallback).
pub(crate) const WL_OPCODE_SURFACE_DAMAGE: u32 = 2;

/// Wayland `wl_surface.commit` request opcode.
pub(crate) const WL_OPCODE_SURFACE_COMMIT: u32 = 6;

/// Wayland `wl_surface.set_buffer_scale` request opcode (surface version >= 3).
pub(crate) const WL_OPCODE_SURFACE_SET_BUFFER_SCALE: u32 = 8;

/// Wayland `wl_surface.damage_buffer` request opcode (surface version >= 4).
pub(crate) const WL_OPCODE_SURFACE_DAMAGE_BUFFER: u32 = 9;

/// Minimum `wl_surface` interface version required for `wl_surface.set_buffer_scale`.
pub(crate) const WL_SURFACE_BUFFER_SCALE_MIN_VERSION: u32 = 3;

/// Wayland `WL_MARSHAL_FLAG_DESTROY` flag destroying a client proxy on marshal.
pub(crate) const WL_MARSHAL_FLAG_DESTROY: u32 = 1;

/// Maximum `wl_compositor` protocol version bound for `wl_surface.damage_buffer` support.
pub(crate) const WL_COMPOSITOR_MAX_VERSION: u32 = 4;

/// Maximum `wl_shm` protocol version bound for software pool creation.
pub(crate) const WL_SHM_MAX_VERSION: u32 = 1;

/// POSIX `PROT_READ | PROT_WRITE` protection mask for `mmap`.
pub(crate) const LINUX_MMAP_PROT_RW: i32 = 3;

/// POSIX `MAP_SHARED` flag for `mmap`.
pub(crate) const LINUX_MMAP_SHARED: i32 = 1;

/// Linux `MFD_CLOEXEC` flag for `memfd_create`.
pub(crate) const LINUX_MFD_CLOEXEC: u32 = 1;

/// POSIX `O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC` open flags on Linux (`2 | 64 | 128 | 524288`).
pub(crate) const LINUX_SHM_OPEN_FLAGS: i32 = 524_482;

/// POSIX file mode `0o600` for fallback `/dev/shm` files.
pub(crate) const LINUX_SHM_FILE_MODE: u32 = 0o600;

/// Number of double-buffered Wayland `wl_shm` slots per window.
pub(crate) const WL_SHM_SLOT_COUNT: usize = 2;

/// Error message when Wayland software presentation lacks a live display, surface, or `wl_shm` global.
pub(crate) const ERR_LINUX_WL_SURFACE_MISSING: &str =
    "wayland software presentation missing wl_display, wl_surface, or wl_shm";

/// Error message when POSIX shared-memory allocation (`memfd_create` / `mmap`) fails.
pub(crate) const ERR_LINUX_WL_SHM_ALLOC: &str = "failed to allocate Wayland wl_shm memory mapping";

/// Error message when `wl_shm_pool` or `wl_buffer` creation fails.
pub(crate) const ERR_LINUX_WL_BUFFER_CREATE: &str =
    "failed to create Wayland wl_shm_pool or wl_buffer";

/// Error message when all double-buffered Wayland shm slots are still busy awaiting `wl_buffer.release`.
pub(crate) const ERR_LINUX_WL_NO_FREE_BUFFER: &str =
    "no free Wayland wl_shm buffer slot available (all buffers busy)";

/// Error message when `wl_display_flush` or `wl_display_get_error` reports a compositor failure.
pub(crate) const ERR_LINUX_WL_DISPLAY_FLUSH: &str =
    "wayland surface commit or wl_display_flush failed";

/// Error message when framebuffer pixel dimensions or backing slice are invalid for presentation.
pub(crate) const ERR_LINUX_INVALID_FRAMEBUFFER: &str =
    "invalid framebuffer dimensions or truncated pixel buffer";

/// Error message when X11 presentation lacks a valid `Display*`, `Window`, `GC`, or `Visual*`.
pub(crate) const ERR_LINUX_X11_SURFACE_MISSING: &str =
    "X11 presentation missing Display, Window, GC, Visual, or XPutImage symbols";

/// Error message when direct GPU presentation is requested without available Vulkan WSI surface support.
pub(crate) const ERR_LINUX_GPU_DIRECT_UNAVAILABLE: &str =
    "Linux direct GPU presentation requires active Vulkan WSI or EGL/GLES3 surface support";

/// Error message when Linux dynamic EGL/GLES3 initialization or swap fails.
pub(crate) const ERR_LINUX_EGL_UNAVAILABLE: &str =
    "Linux dynamic libEGL.so.1 / libGLESv2.so.2 presentation unavailable";

/// Shared library name for dynamic EGL loading on Linux.
pub(crate) const LINUX_LIB_EGL: &str = "libEGL.so.1\0";

/// Shared library name for dynamic OpenGL ES 3 loading on Linux.
pub(crate) const LINUX_LIB_GLESV2: &str = "libGLESv2.so.2\0";

/// Shared library name for dynamic Wayland EGL window bindings on Linux.
pub(crate) const LINUX_LIB_WAYLAND_EGL: &str = "libwayland-egl.so.1\0";

/// Dynamic Xlib symbol returning the underlying socket file descriptor for `poll()`.
pub(crate) const SYM_X_CONNECTION_NUMBER: &str = "XConnectionNumber\0";

/// Dynamic Wayland symbol returning the compositor socket file descriptor for `poll()`.
pub(crate) const SYM_WL_DISPLAY_GET_FD: &str = "wl_display_get_fd\0";

/// Dynamic Wayland symbol preparing a thread-safe `poll()` read on `wl_display`.
pub(crate) const SYM_WL_DISPLAY_PREPARE_READ: &str = "wl_display_prepare_read\0";

/// Dynamic Wayland symbol reading events from the Wayland socket after `poll()`.
pub(crate) const SYM_WL_DISPLAY_READ_EVENTS: &str = "wl_display_read_events\0";

/// Dynamic Wayland symbol canceling a prepared read when `poll()` woke on another fd.
pub(crate) const SYM_WL_DISPLAY_CANCEL_READ: &str = "wl_display_cancel_read\0";

/// Exported Wayland protocol descriptor symbol for `wl_callback_interface`.
pub(crate) const SYM_WL_CALLBACK_INTERFACE: &str = "wl_callback_interface\0";

/// Wayland `wl_surface.frame` request opcode (`3`) for compositor VSync pacing.
pub(crate) const WL_OPCODE_SURFACE_FRAME: u32 = 3;

/// POSIX `POLLIN` event bit (`0x0001`).
pub(crate) const LINUX_POLLIN: i16 = 0x0001;

/// Linux `O_NONBLOCK | O_CLOEXEC` flags (`0x0008_0800`) for `pipe2`.
pub(crate) const LINUX_PIPE_NONBLOCK_CLOEXEC: i32 = 0x0008_0800;

/// Dynamic EGL symbol `eglGetDisplay`.
pub(crate) const SYM_EGL_GET_DISPLAY: &str = "eglGetDisplay\0";

/// Dynamic EGL symbol `eglInitialize`.
pub(crate) const SYM_EGL_INITIALIZE: &str = "eglInitialize\0";

/// Dynamic EGL symbol `eglBindAPI`.
pub(crate) const SYM_EGL_BIND_API: &str = "eglBindAPI\0";

/// Dynamic EGL symbol `eglChooseConfig`.
pub(crate) const SYM_EGL_CHOOSE_CONFIG: &str = "eglChooseConfig\0";

/// Dynamic EGL symbol `eglCreateContext`.
pub(crate) const SYM_EGL_CREATE_CONTEXT: &str = "eglCreateContext\0";

/// Dynamic EGL symbol `eglCreateWindowSurface`.
pub(crate) const SYM_EGL_CREATE_WINDOW_SURFACE: &str = "eglCreateWindowSurface\0";

/// Dynamic EGL symbol `eglMakeCurrent`.
pub(crate) const SYM_EGL_MAKE_CURRENT: &str = "eglMakeCurrent\0";

/// Dynamic EGL symbol `eglSwapInterval`.
pub(crate) const SYM_EGL_SWAP_INTERVAL: &str = "eglSwapInterval\0";

/// Dynamic EGL symbol `eglSwapBuffers`.
pub(crate) const SYM_EGL_SWAP_BUFFERS: &str = "eglSwapBuffers\0";

/// Dynamic EGL symbol `eglDestroySurface`.
pub(crate) const SYM_EGL_DESTROY_SURFACE: &str = "eglDestroySurface\0";

/// Dynamic EGL symbol `eglDestroyContext`.
pub(crate) const SYM_EGL_DESTROY_CONTEXT: &str = "eglDestroyContext\0";

/// Dynamic EGL symbol `eglTerminate`.
pub(crate) const SYM_EGL_TERMINATE: &str = "eglTerminate\0";

/// Dynamic Wayland-EGL symbol `wl_egl_window_create`.
pub(crate) const SYM_WL_EGL_WINDOW_CREATE: &str = "wl_egl_window_create\0";

/// Dynamic Wayland-EGL symbol `wl_egl_window_resize`.
pub(crate) const SYM_WL_EGL_WINDOW_RESIZE: &str = "wl_egl_window_resize\0";

/// Dynamic Wayland-EGL symbol `wl_egl_window_destroy`.
pub(crate) const SYM_WL_EGL_WINDOW_DESTROY: &str = "wl_egl_window_destroy\0";

/// Dynamic GLES3 symbol `glCreateShader`.
pub(crate) const SYM_GL_CREATE_SHADER: &str = "glCreateShader\0";

/// Dynamic GLES3 symbol `glShaderSource`.
pub(crate) const SYM_GL_SHADER_SOURCE: &str = "glShaderSource\0";

/// Dynamic GLES3 symbol `glCompileShader`.
pub(crate) const SYM_GL_COMPILE_SHADER: &str = "glCompileShader\0";

/// Dynamic GLES3 symbol `glGetShaderiv`.
pub(crate) const SYM_GL_GET_SHADERIV: &str = "glGetShaderiv\0";

/// Dynamic GLES3 symbol `glDeleteShader`.
pub(crate) const SYM_GL_DELETE_SHADER: &str = "glDeleteShader\0";

/// Dynamic GLES3 symbol `glCreateProgram`.
pub(crate) const SYM_GL_CREATE_PROGRAM: &str = "glCreateProgram\0";

/// Dynamic GLES3 symbol `glAttachShader`.
pub(crate) const SYM_GL_ATTACH_SHADER: &str = "glAttachShader\0";

/// Dynamic GLES3 symbol `glLinkProgram`.
pub(crate) const SYM_GL_LINK_PROGRAM: &str = "glLinkProgram\0";

/// Dynamic GLES3 symbol `glGetProgramiv`.
pub(crate) const SYM_GL_GET_PROGRAMIV: &str = "glGetProgramiv\0";

/// Dynamic GLES3 symbol `glDeleteProgram`.
pub(crate) const SYM_GL_DELETE_PROGRAM: &str = "glDeleteProgram\0";

/// Dynamic GLES3 symbol `glUseProgram`.
pub(crate) const SYM_GL_USE_PROGRAM: &str = "glUseProgram\0";

/// Dynamic GLES3 symbol `glGetUniformLocation`.
pub(crate) const SYM_GL_GET_UNIFORM_LOCATION: &str = "glGetUniformLocation\0";

/// Dynamic GLES3 symbol `glUniform1i`.
pub(crate) const SYM_GL_UNIFORM1I: &str = "glUniform1i\0";

/// Dynamic GLES3 symbol `glUniform2f`.
pub(crate) const SYM_GL_UNIFORM2F: &str = "glUniform2f\0";

/// Dynamic GLES3 symbol `glGenVertexArrays`.
pub(crate) const SYM_GL_GEN_VERTEX_ARRAYS: &str = "glGenVertexArrays\0";

/// Dynamic GLES3 symbol `glBindVertexArray`.
pub(crate) const SYM_GL_BIND_VERTEX_ARRAY: &str = "glBindVertexArray\0";

/// Dynamic GLES3 symbol `glDeleteVertexArrays`.
pub(crate) const SYM_GL_DELETE_VERTEX_ARRAYS: &str = "glDeleteVertexArrays\0";

/// Dynamic GLES3 symbol `glGenBuffers`.
pub(crate) const SYM_GL_GEN_BUFFERS: &str = "glGenBuffers\0";

/// Dynamic GLES3 symbol `glBindBuffer`.
pub(crate) const SYM_GL_BIND_BUFFER: &str = "glBindBuffer\0";

/// Dynamic GLES3 symbol `glBufferData`.
pub(crate) const SYM_GL_BUFFER_DATA: &str = "glBufferData\0";

/// Dynamic GLES3 symbol `glBufferSubData`.
pub(crate) const SYM_GL_BUFFER_SUB_DATA: &str = "glBufferSubData\0";

/// Dynamic GLES3 symbol `glDeleteBuffers`.
pub(crate) const SYM_GL_DELETE_BUFFERS: &str = "glDeleteBuffers\0";

/// Dynamic GLES3 symbol `glEnableVertexAttribArray`.
pub(crate) const SYM_GL_ENABLE_VERTEX_ATTRIB_ARRAY: &str = "glEnableVertexAttribArray\0";

/// Dynamic GLES3 symbol `glVertexAttribPointer`.
pub(crate) const SYM_GL_VERTEX_ATTRIB_POINTER: &str = "glVertexAttribPointer\0";

/// Dynamic GLES3 symbol `glVertexAttribIPointer`.
pub(crate) const SYM_GL_VERTEX_ATTRIB_IPOINTER: &str = "glVertexAttribIPointer\0";

/// Dynamic GLES3 symbol `glVertexAttribDivisor`.
pub(crate) const SYM_GL_VERTEX_ATTRIB_DIVISOR: &str = "glVertexAttribDivisor\0";

/// Dynamic GLES3 symbol `glGenTextures`.
pub(crate) const SYM_GL_GEN_TEXTURES: &str = "glGenTextures\0";

/// Dynamic GLES3 symbol `glBindTexture`.
pub(crate) const SYM_GL_BIND_TEXTURE: &str = "glBindTexture\0";

/// Dynamic GLES3 symbol `glTexParameteri`.
pub(crate) const SYM_GL_TEX_PARAMETERI: &str = "glTexParameteri\0";

/// Dynamic GLES3 symbol `glTexStorage2D`.
pub(crate) const SYM_GL_TEX_STORAGE_2D: &str = "glTexStorage2D\0";

/// Dynamic GLES3 symbol `glTexImage2D`.
pub(crate) const SYM_GL_TEX_IMAGE_2D: &str = "glTexImage2D\0";

/// Dynamic GLES3 symbol `glTexSubImage2D`.
pub(crate) const SYM_GL_TEX_SUB_IMAGE_2D: &str = "glTexSubImage2D\0";

/// Dynamic GLES3 symbol `glDeleteTextures`.
pub(crate) const SYM_GL_DELETE_TEXTURES: &str = "glDeleteTextures\0";

/// Dynamic GLES3 symbol `glActiveTexture`.
pub(crate) const SYM_GL_ACTIVE_TEXTURE: &str = "glActiveTexture\0";

/// Dynamic GLES3 symbol `glPixelStorei`.
pub(crate) const SYM_GL_PIXEL_STOREI: &str = "glPixelStorei\0";

/// Dynamic GLES3 symbol `glViewport`.
pub(crate) const SYM_GL_VIEWPORT: &str = "glViewport\0";

/// Dynamic GLES3 symbol `glEnable`.
pub(crate) const SYM_GL_ENABLE: &str = "glEnable\0";

/// Dynamic GLES3 symbol `glDisable`.
pub(crate) const SYM_GL_DISABLE: &str = "glDisable\0";

/// Dynamic GLES3 symbol `glBlendFuncSeparate`.
pub(crate) const SYM_GL_BLEND_FUNC_SEPARATE: &str = "glBlendFuncSeparate\0";

/// Dynamic GLES3 symbol `glScissor`.
pub(crate) const SYM_GL_SCISSOR: &str = "glScissor\0";

/// Dynamic GLES3 symbol `glClearColor`.
pub(crate) const SYM_GL_CLEAR_COLOR: &str = "glClearColor\0";

/// Dynamic GLES3 symbol `glClear`.
pub(crate) const SYM_GL_CLEAR: &str = "glClear\0";

/// Dynamic GLES3 symbol `glDrawArraysInstanced`.
pub(crate) const SYM_GL_DRAW_ARRAYS_INSTANCED: &str = "glDrawArraysInstanced\0";

/// Dynamic GLES3 symbol `glDrawArrays`.
pub(crate) const SYM_GL_DRAW_ARRAYS: &str = "glDrawArrays\0";

/// Dynamic GLES3 symbol `glUniform4f`.
pub(crate) const SYM_GL_UNIFORM4F: &str = "glUniform4f\0";

/// Dynamic GLES3 symbol `glUniformMatrix4fv`.
pub(crate) const SYM_GL_UNIFORM_MATRIX4FV: &str = "glUniformMatrix4fv\0";

/// Dynamic GLES3 symbol `glGetIntegerv`.
pub(crate) const SYM_GL_GET_INTEGERV: &str = "glGetIntegerv\0";

/// Dynamic GLES3 symbol `glGenFramebuffers`.
pub(crate) const SYM_GL_GEN_FRAMEBUFFERS: &str = "glGenFramebuffers\0";

/// Dynamic GLES3 symbol `glBindFramebuffer`.
pub(crate) const SYM_GL_BIND_FRAMEBUFFER: &str = "glBindFramebuffer\0";

/// Dynamic GLES3 symbol `glFramebufferTexture2D`.
pub(crate) const SYM_GL_FRAMEBUFFER_TEXTURE_2D: &str = "glFramebufferTexture2D\0";

/// Dynamic GLES3 symbol `glDeleteFramebuffers`.
pub(crate) const SYM_GL_DELETE_FRAMEBUFFERS: &str = "glDeleteFramebuffers\0";

/// Dynamic GLES3 symbol `glGenRenderbuffers`.
pub(crate) const SYM_GL_GEN_RENDERBUFFERS: &str = "glGenRenderbuffers\0";

/// Dynamic GLES3 symbol `glBindRenderbuffer`.
pub(crate) const SYM_GL_BIND_RENDERBUFFER: &str = "glBindRenderbuffer\0";

/// Dynamic GLES3 symbol `glRenderbufferStorage`.
pub(crate) const SYM_GL_RENDERBUFFER_STORAGE: &str = "glRenderbufferStorage\0";

/// Dynamic GLES3 symbol `glFramebufferRenderbuffer`.
pub(crate) const SYM_GL_FRAMEBUFFER_RENDERBUFFER: &str = "glFramebufferRenderbuffer\0";

/// Dynamic GLES3 symbol `glDeleteRenderbuffers`.
pub(crate) const SYM_GL_DELETE_RENDERBUFFERS: &str = "glDeleteRenderbuffers\0";

/// Dynamic GLES3 symbol `glDepthMask`.
pub(crate) const SYM_GL_DEPTH_MASK: &str = "glDepthMask\0";

/// Dynamic GLES3 symbol `glDepthFunc`.
pub(crate) const SYM_GL_DEPTH_FUNC: &str = "glDepthFunc\0";

/// Dynamic GLES3 symbol `glClearDepthf`.
pub(crate) const SYM_GL_CLEAR_DEPTHF: &str = "glClearDepthf\0";

/// Dynamic GLES3 symbol `glDrawElementsInstanced`.
pub(crate) const SYM_GL_DRAW_ELEMENTS_INSTANCED: &str = "glDrawElementsInstanced\0";

/// Dynamic GLES3 symbol `glUniform4i`.
pub(crate) const SYM_GL_UNIFORM4I: &str = "glUniform4i\0";

/// Dynamic GLES3 symbol `glUniform4fv`.
pub(crate) const SYM_GL_UNIFORM4FV: &str = "glUniform4fv\0";

/// Dynamic GLES3 symbol `glDisableVertexAttribArray`.
pub(crate) const SYM_GL_DISABLE_VERTEX_ATTRIB_ARRAY: &str = "glDisableVertexAttribArray\0";

/// Dynamic GLES3 symbol `glVertexAttribI4ui`.
pub(crate) const SYM_GL_VERTEX_ATTRIB_I4UI: &str = "glVertexAttribI4ui\0";

/// Dynamic GLES3 symbol `glVertexAttrib4f`.
pub(crate) const SYM_GL_VERTEX_ATTRIB_4F: &str = "glVertexAttrib4f\0";

/// Dynamic GLES3 symbol `glProgramParameteri`.
pub(crate) const SYM_GL_PROGRAM_PARAMETERI: &str = "glProgramParameteri\0";

/// Dynamic GLES3 symbol `glGetProgramBinary`.
pub(crate) const SYM_GL_GET_PROGRAM_BINARY: &str = "glGetProgramBinary\0";

/// Dynamic GLES3 symbol `glProgramBinary`.
pub(crate) const SYM_GL_PROGRAM_BINARY: &str = "glProgramBinary\0";

/// Dynamic GLES3 symbol `glGetString`.
pub(crate) const SYM_GL_GET_STRING: &str = "glGetString\0";

/// GLSL ES3 PBR scene uniform `u_camera_pos_exposure`.
pub(crate) const LINUX_GL_PBR_UNIFORM_CAM_EXP: &str = "u_camera_pos_exposure\0";

/// GLSL ES3 PBR scene uniform `u_sky_zenith_agx`.
pub(crate) const LINUX_GL_PBR_UNIFORM_SKY_ZEN_AGX: &str = "u_sky_zenith_agx\0";

/// GLSL ES3 PBR scene uniform `u_ibl_sh[0]`.
pub(crate) const LINUX_GL_PBR_UNIFORM_IBL_SH: &str = "u_ibl_sh[0]\0";

/// GLSL ES3 PBR scene uniform `u_batch_params`.
pub(crate) const LINUX_GL_PBR_UNIFORM_BATCH_PARAMS: &str = "u_batch_params\0";

/// GLSL ES3 PBR scene uniform `u_draw_offsets`.
pub(crate) const LINUX_GL_PBR_UNIFORM_DRAW_OFFSETS: &str = "u_draw_offsets\0";

/// GLSL ES3 PBR scene uniform `u_morph_weights[0]`.
pub(crate) const LINUX_GL_PBR_UNIFORM_MORPH_WEIGHTS: &str = "u_morph_weights[0]\0";

/// GLSL ES3 PBR scene sampler uniform `u_tex_base`.
pub(crate) const LINUX_GL_PBR_UNIFORM_TEX_BASE: &str = "u_tex_base\0";

/// GLSL ES3 PBR scene sampler uniform `u_tex_mr`.
pub(crate) const LINUX_GL_PBR_UNIFORM_TEX_MR: &str = "u_tex_mr\0";

/// GLSL ES3 PBR scene sampler uniform `u_tex_normal`.
pub(crate) const LINUX_GL_PBR_UNIFORM_TEX_NORMAL: &str = "u_tex_normal\0";

/// GLSL ES3 PBR scene sampler uniform `u_tex_occ`.
pub(crate) const LINUX_GL_PBR_UNIFORM_TEX_OCC: &str = "u_tex_occ\0";

/// GLSL ES3 PBR scene sampler uniform `u_tex_emissive`.
pub(crate) const LINUX_GL_PBR_UNIFORM_TEX_EMISSIVE: &str = "u_tex_emissive\0";

/// GLSL ES3 PBR scene sampler uniform `u_bone_tex`.
pub(crate) const LINUX_GL_PBR_UNIFORM_BONE_TEX: &str = "u_bone_tex\0";

/// GLSL ES3 PBR scene sampler uniform `u_mat_tex`.
pub(crate) const LINUX_GL_PBR_UNIFORM_MAT_TEX: &str = "u_mat_tex\0";

/// GLSL ES3 PBR scene sampler uniform `u_light_tex`.
pub(crate) const LINUX_GL_PBR_UNIFORM_LIGHT_TEX: &str = "u_light_tex\0";

/// GLSL ES3 PBR scene sampler uniform `u_morph_tex`.
pub(crate) const LINUX_GL_PBR_UNIFORM_MORPH_TEX: &str = "u_morph_tex\0";

/// Persistent disk cache filename prefix for Linux GLES3 `glProgramBinary` blobs.
pub(crate) const LINUX_GL_PROG_CACHE_PREFIX: &str = "euv_gles3_pbr_";

/// Persistent disk cache filename suffix for Linux GLES3 `glProgramBinary` blobs.
pub(crate) const LINUX_GL_PROG_CACHE_SUFFIX: &str = ".glbin";

/// GLES3 `GL_ELEMENT_ARRAY_BUFFER`.
pub(crate) const LINUX_GL_ELEMENT_ARRAY_BUFFER: u32 = 0x8893;

/// GLES3 `GL_SHORT`.
pub(crate) const LINUX_GL_SHORT: u32 = 0x1402;

/// GLES3 `GL_UNSIGNED_SHORT`.
pub(crate) const LINUX_GL_UNSIGNED_SHORT: u32 = 0x1403;

/// GLES3 `GL_RGBA32F`.
pub(crate) const LINUX_GL_RGBA32F: u32 = 0x8814;

/// GLES3 `GL_NEAREST`.
pub(crate) const LINUX_GL_NEAREST: i32 = 0x2600;

/// GLES3 `GL_LINEAR_MIPMAP_LINEAR`.
pub(crate) const LINUX_GL_LINEAR_MIPMAP_LINEAR: i32 = 0x2703;

/// GLES3 `GL_REPEAT`.
pub(crate) const LINUX_GL_REPEAT: i32 = 0x2901;

/// GLES3 `GL_TEXTURE_BASE_LEVEL`.
pub(crate) const LINUX_GL_TEXTURE_BASE_LEVEL: u32 = 0x813C;

/// GLES3 `GL_TEXTURE_MAX_LEVEL`.
pub(crate) const LINUX_GL_TEXTURE_MAX_LEVEL: u32 = 0x813D;

/// GLES3 `GL_PROGRAM_BINARY_RETRIEVABLE_HINT`.
pub(crate) const LINUX_GL_PROGRAM_BINARY_RETRIEVABLE_HINT: u32 = 0x8257;

/// GLES3 `GL_PROGRAM_BINARY_LENGTH`.
pub(crate) const LINUX_GL_PROGRAM_BINARY_LENGTH: u32 = 0x8741;

/// GLES3 `GL_RENDERER`.
pub(crate) const LINUX_GL_RENDERER: u32 = 0x1F01;

/// GLES3 `GL_VERSION`.
pub(crate) const LINUX_GL_VERSION: u32 = 0x1F02;

/// GLSL ES3 scene uniform `u_wake_tex`.
pub(crate) const LINUX_GL_UNIFORM_WAKE_TEX: &str = "u_wake_tex\0";

/// GLSL ES3 scene uniform `u_time_agit_hor_aspect`.
pub(crate) const LINUX_GL_UNIFORM_OCEAN_TIME: &str = "u_time_agit_hor_aspect\0";

/// GLSL ES3 scene uniform `u_roll_boat_uv`.
pub(crate) const LINUX_GL_UNIFORM_OCEAN_ROLL: &str = "u_roll_boat_uv\0";

/// GLSL ES3 scene uniform `u_sun_dir_wake`.
pub(crate) const LINUX_GL_UNIFORM_OCEAN_SUN: &str = "u_sun_dir_wake\0";

/// GLSL ES3 scene uniform `u_camera_pos`.
pub(crate) const LINUX_GL_UNIFORM_CAM_POS: &str = "u_camera_pos\0";

/// GLSL ES3 scene uniform `u_sun_dir_fog`.
pub(crate) const LINUX_GL_UNIFORM_SUN_FOG: &str = "u_sun_dir_fog\0";

/// GLSL ES3 scene uniform `u_sun_color_ambient`.
pub(crate) const LINUX_GL_UNIFORM_SUN_COL: &str = "u_sun_color_ambient\0";

/// GLSL ES3 scene uniform `u_sky_horizon_tod`.
pub(crate) const LINUX_GL_UNIFORM_SKY_HOR: &str = "u_sky_horizon_tod\0";

/// GLSL ES3 scene uniform `u_sky_zenith_aspect`.
pub(crate) const LINUX_GL_UNIFORM_SKY_ZEN: &str = "u_sky_zenith_aspect\0";

/// GLSL ES3 scene uniform `u_view_proj`.
pub(crate) const LINUX_GL_UNIFORM_VIEW_PROJ: &str = "u_view_proj\0";

/// GLSL ES3 uniform name `u_viewport`.
pub(crate) const LINUX_GL_UNIFORM_VIEWPORT: &str = "u_viewport\0";

/// GLSL ES3 uniform name `u_scroll_offset`.
pub(crate) const LINUX_GL_UNIFORM_SCROLL_OFFSET: &str = "u_scroll_offset\0";

/// GLSL ES3 sampler uniform `u_glyph_r8`.
pub(crate) const LINUX_GL_UNIFORM_GLYPH_R8: &str = "u_glyph_r8\0";

/// GLSL ES3 sampler uniform `u_glyph_rgba8`.
pub(crate) const LINUX_GL_UNIFORM_GLYPH_RGBA8: &str = "u_glyph_rgba8\0";

/// GLSL ES3 sampler uniform `u_image_tex`.
pub(crate) const LINUX_GL_UNIFORM_IMAGE_TEX: &str = "u_image_tex\0";

/// GLSL ES3 sampler uniform `u_external_tex`.
pub(crate) const LINUX_GL_UNIFORM_EXTERNAL_TEX: &str = "u_external_tex\0";

/// `EGL_OPENGL_ES_API` (`0x30A0`).
pub(crate) const LINUX_EGL_OPENGL_ES_API: u32 = 0x30A0;

/// `EGL_SURFACE_TYPE` (`0x3033`).
pub(crate) const LINUX_EGL_SURFACE_TYPE: i32 = 0x3033;

/// `EGL_WINDOW_BIT` (`0x0004`).
pub(crate) const LINUX_EGL_WINDOW_BIT: i32 = 0x0004;

/// `EGL_RENDERABLE_TYPE` (`0x3040`).
pub(crate) const LINUX_EGL_RENDERABLE_TYPE: i32 = 0x3040;

/// `EGL_OPENGL_ES3_BIT` (`0x0040`).
pub(crate) const LINUX_EGL_OPENGL_ES3_BIT: i32 = 0x0040;

/// `EGL_RED_SIZE` (`0x3024`).
pub(crate) const LINUX_EGL_RED_SIZE: i32 = 0x3024;

/// `EGL_GREEN_SIZE` (`0x3023`).
pub(crate) const LINUX_EGL_GREEN_SIZE: i32 = 0x3023;

/// `EGL_BLUE_SIZE` (`0x3022`).
pub(crate) const LINUX_EGL_BLUE_SIZE: i32 = 0x3022;

/// `EGL_ALPHA_SIZE` (`0x3021`).
pub(crate) const LINUX_EGL_ALPHA_SIZE: i32 = 0x3021;

/// `EGL_NONE` (`0x3038`).
pub(crate) const LINUX_EGL_NONE: i32 = 0x3038;

/// `EGL_CONTEXT_CLIENT_VERSION` (`0x3098`).
pub(crate) const LINUX_EGL_CONTEXT_CLIENT_VERSION: i32 = 0x3098;

/// `GL_VERTEX_SHADER` (`0x8B31`).
pub(crate) const LINUX_GL_VERTEX_SHADER: u32 = 0x8B31;

/// `GL_FRAGMENT_SHADER` (`0x8B30`).
pub(crate) const LINUX_GL_FRAGMENT_SHADER: u32 = 0x8B30;

/// `GL_COMPILE_STATUS` (`0x8B81`).
pub(crate) const LINUX_GL_COMPILE_STATUS: u32 = 0x8B81;

/// `GL_LINK_STATUS` (`0x8B82`).
pub(crate) const LINUX_GL_LINK_STATUS: u32 = 0x8B82;

/// `GL_ARRAY_BUFFER` (`0x8892`).
pub(crate) const LINUX_GL_ARRAY_BUFFER: u32 = 0x8892;

/// `GL_STATIC_DRAW` (`0x88E4`).
pub(crate) const LINUX_GL_STATIC_DRAW: u32 = 0x88E4;

/// `GL_STREAM_DRAW` (`0x88E0`).
pub(crate) const LINUX_GL_STREAM_DRAW: u32 = 0x88E0;

/// `GL_TRIANGLES` (`0x0004`).
pub(crate) const LINUX_GL_TRIANGLES: u32 = 0x0004;

/// `GL_FLOAT` (`0x1406`).
pub(crate) const LINUX_GL_FLOAT: u32 = 0x1406;

/// `GL_UNSIGNED_INT` (`0x1405`).
pub(crate) const LINUX_GL_UNSIGNED_INT: u32 = 0x1405;

/// `GL_UNSIGNED_BYTE` (`0x1401`).
pub(crate) const LINUX_GL_UNSIGNED_BYTE: u32 = 0x1401;

/// `GL_TEXTURE_2D` (`0x0DE1`).
pub(crate) const LINUX_GL_TEXTURE_2D: u32 = 0x0DE1;

/// `GL_TEXTURE0` (`0x84C0`).
pub(crate) const LINUX_GL_TEXTURE0: u32 = 0x84C0;

/// `GL_TEXTURE1` (`0x84C1`).
pub(crate) const LINUX_GL_TEXTURE1: u32 = 0x84C1;

/// `GL_TEXTURE2` (`0x84C2`).
pub(crate) const LINUX_GL_TEXTURE2: u32 = 0x84C2;

/// `GL_TEXTURE3` (`0x84C3`).
pub(crate) const LINUX_GL_TEXTURE3: u32 = 0x84C3;

/// `GL_TEXTURE_MIN_FILTER` (`0x2801`).
pub(crate) const LINUX_GL_TEXTURE_MIN_FILTER: u32 = 0x2801;

/// `GL_TEXTURE_MAG_FILTER` (`0x2800`).
pub(crate) const LINUX_GL_TEXTURE_MAG_FILTER: u32 = 0x2800;

/// `GL_TEXTURE_WRAP_S` (`0x2802`).
pub(crate) const LINUX_GL_TEXTURE_WRAP_S: u32 = 0x2802;

/// `GL_TEXTURE_WRAP_T` (`0x2803`).
pub(crate) const LINUX_GL_TEXTURE_WRAP_T: u32 = 0x2803;

/// `GL_LINEAR` (`0x2601`).
pub(crate) const LINUX_GL_LINEAR: i32 = 0x2601;

/// `GL_CLAMP_TO_EDGE` (`0x812F`).
pub(crate) const LINUX_GL_CLAMP_TO_EDGE: i32 = 0x812F;

/// `GL_R8` (`0x8229`).
pub(crate) const LINUX_GL_R8: u32 = 0x8229;

/// `GL_RED` (`0x1903`).
pub(crate) const LINUX_GL_RED: u32 = 0x1903;

/// `GL_RGBA8` (`0x8058`).
pub(crate) const LINUX_GL_RGBA8: u32 = 0x8058;

/// `GL_RGBA` (`0x1908`).
pub(crate) const LINUX_GL_RGBA: u32 = 0x1908;

/// `GL_UNPACK_ALIGNMENT` (`0x0CF5`).
pub(crate) const LINUX_GL_UNPACK_ALIGNMENT: u32 = 0x0CF5;

/// `GL_UNPACK_ROW_LENGTH` (`0x0CF2`).
pub(crate) const LINUX_GL_UNPACK_ROW_LENGTH: u32 = 0x0CF2;

/// `GL_BLEND` (`0x0BE2`).
pub(crate) const LINUX_GL_BLEND: u32 = 0x0BE2;

/// `GL_SCISSOR_TEST` (`0x0C11`).
pub(crate) const LINUX_GL_SCISSOR_TEST: u32 = 0x0C11;

/// `GL_SRC_ALPHA` (`0x0302`).
pub(crate) const LINUX_GL_SRC_ALPHA: u32 = 0x0302;

/// `GL_ONE_MINUS_SRC_ALPHA` (`0x0303`).
pub(crate) const LINUX_GL_ONE_MINUS_SRC_ALPHA: u32 = 0x0303;

/// `GL_ONE` (`1`).
pub(crate) const LINUX_GL_ONE: u32 = 1;

/// `GL_COLOR_BUFFER_BIT` (`0x4000`).
pub(crate) const LINUX_GL_COLOR_BUFFER_BIT: u32 = 0x4000;

/// `GL_DEPTH_BUFFER_BIT` (`0x0100`).
pub(crate) const LINUX_GL_DEPTH_BUFFER_BIT: u32 = 0x0100;

/// `GL_DEPTH_TEST` (`0x0B71`).
pub(crate) const LINUX_GL_DEPTH_TEST: u32 = 0x0B71;

/// `GL_LESS` (`0x0201`).
pub(crate) const LINUX_GL_LESS: u32 = 0x0201;

/// `GL_FRAMEBUFFER` (`0x8D40`).
pub(crate) const LINUX_GL_FRAMEBUFFER: u32 = 0x8D40;

/// `GL_RENDERBUFFER` (`0x8D41`).
pub(crate) const LINUX_GL_RENDERBUFFER: u32 = 0x8D41;

/// `GL_COLOR_ATTACHMENT0` (`0x8CE0`).
pub(crate) const LINUX_GL_COLOR_ATTACHMENT0: u32 = 0x8CE0;

/// `GL_DEPTH_ATTACHMENT` (`0x8D00`).
pub(crate) const LINUX_GL_DEPTH_ATTACHMENT: u32 = 0x8D00;

/// `GL_DEPTH_COMPONENT24` (`0x81A6`).
pub(crate) const LINUX_GL_DEPTH_COMPONENT24: u32 = 0x81A6;

/// `GL_MAX_TEXTURE_SIZE` (`0x0D33`).
pub(crate) const LINUX_GL_MAX_TEXTURE_SIZE: u32 = 0x0D33;

/// `GL_UNPACK_SKIP_ROWS` (`0x0CF3`).
pub(crate) const LINUX_GL_UNPACK_SKIP_ROWS: u32 = 0x0CF3;

/// `GL_UNPACK_SKIP_PIXELS` (`0x0CF4`).
pub(crate) const LINUX_GL_UNPACK_SKIP_PIXELS: u32 = 0x0CF4;

/// Unit quad triangle corners matching `GLSL_ES3_UI_VERTEX_SHADER` attribute location `0`.
pub(crate) const LINUX_GL_UNIT_QUAD: [f32; 12] =
    [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0];

/// Hardware GLSL ES 3.0 full-screen triangle vertex shader for offscreen Linux GPU scene passes.
pub(crate) const LINUX_GL_SCENE_FULLSCREEN_VERT: &str = "#version 300 es\n\
precision highp float;\n\
precision highp int;\n\
out highp vec2 v_uv;\n\
void main() {\n\
    vec2 p = vec2((gl_VertexID == 1) ? 3.0 : -1.0, (gl_VertexID == 2) ? 3.0 : -1.0);\n\
    gl_Position = vec4(p, 0.0, 1.0);\n\
    v_uv = vec2(p.x * 0.5 + 0.5, 0.5 - p.y * 0.5);\n\
}\n";

/// Hardware GLSL ES 3.0 8-wave isotropic Nyquist-AA Gerstner ocean, sunlit coastal golden-day sky, Toksvig dual-lobe GGX sun glitter, PBF wake, and hull foam fragment shader.
pub(crate) const LINUX_GL_SCENE_OCEAN_FRAG: &str = r#"#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
in highp vec2 v_uv;
uniform highp vec4 u_time_agit_hor_aspect;
uniform highp vec4 u_roll_boat_uv;
uniform highp vec4 u_sun_dir_wake;
uniform highp sampler2D u_wake_tex;
out vec4 out_color;
const vec4 EUV_OCEAN_WAVES[8] = vec4[8](
    vec4( 0.2419,  0.9703,  0.56, 0.245),
    vec4(-0.5592,  0.8290,  0.94, 0.172),
    vec4( 0.7986,  0.6018,  1.58, 0.114),
    vec4(-0.9455,  0.3256,  2.63, 0.072),
    vec4( 0.5299,  0.8480,  4.38, 0.045),
    vec4(-0.7880, -0.6157,  7.21, 0.028),
    vec4( 0.7314, -0.6820, 11.85, 0.018),
    vec4(-0.3746,  0.9272, 19.30, 0.0105)
);
const float EUV_OCEAN_SPEEDS[8] = float[8](0.84, 1.12, 1.48, 1.92, 2.46, 3.08, 3.82, 4.68);
vec3 euv_aces(vec3 hdr) {
    vec3 x = max(hdr, vec3(0.0));
    float luma = dot(x, vec3(0.2126, 0.7152, 0.0722));
    float sat_roll = 1.0 / (1.0 + luma * 0.14);
    vec3 agx_in = mix(vec3(luma), x, 0.78 + 0.22 * sat_roll);
    vec3 num = agx_in * (2.51 * agx_in + 0.03);
    vec3 den = agx_in * (2.43 * agx_in + 0.59) + 0.14;
    return pow(clamp(num / max(den, vec3(0.0001)), vec3(0.0), vec3(1.0)), vec3(1.0 / 2.2));
}
float euv_hash_i2(ivec2 cell) {
    uint n = uint(cell.x) * 374761393u + uint(cell.y) * 668265263u;
    n = (n ^ (n >> 13u)) * 1274126177u;
    n ^= n >> 16u;
    return float(n & 0x00FFFFFFu) * (1.0 / 16777215.0);
}
float euv_noise(vec2 p) {
    vec2 fl = floor(p);
    vec2 f = p - fl;
    vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
    ivec2 i = ivec2(fl);
    float a = euv_hash_i2(i);
    float b = euv_hash_i2(i + ivec2(1, 0));
    float c = euv_hash_i2(i + ivec2(0, 1));
    float d = euv_hash_i2(i + ivec2(1, 1));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float euv_cloud_fbm(vec2 p, int octs) {
    float val = 0.0;
    float amp = 0.54;
    vec2 q = p;
    for (int i = 0; i < 4; ++i) {
        if (i >= octs) {
            break;
        }
        val += euv_noise(q) * amp;
        q = vec2(q.x * 1.92 - q.y * 0.58 + 3.1, q.x * 0.58 + q.y * 1.92 - 1.7);
        amp *= 0.50;
    }
    return clamp(val, 0.0, 1.0);
}
vec3 euv_sky(vec3 ray, vec3 sun_dir, vec2 sun_az_dir, float sim_time, bool high_detail) {
    float ry = max(ray.y, 0.0);
    float sdot = clamp(dot(ray, sun_dir), 0.0, 1.0);
    float sp2 = sdot * sdot;
    float sp4 = sp2 * sp2;
    float sp16 = sp4 * sp4 * sp4 * sp4;
    float sp64 = sp16 * sp16 * sp16 * sp16;
    float az_d = clamp(dot(normalize(vec2(ray.x, ray.z)), sun_az_dir), 0.0, 1.0);
    float az2 = az_d * az_d;
    float sun_az_glow = az2 * az2;
    float disk = smoothstep(0.99910, 0.99968, sdot);
    float horiz_band = exp(-ry * 7.5);
    vec3 horiz_mist = vec3(0.56, 0.70, 0.76)
        + vec3(0.62, 0.34, 0.08) * (sun_az_glow * (0.45 + 0.55 * horiz_band))
        + vec3(0.85, 0.58, 0.24) * sp16
        + vec3(1.25, 0.96, 0.58) * sp64;
    vec3 mid_sky = high_detail ? vec3(0.11, 0.30, 0.42) : vec3(0.12, 0.32, 0.70);
    vec3 zenith = high_detail ? vec3(0.042, 0.165, 0.275) : vec3(0.050, 0.175, 0.52);
    float sky_t1 = smoothstep(0.0, 0.22, ry);
    float sky_t2 = smoothstep(0.14, 0.68, ry);
    vec3 base_sky = mix(mix(horiz_mist, mid_sky, sky_t1), zenith, sky_t2);
    base_sky += vec3(0.32, 0.22, 0.11) * (sp4 * (0.30 + 0.70 * horiz_band));
    float horiz_clear = smoothstep(0.014, 0.115, ry);
    float cloud_alpha = 0.0;
    if (horiz_clear > 0.001) {
        float t_shell = (sqrt(ry * ry + 0.08) - ry) * 25.0;
        vec2 base_c = ray.xz * (t_shell * 0.46) + vec2(sim_time * 0.016, -sim_time * 0.009);
        float warp_a = euv_noise(base_c * 0.48 + vec2(3.1, -1.4));
        float warp_b = high_detail ? euv_noise(base_c * 1.35 + vec2(-2.3, 5.7)) : 0.5;
        vec2 c_uv = base_c + vec2((warp_a - 0.5) * 0.72 + (warp_b - 0.5) * 0.24, (warp_a - 0.5) * 0.72 - (warp_b - 0.5) * 0.24);
        float cloud_raw = euv_cloud_fbm(c_uv, high_detail ? 4 : 2);
        float puff_a = high_detail ? (1.0 - abs(euv_noise(c_uv * 2.4 + vec2(8.4, -3.9)) * 2.0 - 1.0)) : 0.0;
        float puff_b = high_detail ? (1.0 - abs(euv_noise(c_uv * 5.2 + vec2(1.9, -2.6)) * 2.0 - 1.0)) : 0.0;
        float cloud_field = clamp(cloud_raw * 0.76 + puff_a * 0.16 + puff_b * 0.08, 0.0, 1.0);
        float clearing = sun_az_glow * exp(-ry * 5.5) * 0.20;
        float cloud_cov = smoothstep(0.50, 0.76, cloud_field - clearing);
        float light_sample = high_detail ? euv_cloud_fbm(c_uv + sun_dir.xz * 0.22, 3) : (cloud_field * 0.92);
        float sun_lit = clamp((cloud_field - light_sample) * 3.1 + 0.54, 0.0, 1.0);
        float puff_top = smoothstep(0.48, 0.84, cloud_field);
        float edge_thin = (1.0 - cloud_cov * 0.55) * smoothstep(0.04, 0.42, cloud_cov);
        float silver_gold = (edge_thin * (0.34 + 0.90 * sp4) + (1.0 - cloud_cov) * sp16 * 0.35) * (0.40 + 0.60 * sun_lit);
        vec3 cloud_shadow = vec3(0.32, 0.44, 0.58);
        vec3 cloud_bright = vec3(1.22, 1.18, 1.08);
        vec3 cloud_col = mix(cloud_shadow, cloud_bright, clamp(sun_lit * 0.72 + puff_top * 0.28, 0.0, 1.0));
        cloud_col += vec3(1.18, 0.94, 0.62) * (silver_gold * 0.56);
        float dist_aerial = smoothstep(0.015, 0.17, ry);
        cloud_col = mix(horiz_mist, cloud_col, dist_aerial);
        cloud_alpha = clamp(cloud_cov * 0.90 * horiz_clear, 0.0, 0.92);
        base_sky = mix(base_sky, cloud_col, cloud_alpha);
    }
    if (high_detail) {
        float sun_trans = 1.0 - cloud_alpha * 0.45;
        float corona_inner = exp(-(1.0 - sdot) * 420.0);
        float corona_outer = exp(-(1.0 - sdot) * 42.0);
        base_sky += (vec3(4.6, 3.95, 2.95) * disk + vec3(1.35, 1.02, 0.60) * corona_inner + vec3(0.32, 0.24, 0.13) * corona_outer) * sun_trans;
    }
    return base_sky;
}
vec2 euv_coastal_island_h(float ax) {
    float far_h = 0.0;
    if (ax < 0.08) {
        float dl_f = (ax + 0.62) * (1.0 / 0.34);
        far_h = 0.072 * exp(-dl_f * dl_f) * (0.68 + 0.22 * sin(ax * 11.5 + 1.2) + 0.10 * cos(ax * 23.0 - 0.7));
    }
    if (ax > -0.10) {
        float dr_f = (ax - 0.50) * (1.0 / 0.34);
        far_h = max(far_h, 0.070 * exp(-dr_f * dr_f) * (0.66 + 0.24 * sin(ax * 10.2 - 0.8) + 0.10 * cos(ax * 21.5 + 1.4)));
    }
    float near_h = 0.0;
    if (ax < -0.04) {
        float dl_n = (ax + 0.48) * (1.0 / 0.22);
        near_h = 0.042 * exp(-dl_n * dl_n) * (0.65 + 0.23 * sin(ax * 14.5 + 0.9) + 0.12 * cos(ax * 29.0 - 0.4));
    }
    if (ax > -0.05) {
        float dr_n = (ax - 0.34) * (1.0 / 0.24);
        float db_n = (ax - 0.165) * (1.0 / 0.052);
        float di_n = (ax - 0.025) * (1.0 / 0.022);
        float near_r = max(
            0.048 * exp(-dr_n * dr_n) * (0.66 + 0.22 * sin(ax * 15.0 - 1.1) + 0.12 * cos(ax * 31.0 + 0.5)),
            0.0265 * exp(-db_n * db_n) * (0.90 + 0.10 * cos(ax * 46.0))
        );
        float islet = 0.013 * exp(-di_n * di_n) * (0.78 + 0.22 * cos(ax * 95.0));
        near_h = max(near_h, max(near_r, islet));
    }
    return vec2(far_h, near_h);
}
float euv_ocean_swell_h(vec2 wp, float sim_time, float agitation) {
    const float INV_TAU = 0.15915494;
    const float TAU = 6.283185307;
    float t0 = fract(sim_time * 0.84 * INV_TAU);
    float ph0 = (fract(dot(wp, vec2(0.2419, 0.9703)) * 0.56 * INV_TAU) + t0) * TAU;
    float h0 = 0.245 * (exp(-(1.0 - sin(ph0)) * 0.85) - 0.42);
    float t1 = fract(sim_time * 1.12 * INV_TAU);
    float ph1 = (fract(dot(wp, vec2(-0.5592, 0.8290)) * 0.94 * INV_TAU) + t1) * TAU;
    float h1 = 0.172 * (exp(-(1.0 - sin(ph1)) * 0.85) - 0.42);
    return (h0 + h1) * agitation;
}
vec3 euv_coastal_horizon(vec3 base_sky, vec3 ray, float sim_time, float d_rx, float d_ry) {
    float ry = ray.y;
    if (ry >= 0.095 || ray.z <= 0.15) {
        return base_sky;
    }
    float ax = ray.x / max(ray.z, 0.25);
    vec2 ih = euv_coastal_island_h(ax);
    vec2 ih_p = euv_coastal_island_h(ax + 0.008);
    float far_h = ih.x;
    float near_h = ih.y;
    float aa_y = max(d_ry * 1.25, 0.0012);
    float aa_x = max(d_rx * 1.25, 0.0012);
    vec3 horiz_mist = vec3(0.58, 0.72, 0.78);
    vec3 col = base_sky;
    if (far_h > 0.002 && ry < far_h + aa_y * 2.0) {
        float far_mask = 1.0 - smoothstep(far_h - aa_y, far_h + aa_y, ry);
        float far_slope = clamp((ih_p.x - far_h) * 42.0, -1.0, 1.0);
        float far_sun = clamp(0.50 + far_slope * 0.45, 0.15, 1.0);
        vec3 far_col = mix(vec3(0.18, 0.30, 0.48), vec3(0.44, 0.52, 0.62), far_sun);
        float far_haze = 0.46 - 0.14 * smoothstep(0.0, 0.07, ry);
        far_col = mix(far_col, horiz_mist, far_haze);
        col = mix(col, far_col, far_mask);
    }
    if (near_h > 0.002 && ry < near_h + aa_y * 2.0) {
        float near_mask = 1.0 - smoothstep(near_h - aa_y, near_h + aa_y, ry);
        float near_slope = clamp((ih_p.y - near_h) * 48.0, -1.0, 1.0);
        float sun_side = clamp(0.50 + near_slope * 0.48, 0.12, 1.0);
        float cliff_band = smoothstep(0.003, 0.014, ry);
        float ridge_tex = 0.85 + 0.15 * sin(ax * 76.0 + ry * 155.0);
        vec3 beach_sand = vec3(0.74, 0.66, 0.50);
        vec3 cliff_warm = vec3(0.32, 0.42, 0.28) * ridge_tex;
        vec3 cliff_cool = vec3(0.15, 0.27, 0.34) * ridge_tex;
        vec3 land_col = mix(beach_sand, mix(cliff_cool, cliff_warm, sun_side), cliff_band);
        float surf_line = (1.0 - smoothstep(0.0005, 0.0028, ry)) * 0.55;
        land_col = mix(land_col, vec3(0.95, 0.97, 1.0), surf_line);
        float near_haze = 0.26 - 0.08 * smoothstep(0.0, 0.048, ry);
        land_col = mix(land_col, horiz_mist, near_haze);
        col = mix(col, land_col, near_mask);
    }
    float lh_ax = 0.165;
    float lh_dx = abs(ax - lh_ax);
    if (lh_dx < 0.032 && ry > 0.016 && ry < 0.072) {
        float lh_base = 0.0235;
        float lh_top = 0.049;
        float lh_t = clamp((ry - lh_base) / (lh_top - lh_base), 0.0, 1.0);
        float tw = mix(0.0046, 0.0026, lh_t);
        float tower_x = 1.0 - smoothstep(max(tw - aa_x, 0.0), tw + aa_x, lh_dx);
        float tower_y = smoothstep(lh_base - aa_y, lh_base + aa_y, ry) * (1.0 - smoothstep(lh_top - aa_y, lh_top + aa_y, ry));
        float tower_m = tower_x * tower_y;
        if (tower_m > 0.001) {
            float red_band = smoothstep(0.30, 0.36, lh_t) * (1.0 - smoothstep(0.46, 0.52, lh_t))
                + smoothstep(0.68, 0.74, lh_t) * (1.0 - smoothstep(0.84, 0.90, lh_t));
            float sun_f = clamp(0.72 - (ax - lh_ax) * 90.0, 0.44, 1.0);
            vec3 lh_col = mix(vec3(0.92, 0.93, 0.94), vec3(0.76, 0.14, 0.11), clamp(red_band, 0.0, 1.0)) * sun_f;
            lh_col = mix(lh_col, horiz_mist, 0.14);
            col = mix(col, lh_col, tower_m);
        }
        vec2 b_off = vec2((ax - lh_ax) * 1.15, ry - 0.0495);
        float b_d2 = dot(b_off, b_off);
        float pulse = 0.80 + 0.25 * sin(sim_time * 2.2);
        float beacon_core = exp(-b_d2 * 48000.0);
        float beacon_halo = exp(-b_d2 * 8000.0);
        col += (vec3(2.6, 2.05, 0.95) * beacon_core + vec3(0.90, 0.66, 0.28) * beacon_halo) * pulse;
    }
    float sb_ax = -0.052 + 0.003 * sin(sim_time * 0.16);
    float sb_dx = ax - sb_ax;
    if (abs(sb_dx) < 0.020 && ry < 0.030) {
        float bob = 0.00035 * sin(sim_time * 1.45);
        float sy = ry - bob;
        float hull_m = (1.0 - smoothstep(max(0.0082 - aa_x, 0.0), 0.0082 + aa_x, abs(sb_dx)))
            * smoothstep(0.0002, 0.0012, sy)
            * (1.0 - smoothstep(0.0042 - aa_y, 0.0042 + aa_y, sy));
        float sail_t = clamp((sy - 0.0038) / 0.0175, 0.0, 1.0);
        float sail_l = -0.0012 - (1.0 - sail_t) * 0.0052;
        float sail_r = 0.0012 + (1.0 - sail_t) * 0.0072;
        float sail_m = smoothstep(sail_l - aa_x, sail_l + aa_x, sb_dx)
            * (1.0 - smoothstep(sail_r - aa_x, sail_r + aa_x, sb_dx))
            * smoothstep(0.0038 - aa_y, 0.0038 + aa_y, sy)
            * (1.0 - smoothstep(0.0212 - aa_y, 0.0212 + aa_y, sy));
        vec3 hull_col = mix(vec3(0.16, 0.22, 0.30), horiz_mist, 0.22);
        vec3 sail_col = mix(vec3(1.08, 1.03, 0.95), horiz_mist, 0.18);
        col = mix(col, hull_col, hull_m);
        col = mix(col, sail_col, sail_m);
    }
    return col;
}
void main() {
    const float INV_TAU = 0.15915494;
    const float TAU = 6.283185307;
    float sim_time = u_time_agit_hor_aspect.x;
    float agitation = max(u_time_agit_hor_aspect.y, 1.0);
    float horizon_v = u_time_agit_hor_aspect.z;
    float aspect = max(u_time_agit_hor_aspect.w, 0.25);
    float roll_cos = u_roll_boat_uv.x;
    float roll_sin = u_roll_boat_uv.y;
    vec2 boat_uv = u_roll_boat_uv.zw;
    vec3 sun_dir = normalize(u_sun_dir_wake.xyz);
    vec2 sun_az_dir = normalize(vec2(sun_dir.x, sun_dir.z));
    float wake_active = u_sun_dir_wake.w;
    float raw_dx = (v_uv.x - 0.5) * 2.0 * aspect;
    float raw_dy = (horizon_v - v_uv.y) * 2.0;
    float rx_u = raw_dx * roll_cos + raw_dy * roll_sin;
    float ry_u = -raw_dx * roll_sin + raw_dy * roll_cos;
    vec3 ray_u = vec3(rx_u, ry_u, 1.62);
    float inv_rlen = inversesqrt(dot(ray_u, ray_u));
    vec3 ray = ray_u * inv_rlen;
    float inv_res_y = max(abs(dFdy(v_uv.y)), 1.0 / 16384.0);
    float d_ry = (2.0 * inv_res_y) * inv_rlen * max(1.0 - ray.y * ray.y, 0.15);
    float d_rx = (2.0 * inv_res_y) * inv_rlen;
    vec3 hdr;
    if (ray.y >= 0.0) {
        hdr = euv_sky(ray, sun_dir, sun_az_dir, sim_time, true);
        hdr = euv_coastal_horizon(hdr, ray, sim_time, d_rx, d_ry);
    } else {
        float neg_ry_true = max(-ray.y, 1.0e-4);
        float dist_true = 1.42 / neg_ry_true;
        float dist = min(dist_true, 600.0);
        float px_span_z = dist_true * (d_ry / neg_ry_true);
        float px_span_x = dist_true * d_rx;
        float px_span = max(px_span_z, px_span_x);
        vec2 wp0 = ray.xz * dist;
        if (dist_true < 44.0) {
            float p_fade = 1.0 - smoothstep(16.0, 44.0, dist_true);
            float h_p1 = euv_ocean_swell_h(wp0, sim_time, agitation) * p_fade;
            dist = clamp((1.42 - h_p1) / neg_ry_true, 0.45, 600.0);
            wp0 = ray.xz * dist;
            if (dist_true < 26.0) {
                float h_p2 = euv_ocean_swell_h(wp0, sim_time, agitation) * p_fade;
                dist = clamp((1.42 - mix(h_p1, h_p2, 0.62)) / neg_ry_true, 0.45, 600.0);
                wp0 = ray.xz * dist;
            }
        }
        float set_ph = (fract(dot(wp0, vec2(0.32, 0.947)) * 0.115 * INV_TAU) + fract(sim_time * 0.28 * INV_TAU)) * TAU;
        float swell_set = 0.80 + 0.30 * sin(set_ph);
        vec2 fwd_xz = normalize(ray.xz);
        vec2 right_xz = vec2(fwd_xz.y, -fwd_xz.x);
        vec2 wp = wp0;
        float h = 0.0;
        vec2 grad = vec2(0.0);
        float crest_acc = 0.0;
        float normal_var = 0.0009;
        for (int wi = 0; wi < 8; ++wi) {
            vec4 wave = EUV_OCEAN_WAVES[wi];
            vec2 wdir = wave.xy;
            float freq = wave.z;
            float group_k = (wi < 3) ? swell_set : 1.0;
            float amp = wave.w * agitation * group_k;
            float w_span = max(max(abs(dot(wdir, fwd_xz)) * px_span_z, abs(dot(wdir, right_xz)) * px_span_x), px_span_z * 0.55);
            float nyq = freq * w_span * 2.25;
            float aa_w = (nyq < 4.5) ? exp(-0.5 * nyq * nyq) : 0.0;
            float rms_s = wave.w * min(agitation, 1.45) * freq;
            normal_var += 0.10 * rms_s * rms_s * (1.0 - aa_w * aa_w);
            if (aa_w > 0.0005) {
                float t_ph = fract(sim_time * EUV_OCEAN_SPEEDS[wi] * INV_TAU);
                float s_ph = fract(dot(wp, wdir) * freq * INV_TAU);
                float ph = (s_ph + t_ph) * TAU;
                float sp = sin(ph);
                float cp = cos(ph);
                float steep_k = 0.52 + 0.68 * aa_w;
                float sharp_e = exp(-(1.0 - sp) * steep_k);
                h += amp * (sharp_e - 0.42) * aa_w;
                float slope = amp * freq * steep_k * sharp_e * cp * aa_w;
                grad += wdir * slope;
                crest_acc += sharp_e * amp * aa_w;
                wp += wdir * (cp * amp * 0.92 * aa_w);
            }
        }
        float near_steep = 1.0 + 0.46 * exp(-dist * 0.068);
        grad *= near_steep;
        float cap_nyq1 = px_span * 14.5 * 2.25;
        float cap_aa1 = (cap_nyq1 < 4.5) ? exp(-0.5 * cap_nyq1 * cap_nyq1) : 0.0;
        normal_var += 0.0014 * (1.0 - cap_aa1 * cap_aa1) * min(agitation, 1.45);
        if (cap_aa1 > 0.001) {
            float p1a = (fract(dot(wp0, vec2( 0.8572,  0.5150)) * 14.2 * INV_TAU) + fract(sim_time * 3.7 * INV_TAU)) * TAU;
            float p1b = (fract(dot(wp0, vec2(-0.4226,  0.9063)) * 18.7 * INV_TAU) + fract(sim_time * 4.4 * INV_TAU)) * TAU;
            float p1c = (fract(dot(wp0, vec2( 0.9613, -0.2756)) * 23.4 * INV_TAU) + fract(sim_time * 5.1 * INV_TAU)) * TAU;
            float g1a = cos(p1a) * 0.064 * cap_aa1 * agitation;
            float g1b = cos(p1b) * 0.052 * cap_aa1 * agitation;
            float g1c = cos(p1c) * 0.042 * cap_aa1 * agitation;
            grad += vec2(0.8572, 0.5150) * g1a + vec2(-0.4226, 0.9063) * g1b + vec2(0.9613, -0.2756) * g1c;
        }
        float cap_nyq2 = px_span * 34.0 * 2.25;
        float cap_aa2 = (cap_nyq2 < 4.5) ? exp(-0.5 * cap_nyq2 * cap_nyq2) : 0.0;
        normal_var += 0.0008 * (1.0 - cap_aa2 * cap_aa2) * min(agitation, 1.45);
        if (cap_aa2 > 0.001) {
            float p2a = (fract(dot(wp0, vec2(-0.7547, -0.6561)) * 31.8 * INV_TAU) + fract(sim_time * 6.2 * INV_TAU)) * TAU;
            float p2b = (fract(dot(wp0, vec2( 0.2924, -0.9563)) * 41.3 * INV_TAU) + fract(sim_time * 7.3 * INV_TAU)) * TAU;
            float g2a = cos(p2a) * 0.036 * cap_aa2 * agitation;
            float g2b = cos(p2b) * 0.026 * cap_aa2 * agitation;
            grad += vec2(-0.7547, -0.6561) * g2a + vec2(0.2924, -0.9563) * g2b;
        }
        normal_var = clamp(normal_var + min(px_span * 0.006, 0.016), 0.0005, 0.036);
        float wake_foam = 0.0;
        if (wake_active > 0.001) {
            vec2 wake_uv = clamp(v_uv, vec2(0.5 / 96.0, 0.5 / 72.0), vec2(1.0 - 0.5 / 96.0, 1.0 - 0.5 / 72.0));
            vec4 wk = texture(u_wake_tex, wake_uv);
            vec2 edge_d = min(v_uv, vec2(1.0) - v_uv);
            float edge_win = smoothstep(0.0, 0.045, edge_d.x) * smoothstep(0.0, 0.045, edge_d.y);
            float w_gain = clamp(wake_active, 0.0, 1.0) * edge_win;
            vec2 flow = clamp((wk.rg - vec2(128.0 / 255.0)) * 4.0 * 0.22, vec2(-0.20), vec2(0.20)) * w_gain;
            grad += flow;
            wake_foam = wk.b * w_gain;
        }
        vec2 raw_bd = vec2((v_uv.x - boat_uv.x) * aspect, v_uv.y - boat_uv.y);
        vec2 rot_bd = vec2(raw_bd.x * roll_cos - raw_bd.y * roll_sin, raw_bd.x * roll_sin + raw_bd.y * roll_cos);
        vec2 boat_local = vec2(rot_bd.x / 0.145, rot_bd.y / 0.036);
        float boat_ring_d2 = dot(boat_local, boat_local);
        float rip_nyq = px_span * 16.5 * 1.8;
        float rip_aa = (rip_nyq < 4.5) ? exp(-0.5 * rip_nyq * rip_nyq) : 0.0;
        if (boat_ring_d2 > 0.30 && boat_ring_d2 < 12.8) {
            float r_hull = sqrt(boat_ring_d2);
            float rip_env = exp(-max(r_hull - 0.85, 0.0) * 1.08) * smoothstep(0.55, 0.92, r_hull);
            float rip_ph = (fract(r_hull * 16.5 * INV_TAU) - fract(sim_time * 4.2 * INV_TAU)) * TAU;
            float rip_wave = cos(rip_ph) * 0.36 * rip_env * rip_aa;
            float inv_rh = 1.0 / max(r_hull, 0.1);
            grad += vec2(boat_local.x * inv_rh * rip_wave, boat_local.y * inv_rh * rip_wave * 1.38);
        }
        if (boat_local.x > 0.32 && boat_local.x < 5.6 && abs(boat_local.y - 0.22) < 3.4) {
            float kx = boat_local.x - 0.32;
            float ky = boat_local.y - 0.22;
            float arm_y = 0.42 + kx * 0.36;
            float d_arm = abs(abs(ky) - arm_y);
            float k_env = exp(-d_arm * d_arm * 3.8) * smoothstep(0.0, 0.45, kx) * exp(-kx * 0.42) * rip_aa;
            float k_ph = (fract((d_arm * 6.5 - kx * 1.9) * INV_TAU) - fract(sim_time * 3.4 * INV_TAU)) * TAU;
            float k_sl = cos(k_ph) * 0.26 * k_env;
            grad += vec2(-0.34, sign(ky)) * k_sl;
        }
        vec3 norm = normalize(vec3(-grad.x, 1.0, -grad.y));
        vec3 dndx = dFdx(norm);
        vec3 dndy = dFdy(norm);
        float dn_sq = dot(dndx, dndx) + dot(dndy, dndy);
        float screen_nvar = (neg_ry_true > d_ry * 2.5 && dn_sq < 64.0)
            ? min(dn_sq * 0.48, 0.16) * smoothstep(d_ry * 2.5, d_ry * 5.0, neg_ry_true)
            : 0.0;
        float eff_nvar = clamp(normal_var + screen_nvar, 0.0012, 0.18);
        vec3 view_dir = -ray;
        float ndv = clamp(dot(norm, view_dir), 0.005, 1.0);
        float om_nv = 1.0 - ndv;
        float om_nv2 = om_nv * om_nv;
        float fresnel = clamp(0.0204 + (0.94 - min(eff_nvar * 1.6, 0.34)) * (om_nv2 * om_nv2 * om_nv), 0.022, 0.90);
        vec3 refl_dir = reflect(ray, norm);
        refl_dir.y = max(refl_dir.y, 0.008);
        vec3 sky_refl = euv_sky(normalize(refl_dir), sun_dir, sun_az_dir, sim_time, false);
        float ax_w = ray.x / max(ray.z, 0.25);
        if (dist > 34.0) {
            vec2 ih_w = euv_coastal_island_h(ax_w + grad.x * 0.045);
            float isl_ref_h = max(ih_w.y, ih_w.x * 0.55);
            float ref_band = (1.0 - smoothstep(0.0, isl_ref_h * 1.25 + 0.008, neg_ry_true)) * smoothstep(0.005, 0.026, isl_ref_h);
            sky_refl = mix(sky_refl, vec3(0.14, 0.26, 0.36), ref_band * 0.46);
        }
        vec2 cs_uv = (wp0 + sun_dir.xz * 22.0) * 0.026 + vec2(sim_time * 0.016, -sim_time * 0.009);
        float cs_n1 = euv_noise(cs_uv);
        float cs_n2 = 0.5 + 0.5 * sin(cs_uv.x * 4.3 + cs_uv.y * 2.9 + 1.4);
        float cs_cov = smoothstep(0.45, 0.74, cs_n1 * 0.74 + cs_n2 * 0.26);
        float cs_dist = smoothstep(4.0, 14.0, dist) * exp(-dist * 0.0038);
        float cloud_sun_vis = mix(1.0, 0.42, cs_cov * cs_dist);
        vec3 half_vec = normalize(view_dir + sun_dir);
        float ndl = max(dot(norm, sun_dir), 0.0);
        float ndh = max(dot(norm, half_vec), 0.0);
        float vdh = clamp(dot(view_dir, half_vec), 0.0, 1.0);
        float om_vh = 1.0 - vdh;
        float om_vh2 = om_vh * om_vh;
        float fresnel_h = 0.0204 + 0.9796 * (om_vh2 * om_vh2 * om_vh);
        float a1_sq = 0.0012 + eff_nvar * 0.56;
        float d1 = ndh * ndh * (a1_sq - 1.0) + 1.0;
        float ndf1 = a1_sq / max(3.14159265 * d1 * d1, 1.0e-5);
        float k1 = sqrt(a1_sq) * 0.5;
        float vis1 = 0.25 / max((ndv * (1.0 - k1) + k1) * (ndl * (1.0 - k1) + k1), 0.014);
        float a2_sq = 0.0068 + eff_nvar * 0.72;
        float d2 = ndh * ndh * (a2_sq - 1.0) + 1.0;
        float ndf2 = a2_sq / max(3.14159265 * d2 * d2, 1.0e-5);
        float k2 = sqrt(a2_sq) * 0.5;
        float vis2 = 0.25 / max((ndv * (1.0 - k2) + k2) * (ndl * (1.0 - k2) + k2), 0.034);
        float core_e = min(ndf1 * vis1 * ndl * fresnel_h, 11.0);
        float sheen_e = min(ndf2 * vis2 * ndl * fresnel_h, 2.0);
        vec3 spec = (vec3(1.62, 1.44, 1.16) * (core_e * 0.80) + vec3(1.32, 0.98, 0.62) * (sheen_e * 0.22)) * (0.50 + 0.50 * cloud_sun_vis);
        float crest_h = clamp((h + 0.12) * 1.95, 0.0, 1.0);
        float crest_thin = smoothstep(0.22, 0.88, crest_h);
        float trough_ao = 0.34 + 0.66 * smoothstep(-0.28, 0.22, h);
        vec3 sss_axis = normalize(-sun_dir + norm * 0.45);
        float sss_v = max(dot(view_dir, sss_axis), 0.0);
        float sss_back = sss_v * sss_v * sss_v;
        float sss_side = ndl * 0.36 + 0.14;
        float sss = (sss_back * 1.55 + sss_side * 0.42) * crest_thin * crest_thin * (1.0 - fresnel * 0.85) * exp(-dist * 0.026) * cloud_sun_vis;
        float shoal_w = 0.0;
        if (dist < 68.0) {
            float shoal_n = euv_noise(wp0 * 0.042 + vec2(-1.8, 3.4));
            float flank_bias = smoothstep(0.24, 0.88, abs(ax_w));
            shoal_w = smoothstep(0.52, 0.84, shoal_n + flank_bias * 0.20) * exp(-dist * 0.020) * (1.0 - fresnel * 0.82);
        }
        vec3 deep_trough = vec3(0.002, 0.038, 0.185);
        vec3 mid_sea = vec3(0.005, 0.138, 0.395);
        vec3 shoal_col = vec3(0.014, 0.335, 0.465);
        vec3 base_water = mix(deep_trough, mid_sea, trough_ao);
        base_water = mix(base_water, shoal_col, shoal_w * 0.48);
        vec3 sss_glow = vec3(0.018, 0.54, 0.50) * (sss * 1.25) + vec3(0.010, 0.28, 0.52) * (crest_thin * exp(-dist * 0.035) * 0.22 * cloud_sun_vis);
        vec3 deep_body = base_water * (0.62 + 0.38 * cloud_sun_vis) + sss_glow;
        float refl_weight = clamp(fresnel * 0.62 + 0.022, 0.022, 0.72);
        hdr = mix(deep_body, sky_refl, refl_weight) + spec;
        if (rot_bd.y > -0.01 && rot_bd.y < 0.24 && abs(rot_bd.x) < 0.22) {
            float ripple = sin(rot_bd.y * 95.0 - sim_time * 4.5 + grad.x * 6.0) * 0.012;
            float rx_b = abs(rot_bd.x + ripple);
            float y_fade = smoothstep(-0.01, 0.032, rot_bd.y) * (1.0 - smoothstep(0.10, 0.24, rot_bd.y));
            float tri_mask = (1.0 - smoothstep(0.02, 0.14, rx_b + max(rot_bd.y, 0.0) * 0.55)) * y_fade;
            hdr = mix(hdr, vec3(0.55, 0.52, 0.48), tri_mask * 0.32);
        }
        vec2 buoy_xz = vec2(-0.39 * aspect * 15.8 * (1.0 - 0.50 * abs(roll_sin)), 15.8);
        vec2 buoy_rel = wp0 - buoy_xz;
        float buoy_d2 = dot(buoy_rel, buoy_rel);
        if (buoy_d2 < 3.6) {
            float buoy_ao = (1.0 - smoothstep(0.08, 0.62, buoy_d2)) * 0.40;
            hdr *= (1.0 - buoy_ao);
            float b_ring = smoothstep(0.14, 0.45, buoy_d2) * (1.0 - smoothstep(0.55, 1.85, buoy_d2));
            float b_rip = 0.5 + 0.5 * cos(sqrt(buoy_d2) * 8.5 - sim_time * 3.8);
            hdr = mix(hdr, vec3(0.92, 0.96, 1.0), b_ring * b_rip * 0.48);
        }
        float foam_aa = 1.0 - smoothstep(0.045, 0.42, px_span);
        float crest_norm = crest_acc / max(agitation, 1.0);
        float crest_steep = smoothstep(0.20, 0.44, crest_norm) * smoothstep(0.03, 0.18, h);
        float agit_boost = 0.88 + 0.50 * clamp(agitation - 1.0, 0.0, 1.2);
        float cap_mask = crest_steep * agit_boost * foam_aa;
        float lang_aa = (1.0 - smoothstep(0.055, 0.44, px_span)) * exp(-dist * 0.020);
        float lang_mask = 0.0;
        if (lang_aa > 0.01) {
            vec2 l_uv = vec2((wp0.x * 0.92 - wp0.y * 0.39) * 0.46 + h * 0.50, (wp0.x * 0.39 + wp0.y * 0.92) * 0.060 - sim_time * 0.034);
            float l_n = euv_noise(l_uv);
            lang_mask = smoothstep(0.67, 0.85, l_n) * lang_aa * 0.28;
        }
        float raw_foam = clamp(cap_mask + lang_mask + wake_foam * 0.44, 0.0, 1.0);
        if (raw_foam > 0.015) {
            float fn1 = euv_noise(wp0 * 2.6 + vec2(-sim_time * 0.14, sim_time * 0.09));
            float fn2 = euv_noise(wp0 * 8.4 + vec2(sim_time * 0.24, -sim_time * 0.18));
            float lace = smoothstep(0.32, 0.70, fn1 * 0.54 + fn2 * 0.46 + cap_mask * 0.34);
            float foam_cov = clamp((cap_mask * lace * 0.86 + lang_mask * (0.48 + 0.52 * fn2) + wake_foam * 0.42) * (0.66 + 0.34 * cloud_sun_vis), 0.0, 0.88);
            vec3 foam_col = mix(vec3(0.46, 0.66, 0.78), vec3(0.98, 0.99, 1.0), clamp(0.46 + 0.54 * ndl, 0.0, 1.0) * (0.60 + 0.40 * cloud_sun_vis));
            hdr = mix(hdr, foam_col, foam_cov);
        }
        if (boat_local.x > -2.4 && boat_local.x < 5.6 && abs(boat_local.y) < 3.4) {
            if (boat_ring_d2 < 1.12) {
                float contact_ao = (1.0 - smoothstep(0.18, 1.06, boat_ring_d2)) * 0.46;
                hdr *= vec3(1.0 - contact_ao, 1.0 - contact_ao * 0.82, 1.0 - contact_ao * 0.72);
            }
            float foam_ny = boat_local.y - 0.16;
            float foam_d2 = boat_local.x * boat_local.x * 1.16 + foam_ny * foam_ny * 1.08;
            float rim_band = smoothstep(0.40, 0.80, foam_d2) * (1.0 - smoothstep(0.88, 1.56, foam_d2)) * smoothstep(-0.45, 0.24, boat_local.y);
            float aft_wake = smoothstep(0.20, 0.82, boat_local.x) * (1.0 - smoothstep(0.88, 1.68, boat_local.x)) * (1.0 - smoothstep(0.18, 1.24, abs(boat_local.y - 0.26))) * 0.54;
            float kx = max(boat_local.x - 0.28, 0.0);
            float ky = boat_local.y - 0.22;
            float arm_y = 0.44 + kx * 0.36;
            float d_arm = abs(abs(ky) - arm_y);
            float kelvin_arms = exp(-d_arm * d_arm * 4.6) * smoothstep(0.0, 0.45, kx) * exp(-kx * 0.42) * 0.84;
            float stern_trail = exp(-ky * ky * 3.4) * smoothstep(0.10, 0.58, kx) * exp(-kx * 0.50) * 0.70;
            float bx_c = boat_local.x + 0.98;
            float by_c = boat_local.y - 0.14;
            float bow_curl = exp(-(bx_c * bx_c * 4.2 + by_c * by_c * 3.0)) * 0.92;
            float collar_mask = clamp(rim_band + aft_wake + kelvin_arms + stern_trail + bow_curl, 0.0, 1.0);
            if (collar_mask > 0.02) {
                float lacy_a = euv_noise(wp0 * 8.2 + vec2(2.3, -1.7));
                float lacy_b = euv_noise(wp0 * 21.0 + vec2(-sim_time * 0.85, 4.1));
                float flecks = euv_noise(wp0 * 42.0 + vec2(7.1, -sim_time * 1.35));
                float foam_pat = smoothstep(0.38, 0.72, lacy_a * 0.42 + lacy_b * 0.36 + flecks * 0.22);
                float spray_dots = smoothstep(0.70, 0.86, flecks) * (rim_band + bow_curl) * 0.78 * (1.0 - smoothstep(0.015, 0.08, px_span));
                float hull_foam = clamp(collar_mask * foam_pat * 0.88 + spray_dots, 0.0, 0.92);
                hdr = mix(hdr, vec3(0.96, 0.98, 1.0), hull_foam);
            }
        }
        float horiz_blend = exp(-neg_ry_true * 115.0);
        float dist_haze = 1.0 - exp(-dist * 0.0018);
        float pixel_hor_aa = 1.0 - smoothstep(0.0, d_ry * 1.75, neg_ry_true);
        float atm_mix = clamp(max(max(horiz_blend, dist_haze), pixel_hor_aa), 0.0, 1.0);
        float atm_w = clamp((atm_mix - 0.002) * (1.0 / 0.998), 0.0, 1.0);
        if (atm_w > 0.0) {
            vec3 fog_col = euv_sky(normalize(vec3(ray.x, max(0.0005, d_ry * 0.5), ray.z)), sun_dir, sun_az_dir, sim_time, false);
            hdr = mix(hdr, fog_col, atm_w);
        }
    }
    out_color = vec4(euv_aces(hdr), 1.0);
}
"#;

/// Hardware GLSL ES 3.0 origami paper boat vertex shader.
pub(crate) const LINUX_GL_SCENE_BOAT_VERT: &str = "#version 300 es\n\
precision highp float;\n\
layout(location = 0) in vec4 a_pos_ndc;\n\
layout(location = 1) in vec4 a_color;\n\
out vec4 v_color;\n\
void main() {\n\
    float w = max(a_pos_ndc.w, 1.0e-4);\n\
    gl_Position = vec4(a_pos_ndc.xy * w, (a_pos_ndc.z * 2.0 - 1.0) * w, w);\n\
    v_color = a_color;\n\
}\n";

/// Hardware GLSL ES 3.0 origami paper boat fragment shader.
pub(crate) const LINUX_GL_SCENE_BOAT_FRAG: &str = "#version 300 es\n\
precision highp float;\n\
in vec4 v_color;\n\
out vec4 out_color;\n\
void main() {\n\
    out_color = v_color;\n\
}\n";

/// Hardware GLSL ES 3.0 world-space 3D atmospheric sky gradient and sun fragment shader.
pub(crate) const LINUX_GL_SCENE_3D_SKY_FRAG: &str = r#"#version 300 es
precision highp float;
in highp vec2 v_uv;
uniform highp mat4 u_view_proj;
uniform highp vec4 u_camera_pos;
uniform highp vec4 u_sun_dir_fog;
uniform highp vec4 u_sun_color_ambient;
uniform highp vec4 u_sky_horizon_tod;
uniform highp vec4 u_sky_zenith_aspect;
out vec4 out_color;
vec3 euv_aces(vec3 hdr) {
    vec3 x = max(hdr, vec3(0.0));
    vec3 num = x * (2.51 * x + 0.03);
    vec3 den = x * (2.43 * x + 0.59) + 0.14;
    return pow(clamp(num / max(den, vec3(0.0001)), vec3(0.0), vec3(1.0)), vec3(1.0 / 2.2));
}
float euv_sky_hash(vec2 cell) {
    vec2 p = fract(cell * vec2(0.1031, 0.1030));
    p += dot(p, p.yx + 33.33);
    return fract((p.x + p.y) * p.x);
}
float euv_sky_noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = euv_sky_hash(i);
    float b = euv_sky_hash(i + vec2(1.0, 0.0));
    float c = euv_sky_hash(i + vec2(0.0, 1.0));
    float d = euv_sky_hash(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
void main() {
    vec2 ndc = vec2(v_uv.x * 2.0 - 1.0, 1.0 - v_uv.y * 2.0);
    vec3 r0 = vec3(u_view_proj[0].x, u_view_proj[1].x, u_view_proj[2].x);
    vec3 r1 = vec3(u_view_proj[0].y, u_view_proj[1].y, u_view_proj[2].y);
    vec3 r3 = vec3(u_view_proj[0].w, u_view_proj[1].w, u_view_proj[2].w);
    vec3 c0 = cross(r1, r3);
    vec3 c1 = cross(r3, r0);
    vec3 c2 = cross(r0, r1);
    float det = dot(r0, c0);
    float sgn = (det < 0.0) ? -1.0 : 1.0;
    vec3 world_ray = normalize((c0 * ndc.x + c1 * ndc.y + c2) * sgn);
    vec3 sun_dir = normalize(u_sun_dir_fog.xyz);
    float ry = max(world_ray.y, 0.0);
    float sky_t = smoothstep(0.0, 0.62, pow(ry, 0.72));
    vec3 col = mix(u_sky_horizon_tod.rgb, u_sky_zenith_aspect.rgb, sky_t);
    if (world_ray.y < 0.0) {
        col = mix(u_sky_horizon_tod.rgb, u_sky_horizon_tod.rgb * 0.52, smoothstep(0.0, 0.35, -world_ray.y));
    }
    float sdot = clamp(dot(world_ray, sun_dir), 0.0, 1.0);
    float sp2 = sdot * sdot;
    float sp4 = sp2 * sp2;
    float sp8 = sp4 * sp4;
    float horiz_band = exp(-ry * 6.5);
    float sun_aureole = exp(-(1.0 - sdot) * 28.0);
    float sun_corona = exp(-(1.0 - sdot) * 420.0);
    float sun_disk = smoothstep(0.99925, 0.99975, sdot);
    float above_hor = smoothstep(-0.02, 0.03, world_ray.y);
    col += u_sun_color_ambient.rgb * (sp8 * 0.36 * horiz_band + sun_aureole * 0.42 + (sun_corona * 0.95 + sun_disk * 3.4) * above_hor);
    if (ry > 0.012) {
        vec2 dome_uv = world_ray.xz / (ry + 0.14);
        float drift_t = u_sky_horizon_tod.w;
        vec2 cloud_p = dome_uv * 0.72 + vec2(drift_t * 0.018, -drift_t * 0.009);
        float warp = euv_sky_noise(cloud_p * 0.85 + vec2(2.3, -1.9)) - 0.5;
        vec2 wp = cloud_p + vec2(warp * 0.58, warp * 0.44);
        float fbm_base = euv_sky_noise(wp * 1.15) * 0.54
            + euv_sky_noise(wp * 2.45 + vec2(5.1, -3.4)) * 0.30
            + euv_sky_noise(wp * 5.20 - vec2(2.7, 6.3)) * 0.16;
        vec2 sun_off = sun_dir.xz * 0.18;
        float fbm_sun = euv_sky_noise((wp + sun_off) * 1.15) * 0.56
            + euv_sky_noise((wp + sun_off) * 2.45 + vec2(5.1, -3.4)) * 0.30
            + euv_sky_noise((wp + sun_off) * 5.20 - vec2(2.7, 6.3)) * 0.14;
        float hor_fade = smoothstep(0.015, 0.15, ry) * (1.0 - smoothstep(0.68, 0.96, ry));
        float cum_cov = smoothstep(0.47, 0.75, fbm_base) * hor_fade;
        float sun_luma = clamp(dot(u_sun_color_ambient.rgb, vec3(0.30, 0.55, 0.15)), 0.18, 1.45);
        float lit_grad = clamp((fbm_base - fbm_sun) * 3.4 + 0.48 + sp4 * 0.42, 0.0, 1.0);
        vec3 cloud_shadow = mix(u_sky_zenith_aspect.rgb * 0.52 + u_sky_horizon_tod.rgb * 0.40, vec3(0.38, 0.46, 0.60) * sun_luma, 0.55);
        vec3 cloud_top = vec3(1.18, 1.16, 1.14) * sun_luma + u_sun_color_ambient.rgb * (0.24 + 0.48 * sp4);
        vec3 cum_col = mix(cloud_shadow, cloud_top, lit_grad);
        col = mix(col, cum_col, cum_cov * 0.72);
        vec2 cir_p = dome_uv * vec2(1.95, 0.62) + vec2(drift_t * 0.028, drift_t * 0.008);
        float cir_n = euv_sky_noise(cir_p * 1.8) * 0.65 + euv_sky_noise(cir_p * 4.1 + vec2(3.7, -1.5)) * 0.35;
        float cir_mask = smoothstep(0.56, 0.84, cir_n) * smoothstep(0.10, 0.28, ry) * (1.0 - smoothstep(0.60, 0.94, ry));
        vec3 cir_col = u_sky_horizon_tod.rgb * 0.85 + vec3(0.85, 0.88, 0.94) * (0.45 * sun_luma) + u_sun_color_ambient.rgb * (0.18 * sp4);
        col = mix(col, cir_col, cir_mask * 0.24);
    }
    out_color = vec4(euv_aces(col * u_camera_pos.w), 1.0);
}
"#;

/// Hardware GLSL ES 3.0 instanced 3D PBR open-world vertex shader.
pub(crate) const LINUX_GL_SCENE_3D_MESH_VERT: &str = r#"#version 300 es
precision highp float;
layout(location = 0) in vec4 a_pos;
layout(location = 1) in vec4 a_norm;
layout(location = 2) in vec4 a_submat;
layout(location = 3) in vec4 a_m0;
layout(location = 4) in vec4 a_m1;
layout(location = 5) in vec4 a_m2;
layout(location = 6) in vec4 a_m3;
layout(location = 7) in vec4 a_albedo_met;
layout(location = 8) in vec4 a_emissive_rough;
uniform highp mat4 u_view_proj;
out highp vec3 v_world_pos;
out highp vec3 v_world_norm;
out highp vec4 v_base_met;
out highp vec4 v_emissive_rough;
out highp float v_submat_mode;
out highp float v_detail_mode;
void main() {
    float detail_mode = a_m3.w;
    vec4 m3_fixed = vec4(a_m3.xyz, 1.0);
    mat4 model = mat4(a_m0, a_m1, a_m2, m3_fixed);
    vec4 wp = model * vec4(a_pos.xyz, 1.0);
    gl_Position = u_view_proj * wp;
    vec3 c0 = a_m0.xyz;
    vec3 c1 = a_m1.xyz;
    vec3 c2 = a_m2.xyz;
    mat3 cof = mat3(cross(c1, c2), cross(c2, c0), cross(c0, c1));
    v_world_pos = wp.xyz;
    v_world_norm = normalize(cof * a_norm.xyz);
    v_base_met = (a_submat.w > 0.5)
        ? vec4(a_submat.rgb, a_albedo_met.w)
        : vec4(a_albedo_met.rgb * a_submat.rgb, a_albedo_met.w);
    v_emissive_rough = a_emissive_rough;
    v_submat_mode = a_submat.w;
    v_detail_mode = detail_mode;
}
"#;

/// Hardware GLSL ES 3.0 flagship instanced 3D PBR open-world fragment shader with analytical ray-traced Fresnel reflections, clearcoat, contact AO, multi-bounce GI, and volumetric haze.
pub(crate) const LINUX_GL_SCENE_3D_MESH_FRAG: &str = r#"#version 300 es
precision highp float;
in highp vec3 v_world_pos;
in highp vec3 v_world_norm;
in highp vec4 v_base_met;
in highp vec4 v_emissive_rough;
in highp float v_submat_mode;
in highp float v_detail_mode;
uniform highp vec4 u_camera_pos;
uniform highp vec4 u_sun_dir_fog;
uniform highp vec4 u_sun_color_ambient;
uniform highp vec4 u_sky_horizon_tod;
uniform highp vec4 u_sky_zenith_aspect;
out vec4 out_color;
vec3 euv_aces(vec3 hdr) {
    vec3 x = max(hdr, vec3(0.0));
    vec3 num = x * (2.51 * x + 0.03);
    vec3 den = x * (2.43 * x + 0.59) + 0.14;
    return pow(clamp(num / max(den, vec3(0.0001)), vec3(0.0), vec3(1.0)), vec3(1.0 / 2.2));
}
float euv_hash(vec2 cell) {
    vec2 p = fract(cell * vec2(0.1031, 0.1030));
    p += dot(p, p.yx + 33.33);
    return fract((p.x + p.y) * p.x);
}
float euv_noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = euv_hash(i);
    float b = euv_hash(i + vec2(1.0, 0.0));
    float c = euv_hash(i + vec2(0.0, 1.0));
    float d = euv_hash(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float euv_box_pulse(float x, float a, float b, float fw) {
    float w = b - a;
    float f = max(fw, 1.0e-4);
    float x0 = x - 0.5 * f;
    float x1 = x + 0.5 * f;
    float i0 = floor(x0) * w + clamp(fract(x0) - a, 0.0, w);
    float i1 = floor(x1) * w + clamp(fract(x1) - a, 0.0, w);
    return clamp((i1 - i0) / f, 0.0, 1.0);
}
float euv_box_band(float x, float lo, float hi, float fw) {
    float f = max(fw, 1.0e-4);
    float x0 = x - 0.5 * f;
    float x1 = x + 0.5 * f;
    return clamp((min(x1, hi) - max(x0, lo)) / f, 0.0, 1.0);
}
float euv_box_step(float x, float edge, float fw) {
    float f = max(fw, 1.0e-4);
    return clamp((x - edge) / f + 0.5, 0.0, 1.0);
}
float euv_loop_arc_s(float wpx, float wpz) {
    if (wpz < -140.0) {
        float ry = -(wpz + 140.0);
        float rx = -(wpx - 64.0);
        float rx_safe = (abs(rx) < 1.0e-5 && abs(ry) < 1.0e-5) ? 1.0 : rx;
        return 140.0 + 64.0 * atan(ry, rx_safe);
    }
    if (wpz > 140.0) {
        float ry = wpz - 140.0;
        float rx = wpx - 64.0;
        float rx_safe = (abs(rx) < 1.0e-5 && abs(ry) < 1.0e-5) ? 1.0 : rx;
        return 621.06193 + 64.0 * atan(ry, rx_safe);
    }
    return (wpx <= 64.0) ? -wpz : (481.06193 + wpz);
}
vec3 euv_trace_city_reflection(
    vec3 pos,
    vec3 refl_dir,
    vec3 sun_dir,
    vec3 sky_hor,
    vec3 sky_zen,
    vec3 sun_col,
    float roughness
) {
    vec3 env_col = mix(sky_hor, sky_zen, smoothstep(0.0, 0.72, clamp(refl_dir.y, 0.0, 1.0)));
    float s_dot = max(dot(refl_dir, sun_dir), 0.0);
    float s2 = s_dot * s_dot;
    float s4 = s2 * s2;
    float s16 = s4 * s4 * s4 * s4;
    float sun_lobe = mix(s16 * s16 * s16, s4, clamp(roughness * 1.4, 0.0, 1.0));
    env_col += sun_col * (sun_lobe * (1.85 - roughness * 1.25));
    float o_spine_z = clamp(pos.z, -140.0, 140.0);
    vec2 o_rel = vec2(pos.x - 64.0, pos.z - o_spine_z);
    float o_d = length(o_rel);
    float o_u = o_d - 64.0;
    vec2 o_rad = (o_d > 1.0e-4) ? (o_rel / o_d) : vec2(-1.0, 0.0);
    float r_rad = dot(refl_dir.xz, o_rad);
    if (abs(r_rad) > 0.05 && refl_dir.y > -0.25 && abs(o_u) < 16.5) {
        float wall_u = (r_rad >= 0.0) ? 16.2 : -16.2;
        float t_wall = (wall_u - o_u) / r_rad;
        if (t_wall > 0.5 && t_wall < 110.0) {
            float hit_x = pos.x + refl_dir.x * t_wall;
            float hit_y = pos.y + refl_dir.y * t_wall;
            float hit_z = pos.z + refl_dir.z * t_wall;
            if (hit_y > 0.15 && hit_y < 46.0 && hit_x < 142.5) {
                vec2 o_tan = vec2(-o_rad.y, o_rad.x);
                float hit_u = dot(vec2(hit_x, hit_z), o_tan);
                float rough_att = 1.0 / (1.0 + roughness * 8.5);
                float dist_att = exp(-t_wall * 0.016);
                vec3 facade_col = sky_hor * 0.24 + vec3(0.025, 0.030, 0.042);
                float refl_blur = clamp(roughness * 1.8 + t_wall * 0.012, 0.08, 1.25);
                if (hit_y < 3.45) {
                    float su_s = hit_u * 0.26;
                    float pane = euv_box_pulse(su_s, 0.10, 0.90, refl_blur);
                    float sh = euv_hash(vec2(floor(su_s), 7.0));
                    vec3 shop = (sh < 0.40) ? vec3(1.22, 0.80, 0.36) : ((sh < 0.62) ? vec3(0.30, 0.92, 1.24) : ((sh < 0.80) ? vec3(1.16, 0.34, 0.72) : vec3(0.0)));
                    float shop_coh = 1.0 - smoothstep(0.10, 0.28, refl_blur);
                    facade_col += mix(vec3(0.76, 0.58, 0.54), shop, shop_coh) * (pane * 0.72 * rough_att);
                } else {
                    float su = hit_u * 0.48;
                    float sy = hit_y * 0.33;
                    float pane = euv_box_pulse(su, 0.14, 0.86, refl_blur) * euv_box_pulse(sy, 0.18, 0.84, refl_blur);
                    float ch = euv_hash(vec2(floor(su), floor(sy)));
                    vec3 win_glow = (ch < 0.22) ? vec3(1.28, 0.88, 0.44)
                        : ((ch < 0.33) ? vec3(0.30, 0.90, 1.28)
                        : ((ch < 0.42) ? vec3(0.98, 0.96, 0.90)
                        : ((ch < 0.50) ? vec3(1.10, 0.36, 0.74) : vec3(0.0))));
                    float win_coh = 1.0 - smoothstep(0.10, 0.28, refl_blur);
                    facade_col += mix(vec3(0.49, 0.40, 0.36), win_glow, win_coh) * (pane * (0.38 + 0.56 * rough_att));
                }
                env_col = mix(env_col, facade_col, clamp((0.42 + 0.48 * rough_att) * dist_att, 0.0, 0.90));
            }
        }
    }
    if (refl_dir.y > 0.02 && pos.y < 6.6) {
        float t_lamp = (6.8 - pos.y) / refl_dir.y;
        if (t_lamp > 0.5 && t_lamp < 95.0) {
            float lx = pos.x + refl_dir.x * t_lamp;
            float lz = pos.z + refl_dir.z * t_lamp;
            float l_sz = clamp(lz, -140.0, 140.0);
            float l_u = length(vec2(lx - 64.0, lz - l_sz)) - 64.0;
            float du_l = abs(abs(l_u) - 10.6);
            if (du_l < 14.0) {
                float l_s = euv_loop_arc_s(lx, lz);
                float l_phase = l_s * (40.0 / 962.12386);
                float l_idx = floor(l_phase + 0.5);
                float ds_l = abs(l_phase - l_idx) * 24.0531;
                float spread = 0.72 + roughness * 4.4;
                float lamp_glow = exp(-(du_l * du_l + ds_l * ds_l * 0.32) / (spread * spread)) * exp(-t_lamp * 0.020);
                float lp_h = fract(abs(l_idx) * 0.25);
                vec3 lamp_c = (lp_h < 0.25) ? vec3(1.48, 0.96, 0.42) : ((lp_h < 0.50) ? vec3(0.45, 1.15, 1.50) : ((lp_h < 0.75) ? vec3(1.40, 0.58, 0.90) : vec3(1.44, 1.06, 0.58)));
                env_col += lamp_c * (lamp_glow * (1.28 - roughness * 0.72));
            }
        }
    }
    return env_col;
}
void main() {
    vec3 norm = normalize(v_world_norm);
    vec3 view_dir = normalize(u_camera_pos.xyz - v_world_pos);
    vec3 sun_dir = normalize(u_sun_dir_fog.xyz);
    float dist = length(u_camera_pos.xyz - v_world_pos);
    vec3 dpdx = dFdx(v_world_pos);
    vec3 dpdy = dFdy(v_world_pos);
    vec3 fw_p = abs(dpdx) + abs(dpdy);
    float fw_xz = length(fw_p.xz);
    float fw_p_len = length(fw_p);
    float fw_n_len = length(fwidth(norm));
    vec3 base_col = v_base_met.rgb;
    float metallic = clamp(v_base_met.w, 0.0, 1.0);
    float roughness = clamp(v_emissive_rough.w, 0.05, 1.0);
    vec3 emissive = (v_submat_mode > 1.5) ? base_col * 1.15 : v_emissive_rough.rgb;
    float env_boost = 0.85;
    float contact_ao = 0.70 + 0.30 * smoothstep(0.0, 2.4, v_world_pos.y);
    float loop_spine_z = clamp(v_world_pos.z, -140.0, 140.0);
    vec2 loop_rel = vec2(v_world_pos.x - 64.0, v_world_pos.z - loop_spine_z);
    float loop_d_spine = length(loop_rel);
    float loop_road_u = loop_d_spine - 64.0;
    float loop_abs_u = abs(loop_road_u);
    vec3 local_lamp_light = vec3(0.0);
    float water_surface = 0.0;
    float water_is_ocean = 0.0;
    float water_rough = 0.032;
    float water_foam = 0.0;
    float water_crest = 0.0;
    vec3 water_norm = norm;
    vec3 water_sub_col = base_col;
    vec3 water_lamp_diff = vec3(0.0);
    vec3 water_lamp_spec = vec3(0.0);
    if (v_detail_mode > 0.5 && v_detail_mode < 1.5 && norm.y > 0.82) {
        float wpx = v_world_pos.x;
        float wpz = v_world_pos.z;
        float w_time = u_sky_horizon_tod.w;
        float fw_x = max(fw_p.x, 0.015);
        float fw_z = max(fw_p.z, 0.015);
        float d_spine = loop_d_spine;
        float road_u = loop_road_u;
        float abs_u = loop_abs_u;
        vec2 rad_dir = (d_spine > 1.0e-4) ? (loop_rel / d_spine) : vec2(-1.0, 0.0);
        vec2 tan_dir = vec2(-rad_dir.y, rad_dir.x);
        float fw_u = max(abs(rad_dir.x) * fw_p.x + abs(rad_dir.y) * fw_p.z, 0.015);
        float arc_scale = (abs(wpz) > 140.0) ? (64.0 / max(d_spine, 16.0)) : 1.0;
        float fw_s = max((abs(tan_dir.x) * fw_p.x + abs(tan_dir.y) * fw_p.z) * arc_scale, 0.015);
        float bay_w = euv_box_step(wpx, 144.0, fw_x) * smoothstep(77.8, 78.4, d_spine);
        float micro_fade = (1.0 - smoothstep(14.0, 48.0, dist)) * (1.0 - smoothstep(0.05, 0.22, fw_xz));
        if (bay_w > 0.001) {
            float shore_d = max(wpx - 144.0, 0.0);
            float warp_a = euv_noise(vec2(wpx * 0.075 - w_time * 0.14, wpz * 0.065 + w_time * 0.09)) * 2.4;
            float warp_b = euv_noise(vec2(wpx * 0.19 + 4.2, wpz * 0.17 - w_time * 0.22)) * 1.4;
            float aa1 = 1.0 / (1.0 + fw_xz * 0.42);
            float aa2 = 1.0 / (1.0 + fw_xz * 0.68);
            float aa3 = 1.0 / (1.0 + fw_xz * 1.25);
            float aa4 = 1.0 / (1.0 + fw_xz * 2.35);
            float p1 = wpx * 0.23 + wpz * 0.11 + warp_a - w_time * 1.35;
            float p2 = -wpx * 0.17 + wpz * 0.31 + warp_b - w_time * 1.78;
            float p3 = wpx * 0.57 - wpz * 0.49 + warp_b * 0.7 - w_time * 2.45;
            float p4 = wpx * 1.32 + wpz * 1.08 - w_time * 3.15;
            float gx = cos(p1) * 0.23 * 0.12 * aa1 - cos(p2) * 0.17 * 0.095 * aa2 + cos(p3) * 0.57 * 0.048 * aa3 + cos(p4) * 1.32 * 0.018 * aa4;
            float gz = cos(p1) * 0.11 * 0.12 * aa1 + cos(p2) * 0.31 * 0.095 * aa2 - cos(p3) * 0.49 * 0.048 * aa3 + cos(p4) * 1.08 * 0.018 * aa4;
            float dist_damp = 1.0 - smoothstep(65.0, 260.0, dist) * 0.65;
            water_norm = normalize(vec3(-gx * dist_damp, 1.0, -gz * dist_damp));
            water_crest = clamp((sin(p1) * 0.45 + sin(p2) * 0.35 + sin(p3) * 0.20) * 0.5 + 0.5, 0.0, 1.0);
            float shallow_t = exp(-shore_d * 0.055);
            float cloud_var = 0.84 + 0.24 * euv_noise(vec2(wpx * 0.018 - w_time * 0.025, wpz * 0.018 + w_time * 0.015));
            float seawall_shadow = mix(0.46, 1.0, smoothstep(0.0, 1.8, shore_d));
            water_sub_col = mix(vec3(0.005, 0.044, 0.118), vec3(0.018, 0.165, 0.205), shallow_t) * (cloud_var * seawall_shadow);
            float surf_n = euv_noise(vec2(wpx * 0.48 - w_time * 0.55, wpz * 0.34 + w_time * 0.18));
            float surf_n2 = euv_noise(vec2(wpx * 1.35 + 3.1, wpz * 1.15 - w_time * 0.40));
            float surf_wave = sin(shore_d * 1.65 - w_time * 1.95 + surf_n * 3.4) * 0.5 + 0.5;
            float shore_band = exp(-shore_d * 0.38);
            float foam_aa = 1.0 - smoothstep(0.25, 1.25, fw_xz);
            float lace = smoothstep(0.32, 0.72, surf_n * 0.62 + surf_n2 * 0.38 + shore_band * 0.42);
            water_foam = clamp((shore_band * (0.45 + 0.55 * surf_wave) * lace + exp(-shore_d * 2.2) * 0.65) * foam_aa, 0.0, 0.94);
            water_surface = bay_w;
            water_is_ocean = bay_w;
            water_rough = 0.042;
            contact_ao = mix(0.62, 1.0, smoothstep(0.0, 2.2, shore_d));
        }
        if (bay_w < 0.999) {
            float agg_a = (micro_fade > 0.005) ? euv_noise(vec2(wpx * 8.5, wpz * 8.5)) : 0.5;
            float agg_b = (micro_fade > 0.005) ? euv_noise(vec2(wpx * 24.0 + 7.3, wpz * 24.0 - 4.1)) : 0.5;
            float agg_h = agg_a * 0.48 + agg_b * 0.52;
            float grain_n = agg_h - 0.5;
            if (abs_u > 14.2) {
                float is_inner = step(d_spine, 64.0);
                float r_cx = -92.0 + 9.0 * sin(wpz * 0.022) + 3.5 * cos(wpz * 0.048);
                float d_ns = (abs(wpz) < 274.0) ? abs(wpx - r_cx) : 999.0;
                float r_est_off = 4.5 * sin(wpx * 0.032);
                float d_en = (wpx >= r_cx - 6.0 && wpx <= 146.0) ? abs(wpz - (-266.0 + r_est_off)) : 999.0;
                float d_es = (wpx >= r_cx - 6.0 && wpx <= 146.0) ? abs(wpz - (266.0 + r_est_off)) : 999.0;
                float d_river = min(d_ns, min(d_en, d_es));
                float bridge_deck = (d_ns < 11.4)
                    ? max(
                        max(
                            euv_box_band(wpz, -122.4, -113.6, fw_z),
                            euv_box_band(wpz, -4.4, 4.4, fw_z)
                        ),
                        euv_box_band(wpz, 113.6, 122.4, fw_z)
                    )
                    : 0.0;
                float riv_water_w = (1.0 - euv_box_step(d_river, 6.8, fw_xz)) * (1.0 - bridge_deck);
                if (riv_water_w > 0.005) {
                    float flow_v = wpz * 0.42 - w_time * 1.45;
                    float flow_u = wpx * 0.55 + w_time * 0.32;
                    float rw_a = euv_noise(vec2(flow_u * 0.45, flow_v * 0.35)) * 2.2;
                    float rw_b = euv_noise(vec2(wpx * 1.15 - w_time * 0.48, wpz * 0.95 - w_time * 1.12)) * 1.4;
                    float ra1 = 1.0 / (1.0 + fw_xz * 0.55);
                    float ra2 = 1.0 / (1.0 + fw_xz * 1.15);
                    float rp1 = wpx * 0.48 + wpz * 0.32 + rw_a - w_time * 1.85;
                    float rp2 = -wpx * 0.72 + wpz * 0.88 + rw_b - w_time * 2.55;
                    float rgx = cos(rp1) * 0.48 * 0.085 * ra1 - cos(rp2) * 0.72 * 0.042 * ra2;
                    float rgz = cos(rp1) * 0.32 * 0.085 * ra1 + cos(rp2) * 0.88 * 0.042 * ra2;
                    float rdamp = 1.0 - smoothstep(55.0, 210.0, dist) * 0.65;
                    water_norm = normalize(vec3(-rgx * rdamp, 1.0, -rgz * rdamp));
                    water_crest = clamp((sin(rp1) * 0.55 + sin(rp2) * 0.45) * 0.5 + 0.5, 0.0, 1.0);
                    float bank_t = smoothstep(1.2, 6.6, d_river);
                    float caust_a = sin(wpx * 1.35 + wpz * 0.95 + rw_a - w_time * 2.1);
                    float caust_b = cos(-wpx * 1.15 + wpz * 1.45 - rw_b + w_time * 1.7);
                    float caust = pow(abs(caust_a * 0.55 + caust_b * 0.45), 2.0) * (1.0 - smoothstep(35.0, 130.0, dist));
                    vec3 deep_riv = vec3(0.014, 0.115, 0.148);
                    vec3 shallow_riv = vec3(0.048, 0.255, 0.235);
                    water_sub_col = mix(deep_riv, shallow_riv, bank_t) + vec3(0.035, 0.125, 0.110) * caust;
                    float foam_r = smoothstep(5.8, 6.75, d_river) * (0.45 + 0.55 * euv_noise(vec2(wpx * 1.4, wpz * 1.4 - w_time * 0.9))) * (1.0 - smoothstep(0.25, 1.1, fw_xz));
                    water_foam = clamp(foam_r * 0.62, 0.0, 0.72);
                    water_surface = riv_water_w;
                    water_is_ocean = riv_water_w * 0.85;
                    water_rough = 0.034;
                    contact_ao = mix(0.82, 1.0, 1.0 - bank_t * 0.45);
                }
                float cob_row = floor(wpz * 1.65);
                float cob_u = wpx * 1.25 + 0.5 * fract(cob_row * 0.5);
                float cob_v = wpz * 1.65;
                float cob_su = euv_box_pulse(cob_u, 0.0, 0.075, fw_x * 1.25);
                float cob_sv = euv_box_pulse(cob_v, 0.0, 0.085, fw_z * 1.65);
                float cob_joint = clamp(cob_su + cob_sv - cob_su * cob_sv, 0.0, 1.0);
                float cob_h = euv_hash(vec2(floor(cob_u), cob_row));
                float cob_var = 0.90 + 0.20 * mix(0.5, cob_h, 1.0 - smoothstep(0.08, 0.26, fw_xz));
                vec3 plaza_tone = mix(vec3(0.30, 0.31, 0.33), vec3(0.37, 0.36, 0.34), is_inner);
                vec3 cobble_col = plaza_tone * cob_var * mix(1.0, 0.66, cob_joint) * (1.0 + grain_n * 0.14 * micro_fade);
                float west_park = smoothstep(-53.0, -59.0, wpx) * smoothstep(-132.0, -124.0, wpx) * smoothstep(286.0, 276.0, abs(wpz));
                float est_park = smoothstep(24.0, 17.0, min(abs(wpz + 266.0), abs(wpz - 266.0))) * smoothstep(-130.0, -120.0, wpx) * smoothstep(144.0, 138.0, wpx);
                float rim_green = max(smoothstep(-136.0, -145.0, wpx), smoothstep(286.0, 296.0, abs(wpz)));
                float inner_meadow = smoothstep(49.0, 44.0, d_spine)
                    * smoothstep(4.0, 7.5, abs(wpx - 38.0))
                    * smoothstep(4.0, 7.5, abs(wpx - 91.0))
                    * smoothstep(126.0, 118.0, abs(wpz))
                    * smoothstep(0.36, 0.54, euv_noise(vec2(wpx * 0.045 + 1.2, wpz * 0.045 - 2.8)));
                float park_w = clamp(max(max(west_park, est_park), max(rim_green * 0.68, inner_meadow)), 0.0, 1.0) * (1.0 - bridge_deck);
                float grass_n1 = euv_noise(vec2(wpx * 0.085 + 3.4, wpz * 0.085 - 1.9));
                float grass_n2 = euv_noise(vec2(wpx * 0.42 - 7.1, wpz * 0.42 + 4.8));
                float clover_n = (micro_fade > 0.005) ? euv_noise(vec2(wpx * 3.8 + 1.5, wpz * 3.8 - 2.3)) : 0.5;
                vec3 grass_col = mix(vec3(0.068, 0.165, 0.056), vec3(0.145, 0.255, 0.082), grass_n1 * 0.62 + grass_n2 * 0.38);
                grass_col = mix(grass_col, vec3(0.195, 0.285, 0.098), smoothstep(0.58, 0.85, clover_n) * 0.42 * micro_fade);
                grass_col *= (0.92 + 0.16 * grain_n * micro_fade);
                vec3 land_col = mix(cobble_col, grass_col, park_w);
                float shore_sand_w = smoothstep(8.6, 7.1, d_river) * (1.0 - riv_water_w) * (1.0 - bridge_deck);
                float emb_stone_w = euv_box_band(d_river, 8.2, 10.2, fw_xz) * (1.0 - bridge_deck);
                vec3 pebble_col = vec3(0.32, 0.29, 0.23) * (0.78 + 0.40 * agg_h) * mix(0.68, 1.0, smoothstep(6.8, 8.2, d_river));
                vec3 emb_col = mix(vec3(0.30, 0.31, 0.29), vec3(0.12, 0.20, 0.10), 0.35 * cob_joint + 0.25 * grass_n2) * mix(1.0, 0.68, cob_joint);
                land_col = mix(land_col, pebble_col, shore_sand_w);
                land_col = mix(land_col, emb_col, emb_stone_w * 0.88);
                if (bridge_deck > 0.005) {
                    float br_edge = max(
                        max(euv_box_band(abs(wpz + 118.0), 3.7, 4.4, fw_z), euv_box_band(abs(wpz), 3.7, 4.4, fw_z)),
                        euv_box_band(abs(wpz - 118.0), 3.7, 4.4, fw_z)
                    );
                    vec3 br_col = mix(vec3(0.35, 0.34, 0.32) * cob_var * mix(1.0, 0.72, cob_joint), vec3(0.48, 0.46, 0.42), br_edge);
                    land_col = mix(land_col, br_col, bridge_deck);
                }
                float seawall_coping = euv_box_band(wpx, 142.4, 144.0, fw_x) * smoothstep(77.8, 78.4, d_spine);
                float seawall_lip = euv_box_band(wpx, 143.65, 144.0, fw_x) * smoothstep(77.8, 78.4, d_spine);
                float sea_spray = exp(-max(144.0 - wpx, 0.0) * 0.65) * smoothstep(77.8, 78.4, d_spine);
                vec3 coping_col = mix(vec3(0.42, 0.44, 0.46), vec3(0.22, 0.24, 0.27), seawall_lip) * (1.0 - 0.28 * sea_spray);
                base_col = mix(land_col, coping_col, seawall_coping);
                metallic = 0.0;
                float plaza_damp = smoothstep(0.56, 0.78, euv_noise(vec2(wpx * 0.14 + 2.1, wpz * 0.14 - 3.7))) * (1.0 - park_w);
                roughness = mix(mix(0.82, 0.50, plaza_damp), 0.90, park_w);
                roughness = mix(roughness, 0.54, max(sea_spray, shore_sand_w * 0.7));
                env_boost = mix(0.84, 0.52, park_w);
                if (riv_water_w <= 0.005) {
                    contact_ao = mix(mix(0.86, 0.96, seawall_coping) * (1.0 - 0.28 * seawall_lip), 0.92, park_w);
                }
            } else {
                float road_s = euv_loop_arc_s(wpx, wpz);
                float curb_w = euv_box_step(abs_u, 10.2, fw_u);
                float walk_w = euv_box_step(abs_u, 10.8, fw_u);
                float om_curb = 1.0 - curb_w;
                float inner_lin = 1.0 - clamp(abs(abs_u - 3.65) * 0.85, 0.0, 1.0);
                float outer_lin = 1.0 - clamp(abs(abs_u - 6.85) * 0.95, 0.0, 1.0);
                float tire_wear = clamp(inner_lin * inner_lin + outer_lin * outer_lin, 0.0, 1.0) * om_curb;
                float gutter_band = (1.0 - clamp(abs(abs_u - 9.55) * 2.2, 0.0, 1.0)) * om_curb;
                float curb_pool_guard = smoothstep(10.18, 9.72, abs_u);
                float center_crown = (1.0 - clamp(abs_u * 0.65, 0.0, 1.0)) * om_curb;
                float puddle_fade = 1.0 - smoothstep(54.0, 140.0, dist);
                float wet_field = 0.0;
                float wet_porosity = 0.0;
                float water_mask = 0.0;
                float w_depth = 0.0;
                if (puddle_fade > 0.005 && om_curb > 0.005) {
                    float n0 = euv_noise(vec2(wpx * 0.13 + 3.1, wpz * 0.075 - 1.7));
                    float n1 = euv_noise(vec2(wpx * 0.38 - 5.4, wpz * 0.22 + 2.9));
                    float n2 = euv_noise(vec2(wpx * 1.45 + 8.7, wpz * 1.05 - 4.3));
                    wet_field = n0 * 0.56 + n1 * 0.28 + n2 * 0.16 + tire_wear * 0.13 + gutter_band * 0.17 - center_crown * 0.22;
                    wet_porosity = smoothstep(0.45, 0.60, wet_field) * om_curb * puddle_fade;
                    float shore_micro = (agg_h - 0.5) * 0.058 * (0.28 + 0.72 * micro_fade) + (n2 - 0.5) * 0.045;
                    float puddle_h = wet_field + shore_micro;
                    float shore_w = clamp(0.020 + fw_xz * 0.042, 0.020, 0.095);
                    water_mask = smoothstep(0.60 - shore_w, 0.60 + shore_w, puddle_h) * om_curb * curb_pool_guard * puddle_fade;
                    w_depth = clamp((wet_field - 0.58) * 3.8, 0.0, 1.0) * water_mask;
                }
                float agg_bump_w = (1.0 - smoothstep(8.0, 36.0, dist)) * (1.0 - smoothstep(0.025, 0.13, fw_xz)) * om_curb;
                if (agg_bump_w > 0.01) {
                    float agg_bx = euv_noise(vec2((wpx + 0.022) * 24.0 + 7.3, wpz * 24.0 - 4.1));
                    float agg_bz = euv_noise(vec2(wpx * 24.0 + 7.3, (wpz + 0.022) * 24.0 - 4.1));
                    float bump_amp = 0.22 * agg_bump_w * mix(1.0, 0.52, wet_porosity);
                    norm = normalize(vec3((agg_b - agg_bx) * bump_amp, 1.0, (agg_b - agg_bz) * bump_amp));
                }
                vec2 total_ripple = vec2(0.0);
                float ripple_fade = (1.0 - smoothstep(24.0, 72.0, dist)) * (1.0 - smoothstep(0.05, 0.22, fw_xz)) * smoothstep(0.03, 0.26, w_depth);
                if (water_mask > 0.005) {
                    if (ripple_fade > 0.005) {
                        float rw1 = wpx * 2.4 + wpz * 1.5 - w_time * 1.85;
                        float rw2 = -wpx * 1.8 + wpz * 3.1 - w_time * 2.35;
                        vec2 wind_g = vec2(cos(rw1) * 2.4 - cos(rw2) * 1.8, cos(rw1) * 1.5 + cos(rw2) * 3.1) * 0.0028;
                        vec2 r_uv = vec2(wpx, wpz) * 0.62;
                        vec2 r_cell = floor(r_uv);
                        vec2 r_f = fract(r_uv) - 0.5;
                        float r_seed = euv_hash(r_cell);
                        float r_cyc = fract(w_time * 0.45 + r_seed * 6.2831);
                        vec2 r_ctr = (vec2(fract(r_seed * 17.3), fract(r_seed * 31.7)) - 0.5) * 0.42;
                        vec2 r_d = r_f - r_ctr;
                        float r_dist = length(r_d);
                        float r_rad = r_cyc * 0.44;
                        float r_dr = (r_dist - r_rad) * 20.0;
                        float r_env = exp(-(r_dr * r_dr)) * (1.0 - r_cyc) * smoothstep(0.0, 0.08, r_cyc) * smoothstep(0.45, 0.24, r_dist);
                        float r_w = sin((r_dist - r_rad) * 46.0) * r_env * step(0.42, r_seed);
                        vec2 drop_g = (r_d / max(r_dist, 0.015)) * (r_w * 0.012);
                        total_ripple = clamp((wind_g + drop_g) * ripple_fade, vec2(-0.018), vec2(0.018));
                    }
                    water_norm = normalize(vec3(-total_ripple.x, 1.0, -total_ripple.y));
                }
                vec3 dry_road = (base_col * 1.38 + vec3(0.036, 0.038, 0.042))
                    * (1.0 + grain_n * 0.34 * micro_fade)
                    * (1.0 - 0.14 * tire_wear)
                    * (1.0 - 0.22 * gutter_band);
                vec3 road_col = dry_road;
                float yellow_w = euv_box_band(abs_u, 0.135, 0.305, fw_u) * om_curb;
                if (yellow_w > 0.005) {
                    float pv = 0.94 + grain_n * 0.16 * micro_fade;
                    road_col = mix(road_col, vec3(0.92, 0.65, 0.10) * pv, yellow_w * 0.95);
                }
                float dash_on = euv_box_pulse(road_s * 0.12472407, 0.0, 0.42, fw_s * 0.12472407);
                float lane_w = euv_box_band(abs_u, 4.565, 4.735, fw_u) * dash_on * om_curb;
                float shoulder_w = euv_box_band(abs_u, 9.15, 9.35, fw_u) * om_curb;
                float straight_w = smoothstep(134.0, 126.0, abs(wpz));
                float cross_z = euv_box_pulse((road_s + 18.0) * 0.02494481, 0.455, 0.545, fw_s * 0.02494481) * straight_w;
                float cross_x_mask = euv_box_band(abs_u, 0.55, 8.65, fw_u);
                float cross_bars = euv_box_pulse(road_u * 0.85, 0.0, 0.50, fw_u * 0.85);
                float cross_w = cross_z * cross_x_mask * cross_bars * 0.88 * om_curb;
                float white_w = max(max(shoulder_w, lane_w), cross_w);
                if (white_w > 0.005) {
                    float pv = 0.92 + grain_n * 0.14 * micro_fade;
                    road_col = mix(road_col, vec3(0.82, 0.85, 0.90) * pv, white_w * 0.92);
                }
                vec3 damp_col = pow(max(road_col, vec3(1.0e-4)), vec3(1.35)) * 0.60;
                vec3 surf_road_col = mix(road_col, damp_col, wet_porosity);
                vec3 sub_col = damp_col;
                if (water_mask > 0.005) {
                    vec2 ref_shift = (-view_dir.xz / max(view_dir.y + 0.35, 0.35)) * (w_depth * 0.032) + total_ripple * (w_depth * 1.65);
                    float sub_u = road_u + dot(ref_shift, rad_dir);
                    float sub_abs_u = abs(sub_u);
                    float sub_s = road_s + dot(ref_shift, tan_dir);
                    float sub_agg = (micro_fade > 0.005) ? (euv_noise(vec2((wpx + ref_shift.x) * 24.0 + 7.3, (wpz + ref_shift.y) * 24.0 - 4.1)) - 0.5) : 0.0;
                    vec3 sub_base = (base_col * 1.38 + vec3(0.036, 0.038, 0.042))
                        * (1.0 + sub_agg * 0.30 * micro_fade)
                        * (1.0 - 0.14 * tire_wear)
                        * (1.0 - 0.22 * gutter_band);
                    float sy_w = euv_box_band(sub_abs_u, 0.135, 0.305, fw_u) * om_curb;
                    if (sy_w > 0.005) {
                        sub_base = mix(sub_base, vec3(0.92, 0.65, 0.10) * (0.92 + sub_agg * 0.14 * micro_fade), sy_w * 0.95);
                    }
                    float sd_on = euv_box_pulse(sub_s * 0.12472407, 0.0, 0.42, fw_s * 0.12472407);
                    float sl_w = euv_box_band(sub_abs_u, 4.565, 4.735, fw_u) * sd_on * om_curb;
                    float ss_w = euv_box_band(sub_abs_u, 9.15, 9.35, fw_u) * om_curb;
                    float sc_z = euv_box_pulse((sub_s + 18.0) * 0.02494481, 0.455, 0.545, fw_s * 0.02494481) * straight_w;
                    float sc_w = sc_z * euv_box_band(sub_abs_u, 0.55, 8.65, fw_u) * euv_box_pulse(sub_u * 0.85, 0.0, 0.50, fw_u * 0.85) * 0.88 * om_curb;
                    float sw_w = max(max(ss_w, sl_w), sc_w);
                    if (sw_w > 0.005) {
                        sub_base = mix(sub_base, vec3(0.82, 0.85, 0.90) * (0.90 + sub_agg * 0.12 * micro_fade), sw_w * 0.92);
                    }
                    vec3 sub_damp = pow(max(sub_base, vec3(1.0e-4)), vec3(1.35)) * 0.58;
                    vec3 beer_atten = exp(-w_depth * vec3(1.15, 0.88, 0.72));
                    float meniscus_rim = 1.0 - 0.18 * (1.0 - smoothstep(0.0, 0.22, w_depth));
                    sub_col = sub_damp * beer_atten * meniscus_rim;
                }
                float curb_joint = euv_box_pulse(road_s * 0.833, 0.0, 0.045, fw_s * 0.833);
                float curb_bevel = euv_box_band(abs_u, 10.22, 10.42, fw_u);
                vec3 curb_col = mix(vec3(0.42, 0.43, 0.45), vec3(0.50, 0.51, 0.52), curb_bevel)
                    * mix(1.0, 0.72, curb_joint)
                    * (1.0 + grain_n * 0.14 * micro_fade);
                float slab_s = road_s * 0.50;
                float slab_u = (abs_u - 10.8) * 0.625;
                float slab_js = euv_box_pulse(slab_s, 0.0, 0.036, fw_s * 0.50);
                float slab_ju = euv_box_pulse(slab_u, 0.0, 0.045, fw_u * 0.625);
                float slab_joint = clamp(slab_js + slab_ju - slab_js * slab_ju, 0.0, 1.0);
                float slab_h = euv_hash(vec2(floor(slab_s), floor(slab_u)));
                float slab_var = 0.92 + 0.16 * mix(0.5, slab_h, 1.0 - smoothstep(0.08, 0.28, fw_xz));
                float tactile_w = euv_box_band(abs_u, 10.84, 11.16, fw_u);
                float walk_damp = smoothstep(0.52, 0.74, euv_noise(vec2(wpx * 0.18 - 1.4, wpz * 0.18 + 3.2))) * walk_w;
                vec3 walk_slab = vec3(0.32, 0.33, 0.35) * slab_var * mix(1.0, 0.66, slab_joint) * (1.0 + grain_n * 0.16 * micro_fade);
                walk_slab = mix(walk_slab, vec3(0.56, 0.46, 0.24) * (0.90 + 0.18 * grain_n * micro_fade), tactile_w * 0.58);
                vec3 walk_col = mix(walk_slab, walk_slab * 0.68, walk_damp * 0.65);
                base_col = mix(mix(surf_road_col, curb_col, curb_w), walk_col, walk_w);
                metallic = 0.0;
                float dry_rough = mix(0.88, 0.76, tire_wear);
                float damp_rough = mix(dry_rough, 0.38, wet_porosity);
                roughness = mix(mix(damp_rough, 0.74, curb_w), mix(0.82, 0.44, walk_damp), walk_w);
                env_boost = 0.88;
                contact_ao = 0.76 + 0.24 * (1.0 - smoothstep(9.8, 14.2, abs_u) * 0.45);
                water_surface = water_mask;
                water_rough = 0.026;
                water_sub_col = sub_col;
                float l_phase = road_s * (40.0 / 962.12386);
                float l_idx = floor(l_phase + 0.5);
                float ds_lamp = (l_idx - l_phase) * 24.0531;
                float du_lamp = ((road_u >= 0.0) ? 10.6 : -10.6) - road_u;
                vec3 to_lamp = vec3(rad_dir.x * du_lamp + tan_dir.x * ds_lamp, 6.8, rad_dir.y * du_lamp + tan_dir.y * ds_lamp);
                float d2_lamp = dot(to_lamp, to_lamp);
                float lamp_att = 1.0 / (1.0 + d2_lamp * 0.026);
                vec3 lamp_dir = to_lamp * inversesqrt(max(d2_lamp, 0.1));
                float l_ndl = max(dot(norm, lamp_dir), 0.0);
                vec3 lamp_h = normalize(view_dir + lamp_dir);
                float l_ndh = max(dot(norm, lamp_h), 0.0);
                float l_a2 = max(roughness * roughness * roughness * roughness, 0.018);
                float l_den = l_ndh * l_ndh * (l_a2 - 1.0) + 1.0;
                float l_spec = min(l_a2 / max(3.14159265 * l_den * l_den, 0.0005), 6.0);
                float lp_h = fract(abs(l_idx) * 0.25);
                vec3 lamp_col = (lp_h < 0.25) ? vec3(1.15, 0.78, 0.34) : ((lp_h < 0.50) ? vec3(0.36, 0.88, 1.18) : ((lp_h < 0.75) ? vec3(1.10, 0.44, 0.72) : vec3(1.12, 0.84, 0.46)));
                local_lamp_light = lamp_col * (lamp_att * l_ndl) * (base_col * 0.48 + vec3(0.02 + 0.06 * wet_porosity) * l_spec);
                if (water_mask > 0.005) {
                    float wl_ndl = max(dot(water_norm, lamp_dir), 0.0);
                    float wl_ndh = max(dot(water_norm, lamp_h), 0.0);
                    float wl_hdv = max(dot(lamp_h, view_dir), 0.0);
                    float wl_om = 1.0 - wl_hdv;
                    float wl_f = 0.02 + 0.98 * (wl_om * wl_om * wl_om * wl_om * wl_om);
                    float wl_a2 = clamp(0.00075 + dist * 0.00004, 0.00075, 0.006);
                    float wl_den = wl_ndh * wl_ndh * (wl_a2 - 1.0) + 1.0;
                    float wl_spec = min(wl_a2 / max(3.14159265 * wl_den * wl_den, 0.0004), 18.0);
                    water_lamp_diff = lamp_col * (lamp_att * wl_ndl) * (sub_col * 0.46);
                    water_lamp_spec = lamp_col * (lamp_att * wl_ndl * wl_f * wl_spec * 0.55);
                }
            }
        }
    } else if (v_detail_mode > 1.5 && v_detail_mode < 2.5) {
        float world_y = v_world_pos.y;
        if (norm.y > 0.70 && v_submat_mode < 0.5) {
            float roof_u = euv_box_pulse(v_world_pos.x * 0.35, 0.0, 0.04, max(fw_p.x, 0.01) * 0.35);
            float roof_v = euv_box_pulse(v_world_pos.z * 0.35, 0.0, 0.04, max(fw_p.z, 0.01) * 0.35);
            float roof_seam = clamp(roof_u + roof_v - roof_u * roof_v, 0.0, 1.0);
            float roof_gr = euv_noise(v_world_pos.xz * 4.5) - 0.5;
            base_col = (base_col * 0.42 + vec3(0.16, 0.17, 0.19)) * mix(1.0, 0.72, roof_seam) * (1.0 + roof_gr * 0.14);
            metallic = 0.08;
            roughness = 0.84;
            env_boost = 0.65;
        } else if (abs(norm.y) < 0.35 && v_submat_mode < 0.5) {
            vec2 horiz_n = norm.xz;
            float horiz_len = max(length(horiz_n), 1.0e-4);
            vec2 wall_tan = vec2(-horiz_n.y, horiz_n.x) / horiz_len;
            float u_coord = dot(v_world_pos.xz, wall_tan);
            float fw_u_w = max(abs(dot(dpdx.xz, wall_tan)) + abs(dot(dpdy.xz, wall_tan)), 0.0005);
            float fw_y_w = max(fw_p.y, 0.0005);
            float dist_fade = 1.0 - smoothstep(130.0, 320.0, dist);
            float mat_fade = 1.0 - smoothstep(45.0, 165.0, dist);
            float is_glass_tower = step(0.55, metallic);
            float is_brick_wall = (1.0 - is_glass_tower) * step(base_col.g * 1.20, base_col.r);
            float is_stone_wall = (1.0 - is_glass_tower) * (1.0 - is_brick_wall) * step(base_col.b * 1.04, base_col.r);
            vec3 arch_wall_col = base_col;
            float arch_wall_rough = roughness;
            if (is_brick_wall > 0.5 && mat_fade > 0.01) {
                float b_row = floor(world_y * 6.2);
                float b_u = u_coord * 2.1 + 0.5 * fract(b_row * 0.5);
                float b_v = world_y * 6.2;
                float m_u = euv_box_pulse(b_u, 0.0, 0.085, fw_u_w * 2.1);
                float m_v = euv_box_pulse(b_v, 0.0, 0.14, fw_y_w * 6.2);
                float mortar = clamp(m_u + m_v - m_u * m_v, 0.0, 1.0) * mat_fade;
                float b_h = euv_hash(vec2(floor(b_u), b_row));
                float b_coh = 1.0 - smoothstep(0.14, 0.36, max(fw_u_w * 2.1, fw_y_w * 6.2));
                vec3 brick_t = base_col * (0.84 + 0.32 * mix(0.5, b_h, b_coh));
                arch_wall_col = mix(brick_t, vec3(0.46, 0.43, 0.39), mortar * 0.72);
                arch_wall_rough = mix(0.76, 0.90, mortar);
            } else if (is_stone_wall > 0.5 && mat_fade > 0.01) {
                float s_row = floor(world_y * 1.35);
                float s_u = u_coord * 0.72 + 0.5 * fract(s_row * 0.5);
                float s_v = world_y * 1.35;
                float j_u = euv_box_pulse(s_u, 0.0, 0.045, fw_u_w * 0.72);
                float j_v = euv_box_pulse(s_v, 0.0, 0.060, fw_y_w * 1.35);
                float joint = clamp(j_u + j_v - j_u * j_v, 0.0, 1.0) * mat_fade;
                float s_h = euv_hash(vec2(floor(s_u), s_row));
                float s_coh = 1.0 - smoothstep(0.12, 0.34, max(fw_u_w * 0.72, fw_y_w * 1.35));
                float vein = euv_noise(vec2(u_coord * 1.4, world_y * 2.8)) - 0.5;
                float cornice = euv_box_pulse(world_y * 0.33, 0.0, 0.09, fw_y_w * 0.33) * mat_fade;
                arch_wall_col = base_col * (0.88 + 0.22 * mix(0.5, s_h, s_coh) + vein * 0.10 * mat_fade) * mix(1.0, 0.68, joint);
                arch_wall_col = mix(arch_wall_col, arch_wall_col * 1.18, cornice * 0.65);
                arch_wall_rough = mix(0.64, 0.84, joint);
            } else if (is_glass_tower < 0.5 && mat_fade > 0.01) {
                float flute = euv_box_pulse(u_coord * 2.4, 0.0, 0.32, fw_u_w * 2.4) * mat_fade;
                float panel_v = euv_box_pulse(world_y * 0.33, 0.0, 0.05, fw_y_w * 0.33) * mat_fade;
                float conc_n = (euv_noise(vec2(u_coord * 2.2, world_y * 2.2)) - 0.5) * 0.12 * mat_fade;
                arch_wall_col = base_col * (0.88 + 0.18 * flute + conc_n) * mix(1.0, 0.70, panel_v);
                arch_wall_rough = mix(0.70, 0.86, panel_v);
            }
            if (world_y < 3.45) {
                float su_s = u_coord * 0.26;
                float fw_su_s = max(fw_u_w * 0.26, 0.0005);
                float fy_s = clamp(world_y / 3.45, 0.0, 1.0);
                float fw_fy_s = max(fw_y_w / 3.45, 0.0005);
                float pane_w = euv_box_pulse(su_s, 0.11, 0.89, fw_su_s) * euv_box_band(fy_s, 0.18, 0.89, fw_fy_s) * dist_fade;
                float sh = euv_hash(vec2(floor(su_s), 7.0));
                vec3 shop_glow = (sh < 0.40) ? vec3(1.18, 0.78, 0.36) : ((sh < 0.62) ? vec3(0.28, 0.88, 1.18) : ((sh < 0.80) ? vec3(1.12, 0.32, 0.68) : vec3(0.0)));
                float shop_coh = 1.0 - smoothstep(0.08, 0.22, max(fw_su_s, fw_fy_s));
                vec3 shop_eff = mix(vec3(0.74, 0.56, 0.52), shop_glow, shop_coh);
                base_col = mix(arch_wall_col * 0.72, base_col * 0.40, pane_w);
                emissive += shop_eff * (pane_w * mix(0.82, 0.55 + 0.55 * (1.0 - fy_s), shop_coh) * 0.92);
                roughness = mix(arch_wall_rough, mix(0.24, 0.12, shop_coh), pane_w);
            } else {
                float su = u_coord * 0.48;
                float sy = world_y * 0.33;
                float fw_su = max(fw_u_w * 0.48, 0.0005);
                float fw_sy = max(fw_y_w * 0.33, 0.0005);
                float fy_win = fract(sy);
                float win_lo_u = mix(0.22, 0.11, is_glass_tower);
                float win_hi_u = mix(0.78, 0.89, is_glass_tower);
                float win_lo_v = mix(0.24, 0.15, is_glass_tower);
                float win_hi_v = mix(0.82, 0.87, is_glass_tower);
                float pane_u = euv_box_pulse(su, win_lo_u, win_hi_u, fw_su);
                float pane_v = euv_box_pulse(sy, win_lo_v, win_hi_v, fw_sy);
                float pane_w = clamp(pane_u * pane_v * dist_fade, 0.0, 1.0);
                float mullion_u = euv_box_pulse(su, 0.48, 0.52, fw_su) * is_glass_tower * mat_fade;
                pane_w *= (1.0 - mullion_u * 0.75);
                float spandrel = mix(0.70, 0.88, euv_box_pulse(sy, 0.14, 0.88, fw_sy));
                vec3 wall_base = mix(arch_wall_col, base_col * spandrel, is_glass_tower);
                float ch = euv_hash(vec2(floor(su), floor(sy)));
                float room_lvl = 0.68 + fract(ch * 17.3) * 0.45;
                float blind_grad = clamp(0.48 + 0.66 * (1.0 - fy_win), 0.38, 1.14);
                float cell_detail = 1.0 - smoothstep(0.10, 0.28, max(fw_su, fw_sy));
                float eff_blind = mix(0.81, blind_grad, cell_detail);
                vec3 cell_em = vec3(0.0);
                float cell_glass_mul = clamp(0.36 + 0.22 * fy_win, 0.32, 0.62);
                if (ch < 0.22) {
                    cell_glass_mul = 0.86;
                    cell_em = vec3(1.24, 0.86, 0.42) * (room_lvl * eff_blind);
                } else if (ch < 0.33) {
                    cell_glass_mul = 0.84;
                    cell_em = vec3(0.28, 0.86, 1.24) * (room_lvl * eff_blind);
                } else if (ch < 0.42) {
                    cell_glass_mul = 0.88;
                    cell_em = vec3(0.96, 0.94, 0.88) * (room_lvl * eff_blind * 0.82);
                } else if (ch < 0.50) {
                    cell_glass_mul = 0.84;
                    cell_em = vec3(1.06, 0.34, 0.72) * (room_lvl * eff_blind * 0.86);
                }
                vec3 mean_em = vec3(0.35, 0.28, 0.25);
                base_col = mix(wall_base, base_col * mix(0.65, cell_glass_mul, cell_detail), pane_w);
                emissive += mix(mean_em, cell_em, cell_detail) * pane_w;
                roughness = mix(arch_wall_rough, mix(0.26, 0.10, cell_detail), pane_w);
                env_boost = mix(mix(0.72, 1.05, is_glass_tower), 1.55, pane_w);
            }
        }
        float toksvig_r = sqrt(clamp(fw_n_len * 0.5, 0.0, 0.35));
        float footprint_r = clamp(fw_p_len * 0.14, 0.0, 0.26);
        roughness = max(roughness, max(toksvig_r, footprint_r));
    } else if (v_detail_mode > 2.5 && v_detail_mode < 3.5 && norm.y > 0.80) {
        vec2 wp = v_world_pos.xz;
        float w_time = u_sky_horizon_tod.w;
        float warp_a = euv_noise(vec2(wp.x * 0.075 - w_time * 0.14, wp.y * 0.065 + w_time * 0.09)) * 2.4;
        float warp_b = euv_noise(vec2(wp.x * 0.19 + 4.2, wp.y * 0.17 - w_time * 0.22)) * 1.4;
        float aa1 = 1.0 / (1.0 + fw_xz * 0.42);
        float aa2 = 1.0 / (1.0 + fw_xz * 0.68);
        float aa3 = 1.0 / (1.0 + fw_xz * 1.25);
        float p1 = wp.x * 0.23 + wp.y * 0.11 + warp_a - w_time * 1.35;
        float p2 = -wp.x * 0.17 + wp.y * 0.31 + warp_b - w_time * 1.78;
        float p3 = wp.x * 0.57 - wp.y * 0.49 + warp_b * 0.7 - w_time * 2.45;
        float gx = cos(p1) * 0.23 * 0.12 * aa1 - cos(p2) * 0.17 * 0.095 * aa2 + cos(p3) * 0.57 * 0.048 * aa3;
        float gz = cos(p1) * 0.11 * 0.12 * aa1 + cos(p2) * 0.31 * 0.095 * aa2 - cos(p3) * 0.49 * 0.048 * aa3;
        float dist_damp = 1.0 - smoothstep(65.0, 260.0, dist) * 0.65;
        water_norm = normalize(vec3(-gx * dist_damp, 1.0, -gz * dist_damp));
        norm = water_norm;
        water_crest = clamp((sin(p1) * 0.45 + sin(p2) * 0.35 + sin(p3) * 0.20) * 0.5 + 0.5, 0.0, 1.0);
        float cloud_var = 0.84 + 0.24 * euv_noise(vec2(wp.x * 0.018 - w_time * 0.025, wp.y * 0.018 + w_time * 0.015));
        water_sub_col = vec3(0.005, 0.044, 0.118) * cloud_var;
        water_surface = 1.0;
        water_is_ocean = 1.0;
        water_rough = 0.042;
        contact_ao = 1.0;
    } else if (v_detail_mode > 3.5 && v_detail_mode < 4.5 && v_submat_mode < 0.5) {
        vec2 horiz_n = norm.xz;
        float horiz_len = max(length(horiz_n), 1.0e-4);
        vec2 wall_tan = vec2(-horiz_n.y, horiz_n.x) / horiz_len;
        float u_coord = (abs(norm.y) < 0.65) ? dot(v_world_pos.xz, wall_tan) : v_world_pos.x;
        float v_coord = (abs(norm.y) < 0.65) ? v_world_pos.y : v_world_pos.z;
        float fw_u_m = max((abs(norm.y) < 0.65) ? (abs(dot(dpdx.xz, wall_tan)) + abs(dot(dpdy.xz, wall_tan))) : fw_p.x, 0.001);
        float fw_v_m = max((abs(norm.y) < 0.65) ? fw_p.y : fw_p.z, 0.001);
        float m_fade = 1.0 - smoothstep(45.0, 175.0, dist);
        float row = floor(v_coord * 6.0);
        float bu = u_coord * 2.2 + 0.5 * fract(row * 0.5);
        float bv = v_coord * 6.0;
        float mu = euv_box_pulse(bu, 0.0, 0.09, fw_u_m * 2.2);
        float mv = euv_box_pulse(bv, 0.0, 0.15, fw_v_m * 6.0);
        float mortar = clamp(mu + mv - mu * mv, 0.0, 1.0) * m_fade;
        float bh = euv_hash(vec2(floor(bu), row));
        float bcoh = 1.0 - smoothstep(0.14, 0.38, max(fw_u_m * 2.2, fw_v_m * 6.0));
        float gr = (euv_noise(vec2(u_coord * 8.0, v_coord * 8.0)) - 0.5) * 0.14 * m_fade;
        base_col = mix(base_col * (0.82 + 0.34 * mix(0.5, bh, bcoh) + gr), vec3(0.48, 0.45, 0.40), mortar * 0.75);
        metallic = 0.0;
        roughness = mix(0.78, 0.92, mortar);
        env_boost = 0.55;
        if (abs(norm.y) < 0.35 && v_world_pos.y > 2.4 && m_fade > 0.02) {
            float wu = euv_box_pulse(u_coord * 0.38, 0.24, 0.76, fw_u_m * 0.38);
            float wv = euv_box_pulse(v_world_pos.y * 0.31, 0.24, 0.80, fw_v_m * 0.31);
            float win_w = wu * wv * m_fade;
            float wh = euv_hash(vec2(floor(u_coord * 0.38), floor(v_world_pos.y * 0.31)));
            vec3 apt_em = (wh < 0.38) ? vec3(1.12, 0.78, 0.38) : ((wh < 0.52) ? vec3(0.82, 0.86, 0.92) : vec3(0.0));
            base_col = mix(base_col, vec3(0.08, 0.12, 0.18), win_w);
            emissive += apt_em * (win_w * 0.65);
            roughness = mix(roughness, 0.16, win_w);
            env_boost = mix(env_boost, 1.35, win_w);
        }
    } else if (v_detail_mode > 4.5 && v_detail_mode < 5.5 && v_submat_mode < 0.5) {
        vec2 horiz_n = norm.xz;
        float horiz_len = max(length(horiz_n), 1.0e-4);
        vec2 wall_tan = vec2(-horiz_n.y, horiz_n.x) / horiz_len;
        float u_coord = (abs(norm.y) < 0.65) ? dot(v_world_pos.xz, wall_tan) : v_world_pos.x;
        float v_coord = (abs(norm.y) < 0.65) ? v_world_pos.y : v_world_pos.z;
        float fw_u_s = max((abs(norm.y) < 0.65) ? (abs(dot(dpdx.xz, wall_tan)) + abs(dot(dpdy.xz, wall_tan))) : fw_p.x, 0.001);
        float fw_v_s = max((abs(norm.y) < 0.65) ? fw_p.y : fw_p.z, 0.001);
        float s_fade = 1.0 - smoothstep(50.0, 190.0, dist);
        float row = floor(v_coord * 1.65);
        float su = u_coord * 0.95 + 0.5 * fract(row * 0.5);
        float sv = v_coord * 1.65;
        float ju = euv_box_pulse(su, 0.0, 0.06, fw_u_s * 0.95);
        float jv = euv_box_pulse(sv, 0.0, 0.08, fw_v_s * 1.65);
        float joint = clamp(ju + jv - ju * jv, 0.0, 1.0) * s_fade;
        float sh = euv_hash(vec2(floor(su), row));
        float scoh = 1.0 - smoothstep(0.12, 0.34, max(fw_u_s * 0.95, fw_v_s * 1.65));
        float sn = euv_noise(vec2(u_coord * 3.6, v_coord * 3.6));
        float moss = smoothstep(1.35, 0.10, v_world_pos.y) * smoothstep(0.38, 0.72, sn) * s_fade;
        base_col = base_col * (0.86 + 0.26 * mix(0.5, sh, scoh) + (sn - 0.5) * 0.14 * s_fade) * mix(1.0, 0.64, joint);
        base_col = mix(base_col, vec3(0.11, 0.21, 0.08), moss * 0.45);
        metallic = 0.02;
        roughness = mix(0.76, 0.92, max(joint, moss));
        env_boost = 0.62;
    } else if (v_detail_mode > 5.5 && v_detail_mode < 6.5) {
        float leaf_n1 = euv_noise(vec2(v_world_pos.x * 2.2 + v_world_pos.y * 1.7, v_world_pos.z * 2.2 - v_world_pos.y * 1.3));
        float leaf_n2 = euv_noise(vec2(v_world_pos.x * 6.8 - 2.1, v_world_pos.z * 6.8 + v_world_pos.y * 4.5));
        float crown_lift = smoothstep(0.0, 6.5, v_world_pos.y) * 0.16;
        float var_l = 0.78 + 0.34 * (leaf_n1 * 0.62 + leaf_n2 * 0.38) + crown_lift;
        base_col = base_col * var_l;
        metallic = 0.0;
        roughness = 0.84;
        env_boost = 0.42;
        float back_dot = max(dot(view_dir, normalize(-sun_dir + norm * 0.35)), 0.0);
        float sss_fol = back_dot * back_dot * back_dot * (0.35 + 0.65 * max(sun_dir.y, 0.0));
        emissive += base_col * u_sun_color_ambient.rgb * (sss_fol * 0.36);
        contact_ao = mix(0.66, 1.0, smoothstep(-0.35, 0.75, norm.y));
    } else if (v_detail_mode > 6.5 && v_detail_mode < 7.5) {
        float w_time = u_sky_horizon_tod.w;
        float puff = euv_noise(vec2(v_world_pos.x * 0.14 - w_time * 0.03, v_world_pos.z * 0.14 + v_world_pos.y * 0.22));
        float top_lit = clamp(norm.y * 0.55 + 0.50 + (puff - 0.5) * 0.25, 0.0, 1.0);
        float sun_fwd = clamp(dot(-view_dir, sun_dir), 0.0, 1.0);
        float silver = sun_fwd * sun_fwd * sun_fwd * sun_fwd;
        float sun_luma = clamp(dot(u_sun_color_ambient.rgb, vec3(0.30, 0.55, 0.15)), 0.16, 1.45);
        vec3 under_c = mix(u_sky_zenith_aspect.rgb * 0.58 + u_sky_horizon_tod.rgb * 0.42, vec3(0.48, 0.56, 0.70) * sun_luma, 0.52);
        vec3 crown_c = vec3(1.04, 1.03, 1.01) * sun_luma + u_sun_color_ambient.rgb * (0.16 + 0.32 * silver);
        base_col = mix(under_c, crown_c, top_lit);
        emissive = base_col * 0.52;
        metallic = 0.0;
        roughness = 0.98;
        env_boost = 0.18;
        contact_ao = mix(0.84, 1.0, top_lit);
    } else if (v_detail_mode > 7.5 && v_detail_mode < 8.5 && norm.y > 0.75) {
        vec2 wp = v_world_pos.xz;
        float w_time = u_sky_horizon_tod.w;
        float rw_a = euv_noise(vec2(wp.x * 0.35 + w_time * 0.22, wp.y * 0.28 - w_time * 0.95)) * 2.0;
        float rp1 = wp.x * 0.48 + wp.y * 0.34 + rw_a - w_time * 1.85;
        float rp2 = -wp.x * 0.75 + wp.y * 0.86 - w_time * 2.45;
        float aa_r = 1.0 / (1.0 + fw_xz * 0.65);
        float gx = (cos(rp1) * 0.48 * 0.08 - cos(rp2) * 0.75 * 0.04) * aa_r;
        float gz = (cos(rp1) * 0.34 * 0.08 + cos(rp2) * 0.86 * 0.04) * aa_r;
        water_norm = normalize(vec3(-gx, 1.0, -gz));
        norm = water_norm;
        water_crest = clamp((sin(rp1) * 0.55 + sin(rp2) * 0.45) * 0.5 + 0.5, 0.0, 1.0);
        float caust = pow(abs(sin(wp.x * 1.2 + wp.y * 0.9 + rw_a - w_time * 1.9)), 2.0);
        water_sub_col = vec3(0.022, 0.168, 0.185) + vec3(0.032, 0.115, 0.105) * caust;
        water_surface = 1.0;
        water_is_ocean = 0.85;
        water_rough = 0.034;
        contact_ao = 1.0;
    }
    vec3 dndx = dFdx(norm);
    vec3 dndy = dFdy(norm);
    float norm_var = clamp(dot(dndx, dndx) + dot(dndy, dndy), 0.0, 0.50);
    vec3 dwnx = dFdx(water_norm);
    vec3 dwny = dFdy(water_norm);
    float wn_var = clamp(dot(dwnx, dwnx) + dot(dwny, dwny), 0.0, 0.35);
    float rough_aa = sqrt(clamp(roughness * roughness + min(norm_var * 0.50, 0.18), 0.0025, 1.0));
    vec3 half_vec = normalize(view_dir + sun_dir);
    float ndl = max(dot(norm, sun_dir), 0.0);
    float ndv = max(dot(norm, view_dir), 0.002);
    float ndh = max(dot(norm, half_vec), 0.0);
    float hdv = max(dot(half_vec, view_dir), 0.0);
    float om_hdv = 1.0 - hdv;
    float om_hdv2 = om_hdv * om_hdv;
    float f_pow5 = om_hdv2 * om_hdv2 * om_hdv;
    float r2 = rough_aa * rough_aa;
    float a2 = max(r2 * r2, 0.0004);
    float d_den = ndh * ndh * (a2 - 1.0) + 1.0;
    float ndf = min(a2 / max(3.14159265 * d_den * d_den, 0.0001), 48.0);
    vec3 f0 = mix(vec3(0.04), base_col, metallic);
    vec3 fresnel = f0 + (vec3(1.0) - f0) * f_pow5;
    float k_g = (rough_aa + 1.0);
    k_g = (k_g * k_g) * 0.125;
    float g_v = ndv / (ndv * (1.0 - k_g) + k_g);
    float g_l = ndl / (ndl * (1.0 - k_g) + k_g);
    vec3 spec = (ndf * g_v * g_l * fresnel) / max(4.0 * ndv * ndl, 0.001);
    float clearcoat = (v_detail_mode < 0.5 && (metallic > 0.30 || roughness < 0.22)) ? 0.55 : 0.0;
    if (clearcoat > 0.0) {
        float cc_a2 = clamp(0.0045 + norm_var * 0.45, 0.0045, 0.25);
        float cc_den = ndh * ndh * (cc_a2 - 1.0) + 1.0;
        float cc_ndf = min(cc_a2 / max(3.14159265 * cc_den * cc_den, 0.0001), 24.0);
        float cc_f = 0.04 + 0.96 * f_pow5;
        spec += vec3(cc_ndf * cc_f * clearcoat * 0.32);
    }
    float canyon_shadow = 1.0 - smoothstep(10.8, 22.0, loop_abs_u) * (1.0 - smoothstep(0.0, 14.0, v_world_pos.y)) * (1.0 - smoothstep(138.0, 144.0, v_world_pos.x)) * 0.38;
    vec3 kd = (vec3(1.0) - fresnel) * (1.0 - metallic);
    vec3 direct = (kd * base_col / 3.14159265 + spec) * u_sun_color_ambient.rgb * (ndl * 1.48 * canyon_shadow);
    vec3 refl_dir = reflect(-view_dir, norm);
    float refl_y = clamp(refl_dir.y * 0.5 + 0.5, 0.0, 1.0);
    vec3 env_refl = mix(u_sky_horizon_tod.rgb, u_sky_zenith_aspect.rgb, refl_y);
    float sun_refl = pow(max(dot(refl_dir, sun_dir), 1.0e-5), mix(96.0, 12.0, rough_aa));
    env_refl += u_sun_color_ambient.rgb * sun_refl * (1.35 - rough_aa * 0.9);
    float facade_refl_band = smoothstep(0.18, 0.85, abs(refl_dir.x)) * (1.0 - smoothstep(0.05, 0.58, abs(refl_dir.y)));
    if (facade_refl_band > 0.01 && (env_boost > 1.05 || metallic > 0.30 || roughness < 0.28)) {
        float win_u = v_world_pos.z * 0.22 + refl_dir.z * 6.0;
        float win_v = refl_dir.y * 9.0 + v_world_pos.y * 0.18;
        float refl_blur = clamp(rough_aa * 2.0 + 0.12, 0.12, 1.25);
        float win_h = euv_hash(floor(vec2(win_u, win_v)));
        vec3 win_col = (win_h < 0.35) ? vec3(1.15, 0.78, 0.38) : ((win_h < 0.62) ? vec3(0.28, 0.86, 1.20) : ((win_h < 0.80) ? vec3(1.05, 0.32, 0.70) : vec3(0.12, 0.16, 0.24)));
        float win_coh = 1.0 - smoothstep(0.10, 0.28, refl_blur);
        float win_grid = euv_box_pulse(win_u, 0.14, 0.86, refl_blur);
        env_refl += mix(vec3(0.68, 0.58, 0.56), win_col, win_coh) * (facade_refl_band * win_grid * (0.42 + 0.55 * (1.0 - rough_aa)));
    }
    vec3 upper_gi = u_sky_zenith_aspect.rgb * 0.92 + u_sky_horizon_tod.rgb * 0.36;
    float sky_w = norm.y * 0.5 + 0.5;
    vec3 hemi = mix(u_sky_horizon_tod.rgb * 0.44 + vec3(0.05, 0.06, 0.09), upper_gi, sky_w);
    float om_ndv = 1.0 - ndv;
    float om_ndv2 = om_ndv * om_ndv;
    vec3 f_env = f0 + (max(vec3(1.0 - rough_aa), f0) - f0) * (om_ndv2 * om_ndv2 * om_ndv);
    vec3 ambient = (base_col * (1.0 - metallic * 0.65) * hemi + env_refl * f_env * env_boost) * (0.46 + u_sun_color_ambient.w) * contact_ao;
    vec3 hdr = direct + ambient + local_lamp_light + emissive;
    if (water_surface > 0.001) {
        float w_rough_aa = sqrt(clamp(water_rough * water_rough + min(wn_var * 0.65, 0.14) + clamp(dist * 0.00035, 0.0, 0.045), 0.0009, 0.55));
        float w_ndv = max(dot(water_norm, view_dir), 0.002);
        float w_ndl = max(dot(water_norm, sun_dir), 0.0);
        float w_ndh = max(dot(water_norm, half_vec), 0.0);
        float w_om = 1.0 - w_ndv;
        float w_om2 = w_om * w_om;
        float f_water_v = 0.02 + 0.98 * (w_om2 * w_om2 * w_om);
        float f_water_h = 0.02 + 0.98 * f_pow5;
        float w_r2 = w_rough_aa * w_rough_aa;
        float w_a2 = max(w_r2 * w_r2, 0.00018);
        float w_den = w_ndh * w_ndh * (w_a2 - 1.0) + 1.0;
        float w_ndf = min(w_a2 / max(3.14159265 * w_den * w_den, 0.0001), 42.0);
        float w_kg = (w_rough_aa + 1.0);
        w_kg = (w_kg * w_kg) * 0.125;
        float w_gv = w_ndv / (w_ndv * (1.0 - w_kg) + w_kg);
        float w_gl = w_ndl / (w_ndl * (1.0 - w_kg) + w_kg);
        vec3 w_sun_spec = u_sun_color_ambient.rgb * ((w_ndf * w_gv * w_gl * f_water_h) / max(4.0 * w_ndv * w_ndl, 0.001) * w_ndl * 1.48 * canyon_shadow);
        vec3 w_refl_dir = reflect(-view_dir, water_norm);
        vec3 w_env_refl = (water_is_ocean > 0.5)
            ? mix(u_sky_horizon_tod.rgb, u_sky_zenith_aspect.rgb, smoothstep(0.0, 0.72, clamp(w_refl_dir.y, 0.0, 1.0)))
                + u_sun_color_ambient.rgb * (pow(max(dot(w_refl_dir, sun_dir), 1.0e-5), mix(96.0, 12.0, w_rough_aa)) * (1.45 - w_rough_aa * 0.9))
            : euv_trace_city_reflection(
                v_world_pos,
                w_refl_dir,
                sun_dir,
                u_sky_horizon_tod.rgb,
                u_sky_zenith_aspect.rgb,
                u_sun_color_ambient.rgb,
                w_rough_aa
            );
        vec3 sub_direct = (water_sub_col / 3.14159265) * u_sun_color_ambient.rgb * (max(sun_dir.y, 0.0) * 1.48 * canyon_shadow);
        vec3 sub_amb = water_sub_col * upper_gi * contact_ao * (0.46 + u_sun_color_ambient.w);
        vec3 ocean_sss = vec3(0.0);
        if (water_is_ocean > 0.001) {
            float sss_dot = max(dot(view_dir, normalize(-sun_dir + water_norm * 0.45)), 0.0);
            float sss_pow = sss_dot * sss_dot * sss_dot;
            ocean_sss = vec3(0.04, 0.36, 0.34) * (sss_pow * smoothstep(0.20, 0.85, water_crest) * (0.55 + 0.45 * max(sun_dir.y, 0.0)) * water_is_ocean);
        }
        vec3 sub_lit = sub_direct + sub_amb + water_lamp_diff + ocean_sss;
        vec3 water_col = (1.0 - f_water_v) * sub_lit + f_water_v * w_env_refl * mix(0.76, 0.96, contact_ao) + w_sun_spec + water_lamp_spec;
        if (water_foam > 0.001) {
            vec3 foam_lit = vec3(0.84, 0.90, 0.94) * (u_sun_color_ambient.rgb * (max(sun_dir.y, 0.0) * 0.55) + upper_gi * 0.65);
            water_col = mix(water_col, foam_lit, water_foam);
        }
        hdr = mix(hdr, water_col + emissive, water_surface);
    }
    float fog_t = 1.0 - exp(-dist * u_sun_dir_fog.w);
    float sun_scatter = pow(max(dot(-view_dir, sun_dir), 1.0e-5), 6.0);
    vec3 fog_color = u_sky_horizon_tod.rgb + u_sun_color_ambient.rgb * (sun_scatter * 0.36);
    hdr = mix(hdr, fog_color, clamp(fog_t, 0.0, 0.86));
    out_color = vec4(euv_aces(hdr * u_camera_pos.w), 1.0);
}
"#;