proof-engine 0.2.1

Real-time graphics from math: glyphs and particles moved by ODEs, strange attractors and force fields, drawn with HDR bloom on OpenGL.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
//! Render pipeline — glutin 0.32 / winit 0.30 window + OpenGL 3.3 Core context,
//! instanced glyph batch rendering, and the full multi-pass post-processing pipeline
//! (bloom, chromatic aberration, film grain, vignette, scanlines) wired through
//! `PostFxPipeline` so that `RenderConfig` actually controls runtime behaviour.
//!
//! # Post-processing flow
//!
//! ```text
//! GlyphPass (to scene FBO, dual attachments)
//!   └─ color    ──┐
//!   └─ emission ──┤
//!                 ├─ PostFxPipeline::run(RenderConfig)
//!                 │   ├─ Bloom H-blur
//!                 │   ├─ Bloom V-blur   (×2 for softness)
//!                 │   └─ Composite: scene + bloom + CA + grain + vignette → screen
//!                 └─► Default framebuffer
//! ```

use std::num::NonZeroU32;
use std::ffi::CString;
use std::time::{Duration, Instant};

use glutin::config::ConfigTemplateBuilder;
use glutin::context::{ContextApi, ContextAttributesBuilder, NotCurrentGlContext,
                      PossiblyCurrentContext, Version};
use glutin::display::{GetGlDisplay, GlDisplay};
use glutin::surface::{GlSurface, Surface, WindowSurface};
use glutin_winit::{DisplayBuilder, GlWindow};
use glow::HasContext;
use raw_window_handle::HasWindowHandle;
use winit::dpi::LogicalSize;
use winit::event::{ElementState, Event, MouseButton, MouseScrollDelta, WindowEvent};
use winit::event_loop::EventLoop;
use winit::keyboard::{KeyCode, PhysicalKey};
use winit::platform::pump_events::{EventLoopExtPumpEvents, PumpStatus};
use winit::window::Window;
use glam::{Mat4, Vec2, Vec3};
use bytemuck::cast_slice;

use crate::config::{EngineConfig, RenderConfig};
use crate::scene::Scene;
use crate::render::camera::ProofCamera;
use crate::render::postfx::PostFxPipeline;
use crate::input::{InputState, Key};
use crate::glyph::atlas::FontAtlas;
use crate::glyph::batch::GlyphInstance;
use crate::render::ui_layer::UiLayer;
use crate::render::screen_fx::ScreenFx;
use crate::particle::gpu_density::{GpuDensityEntityData, GpuDensityRenderer};

// ── Glyph vertex shader ────────────────────────────────────────────────────────

const VERT_SRC: &str = r#"
#version 330 core

layout(location = 0) in vec2  v_pos;
layout(location = 1) in vec2  v_uv;

layout(location = 2)  in vec3  i_position;
layout(location = 3)  in vec2  i_scale;
layout(location = 4)  in float i_rotation;
layout(location = 5)  in vec4  i_color;
layout(location = 6)  in float i_emission;
layout(location = 7)  in vec3  i_glow_color;
layout(location = 8)  in float i_glow_radius;
layout(location = 9)  in vec2  i_uv_offset;
layout(location = 10) in vec2  i_uv_size;
// x: 1.0 for a fill (ground, panel) that must not cast shadow.
layout(location = 11) in vec2  i_flags;

uniform mat4 u_view_proj;
// Oversampling: each base instance is rendered u_n_copies times.
// vertex_attrib_divisor is set to u_n_copies so all copies share the same
// instance attributes. gl_InstanceID / u_n_copies selects the base particle;
// gl_InstanceID % u_n_copies selects the copy and drives the jitter hash.
uniform uint u_n_copies;

out vec2  f_uv;
out vec4  f_color;
out float f_emission;
out vec3  f_glow_color;
out float f_glow_radius;
out float f_fill;

// Hash matching the CPU hf(seed, v) function — same constants, same bit ops.
float hf(uint seed, uint v) {
    uint n = seed * 374761393u + v * 668265263u;
    n ^= (n >> 13u);
    n *= 0x5851F42Du;
    n ^= (n >> 16u);
    return float(n & 0x00FFFFFFu) / float(0x01000000u);
}

void main() {
    float c = cos(i_rotation);
    float s = sin(i_rotation);
    vec2 rotated = vec2(
        v_pos.x * c - v_pos.y * s,
        v_pos.x * s + v_pos.y * c
    ) * i_scale;

    // Per-copy position jitter — only active when oversampling.
    // Seeds mirror the CPU oversampling: seed = real_id*17 + copy_id, slots 30/31.
    vec3 jitter = vec3(0.0);
    if (u_n_copies > 1u) {
        uint real_id = uint(gl_InstanceID) / u_n_copies;
        uint copy_id = uint(gl_InstanceID) % u_n_copies;
        uint jseed   = real_id * 17u + copy_id;
        jitter = vec3(
            (hf(jseed, 30u) - 0.5) * i_scale.x * 0.70,
            (hf(jseed, 31u) - 0.5) * i_scale.y * 0.70,
            0.0
        );
    }

    gl_Position = u_view_proj * vec4(i_position + jitter + vec3(rotated, 0.0), 1.0);
    gl_Position.y = -gl_Position.y;  // FBO renders upside-down relative to screen

    f_uv         = i_uv_offset + v_uv * i_uv_size;
    // Divide alpha and emission by n_copies so n_copies additive contributions
    // sum to the same luminance as a single unscaled instance.
    float inv_n  = 1.0 / float(u_n_copies);
    f_color      = vec4(i_color.rgb, i_color.a * inv_n);
    f_emission   = i_emission * inv_n;
    f_glow_color = i_glow_color;
    f_glow_radius = i_glow_radius;
    f_fill        = i_flags.x;
}
"#;

/// Glyph fragment shader -- SDF rendering with outline, glow halo, and drop shadow.
///
/// The atlas stores Signed Distance Field values:
///   128/255 = edge, >128 = inside, <128 = outside.
///
/// `o_color`    -> COLOR_ATTACHMENT0 -- blended scene color
/// `o_emission` -> COLOR_ATTACHMENT1 -- bloom input
const FRAG_SRC: &str = r#"
#version 330 core

in vec2  f_uv;
in vec4  f_color;
in float f_emission;
in vec3  f_glow_color;
in float f_glow_radius;
in float f_fill;

uniform sampler2D u_atlas;

layout(location = 0) out vec4 o_color;
layout(location = 1) out vec4 o_emission;
// Coverage of matter, for the light map's shadows. Fills write none, and
// cover whatever was under them.
layout(location = 2) out vec4 o_occluder;

