rFactor2_vk_hud/src/lib.rs

223 lines
6.4 KiB
Rust
Raw Normal View History

2023-01-10 13:24:04 +00:00
pub mod enums;
pub mod structs;
2023-01-11 14:03:32 +00:00
mod vk_handles;
2023-01-10 13:24:04 +00:00
2023-01-10 18:33:45 +00:00
use std::{
fs::{File, OpenOptions},
io::Write,
mem,
os::raw::c_char,
ptr,
};
2023-01-10 17:20:18 +00:00
2023-01-10 13:24:04 +00:00
use enums::*;
use structs::*;
2023-01-11 14:03:32 +00:00
use vk_handles::*;
2023-01-10 13:24:04 +00:00
use vulkan_sys::prelude::*;
2023-01-10 18:33:45 +00:00
const LOG_FILE: &'static str = "/home/michael/rf2_vk_hud.log";
2023-01-10 13:24:04 +00:00
#[no_mangle]
#[allow(non_snake_case)]
pub extern "C" fn vkNegotiateLoaderLayerInterfaceVersion(
pVersionStruct: *mut VkNegotiateLayerInterface,
) -> VkResult {
2023-01-10 18:33:45 +00:00
File::create(LOG_FILE).unwrap();
write_log(" ==================================================================");
write_log(" ======================= New Negotiation ==========================");
write_log(" ==================================================================");
2023-01-10 13:24:04 +00:00
unsafe {
*pVersionStruct = VkNegotiateLayerInterface {
sType: enums::VkNegotiateLayerStructType::LAYER_NEGOTIATE_INTERFACE_STRUCT,
pNext: ptr::null_mut(),
loaderLayerInterfaceVersion: 2,
pfnGetInstanceProcAddr: Some(get_instance_proc_addr),
pfnGetDeviceProcAddr: Some(get_device_proc_addr),
pfnGetPhysicalDeviceProcAddr: None,
}
};
2023-01-11 17:51:31 +00:00
set_vk_handles(VkTypedefHandles::new().unwrap());
2023-01-11 14:03:32 +00:00
2023-01-10 13:24:04 +00:00
VK_SUCCESS
}
#[no_mangle]
extern "system" fn get_device_proc_addr(
2023-01-10 17:20:18 +00:00
_device: VkDevice,
2023-01-10 13:24:04 +00:00
function_name: *const c_char,
) -> PFN_vkVoidFunction {
let func_string = match VkString::try_from(function_name) {
Ok(func) => func,
Err(_) => {
write_log("Err: failed creating string");
return Functions::Null.convert();
}
};
2023-01-10 15:56:56 +00:00
let s = func_string.as_str();
match s {
2023-01-10 18:41:45 +00:00
"vkCreateDevice" => return Functions::CreateDevice(create_device).convert(),
"vkDestroyDevice" => return Functions::DestroyDevice(destroy_device).convert(),
"vkCreateInstance" => return Functions::CreateInstance(create_instance).convert(),
"vkDestroyInstance" => return Functions::DestroyInstance(destroy_instance).convert(),
2023-01-10 13:24:04 +00:00
2023-01-10 18:41:45 +00:00
_ => (),
};
2023-01-11 17:51:31 +00:00
if let Some(func) = vk_handles().handle(s) {
return func;
}
write_log(format!("\trequested fn: {} in device proc addr", s));
2023-01-10 18:41:45 +00:00
write_log(format!("\t-> not found"));
Functions::Null.convert()
2023-01-10 13:24:04 +00:00
}
#[no_mangle]
extern "system" fn get_instance_proc_addr(
_instance: VkInstance,
function_name: *const c_char,
) -> PFN_vkVoidFunction {
let func_string = match VkString::try_from(function_name) {
Ok(func) => func,
Err(_) => {
write_log("Err: failed creating string");
return Functions::Null.convert();
2023-01-11 05:00:35 +00:00
}
};
2023-01-11 05:00:35 +00:00
let s = func_string.as_str();
2023-01-11 05:00:35 +00:00
match s {
"vkCreateDevice" => return Functions::CreateDevice(create_device).convert(),
"vkDestroyDevice" => return Functions::DestroyDevice(destroy_device).convert(),
"vkCreateInstance" => return Functions::CreateInstance(create_instance).convert(),
"vkDestroyInstance" => return Functions::DestroyInstance(destroy_instance).convert(),
_ => (),
};
2023-01-11 17:51:31 +00:00
if let Some(func) = vk_handles().handle(s) {
return func;
}
write_log(format!("\trequested fn: {} in instance proc addr", s));
write_log(format!("\t-> not found"));
Functions::Null.convert()
2023-01-11 05:00:35 +00:00
}
2023-01-10 13:24:04 +00:00
extern "system" fn create_instance(
create_info: *const VkInstanceCreateInfo,
allocator: *const VkAllocationCallbacks,
instance: *mut VkInstance,
) -> VkResult {
2023-01-10 15:56:56 +00:00
write_log(" ================== vulkan layer create instance ==================");
2023-01-10 13:24:04 +00:00
let chain_info = match VkLayerInstanceCreateInfo::get_chain_info(
unsafe { &*create_info },
VK_LAYER_LINK_INFO,
) {
Some(info) => info,
None => {
2023-01-10 17:20:18 +00:00
write_log("instance chain info not found.");
2023-01-10 13:24:04 +00:00
return VK_ERROR_LAYER_NOT_PRESENT;
}
};
let proc_addr = chain_info.layer_info().next_instance_proc_addr;
2023-01-11 14:03:32 +00:00
let create_instance: PFN_vkCreateInstance = unsafe {
mem::transmute(proc_addr(
VkInstance::NULL_HANDLE,
VkString::new("vkCreateInstance").as_ptr(),
))
2023-01-10 13:24:04 +00:00
};
chain_info.advance_layer_info();
2023-01-11 14:03:32 +00:00
let result = create_instance(create_info, allocator, instance);
2023-01-10 13:24:04 +00:00
if result != VK_SUCCESS {
return result;
2023-01-10 17:20:18 +00:00
};
2023-01-10 15:56:56 +00:00
2023-01-11 14:03:32 +00:00
vk_handles_mut().load_instance_functions(unsafe { *instance }, proc_addr);
2023-01-10 15:56:56 +00:00
write_log("-> successfully created instance.");
2023-01-10 13:24:04 +00:00
VK_SUCCESS
}
extern "system" fn destroy_instance(instance: VkInstance, allocator: *const VkAllocationCallbacks) {
2023-01-10 15:56:56 +00:00
write_log(" ================== vulkan layer destroy instance ==================");
2023-01-11 17:51:31 +00:00
unsafe {
let destroy_instance: PFN_vkDestroyInstance =
mem::transmute(vk_handles().handle("vkDestroyInstance").unwrap());
destroy_instance(instance, allocator);
}
2023-01-10 13:24:04 +00:00
}
extern "system" fn create_device(
physical_device: VkPhysicalDevice,
create_info: *const VkDeviceCreateInfo<'_>,
allocator: *const VkAllocationCallbacks,
device: *mut VkDevice,
) -> VkResult {
2023-01-10 15:56:56 +00:00
write_log(" ================== vulkan layer create device ==================");
2023-01-10 13:24:04 +00:00
2023-01-10 17:20:18 +00:00
let chain_info =
match VkLayerDeviceCreateInfo::get_chain_info(unsafe { &*create_info }, VK_LAYER_LINK_INFO)
{
Some(info) => info,
None => {
write_log("device chain info not found.");
return VK_ERROR_LAYER_NOT_PRESENT;
}
};
2023-01-11 14:03:32 +00:00
let proc_addr = chain_info.layer_info().next_device_proc_addr;
chain_info.advance_layer_info();
2023-01-10 17:20:18 +00:00
let result = unsafe {
2023-01-11 14:03:32 +00:00
let create_device: PFN_vkCreateDevice =
mem::transmute(vk_handles().handle("vkCreateDevice").unwrap());
create_device(physical_device, create_info, allocator, device)
2023-01-10 17:20:18 +00:00
};
if result != VK_SUCCESS {
return result;
}
2023-01-11 14:03:32 +00:00
vk_handles_mut().load_device_functions(unsafe { *device }, proc_addr);
2023-01-10 17:20:18 +00:00
2023-01-10 13:24:04 +00:00
VK_SUCCESS
}
extern "system" fn destroy_device(device: VkDevice, allocator: *const VkAllocationCallbacks) {
2023-01-10 15:56:56 +00:00
write_log(" ================== vulkan layer destroy device ==================");
2023-01-11 17:51:31 +00:00
unsafe {
let destroy_device: PFN_vkDestroyDevice =
mem::transmute(vk_handles().handle("vkDestroyDevice").unwrap());
destroy_device(device, allocator);
}
2023-01-10 13:24:04 +00:00
}
2023-01-11 17:51:31 +00:00
pub fn write_log(msg: impl ToString) {
2023-01-10 13:24:04 +00:00
let mut file = OpenOptions::new()
.append(true)
.create(true)
2023-01-10 18:33:45 +00:00
.open(LOG_FILE)
2023-01-10 13:24:04 +00:00
.unwrap();
file.write_all(format!("{}\n", msg.to_string()).as_bytes())
.unwrap();
}