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
use std::ffi::c_void;
#[cfg(any(
    target_os = "linux",
    target_os = "dragonflybsd",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
use std::os::raw::c_ulong;
use std::sync::Arc;

use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use vulkano::instance::Instance;
use vulkano::swapchain::Surface;
use vulkano::swapchain::SurfaceCreationError;

/// Creates a vulkan surface from a generic window
/// which implements HasRawWindowHandle and thus can reveal the os-dependent handle
pub fn create_vk_surface_from_handle<W>(
    window: W,
    instance: Arc<Instance>,
) -> Result<Arc<Surface<W>>, SurfaceCreationError>
where
    W: HasRawWindowHandle,
{
    unsafe {
        match window.raw_window_handle() {
            #[cfg(target_os = "ios")]
            RawWindowHandle::IOS(h) => handle_to_surface(h.ui_view, instance, window),
            #[cfg(target_os = "macos")]
            RawWindowHandle::MacOS(h) => handle_to_surface(h.ns_view, instance, window),
            #[cfg(any(
                target_os = "linux",
                target_os = "dragonflybsd",
                target_os = "freebsd",
                target_os = "netbsd",
                target_os = "openbsd"
            ))]
            RawWindowHandle::Xlib(h) => {
                handle_to_surface_xlib(h.window, h.display, instance, window)
            }
            #[cfg(any(
                target_os = "linux",
                target_os = "dragonflybsd",
                target_os = "freebsd",
                target_os = "netbsd",
                target_os = "openbsd"
            ))]
            RawWindowHandle::Xcb(h) => {
                handle_to_surface_xcb(h.window, h.connection, instance, window)
            }
            #[cfg(any(
                target_os = "linux",
                target_os = "dragonflybsd",
                target_os = "freebsd",
                target_os = "netbsd",
                target_os = "openbsd"
            ))]
            RawWindowHandle::Wayland(h) => {
                handle_to_surface_wl(h.surface, h.display, instance, window)
            }
            #[cfg(target_os = "android")]
            RawWindowHandle::Android(h) => handle_to_surface(h.a_native_window, instance, window),
            #[cfg(target_os = "windows")]
            RawWindowHandle::Windows(h) => handle_to_surface(h.hinstance, h.hwnd, instance, window),
            #[cfg(target_os = "wasm")]
            RawWindowHandle::Web(_) => unimplemented!(),
            _ => unimplemented!(),
        }
    }
}

#[cfg(any(
    target_os = "linux",
    target_os = "dragonflybsd",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
unsafe fn handle_to_surface_xlib<W: Sized>(
    window: c_ulong,
    handle: *const c_void,
    instance: Arc<Instance>,
    win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
    Surface::from_xlib(instance, handle, window, win)
}

#[cfg(any(
    target_os = "linux",
    target_os = "dragonflybsd",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
unsafe fn handle_to_surface_xcb<W: Sized>(
    window: u32,
    handle: *mut c_void,
    instance: Arc<Instance>,
    win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
    Surface::from_xcb(instance, handle as *const _, window, win)
}

#[cfg(any(
    target_os = "linux",
    target_os = "dragonflybsd",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
unsafe fn handle_to_surface_wl<W: Sized>(
    surface: *mut c_void,
    display: *mut c_void,
    instance: Arc<Instance>,
    win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
    Surface::from_wayland(instance, display as *const _, surface as *const _, win)
}

#[cfg(target_os = "macos")]
unsafe fn handle_to_surface<W: Sized>(
    view: *mut c_void,
    instance: Arc<Instance>,
    win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
    Surface::from_macos_moltenvk(instance, view as *const _, win)
}

#[cfg(target_os = "ios")]
unsafe fn handle_to_surface<W: Sized>(
    view: *mut c_void,
    instance: Arc<Instance>,
    win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
    Surface::from_ios_moltenvk(instance, view as *const _, win)
}

#[cfg(target_os = "android")]
unsafe fn handle_to_surface<W: Sized>(
    window: *mut c_void,
    instance: Arc<Instance>,
    win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
    Surface::from_anativewindow(instance, window as *const _, win)
}

#[cfg(target_os = "windows")]
unsafe fn handle_to_surface<W: Sized>(
    hinstance: *mut c_void,
    hwnd: *mut c_void,
    instance: Arc<Instance>,
    win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
    Surface::from_hwnd(instance, hinstance as *const _, hwnd as *const _, win)
}