aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/pugl/examples/pugl_vulkan_cxx_demo.cpp
blob: e816091f847ef9c5fca197accfe51ba0b27ebe86 (plain)
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
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
/*
  Copyright 2019-2020 David Robillard <d@drobilla.net>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
  An example of drawing with Vulkan.

  This is an example of using Vulkan for pixel-perfect 2D drawing.  It uses
  the same data and shaders as pugl_shader_demo.c and attempts to draw the
  same thing, except using Vulkan.

  Since Vulkan is a complicated and very verbose API, this example is
  unfortunately much larger than the others.  You should not use this as a
  resource to learn Vulkan, but it provides a decent demo of using Vulkan with
  Pugl that works nicely on all supported platforms.
*/

#include "demo_utils.h"
#include "file_utils.h"
#include "rects.h"
#include "test/test_utils.h"

#include "sybok.hpp"

#include "pugl/pugl.h"
#include "pugl/pugl.hpp"
#include "pugl/vulkan.hpp"

#include <vulkan/vk_platform.h>

#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

namespace {

constexpr uintptr_t resizeTimerId = 1u;

struct PhysicalDeviceSelection {
  sk::PhysicalDevice physicalDevice;
  uint32_t           graphicsFamilyIndex;
};

/// Basic Vulkan context associated with the window
struct VulkanContext {
  VkResult init(pugl::VulkanLoader& loader, const PuglTestOptions& opts);

  sk::VulkanApi              vk;
  sk::Instance               instance;
  sk::DebugReportCallbackEXT debugCallback;
};

/// Basic setup of graphics device
struct GraphicsDevice {
  VkResult init(const pugl::VulkanLoader& loader,
                const VulkanContext&      context,
                pugl::View&               view,
                const PuglTestOptions&    opts);

  sk::SurfaceKHR     surface;
  sk::PhysicalDevice physicalDevice{};
  uint32_t           graphicsIndex{};
  VkSurfaceFormatKHR surfaceFormat{};
  VkPresentModeKHR   presentMode{};
  VkPresentModeKHR   resizePresentMode{};
  sk::Device         device{};
  sk::Queue          graphicsQueue{};
  sk::CommandPool    commandPool{};
};

/// Buffer allocated on the GPU
struct Buffer {
  VkResult init(const sk::VulkanApi&  vk,
                const GraphicsDevice& gpu,
                VkDeviceSize          size,
                VkBufferUsageFlags    usage,
                VkMemoryPropertyFlags properties);

  sk::Buffer       buffer;
  sk::DeviceMemory deviceMemory;
};

/// A set of frames that can be rendered concurrently
struct Swapchain {
  VkResult init(const sk::VulkanApi&     vk,
                const GraphicsDevice&    gpu,
                VkSurfaceCapabilitiesKHR capabilities,
                VkExtent2D               extent,
                VkSwapchainKHR           oldSwapchain,
                bool                     resizing);

  VkSurfaceCapabilitiesKHR   capabilities{};
  VkExtent2D                 extent{};
  sk::SwapchainKHR           swapchain{};
  std::vector<sk::ImageView> imageViews{};
};

/// A pass that renders to a target
struct RenderPass {
  VkResult init(const sk::VulkanApi&  vk,
                const GraphicsDevice& gpu,
                const Swapchain&      swapchain);

  sk::RenderPass                                   renderPass;
  std::vector<sk::Framebuffer>                     framebuffers;
  sk::CommandBuffers<std::vector<VkCommandBuffer>> commandBuffers;
};

/// Uniform buffer for constant data used in shaders
struct UniformBufferObject {
  mat4 projection;
};

/// Rectangle data that does not depend on renderer configuration
struct RectData {
  VkResult init(const sk::VulkanApi&  vk,
                const GraphicsDevice& gpu,
                size_t                nRects);

  sk::DescriptorSetLayout descriptorSetLayout{};
  Buffer                  uniformBuffer{};
  sk::MappedMemory        uniformData{};
  Buffer                  modelBuffer{};
  Buffer                  instanceBuffer{};
  sk::MappedMemory        vertexData{};
  size_t                  numRects{};
};

/// Shader modules for drawing rectangles
struct RectShaders {
  VkResult init(const sk::VulkanApi&  vk,
                const GraphicsDevice& gpu,
                const std::string&    programPath);

  sk::ShaderModule vert{};
  sk::ShaderModule frag{};
};

/// A pipeline to render rectangles with our shaders
struct RectPipeline {
  VkResult init(const sk::VulkanApi&  vk,
                const GraphicsDevice& gpu,
                const RectData&       rectData,
                const RectShaders&    shaders,
                const Swapchain&      swapchain,
                const RenderPass&     renderPass);

  sk::DescriptorPool                               descriptorPool{};
  sk::DescriptorSets<std::vector<VkDescriptorSet>> descriptorSets{};
  sk::PipelineLayout                               pipelineLayout{};
  std::array<sk::Pipeline, 1>                      pipelines{};
  uint32_t                                         numImages{};
};

/// Synchronization primitives used to coordinate drawing frames
struct RenderSync {
  VkResult init(const sk::VulkanApi& vk,
                const sk::Device&    device,
                uint32_t             numImages);

  std::vector<sk::Semaphore> imageAvailable{};
  std::vector<sk::Semaphore> renderFinished{};
  std::vector<sk::Fence>     inFlight{};
  size_t                     currentFrame{};
};

/// Renderer that owns the above and everything required to draw
struct Renderer {
  VkResult init(const sk::VulkanApi&  vk,
                const GraphicsDevice& gpu,
                const RectData&       rectData,
                const RectShaders&    rectShaders,
                VkExtent2D            extent,
                bool                  resizing);

  VkResult recreate(const sk::VulkanApi&  vk,
                    const sk::SurfaceKHR& surface,
                    const GraphicsDevice& gpu,
                    const RectData&       rectData,
                    const RectShaders&    rectShaders,
                    VkExtent2D            extent,
                    bool                  resizing);

