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
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
use super::{FullScreenExclusive, PresentGravityFlags, PresentScalingFlags, Win32Monitor};
use crate::{
cache::OnceCache,
device::physical::PhysicalDevice,
display::{DisplayMode, DisplayPlaneAlpha},
format::Format,
image::ImageUsage,
instance::{Instance, InstanceExtensions, InstanceOwned},
macros::{impl_id_counter, vulkan_bitflags_enum, vulkan_enum},
DebugWrapper, Requires, RequiresAllOf, RequiresOneOf, Validated, ValidationError, VulkanError,
VulkanObject,
};
#[cfg(any(target_os = "macos", target_os = "ios"))]
use objc::{class, msg_send, runtime::Object, sel, sel_impl};
use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use smallvec::SmallVec;
use std::{
any::Any,
fmt::{Debug, Error as FmtError, Formatter},
mem::MaybeUninit,
num::NonZeroU64,
ptr,
sync::Arc,
};
/// Represents a surface on the screen.
///
/// Creating a `Surface` is platform-specific.
pub struct Surface {
handle: ash::vk::SurfaceKHR,
instance: DebugWrapper<Arc<Instance>>,
id: NonZeroU64,
api: SurfaceApi,
object: Option<Arc<dyn Any + Send + Sync>>,
// FIXME: This field is never set.
#[cfg(target_os = "ios")]
metal_layer: IOSMetalLayer,
// Data queried by the user at runtime, cached for faster lookups.
// This is stored here rather than on `PhysicalDevice` to ensure that it's freed when the
// `Surface` is destroyed.
pub(crate) surface_formats:
OnceCache<(ash::vk::PhysicalDevice, SurfaceInfo), Vec<(Format, ColorSpace)>>,
pub(crate) surface_present_modes:
OnceCache<(ash::vk::PhysicalDevice, SurfaceInfo), Vec<PresentMode>>,
pub(crate) surface_support: OnceCache<(ash::vk::PhysicalDevice, u32), bool>,
}
impl Surface {
/// Returns the instance extensions required to create a surface from a window of the given
/// event loop.
pub fn required_extensions(event_loop: &impl HasRawDisplayHandle) -> InstanceExtensions {
let mut extensions = InstanceExtensions {
khr_surface: true,
..InstanceExtensions::empty()
};
match event_loop.raw_display_handle() {
RawDisplayHandle::Android(_) => extensions.khr_android_surface = true,
// FIXME: `mvk_macos_surface` and `mvk_ios_surface` are deprecated.
RawDisplayHandle::AppKit(_) => extensions.mvk_macos_surface = true,
RawDisplayHandle::UiKit(_) => extensions.mvk_ios_surface = true,
RawDisplayHandle::Windows(_) => extensions.khr_win32_surface = true,
RawDisplayHandle::Wayland(_) => extensions.khr_wayland_surface = true,
RawDisplayHandle::Xcb(_) => extensions.khr_xcb_surface = true,
RawDisplayHandle::Xlib(_) => extensions.khr_xlib_surface = true,
_ => unimplemented!(),
}
extensions
}
/// Creates a new `Surface` from the given `window`.
pub fn from_window(
instance: Arc<Instance>,
window: Arc<impl HasRawWindowHandle + HasRawDisplayHandle + Any + Send + Sync>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
let mut surface = unsafe { Self::from_window_ref(instance, &*window) }?;
Arc::get_mut(&mut surface).unwrap().object = Some(window);
Ok(surface)
}
/// Creates a new `Surface` from the given `window` without ensuring that the window outlives
/// the surface.
///
/// # Safety
///
/// - The given `window` must outlive the created surface.
pub unsafe fn from_window_ref(
instance: Arc<Instance>,
window: &(impl HasRawWindowHandle + HasRawDisplayHandle),
) -> Result<Arc<Self>, Validated<VulkanError>> {
match (window.raw_window_handle(), window.raw_display_handle()) {
(RawWindowHandle::AndroidNdk(window), RawDisplayHandle::Android(_display)) => {
Self::from_android(instance, window.a_native_window, None)
}
#[cfg(target_os = "macos")]
(RawWindowHandle::AppKit(window), RawDisplayHandle::AppKit(_display)) => {
// Ensure the layer is `CAMetalLayer`.
let layer = get_metal_layer_macos(window.ns_view);
Self::from_mac_os(instance, layer as *const (), None)
}
#[cfg(target_os = "ios")]
(RawWindowHandle::UiKit(window), RawDisplayHandle::UiKit(_display)) => {
// Ensure the layer is `CAMetalLayer`.
let layer = get_metal_layer_ios(window.ui_view);
Self::from_ios(instance, layer, None)
}
(RawWindowHandle::Wayland(window), RawDisplayHandle::Wayland(display)) => {
Self::from_wayland(instance, display.display, window.surface, None)
}
(RawWindowHandle::Win32(window), RawDisplayHandle::Windows(_display)) => {
Self::from_win32(instance, window.hinstance, window.hwnd, None)
}
(RawWindowHandle::Xcb(window), RawDisplayHandle::Xcb(display)) => {
Self::from_xcb(instance, display.connection, window.window, None)
}
(RawWindowHandle::Xlib(window), RawDisplayHandle::Xlib(display)) => {
Self::from_xlib(instance, display.display, window.window, None)
}
_ => unimplemented!(
"the window was created with a windowing API that is not supported \
by Vulkan/Vulkano"
),
}
}
/// Creates a `Surface` from a raw handle.
///
/// # Safety
///
/// - `handle` must be a valid Vulkan object handle created from `instance`.
/// - `handle` must have been created using the function specified by `api`.
/// - The window object that `handle` was created from must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_handle(
instance: Arc<Instance>,
handle: ash::vk::SurfaceKHR,
api: SurfaceApi,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Self {
Surface {
handle,
instance: DebugWrapper(instance),
id: Self::next_id(),
api,
object,
#[cfg(target_os = "ios")]
metal_layer: IOSMetalLayer::new(std::ptr::null_mut(), std::ptr::null_mut()),
surface_formats: OnceCache::new(),
surface_present_modes: OnceCache::new(),
surface_support: OnceCache::new(),
}
}
/// Creates a `Surface` with no backing window or display.
///
/// Presenting to a headless surface does nothing, so this is mostly useless in itself. However,
/// it may be useful for testing, and it is available for future extensions to layer on top of.
pub fn headless(
instance: Arc<Instance>,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_headless(&instance)?;
unsafe { Ok(Self::headless_unchecked(instance, object)?) }
}
fn validate_headless(instance: &Instance) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().ext_headless_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"ext_headless_surface",
)])]),
..Default::default()
}));
}
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn headless_unchecked(
instance: Arc<Instance>,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::HeadlessSurfaceCreateInfoEXT {
flags: ash::vk::HeadlessSurfaceCreateFlagsEXT::empty(),
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.ext_headless_surface.create_headless_surface_ext)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Headless,
object,
)))
}
/// Creates a `Surface` from a `DisplayMode` and display plane.
#[inline]
pub fn from_display_plane(
display_mode: Arc<DisplayMode>,
create_info: DisplaySurfaceCreateInfo,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_display_plane(&display_mode, &create_info)?;
unsafe {
Ok(Self::from_display_plane_unchecked(
display_mode,
create_info,
)?)
}
}
fn validate_from_display_plane(
display_mode: &DisplayMode,
create_info: &DisplaySurfaceCreateInfo,
) -> Result<(), Box<ValidationError>> {
if !display_mode.instance().enabled_extensions().khr_display {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"khr_display",
)])]),
..Default::default()
}));
}
// VUID-vkCreateDisplayPlaneSurfaceKHR-pCreateInfo-parameter
create_info
.validate(display_mode.display().physical_device())
.map_err(|err| err.add_context("create_info"))?;
let &DisplaySurfaceCreateInfo {
plane_index,
plane_stack_index,
transform,
alpha_mode,
global_alpha: _,
image_extent: _,
_ne: _,
} = create_info;
let display_plane_properties_raw = unsafe {
display_mode
.display()
.physical_device()
.display_plane_properties_raw()
.map_err(|_err| {
Box::new(ValidationError {
problem: "`PhysicalDevice::display_plane_properties` \
returned an error"
.into(),
..Default::default()
})
})?
};
if display_mode.display().plane_reorder_possible() {
if plane_stack_index as usize >= display_plane_properties_raw.len() {
return Err(Box::new(ValidationError {
problem: "`plane_stack_index` is not less than the number of display planes \
on the physical device"
.into(),
vuids: &["VUID-VkDisplaySurfaceCreateInfoKHR-planeReorderPossible-01253"],
..Default::default()
}));
}
} else {
if plane_stack_index
!= display_plane_properties_raw[plane_index as usize].current_stack_index
{
return Err(Box::new(ValidationError {
problem: "`display_mode.display().plane_reorder_possible()` is `false`, and \
`plane_stack_index` does not equal the `current_stack_index` value of the \
display plane properties"
.into(),
vuids: &["VUID-VkDisplaySurfaceCreateInfoKHR-planeReorderPossible-01253"],
..Default::default()
}));
}
}
let display_plane_capabilities = unsafe {
display_mode
.display_plane_capabilities_unchecked(plane_index)
.map_err(|_err| {
Box::new(ValidationError {
problem: "`DisplayMode::display_plane_capabilities` \
returned an error"
.into(),
..Default::default()
})
})?
};
if !display_plane_capabilities
.supported_alpha
.contains_enum(alpha_mode)
{
return Err(Box::new(ValidationError {
problem: "the `supported_alpha` value of the display plane capabilities of \
`display_mode` for the provided plane index does not contain \
`create_info.alpha_mode`"
.into(),
vuids: &["VUID-VkDisplaySurfaceCreateInfoKHR-alphaMode-01255"],
..Default::default()
}));
}
if !display_mode
.display()
.supported_transforms()
.contains_enum(transform)
{
return Err(Box::new(ValidationError {
problem: "`display_mode.display().supported_transforms()` does not contain \
`create_info.transform`"
.into(),
vuids: &["VUID-VkDisplaySurfaceCreateInfoKHR-transform-06740"],
..Default::default()
}));
}
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_display_plane_unchecked(
display_mode: Arc<DisplayMode>,
create_info: DisplaySurfaceCreateInfo,
) -> Result<Arc<Self>, VulkanError> {
let &DisplaySurfaceCreateInfo {
plane_index,
plane_stack_index,
transform,
alpha_mode,
global_alpha,
image_extent,
_ne: _,
} = &create_info;
let create_info_vk = ash::vk::DisplaySurfaceCreateInfoKHR {
flags: ash::vk::DisplaySurfaceCreateFlagsKHR::empty(),
display_mode: display_mode.handle(),
plane_index,
plane_stack_index,
transform: transform.into(),
alpha_mode: alpha_mode.into(),
global_alpha,
image_extent: ash::vk::Extent2D {
width: image_extent[0],
height: image_extent[1],
},
..Default::default()
};
let instance = display_mode.instance();
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.khr_display.create_display_plane_surface_khr)(
instance.handle(),
&create_info_vk,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance.clone(),
handle,
SurfaceApi::DisplayPlane,
None,
)))
}
/// Creates a `Surface` from an Android window.
///
/// # Safety
///
/// - `window` must be a valid Android `ANativeWindow` handle.
/// - The object referred to by `window` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_android<W>(
instance: Arc<Instance>,
window: *const W,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_android(&instance, window)?;
Ok(Self::from_android_unchecked(instance, window, object)?)
}
fn validate_from_android<W>(
instance: &Instance,
_window: *const W,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().khr_android_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"khr_android_surface",
)])]),
..Default::default()
}));
}
// VUID-VkAndroidSurfaceCreateInfoKHR-window-01248
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_android_unchecked<W>(
instance: Arc<Instance>,
window: *const W,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::AndroidSurfaceCreateInfoKHR {
flags: ash::vk::AndroidSurfaceCreateFlagsKHR::empty(),
window: window as *mut _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.khr_android_surface.create_android_surface_khr)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Android,
object,
)))
}
/// Creates a `Surface` from a DirectFB surface.
///
/// # Safety
///
/// - `dfb` must be a valid DirectFB `IDirectFB` handle.
/// - `surface` must be a valid DirectFB `IDirectFBSurface` handle.
/// - The object referred to by `dfb` and `surface` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_directfb<D, S>(
instance: Arc<Instance>,
dfb: *const D,
surface: *const S,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_directfb(&instance, dfb, surface)?;
Ok(Self::from_directfb_unchecked(
instance, dfb, surface, object,
)?)
}
fn validate_from_directfb<D, S>(
instance: &Instance,
_dfb: *const D,
_surface: *const S,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().ext_directfb_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"ext_directfb_surface",
)])]),
..Default::default()
}));
}
// VUID-VkDirectFBSurfaceCreateInfoEXT-dfb-04117
// Can't validate, therefore unsafe
// VUID-VkDirectFBSurfaceCreateInfoEXT-surface-04118
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_directfb_unchecked<D, S>(
instance: Arc<Instance>,
dfb: *const D,
surface: *const S,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::DirectFBSurfaceCreateInfoEXT {
flags: ash::vk::DirectFBSurfaceCreateFlagsEXT::empty(),
dfb: dfb as *mut _,
surface: surface as *mut _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.ext_directfb_surface.create_direct_fb_surface_ext)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::DirectFB,
object,
)))
}
/// Creates a `Surface` from an Fuchsia ImagePipe.
///
/// # Safety
///
/// - `image_pipe_handle` must be a valid Fuchsia `zx_handle_t` handle.
/// - The object referred to by `image_pipe_handle` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_fuchsia_image_pipe(
instance: Arc<Instance>,
image_pipe_handle: ash::vk::zx_handle_t,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_fuchsia_image_pipe(&instance, image_pipe_handle)?;
Ok(Self::from_fuchsia_image_pipe_unchecked(
instance,
image_pipe_handle,
object,
)?)
}
fn validate_from_fuchsia_image_pipe(
instance: &Instance,
_image_pipe_handle: ash::vk::zx_handle_t,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().fuchsia_imagepipe_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"fuchsia_imagepipe_surface",
)])]),
..Default::default()
}));
}
// VUID-VkImagePipeSurfaceCreateInfoFUCHSIA-imagePipeHandle-04863
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_fuchsia_image_pipe_unchecked(
instance: Arc<Instance>,
image_pipe_handle: ash::vk::zx_handle_t,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::ImagePipeSurfaceCreateInfoFUCHSIA {
flags: ash::vk::ImagePipeSurfaceCreateFlagsFUCHSIA::empty(),
image_pipe_handle,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.fuchsia_imagepipe_surface
.create_image_pipe_surface_fuchsia)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::FuchsiaImagePipe,
object,
)))
}
/// Creates a `Surface` from a Google Games Platform stream descriptor.
///
/// # Safety
///
/// - `stream_descriptor` must be a valid Google Games Platform `GgpStreamDescriptor` handle.
/// - The object referred to by `stream_descriptor` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_ggp_stream_descriptor(
instance: Arc<Instance>,
stream_descriptor: ash::vk::GgpStreamDescriptor,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_ggp_stream_descriptor(&instance, stream_descriptor)?;
Ok(Self::from_ggp_stream_descriptor_unchecked(
instance,
stream_descriptor,
object,
)?)
}
fn validate_from_ggp_stream_descriptor(
instance: &Instance,
_stream_descriptor: ash::vk::GgpStreamDescriptor,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().ggp_stream_descriptor_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"ggp_stream_descriptor_surface",
)])]),
..Default::default()
}));
}
// VUID-VkStreamDescriptorSurfaceCreateInfoGGP-streamDescriptor-02681
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_ggp_stream_descriptor_unchecked(
instance: Arc<Instance>,
stream_descriptor: ash::vk::GgpStreamDescriptor,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::StreamDescriptorSurfaceCreateInfoGGP {
flags: ash::vk::StreamDescriptorSurfaceCreateFlagsGGP::empty(),
stream_descriptor,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.ggp_stream_descriptor_surface
.create_stream_descriptor_surface_ggp)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::GgpStreamDescriptor,
object,
)))
}
/// Creates a `Surface` from an iOS `UIView`.
///
/// # Safety
///
/// - `metal_layer` must be a valid `IOSMetalLayer` handle.
/// - The object referred to by `metal_layer` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
/// - The `UIView` must be backed by a `CALayer` instance of type `CAMetalLayer`.
#[cfg(target_os = "ios")]
pub unsafe fn from_ios(
instance: Arc<Instance>,
metal_layer: IOSMetalLayer,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_ios(&instance, &metal_layer)?;
Ok(Self::from_ios_unchecked(instance, metal_layer, object)?)
}
#[cfg(target_os = "ios")]
fn validate_from_ios(
instance: &Instance,
_metal_layer: &IOSMetalLayer,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().mvk_ios_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"mvk_ios_surface",
)])]),
..Default::default()
}));
}
// VUID-VkIOSSurfaceCreateInfoMVK-pView-04143
// Can't validate, therefore unsafe
// VUID-VkIOSSurfaceCreateInfoMVK-pView-01316
// Can't validate, therefore unsafe
Ok(())
}
#[cfg(target_os = "ios")]
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_ios_unchecked(
instance: Arc<Instance>,
metal_layer: IOSMetalLayer,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::IOSSurfaceCreateInfoMVK {
flags: ash::vk::IOSSurfaceCreateFlagsMVK::empty(),
p_view: metal_layer.render_layer.0 as *const _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.mvk_ios_surface.create_ios_surface_mvk)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Ios,
object,
)))
}
/// Creates a `Surface` from a MacOS `NSView`.
///
/// # Safety
///
/// - `view` must be a valid `CAMetalLayer` or `NSView` handle.
/// - The object referred to by `view` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
/// - The `NSView` must be backed by a `CALayer` instance of type `CAMetalLayer`.
#[cfg(target_os = "macos")]
pub unsafe fn from_mac_os<V>(
instance: Arc<Instance>,
view: *const V,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_mac_os(&instance, view)?;
Ok(Self::from_mac_os_unchecked(instance, view, object)?)
}
#[cfg(target_os = "macos")]
fn validate_from_mac_os<V>(
instance: &Instance,
_view: *const V,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().mvk_macos_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"mvk_macos_surface",
)])]),
..Default::default()
}));
}
// VUID-VkMacOSSurfaceCreateInfoMVK-pView-04144
// Can't validate, therefore unsafe
// VUID-VkMacOSSurfaceCreateInfoMVK-pView-01317
// Can't validate, therefore unsafe
Ok(())
}
#[cfg(target_os = "macos")]
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_mac_os_unchecked<V>(
instance: Arc<Instance>,
view: *const V,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::MacOSSurfaceCreateInfoMVK {
flags: ash::vk::MacOSSurfaceCreateFlagsMVK::empty(),
p_view: view as *const _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.mvk_macos_surface.create_mac_os_surface_mvk)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::MacOs,
object,
)))
}
/// Creates a `Surface` from a Metal `CAMetalLayer`.
///
/// # Safety
///
/// - `layer` must be a valid Metal `CAMetalLayer` handle.
/// - The object referred to by `layer` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_metal<L>(
instance: Arc<Instance>,
layer: *const L,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_metal(&instance, layer)?;
Ok(Self::from_metal_unchecked(instance, layer, object)?)
}
fn validate_from_metal<L>(
instance: &Instance,
_layer: *const L,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().ext_metal_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"ext_metal_surface",
)])]),
..Default::default()
}));
}
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_metal_unchecked<L>(
instance: Arc<Instance>,
layer: *const L,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::MetalSurfaceCreateInfoEXT {
flags: ash::vk::MetalSurfaceCreateFlagsEXT::empty(),
p_layer: layer as *const _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.ext_metal_surface.create_metal_surface_ext)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Metal,
object,
)))
}
/// Creates a `Surface` from a QNX Screen window.
///
/// # Safety
///
/// - `context` must be a valid QNX Screen `_screen_context` handle.
/// - `window` must be a valid QNX Screen `_screen_window` handle.
/// - The object referred to by `window` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_qnx_screen<C, W>(
instance: Arc<Instance>,
context: *const C,
window: *const W,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_qnx_screen(&instance, context, window)?;
Ok(Self::from_qnx_screen_unchecked(
instance, context, window, object,
)?)
}
fn validate_from_qnx_screen<C, W>(
instance: &Instance,
_context: *const C,
_window: *const W,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().qnx_screen_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"qnx_screen_surface",
)])]),
..Default::default()
}));
}
// VUID-VkScreenSurfaceCreateInfoQNX-context-04741
// Can't validate, therefore unsafe
// VUID-VkScreenSurfaceCreateInfoQNX-window-04742
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_qnx_screen_unchecked<C, W>(
instance: Arc<Instance>,
context: *const C,
window: *const W,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::ScreenSurfaceCreateInfoQNX {
flags: ash::vk::ScreenSurfaceCreateFlagsQNX::empty(),
context: context as *mut _,
window: window as *mut _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.qnx_screen_surface.create_screen_surface_qnx)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::QnxScreen,
object,
)))
}
/// Creates a `Surface` from a `code:nn::code:vi::code:Layer`.
///
/// # Safety
///
/// - `window` must be a valid `nn::vi::NativeWindowHandle` handle.
/// - The object referred to by `window` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_vi<W>(
instance: Arc<Instance>,
window: *const W,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_vi(&instance, window)?;
Ok(Self::from_vi_unchecked(instance, window, object)?)
}
fn validate_from_vi<W>(
instance: &Instance,
_window: *const W,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().nn_vi_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"nn_vi_surface",
)])]),
..Default::default()
}));
}
// VUID-VkViSurfaceCreateInfoNN-window-01318
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_vi_unchecked<W>(
instance: Arc<Instance>,
window: *const W,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::ViSurfaceCreateInfoNN {
flags: ash::vk::ViSurfaceCreateFlagsNN::empty(),
window: window as *mut _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.nn_vi_surface.create_vi_surface_nn)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Vi,
object,
)))
}
/// Creates a `Surface` from a Wayland window.
///
/// The window's dimensions will be set to the size of the swapchain.
///
/// # Safety
///
/// - `display` must be a valid Wayland `wl_display` handle.
/// - `surface` must be a valid Wayland `wl_surface` handle.
/// - The objects referred to by `display` and `surface` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_wayland<D, S>(
instance: Arc<Instance>,
display: *const D,
surface: *const S,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_wayland(&instance, display, surface)?;
Ok(Self::from_wayland_unchecked(
instance, display, surface, object,
)?)
}
fn validate_from_wayland<D, S>(
instance: &Instance,
_display: *const D,
_surface: *const S,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().khr_wayland_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"khr_wayland_surface",
)])]),
..Default::default()
}));
}
// VUID-VkWaylandSurfaceCreateInfoKHR-display-01304
// Can't validate, therefore unsafe
// VUID-VkWaylandSurfaceCreateInfoKHR-surface-01305
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_wayland_unchecked<D, S>(
instance: Arc<Instance>,
display: *const D,
surface: *const S,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::WaylandSurfaceCreateInfoKHR {
flags: ash::vk::WaylandSurfaceCreateFlagsKHR::empty(),
display: display as *mut _,
surface: surface as *mut _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.khr_wayland_surface.create_wayland_surface_khr)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Wayland,
object,
)))
}
/// Creates a `Surface` from a Win32 window.
///
/// The surface's min, max and current extent will always match the window's dimensions.
///
/// # Safety
///
/// - `hinstance` must be a valid Win32 `HINSTANCE` handle.
/// - `hwnd` must be a valid Win32 `HWND` handle.
/// - The objects referred to by `hwnd` and `hinstance` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_win32<I, W>(
instance: Arc<Instance>,
hinstance: *const I,
hwnd: *const W,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_win32(&instance, hinstance, hwnd)?;
Ok(Self::from_win32_unchecked(
instance, hinstance, hwnd, object,
)?)
}
fn validate_from_win32<I, W>(
instance: &Instance,
_hinstance: *const I,
_hwnd: *const W,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().khr_win32_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"khr_win32_surface",
)])]),
..Default::default()
}));
}
// VUID-VkWin32SurfaceCreateInfoKHR-hinstance-01307
// Can't validate, therefore unsafe
// VUID-VkWin32SurfaceCreateInfoKHR-hwnd-01308
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_win32_unchecked<I, W>(
instance: Arc<Instance>,
hinstance: *const I,
hwnd: *const W,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::Win32SurfaceCreateInfoKHR {
flags: ash::vk::Win32SurfaceCreateFlagsKHR::empty(),
hinstance: hinstance as *mut _,
hwnd: hwnd as *mut _,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.khr_win32_surface.create_win32_surface_khr)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Win32,
object,
)))
}
/// Creates a `Surface` from an XCB window.
///
/// The surface's min, max and current extent will always match the window's dimensions.
///
/// # Safety
///
/// - `connection` must be a valid X11 `xcb_connection_t` handle.
/// - `window` must be a valid X11 `xcb_window_t` handle.
/// - The objects referred to by `connection` and `window` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_xcb<C>(
instance: Arc<Instance>,
connection: *const C,
window: ash::vk::xcb_window_t,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_xcb(&instance, connection, window)?;
Ok(Self::from_xcb_unchecked(
instance, connection, window, object,
)?)
}
fn validate_from_xcb<C>(
instance: &Instance,
_connection: *const C,
_window: ash::vk::xcb_window_t,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().khr_xcb_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"khr_xcb_surface",
)])]),
..Default::default()
}));
}
// VUID-VkXcbSurfaceCreateInfoKHR-connection-01310
// Can't validate, therefore unsafe
// VUID-VkXcbSurfaceCreateInfoKHR-window-01311
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_xcb_unchecked<C>(
instance: Arc<Instance>,
connection: *const C,
window: ash::vk::xcb_window_t,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::XcbSurfaceCreateInfoKHR {
flags: ash::vk::XcbSurfaceCreateFlagsKHR::empty(),
connection: connection as *mut _,
window,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.khr_xcb_surface.create_xcb_surface_khr)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Xcb,
object,
)))
}
/// Creates a `Surface` from an Xlib window.
///
/// The surface's min, max and current extent will always match the window's dimensions.
///
/// # Safety
///
/// - `display` must be a valid Xlib `Display` handle.
/// - `window` must be a valid Xlib `Window` handle.
/// - The objects referred to by `display` and `window` must outlive the created `Surface`.
/// The `object` parameter can be used to ensure this.
pub unsafe fn from_xlib<D>(
instance: Arc<Instance>,
display: *const D,
window: ash::vk::Window,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, Validated<VulkanError>> {
Self::validate_from_xlib(&instance, display, window)?;
Ok(Self::from_xlib_unchecked(
instance, display, window, object,
)?)
}
fn validate_from_xlib<D>(
instance: &Instance,
_display: *const D,
_window: ash::vk::Window,
) -> Result<(), Box<ValidationError>> {
if !instance.enabled_extensions().khr_xlib_surface {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::InstanceExtension(
"khr_xlib_surface",
)])]),
..Default::default()
}));
}
// VUID-VkXlibSurfaceCreateInfoKHR-dpy-01313
// Can't validate, therefore unsafe
// VUID-VkXlibSurfaceCreateInfoKHR-window-01314
// Can't validate, therefore unsafe
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn from_xlib_unchecked<D>(
instance: Arc<Instance>,
display: *const D,
window: ash::vk::Window,
object: Option<Arc<dyn Any + Send + Sync>>,
) -> Result<Arc<Self>, VulkanError> {
let create_info = ash::vk::XlibSurfaceCreateInfoKHR {
flags: ash::vk::XlibSurfaceCreateFlagsKHR::empty(),
dpy: display as *mut _,
window,
..Default::default()
};
let handle = {
let fns = instance.fns();
let mut output = MaybeUninit::uninit();
(fns.khr_xlib_surface.create_xlib_surface_khr)(
instance.handle(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Arc::new(Self::from_handle(
instance,
handle,
SurfaceApi::Xlib,
object,
)))
}
/// Returns the instance this surface was created with.
#[inline]
pub fn instance(&self) -> &Arc<Instance> {
&self.instance
}
/// Returns the windowing API that was used to construct the surface.
#[inline]
pub fn api(&self) -> SurfaceApi {
self.api
}
/// Returns a reference to the `object` parameter that was passed when creating the
/// surface.
#[inline]
pub fn object(&self) -> Option<&Arc<dyn Any + Send + Sync>> {
self.object.as_ref()
}
/// Resizes the sublayer bounds on iOS.
/// It may not be necessary if original window size matches device's, but often it does not.
/// Thus this should be called after a resize has occurred abd swapchain has been recreated.
///
/// On iOS, we've created CAMetalLayer as a sublayer. However, when the view changes size,
/// its sublayers are not automatically resized, and we must resize
/// it here.
#[cfg(target_os = "ios")]
#[inline]
pub unsafe fn update_ios_sublayer_on_resize(&self) {
use core_graphics_types::geometry::CGRect;
let class = class!(CAMetalLayer);
let main_layer: *mut Object = self.metal_layer.main_layer.0;
let bounds: CGRect = msg_send![main_layer, bounds];
let render_layer: *mut Object = self.metal_layer.render_layer.0;
let () = msg_send![render_layer, setFrame: bounds];
}
}
impl Drop for Surface {
#[inline]
fn drop(&mut self) {
unsafe {
let fns = self.instance.fns();
(fns.khr_surface.destroy_surface_khr)(self.instance.handle(), self.handle, ptr::null());
}
}
}
unsafe impl VulkanObject for Surface {
type Handle = ash::vk::SurfaceKHR;
#[inline]
fn handle(&self) -> Self::Handle {
self.handle
}
}
unsafe impl InstanceOwned for Surface {
#[inline]
fn instance(&self) -> &Arc<Instance> {
&self.instance
}
}
impl Debug for Surface {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
let Self {
handle,
instance,
id,
api,
object,
surface_formats: _,
surface_present_modes: _,
surface_support: _,
} = self;
f.debug_struct("Surface")
.field("handle", handle)
.field("instance", instance)
.field("id", id)
.field("api", api)
.field("object", object)
.finish_non_exhaustive()
}
}
impl_id_counter!(Surface);
/// Get sublayer from iOS main view (ui_view). The sublayer is created as `CAMetalLayer`.
#[cfg(target_os = "ios")]
unsafe fn get_metal_layer_ios(ui_view: *mut std::ffi::c_void) -> IOSMetalLayer {
use core_graphics_types::{base::CGFloat, geometry::CGRect};
let view: *mut Object = ui_view.cast();
let main_layer: *mut Object = msg_send![view, layer];
let class = class!(CAMetalLayer);
let new_layer: *mut Object = msg_send![class, new];
let frame: CGRect = msg_send![main_layer, bounds];
let () = msg_send![new_layer, setFrame: frame];
let () = msg_send![main_layer, addSublayer: new_layer];
let screen: *mut Object = msg_send![class!(UIScreen), mainScreen];
let scale_factor: CGFloat = msg_send![screen, nativeScale];
let () = msg_send![view, setContentScaleFactor: scale_factor];
IOSMetalLayer::new(view, new_layer)
}
/// Get (and set) `CAMetalLayer` to `ns_view`. This is necessary to be able to render on Mac.
#[cfg(target_os = "macos")]
unsafe fn get_metal_layer_macos(ns_view: *mut std::ffi::c_void) -> *mut Object {
use core_graphics_types::base::CGFloat;
use objc::runtime::YES;
use objc::runtime::{BOOL, NO};
let view: *mut Object = ns_view.cast();
let main_layer: *mut Object = msg_send![view, layer];
let class = class!(CAMetalLayer);
let is_valid_layer: BOOL = msg_send![main_layer, isKindOfClass: class];
if is_valid_layer == NO {
let new_layer: *mut Object = msg_send![class, new];
let () = msg_send![new_layer, setEdgeAntialiasingMask: 0];
let () = msg_send![new_layer, setPresentsWithTransaction: false];
let () = msg_send![new_layer, removeAllAnimations];
let () = msg_send![view, setLayer: new_layer];
let () = msg_send![view, setWantsLayer: YES];
let window: *mut Object = msg_send![view, window];
if !window.is_null() {
let scale_factor: CGFloat = msg_send![window, backingScaleFactor];
let () = msg_send![new_layer, setContentsScale: scale_factor];
}
new_layer
} else {
main_layer
}
}
/// Parameters to create a surface from a display mode and plane.
#[derive(Clone, Debug)]
pub struct DisplaySurfaceCreateInfo {
/// The index of the display plane in which the surface will appear.
///
/// The default value is 0.
pub plane_index: u32,
/// The z-order of the plane.
///
/// The default value is 0.
pub plane_stack_index: u32,
/// The transformation to apply to images when presented.
///
/// The default value is [`SurfaceTransform::Identity`].
pub transform: SurfaceTransform,
/// How to do alpha blending on the surface when presenting new content.
///
/// The default value is [`DisplayPlaneAlpha::Opaque`].
pub alpha_mode: DisplayPlaneAlpha,
/// If `alpha_mode` is `DisplayPlaneAlpha::Global`, specifies the global alpha value to use for
/// all blending.
///
/// The default value is 1.0.
pub global_alpha: f32,
/// The size of the presentable images that will be used.
///
/// The default value is `[0; 2]`, which must be overridden.
pub image_extent: [u32; 2],
pub _ne: crate::NonExhaustive,
}
impl Default for DisplaySurfaceCreateInfo {
#[inline]
fn default() -> Self {
Self {
plane_index: 0,
plane_stack_index: 0,
transform: SurfaceTransform::Identity,
alpha_mode: DisplayPlaneAlpha::Opaque,
global_alpha: 1.0,
image_extent: [0; 2],
_ne: crate::NonExhaustive(()),
}
}
}
impl DisplaySurfaceCreateInfo {
pub(crate) fn validate(
&self,
physical_device: &PhysicalDevice,
) -> Result<(), Box<ValidationError>> {
let &Self {
plane_index,
plane_stack_index: _,
transform,
alpha_mode,
global_alpha,
image_extent,
_ne: _,
} = self;
let properties = physical_device.properties();
transform
.validate_physical_device(physical_device)
.map_err(|err| {
err.add_context("transform")
.set_vuids(&["VUID-VkDisplaySurfaceCreateInfoKHR-transform-parameter"])
})?;
alpha_mode
.validate_physical_device(physical_device)
.map_err(|err| {
err.add_context("alpha_mode")
.set_vuids(&["VUID-VkDisplaySurfaceCreateInfoKHR-alphaMode-parameter"])
})?;
let display_plane_properties_raw = unsafe {
physical_device
.display_plane_properties_raw()
.map_err(|_err| {
Box::new(ValidationError {
problem: "`PhysicalDevice::display_plane_properties` \
returned an error"
.into(),
..Default::default()
})
})?
};
if plane_index as usize >= display_plane_properties_raw.len() {
return Err(Box::new(ValidationError {
problem: "`plane_index` is not less than the number of display planes on the \
physical device"
.into(),
vuids: &["VUID-VkDisplaySurfaceCreateInfoKHR-planeIndex-01252"],
..Default::default()
}));
}
if alpha_mode == DisplayPlaneAlpha::Global && !(0.0..=1.0).contains(&global_alpha) {
return Err(Box::new(ValidationError {
problem: "`alpha_mode` is `DisplayPlaneAlpha::Global`, but \
`global_alpha` is not between 0 and 1 inclusive"
.into(),
vuids: &["VUID-VkDisplaySurfaceCreateInfoKHR-alphaMode-01254"],
..Default::default()
}));
}
if image_extent[0] > properties.max_image_dimension2_d {
return Err(Box::new(ValidationError {
context: "image_extent[0]".into(),
problem: "is greater than the `max_image_dimension2_d` device limit".into(),
vuids: &["VUID-VkDisplaySurfaceCreateInfoKHR-width-01256"],
..Default::default()
}));
}
if image_extent[1] > properties.max_image_dimension2_d {
return Err(Box::new(ValidationError {
context: "image_extent[1]".into(),
problem: "is greater than the `max_image_dimension2_d` device limit".into(),
vuids: &["VUID-VkDisplaySurfaceCreateInfoKHR-width-01256"],
..Default::default()
}));
}
Ok(())
}
}
/// The windowing API function that was used to construct a surface.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SurfaceApi {
/// The surface was constructed using [`Surface::headless`] or the
/// `vkCreateHeadlessSurfaceEXT` Vulkan API function.
Headless,
/// The surface was constructed using [`Surface::from_display_plane`] or the
/// `vkCreateDisplayPlaneSurfaceKHR` Vulkan API function.
DisplayPlane,
/*
Alphabetical order
*/
/// The surface was constructed using [`Surface::from_android`] or the
/// `vkCreateAndroidSurfaceKHR` Vulkan API function.
Android,
/// The surface was constructed using [`Surface::from_directfb`] or the
/// `vkCreateDirectFBSurfaceEXT` Vulkan API function.
DirectFB,
/// The surface was constructed using [`Surface::from_fuchsia_image_pipe`] or the
/// `vkCreateImagePipeSurfaceFUCHSIA` Vulkan API function.
FuchsiaImagePipe,
/// The surface was constructed using [`Surface::from_ggp_stream_descriptor`] or the
/// `vkCreateStreamDescriptorSurfaceGGP` Vulkan API function.
GgpStreamDescriptor,
/// The surface was constructed using [`Surface::from_ios`] or the
/// `vkCreateIOSSurfaceMVK` Vulkan API function.
Ios,
/// The surface was constructed using [`Surface::from_mac_os`] or the
/// `vkCreateMacOSSurfaceMVK` Vulkan API function.
MacOs,
/// The surface was constructed using [`Surface::from_metal`] or the
/// `vkCreateMetalSurfaceEXT` Vulkan API function.
Metal,
/// The surface was constructed using [`Surface::from_qnx_screen`] or the
/// `vkCreateScreenSurfaceQNX` Vulkan API function.
QnxScreen,
/// The surface was constructed using [`Surface::from_vi`] or the
/// `vkCreateViSurfaceNN` Vulkan API function.
Vi,
/// The surface was constructed using [`Surface::from_wayland`] or the
/// `vkCreateWaylandSurfaceKHR` Vulkan API function.
Wayland,
/// The surface was constructed using [`Surface::from_win32`] or the
/// `vkCreateWin32SurfaceKHR` Vulkan API function.
Win32,
/// The surface was constructed using [`Surface::from_xcb`] or the
/// `vkCreateXcbSurfaceKHR` Vulkan API function.
Xcb,
/// The surface was constructed using [`Surface::from_xlib`] or the
/// `vkCreateXlibSurfaceKHR` Vulkan API function.
Xlib,
}
impl TryFrom<RawWindowHandle> for SurfaceApi {
type Error = ();
fn try_from(handle: RawWindowHandle) -> Result<Self, Self::Error> {
match handle {
RawWindowHandle::UiKit(_) => Ok(SurfaceApi::Ios),
RawWindowHandle::AppKit(_) => Ok(SurfaceApi::MacOs),
RawWindowHandle::Orbital(_) => Err(()),
RawWindowHandle::Xlib(_) => Ok(SurfaceApi::Xlib),
RawWindowHandle::Xcb(_) => Ok(SurfaceApi::Xcb),
RawWindowHandle::Wayland(_) => Ok(SurfaceApi::Wayland),
RawWindowHandle::Drm(_) => Err(()),
RawWindowHandle::Gbm(_) => Err(()),
RawWindowHandle::Win32(_) => Ok(SurfaceApi::Win32),
RawWindowHandle::WinRt(_) => Err(()),
RawWindowHandle::Web(_) => Err(()),
RawWindowHandle::AndroidNdk(_) => Ok(SurfaceApi::Android),
RawWindowHandle::Haiku(_) => Err(()),
_ => Err(()),
}
}
}
vulkan_enum! {
#[non_exhaustive]
/// The mode of action when a swapchain image is presented.
///
/// Swapchain images can be in one of three possible states:
/// - Exactly one image is currently displayed on the screen.
/// - Zero or more are acquired by the application, or available to be acquired.
/// - Some may be held inside the presentation engine waiting to be displayed. The present mode
/// concerns the behaviour of this category, and by extension, which images are left over for
/// acquiring.
///
/// The present mode affects what is commonly known as "vertical sync" or "vsync" for short.
/// The `Immediate` mode is equivalent to disabling vertical sync, while the others enable
/// vertical sync in various forms. An important aspect of the present modes is their potential
/// *latency*: the time between when an image is presented, and when it actually appears on
/// the display.
///
/// Only `Fifo` is guaranteed to be supported on every device. For the others, you must call
/// [`surface_present_modes`] to see if they are supported.
///
/// [`surface_present_modes`]: crate::device::physical::PhysicalDevice::surface_present_modes
PresentMode = PresentModeKHR(i32);
/// The presentation engine holds only the currently displayed image. When presenting an image,
/// the currently displayed image is immediately replaced with the presented image. The old
/// image will be available for future acquire operations.
///
/// This mode has the lowest latency of all present modes, but if the display is not in a
/// vertical blanking period when the image is replaced, a tear will be visible.
Immediate = IMMEDIATE,
/// The presentation engine holds the currently displayed image, and optionally another in a
/// waiting slot. The presentation engine waits until the next vertical blanking period, then
/// removes any image from the waiting slot and displays it. Tearing will never be visible.
/// When presenting an image, it is stored in the waiting slot. Any previous entry
/// in the slot is discarded, and will be available for future acquire operations.
///
/// Latency is relatively low with this mode, and will never be longer than the time between
/// vertical blanking periods. However, if a previous image in the waiting slot is discarded,
/// the work that went into producing that image was wasted.
///
/// With two swapchain images, this mode behaves essentially identical to `Fifo`: once both
/// images are held in the presentation engine, no images can be acquired until one is finished
/// displaying. But with three or more swapchain images, any images beyond those two are always
/// available to acquire.
Mailbox = MAILBOX,
/// The presentation engine holds the currently displayed image, and a queue of waiting images.
/// When presenting an image, it is added to the tail of the queue, after previously presented
/// images. The presentation engine waits until the next vertical blanking period, then removes
/// an image from the head of the queue and displays it. Tearing will never be visible. Images
/// become available for future acquire operations only after they have been displayed.
///
/// This mode is guaranteed to be always supported. It is possible for all swapchain images to
/// end up being held by the presentation engine, either being displayed or in the queue. When
/// that happens, no images can be acquired until one is finished displaying. This can be used
/// to limit the presentation rate to the display frame rate. Latency is bounded only by the
/// number of images in the swapchain.
///
/// This is the equivalent of OpenGL's `SwapInterval` with a value of 1.
Fifo = FIFO,
/// Similar to `Fifo`, but with the ability for images to "skip the queue" if presentation is
/// lagging behind the display frame rate. If the queue is empty and a vertical blanking period
/// has already passed since the previous image was displayed, then the currently displayed
/// image is immediately replaced with the presented image, as in `Immediate`.
///
/// This mode has high latency if images are presented faster than the display frame rate,
/// as they will accumulate in the queue. But the latency is low if images are presented slower
/// than the display frame rate. However, slower presentation can result in visible tearing.
///
/// This is the equivalent of OpenGL's `SwapInterval` with a value of -1.
FifoRelaxed = FIFO_RELAXED,
/* TODO: enable
// TODO: document
SharedDemandRefresh = SHARED_DEMAND_REFRESH_KHR
RequiresOneOf([
RequiresAllOf([DeviceExtension(khr_shared_presentable_image)]),
]),*/
/* TODO: enable
// TODO: document
SharedContinuousRefresh = SHARED_CONTINUOUS_REFRESH_KHR
RequiresOneOf([
RequiresAllOf([DeviceExtension(khr_shared_presentable_image)]),
]),*/
}
vulkan_bitflags_enum! {
#[non_exhaustive]
/// A set of [`SurfaceTransform`] values.
SurfaceTransforms,
/// The presentation transform to apply when presenting a swapchain image to a surface.
SurfaceTransform,
= SurfaceTransformFlagsKHR(u32);
/// Don't transform the image.
IDENTITY, Identity = IDENTITY,
/// Rotate 90 degrees.
ROTATE_90, Rotate90 = ROTATE_90,
/// Rotate 180 degrees.
ROTATE_180, Rotate180 = ROTATE_180,
/// Rotate 270 degrees.
ROTATE_270, Rotate270 = ROTATE_270,
/// Mirror the image horizontally.
HORIZONTAL_MIRROR, HorizontalMirror = HORIZONTAL_MIRROR,
/// Mirror the image horizontally and rotate 90 degrees.
HORIZONTAL_MIRROR_ROTATE_90, HorizontalMirrorRotate90 = HORIZONTAL_MIRROR_ROTATE_90,
/// Mirror the image horizontally and rotate 180 degrees.
HORIZONTAL_MIRROR_ROTATE_180, HorizontalMirrorRotate180 = HORIZONTAL_MIRROR_ROTATE_180,
/// Mirror the image horizontally and rotate 270 degrees.
HORIZONTAL_MIRROR_ROTATE_270, HorizontalMirrorRotate270 = HORIZONTAL_MIRROR_ROTATE_270,
/// Let the operating system or driver implementation choose.
INHERIT, Inherit = INHERIT,
}
impl Default for SurfaceTransform {
#[inline]
fn default() -> SurfaceTransform {
SurfaceTransform::Identity
}
}
vulkan_bitflags_enum! {
#[non_exhaustive]
/// A set of [`CompositeAlpha`] values.
CompositeAlphas,
/// How the alpha values of the pixels of the window are treated.
CompositeAlpha,
= CompositeAlphaFlagsKHR(u32);
/// The alpha channel of the image is ignored. All the pixels are considered as if they have a
/// value of 1.0.
OPAQUE, Opaque = OPAQUE,
/// The alpha channel of the image is respected. The color channels are expected to have
/// already been multiplied by the alpha value.
PRE_MULTIPLIED, PreMultiplied = PRE_MULTIPLIED,
/// The alpha channel of the image is respected. The color channels will be multiplied by the
/// alpha value by the compositor before being added to what is behind.
POST_MULTIPLIED, PostMultiplied = POST_MULTIPLIED,
/// Let the operating system or driver implementation choose.
INHERIT, Inherit = INHERIT,
}
vulkan_enum! {
#[non_exhaustive]
/// How the presentation engine should interpret the data.
///
/// # A quick lesson about color spaces
///
/// ## What is a color space?
///
/// Each pixel of a monitor is made of three components: one red, one green, and one blue. In
/// the past, computers would simply send to the monitor the intensity of each of the three
/// components.
///
/// This proved to be problematic, because depending on the brand of the monitor the colors
/// would not exactly be the same. For example on some monitors, a value of `[1.0, 0.0, 0.0]`
/// would be a
/// bit more orange than on others.
///
/// In order to standardize this, there exist what are called *color spaces*: sRGB, AdobeRGB,
/// DCI-P3, scRGB, etc. When you manipulate RGB values in a specific color space, these values
/// have a precise absolute meaning in terms of color, that is the same across all systems and
/// monitors.
///
/// > **Note**: Color spaces are orthogonal to concept of RGB. *RGB* only indicates what is the
/// > representation of the data, but not how it is interpreted. You can think of this a bit
/// > like text encoding. An *RGB* value is a like a byte, in other words it is the medium by
/// > which values are communicated, and a *color space* is like a text encoding (eg. UTF-8),
/// > in other words it is the way the value should be interpreted.
///
/// The most commonly used color space today is sRGB. Most monitors today use this color space,
/// and most images files are encoded in this color space.
///
/// ## Pixel formats and linear vs non-linear
///
/// In Vulkan all images have a specific format in which the data is stored. The data of an
/// image consists of pixels in RGB but contains no information about the color space (or lack
/// thereof) of these pixels. You are free to store them in whatever color space you want.
///
/// But one big practical problem with color spaces is that they are sometimes not linear, and
/// in particular the popular sRGB color space is not linear. In a non-linear color space, a
/// value of `[0.6, 0.6, 0.6]` for example is **not** twice as bright as a value of `[0.3, 0.3,
/// 0.3]`. This is problematic, because operations such as taking the average of two colors or
/// calculating the lighting of a texture with a dot product are mathematically incorrect and
/// will produce incorrect colors.
///
/// > **Note**: If the texture format has an alpha component, it is not affected by the color
/// > space and always behaves linearly.
///
/// In order to solve this Vulkan also provides image formats with the `Srgb` suffix, which are
/// expected to contain RGB data in the sRGB color space. When you sample an image with such a
/// format from a shader, the implementation will automatically turn the pixel values into a
/// linear color space that is suitable for linear operations (such as additions or
/// multiplications). When you write to a framebuffer attachment with such a format, the
/// implementation will automatically perform the opposite conversion. These conversions are
/// most of the time performed by the hardware and incur no additional cost.
///
/// ## Color space of the swapchain
///
/// The color space that you specify when you create a swapchain is how the implementation will
/// interpret the raw data inside of the image.
///
/// > **Note**: The implementation can choose to send the data in the swapchain image directly
/// > to the monitor, but it can also choose to write it in an intermediary buffer that is then
/// > read by the operating system or windowing system. Therefore the color space that the
/// > implementation supports is not necessarily the same as the one supported by the monitor.
///
/// It is *your* job to ensure that the data in the swapchain image is in the color space
/// that is specified here, otherwise colors will be incorrect. The implementation will never
/// perform any additional automatic conversion after the colors have been written to the
/// swapchain image.
///
/// # How do I handle this correctly?
///
/// The easiest way to handle color spaces in a cross-platform program is:
///
/// - Always request the `SrgbNonLinear` color space when creating the swapchain.
/// - Make sure that all your image files use the sRGB color space, and load them in images
/// whose format has the `Srgb` suffix. Only use non-sRGB image formats for intermediary
/// computations or to store non-color data.
/// - Swapchain images should have a format with the `Srgb` suffix.
///
/// > **Note**: Lots of developers are confused by color spaces. You can sometimes find articles
/// > talking about gamma correction and suggestion to put your colors to the power 2.2 for
/// > example. These are all hacks and you should use the sRGB pixel formats instead.
///
/// If you follow these three rules, then everything should render the same way on all
/// platforms.
///
/// Additionally you can try detect whether the implementation supports any additional color
/// space and perform a manual conversion to that color space from inside your shader.
ColorSpace = ColorSpaceKHR(i32);
// TODO: document
SrgbNonLinear = SRGB_NONLINEAR,
// TODO: document
DisplayP3NonLinear = DISPLAY_P3_NONLINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
ExtendedSrgbLinear = EXTENDED_SRGB_LINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
ExtendedSrgbNonLinear = EXTENDED_SRGB_NONLINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
DisplayP3Linear = DISPLAY_P3_LINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
DciP3NonLinear = DCI_P3_NONLINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
Bt709Linear = BT709_LINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
Bt709NonLinear = BT709_NONLINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
Bt2020Linear = BT2020_LINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
Hdr10St2084 = HDR10_ST2084_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
DolbyVision = DOLBYVISION_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
Hdr10Hlg = HDR10_HLG_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
AdobeRgbLinear = ADOBERGB_LINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
AdobeRgbNonLinear = ADOBERGB_NONLINEAR_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
PassThrough = PASS_THROUGH_EXT
RequiresOneOf([
RequiresAllOf([InstanceExtension(ext_swapchain_colorspace)]),
]),
// TODO: document
DisplayNative = DISPLAY_NATIVE_AMD
RequiresOneOf([
RequiresAllOf([DeviceExtension(amd_display_native_hdr)]),
]),
}
/// Parameters for [`PhysicalDevice::surface_capabilities`] and [`PhysicalDevice::surface_formats`].
///
/// [`PhysicalDevice::surface_capabilities`]: crate::device::physical::PhysicalDevice::surface_capabilities
/// [`PhysicalDevice::surface_formats`]: crate::device::physical::PhysicalDevice::surface_formats
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SurfaceInfo {
/// If this is `Some`, the
/// [`ext_surface_maintenance1`](crate::instance::InstanceExtensions::ext_surface_maintenance1)
/// extension must be enabled on the instance.
pub present_mode: Option<PresentMode>,
/// If this is not [`FullScreenExclusive::Default`], the
/// [`ext_full_screen_exclusive`](crate::device::DeviceExtensions::ext_full_screen_exclusive)
/// extension must be supported by the physical device.
pub full_screen_exclusive: FullScreenExclusive,
/// If `full_screen_exclusive` is [`FullScreenExclusive::ApplicationControlled`], and the
/// surface being queried is a Win32 surface, then this must be `Some`. Otherwise, it must be
/// `None`.
pub win32_monitor: Option<Win32Monitor>,
pub _ne: crate::NonExhaustive,
}
impl Default for SurfaceInfo {
#[inline]
fn default() -> Self {
Self {
present_mode: None,
full_screen_exclusive: FullScreenExclusive::Default,
win32_monitor: None,
_ne: crate::NonExhaustive(()),
}
}
}
impl SurfaceInfo {
pub(crate) fn validate(
&self,
physical_device: &PhysicalDevice,
) -> Result<(), Box<ValidationError>> {
let &Self {
present_mode,
full_screen_exclusive,
win32_monitor: _,
_ne: _,
} = self;
if let Some(present_mode) = present_mode {
if !physical_device
.instance()
.enabled_extensions()
.ext_surface_maintenance1
{
return Err(Box::new(ValidationError {
context: "present_mode".into(),
problem: "is `Some`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[
Requires::InstanceExtension("ext_surface_maintenance1"),
])]),
..Default::default()
}));
}
present_mode
.validate_physical_device(physical_device)
.map_err(|err| {
err.add_context("present_mode")
.set_vuids(&["VUID-VkSurfacePresentModeEXT-presentMode-parameter"])
})?;
}
if full_screen_exclusive != FullScreenExclusive::Default
&& !physical_device
.supported_extensions()
.ext_full_screen_exclusive
{
return Err(Box::new(ValidationError {
context: "full_screen_exclusive".into(),
problem: "is not `FullScreenExclusive::Default`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::DeviceExtension(
"ext_full_screen_exclusive",
)])]),
..Default::default()
}));
}
Ok(())
}
}
#[cfg(target_os = "ios")]
struct LayerHandle(*mut Object);
#[cfg(target_os = "ios")]
unsafe impl Send for LayerHandle {}
#[cfg(target_os = "ios")]
unsafe impl Sync for LayerHandle {}
/// Represents the metal layer for IOS
#[cfg(target_os = "ios")]
pub struct IOSMetalLayer {
main_layer: LayerHandle,
render_layer: LayerHandle,
}
#[cfg(target_os = "ios")]
impl IOSMetalLayer {
#[inline]
pub fn new(main_layer: *mut Object, render_layer: *mut Object) -> Self {
Self {
main_layer: LayerHandle(main_layer),
render_layer: LayerHandle(render_layer),
}
}
}
#[cfg(target_os = "ios")]
unsafe impl Send for IOSMetalLayer {}
#[cfg(target_os = "ios")]
unsafe impl Sync for IOSMetalLayer {}
/// The capabilities of a surface when used by a physical device.
///
/// You have to match these capabilities when you create a swapchain.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct SurfaceCapabilities {
/// Minimum number of images that must be present in the swapchain.
pub min_image_count: u32,
/// Maximum number of images that must be present in the swapchain, or `None` if there is no
/// maximum value. Note that "no maximum" doesn't mean that you can set a very high value, as
/// you may still get out of memory errors.
pub max_image_count: Option<u32>,
/// The current dimensions of the surface.
///
/// `None` means that the surface's dimensions will depend on the dimensions of the swapchain.
pub current_extent: Option<[u32; 2]>,
/// Minimum width and height of a swapchain that uses this surface.
pub min_image_extent: [u32; 2],
/// Maximum width and height of a swapchain that uses this surface.
pub max_image_extent: [u32; 2],
/// Maximum number of image layers if you create an image array. The minimum is 1.
pub max_image_array_layers: u32,
/// List of transforms supported for the swapchain.
pub supported_transforms: SurfaceTransforms,
/// Current transform used by the surface.
pub current_transform: SurfaceTransform,
/// List of composite alpha modes supports for the swapchain.
pub supported_composite_alpha: CompositeAlphas,
/// List of image usages that are supported for images of the swapchain. Only
/// the `color_attachment` usage is guaranteed to be supported.
pub supported_usage_flags: ImageUsage,
/// When [`SurfaceInfo::present_mode`] is provided,
/// lists that present mode and any modes that are compatible with that present mode.
///
/// If [`SurfaceInfo::present_mode`] was not provided, the value will be empty.
pub compatible_present_modes: SmallVec<[PresentMode; PresentMode::COUNT]>,
/// When [`SurfaceInfo::present_mode`] is provided,
/// the supported present scaling modes for the queried present mode.
///
/// If [`SurfaceInfo::present_mode`] was not provided, the value will be empty.
pub supported_present_scaling: PresentScalingFlags,
/// When [`SurfaceInfo::present_mode`] is provided,
/// the supported present gravity modes, horizontally and vertically,
/// for the queried present mode.
///
/// If [`SurfaceInfo::present_mode`] was not provided, both values will be empty.
pub supported_present_gravity: [PresentGravityFlags; 2],
/// When [`SurfaceInfo::present_mode`] is provided,
/// the smallest allowed extent for a swapchain, if it uses the queried present mode, and
/// one of the scaling modes in `supported_present_scaling`.
///
/// This is never greater than [`SurfaceCapabilities::min_image_extent`].
///
/// `None` means that the surface's dimensions will depend on the dimensions of the swapchain.
///
/// If [`SurfaceInfo::present_mode`] was not provided, this is will be equal to
/// `min_image_extent`.
pub min_scaled_image_extent: Option<[u32; 2]>,
/// When [`SurfaceInfo::present_mode`] is provided,
/// the largest allowed extent for a swapchain, if it uses the queried present mode, and
/// one of the scaling modes in `supported_present_scaling`.
///
/// This is never less than [`SurfaceCapabilities::max_image_extent`].
///
/// `None` means that the surface's dimensions will depend on the dimensions of the swapchain.
///
/// If [`SurfaceInfo::present_mode`] was not provided, this is will be equal to
/// `max_image_extent`.
pub max_scaled_image_extent: Option<[u32; 2]>,
/// Whether creating a protected swapchain is supported.
pub supports_protected: bool,
/// Whether full-screen exclusivity is supported.
pub full_screen_exclusive_supported: bool,
}
#[cfg(test)]
mod tests {
use crate::{
swapchain::Surface, Requires, RequiresAllOf, RequiresOneOf, Validated, ValidationError,
};
use std::ptr;
#[test]
fn khr_win32_surface_ext_missing() {
let instance = instance!();
match unsafe { Surface::from_win32(instance, ptr::null::<u8>(), ptr::null::<u8>(), None) } {
Err(Validated::ValidationError(err))
if matches!(
*err,
ValidationError {
requires_one_of: RequiresOneOf([RequiresAllOf([
Requires::InstanceExtension("khr_win32_surface")
])]),
..
}
) => {}
_ => panic!(),
}
}
#[test]
fn khr_xcb_surface_ext_missing() {
let instance = instance!();
match unsafe { Surface::from_xcb(instance, ptr::null::<u8>(), 0, None) } {
Err(Validated::ValidationError(err))
if matches!(
*err,
ValidationError {
requires_one_of: RequiresOneOf([RequiresAllOf([
Requires::InstanceExtension("khr_xcb_surface")
])]),
..
}
) => {}
_ => panic!(),
}
}
#[test]
fn khr_xlib_surface_ext_missing() {
let instance = instance!();
match unsafe { Surface::from_xlib(instance, ptr::null::<u8>(), 0, None) } {
Err(Validated::ValidationError(err))
if matches!(
*err,
ValidationError {
requires_one_of: RequiresOneOf([RequiresAllOf([
Requires::InstanceExtension("khr_xlib_surface")
])]),
..
}
) => {}
_ => panic!(),
}
}
#[test]
fn khr_wayland_surface_ext_missing() {
let instance = instance!();
match unsafe { Surface::from_wayland(instance, ptr::null::<u8>(), ptr::null::<u8>(), None) }
{
Err(Validated::ValidationError(err))
if matches!(
*err,
ValidationError {
requires_one_of: RequiresOneOf([RequiresAllOf([
Requires::InstanceExtension("khr_wayland_surface")
])]),
..
}
) => {}
_ => panic!(),
}
}
#[test]
fn khr_android_surface_ext_missing() {
let instance = instance!();
match unsafe { Surface::from_android(instance, ptr::null::<u8>(), None) } {
Err(Validated::ValidationError(err))
if matches!(
*err,
ValidationError {
requires_one_of: RequiresOneOf([RequiresAllOf([
Requires::InstanceExtension("khr_android_surface")
])]),
..
}
) => {}
_ => panic!(),
}
}
}