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
#![warn(trivial_casts, trivial_numeric_casts)]
use std::os::raw::c_char;
use ash::{extensions::khr, prelude::*, vk, Entry, Instance};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
#[cfg(any(target_os = "macos", target_os = "ios"))]
use ash::extensions::ext;
pub unsafe fn create_surface(
entry: &Entry,
instance: &Instance,
window_handle: &dyn HasRawWindowHandle,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::SurfaceKHR> {
match window_handle.raw_window_handle() {
#[cfg(target_os = "windows")]
RawWindowHandle::Windows(handle) => {
let surface_desc = vk::Win32SurfaceCreateInfoKHR::builder()
.hinstance(handle.hinstance)
.hwnd(handle.hwnd);
let surface_fn = khr::Win32Surface::new(entry, instance);
surface_fn.create_win32_surface(&surface_desc, allocation_callbacks)
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Wayland(handle) => {
let surface_desc = vk::WaylandSurfaceCreateInfoKHR::builder()
.display(handle.display)
.surface(handle.surface);
let surface_fn = khr::WaylandSurface::new(entry, instance);
surface_fn.create_wayland_surface(&surface_desc, allocation_callbacks)
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Xlib(handle) => {
let surface_desc = vk::XlibSurfaceCreateInfoKHR::builder()
.dpy(handle.display as *mut _)
.window(handle.window);
let surface_fn = khr::XlibSurface::new(entry, instance);
surface_fn.create_xlib_surface(&surface_desc, allocation_callbacks)
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Xcb(handle) => {
let surface_desc = vk::XcbSurfaceCreateInfoKHR::builder()
.connection(handle.connection)
.window(handle.window);
let surface_fn = khr::XcbSurface::new(entry, instance);
surface_fn.create_xcb_surface(&surface_desc, allocation_callbacks)
}
#[cfg(any(target_os = "android"))]
RawWindowHandle::Android(handle) => {
let surface_desc =
vk::AndroidSurfaceCreateInfoKHR::builder().window(handle.a_native_window);
let surface_fn = khr::AndroidSurface::new(entry, instance);
surface_fn.create_android_surface(&surface_desc, allocation_callbacks)
}
#[cfg(any(target_os = "macos"))]
RawWindowHandle::MacOS(handle) => {
use raw_window_metal::{macos, Layer};
let layer = match macos::metal_layer_from_handle(handle) {
Layer::Existing(layer) | Layer::Allocated(layer) => layer as *mut _,
Layer::None => return Err(vk::Result::ERROR_INITIALIZATION_FAILED),
};
let surface_desc = vk::MetalSurfaceCreateInfoEXT::builder().layer(&*layer);
let surface_fn = ext::MetalSurface::new(entry, instance);
surface_fn.create_metal_surface(&surface_desc, allocation_callbacks)
}
#[cfg(any(target_os = "ios"))]
RawWindowHandle::IOS(handle) => {
use raw_window_metal::{ios, Layer};
let layer = match ios::metal_layer_from_handle(handle) {
Layer::Existing(layer) | Layer::Allocated(layer) => layer as *mut _,
Layer::None => return Err(vk::Result::ERROR_INITIALIZATION_FAILED),
};
let surface_desc = vk::MetalSurfaceCreateInfoEXT::builder().layer(&*layer);
let surface_fn = ext::MetalSurface::new(entry, instance);
surface_fn.create_metal_surface(&surface_desc, allocation_callbacks)
}
_ => Err(vk::Result::ERROR_EXTENSION_NOT_PRESENT),
}
}
pub fn enumerate_required_extensions(
window_handle: &dyn HasRawWindowHandle,
) -> VkResult<&'static [*const c_char]> {
let extensions = match window_handle.raw_window_handle() {
#[cfg(target_os = "windows")]
RawWindowHandle::Windows(_) => {
const WINDOWS_EXTS: [*const c_char; 2] = [
khr::Surface::name().as_ptr(),
khr::Win32Surface::name().as_ptr(),
];
&WINDOWS_EXTS
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Wayland(_) => {
const WAYLAND_EXTS: [*const c_char; 2] = [
khr::Surface::name().as_ptr(),
khr::WaylandSurface::name().as_ptr(),
];
&WAYLAND_EXTS
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Xlib(_) => {
const XLIB_EXTS: [*const c_char; 2] = [
khr::Surface::name().as_ptr(),
khr::XlibSurface::name().as_ptr(),
];
&XLIB_EXTS
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Xcb(_) => {
const XCB_EXTS: [*const c_char; 2] = [
khr::Surface::name().as_ptr(),
khr::XcbSurface::name().as_ptr(),
];
&XCB_EXTS
}
#[cfg(any(target_os = "android"))]
RawWindowHandle::Android(_) => {
const ANDROID_EXTS: [*const c_char; 2] = [
khr::Surface::name().as_ptr(),
khr::AndroidSurface::name().as_ptr(),
];
&ANDROID_EXTS
}
#[cfg(any(target_os = "macos"))]
RawWindowHandle::MacOS(_) => {
const MACOS_EXTS: [*const c_char; 2] = [
khr::Surface::name().as_ptr(),
ext::MetalSurface::name().as_ptr(),
];
&MACOS_EXTS
}
#[cfg(any(target_os = "ios"))]
RawWindowHandle::IOS(_) => {
const IOS_EXTS: [*const c_char; 2] = [
khr::Surface::name().as_ptr(),
ext::MetalSurface::name().as_ptr(),
];
&IOS_EXTS
}
_ => return Err(vk::Result::ERROR_EXTENSION_NOT_PRESENT),
};
Ok(extensions)
}