From 9dad1ca64e0149c5e51a0f694de26c72bce3fd10 Mon Sep 17 00:00:00 2001 From: Hal Gentz Date: Tue, 10 Jul 2018 00:45:41 -0600 Subject: [PATCH] Adds support for using custom ways to load the vulkan dynamic libraries. Signed-off-by: Hal Gentz --- ash/Cargo.toml | 4 ++-- ash/src/entry.rs | 56 +++++++++++++++++++++++++++++++----------------- ash/src/lib.rs | 2 +- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/ash/Cargo.toml b/ash/Cargo.toml index 205661e..951ceb6 100644 --- a/ash/Cargo.toml +++ b/ash/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ash" -version = "0.24.3" +version = "0.24.4" authors = ["maik klein "] description = "Vulkan bindings for Rust" license = "MIT" @@ -10,7 +10,7 @@ keywords = ["vulkan", "graphic"] documentation = "https://docs.rs/ash" [dependencies] -shared_library = "0.1.5" +shared_library = "0.1.9" lazy_static = "0.2.1" libc = "0.2.26" diff --git a/ash/src/entry.rs b/ash/src/entry.rs index ec55c45..496e92f 100644 --- a/ash/src/entry.rs +++ b/ash/src/entry.rs @@ -9,6 +9,8 @@ use std::fmt; use std::path::Path; use RawPtr; use version::{EntryLoader, FunctionPointers, InstanceLoader, V1_0}; +use std::sync::Arc; +use libc; #[cfg(windows)] const LIB_PATH: &'static str = "vulkan-1.dll"; @@ -25,15 +27,13 @@ const LIB_PATH: &'static str = "libvulkan.1.dylib"; #[cfg(target_os = "ios")] const LIB_PATH: &'static str = "libMoltenVK.dylib"; -lazy_static!{ - static ref VK_LIB: Result = DynamicLibrary::open(Some(&Path::new(LIB_PATH))); -} - #[derive(Clone)] -pub struct Entry { +pub struct EntryCustom { static_fn: vk::StaticFn, entry_fn: V::EntryFp, + vk_lib: L, } +pub type Entry = EntryCustom>; #[derive(Debug)] pub enum LoadingError { @@ -143,7 +143,7 @@ pub trait EntryV1_0 { } } -impl EntryV1_0 for Entry { +impl EntryV1_0 for EntryCustom { type Fp = V1_0; fn fp_v1_0(&self) -> &vk::EntryFnV1_0 { self.entry_fn.fp_v1_0() @@ -153,24 +153,40 @@ impl EntryV1_0 for Entry { } } -impl Entry { - pub fn new() -> Result { - let lib = VK_LIB - .as_ref() - .map_err(|err| LoadingError::LibraryLoadError(err.clone()))?; +impl EntryCustom { + pub fn new_custom(o: OpenFunc, mut l: LoadFunc) -> Result + where + OpenFunc: FnOnce() -> Result, + 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 { - lib.symbol(&*name.to_string_lossy()) - .unwrap_or(ptr::null_mut()) - }).map_err(LoadingError::StaticLoadError)?; + let entry_fn = + unsafe { V::EntryFp::load(&static_fn) }.map_err(LoadingError::EntryLoadError)?; - let entry_fn = unsafe { - V::EntryFp::load(&static_fn) - }.map_err(LoadingError::EntryLoadError)?; - - Ok(Entry { + Ok(EntryCustom { static_fn, entry_fn, + vk_lib, }) } } + +impl Entry { + pub fn new() -> Result { + 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()) + }, + ) + } +} diff --git a/ash/src/lib.rs b/ash/src/lib.rs index ba433cb..c3d05fc 100644 --- a/ash/src/lib.rs +++ b/ash/src/lib.rs @@ -4,7 +4,7 @@ extern crate shared_library; extern crate lazy_static; pub use instance::{Instance, DeviceError}; pub use device::Device; -pub use entry::{Entry, InstanceError, LoadingError}; +pub use entry::{Entry, EntryCustom, InstanceError, LoadingError}; mod instance; mod device;