void main() {
    float dist = texture(u_atlas, f_uv).r;

    // SDF thresholds (in normalized distance, 0.5 = edge)
    float edge     = 0.5;
    float softness = 0.08; // edge softness (antialiasing)

    // Core glyph alpha (sharp edge with antialiasing)
    float alpha = smoothstep(edge - softness, edge + softness, dist);

    // Outline: a band just outside the edge
    float outline_width = 0.06;
    float outline_alpha = smoothstep(edge - outline_width - softness, edge - outline_width, dist)
                        * (1.0 - smoothstep(edge - softness * 0.5, edge + softness * 0.5, dist));
    vec3 outline_color = f_glow_color * 0.6;

    // Glow halo: soft falloff outside the glyph
    float glow_size  = 0.15 + f_glow_radius * 0.05;
    float glow_alpha = smoothstep(edge - glow_size - 0.1, edge - 0.02, dist) * (1.0 - alpha);
    glow_alpha *= clamp(f_glow_radius * 0.3, 0.0, 0.5);

    // Drop shadow (offset sample)
    vec2 shadow_offset = vec2(0.003, -0.004);
    float shadow_dist = texture(u_atlas, f_uv + shadow_offset).r;
    float shadow_alpha = smoothstep(edge - softness, edge + softness, shadow_dist) * 0.2;

    // Discard if nothing visible
    float total_alpha = max(max(alpha, outline_alpha), max(glow_alpha, shadow_alpha));
    if (total_alpha < 0.01) discard;

    // Composite layers
    float em = clamp(f_emission * 0.5, 0.0, 1.0);
    vec3 base_col = mix(f_color.rgb, f_glow_color, em);

    // Shadow (darkest layer)
    vec3 col = vec3(0.0);
    float a = shadow_alpha * f_color.a * 0.3;

    // Glow halo (behind outline)
    col = mix(col, f_glow_color * 0.5, glow_alpha);
    a = max(a, glow_alpha * f_color.a * 0.4);

    // Outline
    col = mix(col, outline_color, outline_alpha);
    a = max(a, outline_alpha * f_color.a * 0.7);

    // Core fill (on top)
    col = mix(col, base_col, alpha);
    a = max(a, alpha * f_color.a);

    o_color = vec4(col, a);

    // Emission for bloom
    float bloom_strength = clamp(f_emission - 0.3, 0.0, 1.0);
    float glow_boost = clamp(f_glow_radius * 0.15, 0.0, 0.8);
    float em_alpha = max(alpha, glow_alpha * 0.5) * f_color.a;
    o_emission = vec4(f_glow_color * (bloom_strength + glow_boost), em_alpha);
    o_occluder = vec4(alpha * f_color.a * (1.0 - f_fill), 0.0, 0.0, a);
}
"#;

// ── Unit quad geometry ─────────────────────────────────────────────────────────

/// Unit quad: 6 vertices (2 CCW triangles), each: [pos_x, pos_y, uv_x, uv_y]
#[rustfmt::skip]
const QUAD_VERTS: [f32; 24] = [
    -0.5,  0.5,  0.0, 1.0,
    -0.5, -0.5,  0.0, 0.0,
     0.5,  0.5,  1.0, 1.0,
    -0.5, -0.5,  0.0, 0.0,
     0.5, -0.5,  1.0, 0.0,
     0.5,  0.5,  1.0, 1.0,
];

// ── FrameStats ─────────────────────────────────────────────────────────────────

/// Per-frame rendering statistics.
#[derive(Clone, Debug, Default)]
pub struct FrameStats {
    /// Frames per second (rolling average over 60 frames).
    pub fps:              f32,
    /// Time of last frame in seconds.
    pub dt:               f32,
    /// Number of glyphs drawn this frame.
    pub glyph_count:      usize,
    /// Number of particles drawn this frame.
    pub particle_count:   usize,
    /// Number of draw calls this frame.
    pub draw_calls:       u32,
    /// Total frame number since engine start.
    pub frame_number:     u64,
}

/// Rolling FPS calculator over N frames.
struct FpsCounter {
    samples:   [f32; 60],
    head:      usize,
    filled:    bool,
}

impl FpsCounter {
    fn new() -> Self { Self { samples: [0.016; 60], head: 0, filled: false } }

    fn push(&mut self, dt: f32) {
        self.samples[self.head] = dt.max(f32::EPSILON);
        self.head = (self.head + 1) % 60;
        if self.head == 0 { self.filled = true; }
    }

    fn fps(&self) -> f32 {
        let count = if self.filled { 60 } else { self.head.max(1) };
        let avg_dt: f32 = self.samples[..count].iter().sum::<f32>() / count as f32;
        1.0 / avg_dt
    }
}

// ── Pipeline ───────────────────────────────────────────────────────────────────

/// The main render pipeline.
///
/// Created once by `ProofEngine::new()` and kept alive for the duration of the game.
/// Owns the window, OpenGL context, shader programs, font atlas, glyph VAO, and
/// the post-processing pipeline.
#[allow(dead_code)]
pub struct Pipeline {
    // ── Runtime info ──────────────────────────────────────────────────────────
    pub width:   u32,
    pub height:  u32,
    pub stats:   FrameStats,
    running:     bool,

    // ── Config snapshot (not a reference — the engine owns EngineConfig) ──────
    render_config: RenderConfig,

    // ── Windowing ────────────────────────────────────────────────────────────
    event_loop: EventLoop<()>,
    window:     Window,
    surface:    Surface<WindowSurface>,
    context:    PossiblyCurrentContext,

    // ── OpenGL glyph pass ─────────────────────────────────────────────────────
    gl:            glow::Context,
    program:       glow::Program,
    vao:           glow::VertexArray,
    quad_vbo:      glow::Buffer,
    instance_vbo:  glow::Buffer,
    atlas_tex:     glow::Texture,
    loc_view_proj: glow::UniformLocation,
    loc_n_copies:  Option<glow::UniformLocation>,

    // ── Post-processing pipeline (the real deal — reads RenderConfig) ─────────
    postfx: PostFxPipeline,

    // ── Font atlas ────────────────────────────────────────────────────────────
    atlas: FontAtlas,

    // ── Screen-space UI pass ──────────────────────────────────────────────────
    // Builds instances from a UiLayer's draw queue and paints them in pixel
    // coordinates after post-processing, so panels and text stay crisp.
    ui_renderer: super::ui_layer_renderer::UiLayerRenderer,
    /// Whether `render_frame` already built this frame's UI instances, so
    /// `render_ui` need not build them again.
    ui_prepared: bool,

