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

/// `GpuDriver` implementation for `RecordingGpuDriver`.
impl GpuDriver for RecordingGpuDriver {
    /// Returns the configured backend tag.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Driver reference.
    ///
    /// # Returns
    ///
    /// - `GraphicsBackend` - Active backend tag.
    fn backend(&self) -> GraphicsBackend {
        self.get_backend_kind()
    }

    /// Selects the canonical native shader source for this backend.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Driver reference.
    ///
    /// # Returns
    ///
    /// - `&'static str` - Native shader source string.
    fn shader_source(&self) -> &'static str {
        match self.get_backend_kind() {
            GraphicsBackend::Metal => MSL_UI_SHADER,
            GraphicsBackend::Direct3D12 | GraphicsBackend::Direct3D11 => HLSL_UI_SHADER,
            GraphicsBackend::WebGl2 | GraphicsBackend::OpenGlEs => GLSL_ES3_UI_FRAGMENT_SHADER,
            GraphicsBackend::WebGpu
            | GraphicsBackend::Vulkan
            | GraphicsBackend::SoftwareCpu
            | GraphicsBackend::Canvas2d
            | GraphicsBackend::Headless => WGSL_UI_SHADER,
        }
    }

    /// Records submitted GPU batches and returns draw-call count.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable driver.
    /// - `u32` - Surface width.
    /// - `u32` - Surface height.
    /// - `Color` - Clear color.
    /// - `&[GpuBatch]` - Ordered batches.
    ///
    /// # Returns
    ///
    /// - `Result<usize, PlatformError>` - Submitted batch count.
    fn submit_batches(
        &mut self,
        _width: u32,
        _height: u32,
        clear_color: Color,
        batches: &[GpuBatch],
    ) -> Result<usize, PlatformError> {
        let count: usize = batches.len();
        let instances: usize = batches.iter().fold(0_usize, |acc: usize, b: &GpuBatch| {
            acc + b.get_instances().len()
        });
        self.set_last_clear(clear_color);
        self.set_last_instance_count(instances);
        self.set_total_draw_calls(self.get_total_draw_calls().saturating_add(count));
        Ok(count)
    }
}

/// Instanced batch builder, order-preserving coalescer, and telemetry methods for `HardwareRenderer`.
impl HardwareRenderer {
    /// Creates a hardware renderer bound to `backend` with optional deterministic pixel readback.
    ///
    /// # Arguments
    ///
    /// - `GraphicsBackend` - Selected GPU backend.
    /// - `u32` - Initial physical width.
    /// - `u32` - Initial physical height.
    /// - `RenderConfig` - Rendering configuration.
    /// - `bool` - Whether software readback mirror is enabled.
    ///
    /// # Returns
    ///
    /// - `Self` - Configured hardware renderer.
    #[must_use]
    pub fn with_backend(
        backend: GraphicsBackend,
        width: u32,
        height: u32,
        config: RenderConfig,
        enable_readback: bool,
    ) -> Self {
        Self::with_backend_and_font(
            backend,
            width,
            height,
            config,
            enable_readback,
            NativeSystemFont::deterministic(),
        )
    }

    /// Creates a hardware renderer bound to `backend` with an explicit font provider for readback.
    ///
    /// # Arguments
    ///
    /// - `GraphicsBackend` - Selected GPU backend.
    /// - `u32` - Initial physical width.
    /// - `u32` - Initial physical height.
    /// - `RenderConfig` - Rendering configuration.
    /// - `bool` - Whether software readback mirror is enabled.
    /// - `NativeSystemFont` - Font provider used by the software readback mirror.
    ///
    /// # Returns
    ///
    /// - `Self` - Configured hardware renderer.
    #[must_use]
    pub fn with_backend_and_font(
        backend: GraphicsBackend,
        width: u32,
        height: u32,
        config: RenderConfig,
        enable_readback: bool,
        font_provider: NativeSystemFont,
    ) -> Self {
        let driver: NativeGpuDriver = NativeGpuDriver::probe(backend);
        Self {
            driver,
            batches: Vec::with_capacity(16),
            active_batches: 0,
            clip_stack: Vec::with_capacity(16),
            last_submitted_batches: 0,
            last_coalesced_merges: 0,
            last_culled_instances: 0,
            last_uploaded_instances: 0,
            last_uploaded_bytes: 0,
            readback: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
            enable_readback,
            resource_ring: GpuResourceRing::default(),
            direct_gpu_frames: 0,
            cpu_readback_frames: 0,
            last_cpu_rgba_upload_bytes: 0,
            has_unsupported_effect: false,
            last_fence_id: 0,
        }
    }

