ash/extensions/khr/
swapchain.rs

1//! <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_swapchain.html>
2
3#[cfg(doc)]
4use crate::khr;
5use crate::prelude::*;
6use crate::vk;
7use crate::RawPtr;
8use alloc::vec::Vec;
9use core::mem;
10
11impl crate::khr::swapchain::Device {
12    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCreateSwapchainKHR.html>
13    #[inline]
14    pub unsafe fn create_swapchain(
15        &self,
16        create_info: &vk::SwapchainCreateInfoKHR<'_>,
17        allocation_callbacks: Option<&vk::AllocationCallbacks<'_>>,
18    ) -> VkResult<vk::SwapchainKHR> {
19        let mut swapchain = mem::MaybeUninit::uninit();
20        (self.fp.create_swapchain_khr)(
21            self.handle,
22            create_info,
23            allocation_callbacks.as_raw_ptr(),
24            swapchain.as_mut_ptr(),
25        )
26        .assume_init_on_success(swapchain)
27    }
28
29    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkDestroySwapchainKHR.html>
30    #[inline]
31    pub unsafe fn destroy_swapchain(
32        &self,
33        swapchain: vk::SwapchainKHR,
34        allocation_callbacks: Option<&vk::AllocationCallbacks<'_>>,
35    ) {
36        (self.fp.destroy_swapchain_khr)(self.handle, swapchain, allocation_callbacks.as_raw_ptr());
37    }
38
39    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkGetSwapchainImagesKHR.html>
40    #[inline]
41    pub unsafe fn get_swapchain_images(
42        &self,
43        swapchain: vk::SwapchainKHR,
44    ) -> VkResult<Vec<vk::Image>> {
45        read_into_uninitialized_vector(|count, data| {
46            (self.fp.get_swapchain_images_khr)(self.handle, swapchain, count, data)
47        })
48    }
49
50    /// On success, returns the next image's index and whether the swapchain is suboptimal for the surface.
51    ///
52    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkAcquireNextImageKHR.html>
53    #[inline]
54    pub unsafe fn acquire_next_image(
55        &self,
56        swapchain: vk::SwapchainKHR,
57        timeout: u64,
58        semaphore: vk::Semaphore,
59        fence: vk::Fence,
60    ) -> VkResult<(u32, bool)> {
61        let mut index = mem::MaybeUninit::uninit();
62        let err_code = (self.fp.acquire_next_image_khr)(
63            self.handle,
64            swapchain,
65            timeout,
66            semaphore,
67            fence,
68            index.as_mut_ptr(),
69        );
70        match err_code {
71            vk::Result::SUCCESS => Ok((index.assume_init(), false)),
72            vk::Result::SUBOPTIMAL_KHR => Ok((index.assume_init(), true)),
73            _ => Err(err_code),
74        }
75    }
76
77    /// On success, returns whether the swapchain is suboptimal for the surface.
78    ///
79    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkQueuePresentKHR.html>
80    #[inline]
81    pub unsafe fn queue_present(
82        &self,
83        queue: vk::Queue,
84        present_info: &vk::PresentInfoKHR<'_>,
85    ) -> VkResult<bool> {
86        let err_code = (self.fp.queue_present_khr)(queue, present_info);
87        match err_code {
88            vk::Result::SUCCESS => Ok(false),
89            vk::Result::SUBOPTIMAL_KHR => Ok(true),
90            _ => Err(err_code),
91        }
92    }
93
94    /// Only available since [Vulkan 1.1].
95    ///
96    /// Also available as [`khr::device_group::Device::get_device_group_present_capabilities()`]
97    /// when [`VK_KHR_surface`] is enabled.
98    ///
99    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkGetDeviceGroupPresentCapabilitiesKHR.html>
100    ///
101    /// [Vulkan 1.1]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_VERSION_1_1.html
102    /// [`VK_KHR_surface`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_surface.html
103    #[inline]
104    pub unsafe fn get_device_group_present_capabilities(
105        &self,
106        device_group_present_capabilities: &mut vk::DeviceGroupPresentCapabilitiesKHR<'_>,
107    ) -> VkResult<()> {
108        (self.fp.get_device_group_present_capabilities_khr)(
109            self.handle,
110            device_group_present_capabilities,
111        )
112        .result()
113    }
114
115    /// Only available since [Vulkan 1.1].
116    ///
117    /// Also available as [`khr::device_group::Device::get_device_group_surface_present_modes()`]
118    /// when [`VK_KHR_surface`] is enabled.
119    ///
120    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkGetDeviceGroupSurfacePresentModesKHR.html>
121    ///
122    /// [Vulkan 1.1]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_VERSION_1_1.html
123    /// [`VK_KHR_surface`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_surface.html
124    #[inline]
125    pub unsafe fn get_device_group_surface_present_modes(
126        &self,
127        surface: vk::SurfaceKHR,
128    ) -> VkResult<vk::DeviceGroupPresentModeFlagsKHR> {
129        let mut modes = mem::MaybeUninit::uninit();
130        (self.fp.get_device_group_surface_present_modes_khr)(
131            self.handle,
132            surface,
133            modes.as_mut_ptr(),
134        )
135        .assume_init_on_success(modes)
136    }
137
138    /// On success, returns the next image's index and whether the swapchain is suboptimal for the surface.
139    ///
140    /// Only available since [Vulkan 1.1].
141    ///
142    /// Also available as [`khr::device_group::Device::acquire_next_image2()`]
143    /// when [`VK_KHR_swapchain`] is enabled.
144    ///
145    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkAcquireNextImage2KHR.html>
146    ///
147    /// [Vulkan 1.1]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_VERSION_1_1.html
148    /// [`VK_KHR_swapchain`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_swapchain.html
149    #[inline]
150    pub unsafe fn acquire_next_image2(
151        &self,
152        acquire_info: &vk::AcquireNextImageInfoKHR<'_>,
153    ) -> VkResult<(u32, bool)> {
154        let mut index = mem::MaybeUninit::uninit();
155        let err_code =
156            (self.fp.acquire_next_image2_khr)(self.handle, acquire_info, index.as_mut_ptr());
157        match err_code {
158            vk::Result::SUCCESS => Ok((index.assume_init(), false)),
159            vk::Result::SUBOPTIMAL_KHR => Ok((index.assume_init(), true)),
160            _ => Err(err_code),
161        }
162    }
163}
164
165impl crate::khr::swapchain::Instance {
166    /// Only available since [Vulkan 1.1].
167    ///
168    /// Also available as [`khr::device_group::Instance::get_physical_device_present_rectangles()`]
169    /// when [`VK_KHR_surface`] is enabled.
170    ///
171    /// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDevicePresentRectanglesKHR.html>
172    ///
173    /// [Vulkan 1.1]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_VERSION_1_1.html
174    /// [`VK_KHR_surface`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_surface.html
175    #[inline]
176    pub unsafe fn get_physical_device_present_rectangles(
177        &self,
178        physical_device: vk::PhysicalDevice,
179        surface: vk::SurfaceKHR,
180    ) -> VkResult<Vec<vk::Rect2D>> {
181        read_into_uninitialized_vector(|count, data| {
182            (self.fp.get_physical_device_present_rectangles_khr)(
183                physical_device,
184                surface,
185                count,
186                data,
187            )
188        })
189    }
190}