Adds support for using custom ways to load the vulkan dynamic libraries.

Signed-off-by: Hal Gentz <zegentzy@protonmail.com>
This commit is contained in:
Hal Gentz 2018-07-10 00:45:41 -06:00
parent d49bf7d409
commit 9dad1ca64e
No known key found for this signature in database
GPG key ID: AEFF36671C5657F8
3 changed files with 39 additions and 23 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "ash" name = "ash"
version = "0.24.3" version = "0.24.4"
authors = ["maik klein <maikklein@googlemail.com>"] authors = ["maik klein <maikklein@googlemail.com>"]
description = "Vulkan bindings for Rust" description = "Vulkan bindings for Rust"
license = "MIT" license = "MIT"
@ -10,7 +10,7 @@ keywords = ["vulkan", "graphic"]
documentation = "https://docs.rs/ash" documentation = "https://docs.rs/ash"
[dependencies] [dependencies]
shared_library = "0.1.5" shared_library = "0.1.9"
lazy_static = "0.2.1" lazy_static = "0.2.1"
libc = "0.2.26" libc = "0.2.26"

View file

@ -9,6 +9,8 @@ use std::fmt;
use std::path::Path; use std::path::Path;
use RawPtr; use RawPtr;
use version::{EntryLoader, FunctionPointers, InstanceLoader, V1_0}; use version::{EntryLoader, FunctionPointers, InstanceLoader, V1_0};
use std::sync::Arc;
use libc;
#[cfg(windows)] #[cfg(windows)]
const LIB_PATH: &'static str = "vulkan-1.dll"; const LIB_PATH: &'static str = "vulkan-1.dll";
@ -25,15 +27,13 @@ const LIB_PATH: &'static str = "libvulkan.1.dylib";
#[cfg(target_os = "ios")] #[cfg(target_os = "ios")]
const LIB_PATH: &'static str = "libMoltenVK.dylib"; const LIB_PATH: &'static str = "libMoltenVK.dylib";
lazy_static!{
static ref VK_LIB: Result<DynamicLibrary, String> = DynamicLibrary::open(Some(&Path::new(LIB_PATH)));
}
#[derive(Clone)] #[derive(Clone)]
pub struct Entry<V: FunctionPointers> { pub struct EntryCustom<V: FunctionPointers, L> {
static_fn: vk::StaticFn, static_fn: vk::StaticFn,
entry_fn: V::EntryFp, entry_fn: V::EntryFp,
vk_lib: L,
} }
pub type Entry<V> = EntryCustom<V, Arc<DynamicLibrary>>;
#[derive(Debug)] #[derive(Debug)]
pub enum LoadingError { pub enum LoadingError {
@ -143,7 +143,7 @@ pub trait EntryV1_0 {
} }
} }
impl EntryV1_0 for Entry<V1_0> { impl<L> EntryV1_0 for EntryCustom<V1_0, L> {
type Fp = V1_0; type Fp = V1_0;
fn fp_v1_0(&self) -> &vk::EntryFnV1_0 { fn fp_v1_0(&self) -> &vk::EntryFnV1_0 {
self.entry_fn.fp_v1_0() self.entry_fn.fp_v1_0()
@ -153,24 +153,40 @@ impl EntryV1_0 for Entry<V1_0> {
} }
} }
impl<V: FunctionPointers> Entry<V> { impl<V: FunctionPointers, L> EntryCustom<V, L> {
pub fn new() -> Result<Self, LoadingError> { pub fn new_custom<OpenFunc, LoadFunc>(o: OpenFunc, mut l: LoadFunc) -> Result<Self, LoadingError>
let lib = VK_LIB where
.as_ref() OpenFunc: FnOnce() -> Result<L, LoadingError>,
.map_err(|err| LoadingError::LibraryLoadError(err.clone()))?; LoadFunc: FnMut(&mut L, &::std::ffi::CStr) -> *const libc::c_void,
{
let mut vk_lib = o()?;
let static_fn =
vk::StaticFn::load(|name| l(&mut vk_lib, name)).map_err(LoadingError::StaticLoadError)?;
let static_fn = vk::StaticFn::load(|name| unsafe { let entry_fn =
lib.symbol(&*name.to_string_lossy()) unsafe { V::EntryFp::load(&static_fn) }.map_err(LoadingError::EntryLoadError)?;
.unwrap_or(ptr::null_mut())
}).map_err(LoadingError::StaticLoadError)?;
let entry_fn = unsafe { Ok(EntryCustom {
V::EntryFp::load(&static_fn)
}.map_err(LoadingError::EntryLoadError)?;
Ok(Entry {
static_fn, static_fn,
entry_fn, entry_fn,
vk_lib,
}) })
} }
} }
impl<V: FunctionPointers> Entry<V> {
pub fn new() -> Result<Self, LoadingError> {
Self::new_custom(
|| {
DynamicLibrary::open(Some(&Path::new(LIB_PATH)))
.map_err(|err| LoadingError::LibraryLoadError(err.clone()))
.map(|dl| Arc::new(dl))
},
|vk_lib, name| unsafe {
vk_lib
.symbol(&*name.to_string_lossy())
.unwrap_or(ptr::null_mut())
},
)
}
}

View file

@ -4,7 +4,7 @@ extern crate shared_library;
extern crate lazy_static; extern crate lazy_static;
pub use instance::{Instance, DeviceError}; pub use instance::{Instance, DeviceError};
pub use device::Device; pub use device::Device;
pub use entry::{Entry, InstanceError, LoadingError}; pub use entry::{Entry, EntryCustom, InstanceError, LoadingError};
mod instance; mod instance;
mod device; mod device;