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    pub unsafe fn get_physical_device_surface_support(
25        &self,
26        physical_device: vk::PhysicalDevice,
27        queue_family_index: u32,
28        surface: vk::SurfaceKHR,
29    ) -> VkResult<bool> {
30        let mut b = 0;
31        (self.fp.get_physical_device_surface_support_khr)(
32            physical_device,
33            queue_family_index,
34            surface,
35            &mut b,
36        )
37        .result_with_success(b > 0)
38    }
39
40    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfacePresentModesKHR.html>
41    pub unsafe fn get_physical_device_surface_present_modes(
42        &self,
43        physical_device: vk::PhysicalDevice,
44        surface: vk::SurfaceKHR,
45    ) -> VkResult<Vec<vk::PresentModeKHR>> {
46        read_into_uninitialized_vector(|count, data| {
47            (self.fp.get_physical_device_surface_present_modes_khr)(
48                physical_device,
49                surface,
50                count,
51                data,
52            )
53        })
54    }
55
56    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfaceCapabilitiesKHR.html>
57    pub unsafe fn get_physical_device_surface_capabilities(
58        &self,
59        physical_device: vk::PhysicalDevice,
60        surface: vk::SurfaceKHR,
61    ) -> VkResult<vk::SurfaceCapabilitiesKHR> {
62        let mut surface_capabilities = mem::zeroed();
63        (self.fp.get_physical_device_surface_capabilities_khr)(
64            physical_device,
65            surface,
66            &mut surface_capabilities,
67        )
68        .result_with_success(surface_capabilities)
69    }
70
71    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfaceFormatsKHR.html>
72    pub unsafe fn get_physical_device_surface_formats(
73        &self,
74        physical_device: vk::PhysicalDevice,
75        surface: vk::SurfaceKHR,
76    ) -> VkResult<Vec<vk::SurfaceFormatKHR>> {
77        read_into_uninitialized_vector(|count, data| {
78            (self.fp.get_physical_device_surface_formats_khr)(physical_device, surface, count, data)
79        })
80    }
81
82    /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkDestroySurfaceKHR.html>
83    pub unsafe fn destroy_surface(
84        &self,
85        surface: vk::SurfaceKHR,
86        allocation_callbacks: Option<&vk::AllocationCallbacks>,
87    ) {
88        (self.fp.destroy_surface_khr)(self.handle, surface, allocation_callbacks.as_raw_ptr());
89    }
90
91    pub const fn name() -> &'static CStr {
92        vk::KhrSurfaceFn::name()
93    }
94
95    pub fn fp(&self) -> &vk::KhrSurfaceFn {
96        &self.fp
97    }
98
99    pub fn instance(&self) -> vk::Instance {
100        self.handle
101    }
102}