sugarloaf 0.4.5

Sugarloaf is Rio rendering engine, designed to be multiplatform. It is based on WebGPU, Rust library for Desktops and WebAssembly for Web (JavaScript). This project is created and maintained for Rio terminal purposes but feel free to use it.
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
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
//! Vulkan backend built directly on `ash`.
//!
//! Mirrors `context::metal::MetalContext` in shape and intent: one struct
//! owning everything needed to present a swapchain image, plus the
//! per-frame synchronisation primitives. No wgpu involvement.
//!
//! Targets Vulkan 1.3 so we can reach for `VK_KHR_dynamic_rendering` (core
//! in 1.3) later without changing device creation. Anyone on a driver
//! older than early-2022 will fail at `create_instance` with
//! `ERROR_INCOMPATIBLE_DRIVER` — same class of failure as an ancient GPU
//! on the Metal path.
//!
//! Surface creation dispatches on `raw-window-handle` variants inline
//! rather than pulling in `ash-window` — that crate is not in Debian and
//! `ash-window` buys us ~30 lines of glue per platform that we'd rather
//! own.

use crate::sugarloaf::{Colorspace, SugarloafWindow, SugarloafWindowSize};
use ash::khr;
use ash::vk;
use ash::{Device, Entry, Instance};
use raw_window_handle::{
    HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use std::ffi::{c_char, CStr};
use std::sync::Arc;

/// Reference-counted owner of the raw Vulkan handles whose destruction
/// must be sequenced last. Held as `Arc<VkShared>` by every struct
/// (VulkanContext, VulkanGridRenderer, VulkanRenderer, VulkanBuffer,
/// VulkanImage, VulkanImageTexture, the text overlay's Vulkan state)
/// that would otherwise call into the device from its own `Drop`.
///
/// `vkDestroyDevice` runs only when the last `Arc` clone is dropped,
/// so per-resource Drop order across `Sugarloaf` and `Screen.grids`
/// stops being load-bearing — the field-declaration trick that worked
/// for `VulkanRenderer`-inside-`Sugarloaf` (single parent) cannot
/// extend to `VulkanGridRenderer`-inside-`Screen.grids` (a sibling
/// of the parent that owns the device), which is the bug behind
/// raphamorim/rio#1568.
///
/// Mirrors `wgpu_hal::vulkan::DeviceShared`. `raw` is named for the
/// underlying `ash::Device` to match wgpu-hal's convention; `Deref`
/// dispatches `shared.method(...)` calls straight to the device's
/// dispatch table, so consumers don't have to write `shared.raw.method()`.
pub struct VkShared {
    // Declaration order = drop order. Vulkan rules:
    //   * `vkDestroyDevice` requires the parent `Instance` to still be
    //     alive (we look up the destroy entry point through it).
    //   * `vkDestroyInstance` requires the loaded `libvulkan` symbols
    //     (the `Entry`) to still be loaded.
    pub raw: Device,
    pub instance: Instance,
    pub physical_device: vk::PhysicalDevice,
    _entry: Entry,
}

// `ash::Device` / `ash::Instance` / `ash::Entry` are dispatch tables
// with no interior mutability; the underlying Vulkan handles are
// thread-safe per spec (external synchronisation is per-object, not
// per-device). `vk::PhysicalDevice` is a plain handle. So `VkShared`
// is safe to share across threads via `Arc`.
unsafe impl Send for VkShared {}
unsafe impl Sync for VkShared {}

impl Drop for VkShared {
    fn drop(&mut self) {
        unsafe {
            // Defensive: the last clone of `VkShared` should normally
            // be `VulkanContext`, which already idled in its own
            // `Drop`. If a leaf resource (buffer, image, grid
            // renderer) outlives the context — possible because the
            // grid renderers live in `Screen.grids` while
            // `VulkanContext` lives in `Screen.sugarloaf` — this is
            // the only `device_wait_idle` we get. Cheap on an idle
            // queue.
            let _ = self.raw.device_wait_idle();
            self.raw.destroy_device(None);
            self.instance.destroy_instance(None);
            // `_entry` drops here, unloading `libvulkan`.
        }
    }
}

impl std::ops::Deref for VkShared {
    type Target = Device;
    #[inline]
    fn deref(&self) -> &Device {
        &self.raw
    }
}

/// How many frames the CPU is allowed to pipeline ahead of the GPU.
/// Three matches the Metal backend (`MetalLayer::set_maximum_drawable_count(3)`
/// in `context::metal::MetalContext::new`) and Apple's standard sample
/// pattern — CPU / GPU / compositor each work on their own frame in
/// parallel. The cost is two extra `FrameSync` slots and one extra
/// swapchain image's worth of memory.
pub const FRAMES_IN_FLIGHT: usize = 3;

/// One set of synchronisation objects + a command pool & pre-allocated
/// primary buffer, reused each time the same slot comes around. The
/// `in_flight` fence is signalled by the submit that uses this slot so
/// the *next* owner knows the GPU is done with this slot's resources.
struct FrameSync {
    image_available: vk::Semaphore,
    render_finished: vk::Semaphore,
    in_flight: vk::Fence,
    cmd_pool: vk::CommandPool,
    cmd_buffer: vk::CommandBuffer,
}

pub struct VulkanContext {
    // Logical fields for the public surface.
    pub size: SugarloafWindowSize,
    pub scale: f32,
    pub supports_f16: bool,
    pub colorspace: Colorspace,
    /// Updated on every `acquire_frame()` — `true` if the driver hinted
    /// that the swapchain is out of date and we should recreate at our
    /// earliest convenience (we already did if ERROR_OUT_OF_DATE_KHR, but
    /// SUBOPTIMAL_KHR says "still usable this frame").
    pub needs_recreate: bool,

    // Per-frame state.
    frame_index: usize,
    frames: [FrameSync; FRAMES_IN_FLIGHT],

    // Swapchain state. Rebuilt by `resize()`.
    swapchain_extent: vk::Extent2D,
    swapchain_color_space: vk::ColorSpaceKHR,
    swapchain_format: vk::Format,
    swapchain_images: Vec<vk::Image>,
    swapchain_views: Vec<vk::ImageView>,
    swapchain: vk::SwapchainKHR,
    swapchain_loader: khr::swapchain::Device,

    // Core device.
    queue: vk::Queue,
    // Kept around so future phases (atlas uploads, a dedicated transfer
    // pool, pipeline creation) don't have to re-probe the family.
    #[allow(dead_code)]
    queue_family_index: u32,

    /// Reference-counted owner of `device`, `instance`, `physical_device`,
    /// and the loader (`Entry`). Cloned into every per-resource struct
    /// (`VulkanBuffer`, `VulkanImage`, `VulkanGridRenderer`,
    /// `VulkanRenderer`, `VulkanImageTexture`, the text overlay's
    /// Vulkan state) so `vkDestroyDevice` runs only after the last
    /// dependent resource is dropped. Replaces the previous bare
    /// `device.clone()` cloning, which was unsafe across struct
    /// boundaries (raphamorim/rio#1568). `Deref` to `ash::Device`
    /// keeps call sites unchanged: `self.shared.cmd_bind_pipeline(...)`.
    shared: Arc<VkShared>,

    /// Pipeline cache shared by every `create_graphics_pipelines`
    /// call. Loaded from `~/.cache/rio/sugarloaf-vulkan.cache` (best
    /// effort) at startup and serialised back on `Drop`. Saves
    /// ~10–50ms of pipeline build time on subsequent launches.
    pipeline_cache: vk::PipelineCache,

    // Instance-level state — held last so it outlives everything above in
    // the Drop impl (drop order = declaration order).
    surface: vk::SurfaceKHR,
    surface_loader: khr::surface::Instance,
    /// Debug-utils messenger, present only when validation layers
    /// were requested via `RIO_VULKAN_VALIDATION=1`. Drops before
    /// `instance` (declaration order) so the messenger handle is
    /// destroyed while the instance is still alive.
    _debug_messenger: Option<DebugMessenger>,
}

/// Owns one `vk::DebugUtilsMessengerEXT` and its loader. Destroyed
/// in `Drop` — the loader needs the parent `Instance` to still be
/// valid, which the field-order convention ensures.
struct DebugMessenger {
    loader: ash::ext::debug_utils::Instance,
    handle: vk::DebugUtilsMessengerEXT,
}

impl Drop for DebugMessenger {
    fn drop(&mut self) {
        unsafe {
            self.loader.destroy_debug_utils_messenger(self.handle, None);
        }
    }
}

/// In-flight handle returned by `acquire_frame()`. The caller records
/// commands into `cmd_buffer` targeting `image` / `image_view`, then
/// hands it back to `present_frame()`.
pub struct VulkanFrame {
    pub image_index: u32,
    pub image: vk::Image,
    pub image_view: vk::ImageView,
    pub cmd_buffer: vk::CommandBuffer,
    pub extent: vk::Extent2D,
    pub format: vk::Format,
    /// Frame-in-flight slot for this frame. Renderers (grid, text,
    /// images) ring their per-frame GPU resources by this index; the
    /// `in_flight` fence wait inside `acquire_frame` proved this slot
    /// is GPU-idle, so writing into slot `N`'s buffers from the CPU
    /// is safe.
    pub slot: usize,
}

impl VulkanContext {
    pub fn new(sugarloaf_window: SugarloafWindow) -> Self {
        let size = sugarloaf_window.size;
        let scale = sugarloaf_window.scale;

        // Loading the loader itself can fail if libvulkan.so is missing —
        // which is the expected failure on a machine without a Vulkan
        // driver installed. We let the panic propagate: the caller is
        // `Context::new` and the backend selection happened upstream, so
        // there's no graceful degradation path here (the WGPU/CPU
        // backends live behind different enum variants).
        let entry =
            unsafe { Entry::load() }.expect("failed to load Vulkan loader (libvulkan)");

        let validation_requested = validation_requested();
        let instance = create_instance(&entry, &sugarloaf_window, validation_requested);
        let _debug_messenger = if validation_requested {
            create_debug_messenger(&entry, &instance)
        } else {
            None
        };
        let surface_loader = khr::surface::Instance::new(&entry, &instance);
        let surface = create_surface(&entry, &instance, &sugarloaf_window);

        let (physical_device, queue_family_index) =
            pick_physical_device(&instance, &surface_loader, surface);

        let device = create_device(&instance, physical_device, queue_family_index);
        let queue = unsafe { device.get_device_queue(queue_family_index, 0) };
        let pipeline_cache = create_pipeline_cache(&device);

        let swapchain_loader = khr::swapchain::Device::new(&instance, &device);

        let (
            swapchain,
            swapchain_format,
            swapchain_color_space,
            swapchain_extent,
            swapchain_images,
            swapchain_views,
        ) = create_swapchain(
            &device,
            &surface_loader,
            &swapchain_loader,
            physical_device,
            surface,
            size.width as u32,
            size.height as u32,
            vk::SwapchainKHR::null(),
        );

        let frames = create_frames(&device, queue_family_index);

        // f16 = Vulkan's VK_KHR_shader_float16_int8 feature. Probe at
        // device creation time in a follow-up; for the MVP we report
        // false, matching the conservative default.
        let supports_f16 = false;

        tracing::info!(
            "Vulkan device created: {}",
            physical_device_name(&instance, physical_device)
        );
        tracing::info!(
            "Swapchain: {:?} {}x{} ({} images)",
            swapchain_format,
            swapchain_extent.width,
            swapchain_extent.height,
            swapchain_images.len()
        );
        log_memory_heap_choice(&instance, physical_device);

        let shared = Arc::new(VkShared {
            raw: device,
            instance,
            physical_device,
            _entry: entry,
        });

        VulkanContext {
            size,
            scale,
            supports_f16,
            colorspace: Colorspace::Srgb,
            needs_recreate: false,
            frame_index: 0,
            frames,
            swapchain_extent,
            swapchain_color_space,
            swapchain_format,
            swapchain_images,
            swapchain_views,
            swapchain,
            swapchain_loader,
            queue,
            queue_family_index,
            shared,
            pipeline_cache,
            surface,
            surface_loader,
            _debug_messenger,
        }
    }

    #[inline]
    pub fn set_scale(&mut self, scale: f32) {
        self.scale = scale;
    }

    #[inline]
    pub fn supports_f16(&self) -> bool {
        self.supports_f16
    }

    pub fn resize(&mut self, width: u32, height: u32) {
        if width == 0 || height == 0 {
            return;
        }
        self.size.width = width as f32;
        self.size.height = height as f32;
        self.recreate_swapchain(width, height);
    }

    fn recreate_swapchain(&mut self, width: u32, height: u32) {
        // The spec requires no resources tied to the old swapchain be
        // in use. Easiest safe path: wait for the device to go idle.
        // This is a resize, not a per-frame operation, so the stall is
        // acceptable (wgpu does the same thing).
        unsafe {
            let _ = self.shared.device_wait_idle();
        }

        for &view in &self.swapchain_views {
            unsafe { self.shared.destroy_image_view(view, None) };
        }
        self.swapchain_views.clear();
        self.swapchain_images.clear();

        let old = self.swapchain;
        let (swapchain, format, color_space, extent, images, views) = create_swapchain(
            &self.shared.raw,
            &self.surface_loader,
            &self.swapchain_loader,
            self.shared.physical_device,
            self.surface,
            width,
            height,
            old,
        );

        unsafe { self.swapchain_loader.destroy_swapchain(old, None) };

        self.swapchain = swapchain;
        self.swapchain_format = format;
        self.swapchain_color_space = color_space;
        self.swapchain_extent = extent;
        self.swapchain_images = images;
        self.swapchain_views = views;
        self.needs_recreate = false;
    }

    /// Acquire the next swapchain image and begin the per-frame command
    /// buffer. Returns `None` if the swapchain needed recreation (caller
    /// should skip this frame).
    pub fn acquire_frame(&mut self) -> Option<VulkanFrame> {
        // Honor a recreate request observed on the previous tick
        // (suboptimal acquire, suboptimal present, or OUT_OF_DATE on
        // present). Mirrors wgpu/Metal/wgpu-via-zed practice: keep
        // the flag close to the platform-frame entry point so no
        // tick draws against a swapchain the compositor has already
        // declared stale.
        if self.needs_recreate {
            self.recreate_swapchain(self.size.width as u32, self.size.height as u32);
        }

        let slot = self.frame_index;
        let sync = &self.frames[slot];

        unsafe {
            self.shared
                .wait_for_fences(&[sync.in_flight], true, u64::MAX)
                .expect("wait_for_fences");
        }

        let (image_index, suboptimal) = unsafe {
            match self.swapchain_loader.acquire_next_image(
                self.swapchain,
                u64::MAX,
                sync.image_available,
                vk::Fence::null(),
            ) {
                Ok(pair) => pair,
                Err(vk::Result::ERROR_OUT_OF_DATE_KHR) => {
                    self.recreate_swapchain(
                        self.size.width as u32,
                        self.size.height as u32,
                    );
                    return None;
                }
                Err(e) => panic!("acquire_next_image failed: {e:?}"),
            }
        };
        if suboptimal {
            self.needs_recreate = true;
        }

        // Only reset *after* we've committed to submitting — resetting
        // before acquire_next_image would leave us deadlocked if the
        // acquire returned OUT_OF_DATE and we bailed out.
        unsafe {
            self.shared
                .reset_fences(&[sync.in_flight])
                .expect("reset_fences");
            self.shared
                .reset_command_pool(sync.cmd_pool, vk::CommandPoolResetFlags::empty())
                .expect("reset_command_pool");
            self.shared
                .begin_command_buffer(
                    sync.cmd_buffer,
                    &vk::CommandBufferBeginInfo::default()
                        .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT),
                )
                .expect("begin_command_buffer");
        }

        Some(VulkanFrame {
            image_index,
            image: self.swapchain_images[image_index as usize],
            image_view: self.swapchain_views[image_index as usize],
            cmd_buffer: sync.cmd_buffer,
            extent: self.swapchain_extent,
            format: self.swapchain_format,
            slot,
        })
    }

    /// End the command buffer, submit, present, advance frame index.
    pub fn present_frame(&mut self, frame: VulkanFrame) {
        let sync = &self.frames[frame.slot];
        unsafe {
            self.shared
                .end_command_buffer(sync.cmd_buffer)
                .expect("end_command_buffer");

            let wait_semaphores = [sync.image_available];
            let wait_stages = [vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT];
            let signal_semaphores = [sync.render_finished];
            let cmd_buffers = [sync.cmd_buffer];
            let submit = vk::SubmitInfo::default()
                .wait_semaphores(&wait_semaphores)
                .wait_dst_stage_mask(&wait_stages)
                .command_buffers(&cmd_buffers)
                .signal_semaphores(&signal_semaphores);
            self.shared
                .queue_submit(self.queue, &[submit], sync.in_flight)
                .expect("queue_submit");

            let swapchains = [self.swapchain];
            let image_indices = [frame.image_index];
            let present_info = vk::PresentInfoKHR::default()
                .wait_semaphores(&signal_semaphores)
                .swapchains(&swapchains)
                .image_indices(&image_indices);
            match self
                .swapchain_loader
                .queue_present(self.queue, &present_info)
            {
                Ok(suboptimal) => {
                    if suboptimal {
                        self.needs_recreate = true;
                    }
                }
                Err(vk::Result::ERROR_OUT_OF_DATE_KHR) => {
                    self.needs_recreate = true;
                }
                Err(e) => panic!("queue_present failed: {e:?}"),
            }
        }

        self.frame_index = (self.frame_index + 1) % FRAMES_IN_FLIGHT;
    }

    /// Expose the underlying device so the renderer can record commands.
    #[inline]
    pub fn device(&self) -> &Device {
        &self.shared.raw
    }

    /// Reference-counted handle to the device + instance + entry. Each
    /// per-resource type (`VulkanBuffer`, `VulkanImage`,
    /// `VulkanGridRenderer`, `VulkanRenderer`, `VulkanImageTexture`,
    /// `TextVulkanState`) clones this and stores it directly so its
    /// own `Drop` can call `destroy_*` on a device that is guaranteed
    /// to still be alive — `vkDestroyDevice` runs only when the last
    /// `Arc` is dropped. The previous design (each leaf cloning the
    /// raw `ash::Device` dispatch table) crashed when the parent
    /// `VulkanContext` happened to drop first; see
    /// raphamorim/rio#1568.
    #[inline]
    pub fn shared(&self) -> &Arc<VkShared> {
        &self.shared
    }

    /// Color attachment format the swapchain was created with. Real
    /// pipelines need this at construction time so `VkPipelineRenderingCreateInfo`
    /// can declare a matching color attachment format. Stable across
    /// resize (only `extent` changes there).
    #[inline]
    pub fn swapchain_format(&self) -> vk::Format {
        self.swapchain_format
    }

    /// The instance + physical device that own this context's logical
    /// device. Renderers cache these so they can allocate buffers /
    /// images via the free `allocate_host_visible_buffer_raw` /
    /// `allocate_sampled_image_raw` helpers without needing a live
    /// `&VulkanContext` borrow at every allocation site (chiefly,
    /// `resize` which only has `&mut self`).
    #[inline]
    pub fn instance(&self) -> &Instance {
        &self.shared.instance
    }

    #[inline]
    pub fn physical_device(&self) -> vk::PhysicalDevice {
        self.shared.physical_device
    }

    /// Pipeline cache shared by every renderer's
    /// `create_graphics_pipelines` call. Pass this instead of
    /// `vk::PipelineCache::null()` so cached binaries land on disk
    /// at shutdown and short-circuit subsequent compiles.
    #[inline]
    pub fn pipeline_cache(&self) -> vk::PipelineCache {
        self.pipeline_cache
    }

    /// Run `record` against a transient command buffer, submit it,
    /// wait for completion. Used for one-shot transfer work that
    /// can't piggy-back on the per-frame command buffer (atlas /
    /// image / texture uploads triggered from outside the render
    /// loop, where there's no live `cmd` to append to).
    ///
    /// Allocates a fresh `vk::CommandPool` + `vk::Fence` per call
    /// and tears them down at the end. Cheap (microseconds) compared
    /// to the actual GPU transfer; not a hot path.
    pub fn submit_oneshot<F: FnOnce(vk::CommandBuffer)>(&self, record: F) {
        unsafe {
            let pool_info = vk::CommandPoolCreateInfo::default()
                .queue_family_index(self.queue_family_index)
                .flags(vk::CommandPoolCreateFlags::TRANSIENT);
            let pool = self
                .shared
                .create_command_pool(&pool_info, None)
                .expect("create_command_pool(oneshot)");

            let alloc = vk::CommandBufferAllocateInfo::default()
                .command_pool(pool)
                .level(vk::CommandBufferLevel::PRIMARY)
                .command_buffer_count(1);
            let cmd = self
                .shared
                .allocate_command_buffers(&alloc)
                .expect("allocate_command_buffers(oneshot)")[0];

            let begin = vk::CommandBufferBeginInfo::default()
                .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);
            self.shared
                .begin_command_buffer(cmd, &begin)
                .expect("begin_command_buffer(oneshot)");

            record(cmd);

            self.shared
                .end_command_buffer(cmd)
                .expect("end_command_buffer(oneshot)");

            let fence = self
                .shared
                .create_fence(&vk::FenceCreateInfo::default(), None)
                .expect("create_fence(oneshot)");
            let cmds = [cmd];
            let submit = vk::SubmitInfo::default().command_buffers(&cmds);
            self.shared
                .queue_submit(self.queue, &[submit], fence)
                .expect("queue_submit(oneshot)");
            self.shared
                .wait_for_fences(&[fence], true, u64::MAX)
                .expect("wait_for_fences(oneshot)");

            self.shared.destroy_fence(fence, None);
            self.shared.destroy_command_pool(pool, None);
        }
    }

    /// Index of the slot the *next* `acquire_frame` will use. Renderers
    /// (grid, text, image overlay) ring their per-frame GPU resources by
    /// this index so that a write into slot N can't race the GPU still
    /// reading from slot N. Stable for the lifetime of `VulkanContext`.
    #[inline]
    pub fn current_frame_slot(&self) -> usize {
        self.frame_index
    }

    /// Allocate a host-visible, host-coherent, persistently-mapped buffer
    /// suitable for per-frame uploads (uniform buffers, vertex/instance
    /// buffers, storage buffers that the CPU writes into and the GPU
    /// reads from this frame). On UMA/integrated GPUs the underlying
    /// memory will also be `DEVICE_LOCAL` (BAR memory) — the driver
    /// picks the best matching type via `memory_type_bits` filtering.
    ///
    /// We do not run a suballocator: each call burns one device memory
    /// allocation. Vulkan guarantees ≥4096 active allocations per
    /// device, and sugarloaf's working set is well under that ceiling
    /// (a couple of atlases + per-frame ring buffers per terminal).
    /// Switch to a slab allocator only if profiling ever shows
    /// allocation churn — current call sites construct once, reuse
    /// thereafter, and only reallocate on grow.
    pub fn allocate_host_visible_buffer(
        &self,
        size: u64,
        usage: vk::BufferUsageFlags,
    ) -> VulkanBuffer {
        // `vkCreateBuffer` rejects zero-sized buffers; bump up to a
        // single byte so callers don't have to special-case empty rings.
        let size = size.max(1);

        let buffer_info = vk::BufferCreateInfo::default()
            .size(size)
            .usage(usage)
            .sharing_mode(vk::SharingMode::EXCLUSIVE);
        let buffer = unsafe {
            self.shared
                .create_buffer(&buffer_info, None)
                .expect("create_buffer")
        };

        let req = unsafe { self.shared.get_buffer_memory_requirements(buffer) };
        let mem_type = find_memory_type(
            &self.shared.instance,
            self.shared.physical_device,
            req.memory_type_bits,
            vk::MemoryPropertyFlags::HOST_VISIBLE
                | vk::MemoryPropertyFlags::HOST_COHERENT,
        )
        .expect("no HOST_VISIBLE | HOST_COHERENT memory type — driver bug?");

        let alloc_info = vk::MemoryAllocateInfo::default()
            .allocation_size(req.size)
            .memory_type_index(mem_type);
        let memory = unsafe {
            self.shared
                .allocate_memory(&alloc_info, None)
                .expect("allocate_memory")
        };
        unsafe {
            self.shared
                .bind_buffer_memory(buffer, memory, 0)
                .expect("bind_buffer_memory");
        }

        // HOST_COHERENT means we never have to flush; mapping stays
        // valid until `vkUnmapMemory`, which we only do at Drop.
        let mapped = unsafe {
            self.shared
                .map_memory(memory, 0, vk::WHOLE_SIZE, vk::MemoryMapFlags::empty())
                .expect("map_memory") as *mut u8
        };

        VulkanBuffer {
            shared: self.shared.clone(),
            buffer,
            memory,
            mapped,
            size,
        }
    }
}

