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