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
use std::collections::HashSet;
use std::ffi::{CStr, CString};
use std::fmt;
use std::iter::FromIterator;
use std::ptr;
use std::str;
use crate::check_errors;
use crate::extensions::SupportedExtensionsError;
use crate::instance::PhysicalDevice;
use crate::vk;
use crate::VulkanObject;
macro_rules! device_extensions {
($sname:ident, $rawname:ident, [$($ext_req_if_supported:ident,)*], $($ext:ident => $s:expr,)*) => (
extensions! {
$sname, $rawname,
$( $ext => $s,)*
}
impl $rawname {
pub fn supported_by_device_raw(physical_device: PhysicalDevice) -> Result<Self, SupportedExtensionsError> {
let vk = physical_device.instance().pointers();
let properties: Vec<vk::ExtensionProperties> = unsafe {
let mut num = 0;
check_errors(vk.EnumerateDeviceExtensionProperties(
physical_device.internal_object(), ptr::null(), &mut num, ptr::null_mut()
))?;
let mut properties = Vec::with_capacity(num as usize);
check_errors(vk.EnumerateDeviceExtensionProperties(
physical_device.internal_object(), ptr::null(), &mut num, properties.as_mut_ptr()
))?;
properties.set_len(num as usize);
properties
};
Ok($rawname(properties.iter().map(|x| unsafe { CStr::from_ptr(x.extensionName.as_ptr()) }.to_owned()).collect()))
}
pub fn supported_by_device(physical_device: PhysicalDevice) -> Self {
match $rawname::supported_by_device_raw(physical_device) {
Ok(l) => l,
Err(SupportedExtensionsError::LoadingError(_)) => unreachable!(),
Err(SupportedExtensionsError::OomError(e)) => panic!("{:?}", e),
}
}
}
impl $sname {
pub fn supported_by_device_raw(physical_device: PhysicalDevice) -> Result<Self, SupportedExtensionsError> {
let vk = physical_device.instance().pointers();
let properties: Vec<vk::ExtensionProperties> = unsafe {
let mut num = 0;
check_errors(vk.EnumerateDeviceExtensionProperties(
physical_device.internal_object(), ptr::null(), &mut num, ptr::null_mut()
))?;
let mut properties = Vec::with_capacity(num as usize);
check_errors(vk.EnumerateDeviceExtensionProperties(
physical_device.internal_object(), ptr::null(), &mut num, properties.as_mut_ptr()
))?;
properties.set_len(num as usize);
properties
};
let mut extensions = $sname::none();
for property in properties {
let name = unsafe { CStr::from_ptr(property.extensionName.as_ptr()) };
$(
if name.to_bytes() == &$s[..] {
extensions.$ext = true;
}
)*
}
Ok(extensions)
}
pub fn supported_by_device(physical_device: PhysicalDevice) -> Self {
match $sname::supported_by_device_raw(physical_device) {
Ok(l) => l,
Err(SupportedExtensionsError::LoadingError(_)) => unreachable!(),
Err(SupportedExtensionsError::OomError(e)) => panic!("{:?}", e),
}
}
pub fn required_extensions(physical_device: PhysicalDevice) -> Self {
let supported = Self::supported_by_device(physical_device);
let required_if_supported = Self::required_if_supported_extensions();
required_if_supported.intersection(&supported)
}
fn required_if_supported_extensions() -> Self {
Self {
$(
$ext_req_if_supported: true,
)*
..Self::none()
}
}
}
);
}
device_extensions! {
DeviceExtensions,
RawDeviceExtensions,
[
khr_portability_subset,
],
khr_swapchain => b"VK_KHR_swapchain",
khr_display_swapchain => b"VK_KHR_display_swapchain",
khr_sampler_mirror_clamp_to_edge => b"VK_KHR_sampler_mirror_clamp_to_edge",
khr_maintenance1 => b"VK_KHR_maintenance1",
khr_get_memory_requirements2 => b"VK_KHR_get_memory_requirements2",
khr_dedicated_allocation => b"VK_KHR_dedicated_allocation",
khr_incremental_present => b"VK_KHR_incremental_present",
khr_16bit_storage => b"VK_KHR_16bit_storage",
khr_8bit_storage => b"VK_KHR_8bit_storage",
khr_storage_buffer_storage_class => b"VK_KHR_storage_buffer_storage_class",
ext_debug_utils => b"VK_EXT_debug_utils",
khr_multiview => b"VK_KHR_multiview",
ext_full_screen_exclusive => b"VK_EXT_full_screen_exclusive",
khr_external_memory => b"VK_KHR_external_memory",
khr_external_memory_fd => b"VK_KHR_external_memory_fd",
ext_external_memory_dmabuf => b"VK_EXT_external_memory_dma_buf",
khr_portability_subset => b"VK_KHR_portability_subset",
}
#[doc(hidden)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Unbuildable(());
#[cfg(test)]
mod tests {
use crate::device::{DeviceExtensions, RawDeviceExtensions};
#[test]
fn empty_extensions() {
let d: RawDeviceExtensions = (&DeviceExtensions::none()).into();
assert!(d.iter().next().is_none());
}
#[test]
fn required_if_supported_extensions() {
assert_eq!(
DeviceExtensions::required_if_supported_extensions(),
DeviceExtensions {
khr_portability_subset: true,
..DeviceExtensions::none()
}
)
}
}