/// Host-visible, persistently-mapped buffer. Written to via [`as_mut_ptr`]
/// (raw pointer; caller owns the layout / bounds checks). The buffer
/// destroys itself + frees its backing memory on drop.
pub struct VulkanBuffer {
    /// Shared device handle. The Arc keeps the underlying
    /// `vkDestroyDevice` from running until *every* `VulkanBuffer`
    /// (and other resource) is dropped, regardless of the order in
    /// which their parents drop. See `VkShared`.
    shared: Arc<VkShared>,
    buffer: vk::Buffer,
    memory: vk::DeviceMemory,
    mapped: *mut u8,
    size: u64,
}

// `vk::Buffer`, `vk::DeviceMemory`, and the mapped pointer are all
// values the driver hands out per-allocation; `Arc<VkShared>` is
// Send+Sync (see the `unsafe impl` on `VkShared`). Buffers are never
// shared across threads in sugarloaf, but `Send` lets them sit inside
// `Sugarloaf` (which is not `!Send`).
unsafe impl Send for VulkanBuffer {}
unsafe impl Sync for VulkanBuffer {}

impl VulkanBuffer {
    #[inline]
    pub fn handle(&self) -> vk::Buffer {
        self.buffer
    }

    #[inline]
    pub fn size(&self) -> u64 {
        self.size
    }

