Skip to main content

ultrafast_gpu/
stats.rs

1use std::sync::atomic::{AtomicU64, Ordering};
2
3/// Cumulative CPU<->GPU transfer counters. Uniform params bound inline with a
4/// dispatch are counted separately from bulk uploads, since they are the
5/// expected steady-state cost of a frame.
6#[derive(Debug, Default)]
7pub(crate) struct Counters {
8    pub uploads: AtomicU64,
9    pub upload_bytes: AtomicU64,
10    pub readbacks: AtomicU64,
11    pub readback_bytes: AtomicU64,
12    pub uniform_bytes: AtomicU64,
13    pub textures_created: AtomicU64,
14    pub dispatches: AtomicU64,
15    /// GPU-wide barriers recorded (Vulkan: only where commands depend).
16    pub barriers: AtomicU64,
17}
18
19#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
20pub struct TransferStats {
21    pub uploads: u64,
22    pub upload_bytes: u64,
23    pub readbacks: u64,
24    pub readback_bytes: u64,
25    pub uniform_bytes: u64,
26    pub textures_created: u64,
27    pub dispatches: u64,
28    pub barriers: u64,
29}
30
31impl Counters {
32    pub fn snapshot(&self) -> TransferStats {
33        let l = |a: &AtomicU64| a.load(Ordering::Relaxed);
34        TransferStats {
35            uploads: l(&self.uploads),
36            upload_bytes: l(&self.upload_bytes),
37            readbacks: l(&self.readbacks),
38            readback_bytes: l(&self.readback_bytes),
39            uniform_bytes: l(&self.uniform_bytes),
40            textures_created: l(&self.textures_created),
41            dispatches: l(&self.dispatches),
42            barriers: l(&self.barriers),
43        }
44    }
45
46    pub fn add(a: &AtomicU64, n: u64) {
47        a.fetch_add(n, Ordering::Relaxed);
48    }
49}
50
51impl std::ops::Sub for TransferStats {
52    type Output = TransferStats;
53    fn sub(self, o: Self) -> Self {
54        TransferStats {
55            uploads: self.uploads - o.uploads,
56            upload_bytes: self.upload_bytes - o.upload_bytes,
57            readbacks: self.readbacks - o.readbacks,
58            readback_bytes: self.readback_bytes - o.readback_bytes,
59            uniform_bytes: self.uniform_bytes - o.uniform_bytes,
60            textures_created: self.textures_created - o.textures_created,
61            dispatches: self.dispatches - o.dispatches,
62            barriers: self.barriers - o.barriers,
63        }
64    }
65}