    // ── GPU density entities ──────────────────────────────────────────────────
    /// Created the first time an entity is queued.
    density: Option<GpuDensityRenderer>,
    density_entities: Vec<GpuDensityEntityData>,
    density_budget: u32,

    // ── SVOGI Global Illumination ───────────────────────────────────────────
    pub svogi: crate::svogi::integration::CascadedSvogi,

    // ── Volumetric fog ──────────────────────────────────────────────────────
    pub fog: crate::volumetric_fog::VolumetricFogPipeline,

    // ── CPU-side glyph batch ──────────────────────────────────────────────────
    instances: Vec<GlyphInstance>,

    // ── Timing ────────────────────────────────────────────────────────────────
    fps_counter:  FpsCounter,
    frame_start:  Instant,
    scene_time:   f32,

    // ── Mouse state ───────────────────────────────────────────────────────────
    mouse_pos:      Vec2,
    mouse_pos_prev: Vec2,
    /// Normalized device coordinates (NDC) of the mouse cursor.
    mouse_ndc:      Vec2,

    // ── Raw events for external consumers (egui) ────────────────────────────
    /// Raw winit WindowEvents collected during poll_events, drained by consumers.
    pub raw_window_events: Vec<winit::event::WindowEvent>,
}

impl Pipeline {
    /// Initialize window, OpenGL 3.3 Core context, shader programs, font atlas, and PostFxPipeline.
    pub fn init(config: &EngineConfig) -> Self {
        // ── 1. winit EventLoop ────────────────────────────────────────────────
        let event_loop = EventLoop::new().expect("EventLoop::new");

        // ── 2. Window attributes (winit 0.30 API) ─────────────────────────────
        let window_attrs = Window::default_attributes()
            .with_title(&config.window_title)
            .with_inner_size(LogicalSize::new(config.window_width, config.window_height))
            .with_resizable(true)
            // PROOF_HIDDEN: capture frames without a window appearing or
            // taking focus from whatever the person at the machine is doing.
            .with_visible(!crate::capture::hidden_window())
            .with_active(!crate::capture::hidden_window());

        // ── 3. GL config via DisplayBuilder (glutin-winit 0.5) ────────────────
        let template = ConfigTemplateBuilder::new()
            .with_alpha_size(8)
            .with_depth_size(0);

        let display_builder = DisplayBuilder::new()
            .with_window_attributes(Some(window_attrs));

        let (window, gl_config) = display_builder
            .build(&event_loop, template, |mut configs| {
                configs.next().expect("no suitable GL config found")
            })
            .expect("DisplayBuilder::build failed");

        let window = window.expect("window was not created");
        let display = gl_config.display();

        // ── 4. OpenGL context — try 4.3 (compute shaders) then fall back to 3.3 ─
        let raw_handle = window.window_handle().unwrap().as_raw();
        let ctx_attrs_43 = ContextAttributesBuilder::new()
            .with_context_api(ContextApi::OpenGl(Some(Version::new(4, 3))))
            .build(Some(raw_handle));
        let ctx_attrs_33 = ContextAttributesBuilder::new()
            .with_context_api(ContextApi::OpenGl(Some(Version::new(3, 3))))
            .build(Some(raw_handle));

        let not_current = unsafe {
            display.create_context(&gl_config, &ctx_attrs_43)
                   .unwrap_or_else(|_| {
                       display.create_context(&gl_config, &ctx_attrs_33)
                              .expect("create_context failed (both GL 4.3 and 3.3)")
                   })
        };

        // ── 5. Window surface ─────────────────────────────────────────────────
        let size = window.inner_size();
        let w = size.width.max(1);
        let h = size.height.max(1);

        let surface_attrs = window
            .build_surface_attributes(Default::default())
            .expect("build_surface_attributes failed");

        let surface = unsafe {
            display.create_window_surface(&gl_config, &surface_attrs)
                   .expect("create_window_surface failed")
        };

        // ── 6. Make current ───────────────────────────────────────────────────
        let context = not_current.make_current(&surface)
                                 .expect("make_current failed");
        if config.render.vsync {
            use glutin::surface::SwapInterval;
            if let Err(e) = surface.set_swap_interval(
                &context,
                SwapInterval::Wait(NonZeroU32::new(1).unwrap()),
            ) {
                log::warn!("vsync unavailable: {e}");
            }
        }

        // ── 7. glow context from proc address ─────────────────────────────────
        let gl = unsafe {
            glow::Context::from_loader_function(|sym| {
                let sym_c = CString::new(sym).unwrap();
                display.get_proc_address(sym_c.as_c_str()) as *const _
            })
        };

        // ── 8. Compile glyph program ──────────────────────────────────────────
        let program = unsafe { compile_program(&gl, VERT_SRC, FRAG_SRC) };
        let loc_view_proj = unsafe {
            gl.get_uniform_location(program, "u_view_proj")
              .expect("uniform u_view_proj not found")
        };
        let loc_n_copies = unsafe {
            gl.get_uniform_location(program, "u_n_copies")
        };
        unsafe {
            gl.use_program(Some(program));
            if let Some(loc) = gl.get_uniform_location(program, "u_atlas") {
                gl.uniform_1_i32(Some(&loc), 0);
            }
            // Default: no oversampling
            if let Some(ref loc) = loc_n_copies {
                gl.uniform_1_u32(Some(loc), 1u32);
            }
        }

        // ── 9. Geometry: VAO + VBOs ───────────────────────────────────────────
        let (vao, quad_vbo, instance_vbo) = unsafe { setup_vao(&gl) };

        // ── 10. Font atlas ────────────────────────────────────────────────────
        let atlas     = FontAtlas::build(config.render.font_size as f32);
        let atlas_tex = unsafe { upload_atlas(&gl, &atlas) };

        // ── 11. PostFxPipeline — dual-attachment FBOs + bloom shaders ────────
        let postfx = unsafe { PostFxPipeline::new(&gl, w, h, config.render.render_scale) };

        // ── 12. Global GL state ───────────────────────────────────────────────
        unsafe {
            gl.enable(glow::BLEND);
            gl.blend_func(glow::SRC_ALPHA, glow::ONE_MINUS_SRC_ALPHA);
            gl.clear_color(0.02, 0.02, 0.05, 1.0);
            gl.viewport(0, 0, w as i32, h as i32);
        }

        log::info!(
            "Pipeline ready — {}×{} — font atlas {}×{} ({} chars) — PostFxPipeline wired",
            w, h, atlas.width, atlas.height, atlas.uvs.len()
        );

        Self {
            width: w, height: h,
            stats: FrameStats::default(),
            running: true,
            render_config: config.render.clone(),
            event_loop, window, surface, context,
            gl, program, vao, quad_vbo, instance_vbo, atlas_tex, loc_view_proj, loc_n_copies,
            postfx,
            atlas,
            ui_renderer: super::ui_layer_renderer::UiLayerRenderer::new(),
            ui_prepared: false,
            density: None,
            density_entities: Vec::new(),
            density_budget: 0,
            instances: Vec::with_capacity(8192),
            fps_counter: FpsCounter::new(),
            frame_start: Instant::now(),
            scene_time: 0.0,
            mouse_pos: Vec2::ZERO,
            mouse_pos_prev: Vec2::ZERO,
            mouse_ndc: Vec2::ZERO,
            raw_window_events: Vec::new(),
            svogi: crate::svogi::integration::CascadedSvogi::new(3, 64, 50.0),
            fog: crate::volumetric_fog::VolumetricFogPipeline::new(
                crate::volumetric_fog::FogPresets::combat()
            ),
        }
    }