  Swapchain    swapchain;
  RenderPass   renderPass;
  RectPipeline rectPipeline;
  RenderSync   sync;
};

VkResult
selectSurfaceFormat(const sk::VulkanApi&      vk,
                    const sk::PhysicalDevice& physicalDevice,
                    const sk::SurfaceKHR&     surface,
                    VkSurfaceFormatKHR&       surfaceFormat)
{
  std::vector<VkSurfaceFormatKHR> formats;
  if (VkResult r = vk.getPhysicalDeviceSurfaceFormatsKHR(
        physicalDevice, surface, formats)) {
    return r;
  }

  for (const auto& format : formats) {
    if (format.format == VK_FORMAT_B8G8R8A8_UNORM &&
        format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
      surfaceFormat = format;
      return VK_SUCCESS;
    }
  }

  return VK_ERROR_FORMAT_NOT_SUPPORTED;
}

VkResult
selectPresentMode(const sk::VulkanApi&      vk,
                  const sk::PhysicalDevice& physicalDevice,
                  const sk::SurfaceKHR&     surface,
                  const bool                multiBuffer,
                  const bool                sync,
                  VkPresentModeKHR&         presentMode)
{
  // Map command line options to mode priorities
  static constexpr VkPresentModeKHR priorities[][2][4] = {
    {
      // No double buffer, no sync
      {VK_PRESENT_MODE_IMMEDIATE_KHR,
       VK_PRESENT_MODE_MAILBOX_KHR,
       VK_PRESENT_MODE_FIFO_RELAXED_KHR,
       VK_PRESENT_MODE_FIFO_KHR},

      // No double buffer, sync (nonsense, map to FIFO relaxed)
      {VK_PRESENT_MODE_FIFO_RELAXED_KHR,
       VK_PRESENT_MODE_FIFO_KHR,
       VK_PRESENT_MODE_MAILBOX_KHR,
       VK_PRESENT_MODE_IMMEDIATE_KHR},
    },
    {
      // Double buffer, no sync
      {
        VK_PRESENT_MODE_MAILBOX_KHR,
        VK_PRESENT_MODE_IMMEDIATE_KHR,
        VK_PRESENT_MODE_FIFO_RELAXED_KHR,
        VK_PRESENT_MODE_FIFO_KHR,
      },

      // Double buffer, sync
      {VK_PRESENT_MODE_FIFO_KHR,
       VK_PRESENT_MODE_FIFO_RELAXED_KHR,
       VK_PRESENT_MODE_MAILBOX_KHR,
       VK_PRESENT_MODE_IMMEDIATE_KHR},
    },
  };

  std::vector<VkPresentModeKHR> modes;
  if (VkResult r = vk.getPhysicalDeviceSurfacePresentModesKHR(
        physicalDevice, surface, modes)) {
    return r;
  }

  const auto& tryModes = priorities[bool(multiBuffer)][bool(sync)];
  for (const auto m : tryModes) {
    if (std::find(modes.begin(), modes.end(), m) != modes.end()) {
      presentMode = m;
      return VK_SUCCESS;
    }
  }

  return VK_ERROR_INCOMPATIBLE_DRIVER;
}

VkResult
openDevice(const sk::VulkanApi&      vk,
           const sk::PhysicalDevice& physicalDevice,
           const uint32_t            graphicsFamilyIndex,
           sk::Device&               device)
{
  const float       graphicsQueuePriority = 1.0f;
  const char* const swapchainName         = "VK_KHR_swapchain";

  const VkDeviceQueueCreateInfo queueCreateInfo{
    VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
    nullptr,
    0u,
    graphicsFamilyIndex,
    SK_COUNTED(1u, &graphicsQueuePriority),
  };

  const VkDeviceCreateInfo createInfo{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
                                      nullptr,
                                      0u,
                                      SK_COUNTED(1u, &queueCreateInfo),
                                      SK_COUNTED(0u, nullptr), // Deprecated
                                      SK_COUNTED(1u, &swapchainName),
                                      nullptr};

  return vk.createDevice(physicalDevice, createInfo, device);
}

/// Return whether the physical device supports the extensions we require
VkResult
deviceSupportsRequiredExtensions(const sk::VulkanApi&      vk,
                                 const sk::PhysicalDevice& device,
                                 bool&                     supported)
{
  VkResult r = VK_SUCCESS;

  std::vector<VkExtensionProperties> props;
  if ((r = vk.enumerateDeviceExtensionProperties(device, props))) {
    return r;
  }

  supported = std::any_of(
    props.begin(), props.end(), [&](const VkExtensionProperties& e) {
      return !strcmp(e.extensionName, "VK_KHR_swapchain");
    });

  return VK_SUCCESS;
}

/// Return the index of the graphics queue, if there is one
VkResult
findGraphicsQueue(const sk::VulkanApi&      vk,
                  const sk::SurfaceKHR&     surface,
                  const sk::PhysicalDevice& device,
                  uint32_t&                 queueIndex)
{
  VkResult r = VK_SUCCESS;

  std::vector<VkQueueFamilyProperties> queueProps;
  if ((r = vk.getPhysicalDeviceQueueFamilyProperties(device, queueProps))) {
    return r;
  }

  for (uint32_t q = 0u; q < queueProps.size(); ++q) {
    if (queueProps[q].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
      bool supported = false;
      if ((r = vk.getPhysicalDeviceSurfaceSupportKHR(
             device, q, surface, supported))) {
        return r;
      }

      if (supported) {
        queueIndex = q;
        return VK_SUCCESS;
      }
    }
  }

  return VK_ERROR_FEATURE_NOT_PRESENT;
}

/// Select a physical graphics device to use (simply the first found)
VkResult
selectPhysicalDevice(const sk::VulkanApi&     vk,
                     const sk::Instance&      instance,
                     const sk::SurfaceKHR&    surface,
                     PhysicalDeviceSelection& selection)
{
  VkResult r = VK_SUCCESS;

  std::vector<sk::PhysicalDevice> devices;
  if ((r = vk.enumeratePhysicalDevices(instance, devices))) {
    return r;
  }

  for (const auto& device : devices) {
    auto supported = false;
    if ((r = deviceSupportsRequiredExtensions(vk, device, supported))) {
      return r;
    }

    if (supported) {
      auto queueIndex = 0u;
      if ((r = findGraphicsQueue(vk, surface, device, queueIndex))) {
        return r;
      }

      selection = PhysicalDeviceSelection{device, queueIndex};
      return VK_SUCCESS;
    }
  }

  return VK_ERROR_INCOMPATIBLE_DISPLAY_KHR;
}

VkResult
GraphicsDevice::init(const pugl::VulkanLoader& loader,
                     const VulkanContext&      context,
                     pugl::View&               view,
                     const PuglTestOptions&    opts)
{
  const auto& vk = context.vk;
  VkResult    r  = VK_SUCCESS;

  // Create a Vulkan surface for the window using the Pugl API
  VkSurfaceKHR surfaceHandle = {};
  if ((r = pugl::createSurface(loader.getInstanceProcAddrFunc(),
                               view,
                               context.instance,
                               nullptr,
                               &surfaceHandle))) {
    return r;
  }

  // Wrap surface in a safe RAII handle
  surface =
    sk::SurfaceKHR{surfaceHandle, {context.instance, vk.vkDestroySurfaceKHR}};

  PhysicalDeviceSelection physicalDeviceSelection = {};
  // Select a physical device to use
  if ((r = selectPhysicalDevice(
         vk, context.instance, surface, physicalDeviceSelection))) {
    return r;
  }

  physicalDevice = physicalDeviceSelection.physicalDevice;
  graphicsIndex  = physicalDeviceSelection.graphicsFamilyIndex;

  if ((r = selectSurfaceFormat(vk, physicalDevice, surface, surfaceFormat)) ||
      (r = selectPresentMode(vk,
                             physicalDevice,
                             surface,
                             opts.doubleBuffer,
                             opts.sync,
                             presentMode)) ||
      (r = selectPresentMode(
         vk, physicalDevice, surface, true, false, resizePresentMode)) ||
      (r = openDevice(vk, physicalDevice, graphicsIndex, device))) {
    return r;
  }

  const VkCommandPoolCreateInfo commandPoolInfo{
    VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, nullptr, {}, graphicsIndex};

  if ((r = vk.createCommandPool(device, commandPoolInfo, commandPool))) {
    return r;
  }

  graphicsQueue = vk.getDeviceQueue(device, graphicsIndex, 0);
  return VK_SUCCESS;
}

uint32_t
findMemoryType(const sk::VulkanApi&         vk,
               const sk::PhysicalDevice&    physicalDevice,
               const uint32_t               typeFilter,
               const VkMemoryPropertyFlags& properties)
{
  VkPhysicalDeviceMemoryProperties memProperties =
    vk.getPhysicalDeviceMemoryProperties(physicalDevice);

  for (uint32_t i = 0; i < memProperties.memoryTypeCount; ++i) {
    if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags &
                                    properties) == properties) {
      return i;
    }
  }

  return UINT32_MAX;
}

