Debian Codegraph @

third_party/rust/wgpu-hal/src/auxil/dxgi/time.rs

raw
1 #![allow(dead_code)] // IPresentationManager is unused currently
2
3 use windows::Win32::System::Performance::{QueryPerformanceCounter, QueryPerformanceFrequency};
4
5 pub enum PresentationTimer {
6 /// DXGI uses [`QueryPerformanceCounter()`]
7 Dxgi {
8 /// How many ticks of QPC per second
9 frequency: u64,
10 },
11 /// [`IPresentationManager`] uses [`QueryInterruptTimePrecise()`]
12 ///
13 /// [`IPresentationManager`]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Graphics/CompositionSwapchain/struct.IPresentationManager.html
14 /// [`QueryInterruptTimePrecise()`]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/System/WindowsProgramming/fn.QueryInterruptTimePrecise.html
15 #[allow(non_snake_case)]
16 IPresentationManager {
17 fnQueryInterruptTimePrecise: unsafe extern "system" fn(*mut u64),
18 },
19 }
20
21 impl core::fmt::Debug for PresentationTimer {
22 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23 match *self {
24 Self::Dxgi { frequency } => f
25 .debug_struct("DXGI")
26 .field("frequency", &frequency)
27 .finish(),
28 Self::IPresentationManager {
29 fnQueryInterruptTimePrecise,
30 } => f
31 .debug_struct("IPresentationManager")
32 .field(
33 "QueryInterruptTimePrecise",
34 &(fnQueryInterruptTimePrecise as usize),
35 )
36 .finish(),
37 }
38 }
39 }
40
41 impl PresentationTimer {
42 /// Create a presentation timer using QueryPerformanceFrequency (what DXGI uses for presentation times)
43 pub fn new_dxgi() -> Self {
44 let mut frequency = 0;
45 unsafe { QueryPerformanceFrequency(&mut frequency) }.unwrap();
46
47 Self::Dxgi {
48 frequency: frequency
49 .try_into()
50 .expect("Frequency should not be negative"),
51 }
52 }
53
54 /// Create a presentation timer using QueryInterruptTimePrecise (what IPresentationManager uses for presentation times)
55 ///
56 /// Panics if QueryInterruptTimePrecise isn't found (below Win10)
57 pub fn new_ipresentation_manager() -> Self {
58 // We need to load this explicitly, as QueryInterruptTimePrecise is only available on Windows 10+
59 //
60 // Docs say it's in kernel32.dll, but it's actually in kernelbase.dll.
61 // api-ms-win-core-realtime-l1-1-1.dll
62 let kernelbase =
63 libloading::os::windows::Library::open_already_loaded("kernelbase.dll").unwrap();
64 // No concerns about lifetimes here as kernelbase is always there.
65 let ptr = unsafe {
66 kernelbase
67 .get(c"QueryInterruptTimePrecise".to_bytes())
68 .unwrap()
69 };
70 Self::IPresentationManager {
71 fnQueryInterruptTimePrecise: *ptr,
72 }
73 }
74
75 /// Gets the current time in nanoseconds.
76 pub fn get_timestamp_ns(&self) -> u128 {
77 // Always do u128 math _after_ hitting the timing function.
78 match *self {
79 PresentationTimer::Dxgi { frequency } => {
80 let mut counter = 0;
81 unsafe { QueryPerformanceCounter(&mut counter) }.unwrap();
82
83 // counter * (1_000_000_000 / freq) but re-ordered to make more precise
84 (counter as u128 * 1_000_000_000) / frequency as u128
85 }
86 PresentationTimer::IPresentationManager {
87 fnQueryInterruptTimePrecise,
88 } => {
89 let mut counter = 0;
90 unsafe { fnQueryInterruptTimePrecise(&mut counter) };
91
92 // QueryInterruptTimePrecise uses units of 100ns for its tick.
93 counter as u128 * 100
94 }
95 }
96 }
97 }