euv-core 0.53.1

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
/// Win32 window class name registered for euv native windows.
pub(crate) const WIN32_CLASS_NAME: &str = "EuvNativeWindowClass";

/// Win32 `user32.dll` module name for dynamic Per-Monitor DPI awareness lookup.
pub(crate) const WIN32_DLL_USER32: &str = "user32.dll";

/// Win32 dynamic symbol name for `SetProcessDpiAwarenessContext`.
pub(crate) const WIN32_SYM_SET_DPI_CONTEXT: &str = "SetProcessDpiAwarenessContext\0";

/// Win32 `DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2` sentinel (`(HANDLE)-4`).
pub(crate) const DPI_AWARENESS_CONTEXT_PER_MONITOR_V2: isize = -4;

/// Win32 standard overlapped window style (`WS_OVERLAPPEDWINDOW`).
pub(crate) const WS_OVERLAPPEDWINDOW: u32 = 0x00CF_0000;

/// Win32 popup borderless window style (`WS_POPUP`).
pub(crate) const WS_POPUP: u32 = 0x8000_0000;

/// Win32 `SW_SHOW` command constant.
pub(crate) const SW_SHOW: i32 = 5;

/// Win32 `SW_HIDE` command constant.
pub(crate) const SW_HIDE: i32 = 0;

/// Win32 `HWND_TOPMOST` sentinel.
pub(crate) const HWND_TOPMOST: isize = -1;

/// Win32 `HWND_NOTOPMOST` sentinel.
pub(crate) const HWND_NOTOPMOST: isize = -2;

/// Win32 `SWP_NOMOVE | SWP_NOSIZE` flags.
pub(crate) const SWP_NOMOVE_NOSIZE: u32 = 0x0003;

/// Win32 `SWP_NOMOVE | SWP_NOZORDER` flags.
pub(crate) const SWP_NOMOVE_NOZORDER: u32 = 0x0006;

/// Win32 `SWP_NOZORDER | SWP_NOACTIVATE` flags honoring `WM_DPICHANGED` suggested rectangles.
pub(crate) const SWP_NOZORDER_NOACTIVATE: u32 = 0x0014;

/// Win32 `BI_RGB` uncompressed bitmap compression constant.
pub(crate) const BI_RGB: u32 = 0;

/// Win32 `DIB_RGB_COLORS` color table usage constant.
pub(crate) const DIB_RGB_COLORS: u32 = 0;

/// Win32 `SRCCOPY` raster operation code.
pub(crate) const SRCCOPY: u32 = 0x00CC_0020;

/// Win32 `HALFTONE` high-quality stretch blit mode for GDI `SetStretchBltMode`.
pub(crate) const STRETCH_HALFTONE: i32 = 4;

/// Win32 GDI `GetDeviceCaps` index for vertical display refresh rate in Hertz (`VREFRESH`).
pub(crate) const GDI_VREFRESH: i32 = 116;

/// Win32 `PM_REMOVE` flag for `PeekMessageW`.
pub(crate) const PM_REMOVE: u32 = 0x0001;

/// Win32 `WM_DESTROY` message constant.
pub(crate) const WM_DESTROY: u32 = 0x0002;

/// Win32 `WM_SIZE` message constant.
pub(crate) const WM_SIZE: u32 = 0x0005;

/// Win32 `SIZE_MINIMIZED` `w_param` value for `WM_SIZE`.
pub(crate) const SIZE_MINIMIZED: usize = 1;

/// Win32 `WM_SETFOCUS` message constant.
pub(crate) const WM_SETFOCUS: u32 = 0x0007;

/// Win32 `WM_KILLFOCUS` message constant.
pub(crate) const WM_KILLFOCUS: u32 = 0x0008;

/// Win32 `WM_PAINT` message constant.
pub(crate) const WM_PAINT: u32 = 0x000F;

/// Win32 `WM_CLOSE` message constant.
pub(crate) const WM_CLOSE: u32 = 0x0010;

/// Win32 `WM_SETTINGCHANGE` message constant.
pub(crate) const WM_SETTINGCHANGE: u32 = 0x001A;

/// Win32 `WM_ACTIVATEAPP` message constant.
pub(crate) const WM_ACTIVATEAPP: u32 = 0x001C;

/// Win32 `WM_CANCELMODE` message constant sent when OS dialogs or mode transitions cancel active pointer drags.
pub(crate) const WM_CANCELMODE: u32 = 0x001F;

/// Win32 `WM_CAPTURECHANGED` message constant sent when mouse capture is lost during a drag.
pub(crate) const WM_CAPTURECHANGED: u32 = 0x0215;

/// Win32 `WM_POWERBROADCAST` message constant.
pub(crate) const WM_POWERBROADCAST: u32 = 0x0218;

/// Win32 `PBT_POWERSTATUSCHANGE` `w_param` value for `WM_POWERBROADCAST`.
pub(crate) const PBT_POWERSTATUSCHANGE: usize = 0x000A;

/// Win32 `WM_THEMECHANGED` message constant.
pub(crate) const WM_THEMECHANGED: u32 = 0x031A;

/// Win32 `WM_CLIPBOARDUPDATE` message constant.
pub(crate) const WM_CLIPBOARDUPDATE: u32 = 0x031D;

/// Win32 `WM_KEYDOWN` message constant.
pub(crate) const WM_KEYDOWN: u32 = 0x0100;

/// Win32 `WM_KEYUP` message constant.
pub(crate) const WM_KEYUP: u32 = 0x0101;

/// Win32 `WM_CHAR` message constant.
pub(crate) const WM_CHAR: u32 = 0x0102;

/// Win32 `WM_MOUSEMOVE` message constant.
pub(crate) const WM_MOUSEMOVE: u32 = 0x0200;

/// Win32 `WM_LBUTTONDOWN` message constant.
pub(crate) const WM_LBUTTONDOWN: u32 = 0x0201;

/// Win32 `WM_LBUTTONUP` message constant.
pub(crate) const WM_LBUTTONUP: u32 = 0x0202;

/// Win32 `WM_RBUTTONDOWN` message constant.
pub(crate) const WM_RBUTTONDOWN: u32 = 0x0204;

/// Win32 `WM_RBUTTONUP` message constant.
pub(crate) const WM_RBUTTONUP: u32 = 0x0205;

/// Win32 `WM_MBUTTONDOWN` message constant.
pub(crate) const WM_MBUTTONDOWN: u32 = 0x0207;

/// Win32 `WM_MBUTTONUP` message constant.
pub(crate) const WM_MBUTTONUP: u32 = 0x0208;

/// Win32 `WM_MOUSEWHEEL` message constant.
pub(crate) const WM_MOUSEWHEEL: u32 = 0x020A;

/// Win32 `WM_DPICHANGED` message constant.
pub(crate) const WM_DPICHANGED: u32 = 0x02E0;

/// Win32 `VK_SHIFT` virtual key code.
pub(crate) const VK_SHIFT: i32 = 0x10;

/// Win32 `VK_CONTROL` virtual key code.
pub(crate) const VK_CONTROL: i32 = 0x11;

/// Win32 `VK_MENU` (Alt) virtual key code.
pub(crate) const VK_MENU: i32 = 0x12;

/// Win32 `VK_LWIN` virtual key code.
pub(crate) const VK_LWIN: i32 = 0x5B;

/// Win32 `VK_RWIN` virtual key code.
pub(crate) const VK_RWIN: i32 = 0x5C;

/// Win32 default 96 DPI baseline.
pub(crate) const WIN32_DEFAULT_DPI: f32 = 96.0;

/// Win32 `dxgi.dll` module name for dynamic DXGI 1.5/1.6 waitable swapchain & tearing probe.
pub(crate) const WIN32_DLL_DXGI: &str = "dxgi.dll";

