euv-core 0.48.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
/// Portable WGSL 64-byte `GpuPrimitiveInstance` instanced uber-shader covering all 8 `PrimKind` variants for WebGPU and Vulkan pipelines.
pub const WGSL_UI_SHADER: &str = r#"
struct LayerUniforms {
    viewport: vec2<f32>,
    scroll_offset: vec2<f32>,
};
struct GpuPrimitiveInstance {
    rect: vec4<f32>,
    clip_rect: vec4<f32>,
    color_rgba: u32,
    aux_color_rgba: u32,
    uv_rect: vec2<u32>,
    radii_border: vec2<u32>,
    kind_flags: u32,
    extra_param: u32,
};
struct VertexOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) local_pos: vec2<f32>,
    @location(1) half_size: vec2<f32>,
    @location(2) uv: vec2<f32>,
    @location(3) clip_rect: vec4<f32>,
    @location(4) color: vec4<f32>,
    @location(5) aux_color: vec4<f32>,
    @location(6) radii: vec4<f32>,
    @location(7) params: vec2<f32>,
    @location(8) @interpolate(flat) kind_flags: u32,
    @location(9) @interpolate(flat) uv_raw: vec4<f32>,
};
@group(0) @binding(0) var<uniform> u_layer: LayerUniforms;
@group(0) @binding(1) var<storage, read> instances: array<GpuPrimitiveInstance>;
@group(0) @binding(2) var u_sampler: sampler;
@group(0) @binding(3) var glyph_atlas_r8: texture_2d<f32>;
@group(0) @binding(4) var glyph_atlas_rgba8: texture_2d<f32>;
@group(0) @binding(5) var image_tex: texture_2d<f32>;
@group(0) @binding(6) var external_tex: texture_2d<f32>;
fn unpack_rgba8(c: u32) -> vec4<f32> {
    return vec4<f32>(
        f32(c & 0xFFu),
        f32((c >> 8u) & 0xFFu),
        f32((c >> 16u) & 0xFFu),
        f32((c >> 24u) & 0xFFu)
    ) * (1.0 / 255.0);
}
fn unpack_u16x4(packed: vec2<u32>) -> vec4<f32> {
    return vec4<f32>(
        f32(packed.x & 0xFFFFu),
        f32((packed.x >> 16u) & 0xFFFFu),
        f32(packed.y & 0xFFFFu),
        f32((packed.y >> 16u) & 0xFFFFu)
    );
}
@vertex
fn vs_main(@builtin(vertex_index) vid: u32, @builtin(instance_index) iid: u32) -> VertexOut {
    var corners = array<vec2<f32>, 6>(
        vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0),
        vec2<f32>(1.0, 0.0), vec2<f32>(1.0, 1.0), vec2<f32>(0.0, 1.0)
    );
    let inst = instances[iid];
    let corner = corners[vid % 6u];
    let vp = max(u_layer.viewport, vec2<f32>(1.0, 1.0));
    let px = (inst.rect.xy - u_layer.scroll_offset) + corner * inst.rect.zw;
    let ndc = vec2<f32>((px.x / vp.x) * 2.0 - 1.0, 1.0 - (px.y / vp.y) * 2.0);
    let uv_raw = unpack_u16x4(inst.uv_rect) * (1.0 / 65535.0);
    var out: VertexOut;
    out.pos = vec4<f32>(ndc, 0.0, 1.0);
    out.local_pos = (corner - vec2<f32>(0.5, 0.5)) * inst.rect.zw;
    out.half_size = inst.rect.zw * 0.5;
    out.uv = mix(uv_raw.xy, uv_raw.zw, corner);
    out.clip_rect = inst.clip_rect;
    out.color = unpack_rgba8(inst.color_rgba);
    out.aux_color = unpack_rgba8(inst.aux_color_rgba);
    out.radii = unpack_u16x4(inst.radii_border) * (1.0 / 16.0);
    out.params = unpack2x16float(inst.extra_param);
    out.kind_flags = inst.kind_flags;
    out.uv_raw = uv_raw;
    return out;
}
fn sdf_box(p: vec2<f32>, half_sz: vec2<f32>, r: f32) -> f32 {
    let cr = min(r, min(half_sz.x, half_sz.y));
    let q = abs(p) - half_sz + vec2<f32>(cr, cr);
    return length(max(q, vec2<f32>(0.0, 0.0))) + min(max(q.x, q.y), 0.0) - cr;
}
fn sdf_segment(p: vec2<f32>, a: vec2<f32>, b: vec2<f32>) -> f32 {
    let pa = p - a;
    let ba = b - a;
    let denom = max(dot(ba, ba), 1e-6);
    let h = clamp(dot(pa, ba) / denom, 0.0, 1.0);
    return length(pa - ba * h);
}
fn sdf_triangle(p: vec2<f32>, p0: vec2<f32>, p1: vec2<f32>, p2: vec2<f32>) -> f32 {
    let e0 = p1 - p0;
    let e1 = p2 - p1;
    let e2 = p0 - p2;
    let v0 = p - p0;
    let v1 = p - p1;
    let v2 = p - p2;
    let pq0 = v0 - e0 * clamp(dot(v0, e0) / max(dot(e0, e0), 1e-6), 0.0, 1.0);
    let pq1 = v1 - e1 * clamp(dot(v1, e1) / max(dot(e1, e1), 1e-6), 0.0, 1.0);
    let pq2 = v2 - e2 * clamp(dot(v2, e2) / max(dot(e2, e2), 1e-6), 0.0, 1.0);
    let s = sign(e0.x * e2.y - e0.y * e2.x);
    let d0 = vec2<f32>(dot(pq0, pq0), s * (v0.x * e0.y - v0.y * e0.x));
    let d1 = vec2<f32>(dot(pq1, pq1), s * (v1.x * e1.y - v1.y * e1.x));
    let d2 = vec2<f32>(dot(pq2, pq2), s * (v2.x * e2.y - v2.y * e2.x));
    let d = min(min(d0, d1), d2);
    return -sqrt(d.x) * sign(d.y);
}
fn unit_cov(dist: f32) -> f32 {
    return clamp(0.5 - dist, 0.0, 1.0);
}
fn ign_dither(pos_xy: vec2<f32>) -> f32 {
    return fract(52.982918 * fract(dot(pos_xy, vec2<f32>(0.06711056, 0.00583715)))) - 0.5;
}
fn gaussian_falloff(dist: f32, sigma: f32) -> f32 {
    let s = max(sigma, 0.5);
    let x = dist / (s * 1.4142135);
    let ax = abs(x);
    let t = 1.0 / (1.0 + 0.3275911 * ax);
    let poly = t * (0.2548296 + t * (-0.28449672 + t * (1.4214137 + t * (-1.453152 + t * 1.0614054))));
    let erfc_abs = poly * exp(-ax * ax);
    return clamp(select(1.0 - 0.5 * erfc_abs, 0.5 * erfc_abs, x >= 0.0), 0.0, 1.0);
}
@fragment
fn fs_main(in: VertexOut) -> @location(0) vec4<f32> {
    if (in.pos.x < in.clip_rect.x || in.pos.y < in.clip_rect.y || in.pos.x > in.clip_rect.z || in.pos.y > in.clip_rect.w) {
        discard;
    }
    let kind = in.kind_flags & 0xFu;
    let sub_kind = (in.kind_flags >> 4u) & 0x3u;
    let r_top = select(in.radii.y, in.radii.x, in.local_pos.x < 0.0);
    let r_bot = select(in.radii.z, in.radii.w, in.local_pos.x < 0.0);
    let r_quad = select(r_bot, r_top, in.local_pos.y < 0.0);
    let r = min(r_quad, min(in.half_size.x, in.half_size.y));
    let cov_box = unit_cov(sdf_box(in.local_pos, in.half_size, r));
    if (kind == 1u) {
        let dir = vec2<f32>(sin(in.params.y), cos(in.params.y));
        let norm_p = in.local_pos / max(in.half_size, vec2<f32>(1.0, 1.0));
        let t = clamp(dot(norm_p, dir) * 0.5 + 0.5, 0.0, 1.0);
        let lin0 = in.color.rgb * in.color.rgb;
        let lin1 = in.aux_color.rgb * in.aux_color.rgb;
        let grad_rgb = sqrt(clamp(mix(lin0, lin1, t), vec3<f32>(0.0), vec3<f32>(1.0)));
        let grad_a = mix(in.color.a, in.aux_color.a, t) * cov_box;
        return vec4<f32>(grad_rgb, grad_a);
    }
    if (kind == 2u) {
        let caster_half = max(in.half_size - vec2<f32>(in.params.y, in.params.y), vec2<f32>(0.5, 0.5));
        let sdist = sdf_box(in.local_pos, caster_half, r_quad);
        let sa = in.color.a * gaussian_falloff(sdist, in.params.x);
        let d = ign_dither(in.pos.xy) * (1.0 / 255.0);
        return vec4<f32>(clamp(in.color.rgb + vec3<f32>(d, d, d), vec3<f32>(0.0), vec3<f32>(1.0)), clamp(sa + d * 0.5, 0.0, 1.0));
    }
    if (kind == 3u) {
        let glyph_cov = textureSample(glyph_atlas_r8, u_sampler, in.uv).r;
        return vec4<f32>(in.color.rgb, clamp(in.color.a * glyph_cov, 0.0, 1.0));
    }
    if (kind == 4u) {
        let emoji = textureSample(glyph_atlas_rgba8, u_sampler, in.uv);
        return vec4<f32>(emoji.rgb, emoji.a * in.color.a);
    }
    if (kind == 5u) {
        let texel = textureSample(image_tex, u_sampler, in.uv);
        return vec4<f32>(texel.rgb * in.color.rgb, texel.a * in.color.a * cov_box);
    }
    if (kind == 6u) {
        var shape_cov = 0.0;
        if (sub_kind == 0u) {
            let rad = min(in.half_size.x, in.half_size.y);
            shape_cov = unit_cov(length(in.local_pos) - rad);
        } else if (sub_kind == 1u) {
            let a = (in.uv_raw.xy - vec2<f32>(0.5, 0.5)) * (in.half_size * 2.0);
            let b = (in.uv_raw.zw - vec2<f32>(0.5, 0.5)) * (in.half_size * 2.0);
            shape_cov = unit_cov(sdf_segment(in.local_pos, a, b) - max(in.params.x * 0.5, 0.5));
        } else {
            let p0 = (in.aux_color.xy - vec2<f32>(0.5, 0.5)) * (in.half_size * 2.0);
            let p1 = (in.uv_raw.xy - vec2<f32>(0.5, 0.5)) * (in.half_size * 2.0);
            let p2 = (in.uv_raw.zw - vec2<f32>(0.5, 0.5)) * (in.half_size * 2.0);
            shape_cov = unit_cov(sdf_triangle(in.local_pos, p0, p1, p2));
        }
        return vec4<f32>(in.color.rgb, in.color.a * shape_cov);
    }
    if (kind == 7u) {
        let ext_texel = textureSample(external_tex, u_sampler, in.uv);
        return vec4<f32>(ext_texel.rgb * in.color.rgb, ext_texel.a * in.color.a * cov_box);
    }
    let outer_cov = cov_box;
    let bw = in.params.x;
    let inner_half = max(in.half_size - vec2<f32>(bw, bw), vec2<f32>(0.0, 0.0));
    let inner_r = max(r - bw, 0.0);
    let inner_valid = inner_half.x > 0.0 && inner_half.y > 0.0;
    let inner_cov = select(outer_cov, select(0.0, unit_cov(sdf_box(in.local_pos, inner_half, inner_r)), inner_valid), bw > 0.0);
    let border_cov = clamp(outer_cov - inner_cov, 0.0, 1.0);
    let fill_a = in.color.a * inner_cov;
    let border_a = in.aux_color.a * border_cov;
    let total_a = clamp(fill_a + border_a, 0.0, 1.0);
    let safe_a = max(total_a, 0.0001);
    let lin_rgb = (in.color.rgb * in.color.rgb * fill_a + in.aux_color.rgb * in.aux_color.rgb * border_a) / safe_a;
    let rgb = sqrt(clamp(lin_rgb, vec3<f32>(0.0), vec3<f32>(1.0)));
    return vec4<f32>(rgb, total_a);
}
"#;

