Refactor the loading library

This commit is contained in:
maik klein 2016-12-29 05:03:12 +01:00
parent eb02429f1e
commit d5a812e8b5
14 changed files with 30 additions and 38 deletions

View file

@ -7,6 +7,7 @@ use ::RawPtr;
unsafe impl Sync for Device {} unsafe impl Sync for Device {}
unsafe impl Send for Device {} unsafe impl Send for Device {}
#[derive(Clone)]
pub struct Device { pub struct Device {
handle: vk::Device, handle: vk::Device,
device_fn: vk::DeviceFn, device_fn: vk::DeviceFn,

View file

@ -33,14 +33,14 @@ pub struct Entry {
#[derive(Debug)] #[derive(Debug)]
pub enum LoadingError { pub enum LoadingError {
LibraryLoadFailure(String), LibraryLoadError(String),
StaticLoadError(String), EntryLoadError(Vec<&'static str>),
EntryLoadError(String), StaticLoadError(Vec<&'static str>),
} }
#[derive(Debug)] #[derive(Debug)]
pub enum InstanceError { pub enum InstanceError {
LoadError(String), LoadError(Vec<&'static str>),
VkError(vk::Result), VkError(vk::Result),
} }
@ -58,7 +58,7 @@ impl Entry {
}).map_err(|err| LoadingError::StaticLoadError(err))?; }).map_err(|err| LoadingError::StaticLoadError(err))?;
Ok(static_fn) Ok(static_fn)
} }
Err(ref err) => Err(LoadingError::LibraryLoadFailure(err.clone())), Err(ref err) => Err(LoadingError::LibraryLoadError(err.clone())),
}?; }?;
let entry_fn = vk::EntryFn::load(|name| unsafe { let entry_fn = vk::EntryFn::load(|name| unsafe {
mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr())) mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr()))

View file

