rFactor2_vk_hud/src/overlay/mod.rs

225 lines
5.2 KiB
Rust
Raw Normal View History

2023-01-12 16:45:06 +00:00
use crate::write_log;
2023-01-14 19:15:43 +00:00
use self::{
rendering::Rendering,
2023-01-17 06:57:56 +00:00
rfactor_data::{RFactorData, RadarConfig},
2023-01-14 19:15:43 +00:00
};
2023-01-12 09:10:09 +00:00
2023-01-12 12:52:44 +00:00
mod pipeline;
2023-01-12 09:10:09 +00:00
mod rendering;
2023-01-12 16:45:06 +00:00
mod rfactor_data;
2023-01-12 09:10:09 +00:00
use anyhow::Result;
2023-01-17 06:57:56 +00:00
use assetpath::AssetPath;
2023-01-12 12:52:44 +00:00
use std::sync::{Arc, Mutex};
2023-01-17 06:57:56 +00:00
use ui::prelude::*;
2023-01-12 09:10:09 +00:00
use vulkan_rs::prelude::*;
2023-01-16 16:07:39 +00:00
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct OverlayConfig {
2023-01-17 06:57:56 +00:00
pub radar_config: RadarConfig,
pub font_path: String,
2023-01-16 16:07:39 +00:00
}
impl OverlayConfig {
pub const fn new() -> Self {
Self {
2023-01-17 06:57:56 +00:00
radar_config: RadarConfig::new(),
font_path: String::new(),
2023-01-16 16:07:39 +00:00
}
}
}
2023-01-12 09:10:09 +00:00
pub struct Overlay {
2023-01-16 16:07:39 +00:00
config: OverlayConfig,
2023-01-12 09:10:09 +00:00
instance: Option<Arc<Instance>>,
device: Option<Arc<Device>>,
2023-01-12 16:45:06 +00:00
queue: Option<Arc<Mutex<Queue>>>,
rendering: Option<Rendering>,
2023-01-17 06:57:56 +00:00
gui_handler: Option<Arc<GuiHandler>>,
2023-01-12 16:45:06 +00:00
rfactor_data: Option<RFactorData>,
2023-01-12 09:10:09 +00:00
}
impl Overlay {
pub const fn new() -> Self {
Self {
2023-01-16 16:07:39 +00:00
config: OverlayConfig::new(),
2023-01-12 09:10:09 +00:00
instance: None,
device: None,
2023-01-12 16:45:06 +00:00
queue: None,
rendering: None,
2023-01-17 06:57:56 +00:00
gui_handler: None,
2023-01-12 16:45:06 +00:00
rfactor_data: None,
2023-01-12 09:10:09 +00:00
}
}
2023-01-16 16:07:39 +00:00
pub fn set_config(&mut self, config: OverlayConfig) {
self.config = config;
}
2023-01-12 09:10:09 +00:00
pub fn set_instance(&mut self, instance: Arc<Instance>) {
self.instance = Some(instance);
}
pub fn instance(&self) -> Arc<Instance> {
self.instance.as_ref().unwrap().clone()
}
pub fn set_device(&mut self, device: Arc<Device>) {
self.device = Some(device);
}
pub fn device(&self) -> Arc<Device> {
self.device.as_ref().unwrap().clone()
}
2023-01-14 07:08:40 +00:00
pub fn set_queue(&mut self, queue: Arc<Mutex<Queue>>) {
2023-01-12 16:45:06 +00:00
self.queue = Some(queue);
2023-01-12 12:52:44 +00:00
}
2023-01-12 16:45:06 +00:00
pub fn queue(&self) -> Arc<Mutex<Queue>> {
self.queue.as_ref().unwrap().clone()
2023-01-12 12:52:44 +00:00
}
2023-01-13 14:00:01 +00:00
pub fn swapchain(&self, swapchain: VkSwapchainKHR) -> Option<&Arc<Swapchain>> {
let sc = self.rendering.as_ref().unwrap().swapchain();
if sc.vk_handle() == swapchain {
Some(sc)
} else {
None
}
2023-01-12 12:52:44 +00:00
}
2023-01-12 16:45:06 +00:00
pub fn create_rendering(&mut self, swapchain: Arc<Swapchain>) -> Result<()> {
2023-01-14 19:15:43 +00:00
write_log!("-> create rendering: start");
2023-01-12 16:45:06 +00:00
self.rendering = None;
2023-01-14 19:15:43 +00:00
write_log!("-> create rendering: old cleared");
2023-01-12 16:45:06 +00:00
2023-01-17 06:57:56 +00:00
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<dyn ContextInterface>),
)?);
self.rendering = Some(rendering);
2023-01-12 16:45:06 +00:00
2023-01-14 19:15:43 +00:00
write_log!("-> create rendering: new created");
2023-01-17 06:57:56 +00:00
2023-01-14 19:15:43 +00:00
write_log!("-> create rendering: end");
2023-01-12 09:10:09 +00:00
Ok(())
}
2023-01-14 07:08:40 +00:00
pub fn render(&mut self) -> Result<()> {
2023-01-14 19:15:43 +00:00
let swapchain = self.rendering.as_ref().unwrap().swapchain().clone();
if self.rfactor_data.is_none() {
2023-01-14 07:08:40 +00:00
self.rfactor_data = RFactorData::new(
2023-01-17 06:57:56 +00:00
self.config.radar_config,
2023-01-14 07:08:40 +00:00
self.device(),
self.rendering
.as_mut()
.unwrap()
.single_color_pipeline()
.descriptor_layout(),
2023-01-14 19:15:43 +00:00
swapchain.width(),
swapchain.height(),
2023-01-14 07:08:40 +00:00
)
.ok();
2023-01-15 05:58:23 +00:00
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(),
};
2023-01-12 16:45:06 +00:00
2023-01-13 14:00:01 +00:00
self.rendering.as_mut().unwrap().render(swapchain, &objects)
2023-01-12 09:10:09 +00:00
}
}
2023-01-17 06:57:56 +00:00
struct ContextImpl {
device: Arc<Device>,
queue: Arc<Mutex<Queue>>,
swapchain: Arc<Swapchain>,
images: Vec<Arc<Image>>,
}
impl ContextImpl {
fn new(
device: Arc<Device>,
queue: Arc<Mutex<Queue>>,
swapchain: Arc<Swapchain>,
images: Vec<Arc<Image>>,
) -> Self {
Self {
device,
queue,
swapchain,
images,
}
}
}
impl ContextInterface for ContextImpl {
fn device(&self) -> &Arc<Device> {
&self.device
}
fn queue(&self) -> &Arc<Mutex<Queue>> {
&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<Vec<Arc<Image>>> {
TargetMode::Mono(self.images.clone())
}
fn width(&self) -> u32 {
self.swapchain.width()
}
fn height(&self) -> u32 {
self.swapchain.height()
}
}