/// WGSL Dual-Kawase / separable Gaussian `BackdropBlur` downsample & upsample pass shader for WebGPU and Vulkan.
pub const WGSL_BACKDROP_BLUR_SHADER: &str = r#"
struct BlurUniforms {
    texel_size: vec2<f32>,
    blur_offset: f32,
    pass_mode: u32,
};
struct BlurVertexOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) uv: vec2<f32>,
};
@group(0) @binding(0) var<uniform> u_blur: BlurUniforms;
@group(0) @binding(1) var u_blur_sampler: sampler;
@group(0) @binding(2) var u_blur_src: texture_2d<f32>;
@vertex
fn vs_blur(@builtin(vertex_index) vid: u32) -> BlurVertexOut {
    var verts = array<vec2<f32>, 3>(
        vec2<f32>(-1.0, -1.0),
        vec2<f32>(3.0, -1.0),
        vec2<f32>(-1.0, 3.0)
    );
    let p = verts[vid % 3u];
    var out: BlurVertexOut;
    out.pos = vec4<f32>(p, 0.0, 1.0);
    out.uv = vec2<f32>(p.x * 0.5 + 0.5, 0.5 - p.y * 0.5);
    return out;
}
@fragment
fn fs_blur_downsample(in: BlurVertexOut) -> @location(0) vec4<f32> {
    let half_px = u_blur.texel_size * max(u_blur.blur_offset, 0.5);
    var sum = textureSample(u_blur_src, u_blur_sampler, in.uv) * 4.0;
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv - half_px);
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + half_px);
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(half_px.x, -half_px.y));
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv - vec2<f32>(half_px.x, -half_px.y));
    return sum * 0.125;
}
@fragment
fn fs_blur_upsample(in: BlurVertexOut) -> @location(0) vec4<f32> {
    let hp = u_blur.texel_size * max(u_blur.blur_offset, 0.5);
    var sum = textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(-hp.x * 2.0, 0.0));
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(-hp.x, hp.y)) * 2.0;
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(0.0, hp.y * 2.0));
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(hp.x, hp.y)) * 2.0;
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(hp.x * 2.0, 0.0));
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(hp.x, -hp.y)) * 2.0;
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(0.0, -hp.y * 2.0));
    sum += textureSample(u_blur_src, u_blur_sampler, in.uv + vec2<f32>(-hp.x, -hp.y)) * 2.0;
    return sum * (1.0 / 12.0);
}
"#;

