rFactor2_vk_hud/src/overlay/rendering.rs

92 lines
2.2 KiB
Rust
Raw Normal View History

2023-01-12 09:10:09 +00:00
use anyhow::Result;
use vulkan_rs::prelude::*;
2023-01-14 07:08:40 +00:00
use std::{
sync::{Arc, Mutex},
time::Duration,
};
2023-01-12 12:52:44 +00:00
2023-01-12 16:45:06 +00:00
use crate::write_log;
2023-01-12 09:10:09 +00:00
pub struct Rendering {
swapchain: Arc<Swapchain>,
2023-01-17 06:57:56 +00:00
images: Vec<Arc<Image>>,
2023-01-14 07:08:40 +00:00
queue: Arc<Mutex<Queue>>,
2023-01-17 11:18:53 +00:00
render_callbacks: Vec<Box<dyn Fn(u32) -> Result<Arc<CommandBuffer>>>>,
2023-01-12 09:10:09 +00:00
}
impl Rendering {
2023-01-17 11:18:53 +00:00
pub fn new(queue: Arc<Mutex<Queue>>, swapchain: Arc<Swapchain>) -> Result<Self> {
2023-01-14 19:15:43 +00:00
crate::write_log!("-> Rendering ctor: begin");
2023-01-20 07:14:51 +00:00
2023-01-12 16:45:06 +00:00
let vk_images = swapchain.vk_images()?;
2023-01-20 07:14:51 +00:00
2023-01-14 19:15:43 +00:00
write_log!(format!(
2023-01-12 16:45:06 +00:00
"-> Rendering ctor: vk images ({})",
vk_images.len()
));
2023-01-20 07:14:51 +00:00
2023-01-13 07:47:30 +00:00
let images = match swapchain.wrap_images(&vk_images, &queue, true) {
2023-01-12 16:45:06 +00:00
Ok(images) => images,
Err(err) => {
2023-01-14 19:15:43 +00:00
write_log!(format!("-> Rendering ctor: failed wrapper: {:?}", err));
2023-01-12 16:45:06 +00:00
return Err(err);
}
};
2023-01-12 12:52:44 +00:00
2023-01-20 07:14:51 +00:00
write_log!("-> Rendering ctor: wrapped images");
2023-01-12 16:45:06 +00:00
2023-01-14 19:15:43 +00:00
write_log!(format!(
2023-01-13 08:27:58 +00:00
"-> Rendering swapchain extents ({}, {})",
swapchain.width(),
swapchain.height(),
));
2023-01-12 12:52:44 +00:00
Ok(Self {
swapchain,
2023-01-17 06:57:56 +00:00
images,
2023-01-14 07:08:40 +00:00
queue,
2023-01-17 11:18:53 +00:00
render_callbacks: Vec::new(),
2023-01-12 12:52:44 +00:00
})
}
pub fn swapchain(&self) -> &Arc<Swapchain> {
&self.swapchain
2023-01-12 09:10:09 +00:00
}
2023-01-17 11:18:53 +00:00
pub fn add_render_callback<F>(&mut self, f: F)
where
F: Fn(u32) -> Result<Arc<CommandBuffer>> + 'static,
{
self.render_callbacks.push(Box::new(f));
}
2023-01-17 11:18:53 +00:00
pub fn render(&self) -> Result<()> {
2023-01-12 16:45:06 +00:00
let image_index = self.swapchain.current_index();
2023-01-17 11:18:53 +00:00
let command_buffers: Vec<Arc<CommandBuffer>> = self
.render_callbacks
.iter()
.map(|c| c(image_index))
.collect::<Result<Vec<Arc<CommandBuffer>>>>()?;
2023-01-12 16:45:06 +00:00
2023-01-17 14:44:11 +00:00
write_log!(format!(
"submitting {} commandbuffer(s)",
command_buffers.len()
));
2023-01-14 07:08:40 +00:00
let queue = self.queue.lock().unwrap();
2023-01-17 11:18:53 +00:00
queue.minimal_submit(Duration::from_secs(10), &command_buffers)?;
2023-01-14 07:08:40 +00:00
Ok(())
2023-01-12 09:10:09 +00:00
}
2023-01-17 06:57:56 +00:00
pub fn images(&self) -> &Vec<Arc<Image>> {
&self.images
}
2023-01-12 09:10:09 +00:00
}