euv-core 0.52.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
use super::*;

/// Win32 `RECT` ABI struct for `AdjustWindowRectEx` and `GetClientRect`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Win32Rect {
    /// Left X coordinate.
    pub(crate) left: i32,
    /// Top Y coordinate.
    pub(crate) top: i32,
    /// Right X coordinate.
    pub(crate) right: i32,
    /// Bottom Y coordinate.
    pub(crate) bottom: i32,
}

/// Queued Win32 window event tagging its source `HWND`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Win32QueuedEvent {
    /// Originating Win32 `HWND`.
    pub(crate) hwnd: usize,
    /// Translated event payload.
    pub(crate) payload: Win32EventPayload,
}

/// Win32 `WNDCLASSEXW` ABI struct.
#[repr(C)]
pub(crate) struct WndClassExW {
    /// Structure byte size.
    pub(crate) cb_size: u32,
    /// Class style flags.
    pub(crate) style: u32,
    /// Window procedure function pointer.
    pub(crate) lpfn_wnd_proc: unsafe extern "system" fn(isize, u32, usize, isize) -> isize,
    /// Extra class bytes.
    pub(crate) cb_cls_extra: i32,
    /// Extra window bytes.
    pub(crate) cb_wnd_extra: i32,
    /// Module instance handle.
    pub(crate) h_instance: isize,
    /// Class icon handle.
    pub(crate) h_icon: isize,
    /// Class cursor handle.
    pub(crate) h_cursor: isize,
    /// Background brush handle.
    pub(crate) hbr_background: isize,
    /// Optional menu name.
    pub(crate) lpsz_menu_name: *const u16,
    /// Wide class name pointer.
    pub(crate) lpsz_class_name: *const u16,
    /// Small icon handle.
    pub(crate) h_icon_sm: isize,
}

/// Win32 `BITMAPINFOHEADER` ABI struct for `StretchDIBits`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct BitmapInfoHeader {
    /// Header size in bytes (`40`).
    pub(crate) bi_size: u32,
    /// Image width in pixels.
    pub(crate) bi_width: i32,
    /// Negative height indicates top-down scanline order.
    pub(crate) bi_height: i32,
    /// Color planes (`1`).
    pub(crate) bi_planes: u16,
    /// Bits per pixel (`32`).
    pub(crate) bi_bit_count: u16,
    /// Compression format (`BI_RGB`).
    pub(crate) bi_compression: u32,
    /// Image size in bytes.
    pub(crate) bi_size_image: u32,
    /// Horizontal pixels per meter.
    pub(crate) bi_x_pels_per_meter: i32,
    /// Vertical pixels per meter.
    pub(crate) bi_y_pels_per_meter: i32,
    /// Used palette colors.
    pub(crate) bi_clr_used: u32,
    /// Important palette colors.
    pub(crate) bi_clr_important: u32,
}

/// Win32 `BITMAPINFO` wrapper struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct BitmapInfo {
    /// Header specification.
    pub(crate) header: BitmapInfoHeader,
    /// Single color entry placeholder.
    pub(crate) colors: [u32; 1],
}

/// Win32 `SYSTEM_POWER_STATUS` ABI struct for `GetSystemPowerStatus`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Win32SystemPowerStatus {
    /// AC power status (`0` = offline, `1` = online, `255` = unknown).
    pub(crate) ac_line_status: u8,
    /// Battery charge flag bitmask.
    pub(crate) battery_flag: u8,
    /// Percentage of full battery charge remaining (`0..=100`, `255` = unknown).
    pub(crate) battery_life_percent: u8,
    /// Battery saver status (`1` = Battery Saver active on Windows 10+).
    pub(crate) system_status_flag: u8,
    /// Remaining battery lifetime in seconds (`u32::MAX` = unknown).
    pub(crate) battery_life_time: u32,
    /// Full battery lifetime in seconds (`u32::MAX` = unknown).
    pub(crate) battery_full_life_time: u32,
}

/// Win32 `MSG` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Win32Msg {
    /// Target window handle.
    pub(crate) hwnd: isize,
    /// Message identifier.
    pub(crate) message: u32,
    /// Word parameter.
    pub(crate) w_param: usize,
    /// Long parameter.
    pub(crate) l_param: isize,
    /// Timestamp.
    pub(crate) time: u32,
    /// Cursor X.
    pub(crate) pt_x: i32,
    /// Cursor Y.
    pub(crate) pt_y: i32,
}

/// Win32 `MONITORINFOEXW` ABI struct for per-monitor bounds and device name lookup.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct Win32MonitorInfoExW {
    /// Structure size in bytes (`size_of::<Win32MonitorInfoExW>()`).
    pub(crate) cb_size: u32,
    /// Full physical monitor rectangle in virtual-screen coordinates.
    pub(crate) rc_monitor: Win32Rect,
    /// Monitor work-area rectangle excluding taskbar.
    pub(crate) rc_work: Win32Rect,
    /// Monitor flags (`MONITORINFOF_PRIMARY = 1`).
    pub(crate) dw_flags: u32,
    /// Null-terminated UTF-16 GDI display device name (e.g. `\\.\DISPLAY1`).
    pub(crate) sz_device: [u16; 32],
}

