1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! Fence types.
use {
super::{DriverError, device::Device},
ash::vk,
log::{error, trace},
std::{fmt::Debug, thread::panicking},
};
/// Represents a Vulkan fence used to track queue submission completion.
///
/// See [`VkFence`](https://registry.khronos.org/vulkan/specs/latest/man/html/VkFence.html).
#[derive(Debug)]
#[read_only::cast]
pub struct Fence {
/// The device which owns this fence resource.
///
/// _Note:_ This field is read-only.
#[readonly]
pub device: Device,
/// The native Vulkan fence handle.
///
/// _Note:_ This field is read-only.
#[readonly]
pub handle: vk::Fence,
pub(crate) queued: bool,
droppables: Vec<Box<dyn Debug + Send + 'static>>,
}
impl Fence {
/// Creates a Vulkan fence owned by `device`.
///
/// See [`vkCreateFence`](https://registry.khronos.org/vulkan/specs/latest/man/html/vkCreateFence.html).
pub fn create(device: &Device, signaled: bool) -> Result<Self, DriverError> {
Ok(Self {
device: device.clone(),
handle: Device::create_fence(device, signaled)?,
queued: signaled,
droppables: Vec::new(),
})
}
/// Drops an item after this fence signals.
pub(crate) fn drop_when_signaled(&mut self, x: impl Debug + Send + 'static) {
self.droppables.push(Box::new(x));
}
#[profiling::function]
fn drop_signaled(&mut self) {
if !self.droppables.is_empty() {
trace!("dropping {} shared references", self.droppables.len());
}
self.droppables.clear();
}
/// Returns `true` if this fence is signaled.
///
/// See [`vkGetFenceStatus`](https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetFenceStatus.html).
#[profiling::function]
pub fn is_signaled(&self) -> Result<bool, DriverError> {
let res = unsafe { self.device.get_fence_status(self.handle) };
match res {
Ok(status) => Ok(status),
Err(err) if err == vk::Result::ERROR_DEVICE_LOST => {
error!("invalid device state: lost");
Err(DriverError::InvalidData)
}
Err(err) => {
error!("unable to get fence status: {err}");
Err(DriverError::InvalidData)
}
}
}
/// Returns `true` if work has been queued against this fence.
pub fn is_queued(&self) -> bool {
self.queued
}
/// Marks this fence as having work queued against it.
pub(crate) fn mark_queued(&mut self) {
self.queued = true;
}
/// Resets this fence to the unsignaled state.
///
/// See [`vkResetFences`](https://registry.khronos.org/vulkan/specs/latest/man/html/vkResetFences.html).
pub fn reset(&mut self) -> Result<&mut Self, DriverError> {
#[cfg(feature = "checked")]
if !self.queued {
return Ok(self);
}
Device::reset_fences(&self.device, std::slice::from_ref(&self.handle))?;
self.queued = false;
Ok(self)
}
/// Waits for this fence to signal, then drops any deferred payloads.
///
/// See [`vkWaitForFences`](https://registry.khronos.org/vulkan/specs/latest/man/html/vkWaitForFences.html).
#[profiling::function]
pub fn wait_signaled(&mut self) -> Result<&mut Self, DriverError> {
#[cfg(feature = "checked")]
if !self.queued {
return Ok(self);
}
Device::wait_for_fence(&self.device, &self.handle)?;
self.drop_signaled();
Ok(self)
}
}
impl Drop for Fence {
#[profiling::function]
fn drop(&mut self) {
if panicking() {
return;
}
if self.queued && self.wait_signaled().is_err() {
return;
}
unsafe {
self.device.destroy_fence(self.handle, None);
}
}
}