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
// 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.
//! Communication channel with a physical device.
//!
//! The `Device` is one of the most important objects of Vulkan. Creating a `Device` is required
//! before you can create buffers, textures, shaders, etc.
//!
//! Basic example:
//!
//! ```no_run
//! use vulkano::{
//! device::{physical::PhysicalDevice, Device, DeviceCreateInfo, DeviceExtensions, Features, QueueCreateInfo},
//! instance::{Instance, InstanceExtensions},
//! Version, VulkanLibrary,
//! };
//!
//! // Creating the instance. See the documentation of the `instance` module.
//! let library = VulkanLibrary::new()
//! .unwrap_or_else(|err| panic!("Couldn't load Vulkan library: {:?}", err));
//! let instance = Instance::new(library, Default::default())
//! .unwrap_or_else(|err| panic!("Couldn't create instance: {:?}", err));
//!
//! // We just choose the first physical device. In a real application you would choose depending
//! // on the capabilities of the physical device and the user's preferences.
//! let physical_device = instance
//! .enumerate_physical_devices()
//! .unwrap_or_else(|err| panic!("Couldn't enumerate physical devices: {:?}", err))
//! .next().expect("No physical device");
//!
//! // Here is the device-creating code.
//! let device = {
//! let features = Features::empty();
//! let extensions = DeviceExtensions::empty();
//!
//! match Device::new(
//! physical_device,
//! DeviceCreateInfo {
//! queue_create_infos: vec![QueueCreateInfo {
//! queue_family_index: 0,
//! ..Default::default()
//! }],
//! enabled_extensions: extensions,
//! enabled_features: features,
//! ..Default::default()
//! },
//! ) {
//! Ok(d) => d,
//! Err(err) => panic!("Couldn't build device: {:?}", err)
//! }
//! };
//! ```
//!
//! # Features and extensions
//!
//! Two of the parameters that you pass to `Device::new` are the list of the features and the list
//! of extensions to enable on the newly-created device.
//!
//! > **Note**: Device extensions are the same as instance extensions, except for the device.
//! > Features are similar to extensions, except that they are part of the core Vulkan
//! > specifications instead of being separate documents.
//!
//! Some Vulkan capabilities, such as swapchains (that allow you to render on the screen) or
//! geometry shaders for example, require that you enable a certain feature or extension when you
//! create the device. Contrary to OpenGL, you can't use the functions provided by a feature or an
//! extension if you didn't explicitly enable it when creating the device.
//!
//! Not all physical devices support all possible features and extensions. For example mobile
//! devices tend to not support geometry shaders, because their hardware is not capable of it. You
//! can query what is supported with respectively `PhysicalDevice::supported_features` and
//! `DeviceExtensions::supported_by_device`.
//!
//! > **Note**: The fact that you need to manually enable features at initialization also means
//! > that you don't need to worry about a capability not being supported later on in your code.
//!
//! # Queues
//!
//! Each physical device proposes one or more *queues* that are divided in *queue families*. A
//! queue is a thread of execution to which you can submit commands that the GPU will execute.
//!
//! > **Note**: You can think of a queue like a CPU thread. Each queue executes its commands one
//! > after the other, and queues run concurrently. A GPU behaves similarly to the hyper-threading
//! > technology, in the sense that queues will only run partially in parallel.
//!
//! The Vulkan API requires that you specify the list of queues that you are going to use at the
//! same time as when you create the device. This is done in vulkano by passing an iterator where
//! each element is a tuple containing a queue family and a number between 0.0 and 1.0 indicating
//! the priority of execution of the queue relative to the others.
//!
//! TODO: write better doc here
//!
//! The `Device::new` function returns the newly-created device, but also the list of queues.
//!
//! # Extended example
//!
//! TODO: write
use self::physical::PhysicalDevice;
pub(crate) use self::properties::PropertiesFfi;
pub use self::{
properties::Properties,
queue::{Queue, QueueFamilyProperties, QueueFlags, QueueGuard},
};
pub use crate::fns::DeviceFunctions;
use crate::{
acceleration_structure::{
AccelerationStructureBuildGeometryInfo, AccelerationStructureBuildSizesInfo,
AccelerationStructureBuildType, AccelerationStructureGeometries,
},
descriptor_set::layout::{
DescriptorSetLayoutBinding, DescriptorSetLayoutCreateInfo, DescriptorSetLayoutSupport,
},
instance::{Instance, InstanceOwned, InstanceOwnedDebugWrapper},
macros::{impl_id_counter, vulkan_bitflags},
memory::ExternalMemoryHandleType,
Requires, RequiresAllOf, RequiresOneOf, Validated, ValidationError, Version, VulkanError,
VulkanObject,
};
use ash::vk::Handle;
use parking_lot::Mutex;
use smallvec::{smallvec, SmallVec};
use std::{
ffi::CString,
fmt::{Debug, Error as FmtError, Formatter},
fs::File,
mem::MaybeUninit,
num::NonZeroU64,
ops::Deref,
ptr, slice,
sync::{
atomic::{AtomicU32, Ordering},
Arc,
},
};
pub mod physical;
pub mod private_data;
pub(crate) mod properties;
mod queue;
// Generated by build.rs
include!(concat!(env!("OUT_DIR"), "/device_extensions.rs"));
include!(concat!(env!("OUT_DIR"), "/features.rs"));
/// Represents a Vulkan context.
pub struct Device {
handle: ash::vk::Device,
// NOTE: `physical_devices` always contains this.
physical_device: InstanceOwnedDebugWrapper<Arc<PhysicalDevice>>,
id: NonZeroU64,
enabled_extensions: DeviceExtensions,
enabled_features: Features,
physical_devices: SmallVec<[InstanceOwnedDebugWrapper<Arc<PhysicalDevice>>; 2]>,
// The highest version that is supported for this device.
// This is the minimum of Instance::max_api_version and PhysicalDevice::api_version.
api_version: Version,
fns: DeviceFunctions,
active_queue_family_indices: SmallVec<[u32; 2]>,
// This is required for validation in `memory::device_memory`, the count must only be modified
// in that module.
pub(crate) allocation_count: AtomicU32,
fence_pool: Mutex<Vec<ash::vk::Fence>>,
semaphore_pool: Mutex<Vec<ash::vk::Semaphore>>,
event_pool: Mutex<Vec<ash::vk::Event>>,
}
impl Device {
/// Creates a new `Device`.
#[inline]
pub fn new(
physical_device: Arc<PhysicalDevice>,
create_info: DeviceCreateInfo,
) -> Result<(Arc<Device>, impl ExactSizeIterator<Item = Arc<Queue>>), Validated<VulkanError>>
{
Self::validate_new(&physical_device, &create_info)?;
unsafe { Ok(Self::new_unchecked(physical_device, create_info)?) }
}
fn validate_new(
physical_device: &PhysicalDevice,
create_info: &DeviceCreateInfo,
) -> Result<(), Box<ValidationError>> {
create_info
.validate(physical_device)
.map_err(|err| err.add_context("create_info"))?;
let &DeviceCreateInfo {
queue_create_infos: _,
enabled_extensions: _,
enabled_features: _,
ref physical_devices,
private_data_slot_request_count: _,
_ne: _,
} = create_info;
if !physical_devices.is_empty()
&& !physical_devices
.iter()
.any(|p| p.as_ref() == physical_device)
{
return Err(Box::new(ValidationError {
problem: "`create_info.physical_devices` is not empty, but does not contain \
`physical_device`"
.into(),
vuids: &["VUID-VkDeviceGroupDeviceCreateInfo-physicalDeviceCount-00377"],
..Default::default()
}));
}
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn new_unchecked(
physical_device: Arc<PhysicalDevice>,
mut create_info: DeviceCreateInfo,
) -> Result<(Arc<Device>, impl ExactSizeIterator<Item = Arc<Queue>>), VulkanError> {
// VUID-vkCreateDevice-ppEnabledExtensionNames-01387
create_info.enabled_extensions.enable_dependencies(
physical_device.api_version(),
physical_device.supported_extensions(),
);
// VUID-VkDeviceCreateInfo-pProperties-04451
if physical_device
.supported_extensions()
.khr_portability_subset
{
create_info.enabled_extensions.khr_portability_subset = true;
}
if physical_device.api_version() >= Version::V1_1 {
// VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-04476
if create_info.enabled_extensions.khr_shader_draw_parameters {
create_info.enabled_features.shader_draw_parameters = true;
}
}
if physical_device.api_version() >= Version::V1_2 {
// VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-02831
if create_info.enabled_extensions.khr_draw_indirect_count {
create_info.enabled_features.draw_indirect_count = true;
}
// VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-02832
if create_info
.enabled_extensions
.khr_sampler_mirror_clamp_to_edge
{
create_info.enabled_features.sampler_mirror_clamp_to_edge = true;
}
// VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-02833
if create_info.enabled_extensions.ext_descriptor_indexing {
create_info.enabled_features.descriptor_indexing = true;
}
// VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-02834
if create_info.enabled_extensions.ext_sampler_filter_minmax {
create_info.enabled_features.sampler_filter_minmax = true;
}
// VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-02835
if create_info
.enabled_extensions
.ext_shader_viewport_index_layer
{
create_info.enabled_features.shader_output_viewport_index = true;
create_info.enabled_features.shader_output_layer = true;
}
}
let &DeviceCreateInfo {
ref queue_create_infos,
ref enabled_extensions,
ref enabled_features,
ref physical_devices,
private_data_slot_request_count,
_ne: _,
} = &create_info;
let queue_create_infos_vk: SmallVec<[_; 2]> = queue_create_infos
.iter()
.map(|queue_create_info| {
let &QueueCreateInfo {
flags,
queue_family_index,
ref queues,
_ne: _,
} = queue_create_info;
ash::vk::DeviceQueueCreateInfo {
flags: flags.into(),
queue_family_index,
queue_count: queues.len() as u32,
p_queue_priorities: queues.as_ptr(),
..Default::default()
}
})
.collect();
let enabled_extensions_strings_vk = Vec::<CString>::from(enabled_extensions);
let enabled_extensions_ptrs_vk = enabled_extensions_strings_vk
.iter()
.map(|extension| extension.as_ptr())
.collect::<SmallVec<[_; 16]>>();
let mut features_ffi = FeaturesFfi::default();
features_ffi.make_chain(
physical_device.api_version(),
enabled_extensions,
physical_device.instance().enabled_extensions(),
);
features_ffi.write(enabled_features);
let has_khr_get_physical_device_properties2 = physical_device.instance().api_version()
>= Version::V1_1
|| physical_device
.instance()
.enabled_extensions()
.khr_get_physical_device_properties2;
let mut create_info_vk = ash::vk::DeviceCreateInfo {
flags: ash::vk::DeviceCreateFlags::empty(),
queue_create_info_count: queue_create_infos_vk.len() as u32,
p_queue_create_infos: queue_create_infos_vk.as_ptr(),
enabled_extension_count: enabled_extensions_ptrs_vk.len() as u32,
pp_enabled_extension_names: enabled_extensions_ptrs_vk.as_ptr(),
p_enabled_features: ptr::null(),
..Default::default()
};
let mut device_group_create_info_vk = None;
let device_group_physical_devices_vk: SmallVec<[_; 2]>;
// Length of zero and length of one are completely equivalent,
// so only do anything special here if more than one physical device was given.
// Spec:
// A logical device created without using VkDeviceGroupDeviceCreateInfo,
// or with physicalDeviceCount equal to zero, is equivalent to a physicalDeviceCount of one
// and pPhysicalDevices pointing to the physicalDevice parameter to vkCreateDevice.
if physical_devices.len() > 1 {
device_group_physical_devices_vk = physical_devices
.iter()
.map(|physical_device| physical_device.handle())
.collect();
let next = device_group_create_info_vk.insert(ash::vk::DeviceGroupDeviceCreateInfo {
physical_device_count: device_group_physical_devices_vk.len() as u32,
p_physical_devices: device_group_physical_devices_vk.as_ptr(),
..Default::default()
});
next.p_next = create_info_vk.p_next;
create_info_vk.p_next = next as *mut _ as *mut _;
}
let mut private_data_create_info_vk = None;
if private_data_slot_request_count != 0 {
let next = private_data_create_info_vk.insert(ash::vk::DevicePrivateDataCreateInfo {
private_data_slot_request_count,
..Default::default()
});
next.p_next = create_info_vk.p_next;
create_info_vk.p_next = next as *mut _ as *mut _;
}
// VUID-VkDeviceCreateInfo-pNext-00373
if has_khr_get_physical_device_properties2 {
create_info_vk.p_next = features_ffi.head_as_ref() as *const _ as _;
} else {
create_info_vk.p_enabled_features = &features_ffi.head_as_ref().features;
}
let handle = unsafe {
let fns = physical_device.instance().fns();
let mut output = MaybeUninit::uninit();
(fns.v1_0.create_device)(
physical_device.handle(),
&create_info_vk,
ptr::null(),
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Self::from_handle(physical_device, handle, create_info))
}
/// Creates a new `Device` from a raw object handle.
///
/// # Safety
///
/// - `handle` must be a valid Vulkan object handle created from `physical_device`.
/// - `create_info` must match the info used to create the object.
pub unsafe fn from_handle(
physical_device: Arc<PhysicalDevice>,
handle: ash::vk::Device,
create_info: DeviceCreateInfo,
) -> (Arc<Device>, impl ExactSizeIterator<Item = Arc<Queue>>) {
let DeviceCreateInfo {
queue_create_infos,
enabled_features,
enabled_extensions,
physical_devices,
private_data_slot_request_count: _,
_ne: _,
} = create_info;
let api_version = physical_device.api_version();
let fns = DeviceFunctions::load(|name| unsafe {
(physical_device.instance().fns().v1_0.get_device_proc_addr)(handle, name.as_ptr())
.map_or(ptr::null(), |func| func as _)
});
struct QueueToGet {
flags: QueueCreateFlags,
queue_family_index: u32,
id: u32,
}
let mut active_queue_family_indices: SmallVec<[_; 2]> =
SmallVec::with_capacity(queue_create_infos.len());
let mut queues_to_get: SmallVec<[_; 2]> = SmallVec::with_capacity(queue_create_infos.len());
for queue_create_info in &queue_create_infos {
let &QueueCreateInfo {
flags,
queue_family_index,
ref queues,
_ne: _,
} = queue_create_info;
active_queue_family_indices.push(queue_family_index);
queues_to_get.extend((0..queues.len() as u32).map(move |id| QueueToGet {
flags,
queue_family_index,
id,
}));
}
active_queue_family_indices.sort_unstable();
active_queue_family_indices.dedup();
let physical_devices = if physical_devices.is_empty() {
smallvec![physical_device.clone()]
} else {
physical_devices
};
let device = Arc::new(Device {
handle,
physical_device: InstanceOwnedDebugWrapper(physical_device),
id: Self::next_id(),
enabled_extensions,
enabled_features,
physical_devices: physical_devices
.into_iter()
.map(InstanceOwnedDebugWrapper)
.collect(),
api_version,
fns,
active_queue_family_indices,
allocation_count: AtomicU32::new(0),
fence_pool: Mutex::new(Vec::new()),
semaphore_pool: Mutex::new(Vec::new()),
event_pool: Mutex::new(Vec::new()),
});
let queues_iter = {
let device = device.clone();
queues_to_get.into_iter().map(move |queue_to_get| unsafe {
let QueueToGet {
flags,
queue_family_index,
id,
} = queue_to_get;
Queue::new(device.clone(), flags, queue_family_index, id)
})
};
(device, queues_iter)
}
/// Returns the Vulkan version supported by the device.
///
/// This is the lower of the
/// [physical device's supported version](crate::device::physical::PhysicalDevice::api_version)
/// and the instance's [`max_api_version`](crate::instance::Instance::max_api_version).
#[inline]
pub fn api_version(&self) -> Version {
self.api_version
}
/// Returns pointers to the raw Vulkan functions of the device.
#[inline]
pub fn fns(&self) -> &DeviceFunctions {
&self.fns
}
/// Returns the physical device that was used to create this device.
#[inline]
pub fn physical_device(&self) -> &Arc<PhysicalDevice> {
&self.physical_device
}
/// Returns the list of physical devices that was used to create this device. The index of
/// each physical device in this list is its *device index*.
///
/// This always contains the physical device returned by [`physical_device`].
///
/// [`physical_device`]: Self::physical_device
#[inline]
pub fn physical_devices(&self) -> &[Arc<PhysicalDevice>] {
InstanceOwnedDebugWrapper::cast_slice_inner(&self.physical_devices)
}
/// Returns the instance used to create this device.
#[inline]
pub fn instance(&self) -> &Arc<Instance> {
self.physical_device.instance()
}
/// Returns the queue family indices that this device uses.
#[inline]
pub fn active_queue_family_indices(&self) -> &[u32] {
&self.active_queue_family_indices
}
/// Returns the extensions that have been enabled on the device.
///
/// This includes both the extensions specified in [`DeviceCreateInfo::enabled_extensions`],
/// and any extensions that are required by those extensions.
#[inline]
pub fn enabled_extensions(&self) -> &DeviceExtensions {
&self.enabled_extensions
}
/// Returns the features that have been enabled on the device.
///
/// This includes both the features specified in [`DeviceCreateInfo::enabled_features`],
/// and any features that are required by the enabled extensions.
#[inline]
pub fn enabled_features(&self) -> &Features {
&self.enabled_features
}
/// Returns the current number of active [`DeviceMemory`] allocations the device has.
///
/// [`DeviceMemory`]: crate::memory::DeviceMemory
#[inline]
pub fn allocation_count(&self) -> u32 {
self.allocation_count.load(Ordering::Acquire)
}
pub(crate) fn fence_pool(&self) -> &Mutex<Vec<ash::vk::Fence>> {
&self.fence_pool
}
pub(crate) fn semaphore_pool(&self) -> &Mutex<Vec<ash::vk::Semaphore>> {
&self.semaphore_pool
}
pub(crate) fn event_pool(&self) -> &Mutex<Vec<ash::vk::Event>> {
&self.event_pool
}
/// For the given acceleration structure build info and primitive counts, returns the
/// minimum size required to build the acceleration structure, and the minimum size of the
/// scratch buffer used during the build operation.
#[inline]
pub fn acceleration_structure_build_sizes(
&self,
build_type: AccelerationStructureBuildType,
build_info: &AccelerationStructureBuildGeometryInfo,
max_primitive_counts: &[u32],
) -> Result<AccelerationStructureBuildSizesInfo, Box<ValidationError>> {
self.validate_acceleration_structure_build_sizes(
build_type,
build_info,
max_primitive_counts,
)?;
unsafe {
Ok(self.acceleration_structure_build_sizes_unchecked(
build_type,
build_info,
max_primitive_counts,
))
}
}
fn validate_acceleration_structure_build_sizes(
&self,
build_type: AccelerationStructureBuildType,
build_info: &AccelerationStructureBuildGeometryInfo,
max_primitive_counts: &[u32],
) -> Result<(), Box<ValidationError>> {
if !self.enabled_extensions().khr_acceleration_structure {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::DeviceExtension(
"khr_acceleration_structure",
)])]),
..Default::default()
}));
}
if !self.enabled_features().acceleration_structure {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"acceleration_structure",
)])]),
vuids: &[
"VUID-vkGetAccelerationStructureBuildSizesKHR-accelerationStructure-08933",
],
..Default::default()
}));
}
build_type.validate_device(self).map_err(|err| {
err.add_context("build_type")
.set_vuids(&["VUID-vkGetAccelerationStructureBuildSizesKHR-buildType-parameter"])
})?;
// VUID-vkGetAccelerationStructureBuildSizesKHR-pBuildInfo-parameter
build_info
.validate(self)
.map_err(|err| err.add_context("build_info"))?;
let max_primitive_count = self
.physical_device()
.properties()
.max_primitive_count
.unwrap();
let max_instance_count = self
.physical_device()
.properties()
.max_instance_count
.unwrap();
let geometry_count = match &build_info.geometries {
AccelerationStructureGeometries::Triangles(geometries) => {
for (index, &primitive_count) in max_primitive_counts.iter().enumerate() {
if primitive_count as u64 > max_primitive_count {
return Err(Box::new(ValidationError {
context: format!("max_primitive_counts[{}]", index).into(),
problem: "exceeds the `max_primitive_count` limit".into(),
vuids: &["VUID-VkAccelerationStructureBuildGeometryInfoKHR-type-03795"],
..Default::default()
}));
}
}
geometries.len()
}
AccelerationStructureGeometries::Aabbs(geometries) => {
for (index, &primitive_count) in max_primitive_counts.iter().enumerate() {
if primitive_count as u64 > max_primitive_count {
return Err(Box::new(ValidationError {
context: format!("max_primitive_counts[{}]", index).into(),
problem: "exceeds the `max_primitive_count` limit".into(),
vuids: &["VUID-VkAccelerationStructureBuildGeometryInfoKHR-type-03794"],
..Default::default()
}));
}
}
geometries.len()
}
AccelerationStructureGeometries::Instances(_) => {
for (index, &instance_count) in max_primitive_counts.iter().enumerate() {
if instance_count as u64 > max_instance_count {
return Err(Box::new(ValidationError {
context: format!("max_primitive_counts[{}]", index).into(),
problem: "exceeds the `max_instance_count` limit".into(),
vuids: &[
"VUID-vkGetAccelerationStructureBuildSizesKHR-pBuildInfo-03785",
],
..Default::default()
}));
}
}
1
}
};
if max_primitive_counts.len() != geometry_count {
return Err(Box::new(ValidationError {
problem: "`build_info.geometries` and `max_primitive_counts` \
do not have the same length"
.into(),
vuids: &["VUID-vkGetAccelerationStructureBuildSizesKHR-pBuildInfo-03619"],
..Default::default()
}));
}
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn acceleration_structure_build_sizes_unchecked(
&self,
build_type: AccelerationStructureBuildType,
build_info: &AccelerationStructureBuildGeometryInfo,
max_primitive_counts: &[u32],
) -> AccelerationStructureBuildSizesInfo {
let (mut build_info_vk, geometries_vk) = build_info.to_vulkan();
build_info_vk = ash::vk::AccelerationStructureBuildGeometryInfoKHR {
geometry_count: geometries_vk.len() as u32,
p_geometries: geometries_vk.as_ptr(),
..build_info_vk
};
let mut build_sizes_info_vk = ash::vk::AccelerationStructureBuildSizesInfoKHR::default();
let fns = self.fns();
(fns.khr_acceleration_structure
.get_acceleration_structure_build_sizes_khr)(
self.handle,
build_type.into(),
&build_info_vk,
max_primitive_counts.as_ptr(),
&mut build_sizes_info_vk,
);
AccelerationStructureBuildSizesInfo {
acceleration_structure_size: build_sizes_info_vk.acceleration_structure_size,
update_scratch_size: build_sizes_info_vk.update_scratch_size,
build_scratch_size: build_sizes_info_vk.build_scratch_size,
_ne: crate::NonExhaustive(()),
}
}
/// Returns whether a serialized acceleration structure with the specified version data
/// is compatible with this device.
#[inline]
pub fn acceleration_structure_is_compatible(
&self,
version_data: &[u8; 2 * ash::vk::UUID_SIZE],
) -> Result<bool, Box<ValidationError>> {
self.validate_acceleration_structure_is_compatible(version_data)?;
unsafe { Ok(self.acceleration_structure_is_compatible_unchecked(version_data)) }
}
fn validate_acceleration_structure_is_compatible(
&self,
_version_data: &[u8; 2 * ash::vk::UUID_SIZE],
) -> Result<(), Box<ValidationError>> {
if !self.enabled_extensions().khr_acceleration_structure {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::DeviceExtension(
"khr_acceleration_structure",
)])]),
..Default::default()
}));
}
if !self.enabled_features().acceleration_structure {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[
RequiresAllOf(&[Requires::Feature("acceleration_structure")]),
]),
vuids: &["VUID-vkGetDeviceAccelerationStructureCompatibilityKHR-accelerationStructure-08928"],
..Default::default()
}));
}
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn acceleration_structure_is_compatible_unchecked(
&self,
version_data: &[u8; 2 * ash::vk::UUID_SIZE],
) -> bool {
let version_info_vk = ash::vk::AccelerationStructureVersionInfoKHR {
p_version_data: version_data,
..Default::default()
};
let mut compatibility_vk = ash::vk::AccelerationStructureCompatibilityKHR::default();
let fns = self.fns();
(fns.khr_acceleration_structure
.get_device_acceleration_structure_compatibility_khr)(
self.handle,
&version_info_vk,
&mut compatibility_vk,
);
compatibility_vk == ash::vk::AccelerationStructureCompatibilityKHR::COMPATIBLE
}
/// Returns whether a descriptor set layout with the given `create_info` could be created
/// on the device, and additional supported properties where relevant. `Some` is returned if
/// the descriptor set layout is supported, `None` if it is not.
///
/// This is primarily useful for checking whether the device supports a descriptor set layout
/// that goes beyond the [`max_per_set_descriptors`] limit. A layout that does not exceed
/// that limit is guaranteed to be supported, otherwise this function can be called.
///
/// The device API version must be at least 1.1, or the [`khr_maintenance3`] extension must
/// be enabled on the device.
///
/// [`max_per_set_descriptors`]: crate::device::Properties::max_per_set_descriptors
/// [`khr_maintenance3`]: crate::device::DeviceExtensions::khr_maintenance3
#[inline]
pub fn descriptor_set_layout_support(
&self,
create_info: &DescriptorSetLayoutCreateInfo,
) -> Result<Option<DescriptorSetLayoutSupport>, Box<ValidationError>> {
self.validate_descriptor_set_layout_support(create_info)?;
unsafe { Ok(self.descriptor_set_layout_support_unchecked(create_info)) }
}
fn validate_descriptor_set_layout_support(
&self,
create_info: &DescriptorSetLayoutCreateInfo,
) -> Result<(), Box<ValidationError>> {
if !(self.api_version() >= Version::V1_1 || self.enabled_extensions().khr_maintenance3) {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[
RequiresAllOf(&[Requires::APIVersion(Version::V1_1)]),
RequiresAllOf(&[Requires::DeviceExtension("khr_maintenance3")]),
]),
..Default::default()
}));
}
// VUID-vkGetDescriptorSetLayoutSupport-pCreateInfo-parameter
create_info
.validate(self)
.map_err(|err| err.add_context("create_info"))?;
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn descriptor_set_layout_support_unchecked(
&self,
create_info: &DescriptorSetLayoutCreateInfo,
) -> Option<DescriptorSetLayoutSupport> {
let &DescriptorSetLayoutCreateInfo {
flags,
ref bindings,
_ne: _,
} = create_info;
struct PerBinding {
immutable_samplers_vk: Vec<ash::vk::Sampler>,
}
let mut bindings_vk = Vec::with_capacity(bindings.len());
let mut per_binding_vk = Vec::with_capacity(bindings.len());
let mut binding_flags_info_vk = None;
let mut binding_flags_vk = Vec::with_capacity(bindings.len());
let mut support_vk = ash::vk::DescriptorSetLayoutSupport::default();
let mut variable_descriptor_count_support_vk = None;
for (&binding_num, binding) in bindings.iter() {
let &DescriptorSetLayoutBinding {
binding_flags,
descriptor_type,
descriptor_count,
stages,
ref immutable_samplers,
_ne: _,
} = binding;
bindings_vk.push(ash::vk::DescriptorSetLayoutBinding {
binding: binding_num,
descriptor_type: descriptor_type.into(),
descriptor_count,
stage_flags: stages.into(),
p_immutable_samplers: ptr::null(),
});
per_binding_vk.push(PerBinding {
immutable_samplers_vk: immutable_samplers
.iter()
.map(VulkanObject::handle)
.collect(),
});
binding_flags_vk.push(binding_flags.into());
}
for (binding_vk, per_binding_vk) in bindings_vk.iter_mut().zip(per_binding_vk.iter_mut()) {
binding_vk.p_immutable_samplers = per_binding_vk.immutable_samplers_vk.as_ptr();
}
let mut create_info_vk = ash::vk::DescriptorSetLayoutCreateInfo {
flags: flags.into(),
binding_count: bindings_vk.len() as u32,
p_bindings: bindings_vk.as_ptr(),
..Default::default()
};
if self.api_version() >= Version::V1_2 || self.enabled_extensions().ext_descriptor_indexing
{
let next =
binding_flags_info_vk.insert(ash::vk::DescriptorSetLayoutBindingFlagsCreateInfo {
binding_count: binding_flags_vk.len() as u32,
p_binding_flags: binding_flags_vk.as_ptr(),
..Default::default()
});
next.p_next = create_info_vk.p_next;
create_info_vk.p_next = next as *const _ as *const _;
let next = variable_descriptor_count_support_vk
.insert(ash::vk::DescriptorSetVariableDescriptorCountLayoutSupport::default());
next.p_next = support_vk.p_next;
support_vk.p_next = next as *mut _ as *mut _;
}
let fns = self.fns();
if self.api_version() >= Version::V1_1 {
(fns.v1_1.get_descriptor_set_layout_support)(
self.handle(),
&create_info_vk,
&mut support_vk,
)
} else {
(fns.khr_maintenance3.get_descriptor_set_layout_support_khr)(
self.handle(),
&create_info_vk,
&mut support_vk,
)
}
(support_vk.supported != ash::vk::FALSE).then(|| DescriptorSetLayoutSupport {
max_variable_descriptor_count: variable_descriptor_count_support_vk
.map_or(0, |s| s.max_variable_descriptor_count),
})
}
/// Retrieves the properties of an external file descriptor when imported as a given external
/// handle type.
///
/// An error will be returned if the
/// [`khr_external_memory_fd`](DeviceExtensions::khr_external_memory_fd) extension was not
/// enabled on the device, or if `handle_type` is [`ExternalMemoryHandleType::OpaqueFd`].
///
/// # Safety
///
/// - `file` must be a handle to external memory that was created outside the Vulkan API.
#[inline]
pub unsafe fn memory_fd_properties(
&self,
handle_type: ExternalMemoryHandleType,
file: File,
) -> Result<MemoryFdProperties, Validated<VulkanError>> {
self.validate_memory_fd_properties(handle_type, &file)?;
Ok(self.memory_fd_properties_unchecked(handle_type, file)?)
}
fn validate_memory_fd_properties(
&self,
handle_type: ExternalMemoryHandleType,
_file: &File,
) -> Result<(), Box<ValidationError>> {
if !self.enabled_extensions().khr_external_memory_fd {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::DeviceExtension(
"khr_external_memory_fd",
)])]),
..Default::default()
}));
}
handle_type.validate_device(self).map_err(|err| {
err.add_context("handle_type")
.set_vuids(&["VUID-vkGetMemoryFdPropertiesKHR-handleType-parameter"])
})?;
if handle_type == ExternalMemoryHandleType::OpaqueFd {
return Err(Box::new(ValidationError {
context: "handle_type".into(),
problem: "is `ExternalMemoryHandleType::OpaqueFd`".into(),
vuids: &["VUID-vkGetMemoryFdPropertiesKHR-handleType-00674"],
..Default::default()
}));
}
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn memory_fd_properties_unchecked(
&self,
handle_type: ExternalMemoryHandleType,
file: File,
) -> Result<MemoryFdProperties, VulkanError> {
let mut memory_fd_properties = ash::vk::MemoryFdPropertiesKHR::default();
#[cfg(unix)]
let fd = {
use std::os::fd::IntoRawFd;
file.into_raw_fd()
};
#[cfg(not(unix))]
let fd = {
let _ = file;
-1
};
let fns = self.fns();
(fns.khr_external_memory_fd.get_memory_fd_properties_khr)(
self.handle,
handle_type.into(),
fd,
&mut memory_fd_properties,
)
.result()
.map_err(VulkanError::from)?;
Ok(MemoryFdProperties {
memory_type_bits: memory_fd_properties.memory_type_bits,
})
}
/// Assigns a human-readable name to `object` for debugging purposes.
///
/// If `object_name` is `None`, a previously set object name is removed.
///
/// # Panics
/// - If `object` is not owned by this device.
pub fn set_debug_utils_object_name<T: VulkanObject + DeviceOwned>(
&self,
object: &T,
object_name: Option<&str>,
) -> Result<(), VulkanError> {
assert!(object.device().handle() == self.handle());
let object_name_vk = object_name.map(|object_name| CString::new(object_name).unwrap());
let info = ash::vk::DebugUtilsObjectNameInfoEXT {
object_type: T::Handle::TYPE,
object_handle: object.handle().as_raw(),
p_object_name: object_name_vk
.as_ref()
.map_or(ptr::null(), |object_name| object_name.as_ptr()),
..Default::default()
};
unsafe {
let fns = self.instance().fns();
(fns.ext_debug_utils.set_debug_utils_object_name_ext)(self.handle, &info)
.result()
.map_err(VulkanError::from)?;
}
Ok(())
}
/// Waits until all work on this device has finished. You should never need to call
/// this function, but it can be useful for debugging or benchmarking purposes.
///
/// > **Note**: This is the Vulkan equivalent of OpenGL's `glFinish`.
///
/// # Safety
///
/// This function is not thread-safe. You must not submit anything to any of the queue
/// of the device (either explicitly or implicitly, for example with a future's destructor)
/// while this function is waiting.
#[inline]
pub unsafe fn wait_idle(&self) -> Result<(), VulkanError> {
let fns = self.fns();
(fns.v1_0.device_wait_idle)(self.handle)
.result()
.map_err(VulkanError::from)?;
Ok(())
}
}
impl Debug for Device {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
let Self {
handle,
physical_device,
id,
enabled_extensions,
enabled_features,
physical_devices,
api_version,
fns,
active_queue_family_indices,
allocation_count,
fence_pool: _,
semaphore_pool: _,
event_pool: _,
} = self;
f.debug_struct("Device")
.field("handle", handle)
.field("physical_device", physical_device)
.field("id", id)
.field("enabled_extensions", enabled_extensions)
.field("enabled_features", enabled_features)
.field("physical_devices", physical_devices)
.field("api_version", api_version)
.field("fns", fns)
.field("active_queue_family_indices", active_queue_family_indices)
.field("allocation_count", allocation_count)
.finish_non_exhaustive()
}
}
impl Drop for Device {
#[inline]
fn drop(&mut self) {
let fns = self.fns();
unsafe {
for &raw_fence in self.fence_pool.lock().iter() {
(fns.v1_0.destroy_fence)(self.handle, raw_fence, ptr::null());
}
for &raw_sem in self.semaphore_pool.lock().iter() {
(fns.v1_0.destroy_semaphore)(self.handle, raw_sem, ptr::null());
}
for &raw_event in self.event_pool.lock().iter() {
(fns.v1_0.destroy_event)(self.handle, raw_event, ptr::null());
}
(fns.v1_0.destroy_device)(self.handle, ptr::null());
}
}
}
unsafe impl VulkanObject for Device {
type Handle = ash::vk::Device;
#[inline]
fn handle(&self) -> Self::Handle {
self.handle
}
}
unsafe impl InstanceOwned for Device {
#[inline]
fn instance(&self) -> &Arc<Instance> {
self.physical_device().instance()
}
}
impl_id_counter!(Device);
/// Parameters to create a new `Device`.
#[derive(Clone, Debug)]
pub struct DeviceCreateInfo {
/// The queues to create for the device.
///
/// The default value is empty, which must be overridden.
pub queue_create_infos: Vec<QueueCreateInfo>,
/// The extensions to enable on the device.
///
/// You only need to enable the extensions that you need. If the extensions you specified
/// require additional extensions to be enabled, they will be automatically enabled as well.
///
/// If the [`khr_portability_subset`](DeviceExtensions::khr_portability_subset) extension is
/// available, it will be enabled automatically, so you do not have to do this yourself.
/// You are responsible for ensuring that your program can work correctly on such devices.
/// See [the documentation of the `instance` module](crate::instance#portability-subset-devices-and-the-enumerate_portability-flag)
/// for more information.
///
/// The default value is [`DeviceExtensions::empty()`].
pub enabled_extensions: DeviceExtensions,
/// The features to enable on the device.
///
/// You only need to enable the features that you need. If the extensions you specified
/// require certain features to be enabled, they will be automatically enabled as well.
///
/// The default value is [`Features::empty()`].
pub enabled_features: Features,
/// A list of physical devices to create this device from, to act together as a single
/// logical device. The physical devices must all belong to the same device group, as returned
/// by [`Instance::enumerate_physical_device_groups`], and a physical device must not appear
/// in the list more than once.
///
/// The index of each physical device in this list becomes that physical
/// device's *device index*, which can be used in other Vulkan functions to specify
/// a particular physical device within the group.
/// If the list is left empty, then it behaves as if it contained the physical device,
/// that was passed to the `physical_device` parameter of [`Device::new`], as its only element.
/// Otherwise, that physical device must always be part of the list, but it does not need to
/// be the first element.
///
/// If the list contains more than one physical device, the instance API version must be at
/// least 1.1, or the [`khr_device_group_creation`] extension must be enabled on the instance.
/// In order to use any device-level functionality for dealing with device groups,
/// the physical device API version should also be at least 1.1,
/// or `enabled_extensions` should contain [`khr_device_group`].
///
/// The default value is empty.
///
/// [`Instance::enumerate_physical_device_groups`]: Instance::enumerate_physical_device_groups
/// [`khr_device_group_creation`]: crate::instance::InstanceExtensions::khr_device_group_creation
/// [`khr_device_group`]: crate::device::DeviceExtensions::khr_device_group
pub physical_devices: SmallVec<[Arc<PhysicalDevice>; 2]>,
/// The number of [private data slots] to reserve when creating the device.
///
/// This is purely an optimization, and it is not necessary to do this in order to use private
/// data slots, but it may improve performance.
///
/// If not zero, the physical device API version must be at least 1.3, or `enabled_extensions`
/// must contain [`ext_private_data`].
///
/// The default value is `0`.
///
/// [private data slots]: self::private_data
/// [`ext_private_data`]: DeviceExtensions::ext_private_data
pub private_data_slot_request_count: u32,
pub _ne: crate::NonExhaustive,
}
impl Default for DeviceCreateInfo {
#[inline]
fn default() -> Self {
Self {
queue_create_infos: Vec::new(),
enabled_extensions: DeviceExtensions::empty(),
enabled_features: Features::empty(),
physical_devices: SmallVec::new(),
private_data_slot_request_count: 0,
_ne: crate::NonExhaustive(()),
}
}
}
impl DeviceCreateInfo {
pub(crate) fn validate(
&self,
physical_device: &PhysicalDevice,
) -> Result<(), Box<ValidationError>> {
let &Self {
ref queue_create_infos,
ref enabled_extensions,
ref enabled_features,
ref physical_devices,
private_data_slot_request_count,
_ne: _,
} = self;
if queue_create_infos.is_empty() {
return Err(Box::new(ValidationError {
context: "queue_create_infos".into(),
problem: "is empty".into(),
vuids: &["VUID-VkDeviceCreateInfo-queueCreateInfoCount-arraylength"],
..Default::default()
}));
}
for (index, queue_create_info) in queue_create_infos.iter().enumerate() {
queue_create_info
.validate(physical_device, enabled_extensions, enabled_features)
.map_err(|err| err.add_context(format!("queue_create_infos[{}]", index)))?;
let &QueueCreateInfo {
flags: _,
queue_family_index,
queues: _,
_ne: _,
} = queue_create_info;
if queue_create_infos
.iter()
.filter(|qc2| qc2.queue_family_index == queue_family_index)
.count()
!= 1
{
return Err(Box::new(ValidationError {
problem: format!(
"`queue_create_infos[{}].queue_family_index` occurs more than once in \
`queue_create_infos`",
index
)
.into(),
vuids: &["VUID-VkDeviceCreateInfo-queueFamilyIndex-02802"],
..Default::default()
}));
}
}
enabled_extensions
.check_requirements(
physical_device.supported_extensions(),
physical_device.api_version(),
physical_device.instance().enabled_extensions(),
)
.map_err(|err| {
Box::new(ValidationError {
context: "enabled_extensions".into(),
vuids: &["VUID-vkCreateDevice-ppEnabledExtensionNames-01387"],
..ValidationError::from_error(err)
})
})?;
enabled_features
.check_requirements(physical_device.supported_features())
.map_err(|err| {
Box::new(ValidationError {
context: "enabled_features".into(),
..ValidationError::from_error(err)
})
})?;
let mut dependency_extensions = *enabled_extensions;
dependency_extensions.enable_dependencies(
physical_device.api_version(),
physical_device.supported_extensions(),
);
// VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-01840
// VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-00374
// Ensured because `DeviceExtensions` doesn't contain obsoleted extensions.
if enabled_extensions.ext_buffer_device_address {
if enabled_extensions.khr_buffer_device_address {
return Err(Box::new(ValidationError {
context: "enabled_extensions".into(),
problem: "contains `khr_buffer_device_address`, \
but also contains `ext_buffer_device_address`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-03328"],
..Default::default()
}));
} else if dependency_extensions.khr_buffer_device_address {
return Err(Box::new(ValidationError {
context: "enabled_extensions".into(),
problem: "contains an extension that requires `khr_buffer_device_address`, \
but also contains `ext_buffer_device_address`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-ppEnabledExtensionNames-03328"],
..Default::default()
}));
}
if physical_device.api_version() >= Version::V1_2
&& enabled_features.buffer_device_address
{
return Err(Box::new(ValidationError {
problem: "the physical device API version is at least 1.2, \
`enabled_features` contains `buffer_device_address`, and \
`enabled_extensions` contains `ext_buffer_device_address`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-pNext-04748"],
..Default::default()
}));
}
}
if enabled_features.shading_rate_image {
if enabled_features.pipeline_fragment_shading_rate {
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains both `shading_rate_image` and \
`pipeline_fragment_shading_rate`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-shadingRateImage-04478"],
..Default::default()
}));
}
if enabled_features.primitive_fragment_shading_rate {
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains both `shading_rate_image` and \
`primitive_fragment_shading_rate`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-shadingRateImage-04479"],
..Default::default()
}));
}
if enabled_features.attachment_fragment_shading_rate {
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains both `shading_rate_image` and \
`attachment_fragment_shading_rate`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-shadingRateImage-04480"],
..Default::default()
}));
}
}
if enabled_features.fragment_density_map {
if enabled_features.pipeline_fragment_shading_rate {
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains both `fragment_density_map` and \
`pipeline_fragment_shading_rate`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-shadingRateImage-04481"],
..Default::default()
}));
}
if enabled_features.primitive_fragment_shading_rate {
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains both `fragment_density_map` and \
`primitive_fragment_shading_rate`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-shadingRateImage-04482"],
..Default::default()
}));
}
if enabled_features.attachment_fragment_shading_rate {
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains both `fragment_density_map` and \
`attachment_fragment_shading_rate`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-shadingRateImage-04483"],
..Default::default()
}));
}
}
if enabled_features.sparse_image_int64_atomics
&& !enabled_features.shader_image_int64_atomics
{
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains `sparse_image_int64_atomics`, but does not contain \
`shader_image_int64_atomics`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-None-04896"],
..Default::default()
}));
}
if enabled_features.sparse_image_float32_atomics
&& !enabled_features.shader_image_float32_atomics
{
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains `sparse_image_float32_atomics`, but does not contain \
`shader_image_float32_atomics`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-None-04897"],
..Default::default()
}));
}
if enabled_features.sparse_image_float32_atomic_add
&& !enabled_features.shader_image_float32_atomic_add
{
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains `sparse_image_float32_atomic_add`, but does not contain \
`shader_image_float32_atomic_add`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-None-04898"],
..Default::default()
}));
}
if enabled_features.sparse_image_float32_atomic_min_max
&& !enabled_features.shader_image_float32_atomic_min_max
{
return Err(Box::new(ValidationError {
context: "enabled_features".into(),
problem: "contains `sparse_image_float32_atomic_min_max`, but does not contain \
`shader_image_float32_atomic_min_max`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-sparseImageFloat32AtomicMinMax-04975"],
..Default::default()
}));
}
if enabled_features.descriptor_buffer && enabled_extensions.amd_shader_fragment_mask {
return Err(Box::new(ValidationError {
problem: "`enabled_features` contains `descriptor_buffer`, and \
`enabled_extensions` contains `amd_shader_fragment_mask`"
.into(),
vuids: &["VUID-VkDeviceCreateInfo-None-08095"],
..Default::default()
}));
}
if physical_devices.len() > 1 {
for (index, group_physical_device) in physical_devices.iter().enumerate() {
if physical_devices[..index].contains(group_physical_device) {
return Err(Box::new(ValidationError {
context: "physical_devices".into(),
problem: format!(
"the physical device at index {} is contained in the list more than \
once",
index,
)
.into(),
vuids: &["VUID-VkDeviceGroupDeviceCreateInfo-pPhysicalDevices-00375"],
..Default::default()
}));
}
}
if !(physical_device.instance().api_version() >= Version::V1_1
|| physical_device
.instance()
.enabled_extensions()
.khr_device_group_creation)
{
return Err(Box::new(ValidationError {
context: "physical_devices".into(),
problem: "the length is greater than 1".into(),
requires_one_of: RequiresOneOf(&[
RequiresAllOf(&[Requires::APIVersion(Version::V1_1)]),
RequiresAllOf(&[Requires::InstanceExtension("khr_device_group_creation")]),
]),
..Default::default()
}));
}
if !physical_device
.instance()
.is_same_device_group(physical_devices.iter().map(AsRef::as_ref))
{
return Err(Box::new(ValidationError {
context: "physical_devices".into(),
problem: "the physical devices do not all belong to the same device group"
.into(),
vuids: &["VUID-VkDeviceGroupDeviceCreateInfo-pPhysicalDevices-00376"],
..Default::default()
}));
}
}
if private_data_slot_request_count != 0
&& !(physical_device.api_version() >= Version::V1_3
|| enabled_extensions.ext_private_data)
{
return Err(Box::new(ValidationError {
context: "private_data_slot_request_count".into(),
problem: "is not zero".into(),
requires_one_of: RequiresOneOf(&[
RequiresAllOf(&[Requires::APIVersion(Version::V1_3)]),
RequiresAllOf(&[Requires::DeviceExtension("ext_private_data")]),
]),
..Default::default()
}));
}
Ok(())
}
}
/// Parameters to create queues in a new `Device`.
#[derive(Clone, Debug)]
pub struct QueueCreateInfo {
/// Additional properties of the queue.
///
/// The default value is empty.
pub flags: QueueCreateFlags,
/// The index of the queue family to create queues for.
///
/// The default value is `0`.
pub queue_family_index: u32,
/// The queues to create for the given queue family, each with a relative priority.
///
/// The relative priority value is an arbitrary number between 0.0 and 1.0. Giving a queue a
/// higher priority is a hint to the driver that the queue should be given more processing time.
/// As this is only a hint, different drivers may handle this value differently and there are no
/// guarantees about its behavior.
///
/// The default value is a single queue with a priority of 0.5.
pub queues: Vec<f32>,
pub _ne: crate::NonExhaustive,
}
impl Default for QueueCreateInfo {
#[inline]
fn default() -> Self {
Self {
flags: QueueCreateFlags::empty(),
queue_family_index: 0,
queues: vec![0.5],
_ne: crate::NonExhaustive(()),
}
}
}
impl QueueCreateInfo {
pub(crate) fn validate(
&self,
physical_device: &PhysicalDevice,
device_extensions: &DeviceExtensions,
device_features: &Features,
) -> Result<(), Box<ValidationError>> {
let &Self {
flags,
queue_family_index,
ref queues,
_ne: _,
} = self;
flags
.validate_device_raw(
physical_device.api_version(),
device_features,
device_extensions,
physical_device.instance().enabled_extensions(),
)
.map_err(|err| {
err.add_context("flags")
.set_vuids(&["VUID-VkDeviceQueueCreateInfo-flags-parameter"])
})?;
let queue_family_properties = physical_device
.queue_family_properties()
.get(queue_family_index as usize)
.ok_or_else(|| {
Box::new(ValidationError {
context: "queue_family_index".into(),
problem: "is not less than the number of queue families in the physical device"
.into(),
vuids: &["VUID-VkDeviceQueueCreateInfo-queueFamilyIndex-00381"],
..Default::default()
})
})?;
if queues.is_empty() {
return Err(Box::new(ValidationError {
context: "queues".into(),
problem: "is empty".into(),
vuids: &["VUID-VkDeviceQueueCreateInfo-queueCount-arraylength"],
..Default::default()
}));
}
if queues.len() > queue_family_properties.queue_count as usize {
return Err(Box::new(ValidationError {
problem: "the length of `queues` is greater than the number of queues in the
queue family indicated by `queue_family_index`"
.into(),
vuids: &["VUID-VkDeviceQueueCreateInfo-queueCount-00382"],
..Default::default()
}));
}
for (index, &priority) in queues.iter().enumerate() {
if !(0.0..=1.0).contains(&priority) {
return Err(Box::new(ValidationError {
context: format!("queues[{}]", index).into(),
problem: "is not between 0.0 and 1.0 inclusive".into(),
vuids: &["VUID-VkDeviceQueueCreateInfo-pQueuePriorities-00383"],
..Default::default()
}));
}
}
Ok(())
}
}
vulkan_bitflags! {
#[non_exhaustive]
/// Flags specifying additional properties of a queue.
QueueCreateFlags = DeviceQueueCreateFlags(u32);
PROTECTED = PROTECTED
RequiresOneOf([
RequiresAllOf([APIVersion(V1_1)]),
]),
}
/// Implemented on objects that belong to a Vulkan device.
///
/// # Safety
///
/// - `device()` must return the correct device.
pub unsafe trait DeviceOwned {
/// Returns the device that owns `self`.
fn device(&self) -> &Arc<Device>;
}
unsafe impl<T> DeviceOwned for T
where
T: Deref,
T::Target: DeviceOwned,
{
fn device(&self) -> &Arc<Device> {
(**self).device()
}
}
/// Implemented on objects that implement both `DeviceOwned` and `VulkanObject`.
pub unsafe trait DeviceOwnedVulkanObject {
/// Assigns a human-readable name to the object for debugging purposes.
///
/// If `object_name` is `None`, a previously set object name is removed.
fn set_debug_utils_object_name(&self, object_name: Option<&str>) -> Result<(), VulkanError>;
}
unsafe impl<T> DeviceOwnedVulkanObject for T
where
T: DeviceOwned + VulkanObject,
{
fn set_debug_utils_object_name(&self, object_name: Option<&str>) -> Result<(), VulkanError> {
self.device().set_debug_utils_object_name(self, object_name)
}
}
/// Same as [`DebugWrapper`], but also prints the device handle for disambiguation.
///
/// [`DebugWrapper`]: crate:: DebugWrapper
#[derive(Clone, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub(crate) struct DeviceOwnedDebugWrapper<T>(pub(crate) T);
impl<T> DeviceOwnedDebugWrapper<T> {
pub fn cast_slice_inner(slice: &[Self]) -> &[T] {
// SAFETY: `DeviceOwnedDebugWrapper<T>` and `T` have the same layout.
unsafe { slice::from_raw_parts(slice as *const _ as *const _, slice.len()) }
}
}
impl<T> Debug for DeviceOwnedDebugWrapper<T>
where
T: VulkanObject + DeviceOwned,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
write!(
f,
"0x{:x} (device: 0x{:x})",
self.0.handle().as_raw(),
self.0.device().handle().as_raw(),
)
}
}
impl<T> Deref for DeviceOwnedDebugWrapper<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// The properties of a Unix file descriptor when it is imported.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct MemoryFdProperties {
/// A bitmask of the indices of memory types that can be used with the file.
pub memory_type_bits: u32,
}
#[cfg(test)]
mod tests {
use crate::device::{Device, DeviceCreateInfo, DeviceExtensions, Features, QueueCreateInfo};
use std::{ffi::CString, sync::Arc};
#[test]
fn empty_extensions() {
let d: Vec<CString> = (&DeviceExtensions::empty()).into();
assert!(d.get(0).is_none());
}
#[test]
fn extensions_into_iter() {
let extensions = DeviceExtensions {
khr_swapchain: true,
..DeviceExtensions::empty()
};
for (name, enabled) in extensions {
if name == "VK_KHR_swapchain" {
assert!(enabled);
} else {
assert!(!enabled);
}
}
}
#[test]
fn features_into_iter() {
let features = Features {
tessellation_shader: true,
..Features::empty()
};
for (name, enabled) in features {
if name == "tessellationShader" {
assert!(enabled);
} else {
assert!(!enabled);
}
}
}
#[test]
fn one_ref() {
let (mut device, _) = gfx_dev_and_queue!();
assert!(Arc::get_mut(&mut device).is_some());
}
#[test]
fn too_many_queues() {
let instance = instance!();
let physical_device = match instance.enumerate_physical_devices().unwrap().next() {
Some(p) => p,
None => return,
};
let queue_family_index = 0;
let queue_family_properties =
&physical_device.queue_family_properties()[queue_family_index as usize];
let queues = (0..queue_family_properties.queue_count + 1)
.map(|_| (0.5))
.collect();
if Device::new(
physical_device,
DeviceCreateInfo {
queue_create_infos: vec![QueueCreateInfo {
queue_family_index,
queues,
..Default::default()
}],
..Default::default()
},
)
.is_ok()
{
panic!()
}
}
#[test]
fn unsupported_features() {
let instance = instance!();
let physical_device = match instance.enumerate_physical_devices().unwrap().next() {
Some(p) => p,
None => return,
};
let features = Features::all();
// In the unlikely situation where the device supports everything, we ignore the test.
if physical_device.supported_features().contains(&features) {
return;
}
if Device::new(
physical_device,
DeviceCreateInfo {
queue_create_infos: vec![QueueCreateInfo {
queue_family_index: 0,
..Default::default()
}],
enabled_features: features,
..Default::default()
},
)
.is_ok()
{
panic!()
}
}
#[test]
fn priority_out_of_range() {
let instance = instance!();
let physical_device = match instance.enumerate_physical_devices().unwrap().next() {
Some(p) => p,
None => return,
};
if Device::new(
physical_device.clone(),
DeviceCreateInfo {
queue_create_infos: vec![QueueCreateInfo {
queue_family_index: 0,
queues: vec![1.4],
..Default::default()
}],
..Default::default()
},
)
.is_ok()
{
panic!();
}
if Device::new(
physical_device,
DeviceCreateInfo {
queue_create_infos: vec![QueueCreateInfo {
queue_family_index: 0,
queues: vec![-0.2],
..Default::default()
}],
..Default::default()
},
)
.is_ok()
{
panic!();
}
}
}