From 90df19cbd74f635c0dcf35edcf1350e318783321 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Sat, 14 Jan 2023 08:08:40 +0100 Subject: [PATCH] Add a lot of debugging noise --- Cargo.toml | 4 +- src/lib.rs | 193 ++++++++++++++++++++++++++---------- src/overlay/mod.rs | 22 ++-- src/overlay/rendering.rs | 20 +++- src/overlay/rfactor_data.rs | 9 +- 5 files changed, 173 insertions(+), 75 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 948fb30..4792f35 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,8 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -# vulkan-rs = { git = "ssh://gitea@gavania.de:23/Gavania/Gavania.git", branch = "0.2.0_dev" } -vulkan-rs = { path = "/home/michael/Dokumente/Workspace/Gavania/vulkan-rs" } +vulkan-rs = { git = "https://gavania.de/Gavania/Gavania.git", branch = "0.2.0_dev" } +# vulkan-rs = { path = "/home/michael/Dokumente/Workspace/Gavania/vulkan-rs" } rfactor_sm_reader = { git = "https://gavania.de/hodasemi/rfactor_sm_reader.git" } anyhow = { version = "1.0.68", features = ["backtrace"] } diff --git a/src/lib.rs b/src/lib.rs index f9c5b62..300579f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ use std::{ io::Write, mem, os::raw::c_char, - ptr, slice, + ptr, }; use enums::*; @@ -19,7 +19,7 @@ use structs::*; use vk_handles::*; 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 QUEUE_SUBMIT: PFN_vkQueueSubmit = @@ -29,10 +29,15 @@ static mut ACQUIRE_NEXT_IMAGE: PFN_vkAcquireNextImageKHR = #[no_mangle] #[allow(non_snake_case)] -pub extern "C" fn vkNegotiateLoaderLayerInterfaceVersion( +pub(crate) extern "C" fn vkNegotiateLoaderLayerInterfaceVersion( pVersionStruct: *mut VkNegotiateLayerInterface, ) -> 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(" ======================= New Negotiation =========================="); @@ -112,7 +117,7 @@ extern "system" fn get_instance_proc_addr( Functions::Null.convert() } -pub extern "system" fn create_instance( +pub(crate) extern "system" fn create_instance( create_info: *const VkInstanceCreateInfo, allocator: *const VkAllocationCallbacks, instance: *mut VkInstance, @@ -177,7 +182,7 @@ pub extern "system" fn create_instance( VK_SUCCESS } -pub extern "system" fn destroy_instance( +pub(crate) extern "system" fn destroy_instance( instance: VkInstance, 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, create_info: *const VkDeviceCreateInfo<'_>, allocator: *const VkAllocationCallbacks, @@ -257,26 +262,65 @@ pub extern "system" fn create_device( 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() }; write_log(format!("{:?}", ext_names)); + let device = match Device::preinitialized(unsafe { *device }, proc_addr, pdev, &ext_names) { + Ok(dev) => dev, + Err(err) => { + write_log(format!("-> local device creation failed: {:?}", err)); + 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( - match Device::preinitialized(*device, proc_addr, pdev, &ext_names) { - Ok(dev) => dev, - Err(err) => { - write_log(format!("-> local device creation failed: {:?}", err)); - return VK_ERROR_INITIALIZATION_FAILED; - } - }, - ); + OVERLAY.set_device(device); + OVERLAY.set_queue(queue); } 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 =================="); 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, create_info: *const VkSwapchainCreateInfoKHR, _allocator: *const VkAllocationCallbacks, @@ -307,9 +351,10 @@ pub extern "system" fn create_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 = match unsafe { Swapchain::from_raw(OVERLAY.device(), &*create_info, *p_swapchain) } { Ok(swapchain) => swapchain, @@ -327,12 +372,11 @@ pub extern "system" fn create_swapchain( } write_log("-> created renderer"); - // } VK_SUCCESS } -pub extern "system" fn submit_queue( +pub(crate) extern "system" fn submit_queue( queue: VkQueue, submit_count: u32, submits: *const VkSubmitInfo, @@ -341,31 +385,37 @@ pub extern "system" fn submit_queue( write_log(" ================== vulkan layer submit queue =================="); unsafe { - let command_buffers = slice::from_raw_parts( - (*submits).pCommandBuffers, - (*submits).commandBufferCount as usize, - ); + write_log(format!( + "submit queue ({:?}), internal queue: ({:?})", + queue, + OVERLAY.queue().lock().unwrap().vk_handle() + )); - let cb = match OVERLAY.render() { - Ok(cb) => cb, - Err(err) => { - write_log(format!("overlay rendering failed: {:?}", err)); - return VK_ERROR_DEVICE_LOST; - } - }; + // let command_buffers = slice::from_raw_parts( + // (*submits).pCommandBuffers, + // (*submits).commandBufferCount as usize, + // ); - 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; - (*mut_ptr).pCommandBuffers = cb.as_ptr(); + // let mut_ptr = submits as *mut VkSubmitInfo; - 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, queue_family_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); write_log(format!("new queue: {:?}", unsafe { *p_queue })); - unsafe { - if !OVERLAY.has_queue() { - let arc_device = OVERLAY.device(); - OVERLAY.add_queue(Queue::new( - arc_device, - *p_queue, - queue_family_index, - queue_index, - )); - } + write_log(format!( + "internal queue: ({:?})", + OVERLAY.queue().lock().unwrap().vk_handle() + )); } + + // 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, swapchain: VkSwapchainKHR, timeout: u64, @@ -417,7 +472,11 @@ pub extern "system" fn acquire_next_image( Some(fence), ); - write_log(format!("acquire res: {:?}", res)); + write_log(format!( + "acquire (swapchain: {:?}) res: {:?}", + sc.vk_handle(), + 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, present_info: *const VkPresentInfoKHR, ) -> VkResult { @@ -450,6 +509,30 @@ pub extern "system" fn present_queue( write_log(format!("iq: {:?}, cq: {:?}", queue, unsafe { 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") { Some(pfn) => unsafe { mem::transmute(pfn) }, @@ -463,7 +546,11 @@ pub extern "system" fn present_queue( } 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()) {} } } diff --git a/src/overlay/mod.rs b/src/overlay/mod.rs index c6a4623..d35c22f 100644 --- a/src/overlay/mod.rs +++ b/src/overlay/mod.rs @@ -51,7 +51,7 @@ impl Overlay { self.queue.is_some() } - pub fn add_queue(&mut self, queue: Arc>) { + pub fn set_queue(&mut self, queue: Arc>) { self.queue = Some(queue); } @@ -85,17 +85,17 @@ impl Overlay { Ok(()) } - pub fn render(&mut self) -> Result { + pub fn render(&mut self) -> Result<()> { if self.rfactor_data.is_none() { - // self.rfactor_data = RFactorData::new( - // self.device(), - // self.rendering - // .as_mut() - // .unwrap() - // .single_color_pipeline() - // .descriptor_layout(), - // ) - // .ok(); + self.rfactor_data = RFactorData::new( + self.device(), + self.rendering + .as_mut() + .unwrap() + .single_color_pipeline() + .descriptor_layout(), + ) + .ok(); } // check twice for rfactor data, because of borrowing rules diff --git a/src/overlay/rendering.rs b/src/overlay/rendering.rs index 70a7182..55d9d5c 100644 --- a/src/overlay/rendering.rs +++ b/src/overlay/rendering.rs @@ -2,7 +2,10 @@ use anyhow::Result; use cgmath::{Vector2, Vector4}; use vulkan_rs::prelude::*; -use std::sync::{Arc, Mutex}; +use std::{ + sync::{Arc, Mutex}, + time::Duration, +}; use super::{pipeline::SingleColorPipeline, rfactor_data::RenderObject}; use crate::write_log; @@ -48,6 +51,9 @@ pub struct Rendering { pipeline: SingleColorPipeline, render_target: RenderTarget, command_buffer: Arc, + + device: Arc, + queue: Arc>, } impl Rendering { @@ -91,7 +97,10 @@ impl Rendering { swapchain, pipeline: SingleColorPipeline::new(device.clone(), render_target.render_pass())?, 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, swapchain: Arc, objects: &[&dyn RenderObject], - ) -> Result { + ) -> Result<()> { let image_index = self.swapchain.current_index(); let viewport = [VkViewport { @@ -153,6 +162,9 @@ impl Rendering { 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(()) } } diff --git a/src/overlay/rfactor_data.rs b/src/overlay/rfactor_data.rs index f47de8d..576ac00 100644 --- a/src/overlay/rfactor_data.rs +++ b/src/overlay/rfactor_data.rs @@ -14,8 +14,8 @@ pub trait RenderObject { pub struct RFactorData { // rf2 memory mapped data - telemetry_reader: TelemetryReader, - scoring_reader: ScoringReader, + // telemetry_reader: TelemetryReader, + // scoring_reader: ScoringReader, // radar objects background: RadarObject, @@ -30,9 +30,8 @@ impl RFactorData { let car_width = 0.025; Ok(Self { - telemetry_reader: TelemetryReader::new()?, - scoring_reader: ScoringReader::new()?, - + // telemetry_reader: TelemetryReader::new()?, + // scoring_reader: ScoringReader::new()?, background: RadarObject::new( device.clone(), descriptor_layout,