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
//! Vulkan support in SDL2. It accepts the instance from [`ash`] crate.

#![cfg(feature = "vulkan")]
#![doc(cfg(feature = "vulkan"))]

use ash::vk::{Handle as _, Instance};
use std::{
    ffi::{c_void, CStr},
    marker::PhantomData,
    mem::MaybeUninit,
    os::raw::c_uint,
    ptr::NonNull,
};

use crate::{
    bind,
    geo::Size,
    window::{Window, WindowContextKind},
    Result, SdlError,
};

/// A Vulkan instance from a window.
#[derive(Debug)]
pub struct VkInstance<'window> {
    window: &'window Window<'window>,
    extensions: Vec<String>,
}

impl<'window> VkInstance<'window> {
    /// Constructs an instance from the window, or `Err` on failure.
    pub fn new(window: &'window Window<'window>) -> Result<Self> {
        if window.state().context_kind != WindowContextKind::Vulkan {
            return Err(SdlError::UnsupportedFeature);
        }
        let mut num: c_uint = 0;
        let ret = unsafe {
            bind::SDL_Vulkan_GetInstanceExtensions(
                window.as_ptr(),
                &mut num as *mut _,
                std::ptr::null_mut(),
            )
        };
        if ret == bind::SDL_FALSE {
            return Err(SdlError::UnsupportedFeature);
        }
        let mut extensions = vec![std::ptr::null(); num as usize];
        let _ = unsafe {
            bind::SDL_Vulkan_GetInstanceExtensions(
                window.as_ptr(),
                &mut num as *mut _,
                extensions.as_mut_ptr(),
            )
        };
        let extensions = extensions
            .into_iter()
            .map(|ptr| unsafe { CStr::from_ptr(ptr) }.to_string_lossy().into())
            .collect();
        Ok(Self { window, extensions })
    }

    /// Returns the names of the supported extensions.
    pub fn extensions(&self) -> &[String] {
        &self.extensions
    }

    /// Returns the drawable region size.
    pub fn drawable_size(&self) -> Size {
        let mut width = 0;
        let mut height = 0;
        unsafe {
            bind::SDL_Vulkan_GetDrawableSize(
                self.window.as_ptr(),
                &mut width as _,
                &mut height as _,
            )
        }
        Size {
            width: width as _,
            height: height as _,
        }
    }
}

/// A Vulkan surface generated from SDL2.
pub struct VkSurface<'vk> {
    ptr: NonNull<bind::VkSurfaceKHR_T>,
    _phantom: PhantomData<&'vk VkInstance<'vk>>,
}

impl std::fmt::Debug for VkSurface<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VkSurface").finish_non_exhaustive()
    }
}

impl<'vk> VkSurface<'vk> {
    /// Constructs a surface from instances, or `Err` on failure.
    pub fn new(vk: &'vk VkInstance<'vk>, instance: Instance) -> Result<Self> {
        let mut surface = MaybeUninit::uninit();
        let ret = unsafe {
            bind::SDL_Vulkan_CreateSurface(
                vk.window.as_ptr(),
                instance.as_raw() as *mut _,
                surface.as_mut_ptr(),
            )
        };
        if ret == bind::SDL_FALSE {
            Err(SdlError::UnsupportedFeature)
        } else {
            Ok(Self {
                ptr: NonNull::new(unsafe { surface.assume_init() }).unwrap(),
                _phantom: PhantomData,
            })
        }
    }

    /// Returns the raw pointer to the surface.
    pub fn as_raw_surface(&self) -> *mut c_void {
        self.ptr.as_ptr().cast()
    }
}