Skip to main content

cranpose_render_wgpu/render/
frame_history.rs

1use std::sync::atomic::{AtomicU64, Ordering};
2
3use crate::{
4    frame_graph::FrameCommandStats,
5    gpu_stats::{self, FrameStats, FrameStatsSnapshot, gpu_stats_enabled},
6};
7
8const CACHE_MISS_WARMUP_FRAMES: u8 = 1;
9
10static PRESENTED_FRAMES: AtomicU64 = AtomicU64::new(0);
11
12/// How many frames this process has drawn and presented since it started.
13pub fn frames_presented() -> u64 {
14    PRESENTED_FRAMES.load(Ordering::Relaxed)
15}
16
17// What a renderer keeps of the frames it drew: the last frame's stats, the
18// frames a cache miss still asks for, and how many frames it printed.
19#[derive(Default)]
20pub(super) struct FrameHistory {
21    last: Option<FrameStatsSnapshot>,
22    warmup_frames: u8,
23    count: u64,
24}
25
26impl FrameHistory {
27    // Keeps the frame's stats, and logs and prints them as asked.
28    pub(super) fn close(&mut self, stats: &FrameStats, device: &wgpu::Device) {
29        let snapshot = stats.snapshot();
30        if crate::frame_graph::frame_graph_pass_telemetry_threshold_ms().is_some() {
31            log::warn!(
32                "[wgpu-render-stage:frame-stats] layer_hit={} layer_miss={} miss_px={} \
33                 offscreen_acq={} offscreen_new={} isolated={} draws={}",
34                snapshot.layer_cache_hits,
35                snapshot.layer_cache_misses,
36                snapshot.layer_cache_miss_pixels,
37                snapshot.offscreen_acquires,
38                snapshot.offscreen_news,
39                snapshot.isolated_layer_renders,
40                snapshot.draw_calls,
41            );
42        }
43        self.last = Some(snapshot);
44        PRESENTED_FRAMES.fetch_add(1, Ordering::Relaxed);
45        if self.warmup_frames > 0 {
46            self.warmup_frames -= 1;
47        } else if needs_warmup_frame(&snapshot) {
48            self.warmup_frames = CACHE_MISS_WARMUP_FRAMES;
49        }
50        let gpu_stats_on = gpu_stats_enabled();
51        stats.maybe_print_snapshot(snapshot, &mut self.count, gpu_stats_on);
52        if gpu_stats_on && self.count.is_multiple_of(60) {
53            gpu_stats::print_gpu_memory_report(device, self.count);
54        }
55    }
56
57    // A frame the surface already showed takes the warm-up frame it was owed.
58    pub(super) fn skip_frame(&mut self) {
59        self.warmup_frames = self.warmup_frames.saturating_sub(1);
60    }
61
62    pub(super) fn last(&self) -> Option<FrameStatsSnapshot> {
63        self.last
64    }
65
66    // Whether a cache the last frames missed still asks for a frame.
67    pub(super) fn warming_up(&self) -> bool {
68        self.warmup_frames > 0
69    }
70
71    // Adds the commands of a copy made after the frame closed.
72    pub(super) fn add_command_stats(&mut self, stats: FrameCommandStats) {
73        self.last = self
74            .last
75            .map(|snapshot| snapshot.with_command_stats_added(stats));
76    }
77}
78
79fn needs_warmup_frame(snapshot: &FrameStatsSnapshot) -> bool {
80    snapshot.layer_cache_misses > 0
81        || snapshot.shadow_shape_cache_misses > 0
82        || snapshot.text_image_cache_misses > 0
83        || snapshot.text_glyph_atlas_misses > 0
84}