    /// Raw pointer to the start of the mapping. Valid for the lifetime
    /// of this `VulkanBuffer`. Writes through this pointer are visible
    /// to the GPU at submit time — `HOST_COHERENT` removes the need for
    /// `vkFlushMappedMemoryRanges`.
    #[inline]
    pub fn as_mut_ptr(&self) -> *mut u8 {
        self.mapped
    }
}

impl Drop for VulkanBuffer {
    fn drop(&mut self) {
        unsafe {
            // Order: unmap, free memory, destroy buffer.
            // `vkFreeMemory` on a non-mapped allocation is safe; we
            // unmap first only because some validation layers warn
            // about freeing memory that's still mapped.
            self.shared.unmap_memory(self.memory);
            self.shared.destroy_buffer(self.buffer, None);
            self.shared.free_memory(self.memory, None);
        }
    }
}

/// Free-function variant of [`VulkanContext::allocate_host_visible_buffer`]
/// for callers that hold a cached `Arc<VkShared>` rather than a live
/// `&VulkanContext` borrow. The grid / text / image renderers stash
/// the shared handle at construction time so they can allocate from
/// inside their own `resize` paths (which only have `&mut self`, not
/// the parent context).
pub fn allocate_host_visible_buffer_raw(
    shared: &Arc<VkShared>,
    size: u64,
    usage: vk::BufferUsageFlags,
) -> VulkanBuffer {
    let size = size.max(1);
    let buffer_info = vk::BufferCreateInfo::default()
        .size(size)
        .usage(usage)
        .sharing_mode(vk::SharingMode::EXCLUSIVE);
    let buffer = unsafe {
        shared
            .create_buffer(&buffer_info, None)
            .expect("create_buffer")
    };
    let req = unsafe { shared.get_buffer_memory_requirements(buffer) };
    let mem_type = find_memory_type(
        &shared.instance,
        shared.physical_device,
        req.memory_type_bits,
        vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT,
    )
    .expect("no HOST_VISIBLE | HOST_COHERENT memory type");
    let alloc_info = vk::MemoryAllocateInfo::default()
        .allocation_size(req.size)
        .memory_type_index(mem_type);
    let memory = unsafe {
        shared
            .allocate_memory(&alloc_info, None)
            .expect("allocate_memory")
    };
    unsafe {
        shared
            .bind_buffer_memory(buffer, memory, 0)
            .expect("bind_buffer_memory");
    }
    let mapped = unsafe {
        shared
            .map_memory(memory, 0, vk::WHOLE_SIZE, vk::MemoryMapFlags::empty())
            .expect("map_memory") as *mut u8
    };
    VulkanBuffer {
        shared: shared.clone(),
        buffer,
        memory,
        mapped,
        size,
    }
}

