rFactor2_vk_hud/src/overlay/mod.rs

126 lines
3.1 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,
rfactor_data::{DataConfig, RFactorData},
};
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-14 19:15:43 +00:00
use cgmath::vec3;
2023-01-12 12:52:44 +00:00
use std::sync::{Arc, Mutex};
2023-01-12 09:10:09 +00:00
use vulkan_rs::prelude::*;
pub struct Overlay {
instance: Option<Arc<Instance>>,
device: Option<Arc<Device>>,
2023-01-12 16:45:06 +00:00
queue: Option<Arc<Mutex<Queue>>>,
rendering: Option<Rendering>,
rfactor_data: Option<RFactorData>,
2023-01-12 09:10:09 +00:00
}
impl Overlay {
pub const fn new() -> Self {
Self {
instance: None,
device: None,
2023-01-12 16:45:06 +00:00
queue: None,
rendering: None,
rfactor_data: None,
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
self.rendering = Some(Rendering::new(self.device(), self.queue(), swapchain)?);
2023-01-14 19:15:43 +00:00
write_log!("-> create rendering: new created");
2023-01-12 16:45:06 +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-14 19:15:43 +00:00
DataConfig {
radar_scale: 1.0,
radar_car_distance: 20.0,
safe_color: vec3(0.0, 0.75, 0.0),
danger_color: vec3(0.75, 0.0, 0.0),
},
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
}
}