use crate::write_log; use self::{ rendering::Rendering, rfactor_data::{RFactorData, RadarConfig}, }; mod pipeline; mod rendering; mod rfactor_data; use anyhow::Result; use assetpath::AssetPath; use std::sync::{Arc, Mutex}; use ui::prelude::*; use vulkan_rs::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize)] pub struct OverlayConfig { pub radar_config: RadarConfig, pub font_path: String, } impl OverlayConfig { pub const fn new() -> Self { Self { radar_config: RadarConfig::new(), font_path: String::new(), } } } pub struct Overlay { config: OverlayConfig, instance: Option>, device: Option>, queue: Option>>, rendering: Option, gui_handler: Option>, rfactor_data: Option, } impl Overlay { pub const fn new() -> Self { Self { config: OverlayConfig::new(), instance: None, device: None, queue: None, rendering: None, gui_handler: None, rfactor_data: None, } } pub fn set_config(&mut self, config: OverlayConfig) { self.config = config; } pub fn set_instance(&mut self, instance: Arc) { self.instance = Some(instance); } pub fn instance(&self) -> Arc { self.instance.as_ref().unwrap().clone() } pub fn set_device(&mut self, device: Arc) { self.device = Some(device); } pub fn device(&self) -> Arc { self.device.as_ref().unwrap().clone() } pub fn set_queue(&mut self, queue: Arc>) { self.queue = Some(queue); } pub fn queue(&self) -> Arc> { self.queue.as_ref().unwrap().clone() } 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<()> { write_log!("-> create rendering: start"); self.rendering = None; write_log!("-> create rendering: old cleared"); let rendering = Rendering::new(self.device(), self.queue(), swapchain.clone())?; // only font is used let mut create_info = GuiHandlerCreateInfo::default(); create_info.font_path = AssetPath::from(self.config.font_path.clone()); create_info.font_path.assume_prefix_free(); let ctx = Arc::new(ContextImpl::new( self.device(), self.queue(), swapchain, rendering.images().clone(), )); self.gui_handler = Some(GuiHandler::new( create_info, &(ctx as Arc), )?); self.rendering = Some(rendering); write_log!("-> create rendering: new created"); write_log!("-> create rendering: end"); Ok(()) } pub fn render(&mut self) -> Result<()> { let swapchain = self.rendering.as_ref().unwrap().swapchain().clone(); if self.rfactor_data.is_none() { self.rfactor_data = RFactorData::new( self.config.radar_config, self.device(), self.rendering .as_mut() .unwrap() .single_color_pipeline() .descriptor_layout(), swapchain.width(), swapchain.height(), ) .ok(); write_log!("created RFactorData"); } // check twice for rfactor data, because of borrowing rules if let Some(rfactor) = &mut self.rfactor_data { rfactor.update()?; } let objects = match &self.rfactor_data { Some(rfactor) => rfactor.objects(), None => Vec::new(), }; self.rendering.as_mut().unwrap().render(swapchain, &objects) } } struct ContextImpl { device: Arc, queue: Arc>, swapchain: Arc, images: Vec>, } impl ContextImpl { fn new( device: Arc, queue: Arc>, swapchain: Arc, images: Vec>, ) -> Self { Self { device, queue, swapchain, images, } } } impl ContextInterface for ContextImpl { fn device(&self) -> &Arc { &self.device } fn queue(&self) -> &Arc> { &self.queue } fn format(&self) -> VkFormat { self.swapchain.format() } fn image_layout(&self) -> VkImageLayout { VK_IMAGE_LAYOUT_PRESENT_SRC_KHR } fn image_count(&self) -> usize { self.images.len() } fn images(&self) -> TargetMode>> { TargetMode::Mono(self.images.clone()) } fn width(&self) -> u32 { self.swapchain.width() } fn height(&self) -> u32 { self.swapchain.height() } }