/// Win32 `DEVMODEW` ABI struct (`220` bytes) for per-monitor `EnumDisplaySettingsW(ENUM_CURRENT_SETTINGS)`.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct Win32DevModeW {
    /// UTF-16 device name.
    pub(crate) dm_device_name: [u16; 32],
    /// Specification version.
    pub(crate) dm_spec_version: u16,
    /// Driver version.
    pub(crate) dm_driver_version: u16,
    /// Structure byte size (`220`).
    pub(crate) dm_size: u16,
    /// Extra private driver bytes.
    pub(crate) dm_driver_extra: u16,
    /// Active field bitmask.
    pub(crate) dm_fields: u32,
    /// Display position/orientation union words (`16` bytes).
    pub(crate) dm_position_and_orientation: [u32; 4],
    /// Printer color mode.
    pub(crate) dm_color: i16,
    /// Printer duplex mode.
    pub(crate) dm_duplex: i16,
    /// Printer Y resolution.
    pub(crate) dm_y_resolution: i16,
    /// TrueType option.
    pub(crate) dm_tt_option: i16,
    /// Collate setting.
    pub(crate) dm_collate: i16,
    /// UTF-16 form name.
    pub(crate) dm_form_name: [u16; 32],
    /// Logical pixels per inch.
    pub(crate) dm_log_pixels: u16,
    /// Color depth in bits per pixel.
    pub(crate) dm_bits_per_pel: u32,
    /// Active horizontal resolution in pixels.
    pub(crate) dm_pels_width: u32,
    /// Active vertical resolution in pixels.
    pub(crate) dm_pels_height: u32,
    /// Display flags (`DM_GRAYSCALE` / `DM_INTERLACED`).
    pub(crate) dm_display_flags: u32,
    /// Active vertical refresh rate in Hertz (`60`, `144`, `240`, `360`, `480`, ...).
    pub(crate) dm_display_frequency: u32,
    /// ICM method.
    pub(crate) dm_icm_method: u32,
    /// ICM intent.
    pub(crate) dm_icm_intent: u32,
    /// Media type.
    pub(crate) dm_media_type: u32,
    /// Dither type.
    pub(crate) dm_dither_type: u32,
    /// Reserved word 1.
    pub(crate) dm_reserved1: u32,
    /// Reserved word 2.
    pub(crate) dm_reserved2: u32,
    /// Panning width.
    pub(crate) dm_panning_width: u32,
    /// Panning height.
    pub(crate) dm_panning_height: u32,
}

/// Win32 `WINDOWPLACEMENT` ABI struct used to save and restore windowed bounds across borderless fullscreen.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Win32WindowPlacement {
    /// Structure size in bytes (`44`).
    pub(crate) length: u32,
    /// Placement flags.
    pub(crate) flags: u32,
    /// Show state (`SW_SHOWNORMAL`, `SW_SHOWMAXIMIZED`, etc.).
    pub(crate) show_cmd: u32,
    /// Minimized upper-left corner coordinates.
    pub(crate) pt_min_position: [i32; 2],
    /// Maximized upper-left corner coordinates.
    pub(crate) pt_max_position: [i32; 2],
    /// Restored windowed bounding rectangle.
    pub(crate) rc_normal_position: Win32Rect,
}

/// D3D12 / DXGI waitable swapchain configuration descriptor probed for low-latency high-Hz presentation.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct Win32DxgiSwapChainConfig {
    /// Whether `dxgi.dll` DXGI 1.6 waitable swapchain creation is available.
    #[get(type(copy))]
    pub(crate) dxgi_available: bool,
    /// Whether `DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING` / VRR tearing present is enabled.
    #[get(type(copy))]
    pub(crate) allow_tearing: bool,
    /// Swapchain creation flags (`FRAME_LATENCY_WAITABLE_OBJECT | ALLOW_TEARING`).
    #[get(type(copy))]
    pub(crate) swap_chain_flags: u32,
    /// `IDXGISwapChain::Present` flags (`DXGI_PRESENT_ALLOW_TEARING` when tearing unlocked).
    #[get(type(copy))]
    pub(crate) present_flags: u32,
}