    /// Hand over this frame's GPU density entities and the particle budget
    /// each is drawn with. They are drawn into the scene after the glyph
    /// pass, so they bloom and grade with everything else.
    pub fn set_density_entities(&mut self, entities: &[GpuDensityEntityData], budget: u32) {
        self.density_entities.clear();
        self.density_entities.extend_from_slice(entities);
        self.density_budget = budget;
        if !self.density_entities.is_empty() && self.density.is_none() {
            self.density = Some(unsafe { GpuDensityRenderer::new(&self.gl) });
        }
    }

    /// Update the render config used by the PostFx pipeline this frame.
    /// Call from `ProofEngine::run()` whenever the config changes.
    pub fn update_render_config(&mut self, config: &RenderConfig) {
        let old_scale = self.render_config.render_scale;
        self.render_config = config.clone();
        // A change of render scale is a change of target size.
        if (config.render_scale - old_scale).abs() > 1e-4 {
            unsafe { self.postfx.resize(&self.gl, self.width, self.height, config.render_scale); }
        }
    }

    /// Poll window events and update `InputState`. Returns false on quit.
    pub fn poll_events(&mut self, input: &mut InputState) -> bool {
        input.clear_frame();
        self.mouse_pos_prev = self.mouse_pos;

        let mut should_exit = false;
        let mut resize:     Option<(u32, u32)>  = None;
        let mut key_events: Vec<(KeyCode, bool)> = Vec::new();
        let mut mouse_moved:     Option<(f64, f64)> = None;
        let mut mouse_buttons:   Vec<(MouseButton, bool)> = Vec::new();
        let mut scroll_delta:    f32 = 0.0;

        self.raw_window_events.clear();

        #[allow(deprecated)]
        let status = self.event_loop.pump_events(Some(Duration::ZERO), |event, elwt| {
            match event {
                Event::WindowEvent { event: we, .. } => match we {
                    WindowEvent::CloseRequested => {
                        should_exit = true;
                        elwt.exit();
                    }
                    WindowEvent::Resized(s) => {
                        resize = Some((s.width, s.height));
                    }
                    WindowEvent::KeyboardInput { event: key_ev, .. } => {
                        if let PhysicalKey::Code(kc) = key_ev.physical_key {
                            let pressed = key_ev.state == ElementState::Pressed;
                            key_events.push((kc, pressed));
                        }
                    }
                    WindowEvent::CursorMoved { position, .. } => {
                        mouse_moved = Some((position.x, position.y));
                    }
                    WindowEvent::MouseInput { button, state, .. } => {
                        let pressed = state == ElementState::Pressed;
                        mouse_buttons.push((button, pressed));
                    }
                    WindowEvent::MouseWheel { delta, .. } => {
                        scroll_delta += match delta {
                            MouseScrollDelta::LineDelta(_, y) => y,
                            MouseScrollDelta::PixelDelta(d)   => d.y as f32 / 40.0,
                        };
                    }
                    _ => {}
                }
                _ => {}
            }
        });

        // ── Apply resize ───────────────────────────────────────────────────────
        if let Some((w, h)) = resize {
            if w > 0 && h > 0 {
                self.surface.resize(
                    &self.context,
                    NonZeroU32::new(w).unwrap(),
                    NonZeroU32::new(h).unwrap(),
                );
                unsafe { self.gl.viewport(0, 0, w as i32, h as i32); }
                self.width  = w;
                self.height = h;
                input.window_resized = Some((w, h));
                unsafe { self.postfx.resize(&self.gl, w, h, self.render_config.render_scale); }
            }
        }

        // ── Apply key events ───────────────────────────────────────────────────
        for (kc, pressed) in key_events {
            if let Some(key) = keycode_to_engine(kc) {
                if pressed {
                    input.keys_pressed.insert(key);
                    input.keys_just_pressed.insert(key);
                } else {
                    input.keys_pressed.remove(&key);
                    input.keys_just_released.insert(key);
                }
            }
        }

        // ── Apply mouse events ─────────────────────────────────────────────────
        if let Some((x, y)) = mouse_moved {
            self.mouse_pos = Vec2::new(x as f32, y as f32);
            input.mouse_x = x as f32;
            input.mouse_y = y as f32;
            // Compute NDC: x/y ∈ [0, width/height] → [-1, 1]
            let w = self.width.max(1) as f32;
            let h = self.height.max(1) as f32;
            self.mouse_ndc = Vec2::new(
                (x as f32 / w) * 2.0 - 1.0,
                1.0 - (y as f32 / h) * 2.0,
            );
            input.mouse_ndc = self.mouse_ndc;
            input.mouse_delta = self.mouse_pos - self.mouse_pos_prev;
        }

        for (button, pressed) in mouse_buttons {
            match button {
                MouseButton::Left   => {
                    if pressed { input.mouse_left_just_pressed  = true; }
                    else       { input.mouse_left_just_released = true; }
                    input.mouse_left = pressed;
                }
                MouseButton::Right  => {
                    if pressed { input.mouse_right_just_pressed  = true; }
                    else       { input.mouse_right_just_released = true; }
                    input.mouse_right = pressed;
                }
                MouseButton::Middle => {
                    if pressed { input.mouse_middle_just_pressed = true; }
                    input.mouse_middle = pressed;
                }
                _ => {}
            }
        }

        input.scroll_delta = scroll_delta;

        // ── Exit check ─────────────────────────────────────────────────────────
        if should_exit || matches!(status, PumpStatus::Exit(_)) {
            self.running = false;
        }
        self.running
    }

    /// Collect all visible glyphs + particles from the scene, upload to the GPU,
    /// and execute the full multi-pass rendering pipeline.
    pub fn render(&mut self, scene: &Scene, camera: &ProofCamera) {
        let fx = ScreenFx::default();
        self.render_frame(scene, camera, None, &fx);
    }