/// Walks the device's memory types looking for one that matches both
/// `type_filter` (the bitmask returned by `vkGetBufferMemoryRequirements`)
/// and the requested `flags`. Returns `None` if no matching type
/// exists — that's a Vulkan-spec violation the driver should never
/// produce for the standard `HOST_VISIBLE | HOST_COHERENT` and
/// `DEVICE_LOCAL` combinations, but callers should still treat it as
/// fatal rather than ignore it.
fn find_memory_type(
    instance: &Instance,
    physical_device: vk::PhysicalDevice,
    type_filter: u32,
    flags: vk::MemoryPropertyFlags,
) -> Option<u32> {
    let props =
        unsafe { instance.get_physical_device_memory_properties(physical_device) };
    for i in 0..props.memory_type_count {
        let supported = (type_filter & (1 << i)) != 0;
        let matches_flags = props.memory_types[i as usize]
            .property_flags
            .contains(flags);
        if supported && matches_flags {
            return Some(i);
        }
    }
    None
}

/// One-off boot-time log of the memory types we'd pick for our two
/// hot allocation patterns. On UMA / integrated GPUs (Intel iGPU,
/// AMD APU, common Debian-laptop hardware) we expect the
/// host-visible heap to also report `DEVICE_LOCAL` — that's BAR
/// memory and our persistently-mapped per-frame buffers land in fast
/// GPU-accessible memory with no staging copy. On discrete GPUs the
/// host-visible heap is plain system RAM, slower for the GPU to
/// read; we'd want to switch to staging-buffer uploads for hot
/// per-frame data if profiling shows it matters.
fn log_memory_heap_choice(instance: &Instance, physical_device: vk::PhysicalDevice) {
    // Pretend `type_filter = !0` to ignore per-resource alignment
    // filtering — we just want the canonical pick for each pattern.
    let host_visible = find_memory_type(
        instance,
        physical_device,
        !0,
        vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT,
    );
    let device_local = find_memory_type(
        instance,
        physical_device,
        !0,
        vk::MemoryPropertyFlags::DEVICE_LOCAL,
    );
    let props =
        unsafe { instance.get_physical_device_memory_properties(physical_device) };
    if let Some(idx) = host_visible {
        let flags = props.memory_types[idx as usize].property_flags;
        let bar = flags.contains(vk::MemoryPropertyFlags::DEVICE_LOCAL);
        tracing::info!(
            "Vulkan host-visible memory: type {} flags={:?} ({})",
            idx,
            flags,
            if bar {
                "BAR / unified — fast GPU reads"
            } else {
                "system RAM — slower GPU reads, consider staging for hot data"
            }
        );
    }
    if let Some(idx) = device_local {
        tracing::info!(
            "Vulkan device-local memory: type {} flags={:?}",
            idx,
            props.memory_types[idx as usize].property_flags
        );
    }
}