/// Explicit per-frame Windows presentation telemetry separating DXGI/D3D direct GPU swapchain present from GDI software fallback.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct WindowsPresentTelemetry {
    /// Active presentation path (`GpuDirect` vs `SoftwareGdi`).
    #[get(type(copy))]
    pub(crate) path: WindowsPresentationPath,
    /// Explicit CPU-mirror telemetry flag (`false` for `GpuDirect`; `true` for `SoftwareGdi`).
    #[get(type(copy))]
    pub(crate) cpu_mirror_active: bool,
    /// Truthful timing source (`DxgiFrameStatistics` vs `SubmissionProxy` vs `Unobservable`).
    #[get(type(copy))]
    pub(crate) timing_source: WindowsPresentTimingSource,
    /// Last `DXGI_FRAME_STATISTICS.SyncQPCTime` tick observed (`0` on GDI fallback or unobservable).
    #[get(type(copy))]
    pub(crate) dxgi_sync_qpc_time: u64,
    /// Last `DXGI_FRAME_STATISTICS.PresentCount` observed (`0` on GDI fallback).
    #[get(type(copy))]
    pub(crate) dxgi_present_count: u32,
    /// Exact BGRA8 bytes uploaded via GDI `StretchDIBits` (`0` for `GpuDirect` or unchanged skip).
    #[get(type(copy))]
    pub(crate) upload_bytes: u64,
    /// Number of dirty rectangles blitted on this frame.
    #[get(type(copy))]
    pub(crate) dirty_rect_count: u32,
    /// Cumulative frames presented directly through native DXGI GPU swapchain.
    #[get(type(copy))]
    pub(crate) direct_gpu_frames: u64,
    /// Cumulative frames presented through GDI software fallback.
    #[get(type(copy))]
    pub(crate) software_fallback_frames: u64,
    /// Cumulative failed presentation attempts.
    #[get(type(copy))]
    pub(crate) failed_frames: u64,
}

/// Dynamically loaded `dxgi.dll`, `d3d11.dll`, and `d3dcompiler_47.dll` procedure addresses resolved once per process.
#[derive(Clone, Copy, Data, Debug, Default)]
pub(crate) struct Win32D3d11Syms {
    /// `CreateDXGIFactory1` procedure address.
    #[get(type(copy))]
    pub(crate) create_dxgi_factory1: usize,
    /// `D3D11CreateDevice` procedure address.
    #[get(type(copy))]
    pub(crate) d3d11_create_device: usize,
    /// `D3DCompile` procedure address.
    #[get(type(copy))]
    pub(crate) d3d_compile: usize,
}

/// DXGI `DXGI_SAMPLE_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct DxgiSampleDesc {
    /// Multisample count (`1`).
    pub(crate) count: u32,
    /// Multisample quality (`0`).
    pub(crate) quality: u32,
}

/// DXGI 1.2+ `DXGI_SWAP_CHAIN_DESC1` ABI struct for flip-discard waitable swapchains.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct DxgiSwapChainDesc1 {
    /// Physical backbuffer width in pixels.
    pub(crate) width: u32,
    /// Physical backbuffer height in pixels.
    pub(crate) height: u32,
    /// Pixel format (`DXGI_FORMAT_R8G8B8A8_UNORM`).
    pub(crate) format: u32,
    /// Stereo presentation flag (`0`).
    pub(crate) stereo: i32,
    /// Multisampling parameters.
    pub(crate) sample_desc: DxgiSampleDesc,
    /// Surface usage mask (`DXGI_USAGE_RENDER_TARGET_OUTPUT`).
    pub(crate) buffer_usage: u32,
    /// Swapchain buffer count (`2` or `3`).
    pub(crate) buffer_count: u32,
    /// Scaling mode (`DXGI_SCALING_NONE`).
    pub(crate) scaling: u32,
    /// Swap effect (`DXGI_SWAP_EFFECT_FLIP_DISCARD`).
    pub(crate) swap_effect: u32,
    /// Alpha compositing mode (`DXGI_ALPHA_MODE_IGNORE`).
    pub(crate) alpha_mode: u32,
    /// Swapchain flags (`FRAME_LATENCY_WAITABLE_OBJECT | ALLOW_TEARING`).
    pub(crate) flags: u32,
}

/// DXGI `DXGI_FRAME_STATISTICS` ABI struct queried after `IDXGISwapChain::Present`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct DxgiFrameStatistics {
    /// Cumulative present count reported by DWM.
    pub(crate) present_count: u32,
    /// Cumulative present refresh count.
    pub(crate) present_refresh_count: u32,
    /// Synchronized display refresh count.
    pub(crate) sync_refresh_count: u32,
    /// High-resolution QPC timestamp synchronized with VBlank.
    pub(crate) sync_qpc_time: i64,
    /// GPU timestamp synchronized with VBlank.
    pub(crate) sync_gpu_time: i64,
}

/// D3D11 `D3D11_BUFFER_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11BufferDesc {
    /// Buffer byte size.
    pub(crate) byte_width: u32,
    /// Memory usage (`D3D11_USAGE_DYNAMIC` or `D3D11_USAGE_DEFAULT`).
    pub(crate) usage: u32,
    /// Pipeline bind flags.
    pub(crate) bind_flags: u32,
    /// CPU access flags (`D3D11_CPU_ACCESS_WRITE`).
    pub(crate) cpu_access_flags: u32,
    /// Miscellaneous flags (`D3D11_RESOURCE_MISC_BUFFER_STRUCTURED`).
    pub(crate) misc_flags: u32,
    /// Element byte stride for structured buffers (`64`).
    pub(crate) structure_byte_stride: u32,
}