    /// Render the scene, the UI layer's world pass, and post-processing.
    ///
    /// With `ui` given, its world-pass commands are painted into the HDR
    /// scene buffer after the 3D glyphs and before post-processing, so they
    /// bloom, grade and shake with the scene; its HUD-pass commands are
    /// built here and painted by [`render_ui`](Self::render_ui) afterwards.
    /// `fx` supplies the frame's shockwaves, flash and light-shaft source.
    pub fn render_frame(
        &mut self,
        scene: &Scene,
        camera: &ProofCamera,
        ui: Option<&UiLayer>,
        fx: &ScreenFx,
    ) {
        // ── Frame timing ───────────────────────────────────────────────────────
        let now = Instant::now();
        let dt  = now.duration_since(self.frame_start).as_secs_f32().min(0.1);
        self.frame_start = now;
        self.scene_time  = scene.time;

        self.fps_counter.push(dt);
        self.stats.fps          = self.fps_counter.fps();
        self.stats.dt           = dt;
        self.stats.frame_number += 1;

        // ── Build camera matrices ──────────────────────────────────────────────
        let pos    = camera.position.position();
        let tgt    = camera.target.position();
        let fov    = camera.fov.position;
        let aspect = if self.height > 0 { self.width as f32 / self.height as f32 } else { 1.0 };
        let view      = Mat4::look_at_rh(pos, tgt, Vec3::Y);
        let proj      = Mat4::perspective_rh_gl(fov.to_radians(), aspect, camera.near, camera.far);
        let view_proj = proj * view;

        // ── Update SVOGI (voxelize scene, inject light, propagate) ───────────
        if self.render_config.global_illumination {
            use crate::svogi::inject::{LightSource, DirectionalLight, PointLight};
            let sun = LightSource::Directional(DirectionalLight {
                direction: Vec3::new(-0.5, 1.0, 0.8).normalize(),
                color: Vec3::new(0.8, 0.75, 0.65),
                intensity: 1.0,
            });
            let mut all_lights = vec![sun];
            // Inject emissive glyphs as point lights
            let mut emissive_count = 0;
            for (_, glyph) in scene.glyphs.iter() {
                if glyph.emission > 0.5 && emissive_count < 50 {
                    all_lights.push(LightSource::Point(PointLight {
                        position: glyph.position,
                        color: glyph.glow_color,
                        intensity: glyph.emission * 0.3,
                        radius: glyph.glow_radius * 2.0,
                    }));
                    emissive_count += 1;
                }
            }
            self.svogi.update(dt, &[], &all_lights, &[]);
        }

        // ── Update volumetric fog ─────────────────────────────────────────────
        if self.render_config.volumetric_fog {
            use crate::volumetric_fog::FogLight;
            let inv_vp = view_proj.inverse();
            let fog_lights = vec![
                FogLight::Directional {
                    direction: Vec3::new(-0.3, -0.8, -0.5).normalize(),
                    color: Vec3::new(0.6, 0.55, 0.5),
                    intensity: 0.5,
                },
            ];
            self.fog.update(dt, &inv_vp, pos, &fog_lights, &[]);
        }

        // ── Build glyph batch ──────────────────────────────────────────────────
        self.instances.clear();
        let mut glyph_count    = 0;
        let mut particle_count = 0;

        // Glyphs sorted by render layer (entity < particle < UI)
        for (_, glyph) in scene.glyphs.iter() {
            if !glyph.visible { continue; }
            let life_scale = if let Some(ref f) = glyph.life_function {
                f.evaluate(scene.time, 0.0)
            } else {
                1.0
            };
            let uv = self.atlas.uv_for(glyph.character);

            // Apply distance fog: glyphs further from camera fade toward fog color
            let dist = (glyph.position - pos).length();
            let fog_density = 0.003; // very subtle
            let fog_factor = (-dist * fog_density).exp(); // 1.0 = no fog, 0.0 = fully fogged
            let fog_color = [0.03f32, 0.04, 0.06]; // dark blue-ish fog
            let mut color = glyph.color.to_array();
            color[0] = color[0] * fog_factor + fog_color[0] * (1.0 - fog_factor);
            color[1] = color[1] * fog_factor + fog_color[1] * (1.0 - fog_factor);
            color[2] = color[2] * fog_factor + fog_color[2] * (1.0 - fog_factor);
            color[3] *= fog_factor.max(0.1); // alpha also fades but not to zero
            let emission_fogged = glyph.emission * fog_factor;

            self.instances.push(GlyphInstance {
                position:    glyph.position.to_array(),
                scale:       [glyph.scale.x * life_scale, glyph.scale.y * life_scale],
                rotation:    glyph.rotation,
                color,
                emission:    emission_fogged,
                glow_color:  glyph.glow_color.to_array(),
                glow_radius: glyph.glow_radius * fog_factor,
                uv_offset:   uv.offset(),
                uv_size:     uv.size(),
                _pad:        [0.0; 2],
            });
            glyph_count += 1;
        }

        for particle in scene.particles.iter() {
            let g = &particle.glyph;
            if !g.visible { continue; }
            let uv = self.atlas.uv_for(g.character);
            // Apply fog to particles too
            let dist = (g.position - pos).length();
            let fog_factor = (-dist * 0.003).exp();
            let fog_color = [0.03f32, 0.04, 0.06];
            let mut color = g.color.to_array();
            color[0] = color[0] * fog_factor + fog_color[0] * (1.0 - fog_factor);
            color[1] = color[1] * fog_factor + fog_color[1] * (1.0 - fog_factor);
            color[2] = color[2] * fog_factor + fog_color[2] * (1.0 - fog_factor);
            color[3] *= fog_factor.max(0.1);

            self.instances.push(GlyphInstance {
                position:    g.position.to_array(),
                scale:       [g.scale.x, g.scale.y],
                rotation:    g.rotation,
                color,
                emission:    g.emission * fog_factor,
                glow_color:  g.glow_color.to_array(),
                glow_radius: g.glow_radius * fog_factor,
                uv_offset:   uv.offset(),
                uv_size:     uv.size(),
                _pad:        [0.0; 2],
            });
            particle_count += 1;
        }

        self.stats.glyph_count    = glyph_count;
        self.stats.particle_count = particle_count;
        self.stats.draw_calls     = 0;

        // ── World-pass UI ──────────────────────────────────────────────────────
        //
        // Built once here for both passes. The world pass is projected in
        // screen pixels like the HUD, then shifted by the camera's trauma so
        // a blow moves the world and not the interface.
        let mut world_proj = None;
        if let Some(ui) = ui {
            self.ui_renderer.build_instances(ui, &self.atlas);
            self.ui_prepared = true;
            if self.render_config.world_ui_in_scene && self.ui_renderer.world_count() > 0 {
                let trauma = camera.shake.trauma.clamp(0.0, 1.0);
                let amp = trauma * trauma * self.render_config.shake_pixels;
                let t = self.scene_time;
                let shake = Vec3::new((t * 47.3).sin() * amp, (t * 31.7).cos() * amp, 0.0);
                world_proj = Some(ui.world_projection() * Mat4::from_translation(shake));
            }
        }

        // ── Execute render passes ──────────────────────────────────────────────
        unsafe { self.execute_render_passes(view_proj, world_proj, fx); }
    }