/// Apple Metal Shading Language (MSL) 64-byte `GpuPrimitiveInstance` instanced uber-shader covering all 8 `PrimKind` variants for macOS, iOS, and iPadOS.
pub const MSL_UI_SHADER: &str = r#"
#include <metal_stdlib>
using namespace metal;
struct LayerUniforms {
    float2 viewport_size;
    float2 scroll_offset;
};
struct GpuPrimitiveInstance {
    float4 rect;
    float4 clip_rect;
    uint color_rgba;
    uint aux_color_rgba;
    ushort4 uv_rect;
    ushort4 radii_border;
    uint kind_flags;
    uint extra_param;
};
struct QuadIn {
    float4 position [[position]];
    float2 local_pos;
    float2 half_size;
    float2 uv;
    float4 clip_rect;
    float4 color;
    float4 aux_color;
    float4 radii;
    float2 params;
    uint kind_flags [[flat]];
    float4 uv_raw [[flat]];
};
static inline float4 euv_unpack_rgba8(uint c) {
    return float4(
        float(c & 0xFFu),
        float((c >> 8u) & 0xFFu),
        float((c >> 16u) & 0xFFu),
        float((c >> 24u) & 0xFFu)
    ) * (1.0f / 255.0f);
}
vertex QuadIn euv_ui_vertex(
    uint vid [[vertex_id]],
    uint iid [[instance_id]],
    const device GpuPrimitiveInstance* instances [[buffer(0)]],
    constant LayerUniforms& layer [[buffer(1)]]
) {
    float2 corners[6] = {
        float2(0.0f, 0.0f), float2(1.0f, 0.0f), float2(0.0f, 1.0f),
        float2(1.0f, 0.0f), float2(1.0f, 1.0f), float2(0.0f, 1.0f)
    };
    GpuPrimitiveInstance inst = instances[iid];
    float2 corner = corners[vid % 6u];
    float2 vp = max(layer.viewport_size, float2(1.0f, 1.0f));
    float2 px = (inst.rect.xy - layer.scroll_offset) + corner * inst.rect.zw;
    float2 ndc = float2((px.x / vp.x) * 2.0f - 1.0f, 1.0f - (px.y / vp.y) * 2.0f);
    float4 uv_raw = float4(inst.uv_rect) * (1.0f / 65535.0f);
    half2 extra_h = as_type<half2>(inst.extra_param);
    QuadIn out;
    out.position = float4(ndc, 0.0f, 1.0f);
    out.local_pos = (corner - 0.5f) * inst.rect.zw;
    out.half_size = inst.rect.zw * 0.5f;
    out.uv = mix(uv_raw.xy, uv_raw.zw, corner);
    out.clip_rect = inst.clip_rect;
    out.color = euv_unpack_rgba8(inst.color_rgba);
    out.aux_color = euv_unpack_rgba8(inst.aux_color_rgba);
    out.radii = float4(inst.radii_border) * (1.0f / 16.0f);
    out.params = float2(extra_h);
    out.kind_flags = inst.kind_flags;
    out.uv_raw = uv_raw;
    return out;
}
static inline float euv_sdf_box(float2 p, float2 half_sz, float r) {
    float cr = min(r, min(half_sz.x, half_sz.y));
    float2 q = abs(p) - half_sz + float2(cr);
    return length(max(q, 0.0f)) + min(max(q.x, q.y), 0.0f) - cr;
}
static inline float euv_sdf_segment(float2 p, float2 a, float2 b) {
    float2 pa = p - a;
    float2 ba = b - a;
    float denom = max(dot(ba, ba), 1e-6f);
    float h = clamp(dot(pa, ba) / denom, 0.0f, 1.0f);
    return length(pa - ba * h);
}
static inline float euv_sdf_triangle(float2 p, float2 p0, float2 p1, float2 p2) {
    float2 e0 = p1 - p0;
    float2 e1 = p2 - p1;
    float2 e2 = p0 - p2;
    float2 v0 = p - p0;
    float2 v1 = p - p1;
    float2 v2 = p - p2;
    float2 pq0 = v0 - e0 * clamp(dot(v0, e0) / max(dot(e0, e0), 1e-6f), 0.0f, 1.0f);
    float2 pq1 = v1 - e1 * clamp(dot(v1, e1) / max(dot(e1, e1), 1e-6f), 0.0f, 1.0f);
    float2 pq2 = v2 - e2 * clamp(dot(v2, e2) / max(dot(e2, e2), 1e-6f), 0.0f, 1.0f);
    float s = sign(e0.x * e2.y - e0.y * e2.x);
    float2 d0 = float2(dot(pq0, pq0), s * (v0.x * e0.y - v0.y * e0.x));
    float2 d1 = float2(dot(pq1, pq1), s * (v1.x * e1.y - v1.y * e1.x));
    float2 d2 = float2(dot(pq2, pq2), s * (v2.x * e2.y - v2.y * e2.x));
    float2 d = min(min(d0, d1), d2);
    return -sqrt(d.x) * sign(d.y);
}
static inline float euv_unit_cov(float dist) {
    return clamp(0.5f - dist, 0.0f, 1.0f);
}
static inline float euv_ign_dither(float2 pos_xy) {
    return fract(52.982918f * fract(dot(pos_xy, float2(0.06711056f, 0.00583715f)))) - 0.5f;
}
static inline float euv_gaussian_falloff(float dist, float sigma) {
    float s = max(sigma, 0.5f);
    float x = dist / (s * 1.4142135f);
    float ax = fabs(x);
    float t = 1.0f / (1.0f + 0.3275911f * ax);
    float poly = t * (0.2548296f + t * (-0.28449672f + t * (1.4214137f + t * (-1.453152f + t * 1.0614054f))));
    float erfc_abs = poly * exp(-ax * ax);
    return clamp((x >= 0.0f) ? (0.5f * erfc_abs) : (1.0f - 0.5f * erfc_abs), 0.0f, 1.0f);
}
fragment float4 euv_ui_fragment(
    QuadIn in [[stage_in]],
    texture2d<float> glyph_atlas_r8 [[texture(0)]],
    texture2d<float> glyph_atlas_rgba8 [[texture(1)]],
    texture2d<float> image_tex [[texture(2)]],
    texture2d<float> external_tex [[texture(3)]]
) {
    if (in.position.x < in.clip_rect.x || in.position.y < in.clip_rect.y ||
        in.position.x > in.clip_rect.z || in.position.y > in.clip_rect.w) {
        discard_fragment();
    }
    constexpr sampler linear_sampler(mag_filter::linear, min_filter::linear, address::clamp_to_edge);
    uint kind = in.kind_flags & 0xFu;
    uint sub_kind = (in.kind_flags >> 4u) & 0x3u;
    float r_top = (in.local_pos.x < 0.0f) ? in.radii.x : in.radii.y;
    float r_bot = (in.local_pos.x < 0.0f) ? in.radii.w : in.radii.z;
    float r_quad = (in.local_pos.y < 0.0f) ? r_top : r_bot;
    float r = min(r_quad, min(in.half_size.x, in.half_size.y));
    float cov_box = euv_unit_cov(euv_sdf_box(in.local_pos, in.half_size, r));
    if (kind == 1u) {
        float2 dir = float2(sin(in.params.y), cos(in.params.y));
        float2 norm_p = in.local_pos / max(in.half_size, float2(1.0f));
        float t = clamp(dot(norm_p, dir) * 0.5f + 0.5f, 0.0f, 1.0f);
        float3 lin0 = in.color.rgb * in.color.rgb;
        float3 lin1 = in.aux_color.rgb * in.aux_color.rgb;
        float3 grad_rgb = sqrt(clamp(mix(lin0, lin1, t), 0.0f, 1.0f));
        float grad_a = mix(in.color.a, in.aux_color.a, t) * cov_box;
        return float4(grad_rgb, grad_a);
    }
    if (kind == 2u) {
        float2 caster_half = max(in.half_size - float2(in.params.y), float2(0.5f));
        float sdist = euv_sdf_box(in.local_pos, caster_half, r_quad);
        float sa = in.color.a * euv_gaussian_falloff(sdist, in.params.x);
        float d = euv_ign_dither(in.position.xy) * (1.0f / 255.0f);
        return float4(clamp(in.color.rgb + d, 0.0f, 1.0f), clamp(sa + d * 0.5f, 0.0f, 1.0f));
    }
    if (kind == 3u) {
        float glyph_cov = glyph_atlas_r8.sample(linear_sampler, in.uv).r;
        return float4(in.color.rgb, clamp(in.color.a * glyph_cov, 0.0f, 1.0f));
    }
    if (kind == 4u) {
        float4 emoji = glyph_atlas_rgba8.sample(linear_sampler, in.uv);
        return float4(emoji.rgb, emoji.a * in.color.a);
    }
    if (kind == 5u) {
        float4 texel = image_tex.sample(linear_sampler, in.uv);
        return float4(texel.rgb * in.color.rgb, texel.a * in.color.a * cov_box);
    }
    if (kind == 6u) {
        float shape_cov = 0.0f;
        if (sub_kind == 0u) {
            float rad = min(in.half_size.x, in.half_size.y);
            shape_cov = euv_unit_cov(length(in.local_pos) - rad);
        } else if (sub_kind == 1u) {
            float2 a = (in.uv_raw.xy - 0.5f) * (in.half_size * 2.0f);
            float2 b = (in.uv_raw.zw - 0.5f) * (in.half_size * 2.0f);
            shape_cov = euv_unit_cov(euv_sdf_segment(in.local_pos, a, b) - max(in.params.x * 0.5f, 0.5f));
        } else {
            float2 p0 = (in.aux_color.xy - 0.5f) * (in.half_size * 2.0f);
            float2 p1 = (in.uv_raw.xy - 0.5f) * (in.half_size * 2.0f);
            float2 p2 = (in.uv_raw.zw - 0.5f) * (in.half_size * 2.0f);
            shape_cov = euv_unit_cov(euv_sdf_triangle(in.local_pos, p0, p1, p2));
        }
        return float4(in.color.rgb, in.color.a * shape_cov);
    }
    if (kind == 7u) {
        float4 ext_texel = external_tex.sample(linear_sampler, in.uv);
        return float4(ext_texel.rgb * in.color.rgb, ext_texel.a * in.color.a * cov_box);
    }
    float outer_cov = cov_box;
    float bw = in.params.x;
    float2 inner_half = max(in.half_size - float2(bw), float2(0.0f));
    float inner_r = max(r - bw, 0.0f);
    float inner_cov = outer_cov;
    if (bw > 0.0f) {
        inner_cov = (inner_half.x > 0.0f && inner_half.y > 0.0f)
            ? euv_unit_cov(euv_sdf_box(in.local_pos, inner_half, inner_r))
            : 0.0f;
    }
    float border_cov = clamp(outer_cov - inner_cov, 0.0f, 1.0f);
    float fill_a = in.color.a * inner_cov;
    float border_a = in.aux_color.a * border_cov;
    float total_a = clamp(fill_a + border_a, 0.0f, 1.0f);
    if (total_a <= 0.0001f) {
        return float4(0.0f);
    }
    float3 lin_rgb = (in.color.rgb * in.color.rgb * fill_a + in.aux_color.rgb * in.aux_color.rgb * border_a) / total_a;
    float3 rgb = sqrt(clamp(lin_rgb, 0.0f, 1.0f));
    return float4(rgb, total_a);
}
"#;