/// D3D11 `D3D11_SUBRESOURCE_DATA` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct D3d11SubresourceData {
    /// Pointer to initial texel or vertex payload.
    pub(crate) sys_mem: *const c_void,
    /// Row stride in bytes.
    pub(crate) sys_mem_pitch: u32,
    /// Depth slice stride in bytes.
    pub(crate) sys_mem_slice_pitch: u32,
}

/// D3D11 `D3D11_MAPPED_SUBRESOURCE` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct D3d11MappedSubresource {
    /// Mapped write pointer.
    pub(crate) data: *mut c_void,
    /// Row pitch in bytes.
    pub(crate) row_pitch: u32,
    /// Depth pitch in bytes.
    pub(crate) depth_pitch: u32,
}

/// D3D11 `D3D11_TEXTURE2D_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11Texture2dDesc {
    /// Texture width in texels.
    pub(crate) width: u32,
    /// Texture height in texels.
    pub(crate) height: u32,
    /// Mipmap count (`1`).
    pub(crate) mip_levels: u32,
    /// Array slice count (`1`).
    pub(crate) array_size: u32,
    /// DXGI pixel format.
    pub(crate) format: u32,
    /// Multisample descriptor.
    pub(crate) sample_desc: DxgiSampleDesc,
    /// Resource usage.
    pub(crate) usage: u32,
    /// Bind flags.
    pub(crate) bind_flags: u32,
    /// CPU access flags.
    pub(crate) cpu_access_flags: u32,
    /// Miscellaneous flags.
    pub(crate) misc_flags: u32,
}

/// D3D11 `D3D11_SHADER_RESOURCE_VIEW_DESC` ABI struct supporting both `BUFFER` and `TEXTURE2D` views.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11SrvDesc {
    /// DXGI format (`DXGI_FORMAT_UNKNOWN` for structured buffer).
    pub(crate) format: u32,
    /// View dimension (`D3D11_SRV_DIMENSION_BUFFER` or `D3D11_SRV_DIMENSION_TEXTURE2D`).
    pub(crate) view_dimension: u32,
    /// Union word 0 (`FirstElement` / `MostDetailedMip`).
    pub(crate) union_first: u32,
    /// Union word 1 (`NumElements` / `MipLevels`).
    pub(crate) union_second: u32,
    /// Union padding words.
    pub(crate) union_pad: [u32; 2],
}

/// D3D11 `D3D11_VIEWPORT` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11Viewport {
    /// Top-left X coordinate.
    pub(crate) top_left_x: f32,
    /// Top-left Y coordinate.
    pub(crate) top_left_y: f32,
    /// Viewport width in physical pixels.
    pub(crate) width: f32,
    /// Viewport height in physical pixels.
    pub(crate) height: f32,
    /// Minimum depth (`0.0`).
    pub(crate) min_depth: f32,
    /// Maximum depth (`1.0`).
    pub(crate) max_depth: f32,
}

/// D3D11 `D3D11_RENDER_TARGET_BLEND_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11RenderTargetBlendDesc {
    /// Non-zero to enable alpha blending.
    pub(crate) blend_enable: i32,
    /// Source color blend factor.
    pub(crate) src_blend: u32,
    /// Destination color blend factor.
    pub(crate) dest_blend: u32,
    /// Color blend operator.
    pub(crate) blend_op: u32,
    /// Source alpha blend factor.
    pub(crate) src_blend_alpha: u32,
    /// Destination alpha blend factor.
    pub(crate) dest_blend_alpha: u32,
    /// Alpha blend operator.
    pub(crate) blend_op_alpha: u32,
    /// Output color write mask.
    pub(crate) render_target_write_mask: u8,
}

/// D3D11 `D3D11_BLEND_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11BlendDesc {
    /// Alpha-to-coverage toggle.
    pub(crate) alpha_to_coverage_enable: i32,
    /// Independent blend toggle.
    pub(crate) independent_blend_enable: i32,
    /// Per-render-target blend descriptors.
    pub(crate) render_target: [D3d11RenderTargetBlendDesc; 8],
}

/// D3D11 `D3D11_RASTERIZER_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11RasterizerDesc {
    /// Polygon fill mode (`D3D11_FILL_SOLID`).
    pub(crate) fill_mode: u32,
    /// Face culling mode (`D3D11_CULL_NONE`).
    pub(crate) cull_mode: u32,
    /// Front-face winding order.
    pub(crate) front_counter_clockwise: i32,
    /// Depth bias.
    pub(crate) depth_bias: i32,
    /// Depth bias clamp.
    pub(crate) depth_bias_clamp: f32,
    /// Slope-scaled depth bias.
    pub(crate) slope_scaled_depth_bias: f32,
    /// Depth clip enable flag.
    pub(crate) depth_clip_enable: i32,
    /// Scissor rectangle clipping enable flag (`1`).
    pub(crate) scissor_enable: i32,
    /// Multisample enable flag.
    pub(crate) multisample_enable: i32,
    /// Antialiased line enable flag.
    pub(crate) antialiased_line_enable: i32,
}