    /// Paint a screen-space UI layer on top of the finished frame.
    ///
    /// Runs after post-processing and writes straight to the default
    /// framebuffer, so panels and text are not smeared by bloom, chromatic
    /// aberration or grain — a HUD has to stay readable.
    ///
    /// Call once per frame, after `render`, before `swap`.
    pub fn render_ui(&mut self, ui: &super::ui_layer::UiLayer) {
        let prepared = std::mem::replace(&mut self.ui_prepared, false);
        if ui.command_count() == 0 {
            return;
        }
        if !prepared {
            self.ui_renderer.build_instances(ui, &self.atlas);
        }
        let proj = ui.projection();
        // With the world pass switched off, its commands paint here instead,
        // under the HUD, which is how the layer behaved before it existed.
        if !self.render_config.world_ui_in_scene && self.ui_renderer.world_count() > 0 {
            unsafe { self.draw_ui_pass(proj, true) };
            self.stats.draw_calls += 1;
        }
        if self.ui_renderer.glyph_count() == 0 {
            return;
        }
        unsafe { self.draw_ui_pass(proj, false) };
        self.stats.draw_calls += 1;
    }

    /// Upload and draw one of the UI instance buffers, straight to the
    /// screen, with an orthographic projection. `world` picks the world-pass
    /// buffer; otherwise the HUD buffer.
    unsafe fn draw_ui_pass(&mut self, proj: Mat4, world: bool) {
        let gl = &self.gl;
        let (bytes, count) = if world {
            (self.ui_renderer.world_bytes(), self.ui_renderer.world_count())
        } else {
            (self.ui_renderer.glyph_bytes(), self.ui_renderer.glyph_count())
        };

        // Straight to the screen: post-processing has already composited.
        gl.bind_framebuffer(glow::FRAMEBUFFER, None);
        gl.viewport(0, 0, self.width as i32, self.height as i32);

        // UI is 2D and ordered by draw call, so depth testing would only cause
        // z-fighting between overlapping panels.
        gl.disable(glow::DEPTH_TEST);
        gl.enable(glow::BLEND);
        gl.blend_func(glow::SRC_ALPHA, glow::ONE_MINUS_SRC_ALPHA);

        gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.instance_vbo));
        gl.buffer_data_u8_slice(glow::ARRAY_BUFFER, bytes, glow::DYNAMIC_DRAW);

        gl.use_program(Some(self.program));
        gl.uniform_matrix_4_f32_slice(Some(&self.loc_view_proj), false, &proj.to_cols_array());
        // The oversampling trick used by the 3D pass would smear UI text, so
        // the UI always draws exactly one copy per instance.
        gl.uniform_1_u32(self.loc_n_copies.as_ref(), 1);
        gl.active_texture(glow::TEXTURE0);
        gl.bind_texture(glow::TEXTURE_2D, Some(self.atlas_tex));
        gl.bind_vertex_array(Some(self.vao));
        for loc in 2u32..=11 {
            gl.vertex_attrib_divisor(loc, 1);
        }
        gl.draw_arrays_instanced(glow::TRIANGLES, 0, 6, count as i32);

        // Restore state the 3D pass expects on the next frame.
        gl.enable(glow::DEPTH_TEST);
    }

    /// Swap back buffer to screen. Returns false on window close.
    pub fn swap(&mut self) -> bool {
        if let Err(e) = self.surface.swap_buffers(&self.context) {
            log::error!("swap_buffers failed: {e}");
            self.running = false;
        }
        self.running
    }

    // ── Public accessors for editor/egui integration ──────────────────────────

    /// Get a reference to the raw glow OpenGL context.
    /// Used by egui-glow to render UI on top of the scene.
    pub fn gl(&self) -> &glow::Context {
        &self.gl
    }

    /// Get the window reference (for egui-winit event processing).
    pub fn window(&self) -> &Window {
        &self.window
    }

    /// The framebuffer size the viewport is actually set to.
    ///
    /// This is what screen-space passes must project against. On a scaled
    /// display it can differ from the window's logical size, and projecting
    /// against the wrong one magnifies the whole UI.
    pub fn render_size(&self) -> (u32, u32) {
        (self.width, self.height)
    }

    /// Read the frame that is currently on screen back off the GPU.
    ///
    /// Returns `(width, height, rgba)` with the bottom row first, which is how
    /// OpenGL stores it. Call this after everything for the frame has been
    /// drawn and before the buffers are swapped, or the read comes back empty.
    pub fn read_frame(&self) -> (u32, u32, Vec<u8>) {
        let (w, h) = (self.width, self.height);
        let mut buf = vec![0u8; (w as usize) * (h as usize) * 4];
        unsafe {
            let gl = &self.gl;
            gl.bind_framebuffer(glow::FRAMEBUFFER, None);
            gl.read_buffer(glow::BACK);
            gl.pixel_store_i32(glow::PACK_ALIGNMENT, 1);
            gl.read_pixels(
                0,
                0,
                w as i32,
                h as i32,
                glow::RGBA,
                glow::UNSIGNED_BYTE,
                glow::PixelPackData::Slice(Some(&mut buf)),
            );
        }
        (w, h, buf)
    }

    /// Get the current window size.
    pub fn window_size(&self) -> (u32, u32) {
        let size = self.window.inner_size();
        (size.width, size.height)
    }

    // ── Private render pass execution ─────────────────────────────────────────

    unsafe fn execute_render_passes(
        &mut self,
        view_proj: Mat4,
        world_proj: Option<Mat4>,
        fx: &ScreenFx,
    ) {
        let gl = &self.gl;

        // ── Pass 0: clear the HDR scene targets ────────────────────────────────
        //
        // At render scale, which may differ from the window. A dark
        // blue-black ground rather than pure black, so the vignette and the
        // dither have something to work against.
        let (sw, sh) = self.postfx.scene_size();
        gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.postfx.scene_fbo));
        gl.viewport(0, 0, sw as i32, sh as i32);
        gl.clear_color(0.02, 0.025, 0.04, 1.0);
        gl.clear(glow::COLOR_BUFFER_BIT);
        // The occluder buffer starts empty rather than at the ground colour.
        gl.clear_buffer_f32_slice(glow::COLOR, 2, &[0.0, 0.0, 0.0, 0.0]);
        gl.enable(glow::BLEND);
        gl.blend_func(glow::SRC_ALPHA, glow::ONE_MINUS_SRC_ALPHA);

        // ── Pass 1: Render glyphs ────────────────────────────────────────────

        if !self.instances.is_empty() {
            // Upload instance data
            gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.instance_vbo));
            gl.buffer_data_u8_slice(
                glow::ARRAY_BUFFER,
                cast_slice(self.instances.as_slice()),
                glow::DYNAMIC_DRAW,
            );

            // Oversampling: each base instance is rendered n_copies times.
            // vertex_attrib_divisor = n_copies → all n_copies share the same
            // attribute data. The vertex shader uses gl_InstanceID to derive
            // the copy index and compute per-copy position jitter.
            let n_copies = self.render_config.particle_multiplier
                .ceil().max(1.0) as u32;

            gl.use_program(Some(self.program));
            gl.uniform_matrix_4_f32_slice(
                Some(&self.loc_view_proj),
                false,
                &view_proj.to_cols_array(),
            );
            gl.uniform_1_u32(self.loc_n_copies.as_ref(), n_copies);
            gl.active_texture(glow::TEXTURE0);
            gl.bind_texture(glow::TEXTURE_2D, Some(self.atlas_tex));
            gl.bind_vertex_array(Some(self.vao));
            // Set attribute divisor to n_copies so each base instance repeats
            // n_copies times before advancing to the next instance in the VBO.
            for loc in 2u32..=11 {
                gl.vertex_attrib_divisor(loc, n_copies);
            }
            gl.draw_arrays_instanced(
                glow::TRIANGLES, 0, 6,
                (self.instances.len() as u32 * n_copies) as i32,
            );
            self.stats.draw_calls += 1;
        }

        // ── Pass 1a: GPU density entities ──────────────────────────────────────
        //
        // Millions of particles derived on the GPU from a few bones. Into
        // the same HDR targets, before the world pass so screen-space matter
        // can stand in front of them.
        if let Some(ref mut density) = self.density {
            if !self.density_entities.is_empty() {
                let draws = density.draw(
                    gl,
                    &self.density_entities,
                    self.density_budget,
                    &view_proj,
                    (sw, sh),
                    self.scene_time,
                );
                self.stats.draw_calls += draws;
                self.stats.particle_count += density.drawn as usize;
            }
        }

        // ── Pass 1b: the UI layer's world pass ─────────────────────────────────
        //
        // Same program, same instance layout, projected in screen pixels
        // rather than through the camera, into the same HDR targets. From
        // here on the post-processing cannot tell it from the 3D scene.
        if let Some(wp) = world_proj {
            let count = self.ui_renderer.world_count();
            if count > 0 {
                gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.instance_vbo));
                gl.buffer_data_u8_slice(
                    glow::ARRAY_BUFFER,
                    self.ui_renderer.world_bytes(),
                    glow::DYNAMIC_DRAW,
                );
                gl.use_program(Some(self.program));
                gl.uniform_matrix_4_f32_slice(
                    Some(&self.loc_view_proj),
                    false,
                    &wp.to_cols_array(),
                );
                // One copy per instance: the oversampling jitter is sized for
                // the 3D scene and would smear pixel-placed matter.
                gl.uniform_1_u32(self.loc_n_copies.as_ref(), 1);
                gl.active_texture(glow::TEXTURE0);
                gl.bind_texture(glow::TEXTURE_2D, Some(self.atlas_tex));
                gl.bind_vertex_array(Some(self.vao));
                for loc in 2u32..=11 {
                    gl.vertex_attrib_divisor(loc, 1);
                }
                gl.draw_arrays_instanced(glow::TRIANGLES, 0, 6, count as i32);
                self.stats.draw_calls += 1;
            }
        }

        // ── Passes 2+: bloom, composite, anti-aliasing ─────────────────────────
        let draws = self.postfx.run(
            gl, &self.render_config, fx, self.width, self.height, self.scene_time,
        );
        self.stats.draw_calls += draws;
    }
}

