From 0c0b147502e72324463f4512d5f3665d13900cb6 Mon Sep 17 00:00:00 2001 From: maik klein Date: Fri, 30 Dec 2016 06:19:47 +0100 Subject: [PATCH] Add version to Entry --- examples/src/lib.rs | 6 ++-- src/entry.rs | 50 ++++++++++++++++++--------------- src/extensions/debug_report.rs | 2 +- src/extensions/surface.rs | 2 +- src/extensions/win32_surface.rs | 2 +- src/extensions/xlib_surface.rs | 2 +- 6 files changed, 35 insertions(+), 29 deletions(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 5b92542..6dc0de6 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -17,7 +17,7 @@ use std::ptr; use std::ffi::{CStr, CString}; use std::ops::Drop; pub use ash::instance::{V1_0, InstanceV1_0}; -pub use ash::device::{DeviceV1_0}; +pub use ash::device::DeviceV1_0; // Simple offset_of macro akin to C++ offsetof #[macro_export] @@ -82,7 +82,7 @@ pub fn record_submit_commandbuffer, vk::CommandBuffer)>( #[cfg(all(unix, not(target_os = "android")))] unsafe fn create_surface(instance: &Instance, - entry: &Entry, + entry: &Entry, window: &winit::Window) -> Result { use winit::os::unix::WindowExt; @@ -187,7 +187,7 @@ fn resize_callback(width: u32, height: u32) { } pub struct ExampleBase { - pub entry: Entry, + pub entry: Entry, pub instance: Instance, pub device: Device, pub surface_loader: Surface, diff --git a/src/entry.rs b/src/entry.rs index c3f93aa..5703836 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -6,6 +6,8 @@ use instance::{Instance, V1_0, InstanceFpV1_0}; use shared_library::dynamic_library::DynamicLibrary; use std::path::Path; use ::RawPtr; +use std::marker::PhantomData; +use instance::VkVersion; #[cfg(windows)] fn get_path() -> &'static Path { @@ -27,9 +29,11 @@ lazy_static!{ } #[derive(Clone)] -pub struct Entry { +pub struct Entry { static_fn: vk::StaticFn, entry_fn: vk::EntryFn, + _v: PhantomData + } #[derive(Debug)] @@ -45,8 +49,28 @@ pub enum InstanceError { VkError(vk::Result), } -impl Entry { - pub fn load_vulkan() -> Result { +impl Entry{ + pub fn create_instance(&self, + create_info: &vk::InstanceCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> Result, InstanceError> { + unsafe { + let mut instance: vk::Instance = mem::uninitialized(); + let err_code = self.entry_fn.create_instance(create_info, + allocation_callbacks.as_raw_ptr(), + &mut instance); + if err_code != vk::Result::Success { + return Err(InstanceError::VkError(err_code)); + } + let instance_fn = vk::InstanceFn::load(|name| { + mem::transmute(self.static_fn.get_instance_proc_addr(instance, name.as_ptr())) + }).map_err(|err| InstanceError::LoadError(err))?; + Ok(Instance::from_raw(instance, InstanceFpV1_0 { instance_fn: instance_fn })) + } + } +} +impl Entry { + pub fn load_vulkan() -> Result, LoadingError> { let static_fn = match *VK_LIB { Ok(ref lib) => { let static_fn = vk::StaticFn::load(|name| unsafe { @@ -67,28 +91,10 @@ impl Entry { Ok(Entry { static_fn: static_fn, entry_fn: entry_fn, + _v: PhantomData }) } - pub fn create_instance(&self, - create_info: &vk::InstanceCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> Result, InstanceError> { - unsafe { - let mut instance: vk::Instance = mem::uninitialized(); - let err_code = self.entry_fn.create_instance(create_info, - allocation_callbacks.as_raw_ptr(), - &mut instance); - if err_code != vk::Result::Success { - return Err(InstanceError::VkError(err_code)); - } - let instance_fn = vk::InstanceFn::load(|name| { - mem::transmute(self.static_fn.get_instance_proc_addr(instance, name.as_ptr())) - }).map_err(|err| InstanceError::LoadError(err))?; - Ok(Instance::from_raw(instance, InstanceFpV1_0 { instance_fn: instance_fn })) - } - } - pub fn enumerate_instance_layer_properties(&self) -> VkResult> { unsafe { let mut num = 0; diff --git a/src/extensions/debug_report.rs b/src/extensions/debug_report.rs index 4274a01..95e6c54 100644 --- a/src/extensions/debug_report.rs +++ b/src/extensions/debug_report.rs @@ -14,7 +14,7 @@ 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 360895d..8642898 100644 --- a/src/extensions/surface.rs +++ b/src/extensions/surface.rs @@ -16,7 +16,7 @@ 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())) diff --git a/src/extensions/win32_surface.rs b/src/extensions/win32_surface.rs index 0081f5b..f232599 100644 --- a/src/extensions/win32_surface.rs +++ b/src/extensions/win32_surface.rs @@ -16,7 +16,7 @@ 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 941b5e6..4615db1 100644 --- a/src/extensions/xlib_surface.rs +++ b/src/extensions/xlib_surface.rs @@ -15,7 +15,7 @@ pub struct XlibSurface { } impl XlibSurface { - pub fn new(entry: &Entry, instance: &Instance) -> Result> { + pub fn new(entry: &Entry, instance: &Instance) -> Result> { let surface_fn = vk::XlibSurfaceFn::load(|name| { unsafe { mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))