    /// Captures an on-demand CPU reference framebuffer readback without enabling steady-state CPU mirroring.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable hardware renderer.
    /// - `&DisplayList` - Display list to rasterize through the CPU reference backend.
    /// - `DamageRegion` - Damage region to rasterize.
    ///
    /// # Returns
    ///
    /// - `&Framebuffer` - Rendered reference RGBA8 framebuffer.
    pub fn capture_reference_readback(
        &mut self,
        display_list: &DisplayList,
        damage: DamageRegion,
    ) -> &Framebuffer {
        let (_, _): (usize, usize) = self.get_mut_readback().render(display_list, damage);
        self.get_readback().get_framebuffer()
    }

    /// Signals completion of GPU submission `fence_id` to release staging ring slots and reclaim deferred resources.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable hardware renderer.
    /// - `u64` - Completed monotonic GPU fence identifier.
    ///
    /// # Returns
    ///
    /// - `usize` - Number of deferred GPU resources reclaimed.
    pub fn complete_gpu_fence(&mut self, fence_id: u64) -> usize {
        self.get_mut_resource_ring().complete_fence(fence_id)
    }

    /// Trims invisible persistent GPU scroll tile caches under memory pressure according to `retain_ratio`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable hardware renderer.
    /// - `f32` - Retained budget fraction in `(0.0..=1.0]`.
    ///
    /// # Returns
    ///
    /// - `usize` - Number of cold scroll tiles evicted to fence-deferred reclamation.
    pub fn trim_gpu_caches(&mut self, retain_ratio: f32) -> usize {
        self.get_mut_resource_ring()
            .apply_retention_ratio(retain_ratio)
    }

    /// Returns latest-frame GPU batching and upload telemetry `(batches, coalesced_merges, culled_instances, uploaded_instances, uploaded_bytes)`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hardware renderer reference.
    ///
    /// # Returns
    ///
    /// - `(usize, usize, usize, usize, usize)` - Latest frame batching and upload telemetry tuple.
    #[must_use]
    pub fn last_frame_telemetry(&self) -> (usize, usize, usize, usize, usize) {
        (
            self.get_last_submitted_batches(),
            self.get_last_coalesced_merges(),
            self.get_last_culled_instances(),
            self.get_last_uploaded_instances(),
            self.get_last_uploaded_bytes(),
        )
    }

    /// Computes a deterministic 64-bit FNV-1a hash over all compiled GPU batch instances.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hardware renderer reference.
    ///
    /// # Returns
    ///
    /// - `u64` - Deterministic batch geometry hash.
    #[must_use]
    pub fn batch_hash(&self) -> u64 {
        let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
        let active: usize = self.get_active_batches().min(self.get_batches().len());
        for batch in self.get_batches()[..active].iter() {
            for inst in batch.get_instances().iter() {
                for v in inst
                    .get_rect()
                    .iter()
                    .chain(inst.get_color().iter())
                    .chain(inst.get_radii().iter())
                {
                    hash ^= v.to_bits() as u64;
                    hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
                }
            }
        }
        hash
    }