/// D3D11 `D3D11_SAMPLER_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11SamplerDesc {
    /// Texture filter mode (`D3D11_FILTER_MIN_MAG_MIP_LINEAR`).
    pub(crate) filter: u32,
    /// U address mode (`D3D11_TEXTURE_ADDRESS_CLAMP`).
    pub(crate) address_u: u32,
    /// V address mode (`D3D11_TEXTURE_ADDRESS_CLAMP`).
    pub(crate) address_v: u32,
    /// W address mode (`D3D11_TEXTURE_ADDRESS_CLAMP`).
    pub(crate) address_w: u32,
    /// Mip LOD bias.
    pub(crate) mip_lod_bias: f32,
    /// Max anisotropy.
    pub(crate) max_anisotropy: u32,
    /// Comparison function (`D3D11_COMPARISON_NEVER`).
    pub(crate) comparison_func: u32,
    /// Border RGBA color.
    pub(crate) border_color: [f32; 4],
    /// Minimum mip LOD.
    pub(crate) min_lod: f32,
    /// Maximum mip LOD.
    pub(crate) max_lod: f32,
}

/// 16-byte `LayerUniforms` constant buffer uploaded to HLSL slot `b0`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11LayerUniforms {
    /// Physical viewport width and height `[w, h]`.
    pub(crate) viewport_size: [f32; 2],
    /// Active scroll container offset `[scroll_x, scroll_y]`.
    pub(crate) scroll_offset: [f32; 2],
}

/// D3D11 `D3D11_BOX` ABI struct specifying a 3D sub-region for incremental `UpdateSubresource`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11Box {
    /// Left inclusive texel coordinate.
    pub(crate) left: u32,
    /// Top inclusive texel coordinate.
    pub(crate) top: u32,
    /// Front slice (`0`).
    pub(crate) front: u32,
    /// Right exclusive texel coordinate.
    pub(crate) right: u32,
    /// Bottom exclusive texel coordinate.
    pub(crate) bottom: u32,
    /// Back slice (`1`).
    pub(crate) back: u32,
}

/// D3D11 `D3D11_DEPTH_STENCILOP_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11DepthStencilOpDesc {
    /// Stencil fail operation.
    pub(crate) stencil_fail_op: u32,
    /// Stencil depth fail operation.
    pub(crate) stencil_depth_fail_op: u32,
    /// Stencil pass operation.
    pub(crate) stencil_pass_op: u32,
    /// Stencil comparison function.
    pub(crate) stencil_func: u32,
}

/// D3D11 `D3D11_DEPTH_STENCIL_DESC` ABI struct.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11DepthStencilDesc {
    /// Non-zero to enable depth testing.
    pub(crate) depth_enable: i32,
    /// Depth write mask (`D3D11_DEPTH_WRITE_MASK_ZERO` or `D3D11_DEPTH_WRITE_MASK_ALL`).
    pub(crate) depth_write_mask: u32,
    /// Depth comparison function (`D3D11_COMPARISON_LESS`).
    pub(crate) depth_func: u32,
    /// Non-zero to enable stencil testing.
    pub(crate) stencil_enable: i32,
    /// Stencil read mask.
    pub(crate) stencil_read_mask: u8,
    /// Stencil write mask.
    pub(crate) stencil_write_mask: u8,
    /// Front-facing stencil operation descriptor.
    pub(crate) front_face: D3d11DepthStencilOpDesc,
    /// Back-facing stencil operation descriptor.
    pub(crate) back_face: D3d11DepthStencilOpDesc,
}

/// 64-byte `OceanUniforms` constant buffer uploaded to HLSL slot `b0` for `OceanPS`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11OceanUniforms {
    /// `[sim_time, agitation, horizon_v, aspect]`.
    pub(crate) time_agit_hor_aspect: [f32; 4],
    /// `[roll_cos, roll_sin, boat_u, boat_v]`.
    pub(crate) roll_boat_uv: [f32; 4],
    /// `[sun_x, sun_y, sun_z, wake_active]`.
    pub(crate) sun_dir_wake: [f32; 4],
    /// Explicit 16-byte constant buffer padding.
    pub(crate) pad0: [f32; 4],
}