VkResult
Buffer::init(const sk::VulkanApi&        vk,
             const GraphicsDevice&       gpu,
             const VkDeviceSize          size,
             const VkBufferUsageFlags    usage,
             const VkMemoryPropertyFlags properties)
{
  const VkBufferCreateInfo bufferInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
                                      nullptr,
                                      {},
                                      size,
                                      usage,
                                      VK_SHARING_MODE_EXCLUSIVE,
                                      SK_COUNTED(0, nullptr)};

  const auto& device = gpu.device;

  VkResult r = VK_SUCCESS;
  if ((r = vk.createBuffer(device, bufferInfo, buffer))) {
    return r;
  }

  const auto requirements    = vk.getBufferMemoryRequirements(device, buffer);
  const auto memoryTypeIndex = findMemoryType(
    vk, gpu.physicalDevice, requirements.memoryTypeBits, properties);

  if (memoryTypeIndex == UINT32_MAX) {
    return VK_ERROR_FEATURE_NOT_PRESENT;
  }

  const VkMemoryAllocateInfo allocInfo{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
                                       nullptr,
                                       requirements.size,
                                       memoryTypeIndex};

  if ((r = vk.allocateMemory(device, allocInfo, deviceMemory)) ||
      (r = vk.bindBufferMemory(device, buffer, deviceMemory, 0))) {
    return r;
  }

  return VK_SUCCESS;
}

