From aaf0eadc610557bc96de15f4fd174d0a40d28a9e Mon Sep 17 00:00:00 2001 From: Rua Date: Mon, 6 Jun 2022 00:45:37 +0200 Subject: [PATCH] Add `is_signaled` to `FenceSignalFuture` (#1910) --- .../src/command_buffer/submit/queue_submit.rs | 4 +-- vulkano/src/sync/fence.rs | 28 +++++++++---------- vulkano/src/sync/future/fence_signal.rs | 15 +++++++++- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/vulkano/src/command_buffer/submit/queue_submit.rs b/vulkano/src/command_buffer/submit/queue_submit.rs index 823d07ad..ee0f84e6 100644 --- a/vulkano/src/command_buffer/submit/queue_submit.rs +++ b/vulkano/src/command_buffer/submit/queue_submit.rs @@ -313,14 +313,14 @@ mod tests { let (device, queue) = gfx_dev_and_queue!(); let fence = Fence::new(device.clone(), Default::default()).unwrap(); - assert!(!fence.ready().unwrap()); + assert!(!fence.is_signaled().unwrap()); let mut builder = SubmitCommandBufferBuilder::new(); builder.set_fence_signal(&fence); builder.submit(&queue).unwrap(); fence.wait(Some(Duration::from_secs(5))).unwrap(); - assert!(fence.ready().unwrap()); + assert!(fence.is_signaled().unwrap()); } } diff --git a/vulkano/src/sync/fence.rs b/vulkano/src/sync/fence.rs index b774570e..e477c457 100644 --- a/vulkano/src/sync/fence.rs +++ b/vulkano/src/sync/fence.rs @@ -38,7 +38,7 @@ pub struct Fence { // If true, we know that the `Fence` is signaled. If false, we don't know. // This variable exists so that we don't need to call `vkGetFenceStatus` or `vkWaitForFences` // multiple times. - signaled: AtomicBool, + is_signaled: AtomicBool, // Indicates whether this fence was taken from the fence pool. // If true, will be put back into fence pool on drop. @@ -76,7 +76,7 @@ impl Fence { Ok(Fence { handle, device, - signaled: AtomicBool::new(signaled), + is_signaled: AtomicBool::new(signaled), must_put_in_pool: false, }) } @@ -104,7 +104,7 @@ impl Fence { Fence { handle, device, - signaled: AtomicBool::new(false), + is_signaled: AtomicBool::new(false), must_put_in_pool: true, } } @@ -121,9 +121,9 @@ impl Fence { /// Returns true if the fence is signaled. #[inline] - pub fn ready(&self) -> Result { + pub fn is_signaled(&self) -> Result { unsafe { - if self.signaled.load(Ordering::Relaxed) { + if self.is_signaled.load(Ordering::Relaxed) { return Ok(true); } @@ -134,7 +134,7 @@ impl Fence { ))?; match result { Success::Success => { - self.signaled.store(true, Ordering::Relaxed); + self.is_signaled.store(true, Ordering::Relaxed); Ok(true) } Success::NotReady => Ok(false), @@ -150,7 +150,7 @@ impl Fence { /// If you pass a duration of 0, then the function will return without blocking. pub fn wait(&self, timeout: Option) -> Result<(), FenceWaitError> { unsafe { - if self.signaled.load(Ordering::Relaxed) { + if self.is_signaled.load(Ordering::Relaxed) { return Ok(()); } @@ -174,7 +174,7 @@ impl Fence { match r { Success::Success => { - self.signaled.store(true, Ordering::Relaxed); + self.is_signaled.store(true, Ordering::Relaxed); Ok(()) } Success::Timeout => Err(FenceWaitError::Timeout), @@ -207,7 +207,7 @@ impl Fence { ), }; - if fence.signaled.load(Ordering::Relaxed) { + if fence.is_signaled.load(Ordering::Relaxed) { None } else { Some(fence.handle) @@ -258,7 +258,7 @@ impl Fence { 1, &self.handle, ))?; - self.signaled.store(false, Ordering::Relaxed); + self.is_signaled.store(false, Ordering::Relaxed); Ok(()) } } @@ -288,7 +288,7 @@ impl Fence { ), }; - fence.signaled.store(false, Ordering::Relaxed); + fence.is_signaled.store(false, Ordering::Relaxed); fence.handle }) .collect(); @@ -438,7 +438,7 @@ mod tests { let (device, _) = gfx_dev_and_queue!(); let fence = Fence::new(device.clone(), Default::default()).unwrap(); - assert!(!fence.ready().unwrap()); + assert!(!fence.is_signaled().unwrap()); } #[test] @@ -453,7 +453,7 @@ mod tests { }, ) .unwrap(); - assert!(fence.ready().unwrap()); + assert!(fence.is_signaled().unwrap()); } #[test] @@ -484,7 +484,7 @@ mod tests { ) .unwrap(); fence.reset().unwrap(); - assert!(!fence.ready().unwrap()); + assert!(!fence.is_signaled().unwrap()); } #[test] diff --git a/vulkano/src/sync/future/fence_signal.rs b/vulkano/src/sync/future/fence_signal.rs index 10f137f6..01cd2a67 100644 --- a/vulkano/src/sync/future/fence_signal.rs +++ b/vulkano/src/sync/future/fence_signal.rs @@ -14,7 +14,7 @@ use crate::{ device::{Device, DeviceOwned, Queue}, image::{sys::UnsafeImage, ImageLayout}, sync::{AccessFlags, Fence, PipelineStages}, - DeviceSize, + DeviceSize, OomError, }; use parking_lot::{Mutex, MutexGuard}; use std::{mem::replace, ops::Range, sync::Arc, time::Duration}; @@ -120,6 +120,19 @@ impl FenceSignalFuture where F: GpuFuture, { + /// Returns true if the fence is signaled by the GPU. + pub fn is_signaled(&self) -> Result { + let state = self.state.lock(); + + match &*state { + FenceSignalFutureState::Pending(_, fence) + | FenceSignalFutureState::PartiallyFlushed(_, fence) + | FenceSignalFutureState::Flushed(_, fence) => fence.is_signaled(), + FenceSignalFutureState::Cleaned => Ok(true), + FenceSignalFutureState::Poisoned => unreachable!(), + } + } + /// Blocks the current thread until the fence is signaled by the GPU. Performs a flush if /// necessary. ///