/// 160-byte `World3dUniforms` constant buffer uploaded to HLSL slot `b0` for `Sky3dPS` and `Mesh3dVS`/`Mesh3dPS`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct D3d11World3dUniforms {
    /// Column 0 of column-major view-projection matrix.
    pub(crate) vp_c0: [f32; 4],
    /// Column 1 of column-major view-projection matrix.
    pub(crate) vp_c1: [f32; 4],
    /// Column 2 of column-major view-projection matrix.
    pub(crate) vp_c2: [f32; 4],
    /// Column 3 of column-major view-projection matrix.
    pub(crate) vp_c3: [f32; 4],
    /// `[cam_x, cam_y, cam_z, exposure]`.
    pub(crate) camera_pos: [f32; 4],
    /// `[sun_x, sun_y, sun_z, fog_density]`.
    pub(crate) sun_dir_fog: [f32; 4],
    /// `[sun_r, sun_g, sun_b, ambient_strength]`.
    pub(crate) sun_color_ambient: [f32; 4],
    /// `[horizon_r, horizon_g, horizon_b, time_of_day]`.
    pub(crate) sky_horizon_tod: [f32; 4],
    /// `[zenith_r, zenith_g, zenith_b, aspect]`.
    pub(crate) sky_zenith_aspect: [f32; 4],
    /// `[vertex_start, instance_start, 0, 0]` offsetting `SV_VertexID`/`SV_InstanceID`.
    pub(crate) draw_offsets: [u32; 4],
}

/// Lazily initialized Windows D3D11 offscreen scene pipeline executing `DirectGpuScenePass` (`OceanFluid` + `World3d`).
#[derive(Data, Debug, Default)]
pub(crate) struct Win32D3d11ScenePipeline {
    /// Full-screen triangle `ID3D11VertexShader*` (`SceneFsVS`).
    #[get(type(copy))]
    pub(crate) fs_vs: usize,
    /// Ocean optical `ID3D11PixelShader*` (`OceanPS`).
    #[get(type(copy))]
    pub(crate) ocean_ps: usize,
    /// Origami boat `ID3D11VertexShader*` (`BoatVS`).
    #[get(type(copy))]
    pub(crate) boat_vs: usize,
    /// Origami boat `ID3D11PixelShader*` (`BoatPS`).
    #[get(type(copy))]
    pub(crate) boat_ps: usize,
    /// 3D atmospheric sky `ID3D11PixelShader*` (`Sky3dPS`).
    #[get(type(copy))]
    pub(crate) sky3d_ps: usize,
    /// 3D PBR instanced `ID3D11VertexShader*` (`Mesh3dVS`).
    #[get(type(copy))]
    pub(crate) mesh3d_vs: usize,
    /// 3D PBR instanced `ID3D11PixelShader*` (`Mesh3dPS`).
    #[get(type(copy))]
    pub(crate) mesh3d_ps: usize,
    /// `ID3D11Buffer*` constant buffer for `D3d11OceanUniforms`.
    #[get(type(copy))]
    pub(crate) ocean_cb: usize,
    /// `ID3D11Buffer*` constant buffer for `D3d11World3dUniforms`.
    #[get(type(copy))]
    pub(crate) world3d_cb: usize,
    /// `ID3D11DepthStencilState*` with depth test/write enabled (`LESS`).
    #[get(type(copy))]
    pub(crate) depth_on_state: usize,
    /// `ID3D11DepthStencilState*` with depth test/write disabled.
    #[get(type(copy))]
    pub(crate) depth_off_state: usize,
    /// Offscreen RGBA8 scene color `ID3D11Texture2D*`.
    #[get(type(copy))]
    pub(crate) scene_color_tex: usize,
    /// Offscreen scene color `ID3D11RenderTargetView*`.
    #[get(type(copy))]
    pub(crate) scene_color_rtv: usize,
    /// Offscreen scene color `ID3D11ShaderResourceView*` bound to `t4` (`ExternalTex`).
    #[get(type(copy))]
    pub(crate) scene_color_srv: usize,
    /// Offscreen `D24_UNORM_S8_UINT` depth `ID3D11Texture2D*`.
    #[get(type(copy))]
    pub(crate) scene_depth_tex: usize,
    /// Offscreen depth `ID3D11DepthStencilView*`.
    #[get(type(copy))]
    pub(crate) scene_depth_dsv: usize,
    /// Active offscreen scene width in pixels.
    #[get(type(copy))]
    pub(crate) scene_w: u32,
    /// Active offscreen scene height in pixels.
    #[get(type(copy))]
    pub(crate) scene_h: u32,
    /// Interactive PBF wake RGBA8 `ID3D11Texture2D*`.
    #[get(type(copy))]
    pub(crate) wake_tex: usize,
    /// Wake `ID3D11ShaderResourceView*`.
    #[get(type(copy))]
    pub(crate) wake_srv: usize,
    /// Active wake texture width.
    #[get(type(copy))]
    pub(crate) wake_w: u32,
    /// Active wake texture height.
    #[get(type(copy))]
    pub(crate) wake_h: u32,
    /// Dynamic structured `ID3D11Buffer*` for `GpuOceanBoatVertex`.
    #[get(type(copy))]
    pub(crate) boat_buf: usize,
    /// SRV for `boat_buf`.
    #[get(type(copy))]
    pub(crate) boat_srv: usize,
    /// Element capacity of `boat_buf`.
    #[get(type(copy))]
    pub(crate) boat_cap: usize,
    /// Dynamic structured `ID3D11Buffer*` for `GpuWorld3dVertex`.
    #[get(type(copy))]
    pub(crate) mesh3d_vert_buf: usize,
    /// SRV for `mesh3d_vert_buf`.
    #[get(type(copy))]
    pub(crate) mesh3d_vert_srv: usize,
    /// Element capacity of `mesh3d_vert_buf`.
    #[get(type(copy))]
    pub(crate) mesh3d_vert_cap: usize,
    /// Uploaded mesh vertex buffer revision.
    #[get(type(copy))]
    pub(crate) mesh3d_vert_rev: u64,
    /// Dynamic structured `ID3D11Buffer*` for `GpuWorld3dInstance`.
    #[get(type(copy))]
    pub(crate) mesh3d_inst_buf: usize,
    /// SRV for `mesh3d_inst_buf`.
    #[get(type(copy))]
    pub(crate) mesh3d_inst_srv: usize,
    /// Element capacity of `mesh3d_inst_buf`.
    #[get(type(copy))]
    pub(crate) mesh3d_inst_cap: usize,
}

