1 use crate::prelude::*; 2 use crate::vk; 3 use crate::RawPtr; 4 use crate::{Device, Instance}; 5 use std::ffi::CStr; 6 use std::mem; 7 8 #[derive(Clone)] 9 pub struct Swapchain { 10 handle: vk::Device, 11 swapchain_fn: vk::KhrSwapchainFn, 12 } 13 14 impl Swapchain { new(instance: &Instance, device: &Device) -> Self15 pub fn new(instance: &Instance, device: &Device) -> Self { 16 let swapchain_fn = vk::KhrSwapchainFn::load(|name| unsafe { 17 mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) 18 }); 19 Self { 20 handle: device.handle(), 21 swapchain_fn, 22 } 23 } 24 name() -> &'static CStr25 pub fn name() -> &'static CStr { 26 vk::KhrSwapchainFn::name() 27 } 28 29 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkDestroySwapchainKHR.html>"] destroy_swapchain( &self, swapchain: vk::SwapchainKHR, allocation_callbacks: Option<&vk::AllocationCallbacks>, )30 pub unsafe fn destroy_swapchain( 31 &self, 32 swapchain: vk::SwapchainKHR, 33 allocation_callbacks: Option<&vk::AllocationCallbacks>, 34 ) { 35 self.swapchain_fn.destroy_swapchain_khr( 36 self.handle, 37 swapchain, 38 allocation_callbacks.as_raw_ptr(), 39 ); 40 } 41 42 /// On success, returns the next image's index and whether the swapchain is suboptimal for the surface. 43 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkAcquireNextImageKHR.html>"] acquire_next_image( &self, swapchain: vk::SwapchainKHR, timeout: u64, semaphore: vk::Semaphore, fence: vk::Fence, ) -> VkResult<(u32, bool)>44 pub unsafe fn acquire_next_image( 45 &self, 46 swapchain: vk::SwapchainKHR, 47 timeout: u64, 48 semaphore: vk::Semaphore, 49 fence: vk::Fence, 50 ) -> VkResult<(u32, bool)> { 51 let mut index = 0; 52 let err_code = self.swapchain_fn.acquire_next_image_khr( 53 self.handle, 54 swapchain, 55 timeout, 56 semaphore, 57 fence, 58 &mut index, 59 ); 60 match err_code { 61 vk::Result::SUCCESS => Ok((index, false)), 62 vk::Result::SUBOPTIMAL_KHR => Ok((index, true)), 63 _ => Err(err_code), 64 } 65 } 66 67 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCreateSwapchainKHR.html>"] create_swapchain( &self, create_info: &vk::SwapchainCreateInfoKHR, allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult<vk::SwapchainKHR>68 pub unsafe fn create_swapchain( 69 &self, 70 create_info: &vk::SwapchainCreateInfoKHR, 71 allocation_callbacks: Option<&vk::AllocationCallbacks>, 72 ) -> VkResult<vk::SwapchainKHR> { 73 let mut swapchain = mem::zeroed(); 74 self.swapchain_fn 75 .create_swapchain_khr( 76 self.handle, 77 create_info, 78 allocation_callbacks.as_raw_ptr(), 79 &mut swapchain, 80 ) 81 .result_with_success(swapchain) 82 } 83 84 /// On success, returns whether the swapchain is suboptimal for the surface. 85 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkQueuePresentKHR.html>"] queue_present( &self, queue: vk::Queue, create_info: &vk::PresentInfoKHR, ) -> VkResult<bool>86 pub unsafe fn queue_present( 87 &self, 88 queue: vk::Queue, 89 create_info: &vk::PresentInfoKHR, 90 ) -> VkResult<bool> { 91 let err_code = self.swapchain_fn.queue_present_khr(queue, create_info); 92 match err_code { 93 vk::Result::SUCCESS => Ok(false), 94 vk::Result::SUBOPTIMAL_KHR => Ok(true), 95 _ => Err(err_code), 96 } 97 } 98 99 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetSwapchainImagesKHR.html>"] get_swapchain_images( &self, swapchain: vk::SwapchainKHR, ) -> VkResult<Vec<vk::Image>>100 pub unsafe fn get_swapchain_images( 101 &self, 102 swapchain: vk::SwapchainKHR, 103 ) -> VkResult<Vec<vk::Image>> { 104 read_into_uninitialized_vector(|count, data| { 105 self.swapchain_fn 106 .get_swapchain_images_khr(self.handle, swapchain, count, data) 107 }) 108 } 109 fp(&self) -> &vk::KhrSwapchainFn110 pub fn fp(&self) -> &vk::KhrSwapchainFn { 111 &self.swapchain_fn 112 } 113 device(&self) -> vk::Device114 pub fn device(&self) -> vk::Device { 115 self.handle 116 } 117 } 118