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
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
|
/*
* Copyright © 2019 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <vulkan/vulkan.h>
#include <vulkan/vk_layer.h>
#include "imgui.h"
#include "overlay_params.h"
#include "util/debug.h"
#include "util/hash_table.h"
#include "util/list.h"
#include "util/ralloc.h"
#include "util/os_time.h"
#include "util/simple_mtx.h"
#include "vk_enum_to_str.h"
#include "vk_util.h"
/* Mapped from VkInstace/VkPhysicalDevice */
struct instance_data {
struct vk_instance_dispatch_table vtable;
VkInstance instance;
struct overlay_params params;
bool pipeline_statistics_enabled;
};
struct frame_stat {
uint64_t stats[OVERLAY_PARAM_ENABLED_MAX];
};
/* Mapped from VkDevice */
struct queue_data;
struct device_data {
struct instance_data *instance;
PFN_vkSetDeviceLoaderData set_device_loader_data;
struct vk_device_dispatch_table vtable;
VkPhysicalDevice physical_device;
VkDevice device;
VkPhysicalDeviceProperties properties;
struct queue_data *graphic_queue;
struct queue_data **queues;
uint32_t n_queues;
/* For a single frame */
struct frame_stat frame_stats;
};
/* Mapped from VkCommandBuffer */
struct command_buffer_data {
struct device_data *device;
VkCommandBufferLevel level;
VkCommandBuffer cmd_buffer;
VkQueryPool pipeline_query_pool;
VkQueryPool timestamp_query_pool;
uint32_t query_index;
struct frame_stat stats;
struct list_head link; /* link into queue_data::running_command_buffer */
};
/* Mapped from VkQueue */
struct queue_data {
struct device_data *device;
VkQueue queue;
VkQueueFlags flags;
uint32_t family_index;
uint64_t timestamp_mask;
VkFence queries_fence;
struct list_head running_command_buffer;
};
/* Mapped from VkSwapchainKHR */
struct swapchain_data {
struct device_data *device;
VkSwapchainKHR swapchain;
unsigned width, height;
VkFormat format;
uint32_t n_images;
VkImage *images;
VkImageView *image_views;
VkFramebuffer *framebuffers;
VkRenderPass render_pass;
VkDescriptorPool descriptor_pool;
VkDescriptorSetLayout descriptor_layout;
VkDescriptorSet descriptor_set;
VkSampler font_sampler;
VkPipelineLayout pipeline_layout;
VkPipeline pipeline;
VkCommandPool command_pool;
struct {
VkCommandBuffer command_buffer;
VkBuffer vertex_buffer;
VkDeviceMemory vertex_buffer_mem;
VkDeviceSize vertex_buffer_size;
VkBuffer index_buffer;
VkDeviceMemory index_buffer_mem;
VkDeviceSize index_buffer_size;
} frame_data[2];
bool font_uploaded;
VkImage font_image;
VkImageView font_image_view;
VkDeviceMemory font_mem;
VkBuffer upload_font_buffer;
VkDeviceMemory upload_font_buffer_mem;
VkSemaphore submission_semaphore;
/**/
ImGuiContext* imgui_context;
ImVec2 window_size;
/**/
uint64_t n_frames;
uint64_t last_present_time;
unsigned n_frames_since_update;
uint64_t last_fps_update;
double fps;
enum overlay_param_enabled stat_selector;
double time_dividor;
struct frame_stat stats_min, stats_max;
struct frame_stat frames_stats[200];
/* Over a single frame */
struct frame_stat frame_stats;
/* Over fps_sampling_period */
struct frame_stat accumulated_stats;
};
static const VkQueryPipelineStatisticFlags overlay_query_flags =
VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT |
VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_PRIMITIVES_BIT |
VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT |
VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_INVOCATIONS_BIT |
VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT |
VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT |
VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT |
VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT |
VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT |
VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT |
VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT;
#define OVERLAY_QUERY_COUNT (11)
static struct hash_table *vk_object_to_data = NULL;
static simple_mtx_t vk_object_to_data_mutex = _SIMPLE_MTX_INITIALIZER_NP;
thread_local ImGuiContext* __MesaImGui;
static inline void ensure_vk_object_map(void)
{
if (!vk_object_to_data) {
vk_object_to_data = _mesa_hash_table_create(NULL,
_mesa_hash_pointer,
_mesa_key_pointer_equal);
}
}
#define FIND_SWAPCHAIN_DATA(obj) ((struct swapchain_data *)find_object_data((void *) obj))
#define FIND_CMD_BUFFER_DATA(obj) ((struct command_buffer_data *)find_object_data((void *) obj))
#define FIND_DEVICE_DATA(obj) ((struct device_data *)find_object_data((void *) obj))
#define FIND_QUEUE_DATA(obj) ((struct queue_data *)find_object_data((void *) obj))
#define FIND_PHYSICAL_DEVICE_DATA(obj) ((struct instance_data *)find_object_data((void *) obj))
#define FIND_INSTANCE_DATA(obj) ((struct instance_data *)find_object_data((void *) obj))
static void *find_object_data(void *obj)
{
simple_mtx_lock(&vk_object_to_data_mutex);
ensure_vk_object_map();
struct hash_entry *entry = _mesa_hash_table_search(vk_object_to_data, obj);
void *data = entry ? entry->data : NULL;
simple_mtx_unlock(&vk_object_to_data_mutex);
return data;
}
static void map_object(void *obj, void *data)
{
simple_mtx_lock(&vk_object_to_data_mutex);
ensure_vk_object_map();
_mesa_hash_table_insert(vk_object_to_data, obj, data);
simple_mtx_unlock(&vk_object_to_data_mutex);
}
static void unmap_object(void *obj)
{
simple_mtx_lock(&vk_object_to_data_mutex);
struct hash_entry *entry = _mesa_hash_table_search(vk_object_to_data, obj);
_mesa_hash_table_remove(vk_object_to_data, entry);
simple_mtx_unlock(&vk_object_to_data_mutex);
}
/**/
#define VK_CHECK(expr) \
do { \
VkResult __result = (expr); \
if (__result != VK_SUCCESS) { \
fprintf(stderr, "'%s' line %i failed with %s\n", \
#expr, __LINE__, vk_Result_to_str(__result)); \
} \
} while (0)
/**/
static VkLayerInstanceCreateInfo *get_instance_chain_info(const VkInstanceCreateInfo *pCreateInfo,
VkLayerFunction func)
{
vk_foreach_struct(item, pCreateInfo->pNext) {
if (item->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO &&
((VkLayerInstanceCreateInfo *) item)->function == func)
return (VkLayerInstanceCreateInfo *) item;
}
unreachable("instance chain info not found");
return NULL;
}
static VkLayerDeviceCreateInfo *get_device_chain_info(const VkDeviceCreateInfo *pCreateInfo,
VkLayerFunction func)
{
vk_foreach_struct(item, pCreateInfo->pNext) {
if (item->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO &&
((VkLayerDeviceCreateInfo *) item)->function == func)
return (VkLayerDeviceCreateInfo *)item;
}
unreachable("device chain info not found");
return NULL;
}
static struct VkBaseOutStructure *
clone_chain(const struct VkBaseInStructure *chain)
{
struct VkBaseOutStructure *head = NULL, *tail = NULL;
vk_foreach_struct_const(item, chain) {
size_t item_size = vk_structure_type_size(item);
struct VkBaseOutStructure *new_item =
(struct VkBaseOutStructure *)malloc(item_size);;
memcpy(new_item, item, item_size);
if (!head)
head = new_item;
if (tail)
tail->pNext = new_item;
tail = new_item;
}
return head;
}
static void
free_chain(struct VkBaseOutStructure *chain)
{
while (chain) {
void *node = chain;
chain = chain->pNext;
free(node);
}
}
/**/
static void check_vk_result(VkResult err)
{
if (err != VK_SUCCESS)
printf("ERROR!\n");
}
static struct instance_data *new_instance_data(VkInstance instance)
{
struct instance_data *data = rzalloc(NULL, struct instance_data);
data->instance = instance;
map_object(data->instance, data);
return data;
}
static void destroy_instance_data(struct instance_data *data)
{
if (data->params.output_file)
fclose(data->params.output_file);
unmap_object(data->instance);
ralloc_free(data);
}
static void instance_data_map_physical_devices(struct instance_data *instance_data,
bool map)
{
uint32_t physicalDeviceCount = 0;
instance_data->vtable.EnumeratePhysicalDevices(instance_data->instance,
&physicalDeviceCount,
NULL);
VkPhysicalDevice *physicalDevices = (VkPhysicalDevice *) malloc(sizeof(VkPhysicalDevice) * physicalDeviceCount);
instance_data->vtable.EnumeratePhysicalDevices(instance_data->instance,
&physicalDeviceCount,
physicalDevices);
for (uint32_t i = 0; i < physicalDeviceCount; i++) {
if (map)
map_object(physicalDevices[i], instance_data);
else
unmap_object(physicalDevices[i]);
}
free(physicalDevices);
}
/**/
static struct device_data *new_device_data(VkDevice device, struct instance_data *instance)
{
struct device_data *data = rzalloc(NULL, struct device_data);
data->instance = instance;
data->device = device;
map_object(data->device, data);
return data;
}
static struct queue_data *new_queue_data(VkQueue queue,
const VkQueueFamilyProperties *family_props,
uint32_t family_index,
struct device_data *device_data)
{
struct queue_data *data = rzalloc(device_data, struct queue_data);
data->device = device_data;
data->queue = queue;
data->flags = family_props->queueFlags;
data->timestamp_mask = (1ul << family_props->timestampValidBits) - 1;
data->family_index = family_index;
LIST_INITHEAD(&data->running_command_buffer);
map_object(data->queue, data);
/* Fence synchronizing access to queries on that queue. */
VkFenceCreateInfo fence_info = {};
fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fence_info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
VkResult err = device_data->vtable.CreateFence(device_data->device,
&fence_info,
NULL,
&data->queries_fence);
check_vk_result(err);
if (data->flags & VK_QUEUE_GRAPHICS_BIT)
device_data->graphic_queue = data;
return data;
}
static void destroy_queue(struct queue_data *data)
{
struct device_data *device_data = data->device;
device_data->vtable.DestroyFence(device_data->device, data->queries_fence, NULL);
unmap_object(data->queue);
ralloc_free(data);
}
static void device_map_queues(struct device_data *data,
const VkDeviceCreateInfo *pCreateInfo)
{
for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++)
data->n_queues += pCreateInfo->pQueueCreateInfos[i].queueCount;
data->queues = ralloc_array(data, struct queue_data *, data->n_queues);
struct instance_data *instance_data = data->instance;
uint32_t n_family_props;
instance_data->vtable.GetPhysicalDeviceQueueFamilyProperties(data->physical_device,
&n_family_props,
NULL);
VkQueueFamilyProperties *family_props =
(VkQueueFamilyProperties *)malloc(sizeof(VkQueueFamilyProperties) * n_family_props);
instance_data->vtable.GetPhysicalDeviceQueueFamilyProperties(data->physical_device,
&n_family_props,
family_props);
uint32_t queue_index = 0;
for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
for (uint32_t j = 0; j < pCreateInfo->pQueueCreateInfos[i].queueCount; j++) {
VkQueue queue;
data->vtable.GetDeviceQueue(data->device,
pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex,
j, &queue);
VK_CHECK(data->set_device_loader_data(data->device, queue));
data->queues[queue_index++] =
new_queue_data(queue, &family_props[pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex],
pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex, data);
}
}
free(family_props);
}
static void device_unmap_queues(struct device_data *data)
{
for (uint32_t i = 0; i < data->n_queues; i++)
destroy_queue(data->queues[i]);
}
static void destroy_device_data(struct device_data *data)
{
unmap_object(data->device);
ralloc_free(data);
}
/**/
static struct command_buffer_data *new_command_buffer_data(VkCommandBuffer cmd_buffer,
VkCommandBufferLevel level,
VkQueryPool pipeline_query_pool,
VkQueryPool timestamp_query_pool,
uint32_t query_index,
struct device_data *device_data)
{
struct command_buffer_data *data = rzalloc(NULL, struct command_buffer_data);
data->device = device_data;
data->cmd_buffer = cmd_buffer;
data->level = level;
data->pipeline_query_pool = pipeline_query_pool;
data->timestamp_query_pool = timestamp_query_pool;
data->query_index = query_index;
list_inithead(&data->link);
map_object((void *) data->cmd_buffer, data);
return data;
}
static void destroy_command_buffer_data(struct command_buffer_data *data)
{
unmap_object((void *) data->cmd_buffer);
list_delinit(&data->link);
ralloc_free(data);
}
/**/
static struct swapchain_data *new_swapchain_data(VkSwapchainKHR swapchain,
struct device_data *device_data)
{
struct swapchain_data *data = rzalloc(NULL, struct swapchain_data);
data->device = device_data;
data->swapchain = swapchain;
data->window_size = ImVec2(300, 300);
map_object((void *) data->swapchain, data);
return data;
}
static void destroy_swapchain_data(struct swapchain_data *data)
{
unmap_object((void *) data->swapchain);
ralloc_free(data);
}
static void snapshot_swapchain_frame(struct swapchain_data *data)
{
struct device_data *device_data = data->device;
struct instance_data *instance_data = device_data->instance;
uint32_t f_idx = data->n_frames % ARRAY_SIZE(data->frames_stats);
uint64_t now = os_time_get(); /* us */
if (data->last_present_time) {
data->frame_stats.stats[OVERLAY_PARAM_ENABLED_frame_timing] =
now - data->last_present_time;
}
memset(&data->frames_stats[f_idx], 0, sizeof(data->frames_stats[f_idx]));
for (int s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
data->frames_stats[f_idx].stats[s] += device_data->frame_stats.stats[s] + data->frame_stats.stats[s];
data->accumulated_stats.stats[s] += device_data->frame_stats.stats[s] + data->frame_stats.stats[s];
}
if (data->last_fps_update) {
double elapsed = (double)(now - data->last_fps_update); /* us */
if (elapsed >= instance_data->params.fps_sampling_period) {
data->fps = 1000000.0f * data->n_frames_since_update / elapsed;
if (instance_data->params.output_file) {
fprintf(instance_data->params.output_file, "%.2f\n", data->fps);
fflush(instance_data->params.output_file);
}
memset(&data->accumulated_stats, 0, sizeof(data->accumulated_stats));
data->n_frames_since_update = 0;
data->last_fps_update = now;
}
} else {
data->last_fps_update = now;
}
memset(&device_data->frame_stats, 0, sizeof(device_data->frame_stats));
memset(&data->frame_stats, 0, sizeof(device_data->frame_stats));
data->last_present_time = now;
data->n_frames++;
data->n_frames_since_update++;
}
static float get_time_stat(void *_data, int _idx)
{
struct swapchain_data *data = (struct swapchain_data *) _data;
if ((ARRAY_SIZE(data->frames_stats) - _idx) > data->n_frames)
return 0.0f;
int idx = ARRAY_SIZE(data->frames_stats) +
data->n_frames < ARRAY_SIZE(data->frames_stats) ?
_idx - data->n_frames :
_idx + data->n_frames;
idx %= ARRAY_SIZE(data->frames_stats);
/* Time stats are in us. */
return data->frames_stats[idx].stats[data->stat_selector] / data->time_dividor;
}
static float get_stat(void *_data, int _idx)
{
struct swapchain_data *data = (struct swapchain_data *) _data;
if ((ARRAY_SIZE(data->frames_stats) - _idx) > data->n_frames)
return 0.0f;
int idx = ARRAY_SIZE(data->frames_stats) +
data->n_frames < ARRAY_SIZE(data->frames_stats) ?
_idx - data->n_frames :
_idx + data->n_frames;
idx %= ARRAY_SIZE(data->frames_stats);
return data->frames_stats[idx].stats[data->stat_selector];
}
static void position_layer(struct swapchain_data *data)
{
struct device_data *device_data = data->device;
struct instance_data *instance_data = device_data->instance;
const float margin = 10.0f;
ImGui::SetNextWindowBgAlpha(0.5);
ImGui::SetNextWindowSize(data->window_size, ImGuiCond_Always);
switch (instance_data->params.position) {
case LAYER_POSITION_TOP_LEFT:
ImGui::SetNextWindowPos(ImVec2(margin, margin), ImGuiCond_Always);
break;
case LAYER_POSITION_TOP_RIGHT:
ImGui::SetNextWindowPos(ImVec2(data->width - data->window_size.x - margin, margin),
ImGuiCond_Always);
break;
case LAYER_POSITION_BOTTOM_LEFT:
ImGui::SetNextWindowPos(ImVec2(margin, data->height - data->window_size.y - margin),
ImGuiCond_Always);
break;
case LAYER_POSITION_BOTTOM_RIGHT:
ImGui::SetNextWindowPos(ImVec2(data->width - data->window_size.x - margin,
data->height - data->window_size.y - margin),
ImGuiCond_Always);
break;
}
}
static void compute_swapchain_display(struct swapchain_data *data)
{
struct device_data *device_data = data->device;
struct instance_data *instance_data = device_data->instance;
ImGui::SetCurrentContext(data->imgui_context);
ImGui::NewFrame();
position_layer(data);
ImGui::Begin("Mesa overlay");
ImGui::Text("Device: %s", device_data->properties.deviceName);
const char *format_name = vk_Format_to_str(data->format);
format_name = format_name ? (format_name + strlen("VK_FORMAT_")) : "unknown";
ImGui::Text("Swapchain format: %s", format_name);
ImGui::Text("Frames: %" PRIu64, data->n_frames);
if (instance_data->params.enabled[OVERLAY_PARAM_ENABLED_fps])
ImGui::Text("FPS: %.2f" , data->fps);
/* Recompute min/max */
for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
data->stats_min.stats[s] = UINT64_MAX;
data->stats_max.stats[s] = 0;
}
for (uint32_t f = 0; f < MIN2(data->n_frames, ARRAY_SIZE(data->frames_stats)); f++) {
for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
data->stats_min.stats[s] = MIN2(data->frames_stats[f].stats[s],
data->stats_min.stats[s]);
data->stats_max.stats[s] = MAX2(data->frames_stats[f].stats[s],
data->stats_max.stats[s]);
}
}
for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
assert(data->stats_min.stats[s] != UINT64_MAX);
}
for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++) {
if (!instance_data->params.enabled[s] ||
s == OVERLAY_PARAM_ENABLED_fps)
continue;
char hash[40];
snprintf(hash, sizeof(hash), "##%s", overlay_param_names[s]);
data->stat_selector = (enum overlay_param_enabled) s;
data->time_dividor = 1000.0f;
if (s == OVERLAY_PARAM_ENABLED_gpu_timing)
data->time_dividor = 1000000.0f;
if (s == OVERLAY_PARAM_ENABLED_frame_timing ||
s == OVERLAY_PARAM_ENABLED_acquire_timing ||
s == OVERLAY_PARAM_ENABLED_gpu_timing) {
double min_time = data->stats_min.stats[s] / data->time_dividor;
double max_time = data->stats_max.stats[s] / data->time_dividor;
ImGui::PlotHistogram(hash, get_time_stat, data,
ARRAY_SIZE(data->frames_stats), 0,
NULL, min_time, max_time,
ImVec2(ImGui::GetContentRegionAvailWidth(), 30));
ImGui::Text("%s: %.3fms [%.3f, %.3f]", overlay_param_names[s],
get_time_stat(data, ARRAY_SIZE(data->frames_stats) - 1),
min_time, max_time);
} else {
ImGui::PlotHistogram(hash, get_stat, data,
ARRAY_SIZE(data->frames_stats), 0,
NULL,
data->stats_min.stats[s],
data->stats_max.stats[s],
ImVec2(ImGui::GetContentRegionAvailWidth(), 30));
ImGui::Text("%s: %.0f [%" PRIu64 ", %" PRIu64 "]", overlay_param_names[s],
get_stat(data, ARRAY_SIZE(data->frames_stats) - 1),
data->stats_min.stats[s], data->stats_max.stats[s]);
}
}
data->window_size = ImVec2(data->window_size.x, ImGui::GetCursorPosY() + 10.0f);
ImGui::End();
ImGui::EndFrame();
ImGui::Render();
}
static uint32_t vk_memory_type(struct device_data *data,
VkMemoryPropertyFlags properties,
uint32_t type_bits)
{
VkPhysicalDeviceMemoryProperties prop;
data->instance->vtable.GetPhysicalDeviceMemoryProperties(data->physical_device, &prop);
for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
return i;
return 0xFFFFFFFF; // Unable to find memoryType
}
static void ensure_swapchain_fonts(struct swapchain_data *data,
VkCommandBuffer command_buffer)
{
if (data->font_uploaded)
return;
data->font_uploaded = true;
struct device_data *device_data = data->device;
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
size_t upload_size = width * height * 4 * sizeof(char);
/* Upload buffer */
VkBufferCreateInfo buffer_info = {};
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_info.size = upload_size;
buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VK_CHECK(device_data->vtable.CreateBuffer(device_data->device, &buffer_info,
NULL, &data->upload_font_buffer));
VkMemoryRequirements upload_buffer_req;
device_data->vtable.GetBufferMemoryRequirements(device_data->device,
data->upload_font_buffer,
&upload_buffer_req);
VkMemoryAllocateInfo upload_alloc_info = {};
upload_alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
upload_alloc_info.allocationSize = upload_buffer_req.size;
upload_alloc_info.memoryTypeIndex = vk_memory_type(device_data,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
upload_buffer_req.memoryTypeBits);
VK_CHECK(device_data->vtable.AllocateMemory(device_data->device,
&upload_alloc_info,
NULL,
&data->upload_font_buffer_mem));
VK_CHECK(device_data->vtable.BindBufferMemory(device_data->device,
data->upload_font_buffer,
data->upload_font_buffer_mem, 0));
/* Upload to Buffer */
char* map = NULL;
VK_CHECK(device_data->vtable.MapMemory(device_data->device,
data->upload_font_buffer_mem,
0, upload_size, 0, (void**)(&map)));
memcpy(map, pixels, upload_size);
VkMappedMemoryRange range[1] = {};
range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
range[0].memory = data->upload_font_buffer_mem;
range[0].size = upload_size;
VK_CHECK(device_data->vtable.FlushMappedMemoryRanges(device_data->device, 1, range));
device_data->vtable.UnmapMemory(device_data->device,
data->upload_font_buffer_mem);
/* Copy buffer to image */
VkImageMemoryBarrier copy_barrier[1] = {};
copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
copy_barrier[0].image = data->font_image;
copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copy_barrier[0].subresourceRange.levelCount = 1;
copy_barrier[0].subresourceRange.layerCount = 1;
device_data->vtable.CmdPipelineBarrier(command_buffer,
VK_PIPELINE_STAGE_HOST_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
0, 0, NULL, 0, NULL,
1, copy_barrier);
VkBufferImageCopy region = {};
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.imageSubresource.layerCount = 1;
region.imageExtent.width = width;
region.imageExtent.height = height;
region.imageExtent.depth = 1;
device_data->vtable.CmdCopyBufferToImage(command_buffer,
data->upload_font_buffer,
data->font_image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1, ®ion);
VkImageMemoryBarrier use_barrier[1] = {};
use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
use_barrier[0].image = data->font_image;
use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
use_barrier[0].subresourceRange.levelCount = 1;
use_barrier[0].subresourceRange.layerCount = 1;
device_data->vtable.CmdPipelineBarrier(command_buffer,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
0,
0, NULL,
0, NULL,
1, use_barrier);
/* Store our identifier */
io.Fonts->TexID = (ImTextureID)(intptr_t)data->font_image;
}
static void CreateOrResizeBuffer(struct device_data *data,
VkBuffer *buffer,
VkDeviceMemory *buffer_memory,
VkDeviceSize *buffer_size,
size_t new_size, VkBufferUsageFlagBits usage)
{
if (*buffer != VK_NULL_HANDLE)
data->vtable.DestroyBuffer(data->device, *buffer, NULL);
if (*buffer_memory)
data->vtable.FreeMemory(data->device, *buffer_memory, NULL);
VkBufferCreateInfo buffer_info = {};
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_info.size = new_size;
buffer_info.usage = usage;
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VK_CHECK(data->vtable.CreateBuffer(data->device, &buffer_info, NULL, buffer));
VkMemoryRequirements req;
data->vtable.GetBufferMemoryRequirements(data->device, *buffer, &req);
VkMemoryAllocateInfo alloc_info = {};
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc_info.allocationSize = req.size;
alloc_info.memoryTypeIndex =
vk_memory_type(data, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
VK_CHECK(data->vtable.AllocateMemory(data->device, &alloc_info, NULL, buffer_memory));
VK_CHECK(data->vtable.BindBufferMemory(data->device, *buffer, *buffer_memory, 0));
*buffer_size = new_size;
}
static void render_swapchain_display(struct swapchain_data *data,
const VkSemaphore *wait_semaphores,
unsigned n_wait_semaphores,
unsigned image_index)
{
ImDrawData* draw_data = ImGui::GetDrawData();
if (draw_data->TotalVtxCount == 0)
return;
struct device_data *device_data = data->device;
uint32_t idx = data->n_frames % ARRAY_SIZE(data->frame_data);
VkCommandBuffer command_buffer = data->frame_data[idx].command_buffer;
device_data->vtable.ResetCommandBuffer(command_buffer, 0);
VkRenderPassBeginInfo render_pass_info = {};
render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
render_pass_info.renderPass = data->render_pass;
render_pass_info.framebuffer = data->framebuffers[image_index];
render_pass_info.renderArea.extent.width = data->width;
render_pass_info.renderArea.extent.height = data->height;
VkCommandBufferBeginInfo buffer_begin_info = {};
buffer_begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
device_data->vtable.BeginCommandBuffer(command_buffer, &buffer_begin_info);
ensure_swapchain_fonts(data, command_buffer);
/* Bounce the image to display back to color attachment layout for
* rendering on top of it.
*/
VkImageMemoryBarrier imb;
imb.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
imb.pNext = nullptr;
imb.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
imb.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
imb.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
imb.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
imb.image = data->images[image_index];
imb.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imb.subresourceRange.baseMipLevel = 0;
imb.subresourceRange.levelCount = 1;
imb.subresourceRange.baseArrayLayer = 0;
imb.subresourceRange.layerCount = 1;
imb.srcQueueFamilyIndex = device_data->graphic_queue->family_index;
imb.dstQueueFamilyIndex = device_data->graphic_queue->family_index;
device_data->vtable.CmdPipelineBarrier(command_buffer,
VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
0, /* dependency flags */
0, nullptr, /* memory barriers */
0, nullptr, /* buffer memory barriers */
1, &imb); /* image memory barriers */
device_data->vtable.CmdBeginRenderPass(command_buffer, &render_pass_info,
VK_SUBPASS_CONTENTS_INLINE);
/* Create/Resize vertex & index buffers */
size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
if (data->frame_data[idx].vertex_buffer_size < vertex_size) {
CreateOrResizeBuffer(device_data,
&data->frame_data[idx].vertex_buffer,
&data->frame_data[idx].vertex_buffer_mem,
&data->frame_data[idx].vertex_buffer_size,
vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
}
if (data->frame_data[idx].index_buffer_size < index_size) {
CreateOrResizeBuffer(device_data,
&data->frame_data[idx].index_buffer,
&data->frame_data[idx].index_buffer_mem,
&data->frame_data[idx].index_buffer_size,
index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
}
/* Upload vertex & index data */
VkBuffer vertex_buffer = data->frame_data[idx].vertex_buffer;
VkDeviceMemory vertex_mem = data->frame_data[idx].vertex_buffer_mem;
VkBuffer index_buffer = data->frame_data[idx].index_buffer;
VkDeviceMemory index_mem = data->frame_data[idx].index_buffer_mem;
ImDrawVert* vtx_dst = NULL;
ImDrawIdx* idx_dst = NULL;
VK_CHECK(device_data->vtable.MapMemory(device_data->device, vertex_mem,
0, vertex_size, 0, (void**)(&vtx_dst)));
VK_CHECK(device_data->vtable.MapMemory(device_data->device, index_mem,
0, index_size, 0, (void**)(&idx_dst)));
for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* cmd_list = draw_data->CmdLists[n];
memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
vtx_dst += cmd_list->VtxBuffer.Size;
idx_dst += cmd_list->IdxBuffer.Size;
}
VkMappedMemoryRange range[2] = {};
range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
range[0].memory = vertex_mem;
range[0].size = VK_WHOLE_SIZE;
range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
range[1].memory = index_mem;
range[1].size = VK_WHOLE_SIZE;
VK_CHECK(device_data->vtable.FlushMappedMemoryRanges(device_data->device, 2, range));
device_data->vtable.UnmapMemory(device_data->device, vertex_mem);
device_data->vtable.UnmapMemory(device_data->device, index_mem);
/* Bind pipeline and descriptor sets */
device_data->vtable.CmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, data->pipeline);
VkDescriptorSet desc_set[1] = { data->descriptor_set };
device_data->vtable.CmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
data->pipeline_layout, 0, 1, desc_set, 0, NULL);
/* Bind vertex & index buffers */
VkBuffer vertex_buffers[1] = { vertex_buffer };
VkDeviceSize vertex_offset[1] = { 0 };
device_data->vtable.CmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, vertex_offset);
device_data->vtable.CmdBindIndexBuffer(command_buffer, index_buffer, 0, VK_INDEX_TYPE_UINT16);
/* Setup viewport */
VkViewport viewport;
viewport.x = 0;
viewport.y = 0;
viewport.width = draw_data->DisplaySize.x;
viewport.height = draw_data->DisplaySize.y;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
device_data->vtable.CmdSetViewport(command_buffer, 0, 1, &viewport);
/* Setup scale and translation through push constants :
*
* Our visible imgui space lies from draw_data->DisplayPos (top left) to
* draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin
* is typically (0,0) for single viewport apps.
*/
float scale[2];
scale[0] = 2.0f / draw_data->DisplaySize.x;
scale[1] = 2.0f / draw_data->DisplaySize.y;
float translate[2];
translate[0] = -1.0f - draw_data->DisplayPos.x * scale[0];
translate[1] = -1.0f - draw_data->DisplayPos.y * scale[1];
device_data->vtable.CmdPushConstants(command_buffer, data->pipeline_layout,
VK_SHADER_STAGE_VERTEX_BIT,
sizeof(float) * 0, sizeof(float) * 2, scale);
device_data->vtable.CmdPushConstants(command_buffer, data->pipeline_layout,
VK_SHADER_STAGE_VERTEX_BIT,
sizeof(float) * 2, sizeof(float) * 2, translate);
// Render the command lists:
int vtx_offset = 0;
int idx_offset = 0;
ImVec2 display_pos = draw_data->DisplayPos;
for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* cmd_list = draw_data->CmdLists[n];
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
// Apply scissor/clipping rectangle
// FIXME: We could clamp width/height based on clamped min/max values.
VkRect2D scissor;
scissor.offset.x = (int32_t)(pcmd->ClipRect.x - display_pos.x) > 0 ? (int32_t)(pcmd->ClipRect.x - display_pos.x) : 0;
scissor.offset.y = (int32_t)(pcmd->ClipRect.y - display_pos.y) > 0 ? (int32_t)(pcmd->ClipRect.y - display_pos.y) : 0;
scissor.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // FIXME: Why +1 here?
device_data->vtable.CmdSetScissor(command_buffer, 0, 1, &scissor);
// Draw
device_data->vtable.CmdDrawIndexed(command_buffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
idx_offset += pcmd->ElemCount;
}
vtx_offset += cmd_list->VtxBuffer.Size;
}
device_data->vtable.CmdEndRenderPass(command_buffer);
device_data->vtable.EndCommandBuffer(command_buffer);
if (data->submission_semaphore) {
device_data->vtable.DestroySemaphore(device_data->device,
data->submission_semaphore,
NULL);
}
/* Submission semaphore */
VkSemaphoreCreateInfo semaphore_info = {};
semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
VK_CHECK(device_data->vtable.CreateSemaphore(device_data->device, &semaphore_info,
NULL, &data->submission_semaphore));
VkSubmitInfo submit_info = {};
VkPipelineStageFlags stage_wait = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &command_buffer;
submit_info.pWaitDstStageMask = &stage_wait;
submit_info.waitSemaphoreCount = n_wait_semaphores;
submit_info.pWaitSemaphores = wait_semaphores;
submit_info.signalSemaphoreCount = 1;
submit_info.pSignalSemaphores = &data->submission_semaphore;
device_data->vtable.QueueSubmit(device_data->graphic_queue->queue, 1, &submit_info, VK_NULL_HANDLE);
}
static const uint32_t overlay_vert_spv[] = {
#include "overlay.vert.spv.h"
};
static const uint32_t overlay_frag_spv[] = {
#include "overlay.frag.spv.h"
};
static void setup_swapchain_data_pipeline(struct swapchain_data *data)
{
struct device_data *device_data = data->device;
VkShaderModule vert_module, frag_module;
/* Create shader modules */
VkShaderModuleCreateInfo vert_info = {};
vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
vert_info.codeSize = sizeof(overlay_vert_spv);
vert_info.pCode = overlay_vert_spv;
VK_CHECK(device_data->vtable.CreateShaderModule(device_data->device,
&vert_info, NULL, &vert_module));
VkShaderModuleCreateInfo frag_info = {};
frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
frag_info.codeSize = sizeof(overlay_frag_spv);
frag_info.pCode = (uint32_t*)overlay_frag_spv;
VK_CHECK(device_data->vtable.CreateShaderModule(device_data->device,
&frag_info, NULL, &frag_module));
/* Font sampler */
VkSamplerCreateInfo sampler_info = {};
sampler_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
sampler_info.magFilter = VK_FILTER_LINEAR;
sampler_info.minFilter = VK_FILTER_LINEAR;
sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
sampler_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
sampler_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
sampler_info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
sampler_info.minLod = -1000;
sampler_info.maxLod = 1000;
sampler_info.maxAnisotropy = 1.0f;
VK_CHECK(device_data->vtable.CreateSampler(device_data->device, &sampler_info,
NULL, &data->font_sampler));
/* Descriptor pool */
VkDescriptorPoolSize sampler_pool_size = {};
sampler_pool_size.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
sampler_pool_size.descriptorCount = 1;
VkDescriptorPoolCreateInfo desc_pool_info = {};
desc_pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
desc_pool_info.maxSets = 1;
desc_pool_info.poolSizeCount = 1;
desc_pool_info.pPoolSizes = &sampler_pool_size;
VK_CHECK(device_data->vtable.CreateDescriptorPool(device_data->device,
&desc_pool_info,
NULL, &data->descriptor_pool));
/* Descriptor layout */
VkSampler sampler[1] = { data->font_sampler };
VkDescriptorSetLayoutBinding binding[1] = {};
binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
binding[0].descriptorCount = 1;
binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
binding[0].pImmutableSamplers = sampler;
VkDescriptorSetLayoutCreateInfo set_layout_info = {};
set_layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
set_layout_info.bindingCount = 1;
set_layout_info.pBindings = binding;
VK_CHECK(device_data->vtable.CreateDescriptorSetLayout(device_data->device,
&set_layout_info,
NULL, &data->descriptor_layout));
/* Descriptor set */
VkDescriptorSetAllocateInfo alloc_info = {};
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info.descriptorPool = data->descriptor_pool;
alloc_info.descriptorSetCount = 1;
alloc_info.pSetLayouts = &data->descriptor_layout;
VK_CHECK(device_data->vtable.AllocateDescriptorSets(device_data->device,
&alloc_info,
&data->descriptor_set));
/* Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full
* 3d projection matrix
*/
VkPushConstantRange push_constants[1] = {};
push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
push_constants[0].offset = sizeof(float) * 0;
push_constants[0].size = sizeof(float) * 4;
VkPipelineLayoutCreateInfo layout_info = {};
layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
layout_info.setLayoutCount = 1;
layout_info.pSetLayouts = &data->descriptor_layout;
layout_info.pushConstantRangeCount = 1;
layout_info.pPushConstantRanges = push_constants;
VK_CHECK(device_data->vtable.CreatePipelineLayout(device_data->device,
&layout_info,
NULL, &data->pipeline_layout));
VkPipelineShaderStageCreateInfo stage[2] = {};
stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
stage[0].module = vert_module;
stage[0].pName = "main";
stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
stage[1].module = frag_module;
stage[1].pName = "main";
VkVertexInputBindingDescription binding_desc[1] = {};
binding_desc[0].stride = sizeof(ImDrawVert);
binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
VkVertexInputAttributeDescription attribute_desc[3] = {};
attribute_desc[0].location = 0;
attribute_desc[0].binding = binding_desc[0].binding;
attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
attribute_desc[0].offset = IM_OFFSETOF(ImDrawVert, pos);
attribute_desc[1].location = 1;
attribute_desc[1].binding = binding_desc[0].binding;
attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
attribute_desc[1].offset = IM_OFFSETOF(ImDrawVert, uv);
attribute_desc[2].location = 2;
attribute_desc[2].binding = binding_desc[0].binding;
attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
attribute_desc[2].offset = IM_OFFSETOF(ImDrawVert, col);
VkPipelineVertexInputStateCreateInfo vertex_info = {};
vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertex_info.vertexBindingDescriptionCount = 1;
vertex_info.pVertexBindingDescriptions = binding_desc;
vertex_info.vertexAttributeDescriptionCount = 3;
vertex_info.pVertexAttributeDescriptions = attribute_desc;
VkPipelineInputAssemblyStateCreateInfo ia_info = {};
ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPipelineViewportStateCreateInfo viewport_info = {};
viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewport_info.viewportCount = 1;
viewport_info.scissorCount = 1;
VkPipelineRasterizationStateCreateInfo raster_info = {};
raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
raster_info.polygonMode = VK_POLYGON_MODE_FILL;
raster_info.cullMode = VK_CULL_MODE_NONE;
raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
raster_info.lineWidth = 1.0f;
VkPipelineMultisampleStateCreateInfo ms_info = {};
ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState color_attachment[1] = {};
color_attachment[0].blendEnable = VK_TRUE;
color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkPipelineDepthStencilStateCreateInfo depth_info = {};
depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
VkPipelineColorBlendStateCreateInfo blend_info = {};
blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
blend_info.attachmentCount = 1;
blend_info.pAttachments = color_attachment;
VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
VkPipelineDynamicStateCreateInfo dynamic_state = {};
dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
dynamic_state.dynamicStateCount = (uint32_t)IM_ARRAYSIZE(dynamic_states);
dynamic_state.pDynamicStates = dynamic_states;
VkGraphicsPipelineCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
info.flags = 0;
info.stageCount = 2;
info.pStages = stage;
info.pVertexInputState = &vertex_info;
info.pInputAssemblyState = &ia_info;
info.pViewportState = &viewport_info;
info.pRasterizationState = &raster_info;
info.pMultisampleState = &ms_info;
info.pDepthStencilState = &depth_info;
info.pColorBlendState = &blend_info;
info.pDynamicState = &dynamic_state;
info.layout = data->pipeline_layout;
info.renderPass = data->render_pass;
VK_CHECK(
device_data->vtable.CreateGraphicsPipelines(device_data->device, VK_NULL_HANDLE,
1, &info,
NULL, &data->pipeline));
device_data->vtable.DestroyShaderModule(device_data->device, vert_module, NULL);
device_data->vtable.DestroyShaderModule(device_data->device, frag_module, NULL);
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
/* Font image */
VkImageCreateInfo image_info = {};
image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
image_info.imageType = VK_IMAGE_TYPE_2D;
image_info.format = VK_FORMAT_R8G8B8A8_UNORM;
image_info.extent.width = width;
image_info.extent.height = height;
image_info.extent.depth = 1;
image_info.mipLevels = 1;
image_info.arrayLayers = 1;
image_info.samples = VK_SAMPLE_COUNT_1_BIT;
image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
VK_CHECK(device_data->vtable.CreateImage(device_data->device, &image_info,
NULL, &data->font_image));
VkMemoryRequirements font_image_req;
device_data->vtable.GetImageMemoryRequirements(device_data->device,
data->font_image, &font_image_req);
VkMemoryAllocateInfo image_alloc_info = {};
image_alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
image_alloc_info.allocationSize = font_image_req.size;
image_alloc_info.memoryTypeIndex = vk_memory_type(device_data,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
font_image_req.memoryTypeBits);
VK_CHECK(device_data->vtable.AllocateMemory(device_data->device, &image_alloc_info,
NULL, &data->font_mem));
VK_CHECK(device_data->vtable.BindImageMemory(device_data->device,
data->font_image,
data->font_mem, 0));
/* Font image view */
VkImageViewCreateInfo view_info = {};
view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
view_info.image = data->font_image;
view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
view_info.format = VK_FORMAT_R8G8B8A8_UNORM;
view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
view_info.subresourceRange.levelCount = 1;
view_info.subresourceRange.layerCount = 1;
VK_CHECK(device_data->vtable.CreateImageView(device_data->device, &view_info,
NULL, &data->font_image_view));
/* Descriptor set */
VkDescriptorImageInfo desc_image[1] = {};
desc_image[0].sampler = data->font_sampler;
desc_image[0].imageView = data->font_image_view;
desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
VkWriteDescriptorSet write_desc[1] = {};
write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write_desc[0].dstSet = data->descriptor_set;
write_desc[0].descriptorCount = 1;
write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
write_desc[0].pImageInfo = desc_image;
device_data->vtable.UpdateDescriptorSets(device_data->device, 1, write_desc, 0, NULL);
}
static void setup_swapchain_data(struct swapchain_data *data,
const VkSwapchainCreateInfoKHR *pCreateInfo)
{
data->width = pCreateInfo->imageExtent.width;
data->height = pCreateInfo->imageExtent.height;
data->format = pCreateInfo->imageFormat;
data->imgui_context = ImGui::CreateContext();
ImGui::SetCurrentContext(data->imgui_context);
ImGui::GetIO().IniFilename = NULL;
ImGui::GetIO().DisplaySize = ImVec2((float)data->width, (float)data->height);
struct device_data *device_data = data->device;
/* Render pass */
VkAttachmentDescription attachment_desc = {};
attachment_desc.format = pCreateInfo->imageFormat;
attachment_desc.samples = VK_SAMPLE_COUNT_1_BIT;
attachment_desc.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
attachment_desc.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachment_desc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment_desc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment_desc.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachment_desc.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference color_attachment = {};
color_attachment.attachment = 0;
color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &color_attachment;
VkSubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
VkRenderPassCreateInfo render_pass_info = {};
render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
render_pass_info.attachmentCount = 1;
render_pass_info.pAttachments = &attachment_desc;
render_pass_info.subpassCount = 1;
render_pass_info.pSubpasses = &subpass;
render_pass_info.dependencyCount = 1;
render_pass_info.pDependencies = &dependency;
VK_CHECK(device_data->vtable.CreateRenderPass(device_data->device,
&render_pass_info,
NULL, &data->render_pass));
setup_swapchain_data_pipeline(data);
VK_CHECK(device_data->vtable.GetSwapchainImagesKHR(device_data->device,
data->swapchain,
&data->n_images,
NULL));
data->images = ralloc_array(data, VkImage, data->n_images);
data->image_views = ralloc_array(data, VkImageView, data->n_images);
data->framebuffers = ralloc_array(data, VkFramebuffer, data->n_images);
VK_CHECK(device_data->vtable.GetSwapchainImagesKHR(device_data->device,
data->swapchain,
&data->n_images,
data->images));
/* Image views */
VkImageViewCreateInfo view_info = {};
view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
view_info.format = pCreateInfo->imageFormat;
view_info.components.r = VK_COMPONENT_SWIZZLE_R;
view_info.components.g = VK_COMPONENT_SWIZZLE_G;
view_info.components.b = VK_COMPONENT_SWIZZLE_B;
view_info.components.a = VK_COMPONENT_SWIZZLE_A;
view_info.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
for (uint32_t i = 0; i < data->n_images; i++) {
view_info.image = data->images[i];
VK_CHECK(device_data->vtable.CreateImageView(device_data->device,
&view_info, NULL,
&data->image_views[i]));
}
/* Framebuffers */
VkImageView attachment[1];
VkFramebufferCreateInfo fb_info = {};
fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
fb_info.renderPass = data->render_pass;
fb_info.attachmentCount = 1;
fb_info.pAttachments = attachment;
fb_info.width = data->width;
fb_info.height = data->height;
fb_info.layers = 1;
for (uint32_t i = 0; i < data->n_images; i++) {
attachment[0] = data->image_views[i];
VK_CHECK(device_data->vtable.CreateFramebuffer(device_data->device, &fb_info,
NULL, &data->framebuffers[i]));
}
/* Command buffer */
VkCommandPoolCreateInfo cmd_buffer_pool_info = {};
cmd_buffer_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmd_buffer_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
cmd_buffer_pool_info.queueFamilyIndex = device_data->graphic_queue->family_index;
VK_CHECK(device_data->vtable.CreateCommandPool(device_data->device,
&cmd_buffer_pool_info,
NULL, &data->command_pool));
VkCommandBuffer cmd_bufs[ARRAY_SIZE(data->frame_data)];
VkCommandBufferAllocateInfo cmd_buffer_info = {};
cmd_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmd_buffer_info.commandPool = data->command_pool;
cmd_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmd_buffer_info.commandBufferCount = 2;
VK_CHECK(device_data->vtable.AllocateCommandBuffers(device_data->device,
&cmd_buffer_info,
cmd_bufs));
for (uint32_t i = 0; i < ARRAY_SIZE(data->frame_data); i++) {
VK_CHECK(device_data->set_device_loader_data(device_data->device,
cmd_bufs[i]));
data->frame_data[i].command_buffer = cmd_bufs[i];
}
}
static void shutdown_swapchain_data(struct swapchain_data *data)
{
struct device_data *device_data = data->device;
for (uint32_t i = 0; i < data->n_images; i++) {
device_data->vtable.DestroyImageView(device_data->device, data->image_views[i], NULL);
device_data->vtable.DestroyFramebuffer(device_data->device, data->framebuffers[i], NULL);
}
device_data->vtable.DestroyRenderPass(device_data->device, data->render_pass, NULL);
for (uint32_t i = 0; i < ARRAY_SIZE(data->frame_data); i++) {
device_data->vtable.FreeCommandBuffers(device_data->device,
data->command_pool,
1, &data->frame_data[i].command_buffer);
if (data->frame_data[i].vertex_buffer)
device_data->vtable.DestroyBuffer(device_data->device, data->frame_data[i].vertex_buffer, NULL);
if (data->frame_data[i].index_buffer)
device_data->vtable.DestroyBuffer(device_data->device, data->frame_data[i].index_buffer, NULL);
if (data->frame_data[i].vertex_buffer_mem)
device_data->vtable.FreeMemory(device_data->device, data->frame_data[i].vertex_buffer_mem, NULL);
if (data->frame_data[i].index_buffer_mem)
device_data->vtable.FreeMemory(device_data->device, data->frame_data[i].index_buffer_mem, NULL);
}
device_data->vtable.DestroyCommandPool(device_data->device, data->command_pool, NULL);
if (data->submission_semaphore)
device_data->vtable.DestroySemaphore(device_data->device, data->submission_semaphore, NULL);
device_data->vtable.DestroyPipeline(device_data->device, data->pipeline, NULL);
device_data->vtable.DestroyPipelineLayout(device_data->device, data->pipeline_layout, NULL);
device_data->vtable.DestroyDescriptorPool(device_data->device,
data->descriptor_pool, NULL);
device_data->vtable.DestroyDescriptorSetLayout(device_data->device,
data->descriptor_layout, NULL);
device_data->vtable.DestroySampler(device_data->device, data->font_sampler, NULL);
device_data->vtable.DestroyImageView(device_data->device, data->font_image_view, NULL);
device_data->vtable.DestroyImage(device_data->device, data->font_image, NULL);
device_data->vtable.FreeMemory(device_data->device, data->font_mem, NULL);
device_data->vtable.DestroyBuffer(device_data->device, data->upload_font_buffer, NULL);
device_data->vtable.FreeMemory(device_data->device, data->upload_font_buffer_mem, NULL);
ImGui::DestroyContext(data->imgui_context);
}
static void before_present(struct swapchain_data *swapchain_data,
const VkSemaphore *wait_semaphores,
unsigned n_wait_semaphores,
unsigned imageIndex)
{
struct instance_data *instance_data = swapchain_data->device->instance;
snapshot_swapchain_frame(swapchain_data);
if (!instance_data->params.no_display && swapchain_data->n_frames > 0) {
compute_swapchain_display(swapchain_data);
render_swapchain_display(swapchain_data, wait_semaphores, n_wait_semaphores, imageIndex);
}
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_CreateSwapchainKHR(
VkDevice device,
const VkSwapchainCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkSwapchainKHR* pSwapchain)
{
struct device_data *device_data = FIND_DEVICE_DATA(device);
VkResult result = device_data->vtable.CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
if (result != VK_SUCCESS) return result;
struct swapchain_data *swapchain_data = new_swapchain_data(*pSwapchain, device_data);
setup_swapchain_data(swapchain_data, pCreateInfo);
return result;
}
VKAPI_ATTR void VKAPI_CALL overlay_DestroySwapchainKHR(
VkDevice device,
VkSwapchainKHR swapchain,
const VkAllocationCallbacks* pAllocator)
{
struct swapchain_data *swapchain_data = FIND_SWAPCHAIN_DATA(swapchain);
shutdown_swapchain_data(swapchain_data);
swapchain_data->device->vtable.DestroySwapchainKHR(device, swapchain, pAllocator);
destroy_swapchain_data(swapchain_data);
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_QueuePresentKHR(
VkQueue queue,
const VkPresentInfoKHR* pPresentInfo)
{
struct queue_data *queue_data = FIND_QUEUE_DATA(queue);
struct device_data *device_data = queue_data->device;
struct instance_data *instance_data = device_data->instance;
uint32_t query_results[OVERLAY_QUERY_COUNT];
if (list_length(&queue_data->running_command_buffer) > 0) {
/* Before getting the query results, make sure the operations have
* completed.
*/
VkResult err = device_data->vtable.ResetFences(device_data->device,
1, &queue_data->queries_fence);
check_vk_result(err);
err = device_data->vtable.QueueSubmit(queue, 0, NULL, queue_data->queries_fence);
check_vk_result(err);
err = device_data->vtable.WaitForFences(device_data->device,
1, &queue_data->queries_fence,
VK_FALSE, UINT64_MAX);
check_vk_result(err);
/* Now get the results. */
list_for_each_entry_safe(struct command_buffer_data, cmd_buffer_data,
&queue_data->running_command_buffer, link) {
list_delinit(&cmd_buffer_data->link);
if (cmd_buffer_data->pipeline_query_pool) {
memset(query_results, 0, sizeof(query_results));
err =
device_data->vtable.GetQueryPoolResults(device_data->device,
cmd_buffer_data->pipeline_query_pool,
cmd_buffer_data->query_index, 1,
sizeof(uint32_t) * OVERLAY_QUERY_COUNT,
query_results, 0, VK_QUERY_RESULT_WAIT_BIT);
check_vk_result(err);
for (uint32_t i = OVERLAY_PARAM_ENABLED_vertices;
i <= OVERLAY_PARAM_ENABLED_compute_invocations; i++) {
device_data->frame_stats.stats[i] += query_results[i - OVERLAY_PARAM_ENABLED_vertices];
}
}
if (cmd_buffer_data->timestamp_query_pool) {
uint64_t gpu_timestamps[2] = { 0 };
err =
device_data->vtable.GetQueryPoolResults(device_data->device,
cmd_buffer_data->timestamp_query_pool,
cmd_buffer_data->query_index * 2, 2,
2 * sizeof(uint64_t), gpu_timestamps, sizeof(uint64_t),
VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT);
check_vk_result(err);
gpu_timestamps[0] &= queue_data->timestamp_mask;
gpu_timestamps[1] &= queue_data->timestamp_mask;
device_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_gpu_timing] +=
(gpu_timestamps[1] - gpu_timestamps[0]) *
device_data->properties.limits.timestampPeriod;
}
}
}
/* Otherwise we need to add our overlay drawing semaphore to the list of
* semaphores to wait on. If we don't do that the presented picture might
* be have incomplete overlay drawings.
*/
VkResult result = VK_SUCCESS;
if (instance_data->params.no_display) {
for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
VkSwapchainKHR swapchain = pPresentInfo->pSwapchains[i];
struct swapchain_data *swapchain_data = FIND_SWAPCHAIN_DATA(swapchain);
before_present(swapchain_data,
pPresentInfo->pWaitSemaphores,
pPresentInfo->waitSemaphoreCount,
pPresentInfo->pImageIndices[i]);
}
result = queue_data->device->vtable.QueuePresentKHR(queue, pPresentInfo);
} else {
for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
VkSwapchainKHR swapchain = pPresentInfo->pSwapchains[i];
struct swapchain_data *swapchain_data = FIND_SWAPCHAIN_DATA(swapchain);
VkPresentInfoKHR present_info = *pPresentInfo;
present_info.swapchainCount = 1;
present_info.pSwapchains = &swapchain;
before_present(swapchain_data,
pPresentInfo->pWaitSemaphores,
pPresentInfo->waitSemaphoreCount,
pPresentInfo->pImageIndices[i]);
/* Because the submission of the overlay draw waits on the semaphores
* handed for present, we don't need to have this present operation
* wait on them as well, we can just wait on the overlay submission
* semaphore.
*/
present_info.pWaitSemaphores = &swapchain_data->submission_semaphore;
present_info.waitSemaphoreCount = 1;
VkResult chain_result = queue_data->device->vtable.QueuePresentKHR(queue, &present_info);
if (pPresentInfo->pResults)
pPresentInfo->pResults[i] = chain_result;
if (chain_result != VK_SUCCESS && result == VK_SUCCESS)
result = chain_result;
}
}
return result;
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_AcquireNextImageKHR(
VkDevice device,
VkSwapchainKHR swapchain,
uint64_t timeout,
VkSemaphore semaphore,
VkFence fence,
uint32_t* pImageIndex)
{
struct swapchain_data *swapchain_data = FIND_SWAPCHAIN_DATA(swapchain);
struct device_data *device_data = swapchain_data->device;
uint64_t ts0 = os_time_get();
VkResult result = device_data->vtable.AcquireNextImageKHR(device, swapchain, timeout,
semaphore, fence, pImageIndex);
uint64_t ts1 = os_time_get();
swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_acquire_timing] += ts1 - ts0;
swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_acquire]++;
return result;
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_AcquireNextImage2KHR(
VkDevice device,
const VkAcquireNextImageInfoKHR* pAcquireInfo,
uint32_t* pImageIndex)
{
struct swapchain_data *swapchain_data = FIND_SWAPCHAIN_DATA(pAcquireInfo->swapchain);
struct device_data *device_data = swapchain_data->device;
uint64_t ts0 = os_time_get();
VkResult result = device_data->vtable.AcquireNextImage2KHR(device, pAcquireInfo, pImageIndex);
uint64_t ts1 = os_time_get();
swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_acquire_timing] += ts1 - ts0;
swapchain_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_acquire]++;
return result;
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdDraw(
VkCommandBuffer commandBuffer,
uint32_t vertexCount,
uint32_t instanceCount,
uint32_t firstVertex,
uint32_t firstInstance)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw]++;
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdDraw(commandBuffer, vertexCount, instanceCount,
firstVertex, firstInstance);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdDrawIndexed(
VkCommandBuffer commandBuffer,
uint32_t indexCount,
uint32_t instanceCount,
uint32_t firstIndex,
int32_t vertexOffset,
uint32_t firstInstance)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indexed]++;
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdDrawIndexed(commandBuffer, indexCount, instanceCount,
firstIndex, vertexOffset, firstInstance);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdDrawIndirect(
VkCommandBuffer commandBuffer,
VkBuffer buffer,
VkDeviceSize offset,
uint32_t drawCount,
uint32_t stride)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indirect]++;
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdDrawIndexedIndirect(
VkCommandBuffer commandBuffer,
VkBuffer buffer,
VkDeviceSize offset,
uint32_t drawCount,
uint32_t stride)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indexed_indirect]++;
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdDrawIndirectCountKHR(
VkCommandBuffer commandBuffer,
VkBuffer buffer,
VkDeviceSize offset,
VkBuffer countBuffer,
VkDeviceSize countBufferOffset,
uint32_t maxDrawCount,
uint32_t stride)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indirect_count]++;
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdDrawIndirectCountKHR(commandBuffer, buffer, offset,
countBuffer, countBufferOffset,
maxDrawCount, stride);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdDrawIndexedIndirectCountKHR(
VkCommandBuffer commandBuffer,
VkBuffer buffer,
VkDeviceSize offset,
VkBuffer countBuffer,
VkDeviceSize countBufferOffset,
uint32_t maxDrawCount,
uint32_t stride)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_draw_indexed_indirect_count]++;
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset,
countBuffer, countBufferOffset,
maxDrawCount, stride);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdDispatch(
VkCommandBuffer commandBuffer,
uint32_t groupCountX,
uint32_t groupCountY,
uint32_t groupCountZ)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_dispatch]++;
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdDispatch(commandBuffer, groupCountX, groupCountY, groupCountZ);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdDispatchIndirect(
VkCommandBuffer commandBuffer,
VkBuffer buffer,
VkDeviceSize offset)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_dispatch_indirect]++;
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdDispatchIndirect(commandBuffer, buffer, offset);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdBindPipeline(
VkCommandBuffer commandBuffer,
VkPipelineBindPoint pipelineBindPoint,
VkPipeline pipeline)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
switch (pipelineBindPoint) {
case VK_PIPELINE_BIND_POINT_GRAPHICS: cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_pipeline_graphics]++; break;
case VK_PIPELINE_BIND_POINT_COMPUTE: cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_pipeline_compute]++; break;
case VK_PIPELINE_BIND_POINT_RAY_TRACING_NV: cmd_buffer_data->stats.stats[OVERLAY_PARAM_ENABLED_pipeline_raytracing]++; break;
default: break;
}
struct device_data *device_data = cmd_buffer_data->device;
device_data->vtable.CmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_BeginCommandBuffer(
VkCommandBuffer commandBuffer,
const VkCommandBufferBeginInfo* pBeginInfo)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
struct device_data *device_data = cmd_buffer_data->device;
/* We don't record any query in secondary command buffers, just make sure
* we have the right inheritance.
*/
if (cmd_buffer_data->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
VkCommandBufferBeginInfo *begin_info = (VkCommandBufferBeginInfo *)
clone_chain((const struct VkBaseInStructure *)pBeginInfo);
VkCommandBufferInheritanceInfo *parent_inhe_info = (VkCommandBufferInheritanceInfo *)
vk_find_struct(begin_info, COMMAND_BUFFER_INHERITANCE_INFO);
VkCommandBufferInheritanceInfo inhe_info = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
NULL,
VK_NULL_HANDLE,
0,
VK_NULL_HANDLE,
VK_FALSE,
0,
overlay_query_flags,
};
if (parent_inhe_info)
parent_inhe_info->pipelineStatistics = overlay_query_flags;
else {
inhe_info.pNext = begin_info->pNext;
begin_info->pNext = &inhe_info;
}
VkResult result = device_data->vtable.BeginCommandBuffer(commandBuffer, pBeginInfo);
if (!parent_inhe_info)
begin_info->pNext = inhe_info.pNext;
free_chain((struct VkBaseOutStructure *)begin_info);
return result;
}
/* Primary command buffers with no queries. */
if (!cmd_buffer_data->pipeline_query_pool && cmd_buffer_data->timestamp_query_pool)
return device_data->vtable.BeginCommandBuffer(commandBuffer, pBeginInfo);
/* Otherwise record a begin query as first command. */
VkResult result = device_data->vtable.BeginCommandBuffer(commandBuffer, pBeginInfo);
if (result == VK_SUCCESS) {
if (cmd_buffer_data->pipeline_query_pool) {
device_data->vtable.CmdResetQueryPool(commandBuffer,
cmd_buffer_data->pipeline_query_pool,
cmd_buffer_data->query_index, 1);
}
if (cmd_buffer_data->timestamp_query_pool) {
device_data->vtable.CmdResetQueryPool(commandBuffer,
cmd_buffer_data->timestamp_query_pool,
cmd_buffer_data->query_index * 2, 2);
}
if (cmd_buffer_data->pipeline_query_pool) {
device_data->vtable.CmdBeginQuery(commandBuffer,
cmd_buffer_data->pipeline_query_pool,
cmd_buffer_data->query_index, 0);
}
if (cmd_buffer_data->timestamp_query_pool) {
device_data->vtable.CmdWriteTimestamp(commandBuffer,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
cmd_buffer_data->timestamp_query_pool,
cmd_buffer_data->query_index * 2);
}
}
return result;
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_EndCommandBuffer(
VkCommandBuffer commandBuffer)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
struct device_data *device_data = cmd_buffer_data->device;
if (cmd_buffer_data->timestamp_query_pool) {
device_data->vtable.CmdWriteTimestamp(commandBuffer,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
cmd_buffer_data->timestamp_query_pool,
cmd_buffer_data->query_index * 2 + 1);
}
if (cmd_buffer_data->pipeline_query_pool) {
device_data->vtable.CmdEndQuery(commandBuffer,
cmd_buffer_data->pipeline_query_pool,
cmd_buffer_data->query_index);
}
return device_data->vtable.EndCommandBuffer(commandBuffer);
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_ResetCommandBuffer(
VkCommandBuffer commandBuffer,
VkCommandBufferResetFlags flags)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
struct device_data *device_data = cmd_buffer_data->device;
memset(&cmd_buffer_data->stats, 0, sizeof(cmd_buffer_data->stats));
return device_data->vtable.ResetCommandBuffer(commandBuffer, flags);
}
VKAPI_ATTR void VKAPI_CALL overlay_CmdExecuteCommands(
VkCommandBuffer commandBuffer,
uint32_t commandBufferCount,
const VkCommandBuffer* pCommandBuffers)
{
struct command_buffer_data *cmd_buffer_data = FIND_CMD_BUFFER_DATA(commandBuffer);
struct device_data *device_data = cmd_buffer_data->device;
/* Add the stats of the executed command buffers to the primary one. */
for (uint32_t c = 0; c < commandBufferCount; c++) {
struct command_buffer_data *sec_cmd_buffer_data = FIND_CMD_BUFFER_DATA(pCommandBuffers[c]);
for (uint32_t s = 0; s < OVERLAY_PARAM_ENABLED_MAX; s++)
cmd_buffer_data->stats.stats[s] += sec_cmd_buffer_data->stats.stats[s];
}
device_data->vtable.CmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_AllocateCommandBuffers(
VkDevice device,
const VkCommandBufferAllocateInfo* pAllocateInfo,
VkCommandBuffer* pCommandBuffers)
{
struct device_data *device_data = FIND_DEVICE_DATA(device);
VkResult result =
device_data->vtable.AllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
if (result != VK_SUCCESS)
return result;
VkQueryPool pipeline_query_pool = VK_NULL_HANDLE;
VkQueryPool timestamp_query_pool = VK_NULL_HANDLE;
if (device_data->instance->pipeline_statistics_enabled &&
pAllocateInfo->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
VkQueryPoolCreateInfo pool_info = {
VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
NULL,
0,
VK_QUERY_TYPE_PIPELINE_STATISTICS,
pAllocateInfo->commandBufferCount,
overlay_query_flags,
};
VkResult err =
device_data->vtable.CreateQueryPool(device_data->device, &pool_info,
NULL, &pipeline_query_pool);
check_vk_result(err);
}
if (device_data->instance->params.enabled[OVERLAY_PARAM_ENABLED_gpu_timing]) {
VkQueryPoolCreateInfo pool_info = {
VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
NULL,
0,
VK_QUERY_TYPE_TIMESTAMP,
pAllocateInfo->commandBufferCount * 2,
0,
};
VkResult err =
device_data->vtable.CreateQueryPool(device_data->device, &pool_info,
NULL, ×tamp_query_pool);
check_vk_result(err);
}
for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) {
new_command_buffer_data(pCommandBuffers[i], pAllocateInfo->level,
pipeline_query_pool, timestamp_query_pool,
i, device_data);
}
if (pipeline_query_pool)
map_object(pipeline_query_pool, (void *)(uintptr_t) pAllocateInfo->commandBufferCount);
if (timestamp_query_pool)
map_object(timestamp_query_pool, (void *)(uintptr_t) pAllocateInfo->commandBufferCount);
return result;
}
VKAPI_ATTR void VKAPI_CALL overlay_FreeCommandBuffers(
VkDevice device,
VkCommandPool commandPool,
uint32_t commandBufferCount,
const VkCommandBuffer* pCommandBuffers)
{
struct device_data *device_data = FIND_DEVICE_DATA(device);
for (uint32_t i = 0; i < commandBufferCount; i++) {
struct command_buffer_data *cmd_buffer_data =
FIND_CMD_BUFFER_DATA(pCommandBuffers[i]);
uint64_t count = (uintptr_t)find_object_data((void *)cmd_buffer_data->pipeline_query_pool);
if (count == 1) {
unmap_object(cmd_buffer_data->pipeline_query_pool);
device_data->vtable.DestroyQueryPool(device_data->device,
cmd_buffer_data->pipeline_query_pool, NULL);
} else if (count != 0) {
map_object(cmd_buffer_data->pipeline_query_pool, (void *)(uintptr_t)(count - 1));
}
count = (uintptr_t)find_object_data((void *)cmd_buffer_data->timestamp_query_pool);
if (count == 1) {
unmap_object(cmd_buffer_data->timestamp_query_pool);
device_data->vtable.DestroyQueryPool(device_data->device,
cmd_buffer_data->timestamp_query_pool, NULL);
} else if (count != 0) {
map_object(cmd_buffer_data->timestamp_query_pool, (void *)(uintptr_t)(count - 1));
}
destroy_command_buffer_data(cmd_buffer_data);
}
device_data->vtable.FreeCommandBuffers(device, commandPool,
commandBufferCount, pCommandBuffers);
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_QueueSubmit(
VkQueue queue,
uint32_t submitCount,
const VkSubmitInfo* pSubmits,
VkFence fence)
{
struct queue_data *queue_data = FIND_QUEUE_DATA(queue);
struct device_data *device_data = queue_data->device;
device_data->frame_stats.stats[OVERLAY_PARAM_ENABLED_submit]++;
for (uint32_t s = 0; s < submitCount; s++) {
for (uint32_t c = 0; c < pSubmits[s].commandBufferCount; c++) {
struct command_buffer_data *cmd_buffer_data =
FIND_CMD_BUFFER_DATA(pSubmits[s].pCommandBuffers[c]);
/* Merge the submitted command buffer stats into the device. */
for (uint32_t st = 0; st < OVERLAY_PARAM_ENABLED_MAX; st++)
device_data->frame_stats.stats[st] += cmd_buffer_data->stats.stats[st];
/* Attach the command buffer to the queue so we remember to read its
* pipeline statistics & timestamps at QueuePresent().
*/
if (!cmd_buffer_data->pipeline_query_pool &&
!cmd_buffer_data->timestamp_query_pool)
continue;
if (list_empty(&cmd_buffer_data->link)) {
list_addtail(&cmd_buffer_data->link,
&queue_data->running_command_buffer);
} else {
fprintf(stderr, "Command buffer submitted multiple times before present.\n"
"This could lead to invalid data.\n");
}
}
}
return device_data->vtable.QueueSubmit(queue, submitCount, pSubmits, fence);
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_CreateDevice(
VkPhysicalDevice physicalDevice,
const VkDeviceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkDevice* pDevice)
{
struct instance_data *instance_data = FIND_PHYSICAL_DEVICE_DATA(physicalDevice);
VkLayerDeviceCreateInfo *chain_info =
get_device_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
assert(chain_info->u.pLayerInfo);
PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
if (fpCreateDevice == NULL) {
return VK_ERROR_INITIALIZATION_FAILED;
}
// Advance the link info for the next element on the chain
chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
VkPhysicalDeviceFeatures device_features = {};
VkDeviceCreateInfo device_info = *pCreateInfo;
if (pCreateInfo->pEnabledFeatures)
device_features = *(pCreateInfo->pEnabledFeatures);
if (instance_data->pipeline_statistics_enabled) {
device_features.inheritedQueries = true;
device_features.pipelineStatisticsQuery = true;
}
device_info.pEnabledFeatures = &device_features;
VkResult result = fpCreateDevice(physicalDevice, &device_info, pAllocator, pDevice);
if (result != VK_SUCCESS) return result;
struct device_data *device_data = new_device_data(*pDevice, instance_data);
device_data->physical_device = physicalDevice;
vk_load_device_commands(*pDevice, fpGetDeviceProcAddr, &device_data->vtable);
instance_data->vtable.GetPhysicalDeviceProperties(device_data->physical_device,
&device_data->properties);
VkLayerDeviceCreateInfo *load_data_info =
get_device_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK);
device_data->set_device_loader_data = load_data_info->u.pfnSetDeviceLoaderData;
device_map_queues(device_data, pCreateInfo);
return result;
}
VKAPI_ATTR void VKAPI_CALL overlay_DestroyDevice(
VkDevice device,
const VkAllocationCallbacks* pAllocator)
{
struct device_data *device_data = FIND_DEVICE_DATA(device);
device_unmap_queues(device_data);
device_data->vtable.DestroyDevice(device, pAllocator);
destroy_device_data(device_data);
}
VKAPI_ATTR VkResult VKAPI_CALL overlay_CreateInstance(
const VkInstanceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance)
{
VkLayerInstanceCreateInfo *chain_info =
get_instance_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
assert(chain_info->u.pLayerInfo);
PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
PFN_vkCreateInstance fpCreateInstance =
(PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
if (fpCreateInstance == NULL) {
return VK_ERROR_INITIALIZATION_FAILED;
}
// Advance the link info for the next element on the chain
chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
if (result != VK_SUCCESS) return result;
struct instance_data *instance_data = new_instance_data(*pInstance);
vk_load_instance_commands(instance_data->instance,
fpGetInstanceProcAddr,
&instance_data->vtable);
instance_data_map_physical_devices(instance_data, true);
parse_overlay_env(&instance_data->params, getenv("VK_LAYER_MESA_OVERLAY_CONFIG"));
for (int i = OVERLAY_PARAM_ENABLED_vertices;
i <= OVERLAY_PARAM_ENABLED_compute_invocations; i++) {
if (instance_data->params.enabled[i]) {
instance_data->pipeline_statistics_enabled = true;
break;
}
}
return result;
}
VKAPI_ATTR void VKAPI_CALL overlay_DestroyInstance(
VkInstance instance,
const VkAllocationCallbacks* pAllocator)
{
struct instance_data *instance_data = FIND_INSTANCE_DATA(instance);
instance_data_map_physical_devices(instance_data, false);
instance_data->vtable.DestroyInstance(instance, pAllocator);
destroy_instance_data(instance_data);
}
static const struct {
const char *name;
void *ptr;
} name_to_funcptr_map[] = {
{ "vkGetDeviceProcAddr", (void *) vkGetDeviceProcAddr },
#define ADD_HOOK(fn) { "vk" # fn, (void *) overlay_ ## fn }
ADD_HOOK(AllocateCommandBuffers),
ADD_HOOK(FreeCommandBuffers),
ADD_HOOK(ResetCommandBuffer),
ADD_HOOK(BeginCommandBuffer),
ADD_HOOK(EndCommandBuffer),
ADD_HOOK(CmdExecuteCommands),
ADD_HOOK(CmdDraw),
ADD_HOOK(CmdDrawIndexed),
ADD_HOOK(CmdDrawIndexedIndirect),
ADD_HOOK(CmdDispatch),
ADD_HOOK(CmdDispatchIndirect),
ADD_HOOK(CmdDrawIndirectCountKHR),
ADD_HOOK(CmdDrawIndexedIndirectCountKHR),
ADD_HOOK(CmdBindPipeline),
ADD_HOOK(CreateSwapchainKHR),
ADD_HOOK(QueuePresentKHR),
ADD_HOOK(DestroySwapchainKHR),
ADD_HOOK(AcquireNextImageKHR),
ADD_HOOK(AcquireNextImage2KHR),
ADD_HOOK(QueueSubmit),
ADD_HOOK(CreateDevice),
ADD_HOOK(DestroyDevice),
ADD_HOOK(CreateInstance),
ADD_HOOK(DestroyInstance),
#undef ADD_HOOK
};
static void *find_ptr(const char *name)
{
for (uint32_t i = 0; i < ARRAY_SIZE(name_to_funcptr_map); i++) {
if (strcmp(name, name_to_funcptr_map[i].name) == 0)
return name_to_funcptr_map[i].ptr;
}
return NULL;
}
VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev,
const char *funcName)
{
void *ptr = find_ptr(funcName);
if (ptr) return reinterpret_cast<PFN_vkVoidFunction>(ptr);
if (dev == NULL) return NULL;
struct device_data *device_data = FIND_DEVICE_DATA(dev);
if (device_data->vtable.GetDeviceProcAddr == NULL) return NULL;
return device_data->vtable.GetDeviceProcAddr(dev, funcName);
}
VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance,
const char *funcName)
{
void *ptr = find_ptr(funcName);
if (ptr) return reinterpret_cast<PFN_vkVoidFunction>(ptr);
if (instance == NULL) return NULL;
struct instance_data *instance_data = FIND_INSTANCE_DATA(instance);
if (instance_data->vtable.GetInstanceProcAddr == NULL) return NULL;
return instance_data->vtable.GetInstanceProcAddr(instance, funcName);
}
|