/// Live dynamic-loaded D3D11 + DXGI 1.6 waitable swapchain and instanced uber-shader presenter.
#[derive(Data, Debug, Default)]
pub(crate) struct Win32D3d11SwapChain {
    /// `ID3D11Device*` handle stored as `usize`.
    #[get(type(copy))]
    pub(crate) device: usize,
    /// `ID3D11DeviceContext*` handle stored as `usize`.
    #[get(type(copy))]
    pub(crate) context: usize,
    /// `IDXGISwapChain1*` handle stored as `usize`.
    #[get(type(copy))]
    pub(crate) swap_chain: usize,
    /// `IDXGISwapChain2*` handle stored as `usize` (`0` if unavailable).
    #[get(type(copy))]
    pub(crate) swap_chain2: usize,
    /// `ID3D11RenderTargetView*` handle stored as `usize`.
    #[get(type(copy))]
    pub(crate) rtv: usize,
    /// DXGI waitable frame-latency `HANDLE` passed into `MsgWaitForMultipleObjects`.
    #[get(type(copy))]
    pub(crate) waitable_handle: isize,
    /// Compiled `ID3D11VertexShader*` for `HLSL_UI_SHADER::VSMain`.
    #[get(type(copy))]
    pub(crate) vertex_shader: usize,
    /// Compiled `ID3D11PixelShader*` for `HLSL_UI_SHADER::PSMain`.
    #[get(type(copy))]
    pub(crate) pixel_shader: usize,
    /// `ID3D11Buffer*` constant buffer bound at `b0`.
    #[get(type(copy))]
    pub(crate) constant_buffer: usize,
    /// Dynamic `ID3D11Buffer*` structured instance buffer bound at `t0`.
    #[get(type(copy))]
    pub(crate) instance_buffer: usize,
    /// `ID3D11ShaderResourceView*` over `instance_buffer`.
    #[get(type(copy))]
    pub(crate) instance_srv: usize,
    /// Allocated capacity of `instance_buffer` in `GpuPrimitiveInstance` elements.
    #[get(type(copy))]
    pub(crate) instance_capacity: usize,
    /// `ID3D11BlendState*` configured for straight/premultiplied UI alpha blending.
    #[get(type(copy))]
    pub(crate) blend_state: usize,
    /// `ID3D11BlendState*` with blending disabled for opaque offscreen scene passes.
    #[get(type(copy))]
    pub(crate) opaque_blend_state: usize,
    /// `ID3D11RasterizerState*` with scissor clipping enabled.
    #[get(type(copy))]
    pub(crate) rasterizer_state: usize,
    /// `ID3D11SamplerState*` bound at `s0`.
    #[get(type(copy))]
    pub(crate) sampler_state: usize,
    /// Persistent `R8_UNORM` glyph atlas texture (`t1`).
    #[get(type(copy))]
    pub(crate) glyph_r8_tex: usize,
    /// SRV for `glyph_r8_tex`.
    #[get(type(copy))]
    pub(crate) glyph_r8_srv: usize,
    /// Width of `glyph_r8_tex` in texels.
    #[get(type(copy))]
    pub(crate) glyph_r8_w: u32,
    /// Height of `glyph_r8_tex` in texels.
    #[get(type(copy))]
    pub(crate) glyph_r8_h: u32,
    /// Uploaded revision of `glyph_r8_tex`.
    #[get(type(copy))]
    pub(crate) glyph_r8_rev: u64,
    /// Placeholder/color `RGBA8_UNORM` glyph texture (`t2`).
    #[get(type(copy))]
    pub(crate) glyph_rgba8_tex: usize,
    /// SRV for `glyph_rgba8_tex`.
    #[get(type(copy))]
    pub(crate) glyph_rgba8_srv: usize,
    /// Persistent `RGBA8_UNORM` image atlas texture (`t3`).
    #[get(type(copy))]
    pub(crate) image_tex: usize,
    /// SRV for `image_tex`.
    #[get(type(copy))]
    pub(crate) image_srv: usize,
    /// Width of `image_tex` in texels.
    #[get(type(copy))]
    pub(crate) image_w: u32,
    /// Height of `image_tex` in texels.
    #[get(type(copy))]
    pub(crate) image_h: u32,
    /// Uploaded revision of `image_tex`.
    #[get(type(copy))]
    pub(crate) image_rev: u64,
    /// External/video `RGBA8_UNORM` texture (`t4`).
    #[get(type(copy))]
    pub(crate) external_tex: usize,
    /// SRV for `external_tex`.
    #[get(type(copy))]
    pub(crate) external_srv: usize,
    /// Current physical backbuffer width in pixels.
    #[get(type(copy))]
    pub(crate) width: u32,
    /// Current physical backbuffer height in pixels.
    #[get(type(copy))]
    pub(crate) height: u32,
    /// Active DXGI swapchain flags.
    #[get(type(copy))]
    pub(crate) swap_chain_flags: u32,
    /// Active `IDXGISwapChain::Present` flags.
    #[get(type(copy))]
    pub(crate) present_flags: u32,
    /// Lazily initialized offscreen D3D11 scene pipeline (`OceanFluid` + `World3d`).
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) scene: Option<Win32D3d11ScenePipeline>,
    /// Reusable flat primitive staging buffer avoiding per-frame allocations on direct submission.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) scratch_prims: Vec<GpuPrimitiveInstance>,
    /// Reusable draw-range metadata buffer paired with `scratch_prims`.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) scratch_ranges: Vec<(u32, u32, Rect, [f32; 2])>,
}