/// Win32 dynamic symbol name for `CreateDXGIFactory1` in `dxgi.dll`.
pub(crate) const WIN32_SYM_CREATE_DXGI_FACTORY1: &str = "CreateDXGIFactory1\0";

/// Win32 `SWP_NOOWNERZORDER | SWP_FRAMECHANGED` flags for borderless fullscreen transition.
pub(crate) const SWP_FULLSCREEN_ENTER: u32 = 0x0220;

/// Win32 `SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED` flags for fullscreen restore.
pub(crate) const SWP_FULLSCREEN_EXIT: u32 = 0x0227;

/// Win32 `GWL_STYLE` index (`-16`).
pub(crate) const GWL_STYLE: i32 = -16;

/// Win32 `MONITOR_DEFAULTTONEAREST` flag (`2`) for `MonitorFromWindow`.
pub(crate) const MONITOR_DEFAULTTONEAREST: u32 = 2;

/// Win32 `ENUM_CURRENT_SETTINGS` sentinel (`u32::MAX`) for `EnumDisplaySettingsW`.
pub(crate) const ENUM_CURRENT_SETTINGS: u32 = u32::MAX;

/// Win32 `WM_DISPLAYCHANGE` message constant (`0x007E`).
pub(crate) const WM_DISPLAYCHANGE: u32 = 0x007E;

/// Win32 `CREATE_WAITABLE_TIMER_HIGH_RESOLUTION` flag (`0x0000_0002`, Windows 10 1803+) breaking the 15.625ms sleep cap.
pub(crate) const CREATE_WAITABLE_TIMER_HIGH_RESOLUTION: u32 = 0x0000_0002;

/// Win32 `TIMER_ALL_ACCESS` desired access mask (`0x001F_0003`) for `CreateWaitableTimerExW`.
pub(crate) const TIMER_ALL_ACCESS: u32 = 0x001F_0003;

/// Win32 `INFINITE` wait timeout (`u32::MAX`) for `WaitForSingleObject`.
pub(crate) const WIN32_WAIT_INFINITE: u32 = u32::MAX;

/// DXGI `DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT` (`0x0040`).
pub(crate) const DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT: u32 = 0x0040;

/// DXGI `DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING` (`0x0800`).
pub(crate) const DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING: u32 = 0x0800;

/// DXGI `DXGI_PRESENT_ALLOW_TEARING` (`0x0200`) flag for low-latency VRR / unlocked high-Hz presentation.
pub(crate) const DXGI_PRESENT_ALLOW_TEARING: u32 = 0x0200;

/// Win32 `CW_USEDEFAULT` position sentinel.
pub(crate) const CW_USEDEFAULT: i32 = -2_147_483_648;

/// Win32 `QS_ALLINPUT` wake mask (`0x04FF`) for blocking `MsgWaitForMultipleObjects`.
pub(crate) const WIN32_QS_ALLINPUT: u32 = 0x04FF;

/// Null-terminated ASCII `dxgi.dll` library name for `LoadLibraryA`.
pub(crate) const WIN32_DLL_DXGI_ASCII: &str = "dxgi.dll\0";

/// Null-terminated ASCII `d3d11.dll` library name for `LoadLibraryA`.
pub(crate) const WIN32_DLL_D3D11_ASCII: &str = "d3d11.dll\0";

/// Null-terminated ASCII `d3dcompiler_47.dll` library name for `LoadLibraryA`.
pub(crate) const WIN32_DLL_D3DCOMPILER_ASCII: &str = "d3dcompiler_47.dll\0";

/// Null-terminated ASCII symbol name for `D3D11CreateDevice`.
pub(crate) const WIN32_SYM_D3D11_CREATE_DEVICE: &str = "D3D11CreateDevice\0";

/// Null-terminated ASCII symbol name for `D3DCompile`.
pub(crate) const WIN32_SYM_D3D_COMPILE: &str = "D3DCompile\0";

/// Null-terminated HLSL vertex shader entry point matching `HLSL_UI_SHADER`.
pub(crate) const WIN32_HLSL_ENTRY_VS: &str = "VSMain\0";

/// Null-terminated HLSL pixel shader entry point matching `HLSL_UI_SHADER`.
pub(crate) const WIN32_HLSL_ENTRY_PS: &str = "PSMain\0";

/// Null-terminated HLSL shader model 5.0 vertex profile.
pub(crate) const WIN32_HLSL_TARGET_VS: &str = "vs_5_0\0";

/// Null-terminated HLSL shader model 5.0 pixel profile.
pub(crate) const WIN32_HLSL_TARGET_PS: &str = "ps_5_0\0";

/// Null-terminated HLSL fullscreen scene triangle vertex entry point.
pub(crate) const WIN32_HLSL_ENTRY_SCENE_FS_VS: &str = "SceneFsVS\0";

/// Null-terminated HLSL optical ocean pixel entry point.
pub(crate) const WIN32_HLSL_ENTRY_OCEAN_PS: &str = "OceanPS\0";

/// Null-terminated HLSL origami boat vertex entry point.
pub(crate) const WIN32_HLSL_ENTRY_BOAT_VS: &str = "BoatVS\0";

/// Null-terminated HLSL origami boat pixel entry point.
pub(crate) const WIN32_HLSL_ENTRY_BOAT_PS: &str = "BoatPS\0";

/// Null-terminated HLSL 3D atmospheric sky pixel entry point.
pub(crate) const WIN32_HLSL_ENTRY_SKY3D_PS: &str = "Sky3dPS\0";

/// Null-terminated HLSL instanced 3D PBR mesh vertex entry point.
pub(crate) const WIN32_HLSL_ENTRY_MESH3D_VS: &str = "Mesh3dVS\0";

/// Null-terminated HLSL instanced 3D PBR mesh pixel entry point.
pub(crate) const WIN32_HLSL_ENTRY_MESH3D_PS: &str = "Mesh3dPS\0";

/// `DXGI_FORMAT_D24_UNORM_S8_UINT` (`45`) for 3D offscreen depth-stencil buffers.
pub(crate) const DXGI_FORMAT_D24_UNORM_S8_UINT: u32 = 45;

/// `DXGI_FEATURE_PRESENT_ALLOW_TEARING` (`0`) queried on `IDXGIFactory5::CheckFeatureSupport`.
pub(crate) const DXGI_FEATURE_PRESENT_ALLOW_TEARING: u32 = 0;

/// `D3D11_BIND_RENDER_TARGET` (`0x0020`).
pub(crate) const D3D11_BIND_RENDER_TARGET: u32 = 0x0020;

/// `D3D11_BIND_DEPTH_STENCIL` (`0x0040`).
pub(crate) const D3D11_BIND_DEPTH_STENCIL: u32 = 0x0040;

/// `D3D11_CLEAR_DEPTH` (`1`) clear flag for `ClearDepthStencilView`.
pub(crate) const D3D11_CLEAR_DEPTH: u32 = 1;

/// `D3D11_DEPTH_WRITE_MASK_ZERO` (`0`).
pub(crate) const D3D11_DEPTH_WRITE_MASK_ZERO: u32 = 0;

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

/// `D3D11_COMPARISON_LESS` (`2`).
pub(crate) const D3D11_COMPARISON_LESS: u32 = 2;

/// `D3D11_COMPARISON_ALWAYS` (`8`).
pub(crate) const D3D11_COMPARISON_ALWAYS: u32 = 8;

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

/// `DXGI_FORMAT_UNKNOWN` (`0`) for structured buffer SRVs.
pub(crate) const DXGI_FORMAT_UNKNOWN: u32 = 0;

