Add is_signaled to FenceSignalFuture (#1910)

This commit is contained in:
Rua 2022-06-06 00:45:37 +02:00 committed by GitHub
parent 1f685b1420
commit aaf0eadc61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 17 deletions

View File

@ -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());
}
}

View File

@ -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<bool, OomError> {
pub fn is_signaled(&self) -> Result<bool, OomError> {
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<Duration>) -> 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]

View File

@ -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<F> FenceSignalFuture<F>
where
F: GpuFuture,
{
/// Returns true if the fence is signaled by the GPU.
pub fn is_signaled(&self) -> Result<bool, OomError> {
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.
///