/// Native Win32 window record storing `HWND`, `HINSTANCE`, reveal state, fullscreen geometry, and GPU/GDI telemetry.
#[derive(Clone, Data, Debug)]
pub struct WindowsWindowRecord {
    /// Window configuration.
    pub(crate) config: WindowConfig,
    /// Live window state and DPI metrics.
    #[get(type(copy))]
    pub(crate) state: WindowState,
    /// Win32 `HWND`.
    #[get(type(copy))]
    pub(crate) hwnd: usize,
    /// Win32 `HINSTANCE`.
    #[get(type(copy))]
    pub(crate) hinstance: usize,
    /// Whether `ShowWindow(hwnd, SW_SHOW)` has been called after Frame 1 presentation.
    #[get(type(copy))]
    pub(crate) revealed: bool,
    /// Saved pre-fullscreen Win32 style bitmask (`GWL_STYLE`).
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) saved_window_style: i32,
    /// Saved pre-fullscreen `WINDOWPLACEMENT` for exact windowed restoration.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) saved_placement: Win32WindowPlacement,
    /// Probed D3D12/DXGI waitable swapchain configuration for this window.
    #[get(type(copy))]
    pub(crate) dxgi_config: Win32DxgiSwapChainConfig,
    /// Active presentation path (`GpuDirect` on DXGI swapchain or `SoftwareGdi` fallback).
    #[get(type(copy))]
    pub(crate) presentation_path: WindowsPresentationPath,
    /// Explicit CPU-mirror telemetry flag (`false` on `GpuDirect`; `true` on `SoftwareGdi`).
    #[get(type(copy))]
    pub(crate) cpu_mirror_active: bool,
    /// Most recent Windows presentation telemetry snapshot.
    #[get(type(copy))]
    pub(crate) last_telemetry: WindowsPresentTelemetry,
    /// Optional dynamic-loaded D3D11 + DXGI 1.6 waitable swapchain presenter.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) d3d11: Option<Arc<Mutex<Win32D3d11SwapChain>>>,
}

/// Direct Windows Win32 / GDI32 / Direct3D platform backend.
#[derive(Clone, Data, Debug)]
pub struct WindowsPlatform {
    /// Monotonic window handle generator.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) next_id: u64,
    /// Whether the Win32 window class has been registered.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) class_registered: bool,
    /// Active Win32 windows.
    pub(crate) windows: HashMap<WindowId, WindowsWindowRecord>,
    /// Queued window and input events.
    pub(crate) pending_events: Vec<WindowEvent>,
    /// Clipboard cache.
    #[get(skip)]
    pub(crate) clipboard_cache: Option<String>,
    /// Whether the Win32 frame pump is paused for background freeze.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) pump_paused: bool,
    /// Kernel auto-reset `HANDLE` created via `CreateEventW` to wake `MsgWaitForMultipleObjects`.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) wake_event: isize,
}