VkResult
Swapchain::init(const sk::VulkanApi&           vk,
                const GraphicsDevice&          gpu,
                const VkSurfaceCapabilitiesKHR surfaceCapabilities,
                const VkExtent2D               surfaceExtent,
                VkSwapchainKHR                 oldSwapchain,
                bool                           resizing)
{
  capabilities = surfaceCapabilities;
  extent       = surfaceExtent;

  const auto minNumImages =
    (!capabilities.maxImageCount || capabilities.maxImageCount >= 3u)
      ? 3u
      : capabilities.maxImageCount;

  const VkSwapchainCreateInfoKHR swapchainCreateInfo{
    VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
    nullptr,
    {},
    gpu.surface,
    minNumImages,
    gpu.surfaceFormat.format,
    gpu.surfaceFormat.colorSpace,
    surfaceExtent,
    1,
    (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT),
    VK_SHARING_MODE_EXCLUSIVE,
    SK_COUNTED(0, nullptr),
    capabilities.currentTransform,
    VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
    resizing ? gpu.resizePresentMode : gpu.presentMode,
    VK_TRUE,
    oldSwapchain};

  VkResult             r = VK_SUCCESS;
  std::vector<VkImage> images;
  if ((r = vk.createSwapchainKHR(gpu.device, swapchainCreateInfo, swapchain)) ||
      (r = vk.getSwapchainImagesKHR(gpu.device, swapchain, images))) {
    return r;
  }

  imageViews = std::vector<sk::ImageView>(images.size());
  for (size_t i = 0; i < images.size(); ++i) {
    const VkImageViewCreateInfo imageViewCreateInfo{
      VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
      nullptr,
      {},
      images[i],
      VK_IMAGE_VIEW_TYPE_2D,
      gpu.surfaceFormat.format,
      {},
      {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};

    if ((r = vk.createImageView(
           gpu.device, imageViewCreateInfo, imageViews[i]))) {
      return r;
    }
  }

  return VK_SUCCESS;
}

VkResult
RenderPass::init(const sk::VulkanApi&  vk,
                 const GraphicsDevice& gpu,
                 const Swapchain&      swapchain)
{
  const auto numImages = static_cast<uint32_t>(swapchain.imageViews.size());

  assert(numImages > 0);

  // Create command buffers
  const VkCommandBufferAllocateInfo commandBufferAllocateInfo{
    VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
    nullptr,
    gpu.commandPool,
    VK_COMMAND_BUFFER_LEVEL_PRIMARY,
    numImages};

  VkResult r = VK_SUCCESS;
  if ((r = vk.allocateCommandBuffers(
         gpu.device, commandBufferAllocateInfo, commandBuffers))) {
    return r;
  }

  static constexpr VkAttachmentReference colorAttachmentRef{
    0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};

  static constexpr VkSubpassDescription subpass{
    {},
    VK_PIPELINE_BIND_POINT_GRAPHICS,
    SK_COUNTED(0, nullptr),
    SK_COUNTED(1, &colorAttachmentRef, nullptr, nullptr),
    SK_COUNTED(0u, nullptr)};

  static constexpr VkSubpassDependency dependency{
    VK_SUBPASS_EXTERNAL,
    0,
    VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
    VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
    (VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
     VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT),
    {},
    {}};

  const VkAttachmentDescription colorAttachment{
    {},
    gpu.surfaceFormat.format,
    VK_SAMPLE_COUNT_1_BIT,
    VK_ATTACHMENT_LOAD_OP_CLEAR,
    VK_ATTACHMENT_STORE_OP_STORE,
    VK_ATTACHMENT_LOAD_OP_DONT_CARE,
    VK_ATTACHMENT_STORE_OP_DONT_CARE,
    VK_IMAGE_LAYOUT_UNDEFINED,
    VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  };

  const VkRenderPassCreateInfo renderPassCreateInfo{
    VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
    nullptr,
    {},
    SK_COUNTED(1, &colorAttachment),
    SK_COUNTED(1, &subpass),
    SK_COUNTED(1, &dependency)};

  if ((r = vk.createRenderPass(gpu.device, renderPassCreateInfo, renderPass))) {
    return r;
  }

  // Create framebuffers
  framebuffers = std::vector<sk::Framebuffer>(numImages);
  for (uint32_t i = 0; i < numImages; ++i) {
    const VkFramebufferCreateInfo framebufferCreateInfo{
      VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
      nullptr,
      {},
      renderPass,
      SK_COUNTED(1, &swapchain.imageViews[i].get()),
      swapchain.extent.width,
      swapchain.extent.height,
      1};

    if ((r = vk.createFramebuffer(
           gpu.device, framebufferCreateInfo, framebuffers[i]))) {
      return r;
    }
  }

  return VK_SUCCESS;
}

std::vector<uint32_t>
readFile(const char* const programPath, const std::string& filename)
{
  std::unique_ptr<char, decltype(&free)> path{
    resourcePath(programPath, filename.c_str()), &free};

  std::cerr << "Loading shader:           " << path.get() << std::endl;

  std::unique_ptr<FILE, decltype(&fclose)> file{fopen(path.get(), "rb"),
                                                &fclose};

  if (!file) {
    std::cerr << "Failed to open file '" << filename << "'\n";
    return {};
  }

  fseek(file.get(), 0, SEEK_END);
  const auto fileSize = static_cast<size_t>(ftell(file.get()));
  fseek(file.get(), 0, SEEK_SET);

  const auto            numWords = fileSize / sizeof(uint32_t);
  std::vector<uint32_t> buffer(numWords);

  fread(buffer.data(), sizeof(uint32_t), numWords, file.get());

  return buffer;
}

VkResult
createShaderModule(const sk::VulkanApi&         vk,
                   const GraphicsDevice&        gpu,
                   const std::vector<uint32_t>& code,
                   sk::ShaderModule&            shaderModule)
{
  const VkShaderModuleCreateInfo createInfo{
    VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
    nullptr,
    {},
    code.size() * sizeof(uint32_t),
    code.data()};

  return vk.createShaderModule(gpu.device, createInfo, shaderModule);
}

VkResult
RectShaders::init(const sk::VulkanApi&  vk,
                  const GraphicsDevice& gpu,
                  const std::string&    programPath)
{
  auto vertShaderCode = readFile(programPath.c_str(), "shaders/rect.vert.spv");

  auto fragShaderCode = readFile(programPath.c_str(), "shaders/rect.frag.spv");

  if (vertShaderCode.empty() || fragShaderCode.empty()) {
    return VK_ERROR_INITIALIZATION_FAILED;
  }

  VkResult r = VK_SUCCESS;
  if ((r = createShaderModule(vk, gpu, vertShaderCode, vert)) ||
      (r = createShaderModule(vk, gpu, fragShaderCode, frag))) {
    return r;
  }

  return VK_SUCCESS;
}

VkResult
RectPipeline::init(const sk::VulkanApi&  vk,
                   const GraphicsDevice& gpu,
                   const RectData&       rectData,
                   const RectShaders&    shaders,
                   const Swapchain&      swapchain,
                   const RenderPass&     renderPass)
{
  const auto oldNumImages = numImages;
  VkResult   r            = VK_SUCCESS;

  numImages      = static_cast<uint32_t>(swapchain.imageViews.size());
  pipelines      = {};
  pipelineLayout = {};
  descriptorSets = {};

  if (numImages != oldNumImages) {
    // Create layout descriptor pool

    const VkDescriptorPoolSize poolSize{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
                                        numImages};

    const VkDescriptorPoolCreateInfo descriptorPoolCreateInfo{
      VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
      nullptr,
      VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
      numImages,
      1u,
      &poolSize};
    if ((r = vk.createDescriptorPool(
           gpu.device, descriptorPoolCreateInfo, descriptorPool))) {
      return r;
    }
  }

  const std::vector<VkDescriptorSetLayout> layouts(
    numImages, rectData.descriptorSetLayout.get());

  const VkDescriptorSetAllocateInfo descriptorSetAllocateInfo{
    VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
    nullptr,
    descriptorPool,
    numImages,
    layouts.data()};
  if ((r = vk.allocateDescriptorSets(
         gpu.device, descriptorSetAllocateInfo, descriptorSets))) {
    return r;
  }

  const VkDescriptorBufferInfo bufferInfo{
    rectData.uniformBuffer.buffer, 0, sizeof(UniformBufferObject)};

  const std::array<VkWriteDescriptorSet, 1> descriptorWrites{
    {{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
      nullptr,
      descriptorSets[0],
      0,
      0,
      1,
      VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
      nullptr,
      &bufferInfo,
      nullptr}}};

  const std::array<VkCopyDescriptorSet, 0> descriptorCopies{};

  vk.updateDescriptorSets(gpu.device, descriptorWrites, descriptorCopies);

  static constexpr std::array<VkVertexInputAttributeDescription, 4>
    vertexAttributeDescriptions{
      {// Model
       {0u, 0u, VK_FORMAT_R32G32_SFLOAT, 0},

       // Rect instance attributes
       {1u, 1u, VK_FORMAT_R32G32_SFLOAT, offsetof(Rect, pos)},
       {2u, 1u, VK_FORMAT_R32G32_SFLOAT, offsetof(Rect, size)},
       {3u, 1u, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(Rect, fillColor)}}};

  static constexpr std::array<VkVertexInputBindingDescription, 2>
    vertexBindingDescriptions{
      VkVertexInputBindingDescription{
        0, sizeof(vec2), VK_VERTEX_INPUT_RATE_VERTEX},
      VkVertexInputBindingDescription{
        1u, sizeof(Rect), VK_VERTEX_INPUT_RATE_INSTANCE}};

  static constexpr VkPipelineInputAssemblyStateCreateInfo inputAssembly{
    VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
    nullptr,
    {},
    VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
    false};

  static constexpr VkPipelineRasterizationStateCreateInfo rasterizer{
    VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
    nullptr,
    {},
    0,
    0,
    VK_POLYGON_MODE_FILL,
    VK_CULL_MODE_BACK_BIT,
    VK_FRONT_FACE_CLOCKWISE,
    0,
    0,
    0,
    0,
    1.0f};

  static constexpr VkPipelineMultisampleStateCreateInfo multisampling{
    VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
    nullptr,
    {},
    VK_SAMPLE_COUNT_1_BIT,
    false,
    0.0f,
    nullptr,
    false,
    false};

  static constexpr VkPipelineColorBlendAttachmentState colorBlendAttachment{
    true,
    VK_BLEND_FACTOR_SRC_ALPHA,
    VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
    VK_BLEND_OP_ADD,
    VK_BLEND_FACTOR_ONE,
    VK_BLEND_FACTOR_ZERO,
    VK_BLEND_OP_ADD,
    (VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
     VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT)};

  const VkPipelineShaderStageCreateInfo shaderStages[] = {
    {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
     nullptr,
     {},
     VK_SHADER_STAGE_VERTEX_BIT,
     shaders.vert.get(),
     "main",
     nullptr},
    {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
     nullptr,
     {},
     VK_SHADER_STAGE_FRAGMENT_BIT,
     shaders.frag.get(),
     "main",
     nullptr}};

  const VkPipelineVertexInputStateCreateInfo vertexInputInfo{
    VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
    nullptr,
    {},
    SK_COUNTED(static_cast<uint32_t>(vertexBindingDescriptions.size()),
               vertexBindingDescriptions.data()),
    SK_COUNTED(static_cast<uint32_t>(vertexAttributeDescriptions.size()),
               vertexAttributeDescriptions.data())};

  const VkViewport viewport{0.0f,
                            0.0f,
                            float(swapchain.extent.width),
                            float(swapchain.extent.height),
                            0.0f,
                            1.0f};

  const VkRect2D scissor{{0, 0}, swapchain.extent};

  const VkPipelineViewportStateCreateInfo viewportState{
    VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
    nullptr,
    {},
    SK_COUNTED(1, &viewport),
    SK_COUNTED(1, &scissor)};

  const VkPipelineColorBlendStateCreateInfo colorBlending{
    VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
    nullptr,
    {},
    false,
    VK_LOGIC_OP_COPY,
    SK_COUNTED(1, &colorBlendAttachment),
    {1.0f, 0.0f, 0.0f, 0.0f}};

  const VkPipelineLayoutCreateInfo layoutInfo{
    VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
    nullptr,
    {},
    SK_COUNTED(1, &rectData.descriptorSetLayout.get()),
    SK_COUNTED(0, nullptr)};

  if ((r = vk.createPipelineLayout(gpu.device, layoutInfo, pipelineLayout))) {
    return r;
  }

  const std::array<VkGraphicsPipelineCreateInfo, 1> pipelineInfos{
    {{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
      nullptr,
      {},
      SK_COUNTED(2, shaderStages),
      &vertexInputInfo,
      &inputAssembly,
      nullptr,
      &viewportState,
      &rasterizer,
      &multisampling,
      nullptr,
      &colorBlending,
      nullptr,
      pipelineLayout,
      renderPass.renderPass,
      0u,
      {},
      0}}};

  if ((r = vk.createGraphicsPipelines(
         gpu.device, {}, pipelineInfos, pipelines))) {
    return r;
  }

  return VK_SUCCESS;
}

VkResult
RectData::init(const sk::VulkanApi&  vk,
               const GraphicsDevice& gpu,
               const size_t          nRects)
{
  numRects = nRects;

  static constexpr VkDescriptorSetLayoutBinding uboLayoutBinding{
    0,
    VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
    1,
    VK_SHADER_STAGE_VERTEX_BIT,
    nullptr};

  const VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo{
    VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
    nullptr,
    {},
    1,
    &uboLayoutBinding};

  VkResult r = VK_SUCCESS;
  if ((r = vk.createDescriptorSetLayout(
         gpu.device, descriptorSetLayoutInfo, descriptorSetLayout)) ||
      (r = uniformBuffer.init(vk,
                              gpu,
                              sizeof(UniformBufferObject),
                              VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
                              VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
                                VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) ||
      (r = vk.mapMemory(gpu.device,
                        uniformBuffer.deviceMemory,
                        0,
                        sizeof(UniformBufferObject),
                        {},
                        uniformData))) {
    return r;
  }

  const VkBufferUsageFlags usageFlags =
    (VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
     VK_BUFFER_USAGE_TRANSFER_DST_BIT);

  const VkMemoryPropertyFlags propertyFlags =
    (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
     VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);

  if ((r = modelBuffer.init(
         vk, gpu, sizeof(rectVertices), usageFlags, propertyFlags))) {
    return r;
  }

  {
    // Copy model vertices (directly, we do this only once)
    sk::MappedMemory modelData;
    if ((r = vk.mapMemory(gpu.device,
                          modelBuffer.deviceMemory,
                          0,
                          static_cast<VkDeviceSize>(sizeof(rectVertices)),
                          {},
                          modelData))) {
      return r;
    }

    memcpy(modelData.get(), rectVertices, sizeof(rectVertices));
  }

  if ((r = instanceBuffer.init(
         vk, gpu, sizeof(Rect) * numRects, usageFlags, propertyFlags))) {
    return r;
  }

  // Map attribute vertices (we will update them every frame)
  const auto rectsSize = static_cast<VkDeviceSize>(sizeof(Rect) * numRects);
  if ((r = vk.mapMemory(gpu.device,
                        instanceBuffer.deviceMemory,
                        0,
                        rectsSize,
                        {},
                        vertexData))) {
    return r;
  }

  return VK_SUCCESS;
}

VkResult
RenderSync::init(const sk::VulkanApi& vk,
                 const sk::Device&    device,
                 const uint32_t       numImages)
{
  const auto maxInFlight = std::max(1u, numImages - 1u);
  VkResult   r           = VK_SUCCESS;

  imageAvailable = std::vector<sk::Semaphore>(numImages);
  renderFinished = std::vector<sk::Semaphore>(numImages);
  for (uint32_t i = 0; i < numImages; ++i) {
    static constexpr VkSemaphoreCreateInfo semaphoreInfo{
      VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, {}};

    if ((r = vk.createSemaphore(device, semaphoreInfo, imageAvailable[i])) ||
        (r = vk.createSemaphore(device, semaphoreInfo, renderFinished[i]))) {
      return r;
    }
  }

  inFlight = std::vector<sk::Fence>(maxInFlight);
  for (uint32_t i = 0; i < maxInFlight; ++i) {
    static constexpr VkFenceCreateInfo fenceInfo{
      VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
      nullptr,
      VK_FENCE_CREATE_SIGNALED_BIT};

    if ((r = vk.createFence(device, fenceInfo, inFlight[i]))) {
      return r;
    }
  }

  return VK_SUCCESS;
}

VkResult
Renderer::init(const sk::VulkanApi&  vk,
               const GraphicsDevice& gpu,
               const RectData&       rectData,
               const RectShaders&    rectShaders,
               const VkExtent2D      extent,
               bool                  resizing)
{
  VkResult                 r            = VK_SUCCESS;
  VkSurfaceCapabilitiesKHR capabilities = {};

  if ((r = vk.getPhysicalDeviceSurfaceCapabilitiesKHR(
         gpu.physicalDevice, gpu.surface, capabilities)) ||
      (r = swapchain.init(vk, gpu, capabilities, extent, {}, resizing)) ||
      (r = renderPass.init(vk, gpu, swapchain)) ||
      (r = rectPipeline.init(
         vk, gpu, rectData, rectShaders, swapchain, renderPass))) {
    return r;
  }

  const auto numFrames = static_cast<uint32_t>(swapchain.imageViews.size());
  return sync.init(vk, gpu.device, numFrames);
}

VkResult
Renderer::recreate(const sk::VulkanApi&  vk,
                   const sk::SurfaceKHR& surface,
                   const GraphicsDevice& gpu,
                   const RectData&       rectData,
                   const RectShaders&    rectShaders,
                   const VkExtent2D      extent,
                   bool                  resizing)
{
  VkResult   r            = VK_SUCCESS;
  const auto oldNumImages = swapchain.imageViews.size();

  VkSurfaceCapabilitiesKHR capabilities = {};
  if ((r = vk.getPhysicalDeviceSurfaceCapabilitiesKHR(
         gpu.physicalDevice, surface, capabilities)) ||
      (r = swapchain.init(
         vk, gpu, capabilities, extent, swapchain.swapchain, resizing)) ||
      (r = renderPass.init(vk, gpu, swapchain)) ||
      (r = rectPipeline.init(
         vk, gpu, rectData, rectShaders, swapchain, renderPass))) {
    return r;
  }

  const auto numFrames = static_cast<uint32_t>(swapchain.imageViews.size());
  if (swapchain.imageViews.size() != oldNumImages) {
    return sync.init(vk, gpu.device, numFrames);
  }

  return VK_SUCCESS;
}

VKAPI_ATTR
VkBool32 VKAPI_CALL
debugCallback(VkDebugReportFlagsEXT flags,
              VkDebugReportObjectTypeEXT,
              uint64_t,
              size_t,
              int32_t,
              const char* layerPrefix,
              const char* msg,
              void*)
{
  std::cerr << sk::string(static_cast<VkDebugReportFlagBitsEXT>(flags)) << ": "
            << layerPrefix << ": " << msg << std::endl;

  return VK_FALSE;
}

bool
hasExtension(const char*                               name,
             const std::vector<VkExtensionProperties>& properties)
{
  for (const auto& p : properties) {
    if (!strcmp(p.extensionName, name)) {
      return true;
    }
  }

  return false;
}

bool
hasLayer(const char* name, const std::vector<VkLayerProperties>& properties)
{
  for (const auto& p : properties) {
    if (!strcmp(p.layerName, name)) {
      return true;
    }
  }

  return false;
}

template<class Value>
void
logInfo(const char* heading, const Value& value)
{
  std::cout << std::setw(26) << std::left << (std::string(heading) + ":")
            << value << std::endl;
}

VkResult
createInstance(sk::VulkanInitApi&     initApi,
               const PuglTestOptions& opts,
               sk::Instance&          instance)
{
  VkResult r = VK_SUCCESS;

  std::vector<VkLayerProperties>     layerProps;
  std::vector<VkExtensionProperties> extProps;
  if ((r = initApi.enumerateInstanceLayerProperties(layerProps)) ||
      (r = initApi.enumerateInstanceExtensionProperties(extProps))) {
    return r;
  }

  const auto puglExtensions = pugl::getInstanceExtensions();
  auto       extensions =
    std::vector<const char*>(puglExtensions.begin(), puglExtensions.end());

  // Add extra extensions we want to use if they are supported
  if (hasExtension("VK_EXT_debug_report", extProps)) {
    extensions.push_back("VK_EXT_debug_report");
  }

  // Add validation layers if error checking is enabled
  std::vector<const char*> layers;
  if (opts.errorChecking) {
    for (const char* l : {"VK_LAYER_KHRONOS_validation",
                          "VK_LAYER_LUNARG_standard_validation"}) {
      if (hasLayer(l, layerProps)) {
        layers.push_back(l);
      }
    }
  }

  for (const auto& e : extensions) {
    logInfo("Using instance extension", e);
  }

  for (const auto& l : layers) {
    logInfo("Using instance layer", l);
  }

  static constexpr VkApplicationInfo appInfo{
    VK_STRUCTURE_TYPE_APPLICATION_INFO,
    nullptr,
    "Pugl Vulkan Demo",
    0,
    nullptr,
    0,
    VK_MAKE_VERSION(1, 0, 0),
  };

  const VkInstanceCreateInfo createInfo{
    VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
    nullptr,
    VkInstanceCreateFlags{},
    &appInfo,
    SK_COUNTED(uint32_t(layers.size()), layers.data()),
    SK_COUNTED(uint32_t(extensions.size()), extensions.data())};

  return initApi.createInstance(createInfo, instance);
}

VkResult
getDebugReportCallback(sk::VulkanApi&              api,
                       sk::Instance&               instance,
                       const bool                  verbose,
                       sk::DebugReportCallbackEXT& callback)
{
  if (api.vkCreateDebugReportCallbackEXT) {
    VkDebugReportFlagsEXT flags = (VK_DEBUG_REPORT_WARNING_BIT_EXT |
                                   VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
                                   VK_DEBUG_REPORT_ERROR_BIT_EXT);

    if (verbose) {
      flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
      flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
    }

    const VkDebugReportCallbackCreateInfoEXT createInfo{
      VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
      nullptr,
      flags,
      debugCallback,
      nullptr};

    return api.createDebugReportCallbackEXT(instance, createInfo, callback);
  }

  return VK_ERROR_FEATURE_NOT_PRESENT;
}

void
recordCommandBuffer(sk::CommandScope&   cmd,
                    const Swapchain&    swapchain,
                    const RenderPass&   renderPass,
                    const RectPipeline& rectPipeline,
                    const RectData&     rectData,
                    const size_t        imageIndex)
{
  const VkClearColorValue clearColorValue{{0.0f, 0.0f, 0.0f, 1.0f}};
  const VkClearValue      clearValue{clearColorValue};

  const VkRenderPassBeginInfo renderPassBegin{
    VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
    nullptr,
    renderPass.renderPass,
    renderPass.framebuffers[imageIndex],
    VkRect2D{{0, 0}, swapchain.extent},
    SK_COUNTED(1, &clearValue)};

  auto pass = cmd.beginRenderPass(renderPassBegin, VK_SUBPASS_CONTENTS_INLINE);

  pass.bindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, rectPipeline.pipelines[0]);

  const std::array<VkDeviceSize, 1> offsets{0};
  pass.bindVertexBuffers(
    0u, SK_COUNTED(1u, &rectData.modelBuffer.buffer.get(), offsets.data()));

  pass.bindVertexBuffers(
    1u, SK_COUNTED(1u, &rectData.instanceBuffer.buffer.get(), offsets.data()));

  pass.bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS,
                          rectPipeline.pipelineLayout,
                          0u,
                          SK_COUNTED(1u, rectPipeline.descriptorSets.get()),
                          0u,
                          nullptr);

  pass.draw(4u, static_cast<uint32_t>(rectData.numRects), 0u, 0u);
}