/// Apple Metal Dual-Kawase / separable Gaussian `BackdropBlur` pass shader for macOS, iOS, and iPadOS.
pub const MSL_BACKDROP_BLUR_SHADER: &str = r#"
#include <metal_stdlib>
using namespace metal;
struct BlurUniforms {
    float2 texel_size;
    float blur_offset;
    uint pass_mode;
};
struct BlurOut {
    float4 position [[position]];
    float2 uv;
};
vertex BlurOut euv_blur_vertex(uint vid [[vertex_id]]) {
    float2 verts[3] = {
        float2(-1.0f, -1.0f),
        float2(3.0f, -1.0f),
        float2(-1.0f, 3.0f)
    };
    float2 p = verts[vid % 3u];
    BlurOut out;
    out.position = float4(p, 0.0f, 1.0f);
    out.uv = float2(p.x * 0.5f + 0.5f, 0.5f - p.y * 0.5f);
    return out;
}
fragment float4 euv_blur_downsample(
    BlurOut in [[stage_in]],
    constant BlurUniforms& u_blur [[buffer(0)]],
    texture2d<float> src_tex [[texture(0)]]
) {
    constexpr sampler s(mag_filter::linear, min_filter::linear, address::clamp_to_edge);
    float2 hp = u_blur.texel_size * max(u_blur.blur_offset, 0.5f);
    float4 sum = src_tex.sample(s, in.uv) * 4.0f;
    sum += src_tex.sample(s, in.uv - hp);
    sum += src_tex.sample(s, in.uv + hp);
    sum += src_tex.sample(s, in.uv + float2(hp.x, -hp.y));
    sum += src_tex.sample(s, in.uv - float2(hp.x, -hp.y));
    return sum * 0.125f;
}
fragment float4 euv_blur_upsample(
    BlurOut in [[stage_in]],
    constant BlurUniforms& u_blur [[buffer(0)]],
    texture2d<float> src_tex [[texture(0)]]
) {
    constexpr sampler s(mag_filter::linear, min_filter::linear, address::clamp_to_edge);
    float2 hp = u_blur.texel_size * max(u_blur.blur_offset, 0.5f);
    float4 sum = src_tex.sample(s, in.uv + float2(-hp.x * 2.0f, 0.0f));
    sum += src_tex.sample(s, in.uv + float2(-hp.x, hp.y)) * 2.0f;
    sum += src_tex.sample(s, in.uv + float2(0.0f, hp.y * 2.0f));
    sum += src_tex.sample(s, in.uv + float2(hp.x, hp.y)) * 2.0f;
    sum += src_tex.sample(s, in.uv + float2(hp.x * 2.0f, 0.0f));
    sum += src_tex.sample(s, in.uv + float2(hp.x, -hp.y)) * 2.0f;
    sum += src_tex.sample(s, in.uv + float2(0.0f, -hp.y * 2.0f));
    sum += src_tex.sample(s, in.uv + float2(-hp.x, -hp.y)) * 2.0f;
    return sum * (1.0f / 12.0f);
}
"#;

