-
Notifications
You must be signed in to change notification settings - Fork 437
/
winit.rs
201 lines (180 loc) · 6.05 KB
/
winit.rs
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
use std::{
error::Error,
fmt::{Display, Error as FmtError, Formatter},
sync::Arc,
};
use vulkano::{
instance::{Instance, InstanceExtensions},
swapchain::Surface,
Validated, VulkanError, VulkanLibrary,
};
use winit::{
error::OsError as WindowCreationError,
event_loop::EventLoopWindowTarget,
window::{Window, WindowBuilder},
};
pub fn required_extensions(library: &VulkanLibrary) -> InstanceExtensions {
let ideal = InstanceExtensions {
khr_surface: true,
khr_xlib_surface: true,
khr_xcb_surface: true,
khr_wayland_surface: true,
khr_android_surface: true,
khr_win32_surface: true,
ext_metal_surface: true,
khr_get_physical_device_properties2: true,
khr_get_surface_capabilities2: true,
..InstanceExtensions::empty()
};
library.supported_extensions().intersection(&ideal)
}
/// Create a surface from a Winit window or a reference to it. The surface takes `W` to prevent it
/// from being dropped before the surface.
pub fn create_surface_from_winit(
window: Arc<Window>,
instance: Arc<Instance>,
) -> Result<Arc<Surface>, Validated<VulkanError>> {
unsafe { winit_to_surface(instance, window) }
}
pub trait VkSurfaceBuild<E> {
fn build_vk_surface(
self,
event_loop: &EventLoopWindowTarget<E>,
instance: Arc<Instance>,
) -> Result<Arc<Surface>, CreationError>;
}
impl<E> VkSurfaceBuild<E> for WindowBuilder {
fn build_vk_surface(
self,
event_loop: &EventLoopWindowTarget<E>,
instance: Arc<Instance>,
) -> Result<Arc<Surface>, CreationError> {
let window = Arc::new(self.build(event_loop)?);
Ok(create_surface_from_winit(window, instance)?)
}
}
/// Error that can happen when creating a window.
#[derive(Debug)]
pub enum CreationError {
/// Error when creating the surface.
Surface(Validated<VulkanError>),
/// Error when creating the window.
Window(WindowCreationError),
}
impl Error for CreationError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CreationError::Surface(err) => Some(err),
CreationError::Window(err) => Some(err),
}
}
}
impl Display for CreationError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
write!(
f,
"{}",
match self {
CreationError::Surface(_) => "error while creating the surface",
CreationError::Window(_) => "error while creating the window",
}
)
}
}
impl From<Validated<VulkanError>> for CreationError {
fn from(err: Validated<VulkanError>) -> CreationError {
CreationError::Surface(err)
}
}
impl From<WindowCreationError> for CreationError {
fn from(err: WindowCreationError) -> CreationError {
CreationError::Window(err)
}
}
#[cfg(target_os = "android")]
unsafe fn winit_to_surface(
instance: Arc<Instance>,
window: Arc<Window>,
) -> Result<Arc<Surface>, Validated<VulkanError>> {
use raw_window_handle::HasRawWindowHandle;
use raw_window_handle::RawWindowHandle::AndroidNdk;
if let AndroidNdk(handle) = window.raw_window_handle() {
Surface::from_android(instance, handle.a_native_window, Some(window))
} else {
unreachable!("This should be unreachable if the target is android");
}
}
#[cfg(all(unix, not(target_os = "android"), target_vendor = "apple",))]
unsafe fn winit_to_surface(
instance: Arc<Instance>,
window: Arc<Window>,
) -> Result<Arc<Surface>, Validated<VulkanError>> {
use winit::platform::{wayland::WindowExtWayland, x11::WindowExtX11};
match (window.wayland_display(), window.wayland_surface()) {
(Some(display), Some(surface)) => {
Surface::from_wayland(instance, display, surface, Some(window))
}
_ => {
// No wayland display found, check if we can use xlib.
// If not, we use xcb.
if instance.enabled_extensions().khr_xlib_surface {
Surface::from_xlib(
instance,
window.xlib_display().unwrap() as _,
window.xlib_window().unwrap() as _,
Some(window),
)
} else {
Surface::from_xcb(
instance,
window.xcb_connection().unwrap(),
window.xlib_window().unwrap() as _,
Some(window),
)
}
}
}
}
#[cfg(target_os = "macos")]
unsafe fn winit_to_surface(
instance: Arc<Instance>,
window: Arc<Window>,
) -> Result<Arc<Surface>, Validated<VulkanError>> {
use winit::platform::macos::WindowExtMacOS;
let view = std::ptr::NonNull::new(window.ns_view()).unwrap();
let layer = raw_window_metal::Layer::from_ns_view(view);
Surface::from_metal(instance, layer.as_ptr(), Some(window))
}
#[cfg(target_os = "ios")]
unsafe fn winit_to_surface(
instance: Arc<Instance>,
window: Arc<Window>,
) -> Result<Arc<Surface>, Validated<VulkanError>> {
use winit::platform::ios::WindowExtIOS;
let view = std::ptr::NonNull::new(window.ui_view()).unwrap();
let layer = raw_window_metal::Layer::from_ui_view(view);
Surface::from_metal(instance, layer.as_ptr(), Some(window))
}
#[cfg(target_os = "windows")]
unsafe fn winit_to_surface(
instance: Arc<Instance>,
window: Arc<Window>,
) -> Result<Arc<Surface>, Validated<VulkanError>> {
use winit::platform::windows::WindowExtWindows;
Surface::from_win32(
instance,
window.hinstance() as _,
window.hwnd() as _,
Some(window),
)
}
#[cfg(target_os = "windows")]
use vulkano::swapchain::Win32Monitor;
#[cfg(target_os = "windows")]
use winit::{monitor::MonitorHandle, platform::windows::MonitorHandleExtWindows};
#[cfg(target_os = "windows")]
/// Creates a `Win32Monitor` from a Winit monitor handle.
#[inline]
pub fn create_win32_monitor_from_winit(monitor_handle: &MonitorHandle) -> Win32Monitor {
unsafe { Win32Monitor::new(monitor_handle.hmonitor() as _) }
}