VkResult
recordCommandBuffers(const sk::VulkanApi& vk,
                     const Swapchain&     swapchain,
                     const RenderPass&    renderPass,
                     const RectPipeline&  rectPipeline,
                     const RectData&      rectData)
{
  VkResult r = VK_SUCCESS;

  for (size_t i = 0; i < swapchain.imageViews.size(); ++i) {
    const VkCommandBufferBeginInfo beginInfo{
      VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
      nullptr,
      VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
      nullptr};

    auto* const commandBuffer = renderPass.commandBuffers[i];
    auto        cmd           = vk.beginCommandBuffer(commandBuffer, beginInfo);
    if (!cmd) {
      return cmd.error();
    }

    recordCommandBuffer(cmd, swapchain, renderPass, rectPipeline, rectData, i);

    if ((r = cmd.end())) {
      return r;
    }
  }

  return VK_SUCCESS;
}

class PuglVulkanDemo;

class View : public pugl::View
{
public:
  View(pugl::World& world, PuglVulkanDemo& app)
    : pugl::View{world}
    , _app{app}
  {
    setEventHandler(*this);
  }

  template<PuglEventType t, class Base>
  pugl::Status onEvent(const pugl::Event<t, Base>&) noexcept
  {
    return pugl::Status::success;
  }