/// OpenGL ES 3.0 / WebGL 2 instanced 64-byte `GpuPrimitiveInstance` vertex shader paired with `GLSL_ES3_UI_FRAGMENT_SHADER`.
pub const GLSL_ES3_UI_VERTEX_SHADER: &str = r#"#version 300 es
precision highp float;
uniform vec2 u_viewport;
uniform vec2 u_scroll_offset;
layout(location = 0) in vec2 a_corner;
layout(location = 1) in vec4 a_rect;
layout(location = 2) in vec4 a_clip_rect;
layout(location = 3) in uvec2 a_colors;
layout(location = 4) in uvec4 a_uv_radii;
layout(location = 5) in uvec2 a_kind_extra;
out vec2 v_local_pos;
out vec2 v_half_size;
out vec2 v_uv;
out vec4 v_clip_rect;
out vec4 v_color;
out vec4 v_aux_color;
out vec4 v_radii;
out vec2 v_params;
flat out uint v_kind_flags;
flat out vec4 v_uv_raw;
vec4 unpack_rgba8(uint c) {
    return vec4(
        float(c & 0xFFu),
        float((c >> 8u) & 0xFFu),
        float((c >> 16u) & 0xFFu),
        float((c >> 24u) & 0xFFu)
    ) * (1.0 / 255.0);
}
void main() {
    vec2 vp = max(u_viewport, vec2(1.0));
    vec2 px = (a_rect.xy - u_scroll_offset) + a_corner * a_rect.zw;
    vec2 ndc = vec2((px.x / vp.x) * 2.0 - 1.0, 1.0 - (px.y / vp.y) * 2.0);
    vec4 uv_raw = vec4(
        float(a_uv_radii.x & 0xFFFFu),
        float((a_uv_radii.x >> 16u) & 0xFFFFu),
        float(a_uv_radii.y & 0xFFFFu),
        float((a_uv_radii.y >> 16u) & 0xFFFFu)
    ) * (1.0 / 65535.0);
    v_half_size = a_rect.zw * 0.5;
    v_local_pos = (a_corner - 0.5) * a_rect.zw;
    v_uv = mix(uv_raw.xy, uv_raw.zw, a_corner);
    v_clip_rect = a_clip_rect;
    v_color = unpack_rgba8(a_colors.x);
    v_aux_color = unpack_rgba8(a_colors.y);
    v_radii = vec4(
        float(a_uv_radii.z & 0xFFFFu),
        float((a_uv_radii.z >> 16u) & 0xFFFFu),
        float(a_uv_radii.w & 0xFFFFu),
        float((a_uv_radii.w >> 16u) & 0xFFFFu)
    ) * (1.0 / 16.0);
    v_params = unpackHalf2x16(a_kind_extra.y);
    v_kind_flags = a_kind_extra.x;
    v_uv_raw = uv_raw;
    gl_Position = vec4(ndc, 0.0, 1.0);
}
"#;

