824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1 | use crate::device::Shared; |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 2 | use crate::error::{WAIT, observe, waited}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 3 | use ash::vk; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 4 | use rustix::event::{PollFd, PollFlags, poll}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 5 | use smithay::backend::renderer::sync::{Fence, Interrupted}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 6 | use std::fmt; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 7 | use std::os::fd::{FromRawFd, OwnedFd}; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 8 | use std::sync::{Arc, Mutex}; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 9 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 10 | enum State { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 11 | Pending, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 12 | /// Exporting a sync_file resets the Vulkan fence, so from then on the |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 13 | /// file is the only record of completion. `None` means it had already |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 14 | /// signalled when exported. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 15 | Exported(Option<OwnedFd>), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 16 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 17 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 18 | pub(crate) struct FenceInner { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 19 | shared: Arc<Shared>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 20 | fence: vk::Fence, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 21 | exportable: bool, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 22 | state: Mutex<State>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 23 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 24 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 25 | impl FenceInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 26 | pub fn new(shared: &Arc<Shared>) -> Result<Arc<Self>, vk::Result> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 27 | let exportable = shared.device.dmabuf.is_some(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 28 | let mut export = vk::ExportFenceCreateInfo::default() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 29 | .handle_types(vk::ExternalFenceHandleTypeFlags::SYNC_FD); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 30 | let mut info = vk::FenceCreateInfo::default(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 31 | if exportable { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 32 | info = info.push_next(&mut export); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 33 | } |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 34 | let spare = shared |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 35 | .device |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 36 | .spare_fences |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 37 | .lock() |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 38 | .unwrap_or_else(|e| e.into_inner()) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 39 | .pop(); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 40 | let fence = match spare { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 41 | Some(f) => { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 42 | unsafe { shared.device.device.reset_fences(&[f])? }; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 43 | f |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 44 | } |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 45 | None => unsafe { shared.device.device.create_fence(&info, None)? }, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 46 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | Ok(Arc::new(Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 48 | shared: shared.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 49 | fence, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 50 | exportable, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 51 | state: Mutex::new(State::Pending), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 52 | })) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 53 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 54 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 55 | pub fn fence(&self) -> vk::Fence { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 56 | self.fence |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 57 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 58 | |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 59 | /// Whether the GPU has signalled, or the error the query failed with. |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 60 | pub fn status(&self) -> Result<bool, vk::Result> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 61 | match &*self.state.lock().unwrap_or_else(|e| e.into_inner()) { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 62 | State::Pending => unsafe { self.shared.device.device.get_fence_status(self.fence) }, |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 63 | State::Exported(None) => Ok(true), |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 64 | State::Exported(Some(fd)) => Ok(readable(fd, Some(&Default::default()))), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 65 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 66 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 67 | |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 68 | /// Like `status` for callers that cannot report a failure. A failed query |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 69 | /// counts as signalled so nobody waits on a fence that cannot signal. |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 70 | pub fn signaled(&self) -> bool { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 71 | self.status().unwrap_or_else(|e| { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 72 | observe(e); |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 73 | true |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 74 | }) |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 75 | } |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 76 | |
cabbef6c998a | atagen | 2026-10-16 13:42 UTC | 77 | /// Blocks until the GPU signals. A pending fence is first turned into its |
cabbef6c998a | atagen | 2026-10-16 13:42 UTC | 78 | /// sync_file and polled, because waiting on the Vulkan fence itself sleeps |
cabbef6c998a | atagen | 2026-10-16 13:42 UTC | 79 | /// in 10 ms steps on NVIDIA even when the work is done in microseconds. |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 80 | /// Gives up with `TIMEOUT` after [`WAIT`], so a hung GPU cannot freeze the |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 81 | /// caller. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 82 | pub fn wait(&self) -> Result<(), vk::Result> { |
cabbef6c998a | atagen | 2026-10-16 13:42 UTC | 83 | if self.exportable { |
cabbef6c998a | atagen | 2026-10-16 13:42 UTC | 84 | drop(self.export()); |
cabbef6c998a | atagen | 2026-10-16 13:42 UTC | 85 | } |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 86 | let waited_for = match &*self.state.lock().unwrap_or_else(|e| e.into_inner()) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 87 | State::Pending => unsafe { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 88 | self.shared.device.device.wait_for_fences( |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 89 | &[self.fence], |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 90 | true, |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 91 | WAIT.as_nanos() as u64, |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 92 | ) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 93 | }, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 94 | State::Exported(None) => Ok(()), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 95 | State::Exported(Some(fd)) => { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 96 | let timeout = rustix::event::Timespec { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 97 | tv_sec: WAIT.as_secs() as _, |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 98 | tv_nsec: 0, |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 99 | }; |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 100 | if readable(fd, Some(&timeout)) { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 101 | Ok(()) |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 102 | } else { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 103 | Err(vk::Result::TIMEOUT) |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 104 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 105 | } |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 106 | }; |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 107 | if waited_for.is_ok() { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 108 | waited(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 109 | } |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 110 | waited_for |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 111 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 112 | |
cabbef6c998a | atagen | 2026-10-16 13:42 UTC | 113 | #[profiling::function] |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 114 | fn export(&self) -> Option<OwnedFd> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 115 | if !self.exportable { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 116 | return None; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 117 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 118 | let ext = self.shared.device.dmabuf.as_ref()?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 119 | let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 120 | if let State::Pending = *state { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 121 | let info = vk::FenceGetFdInfoKHR::default() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 122 | .fence(self.fence) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 123 | .handle_type(vk::ExternalFenceHandleTypeFlags::SYNC_FD); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 124 | let raw = unsafe { ext.fence_fd.get_fence_fd(&info) }.ok()?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 125 | *state = State::Exported((raw >= 0).then(|| unsafe { OwnedFd::from_raw_fd(raw) })); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 126 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 127 | match &*state { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 128 | State::Exported(Some(fd)) => fd.try_clone().ok(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 129 | _ => None, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 130 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 131 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 132 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 133 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 134 | fn readable(fd: &OwnedFd, timeout: Option<&rustix::event::Timespec>) -> bool { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 135 | let mut fds = [PollFd::new(fd, PollFlags::IN)]; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 136 | loop { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 137 | match poll(&mut fds, timeout) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 138 | Ok(n) => return n > 0, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 139 | Err(rustix::io::Errno::INTR) => continue, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 140 | Err(_) => return true, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 141 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 142 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 143 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 144 | |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 145 | /// Fences go back to a pool instead of being destroyed, which cut the median |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 146 | /// queue submit on NVIDIA from 87 to 52 us. One still pending is destroyed. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 147 | impl Drop for FenceInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 148 | fn drop(&mut self) { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 149 | let d = &self.shared.device.device; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 150 | let state = self.state.get_mut().unwrap_or_else(|e| e.into_inner()); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 151 | let idle = match state { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 152 | State::Pending => unsafe { d.get_fence_status(self.fence) }.unwrap_or(false), |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 153 | State::Exported(_) => true, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 154 | }; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 155 | if idle { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 156 | self.shared |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 157 | .device |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 158 | .spare_fences |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 159 | .lock() |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 160 | .unwrap_or_else(|e| e.into_inner()) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 161 | .push(self.fence); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 162 | } else { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 163 | unsafe { d.destroy_fence(self.fence, None) }; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 164 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 165 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 166 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 167 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 168 | impl fmt::Debug for FenceInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 169 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 170 | f.debug_tuple("FenceInner").field(&self.fence).finish() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 171 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 172 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 173 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 174 | #[derive(Debug)] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 175 | pub(crate) struct VkFence(pub Arc<FenceInner>); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 176 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 177 | impl Fence for VkFence { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 178 | fn is_signaled(&self) -> bool { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 179 | self.0.signaled() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 180 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 181 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 182 | fn wait(&self) -> Result<(), Interrupted> { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 183 | self.0.wait().map_err(|e| { |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 184 | observe(e); |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 185 | Interrupted |
6dfb54e27e7f | atagen | 2026-10-19 07:53 UTC | 186 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 187 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 188 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 189 | fn is_exportable(&self) -> bool { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 190 | self.0.exportable |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 191 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 192 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 193 | fn export(&self) -> Option<OwnedFd> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 194 | self.0.export() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 195 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 196 | } |