Add single submit wrapper

This commit is contained in:
hodasemi 2023-01-20 07:24:54 +01:00
parent f28a2184f7
commit 530f0d4e03
3 changed files with 76 additions and 0 deletions

View file

@ -42,6 +42,7 @@ pub mod swapchain;
pub mod ffi;
mod sampler_manager;
mod single_submit;
#[derive(Clone, Debug)]
pub enum OutOfDate<T> {

View file

@ -47,3 +47,5 @@ pub use super::render_target::{
sub_pass::{ClearValue, CustomTarget, SubPass, SubPassBuilder},
RenderTarget,
};
pub use super::single_submit::SingleSubmit;

View file

@ -0,0 +1,73 @@
use crate::prelude::*;
use anyhow::Result;
use std::sync::{Arc, Mutex};
use std::time::Duration;
pub struct SingleSubmit<'a, F, T>
where
F: FnOnce(&mut CommandBufferRecorder<'_>) -> Result<T>,
{
command_buffer: &'a Arc<CommandBuffer>,
queue: &'a Arc<Mutex<Queue>>,
f: F,
timeout: Option<Duration>,
}
impl<'a, F, T> SingleSubmit<'a, F, T>
where
F: FnOnce(&mut CommandBufferRecorder<'_>) -> Result<T>,
{
pub fn builder(
command_buffer: &'a Arc<CommandBuffer>,
queue: &'a Arc<Mutex<Queue>>,
f: F,
) -> Self {
SingleSubmit {
command_buffer,
queue,
f,
timeout: None,
}
}
pub fn wait_for_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
pub fn submit(self) -> Result<T> {
let result = {
let mut buffer_recorder = self.command_buffer.begin(VkCommandBufferBeginInfo::new(
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
))?;
(self.f)(&mut buffer_recorder)?
};
let submit = SubmitInfo::default().add_command_buffer(self.command_buffer);
let queue_lock = self.queue.lock().unwrap();
match self.timeout {
Some(timeout) => {
let fence = Fence::builder().build(self.command_buffer.device().clone())?;
queue_lock.submit(Some(&fence), &[submit])?;
self.command_buffer
.device()
.wait_for_fences(&[&fence], true, timeout)?;
}
None => {
queue_lock.submit(None, &[submit])?;
queue_lock.wait_idle()?;
}
}
Ok(result)
}
}