    /// Returns `true` when physical quad `quad = [x, y, w, h]` lies completely inside `container`.
    ///
    /// # Arguments
    ///
    /// - `[f32; 4]` - Physical quad bounds `[x, y, width, height]`.
    /// - `Rect` - Enclosing physical clip rectangle.
    ///
    /// # Returns
    ///
    /// - `bool` - True when no fragment of `quad` extends outside `container`.
    #[inline(always)]
    #[must_use]
    fn quad_inside_rect(quad: [f32; 4], container: Rect) -> bool {
        let qx0: f32 = quad[0];
        let qy0: f32 = quad[1];
        let qx1: f32 = qx0 + quad[2];
        let qy1: f32 = qy0 + quad[3];
        let x_in: bool = qx0 >= container.get_x() && qx1 <= container.right();
        let y_in: bool = qy0 >= container.get_y() && qy1 <= container.bottom();
        container.get_width() > 0.0 && container.get_height() > 0.0 && x_in && y_in
    }

    /// Returns `true` when `quad` intersects `clip` with strictly positive area.
    ///
    /// # Arguments
    ///
    /// - `[f32; 4]` - Physical quad bounds `[x, y, width, height]`.
    /// - `Rect` - Active physical scissor clip.
    ///
    /// # Returns
    ///
    /// - `bool` - True when at least part of `quad` falls inside `clip`.
    #[inline(always)]
    #[must_use]
    fn quad_intersects_clip(quad: [f32; 4], clip: Rect) -> bool {
        let qw: f32 = quad[2];
        let qh: f32 = quad[3];
        let quad_empty: bool = qw <= 0.0 || qh <= 0.0;
        let clip_empty: bool = clip.get_width() <= 0.0 || clip.get_height() <= 0.0;
        if quad_empty || clip_empty {
            return false;
        }
        let qx0: f32 = quad[0];
        let qy0: f32 = quad[1];
        let qx1: f32 = qx0 + qw;
        let qy1: f32 = qy0 + qh;
        let x_hit: bool = qx1 > clip.get_x() && qx0 < clip.right();
        let y_hit: bool = qy1 > clip.get_y() && qy0 < clip.bottom();
        x_hit && y_hit
    }

    /// Returns `true` when `inner` lies completely inside `outer`.
    ///
    /// # Arguments
    ///
    /// - `Rect` - Candidate inner rectangle.
    /// - `Rect` - Candidate outer enclosing rectangle.
    ///
    /// # Returns
    ///
    /// - `bool` - True when `inner` is contained inside `outer`.
    #[inline(always)]
    #[must_use]
    fn rect_inside_rect(inner: Rect, outer: Rect) -> bool {
        let x_in: bool = inner.get_x() >= outer.get_x() && inner.right() <= outer.right();
        let y_in: bool = inner.get_y() >= outer.get_y() && inner.bottom() <= outer.bottom();
        x_in && y_in
    }

    /// Returns the union bounding box of `existing` and physical quad `quad = [x, y, w, h]`.
    ///
    /// # Arguments
    ///
    /// - `Rect` - Existing bounding box.
    /// - `[f32; 4]` - Newly appended quad bounds `[x, y, width, height]`.
    ///
    /// # Returns
    ///
    /// - `Rect` - Enclosing union rectangle.
    #[inline(always)]
    #[must_use]
    fn union_rect_quad(existing: Rect, quad: [f32; 4]) -> Rect {
        let x0: f32 = existing.get_x().min(quad[0]);
        let y0: f32 = existing.get_y().min(quad[1]);
        let x1: f32 = existing.right().max(quad[0] + quad[2]);
        let y1: f32 = existing.bottom().max(quad[1] + quad[3]);
        Rect::new(x0, y0, (x1 - x0).max(0.0), (y1 - y0).max(0.0))
    }