/// `DXGI_FORMAT_R8G8B8A8_UNORM` (`28`) for swapchain backbuffers and RGBA8 textures.
pub(crate) const DXGI_FORMAT_R8G8B8A8_UNORM: u32 = 28;

/// `DXGI_FORMAT_R8_UNORM` (`61`) for monochrome R8 glyph atlases.
pub(crate) const DXGI_FORMAT_R8_UNORM: u32 = 61;

/// `DXGI_USAGE_RENDER_TARGET_OUTPUT` (`0x20`).
pub(crate) const DXGI_USAGE_RENDER_TARGET_OUTPUT: u32 = 0x0020;

/// `DXGI_SWAP_EFFECT_FLIP_DISCARD` (`4`) for modern DXGI 1.6 flip-model presentation.
pub(crate) const DXGI_SWAP_EFFECT_FLIP_DISCARD: u32 = 4;

/// `DXGI_SCALING_NONE` (`1`) preserving exact 1:1 physical pixels without DWM stretch.
pub(crate) const DXGI_SCALING_NONE: u32 = 1;

/// `DXGI_ALPHA_MODE_IGNORE` (`3`).
pub(crate) const DXGI_ALPHA_MODE_IGNORE: u32 = 3;

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

/// `D3D_FEATURE_LEVEL_11_0` (`0xB000`).
pub(crate) const D3D_FEATURE_LEVEL_11_0: u32 = 0xB000;

/// `D3D11_SDK_VERSION` (`7`).
pub(crate) const D3D11_SDK_VERSION: u32 = 7;

/// `D3D11_USAGE_DEFAULT` (`0`).
pub(crate) const D3D11_USAGE_DEFAULT: u32 = 0;

/// `D3D11_USAGE_DYNAMIC` (`2`).
pub(crate) const D3D11_USAGE_DYNAMIC: u32 = 2;

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

/// `D3D11_BIND_SHADER_RESOURCE` (`0x0008`).
pub(crate) const D3D11_BIND_SHADER_RESOURCE: u32 = 0x0008;

/// `D3D11_CPU_ACCESS_WRITE` (`0x0001_0000`).
pub(crate) const D3D11_CPU_ACCESS_WRITE: u32 = 0x0001_0000;

/// `D3D11_RESOURCE_MISC_BUFFER_STRUCTURED` (`0x0040`).
pub(crate) const D3D11_RESOURCE_MISC_BUFFER_STRUCTURED: u32 = 0x0040;

/// `D3D11_MAP_WRITE_DISCARD` (`4`).
pub(crate) const D3D11_MAP_WRITE_DISCARD: u32 = 4;

/// `D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST` (`4`).
pub(crate) const D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST: u32 = 4;

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

/// `D3D11_SRV_DIMENSION_TEXTURE2D` (`4`).
pub(crate) const D3D11_SRV_DIMENSION_TEXTURE2D: u32 = 4;

/// `D3D11_FILL_SOLID` (`3`).
pub(crate) const D3D11_FILL_SOLID: u32 = 3;

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

/// `D3D11_BLEND_ONE` (`2`).
pub(crate) const D3D11_BLEND_ONE: u32 = 2;

/// `D3D11_BLEND_SRC_ALPHA` (`5`).
pub(crate) const D3D11_BLEND_SRC_ALPHA: u32 = 5;

/// `D3D11_BLEND_INV_SRC_ALPHA` (`6`).
pub(crate) const D3D11_BLEND_INV_SRC_ALPHA: u32 = 6;

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

/// `D3D11_COLOR_WRITE_ENABLE_ALL` (`0x0F`).
pub(crate) const D3D11_COLOR_WRITE_ENABLE_ALL: u8 = 0x0F;

/// `D3D11_FILTER_MIN_MAG_MIP_LINEAR` (`0x15`).
pub(crate) const D3D11_FILTER_MIN_MAG_MIP_LINEAR: u32 = 0x15;

/// `D3D11_TEXTURE_ADDRESS_CLAMP` (`3`).
pub(crate) const D3D11_TEXTURE_ADDRESS_CLAMP: u32 = 3;

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

/// COM IID for `IDXGIFactory2` (`{50c83a1c-e072-4c48-87b0-3630fa36a6d0}`).
pub(crate) const IID_IDXGI_FACTORY2: [u8; 16] = [
    0x1C, 0x3A, 0xC8, 0x50, 0x72, 0xE0, 0x48, 0x4C, 0x87, 0xB0, 0x36, 0x30, 0xFA, 0x36, 0xA6, 0xD0,
];

/// COM IID for `IDXGISwapChain2` (`{a8be2ac4-199f-4946-b331-79599fb98de7}`).
pub(crate) const IID_IDXGI_SWAP_CHAIN2: [u8; 16] = [
    0xC4, 0x2A, 0xBE, 0xA8, 0x9F, 0x19, 0x46, 0x49, 0xB3, 0x31, 0x79, 0x59, 0x9F, 0xB9, 0x8D, 0xE7,
];

/// COM IID for `ID3D11Texture2D` (`{6f15aaf2-d208-4e89-9ab4-489535d34f9c}`).
pub(crate) const IID_ID3D11_TEXTURE2D: [u8; 16] = [
    0xF2, 0xAA, 0x15, 0x6F, 0x08, 0xD2, 0x89, 0x4E, 0x9A, 0xB4, 0x48, 0x95, 0x35, 0xD3, 0x4F, 0x9C,
];

/// Error text emitted when Windows D3D11/DXGI direct GPU presentation cannot be initialized.
pub(crate) const ERR_WIN32_D3D11_PRESENT: &str =
    "Windows D3D11/DXGI direct GPU presentation unavailable";

/// Error message when `CreateWindowExW` returns a null `HWND`.
pub(crate) const ERR_WIN32_CREATE_WINDOW: &str = "CreateWindowExW returned null HWND";

/// Error message when native DXGI/D3D11 direct GPU swapchain presentation is requested without DXGI availability.
pub(crate) const ERR_WIN32_DXGI_UNAVAILABLE: &str =
    "Windows direct GPU presentation requires DXGI swapchain availability and live HWND";

/// COM IID for `IDXGIFactory5` (`{7632e1f5-ee65-4dca-87fd-84cd75f8838d}`).
pub(crate) const IID_IDXGI_FACTORY5: [u8; 16] = [
    0xF5, 0xE1, 0x32, 0x76, 0x65, 0xEE, 0xCA, 0x4D, 0x87, 0xFD, 0x84, 0xCD, 0x75, 0xF8, 0x83, 0x8D,
];

/// Error message when GDI software presentation lacks a valid `HWND` or device context (`HDC`).
pub(crate) const ERR_WIN32_GDI_SURFACE_MISSING: &str =
    "Windows GDI software presentation requires a live HWND and device context";

