Now loads vulkan globally

This commit is contained in:
maik klein 2016-12-23 23:22:21 +01:00
parent 582948d192
commit 7edf08fe94
5 changed files with 32 additions and 20 deletions

3
Cargo.lock generated
View file

@ -1,7 +1,8 @@
[root]
name = "ash"
version = "0.1.1"
version = "0.2.0"
dependencies = [
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"shared_library 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"sharedlib 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

View file

@ -7,7 +7,9 @@ license = "MIT"
repository = "https://github.com/MaikKlein/ash"
readme = "README.md"
keywords = ["vulkan", "graphic"]
documentation = "https://docs.rs/ash"
[dependencies]
shared_library = "0.1.5"
sharedlib = "6.0.0"
lazy_static = "0.2.1"

View file

@ -2,14 +2,15 @@
name = "example"
version = "0.1.0"
dependencies = [
"ash 0.1.1",
"ash 0.2.0",
"glfw 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ash"
version = "0.1.1"
version = "0.2.0"
dependencies = [
"lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"shared_library 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"sharedlib 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

View file

@ -5,6 +5,10 @@ use vk;
use instance::Instance;
use shared_library::dynamic_library::DynamicLibrary;
use std::path::Path;
use std::cell::UnsafeCell;
use std::sync::{Once, ONCE_INIT};
use std::sync::atomic::AtomicPtr;
#[cfg(windows)]
fn get_path() -> &'static Path {
Path::new("vulkan-1.dll")
@ -20,9 +24,12 @@ fn get_path() -> &'static Path {
Path::new("libvulkan.so")
}
lazy_static!{
static ref VK_LIB: Result<DynamicLibrary, String> = DynamicLibrary::open(Some(get_path()));
}
pub struct Entry {
lib: DynamicLibrary,
pub static_fn: vk::StaticFn,
pub entry_fn: vk::EntryFn,
}
@ -41,31 +48,30 @@ pub enum InstanceError {
}
impl Entry {
pub fn load_vulkan_path(path: &Path) -> Result<Entry, LoadingError> {
let lib =
DynamicLibrary::open(Some(path)).map_err(|err| LoadingError::LibraryLoadFailure(err))?;
let static_fn = vk::StaticFn::load(|name| unsafe {
let name = name.to_str().unwrap();
let f = match lib.symbol(name) {
Ok(s) => s,
Err(_) => ptr::null(),
};
f
}).map_err(|err| LoadingError::StaticLoadError(err))?;
pub fn load_vulkan() -> Result<Entry, LoadingError> {
let static_fn = match *VK_LIB {
Ok(ref lib) => {
let static_fn = vk::StaticFn::load(|name| unsafe {
let name = name.to_str().unwrap();
let f = match lib.symbol(name) {
Ok(s) => s,
Err(_) => ptr::null(),
};
f
}).map_err(|err| LoadingError::StaticLoadError(err))?;
Ok(static_fn)
}
Err(ref err) => Err(LoadingError::LibraryLoadFailure(err.clone())),
}?;
let entry_fn = vk::EntryFn::load(|name| unsafe {
mem::transmute(static_fn.get_instance_proc_addr(ptr::null_mut(), name.as_ptr()))
}).map_err(|err| LoadingError::EntryLoadError(err))?;
Ok(Entry {
lib: lib,
static_fn: static_fn,
entry_fn: entry_fn,
})
}
pub fn load_vulkan() -> Result<Entry, LoadingError> {
Entry::load_vulkan_path(get_path())
}
pub fn create_instance(&self,
create_info: &vk::InstanceCreateInfo)
-> Result<Instance, InstanceError> {

View file

@ -1,4 +1,6 @@
extern crate shared_library;
#[macro_use]
extern crate lazy_static;
pub mod instance;
pub mod device;