Add a lot of debugging noise

This commit is contained in:
hodasemi 2023-01-14 08:08:40 +01:00
parent 392c4164d6
commit 763e3c55e2
5 changed files with 173 additions and 75 deletions

View file

@ -9,8 +9,8 @@ edition = "2021"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
# vulkan-rs = { git = "ssh://gitea@gavania.de:23/Gavania/Gavania.git", branch = "0.2.0_dev" } vulkan-rs = { git = "https://gavania.de/Gavania/Gavania.git", branch = "0.2.0_dev" }
vulkan-rs = { path = "/home/michael/Dokumente/Workspace/Gavania/vulkan-rs" } # vulkan-rs = { path = "/home/michael/Dokumente/Workspace/Gavania/vulkan-rs" }
rfactor_sm_reader = { git = "https://gavania.de/hodasemi/rfactor_sm_reader.git" } rfactor_sm_reader = { git = "https://gavania.de/hodasemi/rfactor_sm_reader.git" }
anyhow = { version = "1.0.68", features = ["backtrace"] } anyhow = { version = "1.0.68", features = ["backtrace"] }

View file

@ -10,7 +10,7 @@ use std::{
io::Write, io::Write,
mem, mem,
os::raw::c_char, os::raw::c_char,
ptr, slice, ptr,
}; };
use enums::*; use enums::*;
@ -19,7 +19,7 @@ use structs::*;
use vk_handles::*; use vk_handles::*;
use vulkan_rs::prelude::*; use vulkan_rs::prelude::*;
const LOG_FILE: &'static str = "/home/michael/rf2_vk_hud.log"; static mut LOG_FILE: String = String::new();
static mut OVERLAY: Overlay = Overlay::new(); static mut OVERLAY: Overlay = Overlay::new();
static mut QUEUE_SUBMIT: PFN_vkQueueSubmit = static mut QUEUE_SUBMIT: PFN_vkQueueSubmit =
@ -29,10 +29,15 @@ static mut ACQUIRE_NEXT_IMAGE: PFN_vkAcquireNextImageKHR =
#[no_mangle] #[no_mangle]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub extern "C" fn vkNegotiateLoaderLayerInterfaceVersion( pub(crate) extern "C" fn vkNegotiateLoaderLayerInterfaceVersion(
pVersionStruct: *mut VkNegotiateLayerInterface, pVersionStruct: *mut VkNegotiateLayerInterface,
) -> VkResult { ) -> VkResult {
if let Err(_) = File::create(LOG_FILE) {} let home = std::env::var("HOME").unwrap();
unsafe {
LOG_FILE = format!("{}/rf2_vk_hud.log", home);
}
if let Err(_) = File::create(unsafe { &LOG_FILE }) {}
write_log(" =================================================================="); write_log(" ==================================================================");
write_log(" ======================= New Negotiation =========================="); write_log(" ======================= New Negotiation ==========================");
@ -112,7 +117,7 @@ extern "system" fn get_instance_proc_addr(
Functions::Null.convert() Functions::Null.convert()
} }
pub extern "system" fn create_instance( pub(crate) extern "system" fn create_instance(
create_info: *const VkInstanceCreateInfo, create_info: *const VkInstanceCreateInfo,
allocator: *const VkAllocationCallbacks, allocator: *const VkAllocationCallbacks,
instance: *mut VkInstance, instance: *mut VkInstance,
@ -177,7 +182,7 @@ pub extern "system" fn create_instance(
VK_SUCCESS VK_SUCCESS
} }
pub extern "system" fn destroy_instance( pub(crate) extern "system" fn destroy_instance(
instance: VkInstance, instance: VkInstance,
allocator: *const VkAllocationCallbacks, allocator: *const VkAllocationCallbacks,
) { ) {
@ -192,7 +197,7 @@ pub extern "system" fn destroy_instance(
} }
} }
pub extern "system" fn create_device( pub(crate) extern "system" fn create_device(
physical_device: VkPhysicalDevice, physical_device: VkPhysicalDevice,
create_info: *const VkDeviceCreateInfo<'_>, create_info: *const VkDeviceCreateInfo<'_>,
allocator: *const VkAllocationCallbacks, allocator: *const VkAllocationCallbacks,
@ -257,26 +262,65 @@ pub extern "system" fn create_device(
return VK_ERROR_INITIALIZATION_FAILED; return VK_ERROR_INITIALIZATION_FAILED;
} }
}; };
let queue_info = match Queue::create_non_presentable_request_info(&pdev, VK_QUEUE_GRAPHICS_BIT)
{
Ok(qi) => qi,
Err(err) => {
write_log(format!("failed getting queue info: {:?}", err));
return VK_ERROR_INITIALIZATION_FAILED;
}
};
unsafe {
let create_queue_info = std::slice::from_raw_parts(
(*create_info).pQueueCreateInfos,
(*create_info).queueCreateInfoCount as usize,
);
for info in create_queue_info {
write_log(format!(
"pCreateDeviceInfo; queue fam: {}, queue count: {}",
info.queueFamilyIndex, info.queueCount
));
}
write_log(format!(
"Queue: queue fam: {}, queue count: {}",
queue_info.queue_create_info.queueFamilyIndex, queue_info.queue_create_info.queueCount
));
}
let ext_names = unsafe { (*create_info).extension_names() }; let ext_names = unsafe { (*create_info).extension_names() };
write_log(format!("{:?}", ext_names)); write_log(format!("{:?}", ext_names));
unsafe { let device = match Device::preinitialized(unsafe { *device }, proc_addr, pdev, &ext_names) {
OVERLAY.set_device(
match Device::preinitialized(*device, proc_addr, pdev, &ext_names) {
Ok(dev) => dev, Ok(dev) => dev,
Err(err) => { Err(err) => {
write_log(format!("-> local device creation failed: {:?}", err)); write_log(format!("-> local device creation failed: {:?}", err));
return VK_ERROR_INITIALIZATION_FAILED; return VK_ERROR_INITIALIZATION_FAILED;
} }
}, };
);
write_log("created device");
let queue = device.get_queue(queue_info.queue_family_index, queue_info.queue_index);
write_log("got queue from device");
unsafe {
OVERLAY.set_device(device);
OVERLAY.set_queue(queue);
} }
VK_SUCCESS VK_SUCCESS
} }
pub extern "system" fn destroy_device(device: VkDevice, allocator: *const VkAllocationCallbacks) { pub(crate) extern "system" fn destroy_device(
device: VkDevice,
allocator: *const VkAllocationCallbacks,
) {
write_log(" ================== vulkan layer destroy device =================="); write_log(" ================== vulkan layer destroy device ==================");
unsafe { unsafe {
@ -288,7 +332,7 @@ pub extern "system" fn destroy_device(device: VkDevice, allocator: *const VkAllo
} }
} }
pub extern "system" fn create_swapchain( pub(crate) extern "system" fn create_swapchain(
_device: VkDevice, _device: VkDevice,
create_info: *const VkSwapchainCreateInfoKHR, create_info: *const VkSwapchainCreateInfoKHR,
_allocator: *const VkAllocationCallbacks, _allocator: *const VkAllocationCallbacks,
@ -307,9 +351,10 @@ pub extern "system" fn create_swapchain(
create_swapchain(_device, create_info, _allocator, p_swapchain); create_swapchain(_device, create_info, _allocator, p_swapchain);
write_log("-> created swapchain vk handle"); write_log(format!("-> created swapchain vk handle {:?}", unsafe {
*p_swapchain
}));
// if unsafe { !OVERLAY.has_rendering() } {
let swapchain = let swapchain =
match unsafe { Swapchain::from_raw(OVERLAY.device(), &*create_info, *p_swapchain) } { match unsafe { Swapchain::from_raw(OVERLAY.device(), &*create_info, *p_swapchain) } {
Ok(swapchain) => swapchain, Ok(swapchain) => swapchain,
@ -327,12 +372,11 @@ pub extern "system" fn create_swapchain(
} }
write_log("-> created renderer"); write_log("-> created renderer");
// }
VK_SUCCESS VK_SUCCESS
} }
pub extern "system" fn submit_queue( pub(crate) extern "system" fn submit_queue(
queue: VkQueue, queue: VkQueue,
submit_count: u32, submit_count: u32,
submits: *const VkSubmitInfo, submits: *const VkSubmitInfo,
@ -341,31 +385,37 @@ pub extern "system" fn submit_queue(
write_log(" ================== vulkan layer submit queue =================="); write_log(" ================== vulkan layer submit queue ==================");
unsafe { unsafe {
let command_buffers = slice::from_raw_parts( write_log(format!(
(*submits).pCommandBuffers, "submit queue ({:?}), internal queue: ({:?})",
(*submits).commandBufferCount as usize, queue,
); OVERLAY.queue().lock().unwrap().vk_handle()
));
let cb = match OVERLAY.render() { // let command_buffers = slice::from_raw_parts(
Ok(cb) => cb, // (*submits).pCommandBuffers,
Err(err) => { // (*submits).commandBufferCount as usize,
write_log(format!("overlay rendering failed: {:?}", err)); // );
return VK_ERROR_DEVICE_LOST;
}
};
let cb = [command_buffers, &[cb]].concat(); // let cb = match OVERLAY.render() {
// Ok(cb) => cb,
// Err(err) => {
// write_log(format!("overlay rendering failed: {:?}", err));
// return VK_ERROR_DEVICE_LOST;
// }
// };
let mut_ptr = submits as *mut VkSubmitInfo; // let cb = [command_buffers, &[cb]].concat();
(*mut_ptr).commandBufferCount = cb.len() as u32; // let mut_ptr = submits as *mut VkSubmitInfo;
(*mut_ptr).pCommandBuffers = cb.as_ptr();
QUEUE_SUBMIT(queue, submit_count, mut_ptr, fence) // (*mut_ptr).commandBufferCount = cb.len() as u32;
// (*mut_ptr).pCommandBuffers = cb.as_ptr();
QUEUE_SUBMIT(queue, submit_count, submits, fence)
} }
} }
pub extern "system" fn get_device_queue( pub(crate) extern "system" fn get_device_queue(
device: VkDevice, device: VkDevice,
queue_family_index: u32, queue_family_index: u32,
queue_index: u32, queue_index: u32,
@ -383,21 +433,26 @@ pub extern "system" fn get_device_queue(
get_device_queue(device, queue_family_index, queue_index, p_queue); get_device_queue(device, queue_family_index, queue_index, p_queue);
write_log(format!("new queue: {:?}", unsafe { *p_queue })); write_log(format!("new queue: {:?}", unsafe { *p_queue }));
unsafe { unsafe {
if !OVERLAY.has_queue() { write_log(format!(
let arc_device = OVERLAY.device(); "internal queue: ({:?})",
OVERLAY.add_queue(Queue::new( OVERLAY.queue().lock().unwrap().vk_handle()
arc_device,
*p_queue,
queue_family_index,
queue_index,
)); ));
} }
}
// unsafe {
// let arc_device = OVERLAY.device();
// arc_device.physical_device().OVERLAY.add_queue(Queue::new(
// arc_device,
// *p_queue,
// queue_family_index,
// queue_index,
// ));
// }
} }
pub extern "system" fn acquire_next_image( pub(crate) extern "system" fn acquire_next_image(
device: VkDevice, device: VkDevice,
swapchain: VkSwapchainKHR, swapchain: VkSwapchainKHR,
timeout: u64, timeout: u64,
@ -417,7 +472,11 @@ pub extern "system" fn acquire_next_image(
Some(fence), Some(fence),
); );
write_log(format!("acquire res: {:?}", res)); write_log(format!(
"acquire (swapchain: {:?}) res: {:?}",
sc.vk_handle(),
res
));
match res { match res {
Ok(res) => match res { Ok(res) => match res {
@ -442,7 +501,7 @@ pub extern "system" fn acquire_next_image(
} }
} }
pub extern "system" fn present_queue( pub(crate) extern "system" fn present_queue(
queue: VkQueue, queue: VkQueue,
present_info: *const VkPresentInfoKHR, present_info: *const VkPresentInfoKHR,
) -> VkResult { ) -> VkResult {
@ -450,6 +509,30 @@ pub extern "system" fn present_queue(
write_log(format!("iq: {:?}, cq: {:?}", queue, unsafe { write_log(format!("iq: {:?}, cq: {:?}", queue, unsafe {
OVERLAY.queue().lock().unwrap().vk_handle() OVERLAY.queue().lock().unwrap().vk_handle()
})); }));
unsafe {
let swapchains = std::slice::from_raw_parts(
(*present_info).pSwapchains,
(*present_info).swapchainCount as usize,
);
write_log(format!("present {} swapchain(s)", swapchains.len()));
for swapchain in swapchains {
write_log(format!("present swapchain: {:?}", swapchain));
if let Some(swch) = OVERLAY.swapchain(*swapchain) {
write_log(" -> internal swapchain found!");
}
}
}
match unsafe { OVERLAY.render() } {
Ok(_) => (),
Err(err) => {
write_log(format!("overlay rendering failed: {:?}", err));
return VK_ERROR_DEVICE_LOST;
}
};
let pfn: PFN_vkQueuePresentKHR = match vk_handles().handle("vkQueuePresentKHR") { let pfn: PFN_vkQueuePresentKHR = match vk_handles().handle("vkQueuePresentKHR") {
Some(pfn) => unsafe { mem::transmute(pfn) }, Some(pfn) => unsafe { mem::transmute(pfn) },
@ -463,7 +546,11 @@ pub extern "system" fn present_queue(
} }
pub fn write_log(msg: impl ToString) { pub fn write_log(msg: impl ToString) {
if let Ok(mut file) = OpenOptions::new().append(true).create(true).open(LOG_FILE) { if let Ok(mut file) = OpenOptions::new()
.append(true)
.create(true)
.open(unsafe { &LOG_FILE })
{
if let Err(_) = file.write_all(format!("{}\n", msg.to_string()).as_bytes()) {} if let Err(_) = file.write_all(format!("{}\n", msg.to_string()).as_bytes()) {}
} }
} }

View file

@ -51,7 +51,7 @@ impl Overlay {
self.queue.is_some() self.queue.is_some()
} }
pub fn add_queue(&mut self, queue: Arc<Mutex<Queue>>) { pub fn set_queue(&mut self, queue: Arc<Mutex<Queue>>) {
self.queue = Some(queue); self.queue = Some(queue);
} }
@ -85,17 +85,17 @@ impl Overlay {
Ok(()) Ok(())
} }
pub fn render(&mut self) -> Result<VkCommandBuffer> { pub fn render(&mut self) -> Result<()> {
if self.rfactor_data.is_none() { if self.rfactor_data.is_none() {
// self.rfactor_data = RFactorData::new( self.rfactor_data = RFactorData::new(
// self.device(), self.device(),
// self.rendering self.rendering
// .as_mut() .as_mut()
// .unwrap() .unwrap()
// .single_color_pipeline() .single_color_pipeline()
// .descriptor_layout(), .descriptor_layout(),
// ) )
// .ok(); .ok();
} }
// check twice for rfactor data, because of borrowing rules // check twice for rfactor data, because of borrowing rules

View file

@ -2,7 +2,10 @@ use anyhow::Result;
use cgmath::{Vector2, Vector4}; use cgmath::{Vector2, Vector4};
use vulkan_rs::prelude::*; use vulkan_rs::prelude::*;
use std::sync::{Arc, Mutex}; use std::{
sync::{Arc, Mutex},
time::Duration,
};
use super::{pipeline::SingleColorPipeline, rfactor_data::RenderObject}; use super::{pipeline::SingleColorPipeline, rfactor_data::RenderObject};
use crate::write_log; use crate::write_log;
@ -48,6 +51,9 @@ pub struct Rendering {
pipeline: SingleColorPipeline, pipeline: SingleColorPipeline,
render_target: RenderTarget, render_target: RenderTarget,
command_buffer: Arc<CommandBuffer>, command_buffer: Arc<CommandBuffer>,
device: Arc<Device>,
queue: Arc<Mutex<Queue>>,
} }
impl Rendering { impl Rendering {
@ -91,7 +97,10 @@ impl Rendering {
swapchain, swapchain,
pipeline: SingleColorPipeline::new(device.clone(), render_target.render_pass())?, pipeline: SingleColorPipeline::new(device.clone(), render_target.render_pass())?,
render_target, render_target,
command_buffer: CommandBuffer::new_primary().build(device, queue)?, command_buffer: CommandBuffer::new_primary().build(device.clone(), queue.clone())?,
device,
queue,
}) })
} }
@ -107,7 +116,7 @@ impl Rendering {
&mut self, &mut self,
swapchain: Arc<Swapchain>, swapchain: Arc<Swapchain>,
objects: &[&dyn RenderObject], objects: &[&dyn RenderObject],
) -> Result<VkCommandBuffer> { ) -> Result<()> {
let image_index = self.swapchain.current_index(); let image_index = self.swapchain.current_index();
let viewport = [VkViewport { let viewport = [VkViewport {
@ -153,6 +162,9 @@ impl Rendering {
self.render_target.end(&recorder); self.render_target.end(&recorder);
} }
Ok(self.command_buffer.vk_handle()) let queue = self.queue.lock().unwrap();
queue.minimal_submit(Duration::from_secs(10), &[self.command_buffer.clone()])?;
Ok(())
} }
} }

View file

@ -14,8 +14,8 @@ pub trait RenderObject {
pub struct RFactorData { pub struct RFactorData {
// rf2 memory mapped data // rf2 memory mapped data
telemetry_reader: TelemetryReader, // telemetry_reader: TelemetryReader,
scoring_reader: ScoringReader, // scoring_reader: ScoringReader,
// radar objects // radar objects
background: RadarObject, background: RadarObject,
@ -30,9 +30,8 @@ impl RFactorData {
let car_width = 0.025; let car_width = 0.025;
Ok(Self { Ok(Self {
telemetry_reader: TelemetryReader::new()?, // telemetry_reader: TelemetryReader::new()?,
scoring_reader: ScoringReader::new()?, // scoring_reader: ScoringReader::new()?,
background: RadarObject::new( background: RadarObject::new(
device.clone(), device.clone(),
descriptor_layout, descriptor_layout,