/// OpenGL ES 3.0 / WebGL 2 fragment uber-shader matching all 8 `PrimKind` variants, exact clipping, and linear-light compositing.
pub const GLSL_ES3_UI_FRAGMENT_SHADER: &str = r#"#version 300 es
precision highp float;
uniform sampler2D u_glyph_r8;
uniform sampler2D u_glyph_rgba8;
uniform sampler2D u_image_tex;
uniform sampler2D u_external_tex;
uniform vec2 u_viewport;
in vec2 v_local_pos;
in vec2 v_half_size;
in vec2 v_uv;
in vec4 v_clip_rect;
in vec4 v_color;
in vec4 v_aux_color;
in vec4 v_radii;
in vec2 v_params;
flat in uint v_kind_flags;
flat in vec4 v_uv_raw;
out vec4 frag_color;
float sdf_box(vec2 p, vec2 half_sz, float r) {
    float cr = min(r, min(half_sz.x, half_sz.y));
    vec2 q = abs(p) - half_sz + vec2(cr);
    return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - cr;
}
float sdf_segment(vec2 p, vec2 a, vec2 b) {
    vec2 pa = p - a;
    vec2 ba = b - a;
    float denom = max(dot(ba, ba), 1e-6);
    float h = clamp(dot(pa, ba) / denom, 0.0, 1.0);
    return length(pa - ba * h);
}
float sdf_triangle(vec2 p, vec2 p0, vec2 p1, vec2 p2) {
    vec2 e0 = p1 - p0;
    vec2 e1 = p2 - p1;
    vec2 e2 = p0 - p2;
    vec2 v0 = p - p0;
    vec2 v1 = p - p1;
    vec2 v2 = p - p2;
    vec2 pq0 = v0 - e0 * clamp(dot(v0, e0) / max(dot(e0, e0), 1e-6), 0.0, 1.0);
    vec2 pq1 = v1 - e1 * clamp(dot(v1, e1) / max(dot(e1, e1), 1e-6), 0.0, 1.0);
    vec2 pq2 = v2 - e2 * clamp(dot(v2, e2) / max(dot(e2, e2), 1e-6), 0.0, 1.0);
    float s = sign(e0.x * e2.y - e0.y * e2.x);
    vec2 d0 = vec2(dot(pq0, pq0), s * (v0.x * e0.y - v0.y * e0.x));
    vec2 d1 = vec2(dot(pq1, pq1), s * (v1.x * e1.y - v1.y * e1.x));
    vec2 d2 = vec2(dot(pq2, pq2), s * (v2.x * e2.y - v2.y * e2.x));
    vec2 d = min(min(d0, d1), d2);
    return -sqrt(d.x) * sign(d.y);
}
float unit_cov(float dist) {
    return clamp(0.5 - dist, 0.0, 1.0);
}
float ign_dither(vec2 pos_xy) {
    return fract(52.982918 * fract(dot(pos_xy, vec2(0.06711056, 0.00583715)))) - 0.5;
}
float gaussian_falloff(float dist, float sigma) {
    float s = max(sigma, 0.5);
    float x = dist / (s * 1.4142135);
    float ax = abs(x);
    float t = 1.0 / (1.0 + 0.3275911 * ax);
    float poly = t * (0.2548296 + t * (-0.28449672 + t * (1.4214137 + t * (-1.453152 + t * 1.0614054))));
    float erfc_abs = poly * exp(-ax * ax);
    return clamp((x >= 0.0) ? (0.5 * erfc_abs) : (1.0 - 0.5 * erfc_abs), 0.0, 1.0);
}
void main() {
    vec2 frag_top = vec2(gl_FragCoord.x, u_viewport.y - gl_FragCoord.y);
    if (frag_top.x < v_clip_rect.x || frag_top.y < v_clip_rect.y ||
        frag_top.x > v_clip_rect.z || frag_top.y > v_clip_rect.w) {
        discard;
    }
    uint kind = v_kind_flags & 0xFu;
    uint sub_kind = (v_kind_flags >> 4u) & 0x3u;
    float r_top = (v_local_pos.x < 0.0) ? v_radii.x : v_radii.y;
    float r_bot = (v_local_pos.x < 0.0) ? v_radii.w : v_radii.z;
    float r_quad = (v_local_pos.y < 0.0) ? r_top : r_bot;
    float r = min(r_quad, min(v_half_size.x, v_half_size.y));
    float cov_box = unit_cov(sdf_box(v_local_pos, v_half_size, r));
    if (kind == 1u) {
        vec2 dir = vec2(sin(v_params.y), cos(v_params.y));
        vec2 norm_p = v_local_pos / max(v_half_size, vec2(1.0));
        float t = clamp(dot(norm_p, dir) * 0.5 + 0.5, 0.0, 1.0);
        vec3 lin0 = v_color.rgb * v_color.rgb;
        vec3 lin1 = v_aux_color.rgb * v_aux_color.rgb;
        vec3 grad_rgb = sqrt(clamp(mix(lin0, lin1, t), 0.0, 1.0));
        float grad_a = mix(v_color.a, v_aux_color.a, t) * cov_box;
        frag_color = vec4(grad_rgb, grad_a);
        return;
    }
    if (kind == 2u) {
        vec2 caster_half = max(v_half_size - vec2(v_params.y), vec2(0.5));
        float sdist = sdf_box(v_local_pos, caster_half, r_quad);
        float sa = v_color.a * gaussian_falloff(sdist, v_params.x);
        float d = ign_dither(gl_FragCoord.xy) * (1.0 / 255.0);
        frag_color = vec4(clamp(v_color.rgb + vec3(d), 0.0, 1.0), clamp(sa + d * 0.5, 0.0, 1.0));
        return;
    }
    if (kind == 3u) {
        float glyph_cov = texture(u_glyph_r8, v_uv).r;
        frag_color = vec4(v_color.rgb, clamp(v_color.a * glyph_cov, 0.0, 1.0));
        return;
    }
    if (kind == 4u) {
        vec4 emoji = texture(u_glyph_rgba8, v_uv);
        frag_color = vec4(emoji.rgb, emoji.a * v_color.a);
        return;
    }
    if (kind == 5u) {
        vec4 texel = texture(u_image_tex, v_uv);
        frag_color = vec4(texel.rgb * v_color.rgb, texel.a * v_color.a * cov_box);
        return;
    }
    if (kind == 6u) {
        float shape_cov = 0.0;
        if (sub_kind == 0u) {
            float rad = min(v_half_size.x, v_half_size.y);
            shape_cov = unit_cov(length(v_local_pos) - rad);
        } else if (sub_kind == 1u) {
            vec2 a = (v_uv_raw.xy - 0.5) * (v_half_size * 2.0);
            vec2 b = (v_uv_raw.zw - 0.5) * (v_half_size * 2.0);
            shape_cov = unit_cov(sdf_segment(v_local_pos, a, b) - max(v_params.x * 0.5, 0.5));
        } else {
            vec2 p0 = (v_aux_color.xy - 0.5) * (v_half_size * 2.0);
            vec2 p1 = (v_uv_raw.xy - 0.5) * (v_half_size * 2.0);
            vec2 p2 = (v_uv_raw.zw - 0.5) * (v_half_size * 2.0);
            shape_cov = unit_cov(sdf_triangle(v_local_pos, p0, p1, p2));
        }
        frag_color = vec4(v_color.rgb, v_color.a * shape_cov);
        return;
    }
    if (kind == 7u) {
        vec4 ext_texel = texture(u_external_tex, v_uv);
        frag_color = vec4(ext_texel.rgb * v_color.rgb, ext_texel.a * v_color.a * cov_box);
        return;
    }
    float outer_cov = cov_box;
    float bw = v_params.x;
    vec2 inner_half = max(v_half_size - vec2(bw), vec2(0.0));
    float inner_r = max(r - bw, 0.0);
    float inner_cov = outer_cov;
    if (bw > 0.0) {
        inner_cov = (inner_half.x > 0.0 && inner_half.y > 0.0)
            ? unit_cov(sdf_box(v_local_pos, inner_half, inner_r))
            : 0.0;
    }
    float border_cov = clamp(outer_cov - inner_cov, 0.0, 1.0);
    float fill_a = v_color.a * inner_cov;
    float border_a = v_aux_color.a * border_cov;
    float total_a = clamp(fill_a + border_a, 0.0, 1.0);
    if (total_a <= 0.0001) {
        frag_color = vec4(0.0);
        return;
    }
    vec3 lin_rgb = (v_color.rgb * v_color.rgb * fill_a + v_aux_color.rgb * v_aux_color.rgb * border_a) / total_a;
    vec3 rgb = sqrt(clamp(lin_rgb, 0.0, 1.0));
    frag_color = vec4(rgb, total_a);
}
"#;

/// OpenGL ES 3.0 / WebGL 2 Dual-Kawase / separable Gaussian `BackdropBlur` fragment shader.
pub const GLSL_ES3_BACKDROP_BLUR_FRAGMENT_SHADER: &str = r#"#version 300 es
precision highp float;
uniform sampler2D u_blur_tex;
uniform vec2 u_texel_size;
uniform float u_blur_offset;
uniform int u_blur_pass_mode;
in vec2 v_uv;
out vec4 frag_color;
void main() {
    vec2 hp = u_texel_size * max(u_blur_offset, 0.5);
    if (u_blur_pass_mode == 0) {
        vec4 sum = texture(u_blur_tex, v_uv) * 4.0;
        sum += texture(u_blur_tex, v_uv - hp);
        sum += texture(u_blur_tex, v_uv + hp);
        sum += texture(u_blur_tex, v_uv + vec2(hp.x, -hp.y));
        sum += texture(u_blur_tex, v_uv - vec2(hp.x, -hp.y));
        frag_color = sum * 0.125;
        return;
    }
    vec4 sum = texture(u_blur_tex, v_uv + vec2(-hp.x * 2.0, 0.0));
    sum += texture(u_blur_tex, v_uv + vec2(-hp.x, hp.y)) * 2.0;
    sum += texture(u_blur_tex, v_uv + vec2(0.0, hp.y * 2.0));
    sum += texture(u_blur_tex, v_uv + vec2(hp.x, hp.y)) * 2.0;
    sum += texture(u_blur_tex, v_uv + vec2(hp.x * 2.0, 0.0));
    sum += texture(u_blur_tex, v_uv + vec2(hp.x, -hp.y)) * 2.0;
    sum += texture(u_blur_tex, v_uv + vec2(0.0, -hp.y * 2.0));
    sum += texture(u_blur_tex, v_uv + vec2(-hp.x, -hp.y)) * 2.0;
    frag_color = sum * (1.0 / 12.0);
}
"#;