// -----------------------------------------------------------------------
// Image helper (device-local 2D image + view + memory)
// -----------------------------------------------------------------------

impl VulkanContext {
    /// Allocate a device-local 2D image suitable for sampling from a
    /// shader (atlas, kitty graphic, background image). Created in
    /// `UNDEFINED` layout — the caller's first transfer command must
    /// include a barrier transitioning to `TRANSFER_DST_OPTIMAL`
    /// before any `vkCmdCopyBufferToImage`.
    pub fn allocate_sampled_image(
        &self,
        width: u32,
        height: u32,
        format: vk::Format,
        usage: vk::ImageUsageFlags,
    ) -> VulkanImage {
        let image_info = vk::ImageCreateInfo::default()
            .image_type(vk::ImageType::TYPE_2D)
            .format(format)
            .extent(vk::Extent3D {
                width,
                height,
                depth: 1,
            })
            .mip_levels(1)
            .array_layers(1)
            .samples(vk::SampleCountFlags::TYPE_1)
            .tiling(vk::ImageTiling::OPTIMAL)
            .usage(usage)
            .sharing_mode(vk::SharingMode::EXCLUSIVE)
            .initial_layout(vk::ImageLayout::UNDEFINED);
        let image = unsafe {
            self.shared
                .create_image(&image_info, None)
                .expect("create_image")
        };

        let req = unsafe { self.shared.get_image_memory_requirements(image) };
        let mem_type = find_memory_type(
            &self.shared.instance,
            self.shared.physical_device,
            req.memory_type_bits,
            vk::MemoryPropertyFlags::DEVICE_LOCAL,
        )
        .expect("no DEVICE_LOCAL memory type — driver bug?");

        let alloc_info = vk::MemoryAllocateInfo::default()
            .allocation_size(req.size)
            .memory_type_index(mem_type);
        let memory = unsafe {
            self.shared
                .allocate_memory(&alloc_info, None)
                .expect("allocate_memory(image)")
        };
        unsafe {
            self.shared
                .bind_image_memory(image, memory, 0)
                .expect("bind_image_memory");
        }

        let view_info = vk::ImageViewCreateInfo::default()
            .image(image)
            .view_type(vk::ImageViewType::TYPE_2D)
            .format(format)
            .components(vk::ComponentMapping::default())
            .subresource_range(
                vk::ImageSubresourceRange::default()
                    .aspect_mask(vk::ImageAspectFlags::COLOR)
                    .base_mip_level(0)
                    .level_count(1)
                    .base_array_layer(0)
                    .layer_count(1),
            );
        let view = unsafe {
            self.shared
                .create_image_view(&view_info, None)
                .expect("create_image_view")
        };

        VulkanImage {
            shared: self.shared.clone(),
            image,
            view,
            memory,
            width,
            height,
            format,
        }
    }
}

/// Device-local 2D image with view + backing memory. Drops the view,
/// image, and memory on `Drop`. The image starts in `UNDEFINED` layout
/// — the first command that uses it must barrier-transition to a
/// usable layout (`TRANSFER_DST_OPTIMAL` for the initial upload).
pub struct VulkanImage {
    /// Shared device handle. See `VkShared`.
    shared: Arc<VkShared>,
    image: vk::Image,
    view: vk::ImageView,
    memory: vk::DeviceMemory,
    pub width: u32,
    pub height: u32,
    pub format: vk::Format,
}

