From d2df2e8ac3cc81c11898688f59d8a3c5712af0ea Mon Sep 17 00:00:00 2001 From: maik klein Date: Fri, 23 Dec 2016 00:00:50 +0100 Subject: [PATCH] First implementation of the new swapchain loader --- src/device.rs | 139 +++++++++++++++++++++++++++--------------------- src/instance.rs | 4 +- src/vk.rs | 28 +++++----- 3 files changed, 97 insertions(+), 74 deletions(-) diff --git a/src/device.rs b/src/device.rs index cf88520..23e4aae 100644 --- a/src/device.rs +++ b/src/device.rs @@ -2,21 +2,97 @@ use prelude::*; use std::ptr; use std::mem; +use instance::Instance; use vk; -unsafe impl Sync for Device{} -unsafe impl Send for Device{} +unsafe impl Sync for Device {} +unsafe impl Send for Device {} pub struct Device { handle: vk::Device, device_fn: vk::DeviceFn, } -pub struct Swapchain{ +pub struct Swapchain { + handle: vk::Device, + swapchain_fn: vk::SwapchainFn, +} +impl Swapchain { + pub fn destroy_swapchain_khr(&self, swapchain: vk::SwapchainKHR) { + unsafe { + self.swapchain_fn.destroy_swapchain_khr(self.handle, swapchain, ptr::null()); + } + } + pub fn acquire_next_image_khr(&self, + swapchain: vk::SwapchainKHR, + timeout: vk::uint64_t, + semaphore: vk::Semaphore, + fence: vk::Fence) + -> VkResult { + unsafe { + let mut index = mem::uninitialized(); + let err_code = self.swapchain_fn + .acquire_next_image_khr(self.handle, + swapchain, + timeout, + semaphore, + fence, + &mut index); + match err_code { + vk::Result::Success => Ok(index), + _ => Err(err_code), + } + } + } + + pub fn create_swapchain_khr(&self, + create_info: &vk::SwapchainCreateInfoKHR) + -> VkResult { + unsafe { + let mut swapchain = mem::uninitialized(); + let err_code = self.swapchain_fn + .create_swapchain_khr(self.handle, create_info, ptr::null(), &mut swapchain); + match err_code { + vk::Result::Success => Ok(swapchain), + _ => Err(err_code), + } + } + } + pub fn get_swapchain_images_khr(&self, + swapchain: vk::SwapchainKHR) + -> VkResult> { + unsafe { + let mut count = 0; + self.swapchain_fn + .get_swapchain_images_khr(self.handle, swapchain, &mut count, ptr::null_mut()); + + let mut v = Vec::with_capacity(count as vk::size_t); + let err_code = self.swapchain_fn + .get_swapchain_images_khr(self.handle, swapchain, &mut count, v.as_mut_ptr()); + v.set_len(count as vk::size_t); + match err_code { + vk::Result::Success => Ok(v), + _ => Err(err_code), + } + } + } } impl Device { + pub fn load_swapchain(&self, instance: &Instance) -> Swapchain { + let swapchain_fn = vk::SwapchainFn::load(|name| { + unsafe { + mem::transmute(instance.instance_fn + .get_device_proc_addr(self.handle, name.as_ptr())) + } + }) + .unwrap(); + Swapchain { + handle: self.handle, + swapchain_fn: swapchain_fn, + } + } pub unsafe fn from_raw(handle: vk::Device, device_fn: vk::DeviceFn) -> Self { Device { handle: handle, @@ -60,11 +136,6 @@ impl Device { } } - pub fn destroy_swapchain_khr(&self, swapchain: vk::SwapchainKHR) { - unsafe { - self.device_fn.destroy_swapchain_khr(self.handle, swapchain, ptr::null()); - } - } pub fn destroy_image_view(&self, image_view: vk::ImageView) { unsafe { @@ -404,27 +475,6 @@ impl Device { } } - pub fn acquire_next_image_khr(&self, - swapchain: vk::SwapchainKHR, - timeout: vk::uint64_t, - semaphore: vk::Semaphore, - fence: vk::Fence) - -> VkResult { - unsafe { - let mut index = mem::uninitialized(); - let err_code = self.device_fn - .acquire_next_image_khr(self.handle, - swapchain, - timeout, - semaphore, - fence, - &mut index); - match err_code { - vk::Result::Success => Ok(index), - _ => Err(err_code), - } - } - } pub fn create_semaphore(&self, create_info: &vk::SemaphoreCreateInfo) @@ -679,24 +729,6 @@ impl Device { } } - pub fn get_swapchain_images_khr(&self, - swapchain: vk::SwapchainKHR) - -> VkResult> { - unsafe { - let mut count = 0; - self.device_fn - .get_swapchain_images_khr(self.handle, swapchain, &mut count, ptr::null_mut()); - - let mut v = Vec::with_capacity(count as vk::size_t); - let err_code = self.device_fn - .get_swapchain_images_khr(self.handle, swapchain, &mut count, v.as_mut_ptr()); - v.set_len(count as vk::size_t); - match err_code { - vk::Result::Success => Ok(v), - _ => Err(err_code), - } - } - } pub fn allocate_command_buffers(&self, create_info: &vk::CommandBufferAllocateInfo) @@ -727,19 +759,6 @@ impl Device { } } - pub fn create_swapchain_khr(&self, - create_info: &vk::SwapchainCreateInfoKHR) - -> VkResult { - unsafe { - let mut swapchain = mem::uninitialized(); - let err_code = self.device_fn - .create_swapchain_khr(self.handle, create_info, ptr::null(), &mut swapchain); - match err_code { - vk::Result::Success => Ok(swapchain), - _ => Err(err_code), - } - } - } pub fn create_image(&self, create_info: &vk::ImageCreateInfo) -> VkResult { unsafe { diff --git a/src/instance.rs b/src/instance.rs index 4e540fa..46d9931 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -14,8 +14,8 @@ pub enum DeviceError { #[derive(Debug)] pub struct Instance { - handle: vk::Instance, - instance_fn: vk::InstanceFn, + pub handle: vk::Instance, + pub instance_fn: vk::InstanceFn, } impl Instance { diff --git a/src/vk.rs b/src/vk.rs index 6ae2cbb..22c6d9b 100644 --- a/src/vk.rs +++ b/src/vk.rs @@ -3939,13 +3939,6 @@ vk_functions!{ - "vkCreateSharedSwapchainsKHR", create_shared_swapchains_khr( - device: Device, - swapchain_count: uint32_t, - p_create_infos: *const SwapchainCreateInfoKHR, - p_allocator: *const AllocationCallbacks, - p_swapchains: *mut SwapchainKHR, - ) -> Result; "vkGetPhysicalDeviceDisplayPropertiesKHR", get_physical_device_display_properties_khr( physical_device: PhysicalDevice, @@ -4849,6 +4842,22 @@ vk_functions!{ command_buffer_count: uint32_t, p_command_buffers: *const CommandBuffer, ) -> (); + + "vkQueuePresentKHR", queue_present_khr( + queue: Queue, + p_present_info: *const PresentInfoKHR, + ) -> Result; +} +vk_functions!{ + SwapchainFn, + "vkCreateSharedSwapchainsKHR", create_shared_swapchains_khr( + device: Device, + swapchain_count: uint32_t, + p_create_infos: *const SwapchainCreateInfoKHR, + p_allocator: *const AllocationCallbacks, + p_swapchains: *mut SwapchainKHR, + ) -> Result; + "vkCreateSwapchainKHR", create_swapchain_khr( device: Device, p_create_info: *const SwapchainCreateInfoKHR, @@ -4877,10 +4886,5 @@ vk_functions!{ fence: Fence, p_image_index: *mut uint32_t, ) -> Result; - - "vkQueuePresentKHR", queue_present_khr( - queue: Queue, - p_present_info: *const PresentInfoKHR, - ) -> Result; } }