/// Hardware HLSL SM 5.0 8-wave isotropic Nyquist-AA Gerstner ocean, C2 quintic storm sky, Toksvig dual-lobe GGX sun glitter, PBF wake, and hull foam shader.
pub(crate) const HLSL_SCENE_OCEAN_SHADER: &str = r#"
cbuffer OceanUniforms : register(b0) {
    float4 u_time_agit_hor_aspect;
    float4 u_roll_boat_uv;
    float4 u_sun_dir_wake;
    float4 u_view_dims;
};
Texture2D<float4> WakeTex : register(t0);
SamplerState LinearSampler : register(s0);
struct FsOut {
    float4 pos : SV_POSITION;
    float2 uv : TEXCOORD0;
};
FsOut SceneFsVS(uint vid : SV_VertexID) {
    float2 p = float2((vid == 1u) ? 3.0 : -1.0, (vid == 2u) ? 3.0 : -1.0);
    FsOut o;
    o.pos = float4(p, 0.0, 1.0);
    o.uv = float2(p.x * 0.5 + 0.5, 0.5 - p.y * 0.5);
    return o;
}
float3 euv_aces(float3 hdr) {
    float3 x = max(hdr, float3(0.0, 0.0, 0.0));
    float luma = dot(x, float3(0.2126, 0.7152, 0.0722));
    float sat_roll = 1.0 / (1.0 + luma * 0.14);
    float3 agx_in = lerp(float3(luma, luma, luma), x, 0.78 + 0.22 * sat_roll);
    float3 num = agx_in * (2.51 * agx_in + 0.03);
    float3 den = agx_in * (2.43 * agx_in + 0.59) + 0.14;
    return pow(clamp(num / max(den, float3(0.0001, 0.0001, 0.0001)), float3(0.0, 0.0, 0.0), float3(1.0, 1.0, 1.0)), float3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2));
}
float euv_hash_i2(int2 cell) {
    uint n = asuint(cell.x) * 374761393u + asuint(cell.y) * 668265263u;
    n = (n ^ (n >> 13u)) * 1274126177u;
    n ^= n >> 16u;
    return (float)(n & 0x00FFFFFFu) * (1.0 / 16777215.0);
}
float euv_noise(float2 p) {
    float2 fl = floor(p);
    float2 f = p - fl;
    float2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
    int2 i = (int2)fl;
    float a = euv_hash_i2(i);
    float b = euv_hash_i2(i + int2(1, 0));
    float c = euv_hash_i2(i + int2(0, 1));
    float d = euv_hash_i2(i + int2(1, 1));
    return lerp(lerp(a, b, u.x), lerp(c, d, u.x), u.y);
}
float euv_cloud_fbm(float2 p, int octs) {
    float val = 0.0;
    float amp = 0.54;
    float2 q = p;
    [unroll]
    for (int i = 0; i < 4; ++i) {
        if (i >= octs) {
            break;
        }
        val += euv_noise(q) * amp;
        q = float2(q.x * 1.92 - q.y * 0.58 + 3.1, q.x * 0.58 + q.y * 1.92 - 1.7);
        amp *= 0.50;
    }
    return saturate(val);
}
float3 euv_sky(float3 ray, float3 sun_dir, float2 sun_az_dir, float sim_time, bool high_detail) {
    float ry = max(ray.y, 0.0);
    float sdot = saturate(dot(ray, sun_dir));
    float sp2 = sdot * sdot;
    float sp4 = sp2 * sp2;
    float sp16 = sp4 * sp4 * sp4 * sp4;
    float sp64 = sp16 * sp16 * sp16 * sp16;
    float az_d = saturate(dot(normalize(float2(ray.x, ray.z)), sun_az_dir));
    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);
    float3 horiz_mist = float3(0.56, 0.70, 0.76)
        + float3(0.62, 0.34, 0.08) * (sun_az_glow * (0.45 + 0.55 * horiz_band))
        + float3(0.85, 0.58, 0.24) * sp16
        + float3(1.25, 0.96, 0.58) * sp64;
    float3 mid_sky = high_detail ? float3(0.11, 0.30, 0.42) : float3(0.12, 0.32, 0.70);
    float3 zenith = high_detail ? float3(0.042, 0.165, 0.275) : float3(0.050, 0.175, 0.52);
    float sky_t1 = smoothstep(0.0, 0.22, ry);
    float sky_t2 = smoothstep(0.14, 0.68, ry);
    float3 base_sky = lerp(lerp(horiz_mist, mid_sky, sky_t1), zenith, sky_t2);
    base_sky += float3(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;
        float2 base_c = ray.xz * (t_shell * 0.46) + float2(sim_time * 0.016, -sim_time * 0.009);
        float warp_a = euv_noise(base_c * 0.48 + float2(3.1, -1.4));
        float warp_b = high_detail ? euv_noise(base_c * 1.35 + float2(-2.3, 5.7)) : 0.5;
        float2 c_uv = base_c + float2((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 + float2(8.4, -3.9)) * 2.0 - 1.0)) : 0.0;
        float puff_b = high_detail ? (1.0 - abs(euv_noise(c_uv * 5.2 + float2(1.9, -2.6)) * 2.0 - 1.0)) : 0.0;
        float cloud_field = saturate(cloud_raw * 0.76 + puff_a * 0.16 + puff_b * 0.08);
        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 = saturate((cloud_field - light_sample) * 3.1 + 0.54);
        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);
        float3 cloud_shadow = float3(0.32, 0.44, 0.58);
        float3 cloud_bright = float3(1.22, 1.18, 1.08);
        float3 cloud_col = lerp(cloud_shadow, cloud_bright, saturate(sun_lit * 0.72 + puff_top * 0.28));
        cloud_col += float3(1.18, 0.94, 0.62) * (silver_gold * 0.56);
        float dist_aerial = smoothstep(0.015, 0.17, ry);
        cloud_col = lerp(horiz_mist, cloud_col, dist_aerial);
        cloud_alpha = clamp(cloud_cov * 0.90 * horiz_clear, 0.0, 0.92);
        base_sky = lerp(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 += (float3(4.6, 3.95, 2.95) * disk + float3(1.35, 1.02, 0.60) * corona_inner + float3(0.32, 0.24, 0.13) * corona_outer) * sun_trans;
    }
    return base_sky;
}
float4 OceanPS(FsOut input) : SV_TARGET {
    static const float INV_TAU = 0.15915494;
    static const float TAU = 6.283185307;
    float2 v_uv = input.uv;
    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;
    float2 boat_uv = u_roll_boat_uv.zw;
    float3 sun_dir = normalize(u_sun_dir_wake.xyz);
    float2 sun_az_dir = normalize(float2(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;
    float3 ray_u = float3(rx_u, ry_u, 1.62);
    float inv_rlen = rsqrt(dot(ray_u, ray_u));
    float3 ray = ray_u * inv_rlen;
    float inv_res_y = (u_view_dims.y > 0.5) ? u_view_dims.w : max(abs(ddy(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;
    float3 hdr;
    if (ray.y >= 0.0) {
        hdr = euv_sky(ray, sun_dir, sun_az_dir, sim_time, true);
    } 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);
        float2 wp0 = ray.xz * dist;
        if (dist < 18.0) {
            float p_fade = 1.0 - smoothstep(10.0, 18.0, dist);
            float t0 = frac(sim_time * 0.84 * INV_TAU);
            float ph0 = (frac(dot(wp0, float2(0.2419, 0.9703)) * 0.56 * INV_TAU) + t0) * TAU;
            float h_p = 0.205 * agitation * (exp(sin(ph0) - 1.0) - 0.42) * p_fade;
            dist = clamp((1.42 - h_p) / neg_ry_true, 0.5, 600.0);
            wp0 = ray.xz * dist;
        }
        static const float4 waves[8] = {
            float4( 0.2419,  0.9703,  0.56, 0.205),
            float4(-0.5592,  0.8290,  0.94, 0.145),
            float4( 0.7986,  0.6018,  1.58, 0.092),
            float4(-0.9455,  0.3256,  2.63, 0.056),
            float4( 0.5299,  0.8480,  4.38, 0.034),
            float4(-0.7880, -0.6157,  7.21, 0.021),
            float4( 0.7314, -0.6820, 11.85, 0.013),
            float4(-0.3746,  0.9272, 19.30, 0.0078)
        };
        static const float speeds[8] = { 0.84, 1.12, 1.48, 1.92, 2.46, 3.08, 3.82, 4.68 };
        float2 fwd_xz = normalize(ray.xz);
        float2 right_xz = float2(fwd_xz.y, -fwd_xz.x);
        float2 wp = wp0;
        float h = 0.0;
        float2 grad = float2(0.0, 0.0);
        float crest_acc = 0.0;
        float normal_var = 0.0009;
        for (int wi = 0; wi < 8; ++wi) {
            float2 wdir = waves[wi].xy;
            float freq = waves[wi].z;
            float amp = waves[wi].w * agitation;
            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 = waves[wi].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 = frac(sim_time * speeds[wi] * INV_TAU);
                float s_ph = frac(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.42 + 0.58 * 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.78 * aa_w);
            }
        }
        float near_steep = 1.0 + 0.36 * exp(-dist * 0.082);
        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.0012 * (1.0 - cap_aa1 * cap_aa1) * min(agitation, 1.45);
        if (cap_aa1 > 0.001) {
            float p1a = (frac(dot(wp0, float2( 0.8572,  0.5150)) * 14.2 * INV_TAU) + frac(sim_time * 3.7 * INV_TAU)) * TAU;
            float p1b = (frac(dot(wp0, float2(-0.4226,  0.9063)) * 18.7 * INV_TAU) + frac(sim_time * 4.4 * INV_TAU)) * TAU;
            float p1c = (frac(dot(wp0, float2( 0.9613, -0.2756)) * 23.4 * INV_TAU) + frac(sim_time * 5.1 * INV_TAU)) * TAU;
            float g1a = cos(p1a) * 0.044 * cap_aa1 * agitation;
            float g1b = cos(p1b) * 0.036 * cap_aa1 * agitation;
            float g1c = cos(p1c) * 0.028 * cap_aa1 * agitation;
            grad += float2(0.8572, 0.5150) * g1a + float2(-0.4226, 0.9063) * g1b + float2(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.0006 * (1.0 - cap_aa2 * cap_aa2) * min(agitation, 1.45);
        if (cap_aa2 > 0.001) {
            float p2a = (frac(dot(wp0, float2(-0.7547, -0.6561)) * 31.8 * INV_TAU) + frac(sim_time * 6.2 * INV_TAU)) * TAU;
            float p2b = (frac(dot(wp0, float2( 0.2924, -0.9563)) * 41.3 * INV_TAU) + frac(sim_time * 7.3 * INV_TAU)) * TAU;
            float g2a = cos(p2a) * 0.022 * cap_aa2 * agitation;
            float g2b = cos(p2b) * 0.016 * cap_aa2 * agitation;
            grad += float2(-0.7547, -0.6561) * g2a + float2(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) {
            float2 wake_uv = clamp(v_uv, float2(0.5 / 96.0, 0.5 / 72.0), float2(1.0 - 0.5 / 96.0, 1.0 - 0.5 / 72.0));
            float4 wk = WakeTex.SampleLevel(LinearSampler, wake_uv, 0.0);
            float2 edge_d = min(v_uv, float2(1.0, 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 = saturate(wake_active) * edge_win;
            float2 flow = clamp((wk.rg - float2(128.0 / 255.0, 128.0 / 255.0)) * 4.0 * 0.22, float2(-0.20, -0.20), float2(0.20, 0.20)) * w_gain;
            grad += flow;
            wake_foam = wk.b * w_gain;
        }
        float2 raw_bd = float2((v_uv.x - boat_uv.x) * aspect, v_uv.y - boat_uv.y);
        float2 rot_bd = float2(raw_bd.x * roll_cos - raw_bd.y * roll_sin, raw_bd.x * roll_sin + raw_bd.y * roll_cos);
        float2 boat_local = float2(rot_bd.x / 0.145, rot_bd.y / 0.036);
        float boat_ring_d2 = dot(boat_local, boat_local);
        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_nyq = px_span * 16.5 * 1.8;
            float rip_aa = (rip_nyq < 4.5) ? exp(-0.5 * rip_nyq * rip_nyq) : 0.0;
            float rip_ph = (frac(r_hull * 16.5 * INV_TAU) - frac(sim_time * 4.2 * INV_TAU)) * TAU;
            float rip_wave = cos(rip_ph) * 0.34 * rip_env * rip_aa;
            float inv_rh = 1.0 / max(r_hull, 0.1);
            grad += float2(boat_local.x * inv_rh * rip_wave, boat_local.y * inv_rh * rip_wave * 1.38);
        }
        float3 norm = normalize(float3(-grad.x, 1.0, -grad.y));
        float3 dndx = ddx(norm);
        float3 dndy = ddy(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);
        float3 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);
        float3 refl_dir = reflect(ray, norm);
        refl_dir.y = max(refl_dir.y, 0.008);
        float3 sky_refl = euv_sky(normalize(refl_dir), sun_dir, sun_az_dir, sim_time, false);
        float3 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 = saturate(dot(view_dir, half_vec));
        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);
        float3 spec = float3(1.52, 1.38, 1.12) * (core_e * 0.76) + float3(1.28, 0.96, 0.60) * (sheen_e * 0.20);
        float sss_dot = saturate(dot(-view_dir, sun_dir));
        float sss = saturate(h * 2.35 + 0.28) * exp(-dist * 0.045) * (0.48 + 0.52 * (sss_dot * sss_dot * sss_dot));
        float3 deep_body = float3(0.004, 0.145, 0.54) + float3(0.018, 0.52, 0.98) * sss;
        float refl_weight = clamp(fresnel * 0.54 + 0.035, 0.035, 0.72);
        hdr = lerp(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) * smoothstep(0.24, 0.10, rot_bd.y);
            float tri_mask = smoothstep(0.14, 0.02, rx_b + max(rot_bd.y, 0.0) * 0.55) * y_fade;
            hdr = lerp(hdr, float3(0.55, 0.52, 0.48), tri_mask * 0.32);
        }
        float crest_norm = crest_acc / max(agitation, 1.0);
        float steep_foam = smoothstep(0.54, 0.74, crest_norm) * saturate(agitation - 1.08);
        float total_foam = clamp(steep_foam * 0.45 + wake_foam * 0.28, 0.0, 0.54);
        hdr = lerp(hdr, float3(0.78, 0.90, 0.96), total_foam);
        if (boat_ring_d2 < 4.5) {
            if (boat_ring_d2 < 1.12) {
                float contact_ao = (1.0 - smoothstep(0.18, 1.06, boat_ring_d2)) * 0.44;
                hdr *= float3(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.42, 0.82, foam_d2) * (1.0 - smoothstep(0.88, 1.54, foam_d2)) * smoothstep(-0.45, 0.24, boat_local.y);
            float aft_wake = smoothstep(0.22, 0.82, boat_local.x) * (1.0 - smoothstep(0.88, 1.62, boat_local.x)) * (1.0 - smoothstep(0.18, 1.20, abs(boat_local.y - 0.26))) * 0.46;
            float collar_mask = saturate(rim_band + aft_wake);
            if (collar_mask > 0.02) {
                float lacy_a = euv_noise(wp0 * 8.2 + float2(2.3, -1.7));
                float lacy_b = euv_noise(wp0 * 21.0 + float2(-sim_time * 0.85, 4.1));
                float flecks = euv_noise(wp0 * 42.0 + float2(7.1, -sim_time * 1.35));
                float foam_pat = smoothstep(0.46, 0.78, lacy_a * 0.42 + lacy_b * 0.36 + flecks * 0.22);
                float spray_dots = smoothstep(0.74, 0.88, flecks) * rim_band * 0.72 * smoothstep(0.08, 0.015, px_span);
                float hull_foam = clamp(collar_mask * foam_pat * 0.78 + spray_dots, 0.0, 0.86);
                hdr = lerp(hdr, float3(0.92, 0.97, 1.0), hull_foam);
            }
        }
        float horiz_blend = exp(-neg_ry_true * 56.0);
        float dist_haze = 1.0 - exp(-dist * 0.0020);
        float pixel_hor_aa = 1.0 - smoothstep(0.0, d_ry * 1.75, neg_ry_true);
        float atm_mix = saturate(max(max(horiz_blend, dist_haze), pixel_hor_aa));
        float atm_w = saturate((atm_mix - 0.002) * (1.0 / 0.998));
        if (atm_w > 0.0) {
            float3 fog_col = euv_sky(normalize(float3(ray.x, max(0.0005, d_ry * 0.5), ray.z)), sun_dir, sun_az_dir, sim_time, false);
            hdr = lerp(hdr, fog_col, atm_w);
        }
    }
    return float4(euv_aces(hdr), 1.0);
}
"#;

/// Hardware HLSL SM 5.0 origami paper boat overlay shader reading structured `GpuOceanBoatVertex` records.
pub(crate) const HLSL_SCENE_BOAT_SHADER: &str = r#"
struct BoatVert {
    float4 pos_ndc;
    float4 color;
};
StructuredBuffer<BoatVert> BoatVerts : register(t0);
struct BoatOut {
    float4 pos : SV_POSITION;
    float4 color : COLOR0;
};
BoatOut BoatVS(uint vid : SV_VertexID) {
    BoatVert v = BoatVerts[vid];
    BoatOut o;
    o.pos = float4(v.pos_ndc.xy, 0.0, 1.0);
    o.color = v.color;
    return o;
}
float4 BoatPS(BoatOut input) : SV_TARGET {
    return input.color;
}
"#;

/// Hardware HLSL SM 5.0 flagship 3D sky and instanced PBR open-world shader with analytical ray-traced Fresnel reflections, clearcoat, contact AO, multi-bounce GI, and volumetric haze.
pub(crate) const HLSL_SCENE_3D_SHADER: &str = r#"
cbuffer World3dUniforms : register(b0) {
    float4 vp_c0;
    float4 vp_c1;
    float4 vp_c2;
    float4 vp_c3;
    float4 u_camera_pos;
    float4 u_sun_dir_fog;
    float4 u_sun_color_ambient;
    float4 u_sky_horizon_tod;
    float4 u_sky_zenith_aspect;
    uint4 u_draw_offsets;
};
struct World3dVert {
    float4 pos;
    float4 norm;
    float4 submat;
};
struct World3dInst {
    float4 m0;
    float4 m1;
    float4 m2;
    float4 m3;
    float4 albedo_met;
    float4 emissive_rough;
};
StructuredBuffer<World3dVert> Verts : register(t0);
StructuredBuffer<World3dInst> Insts : register(t1);
struct SkyOut {
    float4 pos : SV_POSITION;
    float2 uv : TEXCOORD0;
};
SkyOut SceneFsVS(uint vid : SV_VertexID) {
    float2 p = float2((vid == 1u) ? 3.0 : -1.0, (vid == 2u) ? 3.0 : -1.0);
    SkyOut o;
    o.pos = float4(p, 0.0, 1.0);
    o.uv = float2(p.x * 0.5 + 0.5, 0.5 - p.y * 0.5);
    return o;
}
float3 euv_aces(float3 hdr) {
    float3 x = max(hdr, float3(0.0, 0.0, 0.0));
    float3 num = x * (2.51 * x + 0.03);
    float3 den = x * (2.43 * x + 0.59) + 0.14;
    return pow(clamp(num / max(den, float3(0.0001, 0.0001, 0.0001)), float3(0.0, 0.0, 0.0), float3(1.0, 1.0, 1.0)), float3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2));
}
float euv_hash(float2 cell) {
    float2 p = frac(cell * float2(0.1031, 0.1030));
    p += dot(p, p.yx + 33.33);
    return frac((p.x + p.y) * p.x);
}
float euv_noise(float2 p) {
    float2 i = floor(p);
    float2 f = frac(p);
    float2 u = f * f * (3.0 - 2.0 * f);
    float a = euv_hash(i);
    float b = euv_hash(i + float2(1.0, 0.0));
    float c = euv_hash(i + float2(0.0, 1.0));
    float d = euv_hash(i + float2(1.0, 1.0));
    return lerp(lerp(a, b, u.x), lerp(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(frac(x0) - a, 0.0, w);
    float i1 = floor(x1) * w + clamp(frac(x1) - a, 0.0, w);
    return saturate((i1 - i0) / f);
}
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 saturate((min(x1, hi) - max(x0, lo)) / f);
}
float euv_box_step(float x, float edge, float fw) {
    float f = max(fw, 1.0e-4);
    return saturate((x - edge) / f + 0.5);
}
float4 Sky3dPS(SkyOut input) : SV_TARGET {
    float2 ndc = float2(input.uv.x * 2.0 - 1.0, 1.0 - input.uv.y * 2.0);
    float3 r0 = float3(vp_c0.x, vp_c1.x, vp_c2.x);
    float3 r1 = float3(vp_c0.y, vp_c1.y, vp_c2.y);
    float3 r3 = float3(vp_c0.w, vp_c1.w, vp_c2.w);
    float3 c0 = cross(r1, r3);
    float3 c1 = cross(r3, r0);
    float3 c2 = cross(r0, r1);
    float det = dot(r0, c0);
    float sgn = (det < 0.0) ? -1.0 : 1.0;
    float3 world_ray = normalize((c0 * ndc.x + c1 * ndc.y + c2) * sgn);
    float3 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));
    float3 col = lerp(u_sky_horizon_tod.rgb, u_sky_zenith_aspect.rgb, sky_t);
    if (world_ray.y < 0.0) {
        col = lerp(u_sky_horizon_tod.rgb, u_sky_horizon_tod.rgb * 0.52, smoothstep(0.0, 0.35, -world_ray.y));
    }
    float sdot = saturate(dot(world_ray, sun_dir));
    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.015) {
        float2 dome_uv = world_ray.xz / (ry + 0.16);
        float2 cloud_p = dome_uv * 0.85 + float2(u_sky_horizon_tod.w * 0.04, -u_sky_horizon_tod.w * 0.02);
        float wisp_n = euv_noise(cloud_p * 1.45) * 0.65 + euv_noise(cloud_p * 3.10 + float2(4.2, -2.7)) * 0.35;
        float wisp_mask = smoothstep(0.42, 0.78, wisp_n) * smoothstep(0.015, 0.16, ry) * (1.0 - smoothstep(0.55, 0.92, ry));
        float3 wisp_col = u_sky_horizon_tod.rgb * 1.15 + u_sun_color_ambient.rgb * (0.14 + 0.34 * sp4);
        col = lerp(col, wisp_col, wisp_mask * 0.28);
    }
    return float4(euv_aces(col * u_camera_pos.w), 1.0);
}
struct MeshOut {
    float4 pos : SV_POSITION;
    float3 world_pos : TEXCOORD0;
    float3 world_norm : TEXCOORD1;
    float4 base_met : TEXCOORD2;
    float4 emissive_rough : TEXCOORD3;
    float2 modes : TEXCOORD4;
};
MeshOut Mesh3dVS(uint vid : SV_VertexID, uint iid : SV_InstanceID) {
    World3dVert v = Verts[u_draw_offsets.x + vid];
    World3dInst inst = Insts[u_draw_offsets.y + iid];
    float detail_mode = inst.m3.w;
    float4 wp = inst.m0 * v.pos.x + inst.m1 * v.pos.y + inst.m2 * v.pos.z + float4(inst.m3.xyz, 1.0);
    float4 clip_p = vp_c0 * wp.x + vp_c1 * wp.y + vp_c2 * wp.z + vp_c3 * wp.w;
    clip_p.z = clip_p.z * 0.5 + clip_p.w * 0.5;
    float3 c0 = inst.m0.xyz;
    float3 c1 = inst.m1.xyz;
    float3 c2 = inst.m2.xyz;
    float3 cof0 = cross(c1, c2);
    float3 cof1 = cross(c2, c0);
    float3 cof2 = cross(c0, c1);
    MeshOut o;
    o.pos = clip_p;
    o.world_pos = wp.xyz;
    o.world_norm = normalize(cof0 * v.norm.x + cof1 * v.norm.y + cof2 * v.norm.z);
    o.base_met = (v.submat.w > 0.5)
        ? float4(v.submat.rgb, inst.albedo_met.w)
        : float4(inst.albedo_met.rgb * v.submat.rgb, inst.albedo_met.w);
    o.emissive_rough = inst.emissive_rough;
    o.modes = float2(v.submat.w, detail_mode);
    return o;
}
float4 Mesh3dPS(MeshOut input) : SV_TARGET {
    float3 v_world_pos = input.world_pos;
    float3 norm = normalize(input.world_norm);
    float3 view_dir = normalize(u_camera_pos.xyz - v_world_pos);
    float3 sun_dir = normalize(u_sun_dir_fog.xyz);
    float dist = length(u_camera_pos.xyz - v_world_pos);
    float3 fw_p = fwidth(v_world_pos);
    float fw_xz = length(fw_p.xz);
    float3 base_col = input.base_met.rgb;
    float metallic = saturate(input.base_met.w);
    float roughness = clamp(input.emissive_rough.w, 0.05, 1.0);
    float submat_mode = input.modes.x;
    float detail_mode = input.modes.y;
    float3 emissive = (submat_mode > 1.5) ? base_col * 1.15 : input.emissive_rough.rgb;
    float env_boost = 0.85;
    if (detail_mode > 0.5 && detail_mode < 1.5 && norm.y > 0.82) {
        float wpx = v_world_pos.x;
        float wpz = v_world_pos.z;
        float abs_x = abs(wpx);
        float fw_x = max(fw_p.x, 0.015);
        float fw_z = max(fw_p.z, 0.015);
        float curb_w = euv_box_step(abs_x, 9.9, fw_x);
        float walk_w = euv_box_step(abs_x, 10.6, fw_x);
        float micro_fade = (1.0 - smoothstep(14.0, 48.0, dist)) * (1.0 - smoothstep(0.06, 0.22, max(fw_x, fw_z)));
        float grain_n = (micro_fade > 0.005) ? (euv_hash(floor(float2(wpx * 4.3, wpz * 4.3))) - 0.5) : 0.0;
        float om_curb = 1.0 - curb_w;
        float inner_lin = 1.0 - saturate(abs(abs_x - 3.65) * 0.85);
        float outer_lin = 1.0 - saturate(abs(abs_x - 6.85) * 0.95);
        float tire_wear = saturate(inner_lin * inner_lin + outer_lin * outer_lin) * om_curb;
        float gutter_band = (1.0 - saturate(abs(abs_x - 9.75) * 2.8)) * om_curb;
        float puddle_fade = 1.0 - smoothstep(48.0, 120.0, dist);
        float p_coarse = euv_noise(float2(wpx * 0.14 + 3.1, wpz * 0.08 - 1.7));
        float p_fine = euv_noise(float2(wpx * 0.38 - 5.4, wpz * 0.24 + 2.9));
        float center_crown = (1.0 - saturate(abs_x * 0.65)) * om_curb;
        float puddle_raw = p_coarse * 0.66 + p_fine * 0.34 + tire_wear * 0.14 + gutter_band * 0.16 - center_crown * 0.24;
        float puddle = smoothstep(0.44, 0.90, puddle_raw) * om_curb * puddle_fade;
        float tarmac_scale = (1.0 + grain_n * 0.24 * micro_fade * (1.0 - puddle)) * (1.0 - 0.14 * tire_wear) * (1.0 - 0.38 * puddle) * (1.0 - 0.30 * gutter_band);
        float3 road_col = base_col * tarmac_scale;
        float yellow_w = euv_box_band(abs_x, 0.135, 0.305, fw_x) * om_curb;
        if (yellow_w > 0.005) {
            float pv = (1.0 - 0.14 * puddle) * (0.92 + grain_n * 0.14 * micro_fade);
            road_col = lerp(road_col, float3(0.88, 0.62, 0.10) * pv, yellow_w * 0.94);
        }
        float dash_on = euv_box_pulse(wpz * 0.125, 0.0, 0.42, fw_z * 0.125);
        float lane_w = euv_box_band(abs_x, 4.565, 4.735, fw_x) * dash_on * om_curb;
        float shoulder_w = euv_box_band(abs_x, 8.85, 9.05, fw_x) * om_curb;
        float cross_z = euv_box_pulse((wpz + 18.0) * 0.025, 0.455, 0.545, fw_z * 0.025);
        float cross_x_mask = euv_box_band(abs_x, 0.55, 8.40, fw_x);
        float cross_bars = euv_box_pulse(wpx * 0.85, 0.0, 0.50, fw_x * 0.85);
        float cross_w = cross_z * cross_x_mask * cross_bars * 0.85 * om_curb;
        float white_w = max(max(shoulder_w, lane_w), cross_w);
        if (white_w > 0.005) {
            float pv = (1.0 - 0.16 * puddle) * (0.90 + grain_n * 0.12 * micro_fade);
            road_col = lerp(road_col, float3(0.78, 0.81, 0.86) * pv, white_w * 0.90);
        }
        float3 curb_col = float3(0.36, 0.38, 0.42) * (1.0 + grain_n * 0.10 * micro_fade);
        float seam_u = euv_box_pulse(wpx * 0.45, 0.0, 0.045, fw_x * 0.45);
        float seam_v = euv_box_pulse(wpz * 0.45, 0.0, 0.045, fw_z * 0.45);
        float tile_seam = lerp(1.0, 0.72, saturate(seam_u + seam_v - seam_u * seam_v));
        float3 walk_col = float3(0.24, 0.26, 0.30) * tile_seam * (1.0 + grain_n * 0.10 * micro_fade);
        base_col = lerp(lerp(road_col, curb_col, curb_w), walk_col, walk_w);
        roughness = lerp(lerp(roughness, 0.06, puddle), 0.78, walk_w);
        env_boost = lerp(0.85, 1.85, puddle);
    } else if (detail_mode > 1.5 && detail_mode < 2.5 && abs(norm.y) < 0.35 && submat_mode < 0.5) {
        float u_coord = (abs(norm.x) > abs(norm.z)) ? v_world_pos.z : v_world_pos.x;
        float fw_u_w = (abs(norm.x) > abs(norm.z)) ? fw_p.z : fw_p.x;
        float world_y = v_world_pos.y;
        float fw_y_w = fw_p.y;
        float dist_fade = 1.0 - smoothstep(130.0, 320.0, dist);
        if (world_y < 3.45) {
            float su_s = u_coord * 0.26;
            float fw_su_s = max(fw_u_w * 0.26, 0.005);
            float fy_s = saturate(world_y / 3.45);
            float fw_fy_s = max(fw_y_w / 3.45, 0.005);
            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(float2(floor(su_s), 7.0));
            float3 shop_glow = (sh < 0.40) ? float3(1.18, 0.78, 0.36) : ((sh < 0.62) ? float3(0.28, 0.88, 1.18) : ((sh < 0.80) ? float3(1.12, 0.32, 0.68) : float3(0.0, 0.0, 0.0)));
            float shop_coh = 1.0 - smoothstep(0.28, 0.78, fw_su_s);
            float3 shop_eff = lerp(float3(0.74, 0.56, 0.52), shop_glow, shop_coh);
            base_col = lerp(base_col * 0.64, base_col * 0.40, pane_w);
            emissive += shop_eff * (pane_w * (0.55 + 0.55 * (1.0 - fy_s)) * 0.92);
            roughness = lerp(roughness, 0.12, 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.005);
            float fw_sy = max(fw_y_w * 0.33, 0.005);
            float fy_win = frac(sy);
            float pane_u = euv_box_pulse(su, 0.14, 0.86, fw_su);
            float pane_v = euv_box_pulse(sy, 0.18, 0.84, fw_sy);
            float pane_w = saturate(pane_u * pane_v * dist_fade);
            float spandrel = lerp(0.70, 0.84, euv_box_pulse(sy, 0.14, 0.88, fw_sy));
            float3 wall_base = base_col * spandrel;
            float ch = euv_hash(float2(floor(su), floor(sy)));
            float room_lvl = 0.68 + frac(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.22, 0.72, max(fw_su, fw_sy));
            float3 cell_em = float3(0.0, 0.0, 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 = float3(1.24, 0.86, 0.42) * (room_lvl * blind_grad);
            } else if (ch < 0.33) {
                cell_glass_mul = 0.84;
                cell_em = float3(0.28, 0.86, 1.24) * (room_lvl * blind_grad);
            } else if (ch < 0.42) {
                cell_glass_mul = 0.88;
                cell_em = float3(0.96, 0.94, 0.88) * (room_lvl * blind_grad * 0.82);
            } else if (ch < 0.50) {
                cell_glass_mul = 0.84;
                cell_em = float3(1.06, 0.34, 0.72) * (room_lvl * blind_grad * 0.86);
            }
            float3 mean_em = float3(0.35, 0.28, 0.25);
            base_col = lerp(wall_base, base_col * lerp(0.65, cell_glass_mul, cell_detail), pane_w);
            emissive += lerp(mean_em, cell_em, cell_detail) * pane_w;
            roughness = lerp(roughness, lerp(0.24, 0.10, cell_detail), pane_w);
            env_boost = lerp(0.85, 1.45, pane_w);
        }
    } else if (detail_mode > 2.5 && norm.y > 0.80) {
        float2 wp = v_world_pos.xz;
        float aa_w = 1.0 / (1.0 + fw_xz * 0.85);
        float w_a = cos(wp.x * 0.42 + wp.y * 0.64 + u_sky_horizon_tod.w * 2.4) * 0.08 * aa_w;
        float w_b = cos(-wp.x * 0.78 + wp.y * 0.52 + u_sky_horizon_tod.w * 3.1) * 0.06 * aa_w;
        norm = normalize(float3(w_a - w_b, 1.0, w_a + w_b));
        base_col = float3(0.015, 0.095, 0.145);
        roughness = 0.08;
        metallic = 0.02;
        env_boost = 1.65;
    }
    float3 dndx = ddx(norm);
    float3 dndy = ddy(norm);
    float norm_var = clamp(dot(dndx, dndx) + dot(dndy, dndy), 0.0, 0.50);
    float rough_aa = sqrt(clamp(roughness * roughness + min(norm_var * 0.50, 0.18), 0.0025, 1.0));
    float3 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 r2 = rough_aa * rough_aa;
    float a2 = max(r2 * r2, 0.0004);
    float d_den = ndh * ndh * (a2 - 1.0) + 1.0;
    float ndf = a2 / max(3.14159265 * d_den * d_den, 0.0001);
    float3 f0 = lerp(float3(0.04, 0.04, 0.04), base_col, metallic);
    float3 fresnel = f0 + (float3(1.0, 1.0, 1.0) - f0) * pow(1.0 - hdv, 5.0);
    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);
    float3 spec = (ndf * g_v * g_l * fresnel) / max(4.0 * ndv * ndl, 0.001);
    float clearcoat = (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 * pow(1.0 - hdv, 5.0);
        float cc_val = cc_ndf * cc_f * clearcoat * 0.32;
        spec += float3(cc_val, cc_val, cc_val);
    }
    float canyon_shadow = 1.0 - smoothstep(10.5, 22.0, abs(v_world_pos.x)) * (1.0 - smoothstep(0.0, 14.0, v_world_pos.y)) * 0.38;
    float contact_ao = (detail_mode > 0.5 && detail_mode < 1.5)
        ? (0.76 + 0.24 * (1.0 - smoothstep(9.2, 13.5, abs(v_world_pos.x)) * 0.45))
        : (0.70 + 0.30 * smoothstep(0.0, 2.4, v_world_pos.y));
    float3 kd = (float3(1.0, 1.0, 1.0) - fresnel) * (1.0 - metallic);
    float3 direct = (kd * base_col / 3.14159265 + spec) * u_sun_color_ambient.rgb * (ndl * 1.48 * canyon_shadow);
    float3 refl_dir = reflect(-view_dir, norm);
    float refl_y = saturate(refl_dir.y * 0.5 + 0.5);
    float3 env_refl = lerp(u_sky_horizon_tod.rgb, u_sky_zenith_aspect.rgb, refl_y);
    float sun_refl = pow(max(dot(refl_dir, sun_dir), 0.0), lerp(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(float2(win_u, win_v)));
        float3 win_col = (win_h < 0.35) ? float3(1.15, 0.78, 0.38) : ((win_h < 0.62) ? float3(0.28, 0.86, 1.20) : ((win_h < 0.80) ? float3(1.05, 0.32, 0.70) : float3(0.12, 0.16, 0.24)));
        float win_coh = 1.0 - smoothstep(0.22, 0.80, refl_blur);
        float win_grid = euv_box_pulse(win_u, 0.14, 0.86, refl_blur);
        env_refl += lerp(float3(0.68, 0.58, 0.56), win_col, win_coh) * (facade_refl_band * win_grid * (0.42 + 0.55 * (1.0 - rough_aa)));
    }
    float lamp_z = abs(frac((v_world_pos.z + 10.0) * 0.05) - 0.5) * 20.0;
    float lamp_x = abs(abs(v_world_pos.x) - 10.2);
    float lamp_pool = exp(-(lamp_x * lamp_x * 0.08 + lamp_z * lamp_z * 0.055)) * smoothstep(-0.2, 0.8, norm.y);
    float3 city_gi = (float3(0.26, 0.34, 0.52) * 0.18 + float3(1.05, 0.72, 0.36) * (lamp_pool * 0.52)) * (1.0 - smoothstep(14.0, 42.0, v_world_pos.y));
    float sky_w = norm.y * 0.5 + 0.5;
    float3 hemi = lerp(u_sky_horizon_tod.rgb * 0.44 + city_gi, u_sky_zenith_aspect.rgb * 0.92 + u_sky_horizon_tod.rgb * 0.36, sky_w);
    float one_minus_r = 1.0 - rough_aa;
    float3 f_env = f0 + (max(float3(one_minus_r, one_minus_r, one_minus_r), f0) - f0) * pow(1.0 - ndv, 5.0);
    float3 ambient = (base_col * (1.0 - metallic * 0.65) * hemi + env_refl * f_env * env_boost) * (0.46 + u_sun_color_ambient.w) * contact_ao;
    float3 hdr = direct + ambient + emissive;
    float fog_t = 1.0 - exp(-dist * u_sun_dir_fog.w);
    float sun_scatter = pow(max(dot(-view_dir, sun_dir), 0.0), 6.0);
    float3 fog_color = u_sky_horizon_tod.rgb + u_sun_color_ambient.rgb * (sun_scatter * 0.36);
    hdr = lerp(hdr, fog_color, clamp(fog_t, 0.0, 0.86));
    return float4(euv_aces(hdr * u_camera_pos.w), 1.0);
}
"#;