/// Direct3D 11/12 HLSL vertex (`VSMain`) and pixel (`PSMain`) 64-byte `GpuPrimitiveInstance` uber-shader covering all 8 `PrimKind` variants.
pub const HLSL_UI_SHADER: &str = r#"
cbuffer LayerUniforms : register(b0) {
    float2 viewport_size;
    float2 scroll_offset;
};
struct GpuPrimitiveInstance {
    float4 rect;
    float4 clip_rect;
    uint color_rgba;
    uint aux_color_rgba;
    uint2 uv_rect;
    uint2 radii_border;
    uint kind_flags;
    uint extra_param;
};
StructuredBuffer<GpuPrimitiveInstance> Instances : register(t0);
Texture2D<float> GlyphAtlasR8 : register(t1);
Texture2D<float4> GlyphAtlasRgba8 : register(t2);
Texture2D<float4> ImageTex : register(t3);
Texture2D<float4> ExternalTex : register(t4);
SamplerState LinearSampler : register(s0);
struct PSInput {
    float4 pos : SV_POSITION;
    float2 local_pos : TEXCOORD0;
    float2 half_size : TEXCOORD1;
    float2 uv : TEXCOORD2;
    float4 clip_rect : TEXCOORD3;
    float4 color : COLOR0;
    float4 aux_color : COLOR1;
    float4 radii : TEXCOORD4;
    float2 params : TEXCOORD5;
    nointerpolation uint kind_flags : TEXCOORD6;
    nointerpolation float4 uv_raw : TEXCOORD7;
};
float4 UnpackRgba8(uint c) {
    return float4(
        float(c & 0xFFu),
        float((c >> 8u) & 0xFFu),
        float((c >> 16u) & 0xFFu),
        float((c >> 24u) & 0xFFu)
    ) * (1.0 / 255.0);
}
float4 UnpackU16x4(uint2 packed) {
    return float4(
        float(packed.x & 0xFFFFu),
        float((packed.x >> 16u) & 0xFFFFu),
        float(packed.y & 0xFFFFu),
        float((packed.y >> 16u) & 0xFFFFu)
    );
}
PSInput VSMain(uint vid : SV_VertexID, uint iid : SV_InstanceID) {
    static const float2 corners[6] = {
        float2(0.0, 0.0), float2(1.0, 0.0), float2(0.0, 1.0),
        float2(1.0, 0.0), float2(1.0, 1.0), float2(0.0, 1.0)
    };
    GpuPrimitiveInstance inst = Instances[iid];
    float2 corner = corners[vid % 6u];
    float2 vp = max(viewport_size, float2(1.0, 1.0));
    float2 px = (inst.rect.xy - scroll_offset) + corner * inst.rect.zw;
    float2 ndc = float2((px.x / vp.x) * 2.0 - 1.0, 1.0 - (px.y / vp.y) * 2.0);
    float4 uv_raw = UnpackU16x4(inst.uv_rect) * (1.0 / 65535.0);
    PSInput o;
    o.pos = float4(ndc, 0.0, 1.0);
    o.local_pos = (corner - 0.5) * inst.rect.zw;
    o.half_size = inst.rect.zw * 0.5;
    o.uv = lerp(uv_raw.xy, uv_raw.zw, corner);
    o.clip_rect = inst.clip_rect;
    o.color = UnpackRgba8(inst.color_rgba);
    o.aux_color = UnpackRgba8(inst.aux_color_rgba);
    o.radii = UnpackU16x4(inst.radii_border) * (1.0 / 16.0);
    o.params = float2(f16tof32(inst.extra_param & 0xFFFFu), f16tof32(inst.extra_param >> 16u));
    o.kind_flags = inst.kind_flags;
    o.uv_raw = uv_raw;
    return o;
}
float SdfBox(float2 p, float2 half_sz, float r) {
    float cr = min(r, min(half_sz.x, half_sz.y));
    float2 q = abs(p) - half_sz + float2(cr, cr);
    return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - cr;
}
float SdfSegment(float2 p, float2 a, float2 b) {
    float2 pa = p - a;
    float2 ba = b - a;
    float denom = max(dot(ba, ba), 1e-6);
    float h = saturate(dot(pa, ba) / denom);
    return length(pa - ba * h);
}
float SdfTriangle(float2 p, float2 p0, float2 p1, float2 p2) {
    float2 e0 = p1 - p0;
    float2 e1 = p2 - p1;
    float2 e2 = p0 - p2;
    float2 v0 = p - p0;
    float2 v1 = p - p1;
    float2 v2 = p - p2;
    float2 pq0 = v0 - e0 * saturate(dot(v0, e0) / max(dot(e0, e0), 1e-6));
    float2 pq1 = v1 - e1 * saturate(dot(v1, e1) / max(dot(e1, e1), 1e-6));
    float2 pq2 = v2 - e2 * saturate(dot(v2, e2) / max(dot(e2, e2), 1e-6));
    float s = sign(e0.x * e2.y - e0.y * e2.x);
    float2 d0 = float2(dot(pq0, pq0), s * (v0.x * e0.y - v0.y * e0.x));
    float2 d1 = float2(dot(pq1, pq1), s * (v1.x * e1.y - v1.y * e1.x));
    float2 d2 = float2(dot(pq2, pq2), s * (v2.x * e2.y - v2.y * e2.x));
    float2 d = min(min(d0, d1), d2);
    return -sqrt(d.x) * sign(d.y);
}
float UnitCov(float dist) {
    return saturate(0.5 - dist);
}
float IgnDither(float2 pos_xy) {
    return frac(52.982918 * frac(dot(pos_xy, float2(0.06711056, 0.00583715)))) - 0.5;
}
float GaussianFalloff(float dist, float sigma) {
    float s = max(sigma, 0.5);
    float x = dist / (s * 1.4142135);
    float ax = abs(x);
    float t = 1.0 / (1.0 + 0.3275911 * ax);
    float poly = t * (0.2548296 + t * (-0.28449672 + t * (1.4214137 + t * (-1.453152 + t * 1.0614054))));
    float erfc_abs = poly * exp(-ax * ax);
    return saturate((x >= 0.0) ? (0.5 * erfc_abs) : (1.0 - 0.5 * erfc_abs));
}
float4 PSMain(PSInput input) : SV_TARGET {
    if (input.pos.x < input.clip_rect.x || input.pos.y < input.clip_rect.y ||
        input.pos.x > input.clip_rect.z || input.pos.y > input.clip_rect.w) {
        discard;
    }
    uint kind = input.kind_flags & 0xFu;
    uint sub_kind = (input.kind_flags >> 4u) & 0x3u;
    float r_top = (input.local_pos.x < 0.0) ? input.radii.x : input.radii.y;
    float r_bot = (input.local_pos.x < 0.0) ? input.radii.w : input.radii.z;
    float r_quad = (input.local_pos.y < 0.0) ? r_top : r_bot;
    float r = min(r_quad, min(input.half_size.x, input.half_size.y));
    float cov_box = UnitCov(SdfBox(input.local_pos, input.half_size, r));
    if (kind == 1u) {
        float2 dir = float2(sin(input.params.y), cos(input.params.y));
        float2 norm_p = input.local_pos / max(input.half_size, float2(1.0, 1.0));
        float t = saturate(dot(norm_p, dir) * 0.5 + 0.5);
        float3 lin0 = input.color.rgb * input.color.rgb;
        float3 lin1 = input.aux_color.rgb * input.aux_color.rgb;
        float3 grad_rgb = sqrt(saturate(lerp(lin0, lin1, t)));
        float grad_a = lerp(input.color.a, input.aux_color.a, t) * cov_box;
        return float4(grad_rgb, grad_a);
    }
    if (kind == 2u) {
        float2 caster_half = max(input.half_size - float2(input.params.y, input.params.y), float2(0.5, 0.5));
        float sdist = SdfBox(input.local_pos, caster_half, r_quad);
        float sa = input.color.a * GaussianFalloff(sdist, input.params.x);
        float d = IgnDither(input.pos.xy) * (1.0 / 255.0);
        return float4(saturate(input.color.rgb + d), saturate(sa + d * 0.5));
    }
    if (kind == 3u) {
        float glyph_cov = GlyphAtlasR8.Sample(LinearSampler, input.uv);
        return float4(input.color.rgb, saturate(input.color.a * glyph_cov));
    }
    if (kind == 4u) {
        float4 emoji = GlyphAtlasRgba8.Sample(LinearSampler, input.uv);
        return float4(emoji.rgb, emoji.a * input.color.a);
    }
    if (kind == 5u) {
        float4 texel = ImageTex.Sample(LinearSampler, input.uv);
        return float4(texel.rgb * input.color.rgb, texel.a * input.color.a * cov_box);
    }
    if (kind == 6u) {
        float shape_cov = 0.0;
        if (sub_kind == 0u) {
            float rad = min(input.half_size.x, input.half_size.y);
            shape_cov = UnitCov(length(input.local_pos) - rad);
        } else if (sub_kind == 1u) {
            float2 a = (input.uv_raw.xy - 0.5) * (input.half_size * 2.0);
            float2 b = (input.uv_raw.zw - 0.5) * (input.half_size * 2.0);
            shape_cov = UnitCov(SdfSegment(input.local_pos, a, b) - max(input.params.x * 0.5, 0.5));
        } else {
            float2 p0 = (input.aux_color.xy - 0.5) * (input.half_size * 2.0);
            float2 p1 = (input.uv_raw.xy - 0.5) * (input.half_size * 2.0);
            float2 p2 = (input.uv_raw.zw - 0.5) * (input.half_size * 2.0);
            shape_cov = UnitCov(SdfTriangle(input.local_pos, p0, p1, p2));
        }
        return float4(input.color.rgb, input.color.a * shape_cov);
    }
    if (kind == 7u) {
        float4 ext_texel = ExternalTex.Sample(LinearSampler, input.uv);
        return float4(ext_texel.rgb * input.color.rgb, ext_texel.a * input.color.a * cov_box);
    }
    float outer_cov = cov_box;
    float bw = input.params.x;
    float2 inner_half = max(input.half_size - float2(bw, bw), float2(0.0, 0.0));
    float inner_r = max(r - bw, 0.0);
    float inner_cov = outer_cov;
    if (bw > 0.0) {
        inner_cov = (inner_half.x > 0.0 && inner_half.y > 0.0)
            ? UnitCov(SdfBox(input.local_pos, inner_half, inner_r))
            : 0.0;
    }
    float border_cov = saturate(outer_cov - inner_cov);
    float fill_a = input.color.a * inner_cov;
    float border_a = input.aux_color.a * border_cov;
    float total_a = saturate(fill_a + border_a);
    if (total_a <= 0.0001) {
        return float4(0.0, 0.0, 0.0, 0.0);
    }
    float3 lin_rgb = (input.color.rgb * input.color.rgb * fill_a + input.aux_color.rgb * input.aux_color.rgb * border_a) / total_a;
    float3 rgb = sqrt(saturate(lin_rgb));
    return float4(rgb, total_a);
}
"#;

