rFactor2_vk_hud/src/vk_handles.rs

69 lines
2 KiB
Rust
Raw Normal View History

2023-01-11 14:03:32 +00:00
use anyhow::Result;
2023-01-11 17:51:31 +00:00
use std::{collections::HashMap, ffi::c_void, mem, ptr};
2023-01-11 20:46:17 +00:00
use vulkan_rs::prelude::*;
2023-01-11 14:03:32 +00:00
static mut FN_HANDLES: Option<VkTypedefHandles> = None;
pub fn vk_handles() -> &'static VkTypedefHandles {
unsafe { FN_HANDLES.as_ref().unwrap() }
}
pub fn vk_handles_mut() -> &'static mut VkTypedefHandles {
unsafe { FN_HANDLES.as_mut().unwrap() }
}
pub fn set_vk_handles(handles: VkTypedefHandles) {
unsafe { FN_HANDLES = Some(handles) };
}
pub struct VkTypedefHandles {
typedefs: Vec<String>,
functions: HashMap<String, PFN_vkVoidFunction>,
}
impl VkTypedefHandles {
2023-01-11 17:51:31 +00:00
pub fn new() -> Result<Self> {
2023-01-11 14:03:32 +00:00
Ok(Self {
2023-01-11 20:46:17 +00:00
typedefs: include_str!("../vk_functions")
.lines()
.map(|s| s.to_string())
.collect(),
2023-01-11 14:03:32 +00:00
functions: HashMap::new(),
})
}
pub fn load_instance_functions(
&mut self,
instance: VkInstance,
proc_addr: PFN_vkGetInstanceProcAddr,
) {
unsafe {
for symbol in &self.typedefs {
let name = VkString::new(symbol);
let function = proc_addr(instance, name.as_ptr());
if mem::transmute::<PFN_vkVoidFunction, *const c_void>(function) != ptr::null() {
self.functions.insert(symbol.clone(), function);
}
}
}
}
pub fn load_device_functions(&mut self, device: VkDevice, proc_addr: PFN_vkGetDeviceProcAddr) {
unsafe {
for symbol in &self.typedefs {
let name = VkString::new(symbol);
let function = proc_addr(device, name.as_ptr());
if mem::transmute::<PFN_vkVoidFunction, *const c_void>(function) != ptr::null() {
self.functions.insert(symbol.clone(), function);
}
}
}
}
pub fn handle(&self, symbol_name: impl ToString) -> Option<PFN_vkVoidFunction> {
self.functions.get(&symbol_name.to_string()).cloned()
}
}