use crate::check_errors;
pub use crate::fns::EntryFunctions;
use crate::OomError;
use crate::SafeDeref;
use crate::Version;
use lazy_static::lazy_static;
use shared_library;
use std::error;
use std::ffi::CStr;
use std::fmt;
use std::mem;
use std::ops::Deref;
use std::os::raw::c_char;
use std::os::raw::c_void;
use std::path::Path;
pub unsafe trait Loader: Send + Sync {
fn get_instance_proc_addr(
&self,
instance: ash::vk::Instance,
name: *const c_char,
) -> *const c_void;
}
unsafe impl<T> Loader for T
where
T: SafeDeref + Send + Sync,
T::Target: Loader,
{
#[inline]
fn get_instance_proc_addr(
&self,
instance: ash::vk::Instance,
name: *const c_char,
) -> *const c_void {
(**self).get_instance_proc_addr(instance, name)
}
}
impl fmt::Debug for dyn Loader {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
pub struct DynamicLibraryLoader {
vk_lib: shared_library::dynamic_library::DynamicLibrary,
get_proc_addr:
extern "system" fn(instance: ash::vk::Instance, pName: *const c_char) -> *const c_void,
}
impl DynamicLibraryLoader {
pub unsafe fn new<P>(path: P) -> Result<DynamicLibraryLoader, LoadingError>
where
P: AsRef<Path>,
{
let vk_lib = shared_library::dynamic_library::DynamicLibrary::open(Some(path.as_ref()))
.map_err(LoadingError::LibraryLoadFailure)?;
let get_proc_addr = {
let ptr: *mut c_void = vk_lib
.symbol("vkGetInstanceProcAddr")
.map_err(|_| LoadingError::MissingEntryPoint("vkGetInstanceProcAddr".to_owned()))?;
mem::transmute(ptr)
};
Ok(DynamicLibraryLoader {
vk_lib,
get_proc_addr,
})
}
}
unsafe impl Loader for DynamicLibraryLoader {
#[inline]
fn get_instance_proc_addr(
&self,
instance: ash::vk::Instance,
name: *const c_char,
) -> *const c_void {
(self.get_proc_addr)(instance, name)
}
}
#[derive(Debug)]
pub struct FunctionPointers<L> {
loader: L,
fns: EntryFunctions,
}
impl<L> FunctionPointers<L> {
pub fn new(loader: L) -> FunctionPointers<L>
where
L: Loader,
{
let fns = EntryFunctions::load(|name| unsafe {
mem::transmute(loader.get_instance_proc_addr(ash::vk::Instance::null(), name.as_ptr()))
});
FunctionPointers { loader, fns }
}
#[inline]
pub fn fns(&self) -> &EntryFunctions {
&self.fns
}
pub fn api_version(&self) -> Result<Version, OomError>
where
L: Loader,
{
unsafe {
let name = CStr::from_bytes_with_nul_unchecked(b"vkEnumerateInstanceVersion\0");
let func = self.get_instance_proc_addr(ash::vk::Instance::null(), name.as_ptr());
if func.is_null() {
Ok(Version {
major: 1,
minor: 0,
patch: 0,
})
} else {
type Pfn = extern "system" fn(pApiVersion: *mut u32) -> ash::vk::Result;
let func: Pfn = mem::transmute(func);
let mut api_version = 0;
check_errors(func(&mut api_version))?;
Ok(Version::from(api_version))
}
}
}
#[inline]
pub fn get_instance_proc_addr(
&self,
instance: ash::vk::Instance,
name: *const c_char,
) -> *const c_void
where
L: Loader,
{
self.loader.get_instance_proc_addr(instance, name)
}
}
#[macro_export]
macro_rules! statically_linked_vulkan_loader {
() => {{
extern "C" {
fn vkGetInstanceProcAddr(
instance: ash::vk::Instance,
pName: *const c_char,
) -> ash::vk::PFN_vkVoidFunction;
}
struct StaticallyLinkedVulkanLoader;
unsafe impl Loader for StaticallyLinkedVulkanLoader {
fn get_instance_proc_addr(
&self,
instance: ash::vk::Instance,
name: *const c_char,
) -> extern "system" fn() -> () {
unsafe { vkGetInstanceProcAddr(instance, name) }
}
}
StaticallyLinkedVulkanLoader
}};
}
pub fn auto_loader() -> Result<&'static FunctionPointers<Box<dyn Loader>>, LoadingError> {
#[cfg(target_os = "ios")]
#[allow(non_snake_case)]
fn def_loader_impl() -> Result<Box<Loader>, LoadingError> {
let loader = statically_linked_vulkan_loader!();
Ok(Box::new(loader))
}
#[cfg(not(target_os = "ios"))]
fn def_loader_impl() -> Result<Box<dyn Loader>, LoadingError> {
#[cfg(windows)]
fn get_path() -> &'static Path {
Path::new("vulkan-1.dll")
}
#[cfg(all(unix, not(target_os = "android"), not(target_os = "macos")))]
fn get_path() -> &'static Path {
Path::new("libvulkan.so.1")
}
#[cfg(target_os = "macos")]
fn get_path() -> &'static Path {
Path::new("libvulkan.1.dylib")
}
#[cfg(target_os = "android")]
fn get_path() -> &'static Path {
Path::new("libvulkan.so")
}
let loader = unsafe { DynamicLibraryLoader::new(get_path())? };
Ok(Box::new(loader))
}
lazy_static! {
static ref DEFAULT_LOADER: Result<FunctionPointers<Box<dyn Loader>>, LoadingError> =
def_loader_impl().map(FunctionPointers::new);
}
match DEFAULT_LOADER.deref() {
&Ok(ref ptr) => Ok(ptr),
&Err(ref err) => Err(err.clone()),
}
}
#[derive(Debug, Clone)]
pub enum LoadingError {
LibraryLoadFailure(String),
MissingEntryPoint(String),
}
impl error::Error for LoadingError {
}
impl fmt::Display for LoadingError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
fmt,
"{}",
match *self {
LoadingError::LibraryLoadFailure(_) => "failed to load the Vulkan shared library",
LoadingError::MissingEntryPoint(_) => {
"one of the entry points required to be supported by the Vulkan implementation \
is missing"
}
}
)
}
}
#[cfg(test)]
mod tests {
use crate::instance::loader::DynamicLibraryLoader;
use crate::instance::loader::LoadingError;
#[test]
fn dl_open_error() {
unsafe {
match DynamicLibraryLoader::new("_non_existing_library.void") {
Err(LoadingError::LibraryLoadFailure(_)) => (),
_ => panic!(),
}
}
}
}