ash/extensions/khr/
surface.rs

1use crate::prelude::*;
2use crate::vk;
3use crate::RawPtr;
4use crate::{Entry, Instance};
5use std::ffi::CStr;
6use std::mem;
7
8#[derive(Clone)]
9pub struct Surface {
10    handle: vk::Instance,
11    fp: vk::KhrSurfaceFn,
12}
13
14impl Surface {
15    pub fn new(entry: &Entry, instance: &Instance) -> Self {
16        let handle = instance.handle();
17        let fp = vk::KhrSurfaceFn::load(|name| unsafe {
18            mem::transmute(entry.get_instance_proc_addr(handle, name.as_ptr()))
19        });
20        Self { handle, fp }
21    }
22
23    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfaceSupportKHR.html>
24    #[inline]
25    pub unsafe fn get_physical_device_surface_support(
26        &self,
27        physical_device: vk::PhysicalDevice,
28        queue_family_index: u32,
29        surface: vk::SurfaceKHR,
30    ) -> VkResult<bool> {
31        let mut b = 0;
32        (self.fp.get_physical_device_surface_support_khr)(
33            physical_device,
34            queue_family_index,
35            surface,
36            &mut b,
37        )
38        .result_with_success(b > 0)
39    }
40
41    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfacePresentModesKHR.html>
42    #[inline]
43    pub unsafe fn get_physical_device_surface_present_modes(
44        &self,
45        physical_device: vk::PhysicalDevice,
46        surface: vk::SurfaceKHR,
47    ) -> VkResult<Vec<vk::PresentModeKHR>> {
48        read_into_uninitialized_vector(|count, data| {
49            (self.fp.get_physical_device_surface_present_modes_khr)(
50                physical_device,
51                surface,
52                count,
53                data,
54            )
55        })
56    }
57
58    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfaceCapabilitiesKHR.html>
59    #[inline]
60    pub unsafe fn get_physical_device_surface_capabilities(
61        &self,
62        physical_device: vk::PhysicalDevice,
63        surface: vk::SurfaceKHR,
64    ) -> VkResult<vk::SurfaceCapabilitiesKHR> {
65        let mut surface_capabilities = mem::zeroed();
66        (self.fp.get_physical_device_surface_capabilities_khr)(
67            physical_device,
68            surface,
69            &mut surface_capabilities,
70        )
71        .result_with_success(surface_capabilities)
72    }
73
74    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfaceFormatsKHR.html>
75    #[inline]
76    pub unsafe fn get_physical_device_surface_formats(
77        &self,
78        physical_device: vk::PhysicalDevice,
79        surface: vk::SurfaceKHR,
80    ) -> VkResult<Vec<vk::SurfaceFormatKHR>> {
81        read_into_uninitialized_vector(|count, data| {
82            (self.fp.get_physical_device_surface_formats_khr)(physical_device, surface, count, data)
83        })
84    }
85
86    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkDestroySurfaceKHR.html>
87    #[inline]
88    pub unsafe fn destroy_surface(
89        &self,
90        surface: vk::SurfaceKHR,
91        allocation_callbacks: Option<&vk::AllocationCallbacks>,
92    ) {
93        (self.fp.destroy_surface_khr)(self.handle, surface, allocation_callbacks.as_raw_ptr());
94    }
95
96    #[inline]
97    pub const fn name() -> &'static CStr {
98        vk::KhrSurfaceFn::name()
99    }
100
101    #[inline]
102    pub fn fp(&self) -> &vk::KhrSurfaceFn {
103        &self.fp
104    }
105
106    #[inline]
107    pub fn instance(&self) -> vk::Instance {
108        self.handle
109    }
110}