unsafe impl Send for VulkanImage {}
unsafe impl Sync for VulkanImage {}

impl VulkanImage {
    #[inline]
    pub fn handle(&self) -> vk::Image {
        self.image
    }

    #[inline]
    pub fn view(&self) -> vk::ImageView {
        self.view
    }
}

impl Drop for VulkanImage {
    fn drop(&mut self) {
        unsafe {
            self.shared.destroy_image_view(self.view, None);
            self.shared.destroy_image(self.image, None);
            self.shared.free_memory(self.memory, None);
        }
    }
}

impl Drop for VulkanContext {
    fn drop(&mut self) {
        unsafe {
            // Idle the queue before tearing down anything that might
            // still be in flight (swapchain image views, sync prims).
            // `vkDestroyDevice` itself happens later, when the last
            // `Arc<VkShared>` clone drops — see `VkShared::drop`.
            let _ = self.shared.device_wait_idle();

            // Best-effort: serialize the pipeline cache to disk
            // before destroying it. Failure (no XDG_CACHE_HOME, no
            // write perms, etc) is logged but not fatal.
            save_pipeline_cache(&self.shared.raw, self.pipeline_cache);
            self.shared
                .destroy_pipeline_cache(self.pipeline_cache, None);

            for frame in &self.frames {
                self.shared.destroy_semaphore(frame.image_available, None);
                self.shared.destroy_semaphore(frame.render_finished, None);
                self.shared.destroy_fence(frame.in_flight, None);
                self.shared.destroy_command_pool(frame.cmd_pool, None);
            }

            for &view in &self.swapchain_views {
                self.shared.destroy_image_view(view, None);
            }
            self.swapchain_loader
                .destroy_swapchain(self.swapchain, None);
            self.surface_loader.destroy_surface(self.surface, None);
            // `_debug_messenger` (declared after this Drop's body
            // unwind path completes) drops before `shared` (declared
            // before it), so the messenger handle is destroyed while
            // the instance is still alive. `vkDestroyDevice` and
            // `vkDestroyInstance` run in `VkShared::drop` once the
            // last `Arc<VkShared>` clone is gone.
        }
    }
}

// =======================================================================
// Pipeline cache (load on `new`, save on `Drop`)
// =======================================================================

/// Path to the on-disk pipeline cache. Returns `None` if neither
/// `XDG_CACHE_HOME` nor `HOME` is set.
fn pipeline_cache_path() -> Option<std::path::PathBuf> {
    let dir = if let Some(xdg) = std::env::var_os("XDG_CACHE_HOME") {
        std::path::PathBuf::from(xdg)
    } else if let Some(home) = std::env::var_os("HOME") {
        let mut p = std::path::PathBuf::from(home);
        p.push(".cache");
        p
    } else {
        return None;
    };
    Some(dir.join("rio").join("sugarloaf-vulkan.cache"))
}

fn create_pipeline_cache(device: &Device) -> vk::PipelineCache {
    let initial_data: Vec<u8> = pipeline_cache_path()
        .and_then(|p| std::fs::read(&p).ok())
        .unwrap_or_default();
    if !initial_data.is_empty() {
        tracing::info!("loaded Vulkan pipeline cache: {} bytes", initial_data.len());
    }
    let info = vk::PipelineCacheCreateInfo::default().initial_data(&initial_data);
    unsafe {
        device
            .create_pipeline_cache(&info, None)
            .expect("create_pipeline_cache")
    }
}

fn save_pipeline_cache(device: &Device, cache: vk::PipelineCache) {
    let Some(path) = pipeline_cache_path() else {
        return;
    };
    let data = match unsafe { device.get_pipeline_cache_data(cache) } {
        Ok(d) => d,
        Err(e) => {
            tracing::warn!("get_pipeline_cache_data failed: {:?}", e);
            return;
        }
    };
    if data.is_empty() {
        return;
    }
    if let Some(parent) = path.parent() {
        if let Err(e) = std::fs::create_dir_all(parent) {
            tracing::warn!("pipeline cache mkdir {:?} failed: {}", parent, e);
            return;
        }
    }
    if let Err(e) = std::fs::write(&path, &data) {
        tracing::warn!("pipeline cache write {:?} failed: {}", path, e);
    } else {
        tracing::info!(
            "saved Vulkan pipeline cache: {} bytes → {:?}",
            data.len(),
            path
        );
    }
}

// -------------------------------------------------------------------------
// Internal helpers (free functions so `new()` stays readable).
// -------------------------------------------------------------------------

fn create_instance(
    entry: &Entry,
    window: &SugarloafWindow,
    enable_validation: bool,
) -> Instance {
    let app_name = c"sugarloaf";
    let app_info = vk::ApplicationInfo::default()
        .application_name(app_name)
        .application_version(0)
        .engine_name(app_name)
        .engine_version(0)
        .api_version(vk::API_VERSION_1_3);

    // KHR_surface + the right platform surface extension for the window
    // handle we were given. Adding extensions the driver doesn't
    // advertise makes `create_instance` fail, so we match the window
    // type exactly instead of asking for all three.
    let mut extensions: Vec<*const c_char> = vec![khr::surface::NAME.as_ptr()];
    match window.display_handle().unwrap().as_raw() {
        RawDisplayHandle::Xlib(_) => extensions.push(khr::xlib_surface::NAME.as_ptr()),
        RawDisplayHandle::Xcb(_) => extensions.push(khr::xcb_surface::NAME.as_ptr()),
        RawDisplayHandle::Wayland(_) => {
            extensions.push(khr::wayland_surface::NAME.as_ptr())
        }
        other => panic!("Vulkan backend: unsupported display handle {:?}", other),
    }

    // Validation: append `VK_EXT_debug_utils` so we can install a
    // messenger callback after instance creation. The layer
    // (`VK_LAYER_KHRONOS_validation`) is enabled separately via
    // `enabled_layer_names` below.
    let validation_layer_name = c"VK_LAYER_KHRONOS_validation";
    let layer_ptrs: Vec<*const c_char> = if enable_validation {
        if validation_layer_available(entry, validation_layer_name) {
            extensions.push(ash::ext::debug_utils::NAME.as_ptr());
            vec![validation_layer_name.as_ptr()]
        } else {
            tracing::warn!(
                "RIO_VULKAN_VALIDATION set but VK_LAYER_KHRONOS_validation \
                 not available — install `vulkan-validationlayers` (Debian) \
                 / `vulkan-validation-layers` (Arch) to enable it"
            );
            Vec::new()
        }
    } else {
        Vec::new()
    };

    let create_info = vk::InstanceCreateInfo::default()
        .application_info(&app_info)
        .enabled_extension_names(&extensions)
        .enabled_layer_names(&layer_ptrs);

    unsafe { entry.create_instance(&create_info, None) }
        .expect("vkCreateInstance failed — is a Vulkan 1.3 driver installed?")
}