// ── GL helper functions ────────────────────────────────────────────────────────

/// Compile a vertex + fragment shader pair into a linked GL program.
unsafe fn compile_program(gl: &glow::Context, vert_src: &str, frag_src: &str) -> glow::Program {
    let vs = gl.create_shader(glow::VERTEX_SHADER).expect("create vertex shader");
    gl.shader_source(vs, vert_src);
    gl.compile_shader(vs);
    if !gl.get_shader_compile_status(vs) {
        let log = gl.get_shader_info_log(vs);
        panic!("Vertex shader compile error:\n{log}");
    }

    let fs = gl.create_shader(glow::FRAGMENT_SHADER).expect("create fragment shader");
    gl.shader_source(fs, frag_src);
    gl.compile_shader(fs);
    if !gl.get_shader_compile_status(fs) {
        let log = gl.get_shader_info_log(fs);
        panic!("Fragment shader compile error:\n{log}");
    }

    let prog = gl.create_program().expect("create shader program");
    gl.attach_shader(prog, vs);
    gl.attach_shader(prog, fs);
    gl.link_program(prog);
    if !gl.get_program_link_status(prog) {
        let log = gl.get_program_info_log(prog);
        panic!("Shader link error:\n{log}");
    }

    gl.detach_shader(prog, vs);
    gl.detach_shader(prog, fs);
    gl.delete_shader(vs);
    gl.delete_shader(fs);
    prog
}

