Skip to main content

duku/surface/
mod.rs

1// Oliver Berzs
2// https://github.com/oberzs/duku
3
4// a drawable surface that connects to an OS window
5
6mod handle;
7mod properties;
8mod swapchain;
9
10use std::ptr;
11
12use crate::instance::Instance;
13use crate::vk;
14
15pub(crate) use swapchain::Swapchain;
16
17pub use handle::WindowHandle;
18pub use properties::VSync;
19
20pub(crate) struct Surface {
21    handle: vk::SurfaceKHR,
22}
23
24impl Surface {
25    #[cfg(target_os = "windows")]
26    pub(crate) fn new(instance: &Instance, window: WindowHandle) -> Self {
27        let info = vk::Win32SurfaceCreateInfoKHR {
28            s_type: vk::STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR,
29            p_next: ptr::null(),
30            flags: 0,
31            hwnd: window.hwnd,
32            hinstance: ptr::null(),
33        };
34
35        let handle = instance.create_win32_surface(&info);
36
37        Self { handle }
38    }
39
40    #[cfg(target_os = "linux")]
41    pub(crate) fn new(instance: &Instance, window: WindowHandle) -> Self {
42        let info = vk::XlibSurfaceCreateInfoKHR {
43            s_type: vk::STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
44            p_next: ptr::null(),
45            flags: 0,
46            dpy: window.xlib_display.cast(),
47            window: window.xlib_window,
48        };
49
50        let handle = instance.create_linux_surface(&info);
51
52        Self { handle }
53    }
54
55    #[cfg(target_os = "macos")]
56    pub(crate) fn new(instance: &Instance, window: WindowHandle) -> Self {
57        unimplemented!();
58
59        let info = vk::MacOSSurfaceCreateInfoMVK {
60            s_type: vk::STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK,
61            p_next: ptr::null(),
62            flags: 0,
63            p_view: ptr::null(), // TODO: implement
64        };
65
66        let handle = instance.create_macos_surface(&info);
67
68        Self { handle }
69    }
70
71    pub(crate) const fn handle(&self) -> vk::SurfaceKHR {
72        self.handle
73    }
74}