/// True if the user opted into validation via `RIO_VULKAN_VALIDATION=1`.
/// We always read the env var (debug + release) so users can flip it
/// on for one run without recompiling.
fn validation_requested() -> bool {
    std::env::var_os("RIO_VULKAN_VALIDATION")
        .map(|v| v != "0" && !v.is_empty())
        .unwrap_or(false)
}

fn validation_layer_available(entry: &Entry, target: &CStr) -> bool {
    match unsafe { entry.enumerate_instance_layer_properties() } {
        Ok(layers) => layers.iter().any(|l| {
            let name = unsafe { CStr::from_ptr(l.layer_name.as_ptr()) };
            name == target
        }),
        Err(_) => false,
    }
}

fn create_debug_messenger(entry: &Entry, instance: &Instance) -> Option<DebugMessenger> {
    let loader = ash::ext::debug_utils::Instance::new(entry, instance);

    let info = vk::DebugUtilsMessengerCreateInfoEXT::default()
        .message_severity(
            vk::DebugUtilsMessageSeverityFlagsEXT::ERROR
                | vk::DebugUtilsMessageSeverityFlagsEXT::WARNING
                | vk::DebugUtilsMessageSeverityFlagsEXT::INFO,
        )
        .message_type(
            vk::DebugUtilsMessageTypeFlagsEXT::GENERAL
                | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION
                | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE,
        )
        .pfn_user_callback(Some(debug_callback));

    let handle = unsafe { loader.create_debug_utils_messenger(&info, None) }
        .expect("create_debug_utils_messenger");
    tracing::info!("Vulkan validation layers active");
    Some(DebugMessenger { loader, handle })
}

unsafe extern "system" fn debug_callback(
    severity: vk::DebugUtilsMessageSeverityFlagsEXT,
    msg_type: vk::DebugUtilsMessageTypeFlagsEXT,
    callback_data: *const vk::DebugUtilsMessengerCallbackDataEXT<'_>,
    _user_data: *mut std::ffi::c_void,
) -> vk::Bool32 {
    let data = unsafe { &*callback_data };
    let message = if data.p_message.is_null() {
        std::borrow::Cow::Borrowed("<null>")
    } else {
        unsafe { CStr::from_ptr(data.p_message) }.to_string_lossy()
    };
    let kind = if msg_type.contains(vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION) {
        "validation"
    } else if msg_type.contains(vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE) {
        "perf"
    } else {
        "general"
    };
    if severity.contains(vk::DebugUtilsMessageSeverityFlagsEXT::ERROR) {
        tracing::error!("vk[{}] {}", kind, message);
    } else if severity.contains(vk::DebugUtilsMessageSeverityFlagsEXT::WARNING) {
        tracing::warn!("vk[{}] {}", kind, message);
    } else if severity.contains(vk::DebugUtilsMessageSeverityFlagsEXT::INFO) {
        tracing::info!("vk[{}] {}", kind, message);
    } else {
        tracing::debug!("vk[{}] {}", kind, message);
    }
    vk::FALSE
}

fn create_surface(
    entry: &Entry,
    instance: &Instance,
    window: &SugarloafWindow,
) -> vk::SurfaceKHR {
    let display = window.display_handle().unwrap().as_raw();
    let window_handle = window.window_handle().unwrap().as_raw();

    unsafe {
        match (display, window_handle) {
            (RawDisplayHandle::Xlib(d), RawWindowHandle::Xlib(w)) => {
                let loader = khr::xlib_surface::Instance::new(entry, instance);
                let info = vk::XlibSurfaceCreateInfoKHR::default()
                    .dpy(
                        d.display
                            .expect("Xlib display pointer missing")
                            .as_ptr()
                            .cast(),
                    )
                    .window(w.window);
                loader
                    .create_xlib_surface(&info, None)
                    .expect("create_xlib_surface")
            }
            (RawDisplayHandle::Xcb(d), RawWindowHandle::Xcb(w)) => {
                let loader = khr::xcb_surface::Instance::new(entry, instance);
                let info = vk::XcbSurfaceCreateInfoKHR::default()
                    .connection(
                        d.connection
                            .expect("Xcb connection pointer missing")
                            .as_ptr()
                            .cast(),
                    )
                    .window(w.window.get());
                loader
                    .create_xcb_surface(&info, None)
                    .expect("create_xcb_surface")
            }
            (RawDisplayHandle::Wayland(d), RawWindowHandle::Wayland(w)) => {
                let loader = khr::wayland_surface::Instance::new(entry, instance);
                let info = vk::WaylandSurfaceCreateInfoKHR::default()
                    .display(d.display.as_ptr().cast())
                    .surface(w.surface.as_ptr().cast());
                loader
                    .create_wayland_surface(&info, None)
                    .expect("create_wayland_surface")
            }
            (d, w) => panic!(
                "Vulkan backend: mismatched or unsupported handles: display={d:?} window={w:?}"
            ),
        }
    }
}

/// Pick a physical device + queue family. Prefer discrete GPU, require
/// a queue family that supports both graphics and present on our surface.
fn pick_physical_device(
    instance: &Instance,
    surface_loader: &khr::surface::Instance,
    surface: vk::SurfaceKHR,
) -> (vk::PhysicalDevice, u32) {
    let devices = unsafe { instance.enumerate_physical_devices() }
        .expect("enumerate_physical_devices");

    let mut best: Option<(vk::PhysicalDevice, u32, i32)> = None;
    for device in devices {
        let props = unsafe { instance.get_physical_device_properties(device) };
        let qf_props =
            unsafe { instance.get_physical_device_queue_family_properties(device) };

        for (index, qf) in qf_props.iter().enumerate() {
            let index = index as u32;
            if !qf.queue_flags.contains(vk::QueueFlags::GRAPHICS) {
                continue;
            }
            let present_ok = unsafe {
                surface_loader
                    .get_physical_device_surface_support(device, index, surface)
                    .unwrap_or(false)
            };
            if !present_ok {
                continue;
            }
            let score = match props.device_type {
                vk::PhysicalDeviceType::DISCRETE_GPU => 1000,
                vk::PhysicalDeviceType::INTEGRATED_GPU => 500,
                vk::PhysicalDeviceType::VIRTUAL_GPU => 100,
                vk::PhysicalDeviceType::CPU => 10,
                _ => 1,
            };
            if best.map(|(_, _, s)| score > s).unwrap_or(true) {
                best = Some((device, index, score));
            }
        }
    }

    let (device, queue_family, _) =
        best.expect("no Vulkan device with graphics + present support on this surface");
    (device, queue_family)
}

fn physical_device_name(instance: &Instance, device: vk::PhysicalDevice) -> String {
    let props = unsafe { instance.get_physical_device_properties(device) };
    // `device_name` is a C string embedded in a fixed-size array.
    let raw = props.device_name.as_ptr();
    unsafe { CStr::from_ptr(raw) }
        .to_string_lossy()
        .into_owned()
}

