From 0531d67e9921bf62d7e7a6488d4774ec655fac52 Mon Sep 17 00:00:00 2001 From: maik klein Date: Wed, 4 Jan 2017 19:09:27 +0100 Subject: [PATCH] Refactor Entry into EntryV1_0 --- src/entry.rs | 99 +++++++++++++++++++-------------- src/extensions/debug_report.rs | 6 +- src/extensions/surface.rs | 15 +++-- src/extensions/win32_surface.rs | 6 +- src/extensions/xlib_surface.rs | 2 +- src/version.rs | 1 + 6 files changed, 76 insertions(+), 53 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index b5da70b..04e8cce 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -47,6 +47,62 @@ pub enum InstanceError { LoadError(Vec<&'static str>), VkError(vk::Result), } +pub trait EntryV1_0{ + fn fp_v1_0(&self) -> &vk::EntryFnV1_0; + fn static_fn(&self) -> &vk::StaticFn; + fn enumerate_instance_layer_properties(&self) -> VkResult> { + unsafe { + let mut num = 0; + self.fp_v1_0().enumerate_instance_layer_properties(&mut num, ptr::null_mut()); + + let mut v = Vec::with_capacity(num as usize); + let err_code = self + .fp_v1_0() + .enumerate_instance_layer_properties(&mut num, v.as_mut_ptr()); + v.set_len(num as usize); + match err_code { + vk::Result::Success => Ok(v), + _ => Err(err_code), + } + } + } + + fn enumerate_instance_extension_properties(&self) + -> VkResult> { + unsafe { + let mut num = 0; + self + .fp_v1_0() + .enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut()); + let mut data = Vec::with_capacity(num as usize); + let err_code = self + .fp_v1_0() + .enumerate_instance_extension_properties(ptr::null(), &mut num, data.as_mut_ptr()); + data.set_len(num as usize); + match err_code { + vk::Result::Success => Ok(data), + _ => Err(err_code), + } + } + } + + fn get_instance_proc_addr(&self, + instance: vk::Instance, + p_name: *const vk::c_char) + -> vk::PFN_vkVoidFunction { + unsafe { self.static_fn().get_instance_proc_addr(instance, p_name) } + } +} + +impl EntryV1_0 for Entry{ + fn fp_v1_0(&self) -> &vk::EntryFnV1_0{ + self.entry_fn.fp_v1_0() + } + fn static_fn(&self) -> &vk::StaticFn{ + &self.static_fn + } +} + impl Entry { pub fn create_instance(&self, create_info: &vk::InstanceCreateInfo, @@ -87,47 +143,4 @@ impl Entry { _v: PhantomData, }) } - - pub fn enumerate_instance_layer_properties(&self) -> VkResult> { - unsafe { - let mut num = 0; - self.entry_fn.fp_v1_0().enumerate_instance_layer_properties(&mut num, ptr::null_mut()); - - let mut v = Vec::with_capacity(num as usize); - let err_code = self.entry_fn - .fp_v1_0() - .enumerate_instance_layer_properties(&mut num, v.as_mut_ptr()); - v.set_len(num as usize); - match err_code { - vk::Result::Success => Ok(v), - _ => Err(err_code), - } - } - } - - pub fn enumerate_instance_extension_properties(&self) - -> VkResult> { - unsafe { - let mut num = 0; - self.entry_fn - .fp_v1_0() - .enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut()); - let mut data = Vec::with_capacity(num as usize); - let err_code = self.entry_fn - .fp_v1_0() - .enumerate_instance_extension_properties(ptr::null(), &mut num, data.as_mut_ptr()); - data.set_len(num as usize); - match err_code { - vk::Result::Success => Ok(data), - _ => Err(err_code), - } - } - } - - pub fn get_instance_proc_addr(&self, - instance: vk::Instance, - p_name: *const vk::c_char) - -> vk::PFN_vkVoidFunction { - unsafe { self.static_fn.get_instance_proc_addr(instance, p_name) } - } } diff --git a/src/extensions/debug_report.rs b/src/extensions/debug_report.rs index 6a39949..024c5ae 100644 --- a/src/extensions/debug_report.rs +++ b/src/extensions/debug_report.rs @@ -5,7 +5,7 @@ use entry::Entry; use vk; use std::ffi::CStr; use ::RawPtr; -use version::{V1_0, InstanceFpV1_0}; +use version::{V1_0, EntryV1_0}; #[derive(Clone)] pub struct DebugReport { @@ -14,7 +14,9 @@ pub struct DebugReport { } impl DebugReport { - pub fn new(entry: &Entry, instance: &Instance) -> Result> { + pub fn new(entry: &Entry, + instance: &Instance) + -> Result> { let debug_report_fn = vk::DebugReportFn::load(|name| { unsafe { mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) diff --git a/src/extensions/surface.rs b/src/extensions/surface.rs index ba8d2e1..e4a6193 100644 --- a/src/extensions/surface.rs +++ b/src/extensions/surface.rs @@ -7,7 +7,7 @@ use entry::Entry; use vk; use std::ffi::CStr; use ::RawPtr; -use version::{V1_0}; +use version::{V1_0, EntryV1_0}; #[derive(Clone)] pub struct Surface { @@ -16,7 +16,9 @@ pub struct Surface { } impl Surface { - pub fn new(entry: &Entry, instance: &Instance) -> Result> { + pub fn new(entry: &Entry, + instance: &Instance) + -> Result> { let surface_fn = vk::SurfaceFn::load(|name| { unsafe { mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) @@ -28,7 +30,7 @@ impl Surface { }) } - pub fn name() -> &'static CStr{ + pub fn name() -> &'static CStr { CStr::from_bytes_with_nul(b"VK_KHR_surface\0").expect("Wrong extension string") } @@ -112,7 +114,10 @@ impl Surface { } } - pub unsafe fn destroy_surface_khr(&self, surface: vk::SurfaceKHR, allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.surface_fn.destroy_surface_khr(self.handle, surface, allocation_callbacks.as_raw_ptr()); + pub unsafe fn destroy_surface_khr(&self, + surface: vk::SurfaceKHR, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.surface_fn + .destroy_surface_khr(self.handle, surface, allocation_callbacks.as_raw_ptr()); } } diff --git a/src/extensions/win32_surface.rs b/src/extensions/win32_surface.rs index 386b454..a7b3a5c 100644 --- a/src/extensions/win32_surface.rs +++ b/src/extensions/win32_surface.rs @@ -7,7 +7,7 @@ use vk; use std::ffi::CStr; use ::RawPtr; use instance::InstanceV1_0; -use version::{V1_0}; +use version::{V1_0, EntryV1_0}; #[derive(Clone)] pub struct Win32Surface { @@ -16,7 +16,9 @@ pub struct Win32Surface { } impl Win32Surface { - pub fn new(entry: &Entry, instance: &Instance) -> Result> { + pub fn new(entry: &Entry, + instance: &Instance) + -> Result> { let surface_fn = vk::Win32SurfaceFn::load(|name| { unsafe { mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) diff --git a/src/extensions/xlib_surface.rs b/src/extensions/xlib_surface.rs index c1e84c6..c5d9779 100644 --- a/src/extensions/xlib_surface.rs +++ b/src/extensions/xlib_surface.rs @@ -7,7 +7,7 @@ use vk; use std::ffi::CStr; use ::RawPtr; use instance::InstanceV1_0; -use version::{V1_0}; +use version::{V1_0, EntryV1_0}; #[derive(Clone)] pub struct XlibSurface { diff --git a/src/version.rs b/src/version.rs index 7608961..d811750 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,6 +1,7 @@ use vk; pub use instance::InstanceV1_0; pub use device::DeviceV1_0; +pub use entry::EntryV1_0; use std::mem; pub trait FunctionPointers { type InstanceFp: InstanceLoader;