fence.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. 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. // shortcut: a null event means "block until reached"; nothing can
  84. // reach it while no GPU work runs, so fail instead of hanging.
  85. tracing::warn!(value, "blocking fence wait not implemented");
  86. return E_NOTIMPL;
  87. }
  88. waiters.push((value, event as usize));
  89. S_OK
  90. }
  91. pub(crate) extern "system" fn signal(this: This, value: u64) -> HRESULT {
  92. let fence = fence(this);
  93. let mut waiters = fence.waiters.lock().unwrap_or_else(|e| e.into_inner());
  94. fence.value.store(value, Ordering::Release);
  95. waiters.retain(|&(wanted, event)| {
  96. let reached = wanted <= value;
  97. if reached {
  98. set_event(event);
  99. }
  100. !reached
  101. });
  102. S_OK
  103. }
  104. extern "system" fn creation_flags(this: This) -> u32 {
  105. fence(this).flags
  106. }
  107. /// Where the value lives (GPU addresses are CPU addresses).
  108. extern "system" fn gpu_address(this: This) -> u64 {
  109. (&raw const fence(this).value) as u64
  110. }
  111. /// IUnknown 3, object 4, device child 1, fence 3, fence 1 (2, a console
  112. /// one among them).
  113. static FUNCTIONS: Table<24> = {
  114. let mut f = object::d3d12_object(object::missing_table::<FENCE, 24>());
  115. f[0] = query as *const ();
  116. f[2] = release as *const ();
  117. f[7] = object::parent_device as *const (); // 0x38
  118. f[8] = completed as *const (); // 0x40
  119. f[9] = set_event_on_completion as *const (); // 0x48
  120. f[10] = signal as *const (); // 0x50
  121. f[11] = creation_flags as *const (); // 0x58
  122. f[12] = gpu_address as *const (); // 0x60
  123. Table(f)
  124. };
  125. #[cfg(test)]
  126. mod tests {
  127. use super::*;
  128. use crate::object::slot;
  129. #[cfg(windows)]
  130. #[test]
  131. fn signals_set_waiting_events() {
  132. use windows_sys::Win32::Foundation::{CloseHandle, WAIT_OBJECT_0};
  133. use windows_sys::Win32::System::Threading::{CreateEventW, WaitForSingleObject};
  134. let mut f = std::ptr::null();
  135. assert_eq!(create(std::ptr::null(), 1, 0, &FENCE_IIDS[0], &mut f), S_OK);
  136. assert_eq!(slot::<extern "system" fn(This) -> u64>(f, 0x40)(f), 1);
  137. // SAFETY: plain auto-reset event.
  138. let event = unsafe { CreateEventW(std::ptr::null(), 0, 0, std::ptr::null()) };
  139. assert!(!event.is_null());
  140. let wait = slot::<extern "system" fn(This, u64, *mut c_void) -> HRESULT>(f, 0x48);
  141. let signal = slot::<extern "system" fn(This, u64) -> HRESULT>(f, 0x50);
  142. assert_eq!(wait(f, 5, event), S_OK);
  143. // SAFETY: a valid event.
  144. assert_ne!(unsafe { WaitForSingleObject(event, 0) }, WAIT_OBJECT_0);
  145. assert_eq!(signal(f, 5), S_OK);
  146. // SAFETY: as above.
  147. assert_eq!(unsafe { WaitForSingleObject(event, 0) }, WAIT_OBJECT_0);
  148. assert_eq!(wait(f, 5, event), S_OK, "already reached");
  149. // SAFETY: as above.
  150. assert_eq!(unsafe { WaitForSingleObject(event, 0) }, WAIT_OBJECT_0);
  151. assert_eq!(wait(f, 9, std::ptr::null_mut()), E_NOTIMPL);
  152. // Grand Theft Auto VI reads the value at the fence's address.
  153. let at = slot::<extern "system" fn(This) -> u64>(f, 0x60)(f);
  154. // SAFETY: the fence's value.
  155. assert_eq!(unsafe { *(at as *const u64) }, 5);
  156. // SAFETY: ours.
  157. unsafe { CloseHandle(event) };
  158. assert_eq!(slot::<extern "system" fn(This) -> u32>(f, 0x10)(f), 0);
  159. }
  160. }