fence.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //! Fences: a 64-bit value and the events waiting for it. Nothing runs on a
  2. //! GPU yet, so only signals (the title's, later our queues') move one.
  3. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  4. use std::ffi::c_void;
  5. use std::sync::Mutex;
  6. use std::sync::atomic::{AtomicU64, Ordering};
  7. use crate::object::{self, E_NOTIMPL, FENCE, HRESULT, Iid, Object, S_OK, Table, This, guid};
  8. use crate::resource::PAGEABLE_IIDS;
  9. pub(crate) const FENCE_IIDS: [Iid; 4] = [
  10. guid(
  11. 0x0a753dcf,
  12. 0xc4d8,
  13. 0x4b91,
  14. [0xad, 0xf6, 0xbe, 0x5a, 0x60, 0xd9, 0x5a, 0x76],
  15. ),
  16. PAGEABLE_IIDS[0],
  17. PAGEABLE_IIDS[1],
  18. PAGEABLE_IIDS[2],
  19. ];
  20. #[repr(C)]
  21. struct Fence {
  22. object: Object,
  23. /// Titles also read it straight from memory, at the fence's GPU
  24. /// address.
  25. value: AtomicU64,
  26. flags: u32,
  27. /// Events to set once the value reaches theirs (handles as integers).
  28. waiters: Mutex<Vec<(u64, usize)>>,
  29. }
  30. fn fence<'a>(this: This) -> &'a Fence {
  31. // SAFETY: called from fence methods only.
  32. unsafe { &*this.cast::<Fence>() }
  33. }
  34. pub(crate) extern "system" fn create(
  35. _: This,
  36. initial: u64,
  37. flags: u32,
  38. iid: *const Iid,
  39. out: *mut This,
  40. ) -> HRESULT {
  41. tracing::debug!(initial, flags, "fence");
  42. let fence = Box::new(Fence {
  43. object: Object::new(&FUNCTIONS),
  44. value: AtomicU64::new(initial),
  45. flags,
  46. waiters: Mutex::new(Vec::new()),
  47. });
  48. object::hand_out(FENCE, fence, iid, &FENCE_IIDS, out)
  49. }
  50. fn set_event(event: usize) {
  51. #[cfg(windows)]
  52. // SAFETY: the title passed an event handle.
  53. unsafe {
  54. windows_sys::Win32::System::Threading::SetEvent(event as *mut c_void)
  55. };
  56. #[cfg(not(windows))]
  57. let _ = event;
  58. }
  59. extern "system" fn query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  60. object::answer(
  61. FENCE,
  62. iid,
  63. object::is(iid, &FENCE_IIDS).then_some(this),
  64. out,
  65. )
  66. }
  67. extern "system" fn release(this: This) -> u32 {
  68. object::release_boxed::<Fence>(this)
  69. }
  70. pub(crate) extern "system" fn completed(this: This) -> u64 {
  71. fence(this).value.load(Ordering::Acquire)
  72. }
  73. extern "system" fn set_event_on_completion(this: This, value: u64, event: *mut c_void) -> HRESULT {
  74. let fence = fence(this);
  75. let mut waiters = fence.waiters.lock().unwrap_or_else(|e| e.into_inner());
  76. if fence.value.load(Ordering::Acquire) >= value {
  77. if !event.is_null() {
  78. set_event(event as usize);
  79. }
  80. return S_OK;
  81. }
  82. if event.is_null() {
  83. // With submissions sent to the card and not waited for
  84. // (`crate::host::late`) the signal may be behind them: they are
  85. // settled, which sets it.
  86. #[cfg(windows)]
  87. if crate::host::late() {
  88. drop(waiters);
  89. crate::host::settle_for_fence();
  90. if fence.value.load(Ordering::Acquire) >= value {
  91. return S_OK;
  92. }
  93. tracing::warn!(value, "blocking fence wait not implemented");
  94. return E_NOTIMPL;
  95. }
  96. // shortcut: a null event means "block until reached"; nothing can
  97. // reach it while no GPU work runs, so fail instead of hanging.
  98. tracing::warn!(value, "blocking fence wait not implemented");
  99. return E_NOTIMPL;
  100. }
  101. waiters.push((value, event as usize));
  102. S_OK
  103. }
  104. /// Moves the fence to `value` and sets the events that waited for it.
  105. pub(crate) fn set(this: This, value: u64) {
  106. let fence = fence(this);
  107. let mut waiters = fence.waiters.lock().unwrap_or_else(|e| e.into_inner());
  108. fence.value.store(value, Ordering::Release);
  109. waiters.retain(|&(wanted, event)| {
  110. let reached = wanted <= value;
  111. if reached {
  112. set_event(event);
  113. }
  114. !reached
  115. });
  116. }
  117. pub(crate) extern "system" fn signal(this: This, value: u64) -> HRESULT {
  118. set(this, value);
  119. // A queue may have waited for it: what it held runs now.
  120. #[cfg(windows)]
  121. crate::queue::release_held(false);
  122. S_OK
  123. }
  124. extern "system" fn creation_flags(this: This) -> u32 {
  125. fence(this).flags
  126. }
  127. /// Where the value lives (GPU addresses are CPU addresses).
  128. extern "system" fn gpu_address(this: This) -> u64 {
  129. (&raw const fence(this).value) as u64
  130. }
  131. /// IUnknown 3, object 4, device child 1, fence 3, fence 1 (2, a console
  132. /// one among them).
  133. static FUNCTIONS: Table<24> = {
  134. let mut f = object::d3d12_object(object::missing_table::<FENCE, 24>());
  135. f[0] = query as *const ();
  136. f[2] = release as *const ();
  137. f[7] = object::parent_device as *const (); // 0x38
  138. f[8] = completed as *const (); // 0x40
  139. f[9] = set_event_on_completion as *const (); // 0x48
  140. f[10] = signal as *const (); // 0x50
  141. f[11] = creation_flags as *const (); // 0x58
  142. f[12] = gpu_address as *const (); // 0x60
  143. Table(f)
  144. };
  145. #[cfg(test)]
  146. mod tests {
  147. use super::*;
  148. use crate::object::slot;
  149. #[cfg(windows)]
  150. #[test]
  151. fn signals_set_waiting_events() {
  152. use windows_sys::Win32::Foundation::{CloseHandle, WAIT_OBJECT_0};
  153. use windows_sys::Win32::System::Threading::{CreateEventW, WaitForSingleObject};
  154. let mut f = std::ptr::null();
  155. assert_eq!(create(std::ptr::null(), 1, 0, &FENCE_IIDS[0], &mut f), S_OK);
  156. assert_eq!(slot::<extern "system" fn(This) -> u64>(f, 0x40)(f), 1);
  157. // SAFETY: plain auto-reset event.
  158. let event = unsafe { CreateEventW(std::ptr::null(), 0, 0, std::ptr::null()) };
  159. assert!(!event.is_null());
  160. let wait = slot::<extern "system" fn(This, u64, *mut c_void) -> HRESULT>(f, 0x48);
  161. let signal = slot::<extern "system" fn(This, u64) -> HRESULT>(f, 0x50);
  162. assert_eq!(wait(f, 5, event), S_OK);
  163. // SAFETY: a valid event.
  164. assert_ne!(unsafe { WaitForSingleObject(event, 0) }, WAIT_OBJECT_0);
  165. assert_eq!(signal(f, 5), S_OK);
  166. // SAFETY: as above.
  167. assert_eq!(unsafe { WaitForSingleObject(event, 0) }, WAIT_OBJECT_0);
  168. assert_eq!(wait(f, 5, event), S_OK, "already reached");
  169. // SAFETY: as above.
  170. assert_eq!(unsafe { WaitForSingleObject(event, 0) }, WAIT_OBJECT_0);
  171. assert_eq!(wait(f, 9, std::ptr::null_mut()), E_NOTIMPL);
  172. // The title reads the value at the fence's address.
  173. let at = slot::<extern "system" fn(This) -> u64>(f, 0x60)(f);
  174. // SAFETY: the fence's value.
  175. assert_eq!(unsafe { *(at as *const u64) }, 5);
  176. // SAFETY: ours.
  177. unsafe { CloseHandle(event) };
  178. assert_eq!(slot::<extern "system" fn(This) -> u32>(f, 0x10)(f), 0);
  179. }
  180. }