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 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
|
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#define GLAD_VULKAN_IMPLEMENTATION
#include <vulkan.h>
// Include graphics because we use sf::Image for loading images
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <algorithm>
#include <array>
#include <iostream>
#include <limits>
#include <string_view>
#include <vector>
#include <cmath>
#include <cstdint>
#include <cstring>
////////////////////////////////////////////////////////////
// Helper functions
////////////////////////////////////////////////////////////
namespace
{
using Matrix = std::array<std::array<float, 4>, 4>;
// Multiply 2 matrices
void matrixMultiply(Matrix& result, const Matrix& left, const Matrix& right)
{
Matrix temp;
for (std::size_t i = 0; i < temp.size(); ++i)
{
for (std::size_t j = 0; j < temp[0].size(); ++j)
temp[i][j] = left[0][j] * right[i][0] + left[1][j] * right[i][1] + left[2][j] * right[i][2] +
left[3][j] * right[i][3];
}
result = temp;
}
// Rotate a matrix around the x-axis
void matrixRotateX(Matrix& result, sf::Angle angle)
{
const float rad = angle.asRadians();
// clang-format off
const Matrix matrix = {{
{1.f, 0.f, 0.f, 0.f},
{0.f, std::cos(rad), std::sin(rad), 0.f},
{0.f, -std::sin(rad), std::cos(rad), 0.f},
{0.f, 0.f, 0.f, 1.f}
}};
// clang-format on
matrixMultiply(result, result, matrix);
}
// Rotate a matrix around the y-axis
void matrixRotateY(Matrix& result, sf::Angle angle)
{
const float rad = angle.asRadians();
// clang-format off
const Matrix matrix = {{
{ std::cos(rad), 0.f, std::sin(rad), 0.f},
{ 0.f, 1.f, 0.f, 0.f},
{-std::sin(rad), 0.f, std::cos(rad), 0.f},
{ 0.f, 0.f, 0.f, 1.f}
}};
// clang-format on
matrixMultiply(result, result, matrix);
}
// Rotate a matrix around the z-axis
void matrixRotateZ(Matrix& result, sf::Angle angle)
{
const float rad = angle.asRadians();
// clang-format off
const Matrix matrix = {{
{ std::cos(rad), std::sin(rad), 0.f, 0.f},
{-std::sin(rad), std::cos(rad), 0.f, 0.f},
{ 0.f, 0.f, 1.f, 0.f},
{ 0.f, 0.f, 0.f, 1.f}
}};
// clang-format on
matrixMultiply(result, result, matrix);
}
// Construct a lookat view matrix
void matrixLookAt(Matrix& result, const sf::Vector3f& eye, const sf::Vector3f& center, const sf::Vector3f& up)
{
// Forward-looking vector
const sf::Vector3f forward = (center - eye).normalized();
// Side vector (Forward cross product Up)
const sf::Vector3f side = forward.cross(up).normalized();
result[0][0] = side.x;
result[0][1] = side.y * forward.z - side.z * forward.y;
result[0][2] = -forward.x;
result[0][3] = 0.f;
result[1][0] = side.y;
result[1][1] = side.z * forward.x - side.x * forward.z;
result[1][2] = -forward.y;
result[1][3] = 0.f;
result[2][0] = side.z;
result[2][1] = side.x * forward.y - side.y * forward.x;
result[2][2] = -forward.z;
result[2][3] = 0.f;
result[3][0] = (-eye.x) * result[0][0] + (-eye.y) * result[1][0] + (-eye.z) * result[2][0];
result[3][1] = (-eye.x) * result[0][1] + (-eye.y) * result[1][1] + (-eye.z) * result[2][1];
result[3][2] = (-eye.x) * result[0][2] + (-eye.y) * result[1][2] + (-eye.z) * result[2][2];
result[3][3] = (-eye.x) * result[0][3] + (-eye.y) * result[1][3] + (-eye.z) * result[2][3] + 1.0f;
}
// Construct a perspective projection matrix
void matrixPerspective(Matrix& result, sf::Angle fov, float aspect, float nearPlane, float farPlane)
{
const float a = 1.f / std::tan(fov.asRadians() / 2.f);
result[0][0] = a / aspect;
result[0][1] = 0.f;
result[0][2] = 0.f;
result[0][3] = 0.f;
result[1][0] = 0.f;
result[1][1] = -a;
result[1][2] = 0.f;
result[1][3] = 0.f;
result[2][0] = 0.f;
result[2][1] = 0.f;
result[2][2] = -((farPlane + nearPlane) / (farPlane - nearPlane));
result[2][3] = -1.f;
result[3][0] = 0.f;
result[3][1] = 0.f;
result[3][2] = -((2.f * farPlane * nearPlane) / (farPlane - nearPlane));
result[3][3] = 0.f;
}
// Helper function we pass to GLAD to load Vulkan functions via SFML
GLADapiproc getVulkanFunction(const char* name)
{
return sf::Vulkan::getFunction(name);
}
// Debug we pass to Vulkan to call when it detects warnings or errors
VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
VkDebugReportFlagsEXT,
VkDebugReportObjectTypeEXT,
std::uint64_t,
std::size_t,
std::int32_t,
const char*,
const char* pMessage,
void*)
{
std::cerr << pMessage << std::endl;
return VK_FALSE;
}
} // namespace
////////////////////////////////////////////////////////////
// VulkanExample class
////////////////////////////////////////////////////////////
class VulkanExample
{
public:
// Constructor
VulkanExample()
{
// Vulkan setup procedure
if (vulkanAvailable)
setupInstance();
if (vulkanAvailable)
setupDebugReportCallback();
if (vulkanAvailable)
setupSurface();
if (vulkanAvailable)
setupPhysicalDevice();
if (vulkanAvailable)
setupLogicalDevice();
if (vulkanAvailable)
setupSwapchain();
if (vulkanAvailable)
setupSwapchainImages();
if (vulkanAvailable)
setupShaders();
if (vulkanAvailable)
setupRenderpass();
if (vulkanAvailable)
setupDescriptorSetLayout();
if (vulkanAvailable)
setupPipelineLayout();
if (vulkanAvailable)
setupPipeline();
if (vulkanAvailable)
setupCommandPool();
if (vulkanAvailable)
setupVertexBuffer();
if (vulkanAvailable)
setupIndexBuffer();
if (vulkanAvailable)
setupUniformBuffers();
if (vulkanAvailable)
setupDepthImage();
if (vulkanAvailable)
setupDepthImageView();
if (vulkanAvailable)
setupTextureImage();
if (vulkanAvailable)
setupTextureImageView();
if (vulkanAvailable)
setupTextureSampler();
if (vulkanAvailable)
setupFramebuffers();
if (vulkanAvailable)
setupDescriptorPool();
if (vulkanAvailable)
setupDescriptorSets();
if (vulkanAvailable)
setupCommandBuffers();
if (vulkanAvailable)
setupDraw();
if (vulkanAvailable)
setupSemaphores();
if (vulkanAvailable)
setupFences();
// If something went wrong, notify the user by setting the window title
if (!vulkanAvailable)
window.setTitle("SFML window with Vulkan (Vulkan not available)");
}
// Destructor
~VulkanExample()
{
// Wait until there are no pending frames
if (device)
vkDeviceWaitIdle(device);
// Teardown swapchain
cleanupSwapchain();
// Vulkan teardown procedure
for (VkFence fence : fences)
vkDestroyFence(device, fence, nullptr);
for (VkSemaphore renderFinishedSemaphore : renderFinishedSemaphores)
vkDestroySemaphore(device, renderFinishedSemaphore, nullptr);
for (VkSemaphore imageAvailableSemaphore : imageAvailableSemaphores)
vkDestroySemaphore(device, imageAvailableSemaphore, nullptr);
if (descriptorPool)
vkDestroyDescriptorPool(device, descriptorPool, nullptr);
for (VkDeviceMemory i : uniformBuffersMemory)
vkFreeMemory(device, i, nullptr);
for (VkBuffer uniformBuffer : uniformBuffers)
vkDestroyBuffer(device, uniformBuffer, nullptr);
if (textureSampler)
vkDestroySampler(device, textureSampler, nullptr);
if (textureImageView)
vkDestroyImageView(device, textureImageView, nullptr);
if (textureImageMemory)
vkFreeMemory(device, textureImageMemory, nullptr);
if (textureImage)
vkDestroyImage(device, textureImage, nullptr);
if (indexBufferMemory)
vkFreeMemory(device, indexBufferMemory, nullptr);
if (indexBuffer)
vkDestroyBuffer(device, indexBuffer, nullptr);
if (vertexBufferMemory)
vkFreeMemory(device, vertexBufferMemory, nullptr);
if (vertexBuffer)
vkDestroyBuffer(device, vertexBuffer, nullptr);
if (commandPool)
vkDestroyCommandPool(device, commandPool, nullptr);
if (descriptorSetLayout)
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
if (fragmentShaderModule)
vkDestroyShaderModule(device, fragmentShaderModule, nullptr);
if (vertexShaderModule)
vkDestroyShaderModule(device, vertexShaderModule, nullptr);
if (device)
vkDestroyDevice(device, nullptr);
if (surface)
vkDestroySurfaceKHR(instance, surface, nullptr);
if (debugReportCallback)
vkDestroyDebugReportCallbackEXT(instance, debugReportCallback, nullptr);
if (instance)
vkDestroyInstance(instance, nullptr);
}
// Cleanup swapchain
void cleanupSwapchain()
{
// Swapchain teardown procedure
for (VkFence fence : fences)
vkWaitForFences(device, 1, &fence, VK_TRUE, std::numeric_limits<std::uint64_t>::max());
if (!commandBuffers.empty())
vkFreeCommandBuffers(device, commandPool, static_cast<std::uint32_t>(commandBuffers.size()), commandBuffers.data());
commandBuffers.clear();
for (VkFramebuffer swapchainFramebuffer : swapchainFramebuffers)
vkDestroyFramebuffer(device, swapchainFramebuffer, nullptr);
swapchainFramebuffers.clear();
if (graphicsPipeline)
vkDestroyPipeline(device, graphicsPipeline, nullptr);
if (renderPass)
vkDestroyRenderPass(device, renderPass, nullptr);
if (pipelineLayout)
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
if (depthImageView)
vkDestroyImageView(device, depthImageView, nullptr);
if (depthImageMemory)
vkFreeMemory(device, depthImageMemory, nullptr);
if (depthImage)
vkDestroyImage(device, depthImage, nullptr);
for (VkImageView swapchainImageView : swapchainImageViews)
vkDestroyImageView(device, swapchainImageView, nullptr);
swapchainImageViews.clear();
if (swapchain)
vkDestroySwapchainKHR(device, swapchain, nullptr);
}
// Cleanup and recreate swapchain
void recreateSwapchain()
{
// Wait until there are no pending frames
vkDeviceWaitIdle(device);
// Cleanup swapchain
cleanupSwapchain();
// Swapchain setup procedure
if (vulkanAvailable)
setupSwapchain();
if (vulkanAvailable)
setupSwapchainImages();
if (vulkanAvailable)
setupPipelineLayout();
if (vulkanAvailable)
setupRenderpass();
if (vulkanAvailable)
setupPipeline();
if (vulkanAvailable)
setupDepthImage();
if (vulkanAvailable)
setupDepthImageView();
if (vulkanAvailable)
setupFramebuffers();
if (vulkanAvailable)
setupCommandBuffers();
if (vulkanAvailable)
setupDraw();
}
// Setup Vulkan instance
void setupInstance()
{
// Load bootstrap entry points
gladLoadVulkan({}, getVulkanFunction);
if (!vkCreateInstance)
{
vulkanAvailable = false;
return;
}
// Retrieve the available instance layers
std::uint32_t objectCount = 0;
std::vector<VkLayerProperties> layers;
if (vkEnumerateInstanceLayerProperties(&objectCount, nullptr) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
layers.resize(objectCount);
if (vkEnumerateInstanceLayerProperties(&objectCount, layers.data()) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
// Activate the layers we are interested in
std::vector<const char*> validationLayers;
for (VkLayerProperties& layer : layers)
{
// VK_LAYER_LUNARG_standard_validation, meta-layer for the following layers:
// -- VK_LAYER_GOOGLE_threading
// -- VK_LAYER_LUNARG_parameter_validation
// -- VK_LAYER_LUNARG_device_limits
// -- VK_LAYER_LUNARG_object_tracker
// -- VK_LAYER_LUNARG_image
// -- VK_LAYER_LUNARG_core_validation
// -- VK_LAYER_LUNARG_swapchain
// -- VK_LAYER_GOOGLE_unique_objects
// These layers perform error checking and warn about bad or sub-optimal Vulkan API usage
// VK_LAYER_LUNARG_monitor appends an FPS counter to the window title
if (std::string_view(layer.layerName) == "VK_LAYER_LUNARG_standard_validation")
{
validationLayers.push_back("VK_LAYER_LUNARG_standard_validation");
}
else if (std::string_view(layer.layerName) == "VK_LAYER_LUNARG_monitor")
{
validationLayers.push_back("VK_LAYER_LUNARG_monitor");
}
}
// Retrieve the extensions we need to enable in order to use Vulkan with SFML
std::vector<const char*> requiredExtensions = sf::Vulkan::getGraphicsRequiredInstanceExtensions();
requiredExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
// Register our application information
VkApplicationInfo applicationInfo = VkApplicationInfo();
applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
applicationInfo.pApplicationName = "SFML Vulkan Test";
applicationInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
applicationInfo.pEngineName = "SFML Vulkan Test Engine";
applicationInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
applicationInfo.apiVersion = VK_API_VERSION_1_0;
VkInstanceCreateInfo instanceCreateInfo = VkInstanceCreateInfo();
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceCreateInfo.pApplicationInfo = &applicationInfo;
instanceCreateInfo.enabledLayerCount = static_cast<std::uint32_t>(validationLayers.size());
instanceCreateInfo.ppEnabledLayerNames = validationLayers.data();
instanceCreateInfo.enabledExtensionCount = static_cast<std::uint32_t>(requiredExtensions.size());
instanceCreateInfo.ppEnabledExtensionNames = requiredExtensions.data();
// Try to create a Vulkan instance with debug report enabled
VkResult result = vkCreateInstance(&instanceCreateInfo, nullptr, &instance);
// If an extension is missing, try disabling debug report
if (result == VK_ERROR_EXTENSION_NOT_PRESENT)
{
requiredExtensions.pop_back();
instanceCreateInfo.enabledExtensionCount = static_cast<std::uint32_t>(requiredExtensions.size());
instanceCreateInfo.ppEnabledExtensionNames = requiredExtensions.data();
result = vkCreateInstance(&instanceCreateInfo, nullptr, &instance);
}
// If instance creation still fails, give up
if (result != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
// Load instance entry points
gladLoadVulkan({}, getVulkanFunction);
}
// Setup our debug callback function to be called by Vulkan
void setupDebugReportCallback()
{
// Don't try to register the callback if the extension is not available
if (!vkCreateDebugReportCallbackEXT)
return;
// Register for warnings and errors
VkDebugReportCallbackCreateInfoEXT debugReportCallbackCreateInfo = VkDebugReportCallbackCreateInfoEXT();
debugReportCallbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
debugReportCallbackCreateInfo.flags = VK_DEBUG_REPORT_WARNING_BIT_EXT |
VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | VK_DEBUG_REPORT_ERROR_BIT_EXT;
debugReportCallbackCreateInfo.pfnCallback = debugCallback;
// Create the debug callback
if (vkCreateDebugReportCallbackEXT(instance, &debugReportCallbackCreateInfo, nullptr, &debugReportCallback) !=
VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Setup the SFML window Vulkan rendering surface
void setupSurface()
{
if (!window.createVulkanSurface(instance, surface))
vulkanAvailable = false;
}
// Select a GPU to use and query its capabilities
void setupPhysicalDevice()
{
// Last sanity check
if (!vkEnumeratePhysicalDevices || !vkCreateDevice || !vkGetPhysicalDeviceProperties)
{
vulkanAvailable = false;
return;
}
// Retrieve list of GPUs
std::uint32_t objectCount = 0;
std::vector<VkPhysicalDevice> devices;
if (vkEnumeratePhysicalDevices(instance, &objectCount, nullptr) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
devices.resize(objectCount);
if (vkEnumeratePhysicalDevices(instance, &objectCount, devices.data()) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
VkPhysicalDeviceType deviceType = VK_PHYSICAL_DEVICE_TYPE_OTHER;
// Look for a GPU that supports swapchains
for (VkPhysicalDevice dev : devices)
{
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(dev, &deviceProperties);
std::vector<VkExtensionProperties> extensions;
if (vkEnumerateDeviceExtensionProperties(dev, nullptr, &objectCount, nullptr) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
extensions.resize(objectCount);
if (vkEnumerateDeviceExtensionProperties(dev, nullptr, &objectCount, extensions.data()) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
bool supportsSwapchain = false;
for (VkExtensionProperties& extension : extensions)
{
if (std::string_view(extension.extensionName) == VK_KHR_SWAPCHAIN_EXTENSION_NAME)
{
supportsSwapchain = true;
break;
}
}
if (!supportsSwapchain)
continue;
// Prefer discrete over integrated GPUs if multiple are available
if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
{
gpu = dev;
break;
}
if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
{
gpu = dev;
deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
}
else if ((deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) &&
(deviceType != VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU))
{
gpu = dev;
deviceType = VK_PHYSICAL_DEVICE_TYPE_CPU;
}
}
if (!gpu)
{
vulkanAvailable = false;
return;
}
// Load physical device entry points
gladLoadVulkan(gpu, getVulkanFunction);
// Check what depth formats are available and select one
VkFormatProperties formatProperties = VkFormatProperties();
vkGetPhysicalDeviceFormatProperties(gpu, VK_FORMAT_D24_UNORM_S8_UINT, &formatProperties);
if (formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
{
depthFormat = VK_FORMAT_D24_UNORM_S8_UINT;
}
else
{
vkGetPhysicalDeviceFormatProperties(gpu, VK_FORMAT_D32_SFLOAT_S8_UINT, &formatProperties);
if (formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
{
depthFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
}
else
{
vkGetPhysicalDeviceFormatProperties(gpu, VK_FORMAT_D32_SFLOAT, &formatProperties);
if (formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
{
depthFormat = VK_FORMAT_D32_SFLOAT;
}
else
{
vulkanAvailable = false;
return;
}
}
}
}
// Setup logical device and device queue
void setupLogicalDevice()
{
// Select a queue family that supports graphics operations and surface presentation
std::uint32_t objectCount = 0;
std::vector<VkQueueFamilyProperties> queueFamilyProperties;
vkGetPhysicalDeviceQueueFamilyProperties(gpu, &objectCount, nullptr);
queueFamilyProperties.resize(objectCount);
vkGetPhysicalDeviceQueueFamilyProperties(gpu, &objectCount, queueFamilyProperties.data());
for (std::size_t i = 0; i < queueFamilyProperties.size(); ++i)
{
VkBool32 surfaceSupported = VK_FALSE;
vkGetPhysicalDeviceSurfaceSupportKHR(gpu, static_cast<std::uint32_t>(i), surface, &surfaceSupported);
if ((queueFamilyProperties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && (surfaceSupported == VK_TRUE))
{
queueFamilyIndex = static_cast<std::uint32_t>(i);
break;
}
}
if (!queueFamilyIndex.has_value())
{
vulkanAvailable = false;
return;
}
const float queuePriority = 1.0f;
VkDeviceQueueCreateInfo deviceQueueCreateInfo = VkDeviceQueueCreateInfo();
deviceQueueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
deviceQueueCreateInfo.queueCount = 1;
deviceQueueCreateInfo.queueFamilyIndex = *queueFamilyIndex;
deviceQueueCreateInfo.pQueuePriorities = &queuePriority;
// Enable the swapchain extension
static constexpr std::array extensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME};
// Enable anisotropic filtering
VkPhysicalDeviceFeatures physicalDeviceFeatures = VkPhysicalDeviceFeatures();
physicalDeviceFeatures.samplerAnisotropy = VK_TRUE;
VkDeviceCreateInfo deviceCreateInfo = VkDeviceCreateInfo();
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceCreateInfo.enabledExtensionCount = static_cast<std::uint32_t>(extensions.size());
deviceCreateInfo.ppEnabledExtensionNames = extensions.data();
deviceCreateInfo.queueCreateInfoCount = 1;
deviceCreateInfo.pQueueCreateInfos = &deviceQueueCreateInfo;
deviceCreateInfo.pEnabledFeatures = &physicalDeviceFeatures;
// Create our logical device
if (vkCreateDevice(gpu, &deviceCreateInfo, nullptr, &device) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
// Retrieve a handle to the logical device command queue
vkGetDeviceQueue(device, *queueFamilyIndex, 0, &queue);
}
// Query surface formats and set up swapchain
void setupSwapchain()
{
// Select a surface format that supports RGBA color format
std::uint32_t objectCount = 0;
std::vector<VkSurfaceFormatKHR> surfaceFormats;
if (vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &objectCount, nullptr) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
surfaceFormats.resize(objectCount);
if (vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &objectCount, surfaceFormats.data()) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
if ((surfaceFormats.size() == 1) && (surfaceFormats[0].format == VK_FORMAT_UNDEFINED))
{
swapchainFormat.format = VK_FORMAT_B8G8R8A8_UNORM;
swapchainFormat.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
}
else if (!surfaceFormats.empty())
{
for (const VkSurfaceFormatKHR& surfaceFormat : surfaceFormats)
{
if ((surfaceFormat.format == VK_FORMAT_B8G8R8A8_UNORM) &&
(surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR))
{
swapchainFormat.format = VK_FORMAT_B8G8R8A8_UNORM;
swapchainFormat.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
break;
}
}
if (swapchainFormat.format == VK_FORMAT_UNDEFINED)
swapchainFormat = surfaceFormats[0];
}
else
{
vulkanAvailable = false;
return;
}
// Select a swapchain present mode
std::vector<VkPresentModeKHR> presentModes;
if (vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, &objectCount, nullptr) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
presentModes.resize(objectCount);
if (vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, &objectCount, presentModes.data()) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
// Prefer mailbox over FIFO if it is available
VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
for (const VkPresentModeKHR& i : presentModes)
{
if (i == VK_PRESENT_MODE_MAILBOX_KHR)
{
presentMode = i;
break;
}
}
// Determine size and count of swapchain images
VkSurfaceCapabilitiesKHR surfaceCapabilities;
if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, surface, &surfaceCapabilities) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
swapchainExtent.width = std::clamp(window.getSize().x,
surfaceCapabilities.minImageExtent.width,
surfaceCapabilities.maxImageExtent.width);
swapchainExtent.height = std::clamp(window.getSize().y,
surfaceCapabilities.minImageExtent.height,
surfaceCapabilities.maxImageExtent.height);
const auto imageCount = std::clamp(2u, surfaceCapabilities.minImageCount, surfaceCapabilities.maxImageCount);
VkSwapchainCreateInfoKHR swapchainCreateInfo = VkSwapchainCreateInfoKHR();
swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchainCreateInfo.surface = surface;
swapchainCreateInfo.minImageCount = imageCount;
swapchainCreateInfo.imageFormat = swapchainFormat.format;
swapchainCreateInfo.imageColorSpace = swapchainFormat.colorSpace;
swapchainCreateInfo.imageExtent = swapchainExtent;
swapchainCreateInfo.imageArrayLayers = 1;
swapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchainCreateInfo.preTransform = surfaceCapabilities.currentTransform;
swapchainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchainCreateInfo.presentMode = presentMode;
swapchainCreateInfo.clipped = VK_TRUE;
swapchainCreateInfo.oldSwapchain = VK_NULL_HANDLE;
// Create the swapchain
if (vkCreateSwapchainKHR(device, &swapchainCreateInfo, nullptr, &swapchain) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Retrieve the swapchain images and create image views for them
void setupSwapchainImages()
{
// Retrieve swapchain images
std::uint32_t objectCount = 0;
if (vkGetSwapchainImagesKHR(device, swapchain, &objectCount, nullptr) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
swapchainImages.resize(objectCount);
swapchainImageViews.resize(objectCount);
if (vkGetSwapchainImagesKHR(device, swapchain, &objectCount, swapchainImages.data()) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
VkImageViewCreateInfo imageViewCreateInfo = VkImageViewCreateInfo();
imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
imageViewCreateInfo.format = swapchainFormat.format;
imageViewCreateInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
imageViewCreateInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
imageViewCreateInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
imageViewCreateInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
imageViewCreateInfo.subresourceRange.levelCount = 1;
imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
imageViewCreateInfo.subresourceRange.layerCount = 1;
// Create an image view for each swapchain image
for (std::size_t i = 0; i < swapchainImages.size(); ++i)
{
imageViewCreateInfo.image = swapchainImages[i];
if (vkCreateImageView(device, &imageViewCreateInfo, nullptr, &swapchainImageViews[i]) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
}
// Load vertex and fragment shader modules
void setupShaders()
{
VkShaderModuleCreateInfo shaderModuleCreateInfo = VkShaderModuleCreateInfo();
shaderModuleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
// Use the vertex shader SPIR-V code to create a vertex shader module
{
sf::FileInputStream file;
if (!file.open("resources/shader.vert.spv"))
{
vulkanAvailable = false;
return;
}
const auto fileSize = file.getSize().value();
std::vector<std::uint32_t> buffer(fileSize / sizeof(std::uint32_t));
if (file.read(buffer.data(), fileSize) != file.getSize())
{
vulkanAvailable = false;
return;
}
shaderModuleCreateInfo.codeSize = buffer.size() * sizeof(std::uint32_t);
shaderModuleCreateInfo.pCode = buffer.data();
if (vkCreateShaderModule(device, &shaderModuleCreateInfo, nullptr, &vertexShaderModule) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Use the fragment shader SPIR-V code to create a fragment shader module
{
sf::FileInputStream file;
if (!file.open("resources/shader.frag.spv"))
{
vulkanAvailable = false;
return;
}
const auto fileSize = file.getSize().value();
std::vector<std::uint32_t> buffer(fileSize / sizeof(std::uint32_t));
if (file.read(buffer.data(), fileSize) != file.getSize())
{
vulkanAvailable = false;
return;
}
shaderModuleCreateInfo.codeSize = buffer.size() * sizeof(std::uint32_t);
shaderModuleCreateInfo.pCode = buffer.data();
if (vkCreateShaderModule(device, &shaderModuleCreateInfo, nullptr, &fragmentShaderModule) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Prepare the shader stage information for later pipeline creation
shaderStages[0] = VkPipelineShaderStageCreateInfo();
shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
shaderStages[0].module = vertexShaderModule;
shaderStages[0].pName = "main";
shaderStages[1] = VkPipelineShaderStageCreateInfo();
shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
shaderStages[1].module = fragmentShaderModule;
shaderStages[1].pName = "main";
}
// Setup renderpass and its subpass dependencies
void setupRenderpass()
{
std::array<VkAttachmentDescription, 2> attachmentDescriptions{};
// Color attachment
attachmentDescriptions[0] = VkAttachmentDescription();
attachmentDescriptions[0].format = swapchainFormat.format;
attachmentDescriptions[0].samples = VK_SAMPLE_COUNT_1_BIT;
attachmentDescriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachmentDescriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachmentDescriptions[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachmentDescriptions[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentDescriptions[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachmentDescriptions[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
// Depth attachment
attachmentDescriptions[1] = VkAttachmentDescription();
attachmentDescriptions[1].format = depthFormat;
attachmentDescriptions[1].samples = VK_SAMPLE_COUNT_1_BIT;
attachmentDescriptions[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachmentDescriptions[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentDescriptions[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachmentDescriptions[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentDescriptions[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachmentDescriptions[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkAttachmentReference colorAttachmentReference = {};
colorAttachmentReference.attachment = 0;
colorAttachmentReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference depthStencilAttachmentReference = {};
depthStencilAttachmentReference.attachment = 1;
depthStencilAttachmentReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
// Set up the renderpass to depend on commands that execute before the renderpass begins
VkSubpassDescription subpassDescription = VkSubpassDescription();
subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpassDescription.colorAttachmentCount = 1;
subpassDescription.pColorAttachments = &colorAttachmentReference;
subpassDescription.pDepthStencilAttachment = &depthStencilAttachmentReference;
VkSubpassDependency subpassDependency = VkSubpassDependency();
subpassDependency.srcSubpass = VK_SUBPASS_EXTERNAL;
subpassDependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependency.srcAccessMask = 0;
subpassDependency.dstSubpass = 0;
subpassDependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
VkRenderPassCreateInfo renderPassCreateInfo = VkRenderPassCreateInfo();
renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassCreateInfo.attachmentCount = static_cast<std::uint32_t>(attachmentDescriptions.size());
renderPassCreateInfo.pAttachments = attachmentDescriptions.data();
renderPassCreateInfo.subpassCount = 1;
renderPassCreateInfo.pSubpasses = &subpassDescription;
renderPassCreateInfo.dependencyCount = 1;
renderPassCreateInfo.pDependencies = &subpassDependency;
// Create the renderpass
if (vkCreateRenderPass(device, &renderPassCreateInfo, nullptr, &renderPass) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Set up uniform buffer and texture sampler descriptor set layouts
void setupDescriptorSetLayout()
{
std::array<VkDescriptorSetLayoutBinding, 2> descriptorSetLayoutBindings{};
// Layout binding for uniform buffer
descriptorSetLayoutBindings[0] = VkDescriptorSetLayoutBinding();
descriptorSetLayoutBindings[0].binding = 0;
descriptorSetLayoutBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorSetLayoutBindings[0].descriptorCount = 1;
descriptorSetLayoutBindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
// Layout binding for texture sampler
descriptorSetLayoutBindings[1] = VkDescriptorSetLayoutBinding();
descriptorSetLayoutBindings[1].binding = 1;
descriptorSetLayoutBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorSetLayoutBindings[1].descriptorCount = 1;
descriptorSetLayoutBindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = VkDescriptorSetLayoutCreateInfo();
descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptorSetLayoutCreateInfo.bindingCount = static_cast<std::uint32_t>(descriptorSetLayoutBindings.size());
descriptorSetLayoutCreateInfo.pBindings = descriptorSetLayoutBindings.data();
// Create descriptor set layout
if (vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCreateInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Set up pipeline layout
void setupPipelineLayout()
{
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = VkPipelineLayoutCreateInfo();
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutCreateInfo.setLayoutCount = 1;
pipelineLayoutCreateInfo.pSetLayouts = &descriptorSetLayout;
// Create pipeline layout
if (vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Set up rendering pipeline
void setupPipeline()
{
// Set up how the vertex shader pulls data out of our vertex buffer
VkVertexInputBindingDescription vertexInputBindingDescription = VkVertexInputBindingDescription();
vertexInputBindingDescription.binding = 0;
vertexInputBindingDescription.stride = sizeof(float) * 9;
vertexInputBindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
// Set up how the vertex buffer data is interpreted as attributes by the vertex shader
std::array<VkVertexInputAttributeDescription, 3> vertexInputAttributeDescriptions{};
// Position attribute
vertexInputAttributeDescriptions[0] = VkVertexInputAttributeDescription();
vertexInputAttributeDescriptions[0].binding = 0;
vertexInputAttributeDescriptions[0].location = 0;
vertexInputAttributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
vertexInputAttributeDescriptions[0].offset = sizeof(float) * 0;
// Color attribute
vertexInputAttributeDescriptions[1] = VkVertexInputAttributeDescription();
vertexInputAttributeDescriptions[1].binding = 0;
vertexInputAttributeDescriptions[1].location = 1;
vertexInputAttributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT;
vertexInputAttributeDescriptions[1].offset = sizeof(float) * 3;
// Texture coordinate attribute
vertexInputAttributeDescriptions[2] = VkVertexInputAttributeDescription();
vertexInputAttributeDescriptions[2].binding = 0;
vertexInputAttributeDescriptions[2].location = 2;
vertexInputAttributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
vertexInputAttributeDescriptions[2].offset = sizeof(float) * 7;
VkPipelineVertexInputStateCreateInfo vertexInputStateCreateInfo = VkPipelineVertexInputStateCreateInfo();
vertexInputStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputStateCreateInfo.vertexBindingDescriptionCount = 1;
vertexInputStateCreateInfo.pVertexBindingDescriptions = &vertexInputBindingDescription;
vertexInputStateCreateInfo.vertexAttributeDescriptionCount = static_cast<std::uint32_t>(
vertexInputAttributeDescriptions.size());
vertexInputStateCreateInfo.pVertexAttributeDescriptions = vertexInputAttributeDescriptions.data();
// We want to generate a triangle list with our vertex data
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo = VkPipelineInputAssemblyStateCreateInfo();
inputAssemblyStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssemblyStateCreateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssemblyStateCreateInfo.primitiveRestartEnable = VK_FALSE;
// Set up the viewport
VkViewport viewport = VkViewport();
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(swapchainExtent.width);
viewport.height = static_cast<float>(swapchainExtent.height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.f;
// Set up the scissor region
VkRect2D scissor = VkRect2D();
scissor.offset.x = 0;
scissor.offset.y = 0;
scissor.extent = swapchainExtent;
VkPipelineViewportStateCreateInfo pipelineViewportStateCreateInfo = VkPipelineViewportStateCreateInfo();
pipelineViewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
pipelineViewportStateCreateInfo.viewportCount = 1;
pipelineViewportStateCreateInfo.pViewports = &viewport;
pipelineViewportStateCreateInfo.scissorCount = 1;
pipelineViewportStateCreateInfo.pScissors = &scissor;
// Set up rasterization parameters: fill polygons, no backface culling, front face is counter-clockwise
VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo = VkPipelineRasterizationStateCreateInfo();
pipelineRasterizationStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
pipelineRasterizationStateCreateInfo.depthClampEnable = VK_FALSE;
pipelineRasterizationStateCreateInfo.rasterizerDiscardEnable = VK_FALSE;
pipelineRasterizationStateCreateInfo.polygonMode = VK_POLYGON_MODE_FILL;
pipelineRasterizationStateCreateInfo.lineWidth = 1.0f;
pipelineRasterizationStateCreateInfo.cullMode = VK_CULL_MODE_NONE;
pipelineRasterizationStateCreateInfo.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
pipelineRasterizationStateCreateInfo.depthBiasEnable = VK_FALSE;
// Enable depth testing and disable scissor testing
VkPipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo = VkPipelineDepthStencilStateCreateInfo();
pipelineDepthStencilStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
pipelineDepthStencilStateCreateInfo.depthTestEnable = VK_TRUE;
pipelineDepthStencilStateCreateInfo.depthWriteEnable = VK_TRUE;
pipelineDepthStencilStateCreateInfo.depthCompareOp = VK_COMPARE_OP_LESS;
pipelineDepthStencilStateCreateInfo.depthBoundsTestEnable = VK_FALSE;
pipelineDepthStencilStateCreateInfo.stencilTestEnable = VK_FALSE;
// Enable multi-sampling
VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo = VkPipelineMultisampleStateCreateInfo();
pipelineMultisampleStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
pipelineMultisampleStateCreateInfo.sampleShadingEnable = VK_FALSE;
pipelineMultisampleStateCreateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
// Set up blending parameters
VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentState = VkPipelineColorBlendAttachmentState();
pipelineColorBlendAttachmentState.blendEnable = VK_TRUE;
pipelineColorBlendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
pipelineColorBlendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
pipelineColorBlendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
pipelineColorBlendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
pipelineColorBlendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
pipelineColorBlendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
pipelineColorBlendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkPipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo = VkPipelineColorBlendStateCreateInfo();
pipelineColorBlendStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
pipelineColorBlendStateCreateInfo.logicOpEnable = VK_FALSE;
pipelineColorBlendStateCreateInfo.attachmentCount = 1;
pipelineColorBlendStateCreateInfo.pAttachments = &pipelineColorBlendAttachmentState;
VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo = VkGraphicsPipelineCreateInfo();
graphicsPipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
graphicsPipelineCreateInfo.stageCount = static_cast<std::uint32_t>(shaderStages.size());
graphicsPipelineCreateInfo.pStages = shaderStages.data();
graphicsPipelineCreateInfo.pVertexInputState = &vertexInputStateCreateInfo;
graphicsPipelineCreateInfo.pInputAssemblyState = &inputAssemblyStateCreateInfo;
graphicsPipelineCreateInfo.pViewportState = &pipelineViewportStateCreateInfo;
graphicsPipelineCreateInfo.pRasterizationState = &pipelineRasterizationStateCreateInfo;
graphicsPipelineCreateInfo.pDepthStencilState = &pipelineDepthStencilStateCreateInfo;
graphicsPipelineCreateInfo.pMultisampleState = &pipelineMultisampleStateCreateInfo;
graphicsPipelineCreateInfo.pColorBlendState = &pipelineColorBlendStateCreateInfo;
graphicsPipelineCreateInfo.layout = pipelineLayout;
graphicsPipelineCreateInfo.renderPass = renderPass;
graphicsPipelineCreateInfo.subpass = 0;
// Create our graphics pipeline
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, nullptr, &graphicsPipeline) !=
VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Use our renderpass and swapchain images to create the corresponding framebuffers
void setupFramebuffers()
{
swapchainFramebuffers.resize(swapchainImageViews.size());
VkFramebufferCreateInfo framebufferCreateInfo = VkFramebufferCreateInfo();
framebufferCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferCreateInfo.renderPass = renderPass;
framebufferCreateInfo.attachmentCount = 2;
framebufferCreateInfo.width = swapchainExtent.width;
framebufferCreateInfo.height = swapchainExtent.height;
framebufferCreateInfo.layers = 1;
for (std::size_t i = 0; i < swapchainFramebuffers.size(); ++i)
{
// Each framebuffer consists of a corresponding swapchain image and the shared depth image
const std::array attachments = {swapchainImageViews[i], depthImageView};
framebufferCreateInfo.pAttachments = attachments.data();
// Create the framebuffer
if (vkCreateFramebuffer(device, &framebufferCreateInfo, nullptr, &swapchainFramebuffers[i]) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
}
// Set up our command pool
void setupCommandPool()
{
// We want to be able to reset command buffers after submitting them
VkCommandPoolCreateInfo commandPoolCreateInfo = VkCommandPoolCreateInfo();
commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
commandPoolCreateInfo.queueFamilyIndex = *queueFamilyIndex;
commandPoolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
// Create our command pool
if (vkCreateCommandPool(device, &commandPoolCreateInfo, nullptr, &commandPool) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Helper to create a generic buffer with the specified size, usage and memory flags
bool createBuffer(VkDeviceSize size,
VkBufferUsageFlags usage,
VkMemoryPropertyFlags properties,
VkBuffer& buffer,
VkDeviceMemory& memory)
{
// We only have a single queue so we can request exclusive access
VkBufferCreateInfo bufferCreateInfo = VkBufferCreateInfo();
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferCreateInfo.size = size;
bufferCreateInfo.usage = usage;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
// Create the buffer, this does not allocate any memory for it yet
if (vkCreateBuffer(device, &bufferCreateInfo, nullptr, &buffer) != VK_SUCCESS)
return false;
// Check what kind of memory we need to request from the GPU
VkMemoryRequirements memoryRequirements = VkMemoryRequirements();
vkGetBufferMemoryRequirements(device, buffer, &memoryRequirements);
// Check what GPU memory type is available for us to allocate out of
VkPhysicalDeviceMemoryProperties memoryProperties = VkPhysicalDeviceMemoryProperties();
vkGetPhysicalDeviceMemoryProperties(gpu, &memoryProperties);
std::uint32_t memoryType = 0;
for (; memoryType < memoryProperties.memoryTypeCount; ++memoryType)
{
if ((memoryRequirements.memoryTypeBits & static_cast<unsigned int>(1 << memoryType)) &&
((memoryProperties.memoryTypes[memoryType].propertyFlags & properties) == properties))
break;
}
if (memoryType == memoryProperties.memoryTypeCount)
return false;
VkMemoryAllocateInfo memoryAllocateInfo = VkMemoryAllocateInfo();
memoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
memoryAllocateInfo.allocationSize = memoryRequirements.size;
memoryAllocateInfo.memoryTypeIndex = memoryType;
// Allocate the memory out of the GPU pool for the required memory type
if (vkAllocateMemory(device, &memoryAllocateInfo, nullptr, &memory) != VK_SUCCESS)
return false;
// Bind the allocated memory to our buffer object
if (vkBindBufferMemory(device, buffer, memory, 0) != VK_SUCCESS)
return false;
return true;
}
// Helper to copy the contents of one buffer to another buffer
bool copyBuffer(VkBuffer dst, VkBuffer src, VkDeviceSize size)
{
// Allocate a primary command buffer out of our command pool
VkCommandBufferAllocateInfo commandBufferAllocateInfo = VkCommandBufferAllocateInfo();
commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
commandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
commandBufferAllocateInfo.commandPool = commandPool;
commandBufferAllocateInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer = nullptr;
if (vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, &commandBuffer) != VK_SUCCESS)
return false;
// Begin the command buffer
VkCommandBufferBeginInfo commandBufferBeginInfo = VkCommandBufferBeginInfo();
commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
if (vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
return false;
}
// Add our buffer copy command
VkBufferCopy bufferCopy = VkBufferCopy();
bufferCopy.srcOffset = 0;
bufferCopy.dstOffset = 0;
bufferCopy.size = size;
vkCmdCopyBuffer(commandBuffer, src, dst, 1, &bufferCopy);
// End and submit the command buffer
vkEndCommandBuffer(commandBuffer);
VkSubmitInfo submitInfo = VkSubmitInfo();
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
if (vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
return false;
}
// Ensure the command buffer has been processed
if (vkQueueWaitIdle(queue) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
return false;
}
// Free the command buffer
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
return true;
}
// Create our vertex buffer and upload its data
void setupVertexBuffer()
{
// clang-format off
constexpr std::array vertexData = {
// X Y Z R G B A U V
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f
};
// clang-format on
// Create a staging buffer that is writable by the CPU
VkBuffer stagingBuffer = {};
VkDeviceMemory stagingBufferMemory = {};
if (!createBuffer(sizeof(vertexData),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
stagingBuffer,
stagingBufferMemory))
{
vulkanAvailable = false;
return;
}
void* ptr = nullptr;
// Map the buffer into our address space
if (vkMapMemory(device, stagingBufferMemory, 0, sizeof(vertexData), 0, &ptr) != VK_SUCCESS)
{
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Copy the vertex data into the buffer
std::memcpy(ptr, vertexData.data(), sizeof(vertexData));
// Unmap the buffer
vkUnmapMemory(device, stagingBufferMemory);
// Create the GPU local vertex buffer
if (!createBuffer(sizeof(vertexData),
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
vertexBuffer,
vertexBufferMemory))
{
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Copy the contents of the staging buffer into the GPU vertex buffer
vulkanAvailable = copyBuffer(vertexBuffer, stagingBuffer, sizeof(vertexData));
// Free the staging buffer and its memory
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
}
// Create our index buffer and upload its data
void setupIndexBuffer()
{
// clang-format off
constexpr std::array<std::uint16_t, 36> indexData = {
0, 1, 2,
2, 3, 0,
4, 5, 6,
6, 7, 4,
8, 9, 10,
10, 11, 8,
12, 13, 14,
14, 15, 12,
16, 17, 18,
18, 19, 16,
20, 21, 22,
22, 23, 20
};
// clang-format on
// Create a staging buffer that is writable by the CPU
VkBuffer stagingBuffer = {};
VkDeviceMemory stagingBufferMemory = {};
if (!createBuffer(sizeof(indexData),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
stagingBuffer,
stagingBufferMemory))
{
vulkanAvailable = false;
return;
}
void* ptr = nullptr;
// Map the buffer into our address space
if (vkMapMemory(device, stagingBufferMemory, 0, sizeof(indexData), 0, &ptr) != VK_SUCCESS)
{
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Copy the index data into the buffer
std::memcpy(ptr, indexData.data(), sizeof(indexData));
// Unmap the buffer
vkUnmapMemory(device, stagingBufferMemory);
// Create the GPU local index buffer
if (!createBuffer(sizeof(indexData),
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
indexBuffer,
indexBufferMemory))
{
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Copy the contents of the staging buffer into the GPU index buffer
vulkanAvailable = copyBuffer(indexBuffer, stagingBuffer, sizeof(indexData));
// Free the staging buffer and its memory
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
}
// Create our uniform buffer but don't upload any data yet
void setupUniformBuffers()
{
// Create a uniform buffer for every frame that might be in flight to prevent clobbering
for (std::size_t i = 0; i < swapchainImages.size(); ++i)
{
uniformBuffers.push_back({});
uniformBuffersMemory.push_back({});
// The uniform buffer will be host visible and coherent since we use it for streaming data every frame
if (!createBuffer(sizeof(Matrix) * 3,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
uniformBuffers[i],
uniformBuffersMemory[i]))
{
vulkanAvailable = false;
return;
}
}
}
// Helper to create a generic image with the specified size, format, usage and memory flags
bool createImage(std::uint32_t width,
std::uint32_t height,
VkFormat format,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkMemoryPropertyFlags properties,
VkImage& image,
VkDeviceMemory& imageMemory)
{
// We only have a single queue so we can request exclusive access
VkImageCreateInfo imageCreateInfo = VkImageCreateInfo();
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.extent.width = width;
imageCreateInfo.extent.height = height;
imageCreateInfo.extent.depth = 1;
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.format = format;
imageCreateInfo.tiling = tiling;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageCreateInfo.usage = usage;
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
// Create the image, this does not allocate any memory for it yet
if (vkCreateImage(device, &imageCreateInfo, nullptr, &image) != VK_SUCCESS)
return false;
// Check what kind of memory we need to request from the GPU
VkMemoryRequirements memoryRequirements = VkMemoryRequirements();
vkGetImageMemoryRequirements(device, image, &memoryRequirements);
// Check what GPU memory type is available for us to allocate out of
VkPhysicalDeviceMemoryProperties memoryProperties = VkPhysicalDeviceMemoryProperties();
vkGetPhysicalDeviceMemoryProperties(gpu, &memoryProperties);
std::uint32_t memoryType = 0;
for (; memoryType < memoryProperties.memoryTypeCount; ++memoryType)
{
if ((memoryRequirements.memoryTypeBits & static_cast<unsigned int>(1 << memoryType)) &&
((memoryProperties.memoryTypes[memoryType].propertyFlags & properties) == properties))
break;
}
if (memoryType == memoryProperties.memoryTypeCount)
return false;
VkMemoryAllocateInfo memoryAllocateInfo = VkMemoryAllocateInfo();
memoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
memoryAllocateInfo.allocationSize = memoryRequirements.size;
memoryAllocateInfo.memoryTypeIndex = memoryType;
// Allocate the memory out of the GPU pool for the required memory type
if (vkAllocateMemory(device, &memoryAllocateInfo, nullptr, &imageMemory) != VK_SUCCESS)
return false;
// Bind the allocated memory to our image object
if (vkBindImageMemory(device, image, imageMemory, 0) != VK_SUCCESS)
return false;
return true;
}
// Create our depth image and transition it into the proper layout
void setupDepthImage()
{
// Create our depth image
if (!createImage(swapchainExtent.width,
swapchainExtent.height,
depthFormat,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
depthImage,
depthImageMemory))
{
vulkanAvailable = false;
return;
}
// Allocate a command buffer
VkCommandBufferAllocateInfo commandBufferAllocateInfo = VkCommandBufferAllocateInfo();
commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
commandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
commandBufferAllocateInfo.commandPool = commandPool;
commandBufferAllocateInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer = nullptr;
if (vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, &commandBuffer) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
// Begin the command buffer
VkCommandBufferBeginInfo commandBufferBeginInfo = VkCommandBufferBeginInfo();
commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
VkSubmitInfo submitInfo = VkSubmitInfo();
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
if (vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vulkanAvailable = false;
return;
}
// Submit a barrier to transition the image layout to depth stencil optimal
VkImageMemoryBarrier barrier = VkImageMemoryBarrier();
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
barrier.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = depthImage;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT |
((depthFormat == VK_FORMAT_D32_SFLOAT) ? 0 : VK_IMAGE_ASPECT_STENCIL_BIT);
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
vkCmdPipelineBarrier(commandBuffer,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
0,
0,
nullptr,
0,
nullptr,
1,
&barrier);
// End and submit the command buffer
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vulkanAvailable = false;
return;
}
if (vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vulkanAvailable = false;
return;
}
// Ensure the command buffer has been processed
if (vkQueueWaitIdle(queue) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vulkanAvailable = false;
return;
}
// Free the command buffer
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
}
// Create an image view for our depth image
void setupDepthImageView()
{
VkImageViewCreateInfo imageViewCreateInfo = VkImageViewCreateInfo();
imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
imageViewCreateInfo.image = depthImage;
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
imageViewCreateInfo.format = depthFormat;
imageViewCreateInfo.subresourceRange
.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT |
((depthFormat == VK_FORMAT_D32_SFLOAT) ? 0 : VK_IMAGE_ASPECT_STENCIL_BIT);
imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
imageViewCreateInfo.subresourceRange.levelCount = 1;
imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
imageViewCreateInfo.subresourceRange.layerCount = 1;
// Create the depth image view
if (vkCreateImageView(device, &imageViewCreateInfo, nullptr, &depthImageView) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Create an image for our texture data
void setupTextureImage()
{
// Load the image data
sf::Image imageData;
if (!imageData.loadFromFile("resources/logo.png"))
{
vulkanAvailable = false;
return;
}
// Create a staging buffer to transfer the data with
const VkDeviceSize imageSize = imageData.getSize().x * imageData.getSize().y * 4;
VkBuffer stagingBuffer = {};
VkDeviceMemory stagingBufferMemory = {};
createBuffer(imageSize,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
stagingBuffer,
stagingBufferMemory);
void* ptr = nullptr;
// Map the buffer into our address space
if (vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &ptr) != VK_SUCCESS)
{
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Copy the image data into the buffer
std::memcpy(ptr, imageData.getPixelsPtr(), static_cast<std::size_t>(imageSize));
// Unmap the buffer
vkUnmapMemory(device, stagingBufferMemory);
// Create a GPU local image
if (!createImage(imageData.getSize().x,
imageData.getSize().y,
VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
textureImage,
textureImageMemory))
{
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Create a command buffer
VkCommandBufferAllocateInfo commandBufferAllocateInfo = VkCommandBufferAllocateInfo();
commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
commandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
commandBufferAllocateInfo.commandPool = commandPool;
commandBufferAllocateInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer = nullptr;
if (vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, &commandBuffer) != VK_SUCCESS)
{
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Begin the command buffer
VkCommandBufferBeginInfo commandBufferBeginInfo = VkCommandBufferBeginInfo();
commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
VkSubmitInfo submitInfo = VkSubmitInfo();
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
if (vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Submit a barrier to transition the image layout to transfer destination optimal
VkImageMemoryBarrier barrier = VkImageMemoryBarrier();
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = textureImage;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
vkCmdPipelineBarrier(commandBuffer,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
0,
0,
nullptr,
0,
nullptr,
1,
&barrier);
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
if (vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Ensure the command buffer has been processed
if (vkQueueWaitIdle(queue) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Begin the command buffer
if (vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Copy the staging buffer contents into the image
VkBufferImageCopy bufferImageCopy = VkBufferImageCopy();
bufferImageCopy.bufferOffset = 0;
bufferImageCopy.bufferRowLength = 0;
bufferImageCopy.bufferImageHeight = 0;
bufferImageCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
bufferImageCopy.imageSubresource.mipLevel = 0;
bufferImageCopy.imageSubresource.baseArrayLayer = 0;
bufferImageCopy.imageSubresource.layerCount = 1;
bufferImageCopy.imageOffset.x = 0;
bufferImageCopy.imageOffset.y = 0;
bufferImageCopy.imageOffset.z = 0;
bufferImageCopy.imageExtent.width = imageData.getSize().x;
bufferImageCopy.imageExtent.height = imageData.getSize().y;
bufferImageCopy.imageExtent.depth = 1;
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, textureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bufferImageCopy);
// End and submit the command buffer
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
if (vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Ensure the command buffer has been processed
if (vkQueueWaitIdle(queue) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Begin the command buffer
if (vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Submit a barrier to transition the image layout from transfer destination optimal to shader read-only optimal
barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
vkCmdPipelineBarrier(commandBuffer,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
0,
0,
nullptr,
0,
nullptr,
1,
&barrier);
// End and submit the command buffer
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
if (vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Ensure the command buffer has been processed
if (vkQueueWaitIdle(queue) != VK_SUCCESS)
{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vulkanAvailable = false;
return;
}
// Free the command buffer
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
vkFreeMemory(device, stagingBufferMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
}
// Create an image view for our texture
void setupTextureImageView()
{
VkImageViewCreateInfo imageViewCreateInfo = VkImageViewCreateInfo();
imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
imageViewCreateInfo.image = textureImage;
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
imageViewCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
imageViewCreateInfo.subresourceRange.levelCount = 1;
imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
imageViewCreateInfo.subresourceRange.layerCount = 1;
// Create our texture image view
if (vkCreateImageView(device, &imageViewCreateInfo, nullptr, &textureImageView) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Create a sampler for our texture
void setupTextureSampler()
{
// Sampler parameters: linear min/mag filtering, 4x anisotropic
VkSamplerCreateInfo samplerCreateInfo = VkSamplerCreateInfo();
samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerCreateInfo.magFilter = VK_FILTER_LINEAR;
samplerCreateInfo.minFilter = VK_FILTER_LINEAR;
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.anisotropyEnable = VK_TRUE;
samplerCreateInfo.maxAnisotropy = 4;
samplerCreateInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
samplerCreateInfo.compareEnable = VK_FALSE;
samplerCreateInfo.compareOp = VK_COMPARE_OP_ALWAYS;
samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerCreateInfo.mipLodBias = 0.0f;
samplerCreateInfo.minLod = 0.0f;
samplerCreateInfo.maxLod = 0.0f;
// Create our sampler
if (vkCreateSampler(device, &samplerCreateInfo, nullptr, &textureSampler) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Set up our descriptor pool
void setupDescriptorPool()
{
// We need to allocate as many descriptor sets as we have frames in flight
std::array<VkDescriptorPoolSize, 2> descriptorPoolSizes{};
descriptorPoolSizes[0] = VkDescriptorPoolSize();
descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorPoolSizes[0].descriptorCount = static_cast<std::uint32_t>(swapchainImages.size());
descriptorPoolSizes[1] = VkDescriptorPoolSize();
descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorPoolSizes[1].descriptorCount = static_cast<std::uint32_t>(swapchainImages.size());
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = VkDescriptorPoolCreateInfo();
descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descriptorPoolCreateInfo.poolSizeCount = static_cast<std::uint32_t>(descriptorPoolSizes.size());
descriptorPoolCreateInfo.pPoolSizes = descriptorPoolSizes.data();
descriptorPoolCreateInfo.maxSets = static_cast<std::uint32_t>(swapchainImages.size());
// Create the descriptor pool
if (vkCreateDescriptorPool(device, &descriptorPoolCreateInfo, nullptr, &descriptorPool) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Set up our descriptor sets
void setupDescriptorSets()
{
// Allocate a descriptor set for each frame in flight
std::vector<VkDescriptorSetLayout> descriptorSetLayouts(swapchainImages.size(), descriptorSetLayout);
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = VkDescriptorSetAllocateInfo();
descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
descriptorSetAllocateInfo.descriptorPool = descriptorPool;
descriptorSetAllocateInfo.descriptorSetCount = static_cast<std::uint32_t>(swapchainImages.size());
descriptorSetAllocateInfo.pSetLayouts = descriptorSetLayouts.data();
descriptorSets.resize(swapchainImages.size());
if (vkAllocateDescriptorSets(device, &descriptorSetAllocateInfo, descriptorSets.data()) != VK_SUCCESS)
{
descriptorSets.clear();
vulkanAvailable = false;
return;
}
// For every descriptor set, set up the bindings to our uniform buffer and texture sampler
for (std::size_t i = 0; i < descriptorSets.size(); ++i)
{
std::array<VkWriteDescriptorSet, 2> writeDescriptorSets{};
// Uniform buffer binding information
VkDescriptorBufferInfo descriptorBufferInfo = VkDescriptorBufferInfo();
descriptorBufferInfo.buffer = uniformBuffers[i];
descriptorBufferInfo.offset = 0;
descriptorBufferInfo.range = sizeof(Matrix) * 3;
writeDescriptorSets[0] = VkWriteDescriptorSet();
writeDescriptorSets[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSets[0].dstSet = descriptorSets[i];
writeDescriptorSets[0].dstBinding = 0;
writeDescriptorSets[0].dstArrayElement = 0;
writeDescriptorSets[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
writeDescriptorSets[0].descriptorCount = 1;
writeDescriptorSets[0].pBufferInfo = &descriptorBufferInfo;
// Texture sampler binding information
VkDescriptorImageInfo descriptorImageInfo = VkDescriptorImageInfo();
descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
descriptorImageInfo.imageView = textureImageView;
descriptorImageInfo.sampler = textureSampler;
writeDescriptorSets[1] = VkWriteDescriptorSet();
writeDescriptorSets[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSets[1].dstSet = descriptorSets[i];
writeDescriptorSets[1].dstBinding = 1;
writeDescriptorSets[1].dstArrayElement = 0;
writeDescriptorSets[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
writeDescriptorSets[1].descriptorCount = 1;
writeDescriptorSets[1].pImageInfo = &descriptorImageInfo;
// Update the descriptor set
vkUpdateDescriptorSets(device,
static_cast<std::uint32_t>(writeDescriptorSets.size()),
writeDescriptorSets.data(),
0,
nullptr);
}
}
// Set up the command buffers we use for drawing each frame
void setupCommandBuffers()
{
// We need a command buffer for every frame in flight
commandBuffers.resize(swapchainFramebuffers.size());
// These are primary command buffers
VkCommandBufferAllocateInfo commandBufferAllocateInfo = VkCommandBufferAllocateInfo();
commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
commandBufferAllocateInfo.commandPool = commandPool;
commandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
commandBufferAllocateInfo.commandBufferCount = static_cast<std::uint32_t>(commandBuffers.size());
// Allocate the command buffers from our command pool
if (vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, commandBuffers.data()) != VK_SUCCESS)
{
commandBuffers.clear();
vulkanAvailable = false;
return;
}
}
// Set up the commands we need to issue to draw a frame
void setupDraw()
{
// Set up our clear colors
std::array<VkClearValue, 2> clearColors{};
// Clear color buffer to opaque black
clearColors[0] = VkClearValue();
clearColors[0].color.float32[0] = 0.0f;
clearColors[0].color.float32[1] = 0.0f;
clearColors[0].color.float32[2] = 0.0f;
clearColors[0].color.float32[3] = 0.0f;
// Clear depth to 1.0f
clearColors[1] = VkClearValue();
clearColors[1].depthStencil.depth = 1.0f;
clearColors[1].depthStencil.stencil = 0;
VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo();
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.renderArea.offset.x = 0;
renderPassBeginInfo.renderArea.offset.y = 0;
renderPassBeginInfo.renderArea.extent = swapchainExtent;
renderPassBeginInfo.clearValueCount = static_cast<std::uint32_t>(clearColors.size());
renderPassBeginInfo.pClearValues = clearColors.data();
// Simultaneous use: this command buffer can be resubmitted to a queue before a previous submission is completed
VkCommandBufferBeginInfo commandBufferBeginInfo = VkCommandBufferBeginInfo();
commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
// Set up the command buffers for each frame in flight
for (std::size_t i = 0; i < commandBuffers.size(); ++i)
{
// Begin the command buffer
if (vkBeginCommandBuffer(commandBuffers[i], &commandBufferBeginInfo) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
// Begin the renderpass
renderPassBeginInfo.framebuffer = swapchainFramebuffers[i];
vkCmdBeginRenderPass(commandBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
// Bind our graphics pipeline
vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
// Bind our vertex buffer
const VkDeviceSize offset = 0;
vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, &vertexBuffer, &offset);
// Bind our index buffer
vkCmdBindIndexBuffer(commandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16);
// Bind our descriptor sets
vkCmdBindDescriptorSets(commandBuffers[i],
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout,
0,
1,
&descriptorSets[i],
0,
nullptr);
// Draw our primitives
vkCmdDrawIndexed(commandBuffers[i], 36, 1, 0, 0, 0);
// End the renderpass
vkCmdEndRenderPass(commandBuffers[i]);
// End the command buffer
if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
}
// Set up the semaphores we use to synchronize frames among each other
void setupSemaphores()
{
VkSemaphoreCreateInfo semaphoreCreateInfo = VkSemaphoreCreateInfo();
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
// Create a semaphore to track when an swapchain image is available for each frame in flight
for (std::size_t i = 0; i < maxFramesInFlight; ++i)
{
imageAvailableSemaphores.push_back({});
if (vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS)
{
imageAvailableSemaphores.pop_back();
vulkanAvailable = false;
return;
}
}
// Create a semaphore to track when rendering is complete for each frame in flight
for (std::size_t i = 0; i < maxFramesInFlight; ++i)
{
renderFinishedSemaphores.push_back({});
if (vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS)
{
renderFinishedSemaphores.pop_back();
vulkanAvailable = false;
return;
}
}
}
// Set up the fences we use to synchronize frames among each other
void setupFences()
{
// Create the fences in the signaled state
VkFenceCreateInfo fenceCreateInfo = VkFenceCreateInfo();
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
// Create a fence to track when queue submission is complete for each frame in flight
for (std::size_t i = 0; i < maxFramesInFlight; ++i)
{
fences.push_back({});
if (vkCreateFence(device, &fenceCreateInfo, nullptr, &fences[i]) != VK_SUCCESS)
{
fences.pop_back();
vulkanAvailable = false;
return;
}
}
}
// Update the matrices in our uniform buffer every frame
void updateUniformBuffer(float elapsed)
{
// Construct the model matrix
// clang-format off
Matrix model = {{
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f}
}};
// clang-format on
matrixRotateX(model, sf::degrees(elapsed * 59.0f));
matrixRotateY(model, sf::degrees(elapsed * 83.0f));
matrixRotateZ(model, sf::degrees(elapsed * 109.0f));
// Translate the model based on the mouse position
const sf::Vector2f mousePosition = sf::Vector2f(sf::Mouse::getPosition(window));
const sf::Vector2f windowSize = sf::Vector2f(window.getSize());
const float x = std::clamp(mousePosition.x * 2.f / windowSize.x - 1.f, -1.0f, 1.0f) * 2.0f;
const float y = std::clamp(-mousePosition.y * 2.f / windowSize.y + 1.f, -1.0f, 1.0f) * 1.5f;
model[3][0] -= x;
model[3][2] += y;
// Construct the view matrix
const sf::Vector3f eye(0.0f, 4.0f, 0.0f);
const sf::Vector3f center(0.0f, 0.0f, 0.0f);
const sf::Vector3f up(0.0f, 0.0f, 1.0f);
Matrix view;
matrixLookAt(view, eye, center, up);
// Construct the projection matrix
const sf::Angle fov = sf::degrees(45);
const float aspect = static_cast<float>(swapchainExtent.width) / static_cast<float>(swapchainExtent.height);
const float nearPlane = 0.1f;
const float farPlane = 10.0f;
Matrix projection;
matrixPerspective(projection, fov, aspect, nearPlane, farPlane);
char* ptr = nullptr;
// Map the current frame's uniform buffer into our address space
if (vkMapMemory(device, uniformBuffersMemory[currentFrame], 0, sizeof(Matrix) * 3, 0, reinterpret_cast<void**>(&ptr)) !=
VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
// Copy the matrix data into the current frame's uniform buffer
std::memcpy(ptr + sizeof(Matrix) * 0, model.data(), sizeof(Matrix));
std::memcpy(ptr + sizeof(Matrix) * 1, view.data(), sizeof(Matrix));
std::memcpy(ptr + sizeof(Matrix) * 2, projection.data(), sizeof(Matrix));
// Unmap the buffer
vkUnmapMemory(device, uniformBuffersMemory[currentFrame]);
}
void draw()
{
std::uint32_t imageIndex = 0;
// If the objects we need to submit this frame are still pending, wait here
vkWaitForFences(device, 1, &fences[currentFrame], VK_TRUE, std::numeric_limits<std::uint64_t>::max());
{
// Get the next image in the swapchain
const VkResult result = vkAcquireNextImageKHR(device,
swapchain,
std::numeric_limits<std::uint64_t>::max(),
imageAvailableSemaphores[currentFrame],
VK_NULL_HANDLE,
&imageIndex);
// Check if we need to re-create the swapchain (e.g. if the window was resized)
if (result == VK_ERROR_OUT_OF_DATE_KHR)
{
recreateSwapchain();
swapchainOutOfDate = false;
return;
}
if ((result != VK_SUCCESS) && (result != VK_TIMEOUT) && (result != VK_NOT_READY) &&
(result != VK_SUBOPTIMAL_KHR))
{
vulkanAvailable = false;
return;
}
}
// Wait for the swapchain image to be available in the color attachment stage before submitting the queue
const VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
// Signal the render finished semaphore once the queue has been processed
VkSubmitInfo submitInfo = VkSubmitInfo();
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = &imageAvailableSemaphores[currentFrame];
submitInfo.pWaitDstStageMask = &waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &renderFinishedSemaphores[currentFrame];
vkResetFences(device, 1, &fences[currentFrame]);
if (vkQueueSubmit(queue, 1, &submitInfo, fences[currentFrame]) != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
// Wait for rendering to complete before presenting
VkPresentInfoKHR presentInfo = VkPresentInfoKHR();
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = &renderFinishedSemaphores[currentFrame];
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &swapchain;
presentInfo.pImageIndices = &imageIndex;
{
// Queue presentation
const VkResult result = vkQueuePresentKHR(queue, &presentInfo);
// Check if we need to re-create the swapchain (e.g. if the window was resized)
if ((result == VK_ERROR_OUT_OF_DATE_KHR) || (result == VK_SUBOPTIMAL_KHR) || swapchainOutOfDate)
{
recreateSwapchain();
swapchainOutOfDate = false;
}
else if (result != VK_SUCCESS)
{
vulkanAvailable = false;
return;
}
}
// Make sure to use the next frame's objects next frame
currentFrame = (currentFrame + 1) % maxFramesInFlight;
}
void run()
{
const sf::Clock clock;
// Start game loop
while (window.isOpen())
{
// Process events
while (const std::optional event = window.pollEvent())
{
// Window closed or escape key pressed: exit
if (event->is<sf::Event::Closed>() ||
(event->is<sf::Event::KeyPressed>() &&
event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape))
{
window.close();
}
// Re-create the swapchain when the window is resized
if (event->is<sf::Event::Resized>())
swapchainOutOfDate = true;
}
// Check that window was not closed before drawing to it
if (vulkanAvailable && window.isOpen())
{
// Update the uniform buffer (matrices)
updateUniformBuffer(clock.getElapsedTime().asSeconds());
// Render the frame
draw();
}
}
}
private:
// NOLINTBEGIN(readability-identifier-naming)
sf::WindowBase window{sf::VideoMode({800, 600}), "SFML window with Vulkan", sf::Style::Default};
bool vulkanAvailable{sf::Vulkan::isAvailable()};
const unsigned int maxFramesInFlight{2};
unsigned int currentFrame{};
bool swapchainOutOfDate{};
VkInstance instance{};
VkDebugReportCallbackEXT debugReportCallback{};
VkSurfaceKHR surface{};
VkPhysicalDevice gpu{};
std::optional<std::uint32_t> queueFamilyIndex;
VkDevice device{};
VkQueue queue{};
VkSurfaceFormatKHR swapchainFormat{};
VkExtent2D swapchainExtent{};
VkSwapchainKHR swapchain{};
std::vector<VkImage> swapchainImages;
std::vector<VkImageView> swapchainImageViews;
VkFormat depthFormat{VK_FORMAT_UNDEFINED};
VkImage depthImage{};
VkDeviceMemory depthImageMemory{};
VkImageView depthImageView{};
VkShaderModule vertexShaderModule{};
VkShaderModule fragmentShaderModule{};
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages{};
VkDescriptorSetLayout descriptorSetLayout{};
VkPipelineLayout pipelineLayout{};
VkRenderPass renderPass{};
VkPipeline graphicsPipeline{};
std::vector<VkFramebuffer> swapchainFramebuffers;
VkCommandPool commandPool{};
VkBuffer vertexBuffer{};
VkDeviceMemory vertexBufferMemory{};
VkBuffer indexBuffer{};
VkDeviceMemory indexBufferMemory{};
std::vector<VkBuffer> uniformBuffers;
std::vector<VkDeviceMemory> uniformBuffersMemory;
VkImage textureImage{};
VkDeviceMemory textureImageMemory{};
VkImageView textureImageView{};
VkSampler textureSampler{};
VkDescriptorPool descriptorPool{};
std::vector<VkDescriptorSet> descriptorSets;
std::vector<VkCommandBuffer> commandBuffers;
std::vector<VkSemaphore> imageAvailableSemaphores;
std::vector<VkSemaphore> renderFinishedSemaphores;
std::vector<VkFence> fences;
// NOLINTEND(readability-identifier-naming)
};
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
VulkanExample example;
example.run();
}
|