@ -13,7 +13,7 @@ pub struct AndroidSurface {
} }
impl AndroidSurface { impl AndroidSurface {
pub fn new(entry: &Entry, instance: &Instance) -> Result<AndroidSurface, String> { pub fn new(entry: &Entry, instance: &Instance) -> Result<AndroidSurface, Vec<&'static str>> {
let surface_fn = vk::AndroidSurfaceFn::load(|name| { let surface_fn = vk::AndroidSurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -12,7 +12,7 @@ pub struct DebugReport {
} }
impl DebugReport { impl DebugReport {
pub fn new(entry: &Entry, instance: &Instance) -> Result<DebugReport, String> { pub fn new(entry: &Entry, instance: &Instance) -> Result<DebugReport, Vec<&'static str>> {
let debug_report_fn = vk::DebugReportFn::load(|name| { let debug_report_fn = vk::DebugReportFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -13,7 +13,7 @@ pub struct MirSurface {
} }
impl MirSurface { impl MirSurface {
pub fn new(entry: &Entry, instance: &Instance) -> Result<MirSurface, String> { pub fn new(entry: &Entry, instance: &Instance) -> Result<MirSurface, Vec<&'static str>> {
let surface_fn = vk::MirSurfaceFn::load(|name| { let surface_fn = vk::MirSurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -11,8 +11,8 @@ pub use self::android_surface::AndroidSurface;
mod swapchain; mod swapchain;
mod surface; mod surface;
mod xlibsurface; mod xlibsurface;
mod debug_report;
mod win32_surface; mod win32_surface;
mod debug_report;
mod mir_surface; mod mir_surface;
mod android_surface; mod android_surface;
mod wayland_surface; mod wayland_surface;

View file

@ -14,7 +14,7 @@ pub struct Surface {
} }
impl Surface { impl Surface {
pub fn new(entry: &Entry, instance: &Instance) -> Result<Surface, String> { pub fn new(entry: &Entry, instance: &Instance) -> Result<Surface, Vec<&'static str>> {
let surface_fn = vk::SurfaceFn::load(|name| { let surface_fn = vk::SurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -13,7 +13,7 @@ pub struct Swapchain {
} }
impl Swapchain { impl Swapchain {
pub fn new(instance: &Instance, device: &Device) -> Result<Swapchain, String> { pub fn new(instance: &Instance, device: &Device) -> Result<Swapchain, Vec<&'static str>> {
let swapchain_fn = vk::SwapchainFn::load(|name| { let swapchain_fn = vk::SwapchainFn::load(|name| {
unsafe { mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) } unsafe { mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) }
})?; })?;

View file

@ -12,7 +12,7 @@ pub struct WaylandSurface {
} }
impl WaylandSurface { impl WaylandSurface {
pub fn new(entry: &Entry, instance: &Instance) -> Result<WaylandSurface, String> { pub fn new(entry: &Entry, instance: &Instance) -> Result<WaylandSurface, Vec<&'static str>> {
let surface_fn = vk::WaylandSurfaceFn::load(|name| { let surface_fn = vk::WaylandSurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -13,7 +13,7 @@ pub struct Win32Surface {
} }
impl Win32Surface { impl Win32Surface {
pub fn new(entry: &Entry, instance: &Instance) -> Result<Win32Surface, String> { pub fn new(entry: &Entry, instance: &Instance) -> Result<Win32Surface, Vec<&'static str>> {
let surface_fn = vk::Win32SurfaceFn::load(|name| { let surface_fn = vk::Win32SurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -13,7 +13,7 @@ pub struct XcbSurface {
} }
impl XcbSurface { impl XcbSurface {
pub fn new(entry: &Entry, instance: &Instance) -> Result<XcbSurface, String> { pub fn new(entry: &Entry, instance: &Instance) -> Result<XcbSurface, Vec<&'static str>> {
let surface_fn = vk::XcbSurfaceFn::load(|name| { let surface_fn = vk::XcbSurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -13,7 +13,7 @@ pub struct XlibSurface {
} }
impl XlibSurface { impl XlibSurface {
pub fn new(entry: &Entry, instance: &Instance) -> Result<XlibSurface, String> { pub fn new(entry: &Entry, instance: &Instance) -> Result<XlibSurface, Vec<&'static str>> {
let surface_fn = vk::XlibSurfaceFn::load(|name| { let surface_fn = vk::XlibSurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr())) mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -8,11 +8,11 @@ use ::RawPtr;
#[derive(Debug)] #[derive(Debug)]
pub enum DeviceError { pub enum DeviceError {
LoadError(String), LoadError(Vec<&'static str>),
VkError(vk::Result), VkError(vk::Result),
} }
#[derive(Debug)] #[derive(Clone)]
pub struct Instance { pub struct Instance {
handle: vk::Instance, handle: vk::Instance,
instance_fn: vk::InstanceFn, instance_fn: vk::InstanceFn,

View file

@ -3658,48 +3658,40 @@ macro_rules! vk_functions {
unsafe impl Sync for $struct_name {} unsafe impl Sync for $struct_name {}
impl $struct_name { impl $struct_name {
pub fn load<F>(mut f: F) -> ::std::result::Result<$struct_name, String> pub fn load<F>(mut f: F) -> ::std::result::Result<$struct_name, Vec<&'static str>>
where F: FnMut(&::std::ffi::CStr) -> *const c_void where F: FnMut(&::std::ffi::CStr) -> *const c_void
{ {
use std::ffi::{CString}; use std::ffi::{CString};
use std::mem; use std::mem;
let mut err_str = Vec::new();
let s = $struct_name { let s = $struct_name {
$( $(
$name: unsafe { $name: unsafe {
let cname = CString::new($raw_name).unwrap(); let cname = CString::new($raw_name).unwrap();
let val = f(&cname); let val = f(&cname);
if val.is_null(){
err_str.push(stringify!($raw_name));
}
mem::transmute(val) mem::transmute(val)
}, },
)+ )+
}; };
::std::result::Result::Ok(s)
if err_str.is_empty() {
Ok(s)
}
else{
Err(err_str)
}
} }
$( $(
#[inline] #[inline]
pub unsafe fn $name(&self $(, $param_name: $param)*) -> $ret { pub unsafe fn $name(&self $(, $param_name: $param)*) -> $ret {
let fp = self.$name; let fp = self.$name;
debug_assert!(!(self.$name as *const c_void).is_null(), "{} not loaded!.", stringify!($raw_name));
fp($($param_name),*) fp($($param_name),*)
} }
)+ )+
} }
impl ::std::fmt::Debug for $struct_name {
#[inline]
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
writeln!(fmt, stringify!($struct_name))?;
$(
if !(self.$name as *const c_void).is_null() {
write!(fmt," Is loaded => " )?;
}
else{
write!(fmt," Is not loaded => " )?;
}
write!(fmt, $raw_name)?;
writeln!(fmt, ", ")?;
)+
write!(fmt, "")
}
}
} }
} }
@ -4830,7 +4822,6 @@ vk_functions!{
p_allocator: *const AllocationCallbacks, p_allocator: *const AllocationCallbacks,
p_surface: *mut SurfaceKHR, p_surface: *mut SurfaceKHR,
) -> Result; ) -> Result;
} }
vk_functions!{ vk_functions!{
WaylandSurfaceFn, WaylandSurfaceFn,