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
|
/*
* Copyright (c) 2023-2025 Valve Corporation
* Copyright (c) 2023-2025 LunarG, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "../framework/layer_validation_tests.h"
#include "../framework/pipeline_helper.h"
std::optional<VkPhysicalDeviceGroupProperties> WsiTest::FindPhysicalDeviceGroup() {
uint32_t physical_device_group_count = 0;
vk::EnumeratePhysicalDeviceGroups(instance(), &physical_device_group_count, nullptr);
if (physical_device_group_count == 0) {
return {};
}
std::vector<VkPhysicalDeviceGroupProperties> physical_device_groups(physical_device_group_count,
{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES});
vk::EnumeratePhysicalDeviceGroups(instance(), &physical_device_group_count, physical_device_groups.data());
for (const auto &physical_device_group : physical_device_groups) {
for (uint32_t k = 0; k < physical_device_group.physicalDeviceCount; k++) {
if (physical_device_group.physicalDevices[k] == Gpu()) {
return physical_device_group;
}
}
}
return {};
}
class PositiveWsi : public WsiTest {};
TEST_F(PositiveWsi, CreateWaylandSurface) {
TEST_DESCRIPTION("Test creating wayland surface");
#ifndef VK_USE_PLATFORM_WAYLAND_KHR
GTEST_SKIP() << "test not supported on platform";
#else
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
WaylandContext wayland_ctx;
if (!wayland_ctx.Init()) {
GTEST_SKIP() << "Failed to create wayland context.";
}
VkWaylandSurfaceCreateInfoKHR surface_create_info = vku::InitStructHelper();
surface_create_info.display = wayland_ctx.display;
surface_create_info.surface = wayland_ctx.surface;
VkSurfaceKHR vulkan_surface;
vk::CreateWaylandSurfaceKHR(instance(), &surface_create_info, nullptr, &vulkan_surface);
vk::DestroySurfaceKHR(instance(), vulkan_surface, nullptr);
wayland_ctx.Release();
#endif
}
TEST_F(PositiveWsi, CreateXcbSurface) {
TEST_DESCRIPTION("Test creating xcb surface");
#ifndef VK_USE_PLATFORM_XCB_KHR
GTEST_SKIP() << "test not supported on platform";
#else
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
xcb_connection_t *xcb_connection = xcb_connect(nullptr, nullptr);
ASSERT_TRUE(xcb_connection);
// NOTE: This is technically an invalid window! (There is no width/height)
// But there is no robust way to check for a valid window without crashing the app.
xcb_window_t xcb_window = xcb_generate_id(xcb_connection);
ASSERT_TRUE(xcb_window != 0);
VkXcbSurfaceCreateInfoKHR surface_create_info = vku::InitStructHelper();
surface_create_info.connection = xcb_connection;
surface_create_info.window = xcb_window;
VkSurfaceKHR vulkan_surface{};
vk::CreateXcbSurfaceKHR(instance(), &surface_create_info, nullptr, &vulkan_surface);
vk::DestroySurfaceKHR(instance(), vulkan_surface, nullptr);
xcb_destroy_window(xcb_connection, xcb_window);
xcb_disconnect(xcb_connection);
#endif
}
TEST_F(PositiveWsi, CreateX11Surface) {
TEST_DESCRIPTION("Test creating x11 surface");
#ifndef VK_USE_PLATFORM_XLIB_KHR
GTEST_SKIP() << "test not supported on platform";
#else
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
if (std::getenv("DISPLAY") == nullptr) {
GTEST_SKIP() << "Test requires working display\n";
}
Display *x11_display = XOpenDisplay(nullptr);
ASSERT_TRUE(x11_display != nullptr);
const int screen = DefaultScreen(x11_display);
const Window x11_window = XCreateSimpleWindow(x11_display, RootWindow(x11_display, screen), 0, 0, 128, 128, 1,
BlackPixel(x11_display, screen), WhitePixel(x11_display, screen));
VkSurfaceKHR vulkan_surface;
VkXlibSurfaceCreateInfoKHR surface_create_info = vku::InitStructHelper();
surface_create_info.dpy = x11_display;
surface_create_info.window = x11_window;
vk::CreateXlibSurfaceKHR(instance(), &surface_create_info, nullptr, &vulkan_surface);
vk::DestroySurfaceKHR(instance(), vulkan_surface, nullptr);
XDestroyWindow(x11_display, x11_window);
XCloseDisplay(x11_display);
#endif
}
#if defined(VK_USE_PLATFORM_WIN32_KHR)
TEST_F(PositiveWsi, GetPhysicalDeviceSurfaceCapabilities2KHRWithFullScreenEXT) {
TEST_DESCRIPTION("Test vkAcquireFullScreenExclusiveModeEXT.");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_FULL_SCREEN_EXCLUSIVE_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
if (!IsPlatformMockICD()) {
GTEST_SKIP() << "Only run test MockICD due to CI stability";
}
InitRenderTarget();
RETURN_IF_SKIP(InitSwapchain());
const POINT pt_zero = {0, 0};
VkSurfaceFullScreenExclusiveWin32InfoEXT fullscreen_exclusive_win32_info = vku::InitStructHelper();
fullscreen_exclusive_win32_info.hmonitor = MonitorFromPoint(pt_zero, MONITOR_DEFAULTTOPRIMARY);
VkSurfaceFullScreenExclusiveInfoEXT fullscreen_exclusive_info = vku::InitStructHelper(&fullscreen_exclusive_win32_info);
fullscreen_exclusive_info.fullScreenExclusive = VK_FULL_SCREEN_EXCLUSIVE_APPLICATION_CONTROLLED_EXT;
VkPhysicalDeviceSurfaceInfo2KHR surface_info = vku::InitStructHelper(&fullscreen_exclusive_info);
surface_info.surface = m_surface.Handle();
VkSurfaceCapabilities2KHR surface_caps = vku::InitStructHelper();
vk::GetPhysicalDeviceSurfaceCapabilities2KHR(m_device->Physical(), &surface_info, &surface_caps);
}
#endif
TEST_F(PositiveWsi, CmdCopySwapchainImage) {
TEST_DESCRIPTION("Run vkCmdCopyImage with a swapchain image");
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
GTEST_SKIP()
<< "According to valid usage, VkBindImageMemoryInfo-memory should be NULL. But Android will crash if memory is NULL, "
"skipping test";
#endif
SetTargetApiVersion(VK_API_VERSION_1_2);
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
InitRenderTarget();
RETURN_IF_SKIP(InitSwapchain(VK_IMAGE_USAGE_TRANSFER_DST_BIT));
VkImageCreateInfo image_create_info = vku::InitStructHelper();
image_create_info.imageType = VK_IMAGE_TYPE_2D;
image_create_info.format = m_surface_formats[0].format;
image_create_info.extent.width = m_surface_capabilities.minImageExtent.width;
image_create_info.extent.height = m_surface_capabilities.minImageExtent.height;
image_create_info.extent.depth = 1;
image_create_info.mipLevels = 1;
image_create_info.arrayLayers = 1;
image_create_info.samples = VK_SAMPLE_COUNT_1_BIT;
image_create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
image_create_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vkt::Image srcImage(*m_device, image_create_info, vkt::set_layout);
image_create_info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
VkImageSwapchainCreateInfoKHR image_swapchain_create_info = vku::InitStructHelper();
image_swapchain_create_info.swapchain = m_swapchain;
image_create_info.pNext = &image_swapchain_create_info;
vkt::Image image_from_swapchain(*m_device, image_create_info, vkt::no_mem);
VkBindImageMemorySwapchainInfoKHR bind_swapchain_info = vku::InitStructHelper();
bind_swapchain_info.swapchain = m_swapchain;
bind_swapchain_info.imageIndex = 0;
VkBindImageMemoryInfo bind_info = vku::InitStructHelper(&bind_swapchain_info);
bind_info.image = image_from_swapchain;
bind_info.memory = VK_NULL_HANDLE;
bind_info.memoryOffset = 0;
vk::BindImageMemory2(device(), 1, &bind_info);
VkImageCopy copy_region = {};
copy_region.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
copy_region.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
copy_region.srcOffset = {0, 0, 0};
copy_region.dstOffset = {0, 0, 0};
copy_region.extent = {std::min(10u, m_surface_capabilities.minImageExtent.width),
std::min(10u, m_surface_capabilities.minImageExtent.height), 1};
m_command_buffer.Begin();
vk::CmdCopyImage(m_command_buffer, srcImage, VK_IMAGE_LAYOUT_GENERAL, image_from_swapchain, VK_IMAGE_LAYOUT_GENERAL, 1,
©_region);
}
TEST_F(PositiveWsi, TransferImageToSwapchainDeviceGroup) {
TEST_DESCRIPTION("Transfer an image to a swapchain's image between device group");
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
GTEST_SKIP()
<< "According to valid usage, VkBindImageMemoryInfo-memory should be NULL. But Android will crash if memory is NULL, "
"skipping test";
#endif
SetTargetApiVersion(VK_API_VERSION_1_2);
AddSurfaceExtension();
RETURN_IF_SKIP(InitFramework());
const auto physical_device_group = FindPhysicalDeviceGroup();
if (!physical_device_group.has_value()) {
GTEST_SKIP() << "cannot find physical device group that contains selected physical device";
}
VkDeviceGroupDeviceCreateInfo create_device_pnext = vku::InitStructHelper();
create_device_pnext.physicalDeviceCount = physical_device_group->physicalDeviceCount;
create_device_pnext.pPhysicalDevices = physical_device_group->physicalDevices;
RETURN_IF_SKIP(InitState(nullptr, &create_device_pnext));
InitRenderTarget();
RETURN_IF_SKIP(InitSwapchain(VK_IMAGE_USAGE_TRANSFER_DST_BIT));
constexpr uint32_t test_extent_value = 10;
if (m_surface_capabilities.minImageExtent.width < test_extent_value ||
m_surface_capabilities.minImageExtent.height < test_extent_value) {
GTEST_SKIP() << "minImageExtent is not large enough";
}
VkImageCreateInfo image_create_info = vku::InitStructHelper();
image_create_info.imageType = VK_IMAGE_TYPE_2D;
image_create_info.format = m_surface_formats[0].format;
image_create_info.extent.width = m_surface_capabilities.minImageExtent.width;
image_create_info.extent.height = m_surface_capabilities.minImageExtent.height;
image_create_info.extent.depth = 1;
image_create_info.mipLevels = 1;
image_create_info.arrayLayers = 1;
image_create_info.samples = VK_SAMPLE_COUNT_1_BIT;
image_create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
image_create_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vkt::Image src_Image(*m_device, image_create_info, vkt::set_layout);
image_create_info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
VkImageSwapchainCreateInfoKHR image_swapchain_create_info = vku::InitStructHelper();
image_swapchain_create_info.swapchain = m_swapchain;
image_create_info.pNext = &image_swapchain_create_info;
vkt::Image peer_image(*m_device, image_create_info, vkt::no_mem);
VkBindImageMemoryDeviceGroupInfo bind_devicegroup_info = vku::InitStructHelper();
std::array<uint32_t, 1> deviceIndices = {{0}};
bind_devicegroup_info.deviceIndexCount = static_cast<uint32_t>(deviceIndices.size());
bind_devicegroup_info.pDeviceIndices = deviceIndices.data();
bind_devicegroup_info.splitInstanceBindRegionCount = 0;
bind_devicegroup_info.pSplitInstanceBindRegions = nullptr;
VkBindImageMemorySwapchainInfoKHR bind_swapchain_info = vku::InitStructHelper(&bind_devicegroup_info);
bind_swapchain_info.swapchain = m_swapchain;
bind_swapchain_info.imageIndex = 0;
VkBindImageMemoryInfo bind_info = vku::InitStructHelper(&bind_swapchain_info);
bind_info.image = peer_image;
bind_info.memory = VK_NULL_HANDLE;
bind_info.memoryOffset = 0;
vk::BindImageMemory2(device(), 1, &bind_info);
// Can transition layout after the memory is bound
peer_image.SetLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
const auto swapchain_images = m_swapchain.GetImages();
vkt::Fence fence(*m_device);
const uint32_t image_index = m_swapchain.AcquireNextImage(fence, kWaitTimeout);
vk::WaitForFences(device(), 1, &fence.handle(), VK_TRUE, kWaitTimeout);
m_command_buffer.Begin();
VkImageMemoryBarrier img_barrier = vku::InitStructHelper();
img_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
img_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
img_barrier.image = swapchain_images[image_index];
img_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
img_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
img_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
img_barrier.subresourceRange.baseArrayLayer = 0;
img_barrier.subresourceRange.baseMipLevel = 0;
img_barrier.subresourceRange.layerCount = 1;
img_barrier.subresourceRange.levelCount = 1;
vk::CmdPipelineBarrier(m_command_buffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, 0, nullptr,
0, nullptr, 1, &img_barrier);
VkImageCopy copy_region = {};
copy_region.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
copy_region.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
copy_region.srcOffset = {0, 0, 0};
copy_region.dstOffset = {0, 0, 0};
copy_region.extent = {test_extent_value, test_extent_value, 1};
vk::CmdCopyImage(m_command_buffer, src_Image, VK_IMAGE_LAYOUT_GENERAL, peer_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1,
©_region);
m_command_buffer.End();
m_default_queue->SubmitAndWait(m_command_buffer);
}
TEST_F(PositiveWsi, SwapchainAcquireImageAndPresent) {
TEST_DESCRIPTION("Test acquiring swapchain image and then presenting it.");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const vkt::Semaphore acquire_semaphore(*m_device);
const auto swapchain_images = m_swapchain.GetImages();
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
SetPresentImageLayout(swapchain_images[image_index]);
m_default_queue->Present(m_swapchain, image_index, acquire_semaphore);
m_default_queue->Wait();
}
TEST_F(PositiveWsi, SwapchainAcquireImageAndWaitForFence) {
TEST_DESCRIPTION("Test waiting on swapchain image with a fence.");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
const vkt::Fence fence(*m_device);
const uint32_t image_index = m_swapchain.AcquireNextImage(fence, kWaitTimeout);
vk::WaitForFences(device(), 1, &fence.handle(), VK_TRUE, kWaitTimeout);
m_default_queue->Present(m_swapchain, image_index, vkt::no_semaphore);
m_default_queue->Wait();
}
TEST_F(PositiveWsi, WaitForAcquireFenceAndIgnoreSemaphore) {
TEST_DESCRIPTION("Image acquire specifies both semaphore and fence to signal. Only fence is being waited on.");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
// Ask image acquire operation to signal both a semaphore and a fence
const vkt::Semaphore semaphore(*m_device);
const vkt::Fence fence(*m_device);
uint32_t image_index = 0;
vk::AcquireNextImageKHR(device(), m_swapchain, kWaitTimeout, semaphore, fence, &image_index);
vk::WaitForFences(device(), 1, &fence.handle(), VK_TRUE, kWaitTimeout);
// Present without waiting for the semaphore. That's fine because we waited on the fence
m_default_queue->Present(m_swapchain, image_index, vkt::no_semaphore);
m_default_queue->Wait();
}
TEST_F(PositiveWsi, WaitForAcquireSemaphoreAndIgnoreFence) {
TEST_DESCRIPTION("Image acquire specifies both semaphore and fence to signal. Only semaphore is being waited on.");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
// Ask image acquire operation to signal both a semaphore and a fence
const vkt::Semaphore semaphore(*m_device);
const vkt::Fence fence(*m_device);
uint32_t image_index = 0;
vk::AcquireNextImageKHR(device(), m_swapchain, kWaitTimeout, semaphore, fence, &image_index);
// Present without waiting on the fence. That's fine because present waits for the semaphore
m_default_queue->Present(m_swapchain, image_index, semaphore);
// NOTE: this test validates vkQueuePresentKHR.
// At this point it's fine to wait for the fence to avoid in-use errors during test exit
// (QueueWaitIdle does not wait for the fence signaled by the non-queue operation - AcquireNextImageKHR).
vk::WaitForFences(device(), 1, &fence.handle(), VK_TRUE, kWaitTimeout);
m_default_queue->Wait();
}
TEST_F(PositiveWsi, RetireSubmissionUsingAcquireFence) {
TEST_DESCRIPTION("Acquire fence can be used to determine that submission from previous frame finished.");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
std::vector<vkt::CommandBuffer> command_buffers;
std::vector<vkt::Semaphore> submit_semaphores;
for (size_t i = 0; i < swapchain_images.size(); i++) {
command_buffers.emplace_back(*m_device, m_command_pool);
submit_semaphores.emplace_back(*m_device);
}
const vkt::Fence acquire_fence(*m_device);
const int frame_count = 10;
for (int i = 0; i < frame_count; i++) {
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_fence, kWaitTimeout);
// 1) wait on the fence -> image was acquired
// 2) image was acquired -> image was presented in one of the previous frames
// (except for the first few frames, where the image is presented for the first time)
// 3) image was presented -> corresponding present waited on the submit semaphore
// 4) submit semaphore was waited -> corresponding submit finished execution and signaled semaphore
//
// In summary: waiting on the acquire fence (with specific frame setup) means that one of the
// previous submission has finished execution and it should be safe to re-use corresponding command buffer.
vk::WaitForFences(device(), 1, &acquire_fence.handle(), VK_TRUE, kWaitTimeout);
vk::ResetFences(device(), 1, &acquire_fence.handle());
// There should not be in-use errors when we re-use command buffer that corresponds to the acquired image index.
command_buffers[image_index].Begin();
command_buffers[image_index].End();
m_default_queue->Submit(command_buffers[image_index], vkt::Signal(submit_semaphores[image_index]));
m_default_queue->Present(m_swapchain, image_index, submit_semaphores[image_index]);
}
m_default_queue->Wait();
}
TEST_F(PositiveWsi, RetireSubmissionUsingAcquireFence2) {
TEST_DESCRIPTION("Test that retiring submission using acquire fence works correctly after swapchain was changed.");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
std::vector<vkt::CommandBuffer> command_buffers;
std::vector<vkt::Semaphore> submit_semaphores;
for (size_t i = 0; i < swapchain_images.size(); i++) {
command_buffers.emplace_back(*m_device, m_command_pool);
submit_semaphores.emplace_back(*m_device);
}
const vkt::Fence acquire_fence(*m_device);
uint32_t image_index = m_swapchain.AcquireNextImage(acquire_fence, kWaitTimeout);
vk::WaitForFences(device(), 1, &acquire_fence.handle(), VK_TRUE, kWaitTimeout);
vk::ResetFences(device(), 1, &acquire_fence.handle());
command_buffers[image_index].Begin();
command_buffers[image_index].End();
m_default_queue->Submit(command_buffers[image_index], vkt::Signal(submit_semaphores[image_index]));
m_default_queue->Present(m_swapchain, image_index, submit_semaphores[image_index]);
// Here the application decides to destroy swapchain (e.g. resize event)
m_swapchain.destroy();
// At this point there's a pending frame we need to sync with.
// WaitForFences(acquire_fence) logic can't be used, because swapchain was destroyed and its acquire
// fence can't be waited on. Application can use arbitrary logic to sync with the previous frames.
// After swapchain is re-created we can continue to use WaitForFences(acquire_fence) sync model.
//
// Here we just wait on the queue.
// If this line is removed we can get in-use error when begin command buffer.
m_default_queue->Wait();
// Create new swapchain.
m_swapchain = CreateSwapchain(m_surface.Handle(), VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR);
image_index = m_swapchain.AcquireNextImage(acquire_fence, kWaitTimeout);
vk::WaitForFences(device(), 1, &acquire_fence.handle(), VK_TRUE, kWaitTimeout);
vk::ResetFences(device(), 1, &acquire_fence.handle());
command_buffers[image_index].Begin();
command_buffers[image_index].End();
m_default_queue->Wait();
}
TEST_F(PositiveWsi, RetireSubmissionUsingAcquireFence3) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8880
TEST_DESCRIPTION("Test that retiring submission using acquire fence works correctly when using differnt fences.");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
std::vector<vkt::Fence> acquire_fences;
vkt::Fence acquire_fence(*m_device); // extra acquire fence
std::vector<vkt::CommandBuffer> command_buffers;
std::vector<vkt::Semaphore> submit_semaphores;
for (size_t i = 0; i < swapchain_images.size(); i++) {
acquire_fences.emplace_back(*m_device);
command_buffers.emplace_back(*m_device, m_command_pool);
command_buffers[i].Begin();
command_buffers[i].End();
submit_semaphores.emplace_back(*m_device);
}
const int frame_count = 10;
for (int i = 0; i < frame_count; i++) {
uint32_t image_index = 0;
vk::AcquireNextImageKHR(device(), m_swapchain, kWaitTimeout, VK_NULL_HANDLE, acquire_fence, &image_index);
acquire_fence.Wait(kWaitTimeout);
acquire_fence.Reset();
m_default_queue->Submit(command_buffers[image_index], vkt::Signal(submit_semaphores[image_index]));
m_default_queue->Present(m_swapchain, image_index, submit_semaphores[image_index]);
std::swap(acquire_fences[image_index], acquire_fence);
}
m_default_queue->Wait();
}
TEST_F(PositiveWsi, SwapchainImageLayout) {
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
const vkt::Fence fence(*m_device);
uint32_t image_index = 0;
{
VkResult result{};
image_index = m_swapchain.AcquireNextImage(fence, kWaitTimeout, &result);
ASSERT_TRUE(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR);
fence.Wait(vvl::kU32Max);
}
VkAttachmentDescription attach[] = {
{0, m_surface_formats[0].format, VK_SAMPLE_COUNT_1_BIT, VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE,
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL},
};
VkAttachmentReference att_ref = {0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkSubpassDescription subpass = {0, VK_PIPELINE_BIND_POINT_GRAPHICS, 0, nullptr, 1, &att_ref, nullptr, nullptr, 0, nullptr};
VkRenderPassCreateInfo rpci = vku::InitStructHelper();
rpci.attachmentCount = 1;
rpci.pAttachments = attach;
rpci.subpassCount = 1;
rpci.pSubpasses = &subpass;
vkt::RenderPass rp1(*m_device, rpci);
ASSERT_TRUE(rp1.initialized());
attach[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
vkt::RenderPass rp2(*m_device, rpci);
ASSERT_TRUE(rp2.initialized());
VkImageViewCreateInfo ivci = vku::InitStructHelper();
ivci.image = swapchain_images[image_index];
ivci.viewType = VK_IMAGE_VIEW_TYPE_2D;
ivci.format = m_surface_formats[0].format;
ivci.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY};
ivci.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
vkt::ImageView view(*m_device, ivci);
vkt::Framebuffer fb1(*m_device, rp1, 1, &view.handle(), 1, 1);
vkt::Framebuffer fb2(*m_device, rp2, 1, &view.handle(), 1, 1);
m_command_buffer.Begin();
m_command_buffer.BeginRenderPass(rp1, fb1);
m_command_buffer.EndRenderPass();
m_command_buffer.BeginRenderPass(rp2, fb2);
m_command_buffer.EndRenderPass();
VkImageMemoryBarrier present_transition = vku::InitStructHelper();
present_transition.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
present_transition.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
present_transition.image = swapchain_images[image_index];
present_transition.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
vk::CmdPipelineBarrier(m_command_buffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0,
nullptr, 0, nullptr, 1, &present_transition);
m_command_buffer.End();
fence.Wait(kWaitTimeout);
fence.Reset();
m_default_queue->Submit(m_command_buffer, fence);
fence.Wait(kWaitTimeout);
}
TEST_F(PositiveWsi, SwapchainPresentShared) {
TEST_DESCRIPTION("Acquire shared presentable image and Present multiple times without failure.");
AddRequiredExtensions(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME);
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
VkBool32 supported;
vk::GetPhysicalDeviceSurfaceSupportKHR(Gpu(), m_device->graphics_queue_node_index_, m_surface.Handle(), &supported);
if (!supported) {
GTEST_SKIP() << "Graphics queue does not support present";
}
VkPresentModeKHR shared_present_mode = m_surface_non_shared_present_mode;
for (size_t i = 0; i < m_surface_present_modes.size(); i++) {
const VkPresentModeKHR present_mode = m_surface_present_modes[i];
if ((present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR) ||
(present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR)) {
shared_present_mode = present_mode;
break;
}
}
if (shared_present_mode == m_surface_non_shared_present_mode) {
GTEST_SKIP() << "Cannot find supported shared present mode";
}
VkSharedPresentSurfaceCapabilitiesKHR shared_present_capabilities = vku::InitStructHelper();
VkSurfaceCapabilities2KHR capabilities = vku::InitStructHelper(&shared_present_capabilities);
VkPhysicalDeviceSurfaceInfo2KHR surface_info = vku::InitStructHelper();
surface_info.surface = m_surface.Handle();
vk::GetPhysicalDeviceSurfaceCapabilities2KHR(Gpu(), &surface_info, &capabilities);
// This was recently added to CTS, but some drivers might not correctly advertise the flag
if ((shared_present_capabilities.sharedPresentSupportedUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) == 0) {
GTEST_SKIP() << "Driver was suppose to support VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT";
}
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper();
swapchain_create_info.surface = m_surface.Handle();
swapchain_create_info.minImageCount = 1;
swapchain_create_info.imageFormat = m_surface_formats[0].format;
swapchain_create_info.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = m_surface_capabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; // implementations must support
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_create_info.compositeAlpha = m_surface_composite_alpha;
swapchain_create_info.presentMode = shared_present_mode;
swapchain_create_info.clipped = VK_FALSE;
swapchain_create_info.oldSwapchain = 0;
m_swapchain.Init(*m_device, swapchain_create_info);
const auto images = m_swapchain.GetImages();
vkt::Fence fence(*m_device);
const uint32_t image_index = m_swapchain.AcquireNextImage(fence, kWaitTimeout);
vk::WaitForFences(device(), 1, &fence.handle(), true, kWaitTimeout);
SetPresentImageLayout(images[image_index]);
m_default_queue->Present(m_swapchain, image_index, vkt::no_semaphore);
// Presenting image multiple times is valid in the shared present mode.
//
// If a swapchain is created with presentMode set to either VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR or
// VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR, a single presentable image can be acquired, referred to as a shared
// presentable image. A shared presentable image may be concurrently accessed by the application and the presentation engine,
// without transitioning the image’s layout after it is initially presented.
//
// - With VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR, the presentation engine is only required to update to the latest contents
// of a shared presentable image after a present. The application must call vkQueuePresentKHR to guarantee an update. However,
// the presentation engine may update from it at any time.
for (uint32_t i = 0; i < 5; ++i) {
m_default_queue->Present(m_swapchain, image_index, vkt::no_semaphore);
}
}
TEST_F(PositiveWsi, CreateSurface) {
TEST_DESCRIPTION("Create and destroy a surface without ever creating a swapchain");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
DestroySwapchain(); // cleans up both surface and swapchain, if they were created
}
#if defined(VK_USE_PLATFORM_WIN32_KHR)
TEST_F(PositiveWsi, CreateSwapchainFullscreenExclusive) {
TEST_DESCRIPTION(
"Test creating a swapchain with VkSurfaceFullScreenExclusiveWin32InfoEXT and VK_FULL_SCREEN_EXCLUSIVE_DEFAULT_EXT");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_FULL_SCREEN_EXCLUSIVE_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
if (!IsPlatformMockICD()) {
GTEST_SKIP() << "Only run test MockICD due to CI stability";
}
InitRenderTarget();
RETURN_IF_SKIP(InitSwapchain());
VkSurfaceFullScreenExclusiveInfoEXT surface_full_screen_exlusive_info = vku::InitStructHelper();
surface_full_screen_exlusive_info.fullScreenExclusive = VK_FULL_SCREEN_EXCLUSIVE_DEFAULT_EXT;
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper(&surface_full_screen_exlusive_info);
swapchain_create_info.flags = 0;
swapchain_create_info.surface = m_surface.Handle();
swapchain_create_info.minImageCount = m_surface_capabilities.minImageCount;
swapchain_create_info.imageFormat = m_surface_formats[0].format;
swapchain_create_info.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = m_surface_capabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_create_info.compositeAlpha = m_surface_composite_alpha;
swapchain_create_info.presentMode = m_surface_non_shared_present_mode;
swapchain_create_info.clipped = VK_FALSE;
vkt::Swapchain swapchain(*m_device, swapchain_create_info);
}
#endif
#if defined(VK_USE_PLATFORM_WIN32_KHR)
TEST_F(PositiveWsi, CreateSwapchainFullscreenExclusive2) {
TEST_DESCRIPTION(
"Test creating a swapchain with VkSurfaceFullScreenExclusiveWin32InfoEXT and "
"VK_FULL_SCREEN_EXCLUSIVE_APPLICATION_CONTROLLED_EXT");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_FULL_SCREEN_EXCLUSIVE_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
if (!IsPlatformMockICD()) {
GTEST_SKIP() << "Only run test MockICD due to CI stability";
}
InitRenderTarget();
RETURN_IF_SKIP(InitSwapchain());
const POINT pt_zero = {0, 0};
VkSurfaceFullScreenExclusiveWin32InfoEXT fullscreen_exclusive_win32_info = vku::InitStructHelper();
fullscreen_exclusive_win32_info.hmonitor = MonitorFromPoint(pt_zero, MONITOR_DEFAULTTOPRIMARY);
VkSurfaceFullScreenExclusiveInfoEXT surface_full_screen_exlusive_info = vku::InitStructHelper(&fullscreen_exclusive_win32_info);
surface_full_screen_exlusive_info.fullScreenExclusive = VK_FULL_SCREEN_EXCLUSIVE_APPLICATION_CONTROLLED_EXT;
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper(&surface_full_screen_exlusive_info);
swapchain_create_info.flags = 0;
swapchain_create_info.surface = m_surface.Handle();
swapchain_create_info.minImageCount = m_surface_capabilities.minImageCount;
swapchain_create_info.imageFormat = m_surface_formats[0].format;
swapchain_create_info.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = m_surface_capabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_create_info.compositeAlpha = m_surface_composite_alpha;
swapchain_create_info.presentMode = m_surface_non_shared_present_mode;
swapchain_create_info.clipped = VK_FALSE;
vkt::Swapchain swapchain(*m_device, swapchain_create_info);
}
#endif
TEST_F(PositiveWsi, SwapchainImageFormatProps) {
TEST_DESCRIPTION("Try using special format props on a swapchain image");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
// HACK: I know InitSwapchain() will pick first supported format
VkSurfaceFormatKHR format_tmp;
{
uint32_t format_count = 1;
const VkResult err = vk::GetPhysicalDeviceSurfaceFormatsKHR(Gpu(), m_surface.Handle(), &format_count, &format_tmp);
ASSERT_TRUE(err == VK_SUCCESS || err == VK_INCOMPLETE) << string_VkResult(err);
}
const VkFormat format = format_tmp.format;
VkFormatProperties format_props;
vk::GetPhysicalDeviceFormatProperties(Gpu(), format, &format_props);
if (!(format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)) {
GTEST_SKIP() << "We need VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT feature";
}
VkAttachmentReference attach = {};
attach.layout = VK_IMAGE_LAYOUT_GENERAL;
VkSubpassDescription subpass = {};
subpass.pColorAttachments = &attach;
subpass.colorAttachmentCount = 1;
VkAttachmentDescription attach_desc = {};
attach_desc.format = format;
attach_desc.samples = VK_SAMPLE_COUNT_1_BIT;
attach_desc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attach_desc.finalLayout = VK_IMAGE_LAYOUT_GENERAL;
attach_desc.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attach_desc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
VkRenderPassCreateInfo rpci = vku::InitStructHelper();
rpci.subpassCount = 1;
rpci.pSubpasses = &subpass;
rpci.attachmentCount = 1;
rpci.pAttachments = &attach_desc;
vkt::RenderPass render_pass(*m_device, rpci);
VkPipelineColorBlendAttachmentState pcbas = {};
pcbas.blendEnable = VK_TRUE; // !!!
pcbas.colorWriteMask =
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
CreatePipelineHelper pipe(*this);
pipe.gp_ci_.renderPass = render_pass;
pipe.cb_attachments_ = pcbas;
pipe.CreateGraphicsPipeline();
const auto swapchain_images = m_swapchain.GetImages();
const vkt::Fence fence(*m_device);
uint32_t image_index;
{
VkResult result{};
image_index = m_swapchain.AcquireNextImage(fence, kWaitTimeout, &result);
ASSERT_TRUE(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR);
fence.Wait(vvl::kU32Max);
}
VkImageViewCreateInfo ivci = vku::InitStructHelper();
ivci.image = swapchain_images[image_index];
ivci.viewType = VK_IMAGE_VIEW_TYPE_2D;
ivci.format = format;
ivci.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
vkt::ImageView image_view(*m_device, ivci);
vkt::Framebuffer framebuffer(*m_device, render_pass, 1, &image_view.handle(), 1, 1);
vkt::CommandBuffer cmdbuff(*m_device, m_command_pool);
cmdbuff.Begin();
cmdbuff.BeginRenderPass(render_pass, framebuffer);
vk::CmdBindPipeline(cmdbuff, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe);
}
TEST_F(PositiveWsi, SwapchainExclusiveModeQueueFamilyPropertiesReferences) {
TEST_DESCRIPTION("Try using special format props on a swapchain image");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
VkBool32 supported;
vk::GetPhysicalDeviceSurfaceSupportKHR(Gpu(), m_device->graphics_queue_node_index_, m_surface.Handle(), &supported);
if (!supported) {
GTEST_SKIP() << "Graphics queue does not support present";
}
VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
VkSurfaceTransformFlagBitsKHR preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper();
swapchain_create_info.surface = m_surface.Handle();
swapchain_create_info.minImageCount = m_surface_capabilities.minImageCount;
swapchain_create_info.imageFormat = m_surface_formats[0].format;
swapchain_create_info.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = m_surface_capabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = imageUsage;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = preTransform;
swapchain_create_info.compositeAlpha = m_surface_composite_alpha;
swapchain_create_info.presentMode = m_surface_non_shared_present_mode;
swapchain_create_info.clipped = VK_FALSE;
swapchain_create_info.oldSwapchain = 0;
swapchain_create_info.queueFamilyIndexCount = 4094967295; // This SHOULD get ignored
uint32_t bogus_int = 99;
swapchain_create_info.pQueueFamilyIndices = &bogus_int;
vkt::Swapchain swapchain(*m_device, swapchain_create_info);
}
TEST_F(PositiveWsi, InitSwapchain) {
TEST_DESCRIPTION("Make sure InitSwapchain is not producing anying invalid usage");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
DestroySwapchain();
}
TEST_F(PositiveWsi, DestroySwapchainWithBoundImages) {
TEST_DESCRIPTION("Try destroying a swapchain which has multiple images");
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
VkImageCreateInfo image_create_info = vku::InitStructHelper();
image_create_info.imageType = VK_IMAGE_TYPE_2D;
image_create_info.format = m_surface_formats[0].format;
image_create_info.extent.width = m_surface_capabilities.minImageExtent.width;
image_create_info.extent.height = m_surface_capabilities.minImageExtent.height;
image_create_info.extent.depth = 1;
image_create_info.mipLevels = 1;
image_create_info.arrayLayers = 1;
image_create_info.samples = VK_SAMPLE_COUNT_1_BIT;
image_create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
image_create_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VkImageSwapchainCreateInfoKHR image_swapchain_create_info = vku::InitStructHelper();
image_swapchain_create_info.swapchain = m_swapchain;
image_create_info.pNext = &image_swapchain_create_info;
std::vector<vkt::Image> images(m_surface_capabilities.minImageCount);
int i = 0;
for (auto &image : images) {
image.InitNoMemory(*m_device, image_create_info);
VkBindImageMemorySwapchainInfoKHR bind_swapchain_info = vku::InitStructHelper();
bind_swapchain_info.swapchain = m_swapchain;
bind_swapchain_info.imageIndex = i++;
VkBindImageMemoryInfo bind_info = vku::InitStructHelper(&bind_swapchain_info);
bind_info.image = image;
bind_info.memory = VK_NULL_HANDLE;
bind_info.memoryOffset = 0;
vk::BindImageMemory2KHR(device(), 1, &bind_info);
}
}
#if !defined(VK_USE_PLATFORM_ANDROID_KHR)
// Protected swapchains are guaranteed in Android Loader
// VK_KHR_surface_protected_capabilities is needed for other platforms
// Without device to test with, blocking this test from non-Android platforms for now
TEST_F(PositiveWsi, DISABLED_ProtectedSwapchainImageColorAttachment) {
#else
TEST_F(PositiveWsi, ProtectedSwapchainImageColorAttachment) {
#endif
TEST_DESCRIPTION(
"Make sure images from protected swapchain are considered protected image when writing to it as a color attachment");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME);
RETURN_IF_SKIP(InitFramework());
VkPhysicalDeviceProtectedMemoryFeatures protected_memory_features = vku::InitStructHelper();
GetPhysicalDeviceFeatures2(protected_memory_features);
if (protected_memory_features.protectedMemory == VK_FALSE) {
GTEST_SKIP() << "protectedMemory feature not supported, skipped.";
};
// Turns m_command_buffer into a unprotected command buffer
RETURN_IF_SKIP(InitState(nullptr, &protected_memory_features));
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
// Create protected swapchain
VkBool32 supported;
vk::GetPhysicalDeviceSurfaceSupportKHR(Gpu(), m_device->graphics_queue_node_index_, m_surface.Handle(), &supported);
if (!supported) {
GTEST_SKIP() << "Graphics queue does not support present, skipping test";
}
VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
VkSurfaceTransformFlagBitsKHR preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper();
swapchain_create_info.flags = VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR;
swapchain_create_info.surface = m_surface.Handle();
swapchain_create_info.minImageCount = m_surface_capabilities.minImageCount;
swapchain_create_info.imageFormat = m_surface_formats[0].format;
swapchain_create_info.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = m_surface_capabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = imageUsage;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = preTransform;
swapchain_create_info.compositeAlpha = m_surface_composite_alpha;
swapchain_create_info.presentMode = m_surface_non_shared_present_mode;
swapchain_create_info.clipped = VK_FALSE;
swapchain_create_info.oldSwapchain = 0;
swapchain_create_info.queueFamilyIndexCount = 4094967295; // This SHOULD get ignored
uint32_t bogus_int = 99;
swapchain_create_info.pQueueFamilyIndices = &bogus_int;
m_swapchain.Init(*m_device, swapchain_create_info);
ASSERT_TRUE(m_swapchain.initialized());
// Get VkImage from swapchain which should be protected
const auto swapchain_images = m_swapchain.GetImages();
VkImage protected_image = swapchain_images.at(0); // only need 1 image to test
// Create a protected image view
VkImageViewCreateInfo image_view_create_info = {
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
nullptr,
0,
protected_image,
VK_IMAGE_VIEW_TYPE_2D,
swapchain_create_info.imageFormat,
{VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY},
{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
};
vkt::ImageView image_view(*m_device, image_view_create_info);
// A renderpass and framebuffer that contains a protected color image view
VkAttachmentDescription attachments[1] = {{0, swapchain_create_info.imageFormat, VK_SAMPLE_COUNT_1_BIT,
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE,
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE,
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}};
VkAttachmentReference references[1] = {{0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}};
VkSubpassDescription subpass = {0, VK_PIPELINE_BIND_POINT_GRAPHICS, 0, nullptr, 1, references, nullptr, nullptr, 0, nullptr};
VkSubpassDependency dependency = {0,
0,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_ACCESS_SHADER_WRITE_BIT,
VK_ACCESS_SHADER_WRITE_BIT,
VK_DEPENDENCY_BY_REGION_BIT};
// Use framework render pass and framebuffer so pipeline helper uses it
VkRenderPassCreateInfo rp_info = vku::InitStructHelper();
rp_info.attachmentCount = 1;
rp_info.pAttachments = attachments;
rp_info.subpassCount = 1;
rp_info.pSubpasses = &subpass;
rp_info.dependencyCount = 1;
rp_info.pDependencies = &dependency;
ASSERT_EQ(VK_SUCCESS, vk::CreateRenderPass(device(), &rp_info, nullptr, &m_renderPass));
vkt::Framebuffer fb(*m_device, m_renderPass, 1, &image_view.handle(), swapchain_create_info.imageExtent.width,
swapchain_create_info.imageExtent.height);
// basic pipeline to allow for a valid vkCmdDraw()
VkShaderObj vs(this, kVertexMinimalGlsl, VK_SHADER_STAGE_VERTEX_BIT);
VkShaderObj fs(this, kFragmentMinimalGlsl, VK_SHADER_STAGE_FRAGMENT_BIT);
CreatePipelineHelper pipe(*this);
pipe.shader_stages_ = {vs.GetStageCreateInfo(), fs.GetStageCreateInfo()};
pipe.CreateGraphicsPipeline();
// Create a protected command buffer/pool to use
vkt::CommandPool protectedCommandPool(*m_device, m_device->graphics_queue_node_index_, VK_COMMAND_POOL_CREATE_PROTECTED_BIT);
vkt::CommandBuffer protectedCommandBuffer(*m_device, protectedCommandPool);
protectedCommandBuffer.Begin();
VkRect2D render_area = {{0, 0}, swapchain_create_info.imageExtent};
VkRenderPassBeginInfo render_pass_begin =
vku::InitStruct<VkRenderPassBeginInfo>(nullptr, m_renderPass, fb.handle(), render_area, 0u, nullptr);
vk::CmdBeginRenderPass(protectedCommandBuffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
vk::CmdBindPipeline(protectedCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe);
// This should be valid since the framebuffer color attachment is a protected swapchain image
vk::CmdDraw(protectedCommandBuffer, 3, 1, 0, 0);
vk::CmdEndRenderPass(protectedCommandBuffer);
protectedCommandBuffer.End();
}
TEST_F(PositiveWsi, CreateSwapchainWithPresentModeInfo) {
TEST_DESCRIPTION("Try destroying a swapchain which has multiple images");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
// Implementations must support.
// Also most likely to have lower minImageCount than reported for other present modes
// (although this is implementation dependant)
const auto present_mode = VK_PRESENT_MODE_FIFO_KHR;
VkSurfacePresentModeEXT surface_present_mode = vku::InitStructHelper();
surface_present_mode.presentMode = present_mode;
VkPhysicalDeviceSurfaceInfo2KHR surface_info = vku::InitStructHelper(&surface_present_mode);
surface_info.surface = m_surface.Handle();
VkSurfaceCapabilities2KHR surface_caps = vku::InitStructHelper();
vk::GetPhysicalDeviceSurfaceCapabilities2KHR(m_device->Physical(), &surface_info, &surface_caps);
VkSwapchainPresentModesCreateInfoEXT swapchain_present_mode_create_info = vku::InitStructHelper();
swapchain_present_mode_create_info.presentModeCount = 1;
swapchain_present_mode_create_info.pPresentModes = &present_mode;
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper(&swapchain_present_mode_create_info);
swapchain_create_info.surface = m_surface.Handle();
swapchain_create_info.minImageCount = surface_caps.surfaceCapabilities.minImageCount;
swapchain_create_info.imageFormat = m_surface_formats[0].format;
swapchain_create_info.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = surface_caps.surfaceCapabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; // implementations must support
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_create_info.compositeAlpha = m_surface_composite_alpha;
swapchain_create_info.presentMode = present_mode;
swapchain_create_info.clipped = VK_FALSE;
swapchain_create_info.oldSwapchain = 0;
m_swapchain.Init(*m_device, swapchain_create_info);
}
TEST_F(PositiveWsi, RegisterDisplayEvent) {
TEST_DESCRIPTION("Call vkRegisterDisplayEventEXT");
AddRequiredExtensions(VK_EXT_DISPLAY_CONTROL_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
uint32_t prop_count = 0;
vk::GetPhysicalDeviceDisplayPropertiesKHR(Gpu(), &prop_count, nullptr);
if (prop_count == 0) {
GTEST_SKIP() << "No VkDisplayKHR properties to query";
}
std::vector<VkDisplayPropertiesKHR> display_props{prop_count};
vk::GetPhysicalDeviceDisplayPropertiesKHR(Gpu(), &prop_count, display_props.data());
VkDisplayKHR display = display_props[0].display;
VkDisplayEventInfoEXT event_info = vku::InitStructHelper();
event_info.displayEvent = VK_DISPLAY_EVENT_TYPE_FIRST_PIXEL_OUT_EXT;
VkFence fence;
vk::RegisterDisplayEventEXT(device(), display, &event_info, nullptr, &fence);
vk::DestroyFence(device(), fence, nullptr);
}
TEST_F(PositiveWsi, SurfacelessQueryTest) {
TEST_DESCRIPTION("Ensure affected API calls can be made with surfacless query extension");
AddRequiredExtensions(VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME);
RETURN_IF_SKIP(InitFramework());
if (IsPlatformMockICD()) {
GTEST_SKIP() << "VK_GOOGLE_surfaceless_query not supported on desktop";
}
// Use the VK_GOOGLE_surfaceless_query extension to query the available formats and
// colorspaces by using a VK_NULL_HANDLE for the VkSurfaceKHR handle.
uint32_t count;
vk::GetPhysicalDeviceSurfaceFormatsKHR(Gpu(), VK_NULL_HANDLE, &count, nullptr);
std::vector<VkSurfaceFormatKHR> surface_formats(count);
vk::GetPhysicalDeviceSurfaceFormatsKHR(Gpu(), VK_NULL_HANDLE, &count, surface_formats.data());
vk::GetPhysicalDeviceSurfacePresentModesKHR(Gpu(), VK_NULL_HANDLE, &count, nullptr);
std::vector<VkPresentModeKHR> present_modes(count);
vk::GetPhysicalDeviceSurfacePresentModesKHR(Gpu(), VK_NULL_HANDLE, &count, present_modes.data());
}
TEST_F(PositiveWsi, PhysicalDeviceSurfaceSupport) {
TEST_DESCRIPTION("Test if physical device supports surface.");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
VkBool32 supported;
vk::GetPhysicalDeviceSurfaceSupportKHR(Gpu(), 0, m_surface.Handle(), &supported);
if (supported) {
uint32_t count;
vk::GetPhysicalDeviceSurfaceFormatsKHR(Gpu(), m_surface.Handle(), &count, nullptr);
}
}
TEST_F(PositiveWsi, AcquireImageBeforeGettingSwapchainImages) {
TEST_DESCRIPTION("Call vkAcquireNextImageKHR before vkGetSwapchainImagesKHR");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
VkBool32 supported;
vk::GetPhysicalDeviceSurfaceSupportKHR(Gpu(), m_device->graphics_queue_node_index_, m_surface.Handle(), &supported);
if (!supported) {
GTEST_SKIP() << "Surface not supported.";
}
SurfaceInformation info = GetSwapchainInfo(m_surface.Handle());
InitSwapchainInfo();
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper();
swapchain_create_info.surface = m_surface.Handle();
swapchain_create_info.minImageCount = info.surface_capabilities.minImageCount;
swapchain_create_info.imageFormat = info.surface_formats[0].format;
swapchain_create_info.imageColorSpace = info.surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = info.surface_capabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_create_info.compositeAlpha = info.surface_composite_alpha;
swapchain_create_info.presentMode = info.surface_non_shared_present_mode;
swapchain_create_info.clipped = VK_FALSE;
swapchain_create_info.oldSwapchain = VK_NULL_HANDLE;
vkt::Swapchain swapchain(*m_device, swapchain_create_info);
vkt::Fence fence(*m_device);
uint32_t image_index = swapchain.AcquireNextImage(fence, kWaitTimeout);
vk::WaitForFences(device(), 1u, &fence.handle(), VK_FALSE, kWaitTimeout);
const std::vector<VkImage> swapchain_images = swapchain.GetImages();
SetPresentImageLayout(swapchain_images[image_index]);
m_default_queue->Present(swapchain, image_index, vkt::no_semaphore);
}
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/7025
TEST_F(PositiveWsi, PresentFenceWaitsForSubmission) {
TEST_DESCRIPTION("Use present fence to wait for submission");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
// Warm up. Show that we can reset command buffer after waiting on **submit** fence
{
m_command_buffer.Begin();
m_command_buffer.End();
vkt::Fence submit_fence(*m_device);
m_default_queue->Submit(m_command_buffer, submit_fence);
vk::WaitForFences(device(), 1, &submit_fence.handle(), VK_TRUE, kWaitTimeout);
// It's safe to reset command buffer because we waited on the fence
m_command_buffer.Reset();
}
// Main performance. Show that we can reset command buffer after waiting on **present** fence
{
const vkt::Semaphore acquire_semaphore(*m_device);
const vkt::Semaphore submit_semaphore(*m_device);
const auto swapchain_images = m_swapchain.GetImages();
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
SetPresentImageLayout(swapchain_images[image_index]);
m_command_buffer.Begin();
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer, vkt::Wait(acquire_semaphore), vkt::Signal(submit_semaphore));
vkt::Fence present_fence(*m_device);
VkSwapchainPresentFenceInfoEXT present_fence_info = vku::InitStructHelper();
present_fence_info.swapchainCount = 1;
present_fence_info.pFences = &present_fence.handle();
m_default_queue->Present(m_swapchain, image_index, submit_semaphore, &present_fence_info);
present_fence.Wait(kWaitTimeout);
// It should be safe to reset command buffer after waiting on present fence:
// wait on present fence ->
// present was initiated ->
// submit semaphore signaled ->
// QueueSubmit workload has completed ->
// command buffer is no longer in use and we can reset it.
m_command_buffer.Reset();
}
m_default_queue->Wait();
}
TEST_F(PositiveWsi, PresentFenceRetiresPresentQueueOperation) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8047
// The regression will cause occasional failures of this test. The reproducibility
// is very machine dependent and in some configurations the failures can be
// extremely rare. We also found configurations (slower laptop) where it was relatively
// easy to reproduce (still could take some time, tens of seconds and up to few minutes).
//
// NOTE: there are known bugs in the current queue progress tracking, when
// a submission might retire too early (happens for multiple queues, but present
// operation might be an example for a single queue). Reworking queue tracking
// from threading approach to a single manager that collects submits and resolves
// them on request should fix the known issues, but also will bring deterministic
// behavior to the issues like the one being tested here. The idea that resolve
// operation, even if non trivial, still will be a localized piece of code comparing
// to conceptually simple model of queues that process submissions one at a time
// but with more complex synchronization and non-deterministic behavior.
TEST_DESCRIPTION("Check that the wait on the present fence retires present queue operation");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
struct Frame {
vkt::Semaphore image_acquired;
vkt::Semaphore submit_finished;
vkt::Fence present_finished_fence;
uint32_t frame = 0; // for debugging
};
std::vector<Frame> frames;
// TODO: iteration count can be reduced (100?) if queue simulation is done in more deterministic way
for (uint32_t i = 0; i < 500; i++) {
// Remove completed frames
for (auto it = frames.begin(); it != frames.end();) {
if (it->present_finished_fence.GetStatus() == VK_SUCCESS) {
// NOTE: Root cause of the issue. The present fence processed regular queue submissions,
// but not the one associated with a present operation. The present batch usually was
// lucky enough to get through, before we start the following "erase", which deletes the
// present batch semaphore. When the queue thread was not fast enough, then in-use state
// of present semaphore was properly detected (VUID-vkDestroySemaphore-semaphore-05149).
it = frames.erase(it);
} else {
++it;
}
}
// Add new frame
frames.emplace_back(Frame{vkt::Semaphore(*m_device), vkt::Semaphore(*m_device), vkt::Fence(*m_device), i});
const Frame &frame = frames.back();
const uint32_t image_index = m_swapchain.AcquireNextImage(frame.image_acquired, kWaitTimeout);
m_default_queue->Submit(vkt::no_cmd, vkt::Wait(frame.image_acquired), vkt::Signal(frame.submit_finished));
VkSwapchainPresentFenceInfoEXT present_fence_info = vku::InitStructHelper();
present_fence_info.swapchainCount = 1;
present_fence_info.pFences = &frame.present_finished_fence.handle();
m_default_queue->Present(m_swapchain, image_index, frame.submit_finished, &present_fence_info);
}
m_default_queue->Wait();
}
TEST_F(PositiveWsi, QueueWaitsForPresentFence) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8376
// https://gitlab.khronos.org/vulkan/vulkan/-/issues/3962
TEST_DESCRIPTION("QueueWaitIdle waits for present fence");
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const vkt::Semaphore acquire_semaphore(*m_device);
const auto swapchain_images = m_swapchain.GetImages();
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
SetPresentImageLayout(swapchain_images[image_index]);
vkt::Fence present_fence(*m_device);
VkSwapchainPresentFenceInfoEXT present_fence_info = vku::InitStructHelper();
present_fence_info.swapchainCount = 1;
present_fence_info.pFences = &present_fence.handle();
m_default_queue->Present(m_swapchain, image_index, acquire_semaphore, &present_fence_info);
// QueueWaitIdle (and also DeviceWaitIdle) can wait for present fences.
m_default_queue->Wait();
// This should not report in-use error
present_fence.Reset();
}
TEST_F(PositiveWsi, QueueWaitsForPresentFence2) {
TEST_DESCRIPTION("QueueWaitIdle waits for present fence");
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
SurfaceContext surface_context;
vkt::Surface surface2;
CreateSurface(surface_context, surface2);
vkt::Swapchain swapchain2 =
CreateSwapchain(surface2.Handle(), VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR);
const vkt::Semaphore acquire_semaphore(*m_device);
const auto swapchain_images = m_swapchain.GetImages();
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
const vkt::Semaphore acquire_semaphore2(*m_device);
const auto swapchain_images2 = swapchain2.GetImages();
const uint32_t image_index2 = swapchain2.AcquireNextImage(acquire_semaphore2, kWaitTimeout);
SetPresentImageLayout(swapchain_images[image_index]);
SetPresentImageLayout(swapchain_images2[image_index2]);
vkt::Fence present_fence(*m_device);
vkt::Fence present_fence2(*m_device);
const VkFence present_fences[2] = {present_fence, present_fence2};
VkSwapchainPresentFenceInfoEXT present_fence_info = vku::InitStructHelper();
present_fence_info.swapchainCount = 2;
present_fence_info.pFences = present_fences;
const VkSemaphore wait_semaphores[2] = {acquire_semaphore, acquire_semaphore2};
const VkSwapchainKHR swapchains[2] = {m_swapchain, swapchain2};
const uint32_t image_indices[2]{image_index, image_index2};
VkPresentInfoKHR present = vku::InitStructHelper(&present_fence_info);
present.waitSemaphoreCount = 2;
present.pWaitSemaphores = wait_semaphores;
present.swapchainCount = 2;
present.pSwapchains = swapchains;
present.pImageIndices = image_indices;
vk::QueuePresentKHR(*m_default_queue, &present);
m_default_queue->Wait();
present_fence.Reset();
present_fence2.Reset();
}
TEST_F(PositiveWsi, PresentFenceRetiresPresentSemaphores) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8578
TEST_DESCRIPTION("Delete present wait semaphore after waiting on present fence");
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
SurfaceContext surface_context2;
vkt::Surface surface2;
CreateSurface(surface_context2, surface2);
vkt::Swapchain swapchain2 =
CreateSwapchain(surface2.Handle(), VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR);
vkt::Semaphore acquire_semaphore(*m_device);
const auto swapchain_images = m_swapchain.GetImages();
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
SetPresentImageLayout(swapchain_images[image_index]);
vkt::Semaphore acquire_semaphore2(*m_device);
const auto swapchain_images2 = swapchain2.GetImages();
const uint32_t image_index2 = swapchain2.AcquireNextImage(acquire_semaphore2, kWaitTimeout);
SetPresentImageLayout(swapchain_images2[image_index2]);
const VkSemaphore acquire_semaphores_handles[2] = {acquire_semaphore, acquire_semaphore2};
const VkSwapchainKHR swapchain_handles[2] = {m_swapchain, swapchain2};
const VkPipelineStageFlags wait_stage_masks[2] = {VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT};
vkt::Semaphore submit_semaphore(*m_device);
VkSubmitInfo submit_info = vku::InitStructHelper();
submit_info.waitSemaphoreCount = 2;
submit_info.pWaitSemaphores = acquire_semaphores_handles;
submit_info.pWaitDstStageMask = wait_stage_masks;
submit_info.signalSemaphoreCount = 1;
submit_info.pSignalSemaphores = &submit_semaphore.handle();
vk::QueueSubmit(m_default_queue->handle(), 1, &submit_info, VK_NULL_HANDLE);
vkt::Fence present_fence(*m_device);
vkt::Fence present_fence2(*m_device);
const VkFence present_fences_handles[2] = {present_fence, present_fence2};
VkSwapchainPresentFenceInfoEXT present_fence_info = vku::InitStructHelper();
present_fence_info.swapchainCount = 2;
present_fence_info.pFences = present_fences_handles;
const uint32_t image_indices[2] = {image_index, image_index2};
VkPresentInfoKHR present = vku::InitStructHelper(&present_fence_info);
present.waitSemaphoreCount = 1;
present.pWaitSemaphores = &submit_semaphore.handle();
present.swapchainCount = 2;
present.pSwapchains = swapchain_handles;
present.pImageIndices = image_indices;
vk::QueuePresentKHR(*m_default_queue, &present);
present_fence.Wait(kWaitTimeout);
// Waiting on any present fence must retire all present wait semaphores.
// It was not the case in the original issue when multiple images were presented.
// Deleting semaphore after the fence wait resulted in semaphore in-use error.
submit_semaphore = {};
present_fence2.Wait(kWaitTimeout);
}
TEST_F(PositiveWsi, DifferentPerPresentModeImageCount) {
TEST_DESCRIPTION("Create swapchain with per present mode minImageCount that is less than surface's general minImageCount");
#ifndef VK_USE_PLATFORM_WAYLAND_KHR
GTEST_SKIP() << "Test requires wayland platform support";
#else
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
WaylandContext wayland_ctx;
if (!wayland_ctx.Init()) {
GTEST_SKIP() << "Failed to create wayland context.";
}
VkWaylandSurfaceCreateInfoKHR surface_create_info = vku::InitStructHelper();
surface_create_info.display = wayland_ctx.display;
surface_create_info.surface = wayland_ctx.surface;
VkSurfaceKHR surface;
vk::CreateWaylandSurfaceKHR(instance(), &surface_create_info, nullptr, &surface);
auto info = GetSwapchainInfo(surface);
const auto present_mode = VK_PRESENT_MODE_FIFO_KHR; // Implementations must support
VkSurfaceCapabilities2KHR surface_caps = vku::InitStructHelper();
VkPhysicalDeviceSurfaceInfo2KHR surface_info = vku::InitStructHelper();
surface_info.surface = surface;
vk::GetPhysicalDeviceSurfaceCapabilities2KHR(Gpu(), &surface_info, &surface_caps);
const uint32_t general_min_image_count = surface_caps.surfaceCapabilities.minImageCount;
VkSurfacePresentModeEXT surface_present_mode = vku::InitStructHelper();
surface_present_mode.presentMode = present_mode;
surface_info.pNext = &surface_present_mode;
vk::GetPhysicalDeviceSurfaceCapabilities2KHR(Gpu(), &surface_info, &surface_caps);
const uint32_t per_present_mode_min_image_count = surface_caps.surfaceCapabilities.minImageCount;
if (per_present_mode_min_image_count >= general_min_image_count) {
vk::DestroySurfaceKHR(instance(), surface, nullptr);
wayland_ctx.Release();
GTEST_SKIP() << "Can't find present mode that uses less images than a general case";
}
VkSwapchainPresentModesCreateInfoEXT swapchain_present_mode_create_info = vku::InitStructHelper();
swapchain_present_mode_create_info.presentModeCount = 1;
swapchain_present_mode_create_info.pPresentModes = &present_mode;
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper(&swapchain_present_mode_create_info);
swapchain_create_info.surface = surface;
swapchain_create_info.minImageCount = per_present_mode_min_image_count;
swapchain_create_info.imageFormat = info.surface_formats[0].format;
swapchain_create_info.imageColorSpace = info.surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = surface_caps.surfaceCapabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchain_create_info.presentMode = present_mode;
swapchain_create_info.clipped = VK_FALSE;
swapchain_create_info.oldSwapchain = 0;
{ vkt::Swapchain swapchain(*m_device, swapchain_create_info); }
vk::DestroySurfaceKHR(instance(), surface, nullptr);
wayland_ctx.Release();
#endif
}
TEST_F(PositiveWsi, ReleaseSwapchainImages) {
TEST_DESCRIPTION("Test vkReleaseSwapchainImagesEXT");
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
VkBool32 supported;
vk::GetPhysicalDeviceSurfaceSupportKHR(Gpu(), m_device->graphics_queue_node_index_, m_surface.Handle(), &supported);
if (!supported) {
GTEST_SKIP() << "Graphics queue does not support present";
}
SurfaceInformation info = GetSwapchainInfo(m_surface.Handle());
const uint32_t imageCount = 4;
if (info.surface_capabilities.maxImageCount < imageCount) {
GTEST_SKIP() << "Test maxImageCount to be at least 4";
}
InitSwapchainInfo();
VkSwapchainCreateInfoKHR swapchain_create_info = vku::InitStructHelper();
swapchain_create_info.surface = m_surface.Handle();
swapchain_create_info.minImageCount = info.surface_capabilities.maxImageCount;
swapchain_create_info.imageFormat = info.surface_formats[0].format;
swapchain_create_info.imageColorSpace = info.surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = info.surface_capabilities.minImageExtent;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_create_info.compositeAlpha = info.surface_composite_alpha;
swapchain_create_info.presentMode = info.surface_non_shared_present_mode;
swapchain_create_info.clipped = VK_FALSE;
swapchain_create_info.oldSwapchain = VK_NULL_HANDLE;
m_swapchain = vkt::Swapchain(*m_device, swapchain_create_info);
ASSERT_TRUE(m_swapchain.initialized());
vkt::Fence fence(*m_device);
std::vector<uint32_t> release_indices;
uint32_t present_index = 0u;
for (uint32_t i = 0; i < imageCount - 1; ++i) {
uint32_t image_index = m_swapchain.AcquireNextImage(fence, kWaitTimeout);
if (i == 1) {
present_index = image_index;
} else {
release_indices.push_back(image_index);
}
vk::WaitForFences(device(), 1, &fence.handle(), VK_TRUE, kWaitTimeout);
vk::ResetFences(device(), 1, &fence.handle());
}
const auto swapchain_images = m_swapchain.GetImages();
SetPresentImageLayout(swapchain_images[present_index]);
m_default_queue->Present(m_swapchain, present_index, vkt::no_semaphore);
VkReleaseSwapchainImagesInfoEXT releaseInfo = vku::InitStructHelper();
releaseInfo.swapchain = m_swapchain;
releaseInfo.imageIndexCount = (uint32_t)release_indices.size();
releaseInfo.pImageIndices = release_indices.data();
vk::ReleaseSwapchainImagesEXT(device(), &releaseInfo);
vk::DeviceWaitIdle(device());
}
TEST_F(PositiveWsi, ReleaseAndAcquireSwapchainImages) {
TEST_DESCRIPTION("Test vkReleaseSwapchainImagesEXT");
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
vkt::Fence fence(*m_device);
for (uint32_t i = 0; i < 64; ++i) {
uint32_t image_index = m_swapchain.AcquireNextImage(fence, vvl::kU64Max);
vk::WaitForFences(device(), 1, &fence.handle(), VK_TRUE, kWaitTimeout);
vk::ResetFences(device(), 1, &fence.handle());
VkReleaseSwapchainImagesInfoEXT releaseInfo = vku::InitStructHelper();
releaseInfo.swapchain = m_swapchain;
releaseInfo.imageIndexCount = 1u;
releaseInfo.pImageIndices = &image_index;
vk::ReleaseSwapchainImagesEXT(device(), &releaseInfo);
}
vk::DeviceWaitIdle(device());
}
TEST_F(PositiveWsi, MultiSwapchainPresentWithOneBadSwapchain) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8753
TEST_DESCRIPTION("Present swapchains with a single QueuePresent command. One of the swapchains is out of date.");
AddSurfaceExtension();
RETURN_IF_SKIP(SupportMultiSwapchain());
RETURN_IF_SKIP(SupportSurfaceResize());
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
// This test make the second swapchain invalid (VK_ERROR_OUT_OF_DATE_KHR) and then try to present both swapchains.
// Presentation failure due to the second swapchain should not break state tracking for the first swapchain.
// In the origianl issue, state tracking for the first swapchain was skipped during QueuePresent and acquired
// images were never released. This generated false positives that too many images was acquired by the first swapchain.
SurfaceContext surface_context2;
vkt::Surface surface2;
CreateSurface(surface_context2, surface2);
auto swapchain2 =
CreateSwapchain(surface2.Handle(), VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR);
const VkSwapchainKHR swapchain_handles[2] = {m_swapchain, swapchain2};
auto cleanup_resources = [&] { m_default_queue->Wait(); };
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
const auto swapchain_images2 = swapchain2.GetImages();
for (auto image2 : swapchain_images2) {
SetPresentImageLayout(image2);
}
vkt::Semaphore acquire_semaphore(*m_device);
vkt::Semaphore acquire_semaphore2(*m_device);
const VkSemaphore acquire_semaphore_handles[2] = {acquire_semaphore, acquire_semaphore2};
std::vector<vkt::Semaphore> submit_semaphores;
for (size_t i = 0; i < swapchain_images.size(); i++) {
submit_semaphores.emplace_back(*m_device);
}
vkt::Fence frame_fence(*m_device);
const VkPipelineStageFlags wait_stage_masks[2] = {VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT};
// The image index from the second swapchain.
uint32_t image_index2{};
// Resize second swapchain window. This potentially generates VK_ERROR_OUT_OF_DATE_KHR in QueuePresent.
surface_context2.Resize(m_width / 2, m_height / 2);
// The first frame.
// Presentation to the second swapchain fails due to resized window.
{
VkResult acquire_result2{};
image_index2 = swapchain2.AcquireNextImage(acquire_semaphore2, kWaitTimeout, &acquire_result2);
if (acquire_result2 != VK_SUCCESS) {
cleanup_resources();
GTEST_SKIP() << "Cannot acquire image from the second swapchain. The test is designed for a scenario when it is "
"possible to acquire image after window resize (works on windows nvidia drivers)";
}
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
const uint32_t image_indices[2] = {image_index, image_index2};
VkSubmitInfo submit_info = vku::InitStructHelper();
submit_info.waitSemaphoreCount = 2;
submit_info.pWaitSemaphores = acquire_semaphore_handles;
submit_info.pWaitDstStageMask = wait_stage_masks;
submit_info.signalSemaphoreCount = 1;
submit_info.pSignalSemaphores = &submit_semaphores[image_index].handle();
vk::QueueSubmit(m_default_queue->handle(), 1, &submit_info, frame_fence);
VkPresentInfoKHR present = vku::InitStructHelper();
present.waitSemaphoreCount = 1;
present.pWaitSemaphores = &submit_semaphores[image_index].handle();
present.swapchainCount = 2;
present.pSwapchains = swapchain_handles;
present.pImageIndices = image_indices;
VkResult present_result = vk::QueuePresentKHR(*m_default_queue, &present);
if (present_result != VK_ERROR_OUT_OF_DATE_KHR) {
cleanup_resources();
GTEST_SKIP() << "Cannot generate VK_ERROR_OUT_OF_DATE_KHR state required for this test";
}
}
// All other frames.
for (uint32_t i = 0; i < 5; i++) {
frame_fence.Wait(kWaitTimeout);
frame_fence.Reset();
// The test checks that image acquire from the first swapchain does not generate validation error that no images left.
// The second swapchain should not affect acquired image tracking in the first swapchain.
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, vvl::kU64Max);
// Do not try to acquire images from the second swapchain, it is broken.
// image_index presentation should succeed, image_index2 should fail.
const uint32_t image_indices[2] = {image_index, image_index2};
m_default_queue->Submit(vkt::no_cmd, vkt::Wait(acquire_semaphore), vkt::Signal(submit_semaphores[image_index]), frame_fence);
VkPresentInfoKHR present = vku::InitStructHelper();
present.waitSemaphoreCount = 1;
present.pWaitSemaphores = &submit_semaphores[image_index].handle();
present.swapchainCount = 2;
present.pSwapchains = swapchain_handles;
present.pImageIndices = image_indices;
vk::QueuePresentKHR(*m_default_queue, &present);
}
cleanup_resources();
}
TEST_F(PositiveWsi, MixKHRAndKHR2SurfaceCapsQueries) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8772
TEST_DESCRIPTION("Mixing KHR and KHR2 surface queries should not break VVL surface caps caching");
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
AddSurfaceExtension();
RETURN_IF_SKIP(SupportSurfaceResize());
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
// KHR2 query with present mode
VkSurfacePresentModeEXT surface_present_mode = vku::InitStructHelper();
surface_present_mode.presentMode = VK_PRESENT_MODE_FIFO_KHR;
VkPhysicalDeviceSurfaceInfo2KHR surface_info = vku::InitStructHelper(&surface_present_mode);
surface_info.surface = m_surface.Handle();
VkSurfaceCapabilities2KHR surface_caps2 = vku::InitStructHelper();
vk::GetPhysicalDeviceSurfaceCapabilities2KHR(m_device->Physical(), &surface_info, &surface_caps2);
// Resize
m_surface_context.Resize(m_surface_capabilities.currentExtent.width + 25, m_surface_capabilities.currentExtent.height);
// KHR query
VkSurfaceCapabilitiesKHR surface_caps;
vk::GetPhysicalDeviceSurfaceCapabilitiesKHR(Gpu(), m_surface.Handle(), &surface_caps);
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = surface_caps.minImageCount;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = surface_caps.maxImageExtent;
swapchain_ci.imageArrayLayers = 1;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = m_surface_non_shared_present_mode;
vkt::Swapchain swapchain(*m_device, swapchain_ci);
}
TEST_F(PositiveWsi, MixKHRAndKHR2SurfaceCapsQueries2) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8772
TEST_DESCRIPTION("Mixing KHR and KHR2 surface queries should not break VVL surface caps caching");
AddRequiredExtensions(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
AddSurfaceExtension();
RETURN_IF_SKIP(SupportSurfaceResize());
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
// KHR query
VkSurfaceCapabilitiesKHR surface_caps;
vk::GetPhysicalDeviceSurfaceCapabilitiesKHR(Gpu(), m_surface.Handle(), &surface_caps);
// Resize
m_surface_context.Resize(m_surface_capabilities.currentExtent.width + 25, m_surface_capabilities.currentExtent.height);
// KHR2 query with present mode
VkSurfacePresentModeEXT surface_present_mode = vku::InitStructHelper();
surface_present_mode.presentMode = VK_PRESENT_MODE_FIFO_KHR;
VkPhysicalDeviceSurfaceInfo2KHR surface_info = vku::InitStructHelper(&surface_present_mode);
surface_info.surface = m_surface.Handle();
VkSurfaceCapabilities2KHR surface_caps2 = vku::InitStructHelper();
vk::GetPhysicalDeviceSurfaceCapabilities2KHR(m_device->Physical(), &surface_info, &surface_caps2);
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = surface_caps2.surfaceCapabilities.minImageCount;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = surface_caps2.surfaceCapabilities.maxImageExtent;
swapchain_ci.imageArrayLayers = 1;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = m_surface_non_shared_present_mode;
vkt::Swapchain swapchain(*m_device, swapchain_ci);
}
TEST_F(PositiveWsi, CreateSwapchainImagesWithConcurrentSharingMode) {
TEST_DESCRIPTION("Create images from swapchain with concurrent sharing mode");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
if (!m_second_queue) {
GTEST_SKIP() << "Two queues are needed to run this test";
}
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
uint32_t queue_family_indices[] = {m_default_queue->family_index, m_second_queue->family_index};
VkSurfaceCapabilitiesKHR surface_caps;
vk::GetPhysicalDeviceSurfaceCapabilitiesKHR(Gpu(), m_surface.Handle(), &surface_caps);
VkImageFormatProperties img_format_props;
vk::GetPhysicalDeviceImageFormatProperties(Gpu(), m_surface_formats[0].format, VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, 0, &img_format_props);
VkExtent2D img_ext = {std::min(m_surface_capabilities.maxImageExtent.width, img_format_props.maxExtent.width),
std::min(m_surface_capabilities.maxImageExtent.height, img_format_props.maxExtent.height)};
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = surface_caps.minImageCount;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = img_ext;
swapchain_ci.imageArrayLayers = 1u;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
swapchain_ci.queueFamilyIndexCount = 2u;
swapchain_ci.pQueueFamilyIndices = queue_family_indices;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = m_surface_non_shared_present_mode;
vkt::Swapchain swapchain(*m_device, swapchain_ci);
VkImageSwapchainCreateInfoKHR image_swapchain_ci = vku::InitStructHelper();
image_swapchain_ci.swapchain = swapchain.handle();
VkImageCreateInfo image_create_info = vku::InitStructHelper(&image_swapchain_ci);
image_create_info.imageType = VK_IMAGE_TYPE_2D;
image_create_info.format = swapchain_ci.imageFormat;
image_create_info.extent.width = swapchain_ci.imageExtent.width;
image_create_info.extent.height = swapchain_ci.imageExtent.height;
image_create_info.extent.depth = 1u;
image_create_info.mipLevels = 1u;
image_create_info.arrayLayers = 1u;
image_create_info.samples = VK_SAMPLE_COUNT_1_BIT;
image_create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
image_create_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
image_create_info.sharingMode = VK_SHARING_MODE_CONCURRENT;
image_create_info.queueFamilyIndexCount = 2u;
image_create_info.pQueueFamilyIndices = queue_family_indices;
vkt::Image image(*m_device, image_create_info, vkt::no_mem);
}
TEST_F(PositiveWsi, CreateSwapchainImagesWithExclusiveSharingMode) {
TEST_DESCRIPTION("Create images from swapchain with exclusive sharing mode");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
if (!m_second_queue) {
GTEST_SKIP() << "Two queues are needed to run this test";
}
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
uint32_t queue_family_indices[] = {m_default_queue->family_index, m_second_queue->family_index};
VkSurfaceCapabilitiesKHR surface_caps;
vk::GetPhysicalDeviceSurfaceCapabilitiesKHR(Gpu(), m_surface.Handle(), &surface_caps);
VkImageFormatProperties img_format_props;
vk::GetPhysicalDeviceImageFormatProperties(Gpu(), m_surface_formats[0].format, VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, 0, &img_format_props);
VkExtent2D img_ext = {std::min(m_surface_capabilities.maxImageExtent.width, img_format_props.maxExtent.width),
std::min(m_surface_capabilities.maxImageExtent.height, img_format_props.maxExtent.height)};
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = surface_caps.minImageCount;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = img_ext;
swapchain_ci.imageArrayLayers = 1u;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_ci.queueFamilyIndexCount = 1u;
swapchain_ci.pQueueFamilyIndices = queue_family_indices;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = m_surface_non_shared_present_mode;
vkt::Swapchain swapchain(*m_device, swapchain_ci);
VkImageSwapchainCreateInfoKHR image_swapchain_ci = vku::InitStructHelper();
image_swapchain_ci.swapchain = swapchain.handle();
VkImageCreateInfo image_create_info = vku::InitStructHelper(&image_swapchain_ci);
image_create_info.imageType = VK_IMAGE_TYPE_2D;
image_create_info.format = swapchain_ci.imageFormat;
image_create_info.extent.width = swapchain_ci.imageExtent.width;
image_create_info.extent.height = swapchain_ci.imageExtent.height;
image_create_info.extent.depth = 1u;
image_create_info.mipLevels = 1u;
image_create_info.arrayLayers = 1u;
image_create_info.samples = VK_SAMPLE_COUNT_1_BIT;
image_create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
image_create_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
image_create_info.queueFamilyIndexCount = 1u;
image_create_info.pQueueFamilyIndices = queue_family_indices;
vkt::Image image(*m_device, image_create_info, vkt::no_mem);
}
TEST_F(PositiveWsi, CreateSwapchainWithOldSwapchain) {
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
VkSurfaceCapabilitiesKHR surface_caps;
vk::GetPhysicalDeviceSurfaceCapabilitiesKHR(Gpu(), m_surface.Handle(), &surface_caps);
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = surface_caps.minImageCount;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = surface_caps.minImageExtent;
swapchain_ci.imageArrayLayers = 1u;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = m_surface_non_shared_present_mode;
vkt::Swapchain swapchain1(*m_device, swapchain_ci);
swapchain_ci.oldSwapchain = swapchain1;
vkt::Swapchain swapchain2(*m_device, swapchain_ci);
}
TEST_F(PositiveWsi, OldSwapchainFromAnotherSurface) {
TEST_DESCRIPTION("https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/10112");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
if (IsPlatformMockICD()) {
GTEST_SKIP() << "Will leak in ANSN because headless machine uses xvfb, but can only handle a single surface";
}
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
VkSurfaceCapabilitiesKHR surface_caps;
vk::GetPhysicalDeviceSurfaceCapabilitiesKHR(Gpu(), m_surface.Handle(), &surface_caps);
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = surface_caps.minImageCount;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = surface_caps.minImageExtent;
swapchain_ci.imageArrayLayers = 1u;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = m_surface_non_shared_present_mode;
vkt::Swapchain swapchain1(*m_device, swapchain_ci);
vkt::Surface surface2{};
VkResult result = CreateSurface(m_surface_context, surface2);
if (result != VK_SUCCESS) {
GTEST_SKIP() << "Failed to create surface.";
}
swapchain_ci.oldSwapchain = swapchain1;
swapchain_ci.surface = surface2.Handle();
vkt::Swapchain swapchain2(*m_device, swapchain_ci);
}
TEST_F(PositiveWsi, UseAcquireFenceToDeletePresentSemaphore) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/9587
TEST_DESCRIPTION("Use acquire fence to safely delete present semaphore from previous present operations");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
// Frame 0
vkt::Semaphore acquire_semaphore0(*m_device);
uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore0, kWaitTimeout);
if (image_index != 0) {
GTEST_SKIP() << "test scenario assumes the first acquired image index is 0";
}
vkt::Semaphore present_semaphore0(*m_device);
m_default_queue->Submit(vkt::no_cmd, vkt::Wait(acquire_semaphore0), vkt::Signal(present_semaphore0));
m_default_queue->Present(m_swapchain, image_index, present_semaphore0);
// Frame 1
vkt::Semaphore acquire_semaphore1(*m_device);
image_index = m_swapchain.AcquireNextImage(acquire_semaphore1, kWaitTimeout);
if (image_index != 1) {
m_default_queue->Wait();
GTEST_SKIP() << "test scenario assumes the second acquired image index is 1";
}
vkt::Semaphore present_semaphore1(*m_device);
m_default_queue->Submit(vkt::no_cmd, vkt::Wait(acquire_semaphore1), vkt::Signal(present_semaphore1));
m_default_queue->Present(m_swapchain, image_index, present_semaphore1);
// Frame 2
vkt::Fence acquire_fence2(*m_device);
image_index = m_swapchain.AcquireNextImage(acquire_fence2, kWaitTimeout);
if (image_index != 0) {
m_default_queue->Wait();
GTEST_SKIP() << "test scenario assumes the third acquired image index is 0";
}
acquire_fence2.Wait(kWaitTimeout);
// This test checks that destroying present semaphore from frame 0 does not generate in-use error.
present_semaphore0.destroy();
m_default_queue->Wait();
}
TEST_F(PositiveWsi, ExampleHowToReusePresentSemaphores) {
TEST_DESCRIPTION("Example of how to safely reuse present semaphores by allocating one per swapchain image");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
// Use single fence to wait for every frame (not very effective but it's fine for testing purposes)
vkt::Fence frame_fence(*m_device, VK_FENCE_CREATE_SIGNALED_BIT);
// The acquire semaphore should be indexed by the current frame buffering index (0 in this case, 0/1 for double buffering).
vkt::Semaphore acquire_semaphore(*m_device);
// Present semaphores (signaled by submit and waited on by present) are allocated per swapchain image.
// When a swapchain image is acquired, we know that the previous presentation of this image has finished,
// so the associated semaphore is no longer in use.
//
// IMPORTANT: Present semaphores array should be indexed by the acquired image index.
std::vector<vkt::Semaphore> present_semaphores;
for (size_t i = 0; i < swapchain_images.size(); i++) {
present_semaphores.emplace_back(*m_device);
}
for (uint32_t i = 0; i < 10; i++) {
frame_fence.Wait(kWaitTimeout);
frame_fence.Reset();
uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
m_default_queue->Submit(vkt::no_cmd, vkt::Wait(acquire_semaphore), vkt::Signal(present_semaphores[image_index]),
frame_fence);
m_default_queue->Present(m_swapchain, image_index, present_semaphores[image_index]);
}
m_default_queue->Wait();
}
TEST_F(PositiveWsi, ExampleHowToReusePresentSemaphores2) {
TEST_DESCRIPTION("Example of how to safely reuse present semaphores by using presentation fence");
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
vkt::CommandBuffer command_buffers[2] = {vkt::CommandBuffer{*m_device, m_command_pool},
vkt::CommandBuffer(*m_device, m_command_pool)};
// The acquire semaphores should be indexed by the current frame buffering index.
vkt::Semaphore acquire_semaphores[2] = {*m_device, *m_device};
// The present semaphores can also be indexed by the current frame index if we use presentation fence
// and associate presentation fence with each buffered frame.
vkt::Semaphore present_semaphores[2] = {*m_device, *m_device};
vkt::Fence present_fences[2] = {{*m_device, VK_FENCE_CREATE_SIGNALED_BIT}, {*m_device, VK_FENCE_CREATE_SIGNALED_BIT}};
int frame_index = 0;
for (uint32_t i = 0; i < 10; i++) {
vkt::Fence &present_fence = present_fences[frame_index];
vkt::CommandBuffer &command_buffer = command_buffers[frame_index];
vkt::Semaphore &acquire_semaphore = acquire_semaphores[frame_index];
vkt::Semaphore &present_semaphore = present_semaphores[frame_index];
present_fence.Wait(kWaitTimeout);
present_fence.Reset();
command_buffer.Begin();
command_buffer.End();
uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
m_default_queue->Submit(command_buffer, vkt::Wait(acquire_semaphore), vkt::Signal(present_semaphore));
VkSwapchainPresentFenceInfoEXT present_fence_info = vku::InitStructHelper();
present_fence_info.swapchainCount = 1;
present_fence_info.pFences = &present_fence.handle();
m_default_queue->Present(m_swapchain, image_index, present_semaphore, &present_fence_info);
frame_index = 1 - frame_index; // 0 or 1
}
m_default_queue->Wait();
}
TEST_F(PositiveWsi, SignalPresentSemaphoreAfterFenceWait) {
TEST_DESCRIPTION("Signal present wait semaphore after waiting on the presentation fence");
AddSurfaceExtension();
AddRequiredExtensions(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::swapchainMaintenance1);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
vkt::Semaphore acquire_semaphore(*m_device);
uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
vkt::Fence present_fence(*m_device);
VkSwapchainPresentFenceInfoEXT present_fence_info = vku::InitStructHelper();
present_fence_info.swapchainCount = 1;
present_fence_info.pFences = &present_fence.handle();
vkt::Semaphore present_semaphore(*m_device);
m_default_queue->Submit(vkt::no_cmd, vkt::Wait(acquire_semaphore), vkt::Signal(present_semaphore));
m_default_queue->Present(m_swapchain, image_index, present_semaphore, &present_fence_info);
// Test that after waiting on the present fence it's safe to signal present semaphore again
vk::WaitForFences(device(), 1, &present_fence.handle(), VK_TRUE, kWaitTimeout);
m_default_queue->Submit(vkt::no_cmd, vkt::Signal(present_semaphore));
m_default_queue->Wait();
}
TEST_F(PositiveWsi, SignalPresentSemaphoreAfterQueueWait) {
TEST_DESCRIPTION("Signal present wait semaphore after waiting on device queue. Only works for pre-swapchain-maintenance1");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
vkt::Semaphore acquire_semaphore(*m_device);
uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
vkt::Semaphore present_semaphore(*m_device);
m_default_queue->Submit(vkt::no_cmd, vkt::Wait(acquire_semaphore), vkt::Signal(present_semaphore));
m_default_queue->Present(m_swapchain, image_index, present_semaphore);
m_default_queue->Wait();
m_default_queue->Submit(vkt::no_cmd, vkt::Signal(present_semaphore));
m_default_queue->Wait();
}
TEST_F(PositiveWsi, GetDeviceGroupSurfacePresentModes) {
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
VkDeviceGroupPresentModeFlagsKHR present_mode_flags;
vk::GetDeviceGroupSurfacePresentModesKHR(*m_device, m_surface.Handle(), &present_mode_flags);
}
TEST_F(PositiveWsi, ProgressOnPresentOnlyQueue) {
TEST_DESCRIPTION("Enqueue presentation requests on the dedicated queue");
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSwapchain());
if (!m_second_queue) {
GTEST_SKIP() << "Two queues are needed to run this test";
}
VkBool32 supported;
vk::GetPhysicalDeviceSurfaceSupportKHR(Gpu(), m_second_queue->family_index, m_surface.Handle(), &supported);
if (!supported) {
GTEST_SKIP() << "The second queue does not support present";
}
const auto swapchain_images = m_swapchain.GetImages();
for (auto image : swapchain_images) {
SetPresentImageLayout(image);
}
std::vector<vkt::Semaphore> present_wait_semaphores;
for (size_t i = 0; i < swapchain_images.size(); i++) {
present_wait_semaphores.emplace_back(*m_device);
}
vkt::Fence frame_fences[2] = {{*m_device, VK_FENCE_CREATE_SIGNALED_BIT}, {*m_device, VK_FENCE_CREATE_SIGNALED_BIT}};
vkt::Semaphore acquire_semaphores[2] = {*m_device, *m_device};
vkt::CommandBuffer command_buffers[2] = {vkt::CommandBuffer{*m_device, m_command_pool},
vkt::CommandBuffer{*m_device, m_command_pool}};
uint32_t frame_index = 0;
// NOTE: This test can be used for manual inspection of memory usage.
// Increase frame count and observe that the test does not continuously allocate memory.
const int frame_count = 100;
for (int i = 0; i < frame_count; i++) {
const vkt::Fence &frame_fence = frame_fences[frame_index];
const vkt::Semaphore &acquire_semaphore = acquire_semaphores[frame_index];
vkt::CommandBuffer &command_buffer = command_buffers[frame_index];
frame_fence.Wait(kWaitTimeout);
frame_fence.Reset();
const uint32_t image_index = m_swapchain.AcquireNextImage(acquire_semaphore, kWaitTimeout);
const vkt::Semaphore &present_wait_semaphore = present_wait_semaphores[image_index];
command_buffer.Begin();
command_buffer.End();
m_default_queue->Submit(command_buffer, vkt::Wait(acquire_semaphore), vkt::Signal(present_wait_semaphore), frame_fence);
m_second_queue->Present(m_swapchain, image_index, present_wait_semaphore);
frame_index = 1 - frame_index; // 0 or 1
}
m_default_queue->Wait();
m_second_queue->Wait();
}
TEST_F(PositiveWsi, SharedPresentAndPresentSemaphoreReuse) {
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/10201
TEST_DESCRIPTION("Present semaphore in-use check is disabled when shared present mode is used without swapchain maintenance1");
AddRequiredExtensions(VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME);
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
bool found = false;
for (VkPresentModeKHR present_mode : m_surface_present_modes) {
found |= (present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
}
if (!found) {
GTEST_SKIP() << "Cannot find shared present mode";
}
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = 1;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = m_surface_capabilities.minImageExtent;
swapchain_ci.imageArrayLayers = 1;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; // implementations must support
swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR;
vkt::Swapchain swapchain(*m_device, swapchain_ci);
const auto images = swapchain.GetImages();
vkt::Fence fence(*m_device);
const uint32_t image_index = swapchain.AcquireNextImage(fence, kWaitTimeout);
fence.Wait(kWaitTimeout);
SetPresentImageLayout(images[image_index]);
vkt::Semaphore semaphore(*m_device);
m_default_queue->Submit(vkt::no_cmd, vkt::Signal(semaphore));
m_default_queue->Present(swapchain, image_index, semaphore);
// For poor apps without swapchain_maintenance1 that use shared present modes
// and call AcquireNextImage only once, the present semaphore in-use check is disabled.
// The app doesn't have an official ways to do this better.
// If supported, swapchain_maintenance1 should be used in such scenario.
m_default_queue->Submit(vkt::no_cmd, vkt::Signal(semaphore));
m_default_queue->Present(swapchain, image_index, semaphore);
m_default_queue->Wait();
}
TEST_F(PositiveWsi, SharedPresentReuseSemaphoreAfterDestroy) {
TEST_DESCRIPTION("After swapchain with shared present mode is destroyed the present semaphore can be reused");
AddRequiredExtensions(VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME);
AddSurfaceExtension();
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
InitSwapchainInfo();
bool found = false;
for (VkPresentModeKHR present_mode : m_surface_present_modes) {
found |= (present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
}
if (!found) {
GTEST_SKIP() << "Cannot find shared present mode";
}
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = 1;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = m_surface_capabilities.minImageExtent;
swapchain_ci.imageArrayLayers = 1;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; // implementations must support
swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR;
vkt::Swapchain swapchain(*m_device, swapchain_ci);
const auto images = swapchain.GetImages();
for (auto image : images) {
SetPresentImageLayout(image);
}
vkt::Fence fence(*m_device);
const uint32_t image_index = swapchain.AcquireNextImage(fence, kWaitTimeout);
fence.Wait(kWaitTimeout);
fence.Reset();
vkt::Semaphore semaphore(*m_device);
vkt::Semaphore semaphore2(*m_device);
m_default_queue->Submit(vkt::no_cmd, vkt::Signal(semaphore));
m_default_queue->Present(swapchain, image_index, semaphore);
m_default_queue->Submit(vkt::no_cmd, vkt::Signal(semaphore2));
m_default_queue->Present(swapchain, image_index, semaphore2);
swapchain_ci.minImageCount = m_surface_capabilities.minImageCount;
swapchain_ci.presentMode = VK_PRESENT_MODE_FIFO_KHR;
swapchain_ci.oldSwapchain = swapchain;
vkt::Swapchain swapchain2(*m_device, swapchain_ci);
const auto images2 = swapchain2.GetImages();
const uint32_t image_index2 = swapchain2.AcquireNextImage(fence, kWaitTimeout);
fence.Wait(kWaitTimeout);
fence.Reset();
// Destroy swapchain!
swapchain.destroy();
// Transition layout manually, because SetPresentImageLayout calls QueueWaitIdle which
// resets semaphore swapchain state and this is not what we want for this test.
VkImageMemoryBarrier present_transition = vku::InitStructHelper();
present_transition.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
present_transition.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
present_transition.image = images2[image_index2];
present_transition.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
m_command_buffer.Begin();
vk::CmdPipelineBarrier(m_command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr,
0, nullptr, 1, &present_transition);
m_command_buffer.End();
// Test that semaphore does not assume it is still in use by swapchain that was just deleted.
// Swapchain2 is created with FIFO mode in order to enable semaphore-in-use-by-swapchain check
// (it is disabled for shared present modes)
m_default_queue->Submit(m_command_buffer, vkt::Signal(semaphore));
m_default_queue->Present(swapchain2, image_index2, semaphore);
m_default_queue->Wait();
}
TEST_F(PositiveWsi, PresentIdWait2) {
SetTargetApiVersion(VK_API_VERSION_1_1);
AddSurfaceExtension();
AddRequiredExtensions(VK_KHR_PRESENT_ID_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_PRESENT_WAIT_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::presentId2);
AddRequiredFeature(vkt::Feature::presentWait2);
RETURN_IF_SKIP(Init());
RETURN_IF_SKIP(InitSurface());
RETURN_IF_SKIP(InitSwapchainInfo());
VkSwapchainCreateInfoKHR swapchain_ci = vku::InitStructHelper();
swapchain_ci.flags = VK_SWAPCHAIN_CREATE_PRESENT_ID_2_BIT_KHR | VK_SWAPCHAIN_CREATE_PRESENT_WAIT_2_BIT_KHR;
swapchain_ci.surface = m_surface.Handle();
swapchain_ci.minImageCount = m_surface_capabilities.minImageCount;
swapchain_ci.imageFormat = m_surface_formats[0].format;
swapchain_ci.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_ci.imageExtent = m_surface_capabilities.minImageExtent;
swapchain_ci.imageArrayLayers = 1u;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_ci.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_ci.compositeAlpha = m_surface_composite_alpha;
swapchain_ci.presentMode = m_surface_non_shared_present_mode;
swapchain_ci.clipped = VK_FALSE;
swapchain_ci.oldSwapchain = VK_NULL_HANDLE;
vkt::Swapchain swapchain(*m_device, swapchain_ci);
VkSurfaceCapabilitiesPresentWait2KHR present_wait_2_capabilities = vku::InitStructHelper();
VkSurfaceCapabilitiesPresentId2KHR present_id_2_capabilities = vku::InitStructHelper(&present_wait_2_capabilities);
VkSurfaceCapabilities2KHR capabilities2 = vku::InitStructHelper(&present_id_2_capabilities);
VkPhysicalDeviceSurfaceInfo2KHR surface_info = vku::InitStructHelper();
surface_info.surface = m_surface.Handle();
vk::GetPhysicalDeviceSurfaceCapabilities2KHR(gpu_, &surface_info, &capabilities2);
if (!present_id_2_capabilities.presentId2Supported || !present_wait_2_capabilities.presentWait2Supported) {
GTEST_SKIP() << "presentId2 and presentWait2 are not supported for the surface";
}
const auto images = swapchain.GetImages();
uint64_t present_id_value = 1u;
// TODO - understand why this timeout if set the loop to a large value
for (uint32_t i = 0; i < 4; ++i) {
vkt::Fence fence(*m_device);
const uint32_t image_index = swapchain.AcquireNextImage(fence, kWaitTimeout);
vk::WaitForFences(device(), 1, &fence.handle(), true, kWaitTimeout);
SetPresentImageLayout(images[image_index]);
VkPresentId2KHR present_id = vku::InitStructHelper();
present_id.swapchainCount = 1u;
present_id.pPresentIds = &present_id_value;
m_default_queue->Present(swapchain, image_index, vkt::no_semaphore, &present_id);
VkPresentWait2InfoKHR present_wait_2_info = vku::InitStructHelper();
present_wait_2_info.presentId = present_id_value;
present_wait_2_info.timeout = kWaitTimeout;
vk::WaitForPresent2KHR(device(), swapchain, &present_wait_2_info);
++present_id_value;
}
}
|