From 601f5573cfdfc34fb39b8468c2dfb1ecc33508a8 Mon Sep 17 00:00:00 2001 From: maik klein Date: Fri, 23 Dec 2016 02:33:05 +0100 Subject: [PATCH] Surface loader --- src/device.rs | 3 + src/entry.rs | 4 +- src/instance.rs | 192 ++++++++++++++++++++++++++---------------------- src/vk.rs | 67 +++++++++-------- 4 files changed, 146 insertions(+), 120 deletions(-) diff --git a/src/device.rs b/src/device.rs index 23e4aae..a3519f0 100644 --- a/src/device.rs +++ b/src/device.rs @@ -17,6 +17,7 @@ pub struct Swapchain { handle: vk::Device, swapchain_fn: vk::SwapchainFn, } + impl Swapchain { pub fn destroy_swapchain_khr(&self, swapchain: vk::SwapchainKHR) { unsafe { @@ -59,6 +60,7 @@ impl Swapchain { } } } + pub fn get_swapchain_images_khr(&self, swapchain: vk::SwapchainKHR) -> VkResult> { @@ -93,6 +95,7 @@ impl Device { swapchain_fn: swapchain_fn, } } + pub unsafe fn from_raw(handle: vk::Device, device_fn: vk::DeviceFn) -> Self { Device { handle: handle, diff --git a/src/entry.rs b/src/entry.rs index 6aae2e0..ddb766b 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -23,8 +23,8 @@ fn get_path() -> &'static Path { pub struct Entry { lib: DynamicLibrary, - static_fn: vk::StaticFn, - entry_fn: vk::EntryFn, + pub static_fn: vk::StaticFn, + pub entry_fn: vk::EntryFn, } #[derive(Debug)] diff --git a/src/instance.rs b/src/instance.rs index 46d9931..3be02ec 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -4,6 +4,7 @@ use std::ptr; use std::mem; use vk; use device::Device; +use entry::Entry; use shared_library::dynamic_library::DynamicLibrary; #[derive(Debug)] @@ -18,7 +19,112 @@ pub struct Instance { pub instance_fn: vk::InstanceFn, } +pub struct Surface { + pub handle: vk::Instance, + pub surface_fn: vk::SurfaceFn, +} +impl Surface { + pub fn get_physical_device_surface_support_khr(&self, + physical_device: vk::PhysicalDevice, + queue_index: vk::uint32_t, + surface: vk::SurfaceKHR) + -> bool { + unsafe { + let mut b = mem::uninitialized(); + self.surface_fn + .get_physical_device_surface_support_khr(physical_device, + queue_index, + surface, + &mut b); + b > 0 + } + } + pub fn get_physical_device_surface_present_modes_khr(&self, + physical_device: vk::PhysicalDevice, + surface: vk::SurfaceKHR) + -> VkResult> { + unsafe { + let mut count = 0; + self.surface_fn.get_physical_device_surface_present_modes_khr(physical_device, + surface, + &mut count, + ptr::null_mut()); + let mut v = Vec::with_capacity(count as usize); + let err_code = self.surface_fn + .get_physical_device_surface_present_modes_khr(physical_device, + surface, + &mut count, + v.as_mut_ptr()); + v.set_len(count as usize); + match err_code { + vk::Result::Success => Ok(v), + _ => Err(err_code), + } + } + } + + pub fn get_physical_device_surface_capabilities_khr(&self, + physical_device: vk::PhysicalDevice, + surface: vk::SurfaceKHR) + -> VkResult { + unsafe { + let mut surface_capabilities = mem::uninitialized(); + let err_code = self.surface_fn + .get_physical_device_surface_capabilities_khr(physical_device, + surface, + &mut surface_capabilities); + match err_code { + vk::Result::Success => Ok(surface_capabilities), + _ => Err(err_code), + } + } + } + + pub fn get_physical_device_surface_formats_khr(&self, + physical_device: vk::PhysicalDevice, + surface: vk::SurfaceKHR) + -> VkResult> { + unsafe { + let mut count = 0; + self.surface_fn.get_physical_device_surface_formats_khr(physical_device, + surface, + &mut count, + ptr::null_mut()); + let mut v = Vec::with_capacity(count as usize); + let err_code = self.surface_fn + .get_physical_device_surface_formats_khr(physical_device, + surface, + &mut count, + v.as_mut_ptr()); + v.set_len(count as usize); + match err_code { + vk::Result::Success => Ok(v), + _ => Err(err_code), + } + } + } + + pub fn destroy_surface_khr(&self, surface: vk::SurfaceKHR) { + unsafe { + self.surface_fn.destroy_surface_khr(self.handle, surface, ptr::null()); + } + } +} + impl Instance { + pub fn load_surface(&self, entry: &Entry) -> Surface { + let surface_fn = vk::SurfaceFn::load(|name| { + unsafe { + mem::transmute(entry.static_fn + .get_instance_proc_addr(self.handle, name.as_ptr())) + } + }) + .unwrap(); + Surface { + handle: self.handle, + surface_fn: surface_fn, + } + } pub unsafe fn from_raw(handle: vk::Instance, instance_fn: vk::InstanceFn) -> Self { Instance { handle: handle, @@ -95,77 +201,6 @@ impl Instance { } } - pub fn get_physical_device_surface_present_modes_khr(&self, - physical_device: vk::PhysicalDevice, - surface: vk::SurfaceKHR) - -> VkResult> { - unsafe { - let mut count = 0; - self.instance_fn.get_physical_device_surface_present_modes_khr(physical_device, - surface, - &mut count, - ptr::null_mut()); - let mut v = Vec::with_capacity(count as usize); - let err_code = self.instance_fn - .get_physical_device_surface_present_modes_khr(physical_device, - surface, - &mut count, - v.as_mut_ptr()); - v.set_len(count as usize); - match err_code { - vk::Result::Success => Ok(v), - _ => Err(err_code), - } - } - } - - pub fn get_physical_device_surface_capabilities_khr(&self, - physical_device: vk::PhysicalDevice, - surface: vk::SurfaceKHR) - -> VkResult { - unsafe { - let mut surface_capabilities = mem::uninitialized(); - let err_code = self.instance_fn - .get_physical_device_surface_capabilities_khr(physical_device, - surface, - &mut surface_capabilities); - match err_code { - vk::Result::Success => Ok(surface_capabilities), - _ => Err(err_code), - } - } - } - - pub fn get_physical_device_surface_formats_khr(&self, - physical_device: vk::PhysicalDevice, - surface: vk::SurfaceKHR) - -> VkResult> { - unsafe { - let mut count = 0; - self.instance_fn.get_physical_device_surface_formats_khr(physical_device, - surface, - &mut count, - ptr::null_mut()); - let mut v = Vec::with_capacity(count as usize); - let err_code = self.instance_fn - .get_physical_device_surface_formats_khr(physical_device, - surface, - &mut count, - v.as_mut_ptr()); - v.set_len(count as usize); - match err_code { - vk::Result::Success => Ok(v), - _ => Err(err_code), - } - } - } - - pub fn destroy_surface_khr(&self, surface: vk::SurfaceKHR) { - unsafe { - self.instance_fn.destroy_surface_khr(self.handle, surface, ptr::null()); - } - } - pub fn create_xlib_surface_khr(&self, create_info: &vk::XlibSurfaceCreateInfoKHR) -> VkResult { @@ -181,21 +216,6 @@ impl Instance { } - pub fn get_physical_device_surface_support_khr(&self, - physical_device: vk::PhysicalDevice, - queue_index: vk::uint32_t, - surface: vk::SurfaceKHR) - -> bool { - unsafe { - let mut b = mem::uninitialized(); - self.instance_fn - .get_physical_device_surface_support_khr(physical_device, - queue_index, - surface, - &mut b); - b > 0 - } - } pub fn get_physical_device_queue_family_properties(&self, physical_device: vk::PhysicalDevice) diff --git a/src/vk.rs b/src/vk.rs index 22c6d9b..653ae93 100644 --- a/src/vk.rs +++ b/src/vk.rs @@ -3831,38 +3831,6 @@ vk_functions!{ p_properties: *mut SparseImageFormatProperties, ) -> (); - "vkDestroySurfaceKHR", destroy_surface_khr( - instance: Instance, - surface: SurfaceKHR, - p_allocator: *const AllocationCallbacks, - ) -> (); - - "vkGetPhysicalDeviceSurfaceSupportKHR", get_physical_device_surface_support_khr( - physical_device: PhysicalDevice, - queue_family_index: uint32_t, - surface: SurfaceKHR, - p_supported: *mut Bool32, - ) -> Result; - - "vkGetPhysicalDeviceSurfaceCapabilitiesKHR", get_physical_device_surface_capabilities_khr( - physical_device: PhysicalDevice, - surface: SurfaceKHR, - p_surface_capabilities: *mut SurfaceCapabilitiesKHR, - ) -> Result; - - "vkGetPhysicalDeviceSurfaceFormatsKHR", get_physical_device_surface_formats_khr( - physical_device: PhysicalDevice, - surface: SurfaceKHR, - p_surface_format_count: *mut uint32_t, - p_surface_formats: *mut SurfaceFormatKHR, - ) -> Result; - - "vkGetPhysicalDeviceSurfacePresentModesKHR", get_physical_device_surface_present_modes_khr( - physical_device: PhysicalDevice, - surface: SurfaceKHR, - p_present_mode_count: *mut uint32_t, - p_present_modes: *mut PresentModeKHR, - ) -> Result; "vkGetPhysicalDeviceXlibPresentationSupportKHR", get_physical_device_xlib_presentation_support_khr( physical_device: PhysicalDevice, @@ -4887,4 +4855,39 @@ vk_functions!{ p_image_index: *mut uint32_t, ) -> Result; } +vk_functions!{ + SurfaceFn, + "vkDestroySurfaceKHR", destroy_surface_khr( + instance: Instance, + surface: SurfaceKHR, + p_allocator: *const AllocationCallbacks, + ) -> (); + + "vkGetPhysicalDeviceSurfaceSupportKHR", get_physical_device_surface_support_khr( + physical_device: PhysicalDevice, + queue_family_index: uint32_t, + surface: SurfaceKHR, + p_supported: *mut Bool32, + ) -> Result; + + "vkGetPhysicalDeviceSurfaceCapabilitiesKHR", get_physical_device_surface_capabilities_khr( + physical_device: PhysicalDevice, + surface: SurfaceKHR, + p_surface_capabilities: *mut SurfaceCapabilitiesKHR, + ) -> Result; + + "vkGetPhysicalDeviceSurfaceFormatsKHR", get_physical_device_surface_formats_khr( + physical_device: PhysicalDevice, + surface: SurfaceKHR, + p_surface_format_count: *mut uint32_t, + p_surface_formats: *mut SurfaceFormatKHR, + ) -> Result; + + "vkGetPhysicalDeviceSurfacePresentModesKHR", get_physical_device_surface_present_modes_khr( + physical_device: PhysicalDevice, + surface: SurfaceKHR, + p_present_mode_count: *mut uint32_t, + p_present_modes: *mut PresentModeKHR, + ) -> Result; +} }