  pugl::Status onEvent(const pugl::ConfigureEvent& event);
  pugl::Status onEvent(const pugl::UpdateEvent& event);
  pugl::Status onEvent(const pugl::ExposeEvent& event);
  pugl::Status onEvent(const pugl::LoopEnterEvent& event);
  pugl::Status onEvent(const pugl::TimerEvent& event);
  pugl::Status onEvent(const pugl::LoopLeaveEvent& event);
  pugl::Status onEvent(const pugl::KeyPressEvent& event);
  pugl::Status onEvent(const pugl::CloseEvent& event);

private:
  PuglVulkanDemo& _app;
};

class PuglVulkanDemo
{
public:
  PuglVulkanDemo(const char*            executablePath,
                 const PuglTestOptions& o,
                 size_t                 numRects);

  const char*        programPath;
  PuglTestOptions    opts;
  pugl::World        world;
  pugl::VulkanLoader loader;
  View               view;
  VulkanContext      vulkan;
  GraphicsDevice     gpu;
  Renderer           renderer;
  RectData           rectData;
  RectShaders        rectShaders;
  uint32_t           framesDrawn{0};
  VkExtent2D         extent{512u, 512u};
  std::vector<Rect>  rects;
  bool               resizing{false};
  bool               quit{false};
};

std::vector<Rect>
makeRects(const size_t numRects, const uint32_t windowWidth)
{
  std::vector<Rect> rects(numRects);
  for (size_t i = 0; i < numRects; ++i) {
    rects[i] = makeRect(i, static_cast<float>(windowWidth));
  }

  return rects;
}

