Files
EonaCat.XSBox/crates/graphics/src/host/smooth.rs
T
EonaCat 1b0d534e6c
Rust validation / Formatting (push) Failing after 27s
EonaCat/ci-workflows: SonarQube / sonar (push) Successful in 47s
Rust validation / Windows x64 build and tests (push) Canceled after 0s
Initial
2026-12-04 21:03:25 +01:00

190 lines
6.0 KiB
Rust

use std::collections::VecDeque;
use std::time::Instant;
const RECENT: usize = 120;
const HITCH_TIMES: f32 = 2.0;
const HITCH_MS: f32 = 50.0;
const PAUSE_MS: f32 = 150.0;
const EVENTS: usize = 10;
pub(super) fn on() -> bool {
crate::asked_for!("EONACAT_XS_SMOOTHNESS")
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Shown {
pub present: u32,
pub present_refresh: u32,
pub sync_refresh: u32,
pub sync_qpc: i64,
}
pub(super) struct Smoothness {
qpc_hz: f64,
first: Option<Instant>,
since: Option<Instant>,
last: Option<Instant>,
last_frame: Option<u64>,
last_gap: Option<f32>,
recent: VecDeque<f32>,
last_count: Option<u32>,
last_shown: Option<Shown>,
w: Window,
}
#[derive(Default)]
struct Window {
presents: u32,
title_frames: u32,
repeated: u32,
gaps: Vec<(f32, f64)>,
delta_sum: f64,
deltas: u32,
counts: Option<(u32, u32)>,
display: bool,
updates: u32,
held: u32,
dropped: u32,
display_longest: f64,
ticks: i64,
refreshes: u32,
}
impl Smoothness {
pub(super) fn new(qpc_hz: i64) -> Self {
Self {
qpc_hz: qpc_hz.max(1) as f64,
first: None,
since: None,
last: None,
last_frame: None,
last_gap: None,
recent: VecDeque::with_capacity(RECENT),
last_count: None,
last_shown: None,
w: Window::default(),
}
}
pub(super) fn present(&mut self, at: Instant, frame: u64, count: Option<u32>, shown: Option<Shown>, tearing: bool) -> Option<String> {
let first = *self.first.get_or_insert(at);
let since = *self.since.get_or_insert(at);
let w = &mut self.w;
w.presents += 1;
if self.last_frame == Some(frame) {
w.repeated += 1;
} else {
w.title_frames += 1;
}
self.last_frame = Some(frame);
if let Some(last) = self.last.replace(at) {
let gap = at.saturating_duration_since(last).as_secs_f32() * 1000.0;
w.gaps.push((gap, at.saturating_duration_since(first).as_secs_f64()));
if let Some(before) = self.last_gap.replace(gap) {
w.delta_sum += f64::from((gap - before).abs());
w.deltas += 1;
}
if self.recent.len() == RECENT {
self.recent.pop_front();
}
self.recent.push_back(gap);
}
if let Some(count) = count {
w.counts = Some(w.counts.map_or((count, count), |(from, _)| (from, count)));
self.last_count = Some(count);
}
if let Some(s) = shown.filter(|s| s.sync_qpc != 0) {
if let Some(before) = self.last_shown {
w.display = true;
let refreshes = s.sync_refresh.wrapping_sub(before.sync_refresh);
if refreshes > 0 && refreshes < 1 << 20 {
w.ticks += s.sync_qpc - before.sync_qpc;
w.refreshes += refreshes;
}
let presents = s.present.wrapping_sub(before.present);
let advanced = s.present_refresh.wrapping_sub(before.present_refresh);
if presents > 0 && presents < 1 << 20 && advanced < 1 << 20 {
w.updates += 1;
w.held += advanced.saturating_sub(presents);
if !tearing {
w.dropped += presents.saturating_sub(advanced);
}
w.display_longest = w.display_longest.max(f64::from(advanced));
}
}
self.last_shown = Some(s);
}
if at.saturating_duration_since(since).as_secs_f64() < 1.0 {
return None;
}
self.since = Some(at);
let w = std::mem::take(&mut self.w);
let t = at.saturating_duration_since(first).as_secs_f64();
Some(self.line(t, w))
}
fn line(&self, t: f64, w: Window) -> String {
let median = median(&self.recent);
let mut hitches = 0;
let mut pauses = 0;
let mut longest = 0.0f32;
let mut events = Vec::new();
for &(gap, at) in &w.gaps {
longest = longest.max(gap);
let pause = gap > PAUSE_MS;
let hitch = gap > HITCH_MS && gap > HITCH_TIMES * median;
pauses += u32::from(pause);
hitches += u32::from(hitch);
if (pause || hitch) && events.len() < EVENTS {
events.push(format!("{at:.3}:{gap:.1}:{}", if pause { 'p' } else { 'h' }));
}
}
let variability = if w.deltas > 0 { w.delta_sum / f64::from(w.deltas) } else { 0.0 };
let generated = w
.counts
.map_or_else(|| "-".to_owned(), |(from, to)| (i64::from(to.wrapping_sub(from)) - i64::from(w.presents.saturating_sub(1))).max(0).to_string());
let display = if w.display && w.refreshes > 0 {
let period = w.ticks as f64 / self.qpc_hz * 1000.0 / f64::from(w.refreshes);
format!(
"display=dxgi updates={} display_longest_ms={:.1} held={} dropped={} refresh_ms={period:.2}",
w.updates,
w.display_longest * period,
w.held,
w.dropped
)
} else {
"display=none updates=- display_longest_ms=- held=- dropped=- refresh_ms=-".to_owned()
};
format!(
"smoothness: t={t:.3} presents={} title_frames={} repeated={} generated={generated} median_ms={median:.1} \
longest_ms={longest:.1} hitches={hitches} pauses={pauses} var_ms={variability:.2} {display} events={}",
w.presents,
w.title_frames,
w.repeated,
if events.is_empty() { "-".to_owned() } else { events.join(",") }
)
}
}
fn median(values: &VecDeque<f32>) -> f32 {
if values.is_empty() {
return 0.0;
}
let mut sorted: Vec<f32> = values.iter().copied().collect();
sorted.sort_unstable_by(f32::total_cmp);
sorted[sorted.len() / 2]
}