2016-12-10 05:25:48 +11:00
|
|
|
use prelude::*;
|
2016-12-09 11:55:29 +11:00
|
|
|
use std::mem;
|
|
|
|
use std::ptr;
|
2016-12-10 06:15:59 +11:00
|
|
|
use vk;
|
2016-12-09 11:55:29 +11:00
|
|
|
use instance::Instance;
|
|
|
|
use shared_library::dynamic_library::DynamicLibrary;
|
|
|
|
use std::path::Path;
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn get_path() -> &'static Path {
|
|
|
|
Path::new("vulkan-1.dll")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(unix, not(target_os = "android")))]
|
|
|
|
fn get_path() -> &'static Path {
|
|
|
|
Path::new("libvulkan.so.1")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
fn get_path() -> &'static Path {
|
|
|
|
Path::new("libvulkan.so")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Entry {
|
|
|
|
lib: DynamicLibrary,
|
2016-12-10 02:34:31 +11:00
|
|
|
static_fn: vk::StaticFn,
|
2016-12-09 11:55:29 +11:00
|
|
|
entry_fn: vk::EntryFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum LoadingError {
|
|
|
|
LibraryLoadFailure(String),
|
|
|
|
StaticLoadError(String),
|
|
|
|
EntryLoadError(String),
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:25:48 +11:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum InstanceError {
|
|
|
|
LoadError(String),
|
|
|
|
VkError(vk::Result),
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:55:29 +11:00
|
|
|
impl Entry {
|
|
|
|
pub fn load_vulkan_path(path: &Path) -> Result<Entry, LoadingError> {
|
2016-12-10 05:25:48 +11:00
|
|
|
let lib =
|
|
|
|
DynamicLibrary::open(Some(path)).map_err(|err| LoadingError::LibraryLoadFailure(err))?;
|
|
|
|
let static_fn = vk::StaticFn::load(|name| unsafe {
|
2016-12-09 11:55:29 +11:00
|
|
|
let name = name.to_str().unwrap();
|
|
|
|
let f = match lib.symbol(name) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => ptr::null(),
|
|
|
|
};
|
|
|
|
f
|
2016-12-10 05:25:48 +11:00
|
|
|
}).map_err(|err| LoadingError::StaticLoadError(err))?;
|
|
|
|
let entry_fn = vk::EntryFn::load(|name| unsafe {
|
2016-12-09 11:55:29 +11:00
|
|
|
mem::transmute(static_fn.get_instance_proc_addr(ptr::null_mut(), name.as_ptr()))
|
2016-12-10 05:25:48 +11:00
|
|
|
}).map_err(|err| LoadingError::EntryLoadError(err))?;
|
2016-12-09 11:55:29 +11:00
|
|
|
Ok(Entry {
|
|
|
|
lib: lib,
|
|
|
|
static_fn: static_fn,
|
|
|
|
entry_fn: entry_fn,
|
|
|
|
})
|
|
|
|
}
|
2016-12-10 05:25:48 +11:00
|
|
|
|
2016-12-09 11:55:29 +11:00
|
|
|
pub fn load_vulkan() -> Result<Entry, LoadingError> {
|
|
|
|
Entry::load_vulkan_path(get_path())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_instance(&self,
|
|
|
|
create_info: &vk::InstanceCreateInfo)
|
2016-12-10 05:25:48 +11:00
|
|
|
-> Result<Instance, InstanceError> {
|
2016-12-09 11:55:29 +11:00
|
|
|
unsafe {
|
|
|
|
let mut instance: vk::Instance = mem::uninitialized();
|
|
|
|
let err_code = self.entry_fn.create_instance(create_info, ptr::null(), &mut instance);
|
|
|
|
if err_code != vk::Result::Success {
|
2016-12-10 05:25:48 +11:00
|
|
|
return Err(InstanceError::VkError(err_code));
|
2016-12-09 11:55:29 +11:00
|
|
|
}
|
|
|
|
let instance_fn = vk::InstanceFn::load(|name| unsafe {
|
|
|
|
mem::transmute(self.static_fn.get_instance_proc_addr(instance, name.as_ptr()))
|
2016-12-10 05:25:48 +11:00
|
|
|
}).map_err(|err| InstanceError::LoadError(err))?;
|
2016-12-09 11:55:29 +11:00
|
|
|
Ok(Instance::from_raw(instance, instance_fn))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:25:48 +11:00
|
|
|
pub fn enumerate_instance_layer_properties(&self) -> VkResult<Vec<vk::LayerProperties>> {
|
2016-12-09 11:55:29 +11:00
|
|
|
unsafe {
|
|
|
|
let mut num = 0;
|
|
|
|
self.entry_fn.enumerate_instance_layer_properties(&mut num, ptr::null_mut());
|
|
|
|
|
|
|
|
let mut v = Vec::with_capacity(num as usize);
|
|
|
|
let err_code = self.entry_fn
|
|
|
|
.enumerate_instance_layer_properties(&mut num, v.as_mut_ptr());
|
|
|
|
v.set_len(num as usize);
|
|
|
|
match err_code {
|
|
|
|
vk::Result::Success => Ok(v),
|
|
|
|
_ => Err(err_code),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:25:48 +11:00
|
|
|
pub fn enumerate_instance_extension_properties(&self)
|
|
|
|
-> VkResult<Vec<vk::ExtensionProperties>> {
|
2016-12-09 11:55:29 +11:00
|
|
|
unsafe {
|
|
|
|
let mut num = 0;
|
|
|
|
self.entry_fn
|
|
|
|
.enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut());
|
|
|
|
let mut data = Vec::with_capacity(num as usize);
|
|
|
|
let err_code = self.entry_fn
|
|
|
|
.enumerate_instance_extension_properties(ptr::null(), &mut num, data.as_mut_ptr());
|
|
|
|
data.set_len(num as usize);
|
|
|
|
match err_code {
|
|
|
|
vk::Result::Success => Ok(data),
|
|
|
|
_ => Err(err_code),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|