1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use libloading::*;
use const_cstr::*;
use once_cell::sync::Lazy;
use crate::vulkan::*;
use crate::error::MiraError;
use crate::error::MiraError::{CommandLoadError, BackendError};
use std::os::raw::c_char;
#[cfg(target_os = "linux")]
pub const LIB_NAME:&str = "libvulkan.so.1";
#[cfg(target_os = "android")]
pub const LIB_NAME:&str = "libvulkan.so";
#[cfg(target_os = "macos")]
pub const LIB_NAME:&str = "libvulkan.1.dylib";
#[cfg(target_os = "ios")]
pub const LIB_NAME:&str = "libvulkan.1.dylib";
#[cfg(target_os = "windows")]
pub const LIB_NAME:&str = "vulkan-1.dll";
pub struct Command {
library: Library
}
impl Command {
pub unsafe fn new(lib_path:&str) -> Result<Self, MiraError> {
let lib = match Library::new(lib_path) {
Err(e) => return Err(BackendError(e)),
Ok(lib) => lib
};
Ok( Self {
library: lib,
})
}
pub fn instance(&self) -> Result<Symbol<PFN_vkGetInstanceProcAddr>, MiraError> {
let sym = unsafe {
match self.library.get::<PFN_vkGetInstanceProcAddr>(b"vkGetInstanceProcAddr\0") {
Ok(sym) => sym,
Err(e) => return Err(BackendError(e))
}
};
Ok(sym)
}
pub fn device(&self) -> Result<Symbol<PFN_vkGetDeviceProcAddr>, MiraError> {
let sym = unsafe {
match self.library.get::<PFN_vkGetDeviceProcAddr>(b"vkGetDeviceProcAddr\0") {
Ok(sym) => sym,
Err(e) => return Err(BackendError(e))
}
};
Ok(sym)
}
}
static INTERNAL_LOADER:Lazy<Command> = Lazy::new(|| {
unsafe { Command::new(LIB_NAME).unwrap() }
});
pub unsafe fn instance<T: Sized>(instance: VkInstance, command: ConstCStr) -> Result<T, MiraError> {
static I:Lazy<Symbol<'static, PFN_vkGetInstanceProcAddr>> = Lazy::new(|| {
INTERNAL_LOADER.instance().unwrap()
});
let pfn = I(instance, command.as_ptr() as *const c_char) as *const PFN_vkVoidFunction;
if pfn == std::ptr::null() {
return Err(CommandLoadError { command: command.to_str() });
}
Ok(std::mem::transmute_copy(&pfn))
}
pub unsafe fn device<T: Sized>(device: VkDevice, command: ConstCStr) -> Result<T, MiraError> {
static D:Lazy<Symbol<'static, PFN_vkGetDeviceProcAddr>> = Lazy::new(|| {
INTERNAL_LOADER.device().unwrap()
});
let pfn = D(device, command.as_ptr() as *const c_char) as *const PFN_vkVoidFunction;
if pfn == std::ptr::null() {
return Err(CommandLoadError { command: command.to_str() });
}
Ok(std::mem::transmute_copy(&pfn))
}