fn create_device(
    instance: &Instance,
    physical_device: vk::PhysicalDevice,
    queue_family_index: u32,
) -> Device {
    let queue_priorities = [1.0f32];
    let queue_info = vk::DeviceQueueCreateInfo::default()
        .queue_family_index(queue_family_index)
        .queue_priorities(&queue_priorities);

    let device_extensions = [khr::swapchain::NAME.as_ptr()];

    // Enable dynamic_rendering and synchronization2 — both are Vulkan
    // 1.3 core. `synchronization2` is required for `vkCmdPipelineBarrier2`,
    // which the swapchain layout transitions
    // (UNDEFINED → COLOR_ATTACHMENT_OPTIMAL and COLOR_ATTACHMENT_OPTIMAL
    // → PRESENT_SRC_KHR) and atlas image transitions go through. Without
    // the feature enabled, the v2 barrier calls are silently ignored,
    // the swapchain image stays in UNDEFINED layout, and the compositor
    // discards the present — visible as "first frame and stops."
    let mut vk13_features = vk::PhysicalDeviceVulkan13Features::default()
        .dynamic_rendering(true)
        .synchronization2(true);

    let queue_infos = [queue_info];
    let create_info = vk::DeviceCreateInfo::default()
        .queue_create_infos(&queue_infos)
        .enabled_extension_names(&device_extensions)
        .push_next(&mut vk13_features);

    unsafe { instance.create_device(physical_device, &create_info, None) }
        .expect("vkCreateDevice")
}

/// Build a swapchain and its image views. `old` is passed as
/// `old_swapchain` so the driver can recycle images during resize.
#[allow(clippy::too_many_arguments)]
fn create_swapchain(
    device: &Device,
    surface_loader: &khr::surface::Instance,
    swapchain_loader: &khr::swapchain::Device,
    physical_device: vk::PhysicalDevice,
    surface: vk::SurfaceKHR,
    requested_width: u32,
    requested_height: u32,
    old: vk::SwapchainKHR,
) -> (
    vk::SwapchainKHR,
    vk::Format,
    vk::ColorSpaceKHR,
    vk::Extent2D,
    Vec<vk::Image>,
    Vec<vk::ImageView>,
) {
    let caps = unsafe {
        surface_loader
            .get_physical_device_surface_capabilities(physical_device, surface)
            .expect("get_physical_device_surface_capabilities")
    };
    let formats = unsafe {
        surface_loader
            .get_physical_device_surface_formats(physical_device, surface)
            .expect("get_physical_device_surface_formats")
    };
    // Prefer BGRA8_UNORM (linear) so blending stays in gamma space — the
    // same choice Metal makes (`MTLPixelFormat::BGRA8Unorm` + DisplayP3
    // tag). Fragment shaders will emit sRGB-encoded output. If the
    // driver doesn't offer BGRA8_UNORM, fall back to whatever it gives
    // us — formats[0] is guaranteed present per the spec.
    let chosen_format = formats
        .iter()
        .find(|f| {
            f.format == vk::Format::B8G8R8A8_UNORM
                && f.color_space == vk::ColorSpaceKHR::SRGB_NONLINEAR
        })
        .copied()
        .unwrap_or(formats[0]);

    // FIFO. Spec-guaranteed and vsync-throttled at present time, so the
    // renderer can't outrun the display. MAILBOX on X11 (Mesa DRI3/Present)
    // runs a driver-side polling thread and discards rendered frames, which
    // pegged Xorg under sustained input.
    let present_mode = vk::PresentModeKHR::FIFO;

    let extent = if caps.current_extent.width != u32::MAX {
        caps.current_extent
    } else {
        vk::Extent2D {
            width: requested_width
                .clamp(caps.min_image_extent.width, caps.max_image_extent.width),
            height: requested_height
                .clamp(caps.min_image_extent.height, caps.max_image_extent.height),
        }
    };

    // Aim for 3 images where the driver allows it (triple buffering),
    // clamped to the advertised range. `max_image_count == 0` means "no
    // upper limit".
    let mut image_count = caps.min_image_count.max(3);
    if caps.max_image_count != 0 && image_count > caps.max_image_count {
        image_count = caps.max_image_count;
    }

    let create_info = vk::SwapchainCreateInfoKHR::default()
        .surface(surface)
        .min_image_count(image_count)
        .image_format(chosen_format.format)
        .image_color_space(chosen_format.color_space)
        .image_extent(extent)
        .image_array_layers(1)
        .image_usage(
            vk::ImageUsageFlags::COLOR_ATTACHMENT | vk::ImageUsageFlags::TRANSFER_DST,
        )
        .image_sharing_mode(vk::SharingMode::EXCLUSIVE)
        .pre_transform(caps.current_transform)
        .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
        .present_mode(present_mode)
        .clipped(true)
        .old_swapchain(old);

    let swapchain = unsafe { swapchain_loader.create_swapchain(&create_info, None) }
        .expect("create_swapchain");

    let images = unsafe { swapchain_loader.get_swapchain_images(swapchain) }
        .expect("get_swapchain_images");

    let views = images
        .iter()
        .map(|&image| {
            let info = vk::ImageViewCreateInfo::default()
                .image(image)
                .view_type(vk::ImageViewType::TYPE_2D)
                .format(chosen_format.format)
                .components(vk::ComponentMapping::default())
                .subresource_range(
                    vk::ImageSubresourceRange::default()
                        .aspect_mask(vk::ImageAspectFlags::COLOR)
                        .base_mip_level(0)
                        .level_count(1)
                        .base_array_layer(0)
                        .layer_count(1),
                );
            unsafe { device.create_image_view(&info, None) }.expect("create_image_view")
        })
        .collect();

    (
        swapchain,
        chosen_format.format,
        chosen_format.color_space,
        extent,
        images,
        views,
    )
}

fn create_frames(
    device: &Device,
    queue_family_index: u32,
) -> [FrameSync; FRAMES_IN_FLIGHT] {
    std::array::from_fn(|_| {
        let semaphore_info = vk::SemaphoreCreateInfo::default();
        let fence_info =
            vk::FenceCreateInfo::default().flags(vk::FenceCreateFlags::SIGNALED);
        let pool_info = vk::CommandPoolCreateInfo::default()
            .queue_family_index(queue_family_index)
            .flags(vk::CommandPoolCreateFlags::TRANSIENT);

        unsafe {
            let image_available = device
                .create_semaphore(&semaphore_info, None)
                .expect("create_semaphore");
            let render_finished = device
                .create_semaphore(&semaphore_info, None)
                .expect("create_semaphore");
            let in_flight = device
                .create_fence(&fence_info, None)
                .expect("create_fence");
            let cmd_pool = device
                .create_command_pool(&pool_info, None)
                .expect("create_command_pool");
            let alloc_info = vk::CommandBufferAllocateInfo::default()
                .command_pool(cmd_pool)
                .level(vk::CommandBufferLevel::PRIMARY)
                .command_buffer_count(1);
            let cmd_buffer = device
                .allocate_command_buffers(&alloc_info)
                .expect("allocate_command_buffers")[0];

            FrameSync {
                image_available,
                render_finished,
                in_flight,
                cmd_pool,
                cmd_buffer,
            }
        }
    })
}