/// Direct3D 11/12 HLSL Dual-Kawase / separable Gaussian `BackdropBlur` pass shader.
pub const HLSL_BACKDROP_BLUR_SHADER: &str = r#"
cbuffer BlurUniforms : register(b0) {
    float2 texel_size;
    float blur_offset;
    uint pass_mode;
};
Texture2D<float4> SrcTex : register(t0);
SamplerState LinearSampler : register(s0);
struct BlurPSInput {
    float4 pos : SV_POSITION;
    float2 uv : TEXCOORD0;
};
BlurPSInput VSBlur(uint vid : SV_VertexID) {
    static const float2 verts[3] = {
        float2(-1.0, -1.0),
        float2(3.0, -1.0),
        float2(-1.0, 3.0)
    };
    float2 p = verts[vid % 3u];
    BlurPSInput 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;
}
float4 PSBlurDownsample(BlurPSInput input) : SV_TARGET {
    float2 hp = texel_size * max(blur_offset, 0.5);
    float4 sum = SrcTex.Sample(LinearSampler, input.uv) * 4.0;
    sum += SrcTex.Sample(LinearSampler, input.uv - hp);
    sum += SrcTex.Sample(LinearSampler, input.uv + hp);
    sum += SrcTex.Sample(LinearSampler, input.uv + float2(hp.x, -hp.y));
    sum += SrcTex.Sample(LinearSampler, input.uv - float2(hp.x, -hp.y));
    return sum * 0.125;
}
float4 PSBlurUpsample(BlurPSInput input) : SV_TARGET {
    float2 hp = texel_size * max(blur_offset, 0.5);
    float4 sum = SrcTex.Sample(LinearSampler, input.uv + float2(-hp.x * 2.0, 0.0));
    sum += SrcTex.Sample(LinearSampler, input.uv + float2(-hp.x, hp.y)) * 2.0;
    sum += SrcTex.Sample(LinearSampler, input.uv + float2(0.0, hp.y * 2.0));
    sum += SrcTex.Sample(LinearSampler, input.uv + float2(hp.x, hp.y)) * 2.0;
    sum += SrcTex.Sample(LinearSampler, input.uv + float2(hp.x * 2.0, 0.0));
    sum += SrcTex.Sample(LinearSampler, input.uv + float2(hp.x, -hp.y)) * 2.0;
    sum += SrcTex.Sample(LinearSampler, input.uv + float2(0.0, -hp.y * 2.0));
    sum += SrcTex.Sample(LinearSampler, input.uv + float2(-hp.x, -hp.y)) * 2.0;
    return sum * (1.0 / 12.0);
}
"#;

/// Maximum instanced quads packed into a single GPU vertex-instance draw batch.
pub const MAX_QUADS_PER_BATCH: usize = 4096;

/// Exact packed byte stride of one legacy `#[repr(C)]` `GpuQuadInstance` (`5 * 16 + 4 * 4 = 96` bytes).
pub const GPU_QUAD_INSTANCE_BYTES: usize = 96;

/// Exact packed byte stride of one unified `#[repr(C)]` `GpuPrimitiveInstance` (`64` bytes).
pub const GPU_PRIMITIVE_INSTANCE_BYTES: usize = 64;

/// Exact packed byte stride of one `#[repr(C)]` `GpuLayerUniform` (`16` bytes).
pub const GPU_LAYER_UNIFORM_BYTES: usize = 16;