PuglVulkanDemo::PuglVulkanDemo(const char* const      executablePath,
                               const PuglTestOptions& o,
                               const size_t           numRects)
  : programPath{executablePath}
  , opts{o}
  , world{pugl::WorldType::program, pugl::WorldFlag::threads}
  , loader{world}
  , view{world, *this}
  , rects{makeRects(numRects, extent.width)}
{}

VkResult
recreateRenderer(PuglVulkanDemo&       app,
                 const sk::VulkanApi&  vk,
                 const GraphicsDevice& gpu,
                 const VkExtent2D      extent,
                 const RectData&       rectData,
                 const RectShaders&    rectShaders)
{
  VkResult                 r            = VK_SUCCESS;
  VkSurfaceCapabilitiesKHR capabilities = {};
  if ((r = vk.getPhysicalDeviceSurfaceCapabilitiesKHR(
         gpu.physicalDevice, gpu.surface, capabilities))) {
    return r;
  }

  // There is a known race issue here, so we clamp and hope for the best
  const VkExtent2D clampedExtent{
    std::min(capabilities.maxImageExtent.width,
             std::max(capabilities.minImageExtent.width, extent.width)),
    std::min(capabilities.maxImageExtent.height,
             std::max(capabilities.minImageExtent.height, extent.height))};

  if ((r = vk.deviceWaitIdle(gpu.device)) ||
      (r = app.renderer.recreate(vk,
                                 gpu.surface,
                                 gpu,
                                 rectData,
                                 rectShaders,
                                 clampedExtent,
                                 app.resizing))) {
    return r;
  }

  // Reset current (initially signaled) fence because we already waited
  vk.resetFence(gpu.device,
                app.renderer.sync.inFlight[app.renderer.sync.currentFrame]);

  // Record new command buffers
  return recordCommandBuffers(vk,
                              app.renderer.swapchain,
                              app.renderer.renderPass,
                              app.renderer.rectPipeline,
                              rectData);
}

pugl::Status
View::onEvent(const pugl::ConfigureEvent& event)
{
  // We just record the size here and lazily resize the surface when exposed
  _app.extent = {static_cast<uint32_t>(event.width),
                 static_cast<uint32_t>(event.height)};

  return pugl::Status::success;
}

pugl::Status
View::onEvent(const pugl::UpdateEvent&)
{
  return postRedisplay();
}

VkResult
beginFrame(PuglVulkanDemo& app, const sk::Device& device, uint32_t& imageIndex)
{
  const auto& vk = app.vulkan.vk;

  VkResult r = VK_SUCCESS;

  // Wait until we can start rendering the next frame
  if ((r = vk.waitForFence(
         device, app.renderer.sync.inFlight[app.renderer.sync.currentFrame])) ||
      (r = vk.resetFence(
         device, app.renderer.sync.inFlight[app.renderer.sync.currentFrame]))) {
    return r;
  }

  // Rebuild the renderer first if the window size has changed
  if (app.extent.width != app.renderer.swapchain.extent.width ||
      app.extent.height != app.renderer.swapchain.extent.height) {
    if ((r = recreateRenderer(
           app, vk, app.gpu, app.extent, app.rectData, app.rectShaders))) {
      return r;
    }
  }

  // Acquire the next image to render, rebuilding if necessary
  while ((r = vk.acquireNextImageKHR(
            device,
            app.renderer.swapchain.swapchain,
            UINT64_MAX,
            app.renderer.sync.imageAvailable[app.renderer.sync.currentFrame],
            {},
            &imageIndex))) {
    switch (r) {
    case VK_SUBOPTIMAL_KHR:
    case VK_ERROR_OUT_OF_DATE_KHR:
      if ((r = recreateRenderer(app,
                                vk,
                                app.gpu,
                                app.renderer.swapchain.extent,
                                app.rectData,
                                app.rectShaders))) {
        return r;
      }
      continue;
    default:
      return r;
    }
  }

  return VK_SUCCESS;
}

void
update(PuglVulkanDemo& app, const double time)
{
  // Animate rectangles
  for (size_t i = 0; i < app.rects.size(); ++i) {
    moveRect(&app.rects[i],
             i,
             app.rects.size(),
             static_cast<float>(app.extent.width),
             static_cast<float>(app.extent.height),
             time);
  }

  // Update vertex buffer
  memcpy(app.rectData.vertexData.get(),
         app.rects.data(),
         sizeof(Rect) * app.rects.size());

  // Update uniform buffer
  UniformBufferObject ubo = {{}};
  mat4Ortho(ubo.projection,
            0.0f,
            float(app.renderer.swapchain.extent.width),
            0.0f,
            float(app.renderer.swapchain.extent.height),
            -1.0f,
            1.0f);

  memcpy(app.rectData.uniformData.get(), &ubo, sizeof(ubo));
}

VkResult
endFrame(const sk::VulkanApi&  vk,
         const GraphicsDevice& gpu,
         const Renderer&       renderer,
         const uint32_t        imageIndex)
{
  const auto currentFrame = renderer.sync.currentFrame;
  VkResult   r            = VK_SUCCESS;

  static constexpr VkPipelineStageFlags waitStage =
    VK_PIPELINE_STAGE_TRANSFER_BIT;

  const VkSubmitInfo submitInfo{
    VK_STRUCTURE_TYPE_SUBMIT_INFO,
    nullptr,
    SK_COUNTED(1, &renderer.sync.imageAvailable[currentFrame].get()),
    &waitStage,
    SK_COUNTED(1, &renderer.renderPass.commandBuffers[imageIndex]),
    SK_COUNTED(1, &renderer.sync.renderFinished[imageIndex].get())};

  if ((r = vk.queueSubmit(gpu.graphicsQueue,
                          submitInfo,
                          renderer.sync.inFlight[currentFrame]))) {
    return r;
  }

  const VkPresentInfoKHR presentInfo{
    VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
    nullptr,
    SK_COUNTED(1, &renderer.sync.renderFinished[imageIndex].get()),
    SK_COUNTED(1, &renderer.swapchain.swapchain.get(), &imageIndex),
    nullptr};

  switch ((r = vk.queuePresentKHR(gpu.graphicsQueue, presentInfo))) {
  case VK_SUCCESS:               // Sucessfully presented
  case VK_SUBOPTIMAL_KHR:        // Probably a resize race, ignore
  case VK_ERROR_OUT_OF_DATE_KHR: // Probably a resize race, ignore
    break;
  default:
    return r;
  }

  return VK_SUCCESS;
}