    /// Compiles `display_list` into order-preserving coalesced GPU instanced batches and submits them to the GPU driver.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable hardware renderer.
    /// - `&DisplayList` - Backend-agnostic draw command stream.
    /// - `DamageRegion` - Active damage region for readback mirroring.
    ///
    /// # Returns
    ///
    /// - `Result<(usize, usize), PlatformError>` - Tuple of `(draw_batches, pixels_shaded)`.
    pub fn render(
        &mut self,
        display_list: &DisplayList,
        damage: DamageRegion,
    ) -> Result<(usize, usize), PlatformError> {
        let scale: f32 = display_list.get_scale_factor().max(0.5);
        let phys_w: u32 = ((display_list.get_viewport().get_width() * scale).round() as u32).max(1);
        let phys_h: u32 =
            ((display_list.get_viewport().get_height() * scale).round() as u32).max(1);
        let full_clip: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
        let full_entry: GpuClipEntry = GpuClipEntry {
            scissor: full_clip,
            safe_inner: full_clip,
        };
        let Self {
            driver,
            batches,
            active_batches,
            clip_stack,
            last_submitted_batches,
            last_coalesced_merges,
            last_culled_instances,
            last_uploaded_instances,
            last_uploaded_bytes,
            readback,
            enable_readback,
            resource_ring,
            direct_gpu_frames,
            cpu_readback_frames,
            last_cpu_rgba_upload_bytes,
            has_unsupported_effect,
            last_fence_id,
        } = self;
        clip_stack.clear();
        clip_stack.push(full_entry);
        *active_batches = 0;
        *has_unsupported_effect = false;
        let mut tail_state: GpuBatchCoalesceState = GpuBatchCoalesceState::default();
        let mut coalesced_merges: usize = 0;
        let mut culled_instances: usize = 0;
        let mut uploaded_instances: usize = 0;
        let mut clear_color: Color = COLOR_WHITE;
        for cmd in display_list.get_commands().iter() {
            let clip_entry: GpuClipEntry = clip_stack.last().copied().unwrap_or(full_entry);
            let active_clip: Rect = clip_entry.scissor;
            match cmd {
                DrawCommand::Clear(c) => {
                    clear_color = *c;
                }
                DrawCommand::PushClip(rect) => {
                    let phys: Rect = snap_physical_rect(*rect, scale);
                    let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
                    let next_safe: Rect = clip_entry.safe_inner.intersect(phys).unwrap_or_default();
                    clip_stack.push(GpuClipEntry {
                        scissor: next_scissor,
                        safe_inner: next_safe,
                    });
                }
                DrawCommand::PushRoundedClip { rect, radius } => {
                    let phys: Rect = snap_physical_rect(*rect, scale);
                    let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
                    let max_r: f32 = (radius.max_radius() * scale).ceil();
                    let inset_w: f32 = (phys.get_width() - max_r * 2.0).max(0.0);
                    let inset_h: f32 = (phys.get_height() - max_r * 2.0).max(0.0);
                    let rounded_safe: Rect =
                        Rect::new(phys.get_x() + max_r, phys.get_y() + max_r, inset_w, inset_h);
                    let next_safe: Rect = clip_entry
                        .safe_inner
                        .intersect(rounded_safe)
                        .unwrap_or_default();
                    clip_stack.push(GpuClipEntry {
                        scissor: next_scissor,
                        safe_inner: next_safe,
                    });
                }
                DrawCommand::PopClip => {
                    if clip_stack.len() > 1 {
                        let _: Option<GpuClipEntry> = clip_stack.pop();
                    }
                }
                DrawCommand::FillRoundedRect {
                    rect,
                    color,
                    radius,
                    border,
                } => {
                    let snapped: Rect = snap_physical_rect(*rect, scale);
                    let quad: [f32; 4] = [
                        snapped.get_x(),
                        snapped.get_y(),
                        snapped.get_width(),
                        snapped.get_height(),
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let bw: f32 = if border.get_width() > 0.0 {
                        (border.get_width() * scale).max(1.0)
                    } else {
                        0.0
                    };
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: color.to_f32_array(),
                        secondary_color: border.get_color().to_f32_array(),
                        radii: [
                            radius.get_top_left() * scale,
                            radius.get_top_right() * scale,
                            radius.get_bottom_right() * scale,
                            radius.get_bottom_left() * scale,
                        ],
                        stroke_or_blur: bw,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::RoundedRect as u32,
                        reserved: 0,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::LinearGradientRect {
                    rect,
                    gradient,
                    radius,
                } => {
                    let snapped: Rect = snap_physical_rect(*rect, scale);
                    let quad: [f32; 4] = [
                        snapped.get_x(),
                        snapped.get_y(),
                        snapped.get_width(),
                        snapped.get_height(),
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: gradient.get_start_color().to_f32_array(),
                        secondary_color: gradient.get_end_color().to_f32_array(),
                        radii: [
                            radius.get_top_left() * scale,
                            radius.get_top_right() * scale,
                            radius.get_bottom_right() * scale,
                            radius.get_bottom_left() * scale,
                        ],
                        stroke_or_blur: 0.0,
                        angle_rad: gradient.get_angle_deg().to_radians(),
                        kind: GpuPrimitiveKind::Gradient as u32,
                        reserved: 0,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::BoxShadow {
                    rect,
                    radius,
                    shadow,
                } => {
                    let spread: f32 = shadow.get_spread_radius() * scale;
                    let blur: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
                    let sigma: f32 = (blur * 0.5).max(0.75);
                    let pad: f32 = (sigma * 3.0).ceil();
                    let base_rect: Rect = snap_physical_rect(*rect, scale);
                    let caster_x: f32 =
                        base_rect.get_x() + (shadow.get_offset_x() * scale).round() - spread;
                    let caster_y: f32 =
                        base_rect.get_y() + (shadow.get_offset_y() * scale).round() - spread;
                    let caster_w: f32 = (base_rect.get_width() + spread * 2.0).max(1.0);
                    let caster_h: f32 = (base_rect.get_height() + spread * 2.0).max(1.0);
                    let quad: [f32; 4] = [
                        caster_x - pad,
                        caster_y - pad,
                        caster_w + pad * 2.0,
                        caster_h + pad * 2.0,
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: shadow.get_color().to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [
                            (radius.get_top_left() * scale + spread).max(0.0),
                            (radius.get_top_right() * scale + spread).max(0.0),
                            (radius.get_bottom_right() * scale + spread).max(0.0),
                            (radius.get_bottom_left() * scale + spread).max(0.0),
                        ],
                        stroke_or_blur: sigma,
                        angle_rad: pad,
                        kind: GpuPrimitiveKind::Shadow as u32,
                        reserved: 0,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::BackdropBlur {
                    rect,
                    radius,
                    blur_radius,
                } => {
                    *has_unsupported_effect = true;
                    let snapped: Rect = snap_physical_rect(*rect, scale);
                    let quad: [f32; 4] = [
                        snapped.get_x(),
                        snapped.get_y(),
                        snapped.get_width(),
                        snapped.get_height(),
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: [1.0, 1.0, 1.0, 1.0],
                        secondary_color: [0.0; 4],
                        radii: [
                            radius.get_top_left() * scale,
                            radius.get_top_right() * scale,
                            radius.get_bottom_right() * scale,
                            radius.get_bottom_left() * scale,
                        ],
                        stroke_or_blur: (*blur_radius * scale).clamp(0.5, 96.0),
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::BackdropBlur as u32,
                        reserved: 0,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::TextRun { rect, text, style } => {
                    let quad: [f32; 4] = [
                        rect.get_x() * scale,
                        rect.get_y() * scale,
                        rect.get_width() * scale,
                        rect.get_height() * scale,
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let glyph_count: usize = if text.is_ascii() {
                        text.as_bytes()
                            .iter()
                            .filter(|b: &&u8| **b != b' ')
                            .count()
                            .max(1)
                    } else {
                        text.chars().filter(|c: &char| *c != ' ').count().max(1)
                    };
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: style.get_color().to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [0.0; 4],
                        stroke_or_blur: style.get_font_size() * scale,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::Glyph as u32,
                        reserved: glyph_count as u32,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::TextCaret {
                    text_rect,
                    content_rect,
                    text,
                    style,
                    caret_height,
                    color,
                    multiline,
                } => {
                    if active_clip.get_width() <= 0.0 || active_clip.get_height() <= 0.0 {
                        culled_instances += 1;
                        continue;
                    }
                    let meas: TextLayoutResult = DeterministicTextMeasurer.measure(
                        text,
                        *style,
                        if *multiline {
                            Some(content_rect.get_width().max(1.0))
                        } else {
                            None
                        },
                    );
                    let last_w: f32 = meas
                        .get_lines()
                        .last()
                        .map(TextLine::get_width)
                        .unwrap_or(0.0);
                    let line_idx: usize = meas.get_lines().len().saturating_sub(1);
                    let trailing_gap: f32 = if last_w > 0.0 { 1.5 } else { 0.0 };
                    let min_x: f32 = content_rect.get_x();
                    let max_x: f32 = (content_rect.right() - 2.0).max(min_x);
                    let caret_x: f32 = (text_rect.get_x() + last_w + trailing_gap)
                        .min(max_x)
                        .max(min_x);
                    let caret_y: f32 = if *multiline {
                        (content_rect.get_y()
                            + (line_idx as f32) * meas.get_line_height_px()
                            + ((meas.get_line_height_px() - caret_height) * 0.5).max(0.0))
                        .min(content_rect.bottom() - caret_height)
                        .max(content_rect.get_y())
                    } else {
                        content_rect.get_y()
                            + ((content_rect.get_height() - caret_height) * 0.5).max(0.0)
                    };
                    let quad: [f32; 4] = [
                        caret_x * scale,
                        caret_y * scale,
                        (1.5 * scale).max(1.0),
                        (caret_height * scale).max(2.0),
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: color.to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [0.5 * scale; 4],
                        stroke_or_blur: 0.0,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::RoundedRect as u32,
                        reserved: 0,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::Image { rect, radius, .. } => {
                    let snapped: Rect = snap_physical_rect(*rect, scale);
                    let quad: [f32; 4] = [
                        snapped.get_x(),
                        snapped.get_y(),
                        snapped.get_width(),
                        snapped.get_height(),
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: [1.0, 1.0, 1.0, 1.0],
                        secondary_color: [0.0; 4],
                        radii: [
                            radius.get_top_left() * scale,
                            radius.get_top_right() * scale,
                            radius.get_bottom_right() * scale,
                            radius.get_bottom_left() * scale,
                        ],
                        stroke_or_blur: 0.0,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::TexturedQuad as u32,
                        reserved: 0,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::Circle {
                    center,
                    radius,
                    color,
                } => {
                    let r: f32 = radius * scale;
                    let quad: [f32; 4] = [
                        center.get_x() * scale - r,
                        center.get_y() * scale - r,
                        r * 2.0,
                        r * 2.0,
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: color.to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [r; 4],
                        stroke_or_blur: 0.0,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::Circle as u32,
                        reserved: 0,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::Line {
                    from,
                    to,
                    color,
                    width,
                } => {
                    let dx: f32 = (to.get_x() - from.get_x()) * scale;
                    let dy: f32 = (to.get_y() - from.get_y()) * scale;
                    let x0: f32 = from.get_x().min(to.get_x()) * scale;
                    let y0: f32 = from.get_y().min(to.get_y()) * scale;
                    let w: f32 = dx.abs().max(width * scale);
                    let h: f32 = dy.abs().max(width * scale);
                    let quad: [f32; 4] = [x0, y0, w, h];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: color.to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [
                            from.get_x() * scale,
                            from.get_y() * scale,
                            to.get_x() * scale,
                            to.get_y() * scale,
                        ],
                        stroke_or_blur: width * scale,
                        angle_rad: dy.atan2(dx),
                        kind: GpuPrimitiveKind::Line as u32,
                        reserved: 0,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::Triangle { p0, p1, p2, color } => {
                    let min_x: f32 = p0.get_x().min(p1.get_x()).min(p2.get_x()) * scale;
                    let min_y: f32 = p0.get_y().min(p1.get_y()).min(p2.get_y()) * scale;
                    let max_x: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x()) * scale;
                    let max_y: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y()) * scale;
                    let quad: [f32; 4] = [
                        min_x,
                        min_y,
                        (max_x - min_x).max(1.0),
                        (max_y - min_y).max(1.0),
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: color.to_f32_array(),
                        secondary_color: [p0.get_x() * scale, p0.get_y() * scale, 0.0, 0.0],
                        radii: [
                            p1.get_x() * scale,
                            p1.get_y() * scale,
                            p2.get_x() * scale,
                            p2.get_y() * scale,
                        ],
                        stroke_or_blur: 0.0,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::Triangle as u32,
                        reserved: 3,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
                DrawCommand::SpriteRegion { rect, tint, .. } => {
                    let snapped: Rect = snap_physical_rect(*rect, scale);
                    let quad: [f32; 4] = [
                        snapped.get_x(),
                        snapped.get_y(),
                        snapped.get_width(),
                        snapped.get_height(),
                    ];
                    if !Self::quad_intersects_clip(quad, active_clip) {
                        culled_instances += 1;
                        continue;
                    }
                    let inst: GpuQuadInstance = GpuQuadInstance {
                        rect: quad,
                        clip: [
                            active_clip.get_x(),
                            active_clip.get_y(),
                            active_clip.right(),
                            active_clip.bottom(),
                        ],
                        color: tint.to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [0.0; 4],
                        stroke_or_blur: 0.0,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::TexturedQuad as u32,
                        reserved: 1,
                    };
                    Self::push_instance_coalesced(
                        batches,
                        active_batches,
                        &mut tail_state,
                        clip_entry,
                        inst,
                        &mut coalesced_merges,
                    );
                    uploaded_instances += 1;
                }
            }
        }
        let upload_bytes: usize = uploaded_instances.saturating_mul(GPU_QUAD_INSTANCE_BYTES);
        let acquired_fence: u64 = match resource_ring
            .acquire_staging(upload_bytes, GpuUploadPriority::RequiredFrame)
        {
            GpuRingAcquireResult::Acquired { fence_id, .. } => fence_id,
            GpuRingAcquireResult::DeferredFrameRingFull
            | GpuRingAcquireResult::DroppedPrefetchRingFull => {
                let prev: u64 = resource_ring.get_completed_fence_id().saturating_add(1);
                let _: usize = resource_ring.complete_fence(prev);
                match resource_ring.acquire_staging(upload_bytes, GpuUploadPriority::RequiredFrame)
                {
                    GpuRingAcquireResult::Acquired { fence_id, .. } => fence_id,
                    GpuRingAcquireResult::DeferredFrameRingFull
                    | GpuRingAcquireResult::DroppedPrefetchRingFull => {
                        resource_ring.get_next_fence_id()
                    }
                }
            }
        };
        let active_slice: &[GpuBatch] = &batches[..(*active_batches).min(batches.len())];
        let submitted: usize = driver.submit_batches(phys_w, phys_h, clear_color, active_slice)?;
        *last_fence_id = acquired_fence;
        let retire_window: u64 = acquired_fence.saturating_sub(2);
        if retire_window > 0 {
            let _: usize = resource_ring.complete_fence(retire_window);
        }
        *last_submitted_batches = submitted;
        *last_coalesced_merges = coalesced_merges;
        *last_culled_instances = culled_instances;
        *last_uploaded_instances = uploaded_instances;
        *last_uploaded_bytes = upload_bytes;
        let pixels: usize = if *enable_readback {
            let (_, px): (usize, usize) = readback.render(display_list, damage);
            *cpu_readback_frames = cpu_readback_frames.saturating_add(1);
            *last_cpu_rgba_upload_bytes = (phys_w as usize)
                .saturating_mul(phys_h as usize)
                .saturating_mul(4);
            px
        } else {
            *direct_gpu_frames = direct_gpu_frames.saturating_add(1);
            *last_cpu_rgba_upload_bytes = 0;
            (phys_w as usize).saturating_mul(phys_h as usize)
        };
        Ok((submitted, pixels))
    }

    /// Appends `instance` in strict draw order into the tail `GpuBatch` when clip-compatible, or opens the next reusable batch slot.
    ///
    /// # Arguments
    ///
    /// - `&mut Vec<GpuBatch>` - Persistent batch pool vector.
    /// - `&mut usize` - Active batch watermark for the current frame.
    /// - `&mut GpuBatchCoalesceState` - Unclipped containment metadata for the active tail batch.
    /// - `GpuClipEntry` - Active physical scissor and corner-free interior safe clip rectangle.
    /// - `GpuQuadInstance` - Instance data to batch in exact back-to-front order.
    /// - `&mut usize` - Running counter of cross-clip batch splits avoided by coalescing.
    fn push_instance_coalesced(
        batches: &mut Vec<GpuBatch>,
        active_batches: &mut usize,
        tail_state: &mut GpuBatchCoalesceState,
        clip_entry: GpuClipEntry,
        instance: GpuQuadInstance,
        coalesced_merges: &mut usize,
    ) {
        let scissor: Rect = clip_entry.scissor;
        let is_blur: bool = instance.kind == GpuPrimitiveKind::BackdropBlur as u32;
        let inst_unclipped: bool =
            !is_blur && Self::quad_inside_rect(instance.rect, clip_entry.safe_inner);
        let count: usize = *active_batches;
        if count > 0 {
            let last: &mut GpuBatch = &mut batches[count - 1];
            if last.get_instances().len() < MAX_QUADS_PER_BATCH {
                let last_scissor: Rect = last.get_scissor();
                let blur_boundary: bool = tail_state.has_backdrop_blur || is_blur;
                if last_scissor == scissor && !blur_boundary {
                    last.get_mut_instances().push(instance);
                    tail_state.all_unclipped = tail_state.all_unclipped && inst_unclipped;
                    tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
                    return;
                }
                if !blur_boundary
                    && inst_unclipped
                    && Self::quad_inside_rect(instance.rect, last_scissor)
                {
                    last.get_mut_instances().push(instance);
                    tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
                    *coalesced_merges += 1;
                    return;
                }
                if !blur_boundary
                    && tail_state.all_unclipped
                    && Self::rect_inside_rect(tail_state.bounds, scissor)
                {
                    last.set_scissor(scissor);
                    last.get_mut_instances().push(instance);
                    tail_state.all_unclipped = inst_unclipped;
                    tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
                    *coalesced_merges += 1;
                    return;
                }
            }
        }
        if count < batches.len() {
            let slot: &mut GpuBatch = &mut batches[count];
            slot.set_scissor(scissor);
            slot.get_mut_instances().clear();
            slot.get_mut_instances().push(instance);
        } else {
            let mut batch: GpuBatch = GpuBatch::default();
            batch.set_scissor(scissor);
            batch.get_mut_instances().push(instance);
            batches.push(batch);
        }
        *active_batches = count + 1;
        tail_state.all_unclipped = inst_unclipped;
        tail_state.has_backdrop_blur = is_blur;
        tail_state.bounds = Rect::new(
            instance.rect[0],
            instance.rect[1],
            instance.rect[2],
            instance.rect[3],
        );
    }
}