From 9fd1275ce3f95dc6e8e47dd2ed1602336d0b1899 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 13 Jan 2023 15:00:01 +0100 Subject: [PATCH] More testing --- Cargo.toml | 3 +- src/enums.rs | 1 + src/lib.rs | 97 ++++++++++++++++++++++++++++++++++------ src/overlay/mod.rs | 39 ++++++++-------- src/overlay/pipeline.rs | 7 --- src/overlay/rendering.rs | 23 ++++------ 6 files changed, 115 insertions(+), 55 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6214877..948fb30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,8 @@ 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" } +rfactor_sm_reader = { git = "https://gavania.de/hodasemi/rfactor_sm_reader.git" } + anyhow = { version = "1.0.68", features = ["backtrace"] } -rfactor_sm_reader = { path = "../rfactor_sm_reader" } cgmath = "0.18.0" paste = "1.0.11" diff --git a/src/enums.rs b/src/enums.rs index ce26d3a..813d5ed 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -67,4 +67,5 @@ create_functions_enum!( [QueueSubmit, submit_queue], [GetDeviceQueue, get_device_queue], [AcquireNextImageKHR, acquire_next_image], + [QueuePresentKHR, present_queue], ); diff --git a/src/lib.rs b/src/lib.rs index ad12871..f9c5b62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,8 @@ const LOG_FILE: &'static str = "/home/michael/rf2_vk_hud.log"; static mut OVERLAY: Overlay = Overlay::new(); static mut QUEUE_SUBMIT: PFN_vkQueueSubmit = unsafe { mem::transmute(vkVoidFunction as *const c_void) }; +static mut ACQUIRE_NEXT_IMAGE: PFN_vkAcquireNextImageKHR = + unsafe { mem::transmute(vkVoidFunction as *const c_void) }; #[no_mangle] #[allow(non_snake_case)] @@ -237,7 +239,15 @@ pub extern "system" fn create_device( write_log("failed querying vkQueueSubmit"); return VK_ERROR_INITIALIZATION_FAILED; } - } + }; + + ACQUIRE_NEXT_IMAGE = match vk_handles().handle("vkAcquireNextImageKHR") { + Some(acquire_next_image) => mem::transmute(acquire_next_image), + None => { + write_log("failed querying vkAcquireNextImageKHR"); + return VK_ERROR_INITIALIZATION_FAILED; + } + }; }; let pdev = match PhysicalDevice::from_raw(unsafe { OVERLAY.instance() }, physical_device) { @@ -328,24 +338,30 @@ pub extern "system" fn submit_queue( submits: *const VkSubmitInfo, fence: VkFence, ) -> VkResult { + write_log(" ================== vulkan layer submit queue =================="); + unsafe { - let input_submit_info = slice::from_raw_parts(submits, submit_count as usize); - let overlay_submit = match OVERLAY.render() { - Ok(submit) => submit, + let command_buffers = slice::from_raw_parts( + (*submits).pCommandBuffers, + (*submits).commandBufferCount as usize, + ); + + let cb = match OVERLAY.render() { + Ok(cb) => cb, Err(err) => { write_log(format!("overlay rendering failed: {:?}", err)); return VK_ERROR_DEVICE_LOST; } }; - let complete_submits = [input_submit_info, &[overlay_submit]].concat(); + let cb = [command_buffers, &[cb]].concat(); - QUEUE_SUBMIT( - queue, - complete_submits.len() as u32, - complete_submits.as_ptr(), - fence, - ) + let mut_ptr = submits as *mut VkSubmitInfo; + + (*mut_ptr).commandBufferCount = cb.len() as u32; + (*mut_ptr).pCommandBuffers = cb.as_ptr(); + + QUEUE_SUBMIT(queue, submit_count, mut_ptr, fence) } } @@ -366,6 +382,7 @@ 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() { @@ -386,9 +403,63 @@ pub extern "system" fn acquire_next_image( timeout: u64, semaphore: VkSemaphore, fence: VkFence, - pImageIndex: *mut u32, + image_index: *mut u32, ) -> VkResult { - VK_SUCCESS + unsafe { + match OVERLAY.swapchain(swapchain) { + Some(sc) => { + let device = OVERLAY.device(); + + let res = device.acquire_next_image( + sc.vk_handle(), + timeout, + Some(semaphore), + Some(fence), + ); + + write_log(format!("acquire res: {:?}", res)); + + match res { + Ok(res) => match res { + OutOfDate::Ok(index) => { + sc.set_image_index(index); + VK_SUCCESS + } + OutOfDate::OutOfDate => VK_ERROR_OUT_OF_DATE_KHR, + OutOfDate::TimeOut => VK_TIMEOUT, + }, + Err(err) => { + write_log(format!("failed acquiring next image {:?}", err)); + VK_ERROR_DEVICE_LOST + } + } + } + None => { + write_log("acquired other swapchain image"); + ACQUIRE_NEXT_IMAGE(device, swapchain, timeout, semaphore, fence, image_index) + } + } + } +} + +pub extern "system" fn present_queue( + queue: VkQueue, + present_info: *const VkPresentInfoKHR, +) -> VkResult { + write_log(" ================== vulkan layer queue present =================="); + write_log(format!("iq: {:?}, cq: {:?}", queue, unsafe { + OVERLAY.queue().lock().unwrap().vk_handle() + })); + + let pfn: PFN_vkQueuePresentKHR = match vk_handles().handle("vkQueuePresentKHR") { + Some(pfn) => unsafe { mem::transmute(pfn) }, + None => { + write_log("failed querying vkQueuePresentKHR"); + return VK_ERROR_DEVICE_LOST; + } + }; + + pfn(queue, present_info) } pub fn write_log(msg: impl ToString) { diff --git a/src/overlay/mod.rs b/src/overlay/mod.rs index 91376b5..c6a4623 100644 --- a/src/overlay/mod.rs +++ b/src/overlay/mod.rs @@ -59,8 +59,14 @@ impl Overlay { self.queue.as_ref().unwrap().clone() } - pub fn swapchain(&self) -> &Arc { - self.rendering.as_ref().unwrap().swapchain() + pub fn swapchain(&self, swapchain: VkSwapchainKHR) -> Option<&Arc> { + let sc = self.rendering.as_ref().unwrap().swapchain(); + + if sc.vk_handle() == swapchain { + Some(sc) + } else { + None + } } pub fn create_rendering(&mut self, swapchain: Arc) -> Result<()> { @@ -79,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 @@ -102,13 +108,8 @@ impl Overlay { None => Vec::new(), }; - let device = self.device(); - let queue = self.queue(); - let swapchain = self.swapchain().clone(); + let swapchain = self.rendering.as_ref().unwrap().swapchain().clone(); - self.rendering - .as_mut() - .unwrap() - .render(device, queue, swapchain, &objects) + self.rendering.as_mut().unwrap().render(swapchain, &objects) } } diff --git a/src/overlay/pipeline.rs b/src/overlay/pipeline.rs index 97de372..fca50bf 100644 --- a/src/overlay/pipeline.rs +++ b/src/overlay/pipeline.rs @@ -7,11 +7,7 @@ use super::rendering::PositionOnlyVertex; pub struct SingleColorPipeline { pipeline: Arc, - pipeline_layout: Arc, descriptor_layout: Arc, - - vertex_shader: Arc, - fragment_shader: Arc, } impl SingleColorPipeline { @@ -67,11 +63,8 @@ impl SingleColorPipeline { .build(device, &pipeline_layout, &renderpass, 0)?; Ok(Self { - vertex_shader, - fragment_shader, descriptor_layout, pipeline, - pipeline_layout, }) } diff --git a/src/overlay/rendering.rs b/src/overlay/rendering.rs index 03608e6..70a7182 100644 --- a/src/overlay/rendering.rs +++ b/src/overlay/rendering.rs @@ -47,7 +47,7 @@ pub struct Rendering { swapchain: Arc, pipeline: SingleColorPipeline, render_target: RenderTarget, - submit_info: SubmitInfo, + command_buffer: Arc, } impl Rendering { @@ -89,9 +89,9 @@ impl Rendering { Ok(Self { swapchain, - pipeline: SingleColorPipeline::new(device, render_target.render_pass())?, + pipeline: SingleColorPipeline::new(device.clone(), render_target.render_pass())?, render_target, - submit_info: SubmitInfo::default(), + command_buffer: CommandBuffer::new_primary().build(device, queue)?, }) } @@ -105,11 +105,9 @@ impl Rendering { pub fn render( &mut self, - device: Arc, - queue: Arc>, swapchain: Arc, objects: &[&dyn RenderObject], - ) -> Result { + ) -> Result { let image_index = self.swapchain.current_index(); let viewport = [VkViewport { @@ -129,11 +127,10 @@ impl Rendering { }, }]; - let command_buffer = CommandBuffer::new_primary().build(device, queue)?; - { - let mut recorder = command_buffer.begin(VkCommandBufferBeginInfo::new( - VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, + let mut recorder = self.command_buffer.begin(VkCommandBufferBeginInfo::new( + VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT + | VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, ))?; self.render_target @@ -156,10 +153,6 @@ impl Rendering { self.render_target.end(&recorder); } - self.submit_info = SubmitInfo::default() - .add_wait_stage(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT) - .add_command_buffer(command_buffer); - - Ok(self.submit_info.as_vk_submit()) + Ok(self.command_buffer.vk_handle()) } }