rFactor2_vk_hud/src/overlay/mod.rs

116 lines
2.8 KiB
Rust
Raw Normal View History

2023-01-12 16:45:06 +00:00
use crate::write_log;
use self::{rendering::Rendering, rfactor_data::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-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-12 16:45:06 +00:00
pub fn has_queue(&self) -> bool {
self.queue.is_some()
}
2023-01-12 12:52:44 +00:00
pub fn add_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<()> {
write_log("-> create rendering: start");
self.rendering = None;
write_log("-> create rendering: old cleared");
self.rendering = Some(Rendering::new(self.device(), self.queue(), swapchain)?);
write_log("-> create rendering: new created");
write_log("-> create rendering: end");
2023-01-12 09:10:09 +00:00
Ok(())
}
2023-01-13 14:00:01 +00:00
pub fn render(&mut self) -> Result<VkCommandBuffer> {
if self.rfactor_data.is_none() {
2023-01-13 14:00:01 +00:00
// 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
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
let swapchain = self.rendering.as_ref().unwrap().swapchain().clone();
2023-01-12 12:52:44 +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
}
}