pugl::Status
View::onEvent(const pugl::ExposeEvent&)
{
  const auto& vk  = _app.vulkan.vk;
  const auto& gpu = _app.gpu;

  // Acquire the next image, waiting and/or rebuilding if necessary
  auto nextImageIndex = 0u;
  if (beginFrame(_app, gpu.device, nextImageIndex)) {
    return pugl::Status::unknownError;
  }

  // Ready to go, update the data to the current time
  update(_app, world().time());

  // Submit the frame to the queue and present it
  endFrame(vk, gpu, _app.renderer, nextImageIndex);

  ++_app.framesDrawn;
  ++_app.renderer.sync.currentFrame;
  _app.renderer.sync.currentFrame %= _app.renderer.sync.inFlight.size();

  return pugl::Status::success;
}

pugl::Status
View::onEvent(const pugl::LoopEnterEvent&)
{
  _app.resizing = true;
  startTimer(resizeTimerId,
             1.0 / static_cast<double>(getHint(pugl::ViewHint::refreshRate)));

  return pugl::Status::success;
}

pugl::Status
View::onEvent(const pugl::TimerEvent&)
{
  return postRedisplay();
}

pugl::Status
View::onEvent(const pugl::LoopLeaveEvent&)
{
  stopTimer(resizeTimerId);

  // Trigger a swapchain recreation with the normal present mode
  _app.renderer.swapchain.extent = {};
  _app.resizing                  = false;

  return pugl::Status::success;
}

pugl::Status
View::onEvent(const pugl::KeyPressEvent& event)
{
  if (event.key == PUGL_KEY_ESCAPE || event.key == 'q') {
    _app.quit = true;
  }

  return pugl::Status::success;
}

pugl::Status
View::onEvent(const pugl::CloseEvent&)
{
  _app.quit = true;

  return pugl::Status::success;
}

VkResult
VulkanContext::init(pugl::VulkanLoader& loader, const PuglTestOptions& opts)
{
  VkResult r = VK_SUCCESS;

  sk::VulkanInitApi initApi{};

  // Load Vulkan API and set up the fundamentals
  if ((r = initApi.init(loader.getInstanceProcAddrFunc())) ||
      (r = createInstance(initApi, opts, instance)) ||
      (r = vk.init(initApi, instance)) ||
      (r = getDebugReportCallback(vk, instance, opts.verbose, debugCallback))) {
    return r;
  }

  return VK_SUCCESS;
}

int
run(const char* const     programPath,
    const PuglTestOptions opts,
    const size_t          numRects)
{
  PuglVulkanDemo app{programPath, opts, numRects};

  VkResult   r      = VK_SUCCESS;
  const auto width  = static_cast<int>(app.extent.width);
  const auto height = static_cast<int>(app.extent.height);

  // Realize window so we can set up Vulkan
  app.world.setClassName("PuglVulkanDemo");
  app.view.setWindowTitle("Pugl Vulkan Demo");
  app.view.setAspectRatio(1, 1, 16, 9);
  app.view.setDefaultSize(width, height);
  app.view.setMinSize(width / 4, height / 4);
  app.view.setBackend(pugl::vulkanBackend());
  app.view.setHint(pugl::ViewHint::resizable, opts.resizable);
  const pugl::Status st = app.view.realize();
  if (st != pugl::Status::success) {
    return logError("Failed to create window (%s)\n", pugl::strerror(st));
  }

  if (!app.loader) {
    return logError("Failed to load Vulkan library\n");
  }

  // Load Vulkan for the view
  if ((r = app.vulkan.init(app.loader, opts))) {
    return logError("Failed to set up Vulkan API (%s)\n", sk::string(r));
  }

  const auto& vk = app.vulkan.vk;

  // Set up the graphics device
  if ((r = app.gpu.init(app.loader, app.vulkan, app.view, opts))) {
    return logError("Failed to set up device (%s)\n", sk::string(r));
  }

  logInfo("Present mode", sk::string(app.gpu.presentMode));
  logInfo("Resize present mode", sk::string(app.gpu.resizePresentMode));

  // Set up the rectangle data we will render every frame
  if ((r = app.rectData.init(vk, app.gpu, app.rects.size()))) {
    return logError("Failed to allocate render data (%s)\n", sk::string(r));
  }

  // Load shader modules
  if ((r = app.rectShaders.init(vk, app.gpu, app.programPath))) {
    return logError("Failed to load shaders (%s)\n", sk::string(r));
  }

  if ((r = app.renderer.init(app.vulkan.vk,
                             app.gpu,
                             app.rectData,
                             app.rectShaders,
                             app.extent,
                             false))) {
    return logError("Failed to create renderer (%s)\n", sk::string(r));
  }

  logInfo("Swapchain frames",
          std::to_string(app.renderer.swapchain.imageViews.size()));
  logInfo("Frames in flight",
          std::to_string(app.renderer.sync.inFlight.size()));

  recordCommandBuffers(app.vulkan.vk,
                       app.renderer.swapchain,
                       app.renderer.renderPass,
                       app.renderer.rectPipeline,
                       app.rectData);

  const int    refreshRate   = app.view.getHint(pugl::ViewHint::refreshRate);
  const double frameDuration = 1.0 / static_cast<double>(refreshRate);
  const double timeout       = app.opts.sync ? frameDuration : 0.0;

  PuglFpsPrinter fpsPrinter = {app.world.time()};
  app.view.show();
  while (!app.quit) {
    app.world.update(timeout);
    puglPrintFps(app.world.cobj(), &fpsPrinter, &app.framesDrawn);
  }

  if ((r = app.vulkan.vk.deviceWaitIdle(app.gpu.device))) {
    return logError("Failed to wait for device idle (%s)\n", sk::string(r));
  }

  return 0;
}

} // namespace

int
main(int argc, char** argv)
{
  // Parse command line options
  const char* const     programPath = argv[0];
  const PuglTestOptions opts        = puglParseTestOptions(&argc, &argv);
  if (opts.help) {
    puglPrintTestUsage(programPath, "");
    return 0;
  }

  // Parse number of rectangles argument, if given
  int64_t numRects = 1000;
  if (argc >= 1) {
    char* endptr = nullptr;
    numRects     = strtol(argv[0], &endptr, 10);
    if (endptr != argv[0] + strlen(argv[0]) || numRects < 1) {
      logError("Invalid number of rectangles: %s\n", argv[0]);
      return 1;
    }
  }

  // Run application
  return run(programPath, opts, static_cast<size_t>(numRects));
}