/// Create VAO with per-vertex quad data (locations 0–1) and per-instance data (locations 2–10).
unsafe fn setup_vao(gl: &glow::Context) -> (glow::VertexArray, glow::Buffer, glow::Buffer) {
    let vao = gl.create_vertex_array().expect("create vao");
    gl.bind_vertex_array(Some(vao));

    // ── Quad geometry VBO ─────────────────────────────────────────────────────
    let quad_vbo = gl.create_buffer().expect("create quad_vbo");
    gl.bind_buffer(glow::ARRAY_BUFFER, Some(quad_vbo));
    gl.buffer_data_u8_slice(glow::ARRAY_BUFFER, cast_slice(&QUAD_VERTS), glow::STATIC_DRAW);
    // location 0: vec2 v_pos  (offset 0, stride 16)
    gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, 16, 0);
    gl.enable_vertex_attrib_array(0);
    // location 1: vec2 v_uv   (offset 8, stride 16)
    gl.vertex_attrib_pointer_f32(1, 2, glow::FLOAT, false, 16, 8);
    gl.enable_vertex_attrib_array(1);

    // ── Instance VBO (per-glyph data) ─────────────────────────────────────────
    let instance_vbo = gl.create_buffer().expect("create instance_vbo");
    gl.bind_buffer(glow::ARRAY_BUFFER, Some(instance_vbo));

    let stride = std::mem::size_of::<GlyphInstance>() as i32;

    // Macro: set up an instanced float attribute.
    macro_rules! inst_attr {
        ($loc:expr, $count:expr, $off:expr) => {{
            gl.vertex_attrib_pointer_f32($loc, $count, glow::FLOAT, false, stride, $off);
            gl.enable_vertex_attrib_array($loc);
            gl.vertex_attrib_divisor($loc, 1); // advance once per instance
        }};
    }

    inst_attr!(2,  3,  0);  // i_position   vec3   @ byte 0
    inst_attr!(3,  2, 12);  // i_scale      vec2   @ byte 12
    inst_attr!(4,  1, 20);  // i_rotation   float  @ byte 20
    inst_attr!(5,  4, 24);  // i_color      vec4   @ byte 24
    inst_attr!(6,  1, 40);  // i_emission   float  @ byte 40
    inst_attr!(7,  3, 44);  // i_glow_color vec3   @ byte 44
    inst_attr!(8,  1, 56);  // i_glow_radius float @ byte 56
    inst_attr!(9,  2, 60);  // i_uv_offset  vec2   @ byte 60
    inst_attr!(10, 2, 68);  // i_uv_size    vec2   @ byte 68
    inst_attr!(11, 2, 76);  // i_flags      vec2   @ byte 76 (the former padding)

    (vao, quad_vbo, instance_vbo)
}

/// Upload a FontAtlas as an R8 GL texture and return the handle.
unsafe fn upload_atlas(gl: &glow::Context, atlas: &FontAtlas) -> glow::Texture {
    let tex = gl.create_texture().expect("create atlas texture");
    gl.bind_texture(glow::TEXTURE_2D, Some(tex));
    gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1);
    gl.tex_image_2d(
        glow::TEXTURE_2D, 0, glow::R8 as i32,
        atlas.width as i32, atlas.height as i32,
        0, glow::RED, glow::UNSIGNED_BYTE,
        glow::PixelUnpackData::Slice(Some(&atlas.pixels)),
    );
    gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, glow::LINEAR as i32);
    gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::LINEAR as i32);
    gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE as i32);
    gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE as i32);
    tex
}

// ── KeyCode → engine Key mapping ──────────────────────────────────────────────

/// Map a winit `KeyCode` to the engine's `Key` enum. Returns `None` for unknown keys.
fn keycode_to_engine(kc: KeyCode) -> Option<Key> {
    Some(match kc {
        KeyCode::KeyA => Key::A, KeyCode::KeyB => Key::B, KeyCode::KeyC => Key::C,
        KeyCode::KeyD => Key::D, KeyCode::KeyE => Key::E, KeyCode::KeyF => Key::F,
        KeyCode::KeyG => Key::G, KeyCode::KeyH => Key::H, KeyCode::KeyI => Key::I,
        KeyCode::KeyJ => Key::J, KeyCode::KeyK => Key::K, KeyCode::KeyL => Key::L,
        KeyCode::KeyM => Key::M, KeyCode::KeyN => Key::N, KeyCode::KeyO => Key::O,
        KeyCode::KeyP => Key::P, KeyCode::KeyQ => Key::Q, KeyCode::KeyR => Key::R,
        KeyCode::KeyS => Key::S, KeyCode::KeyT => Key::T, KeyCode::KeyU => Key::U,
        KeyCode::KeyV => Key::V, KeyCode::KeyW => Key::W, KeyCode::KeyX => Key::X,
        KeyCode::KeyY => Key::Y, KeyCode::KeyZ => Key::Z,
        KeyCode::Digit1 => Key::Num1, KeyCode::Digit2 => Key::Num2,
        KeyCode::Digit3 => Key::Num3, KeyCode::Digit4 => Key::Num4,
        KeyCode::Digit5 => Key::Num5, KeyCode::Digit6 => Key::Num6,
        KeyCode::Digit7 => Key::Num7, KeyCode::Digit8 => Key::Num8,
        KeyCode::Digit9 => Key::Num9, KeyCode::Digit0 => Key::Num0,
        KeyCode::ArrowUp    => Key::Up,    KeyCode::ArrowDown  => Key::Down,
        KeyCode::ArrowLeft  => Key::Left,  KeyCode::ArrowRight => Key::Right,
        KeyCode::Enter | KeyCode::NumpadEnter => Key::Enter,
        KeyCode::Escape     => Key::Escape,
        KeyCode::Space      => Key::Space,
        KeyCode::Backspace  => Key::Backspace,
        KeyCode::Tab        => Key::Tab,
        KeyCode::ShiftLeft   => Key::LShift,  KeyCode::ShiftRight   => Key::RShift,
        KeyCode::ControlLeft => Key::LCtrl,   KeyCode::ControlRight => Key::RCtrl,
        KeyCode::AltLeft     => Key::LAlt,    KeyCode::AltRight     => Key::RAlt,
        KeyCode::F1  => Key::F1,  KeyCode::F2  => Key::F2,  KeyCode::F3  => Key::F3,
        KeyCode::F4  => Key::F4,  KeyCode::F5  => Key::F5,  KeyCode::F6  => Key::F6,
        KeyCode::F7  => Key::F7,  KeyCode::F8  => Key::F8,  KeyCode::F9  => Key::F9,
        KeyCode::F10 => Key::F10, KeyCode::F11 => Key::F11, KeyCode::F12 => Key::F12,
        KeyCode::Slash        => Key::Slash,
        KeyCode::Backslash    => Key::Backslash,
        KeyCode::Period       => Key::Period,
        KeyCode::Comma        => Key::Comma,
        KeyCode::Semicolon    => Key::Semicolon,
        KeyCode::Quote        => Key::Quote,
        KeyCode::BracketLeft  => Key::LBracket,
        KeyCode::BracketRight => Key::RBracket,
        KeyCode::Minus        => Key::Minus,
        KeyCode::Equal        => Key::Equals,
        KeyCode::Backquote    => Key::Backtick,
        KeyCode::PageUp       => Key::PageUp,
        KeyCode::PageDown     => Key::PageDown,
        KeyCode::Home         => Key::Home,
        KeyCode::End          => Key::End,
        KeyCode::Insert       => Key::Insert,
        KeyCode::Delete       => Key::Delete,
        _ => return None,
    })
}