use core::time::Duration;
use std::time::Instant;
use nadir_core::log::warn;
use nadir_gfx::{Device, GfxError, TimerId};
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct GpuTimes {
pub renders_ago: u32,
pub world: Duration,
pub output: Duration,
}
impl GpuTimes {
pub fn total(&self) -> Duration {
self.world + self.output
}
}
pub(crate) const PASSES: usize = 2;
const SLOTS: usize = 4;
const LATENCY: u64 = 2;
pub(crate) trait Timers {
type Id: Copy;
fn create(&mut self) -> Result<Self::Id, GfxError>;
fn result(&mut self, timer: Self::Id) -> Result<Option<Duration>, GfxError>;
}
impl Timers for Device {
type Id = TimerId;
fn create(&mut self) -> Result<TimerId, GfxError> {
self.create_timer()
}
fn result(&mut self, timer: TimerId) -> Result<Option<Duration>, GfxError> {
self.timer_result(timer)
}
}
#[derive(Clone, Copy)]
struct Slot<T> {
timers: Option<[T; PASSES]>,
waiting: Option<(u64, Instant)>,
}
pub(crate) struct TimerRing<T> {
slots: [Slot<T>; SLOTS],
renders: u64,
current: Option<(usize, u64, Instant)>,
read: Vec<GpuTimes>,
warned: bool,
}
impl<T: Copy> TimerRing<T> {
pub(crate) fn new() -> Self {
Self {
slots: [Slot {
timers: None,
waiting: None,
}; SLOTS],
renders: 0,
current: None,
read: Vec::with_capacity(SLOTS),
warned: false,
}
}
pub(crate) fn read(&self) -> &[GpuTimes] {
&self.read
}
pub(crate) fn begin_render(
&mut self,
timers: &mut impl Timers<Id = T>,
now: Instant,
) -> Result<Option<[T; PASSES]>, GfxError> {
let render = self.renders;
self.renders += 1;
self.current = None;
self.read.clear();
let mut order = [(u64::MAX, 0); SLOTS];
for ((entry, slot), index) in order.iter_mut().zip(&self.slots).zip(0..) {
if let Some((timed, _)) = slot.waiting {
*entry = (timed, index);
}
}
order.sort_unstable();
for (timed, index) in order {
if timed == u64::MAX || render - timed < LATENCY {
continue;
}
let slot = &mut self.slots[index];
let (Some(ids), Some((_, started))) = (slot.timers, slot.waiting) else {
continue;
};
let mut times = [Duration::ZERO; PASSES];
let mut ready = true;
for (time, id) in times.iter_mut().zip(ids) {
match timers.result(id)? {
Some(result) => *time = result,
None => {
ready = false;
break;
}
}
}
if !ready {
continue;
}
slot.waiting = None;
let [world, output] = times;
let passed = now.saturating_duration_since(started);
if world + output > passed {
if !self.warned {
self.warned = true;
warn!(
"the GPU's timers gave {:?} for a render {passed:?} ago: the driver \
timed it wrong, and its times are dropped (logged once)",
world + output
);
}
continue;
}
self.read.push(GpuTimes {
renders_ago: u32::try_from(render - timed).unwrap_or(u32::MAX),
world,
output,
});
}
let index = (render % SLOTS as u64) as usize;
let slot = &mut self.slots[index];
if slot.waiting.is_some() {
return Ok(None);
}
let ids = match slot.timers {
Some(ids) => ids,
None => {
let ids = [timers.create()?, timers.create()?];
slot.timers = Some(ids);
ids
}
};
self.current = Some((index, render, now));
Ok(Some(ids))
}
pub(crate) fn end_render(&mut self) {
if let Some((index, render, started)) = self.current.take() {
self.slots[index].waiting = Some((render, started));
}
}
pub(crate) fn reset(&mut self) {
for slot in &mut self.slots {
slot.waiting = None;
}
self.current = None;
self.read.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default)]
struct FakeGpu {
made: u32,
results: Vec<Option<Duration>>,
asked: Vec<u32>,
}
impl FakeGpu {
fn finish(&mut self, [world, output]: [u32; PASSES], times: [Duration; PASSES]) {
self.results[world as usize] = Some(times[0]);
self.results[output as usize] = Some(times[1]);
}
}
impl Timers for FakeGpu {
type Id = u32;
fn create(&mut self) -> Result<u32, GfxError> {
self.results.push(None);
self.made += 1;
Ok(self.made - 1)
}
fn result(&mut self, timer: u32) -> Result<Option<Duration>, GfxError> {
self.asked.push(timer);
Ok(self.results[timer as usize])
}
}
const FRAME: Duration = Duration::from_millis(16);
fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
}
fn render(ring: &mut TimerRing<u32>, gpu: &mut FakeGpu, now: Instant) -> Option<[u32; 2]> {
let timers = ring.begin_render(gpu, now).unwrap();
ring.end_render();
timers
}
#[test]
fn a_render_is_read_back_two_renders_later() {
let (mut ring, mut gpu) = (TimerRing::new(), FakeGpu::default());
let start = Instant::now();
for frame in 0..12u32 {
let now = start + FRAME * frame;
let timers = render(&mut ring, &mut gpu, now).expect("every render is timed");
gpu.finish(timers, [ms(u64::from(frame)), ms(1)]);
if frame < 2 {
assert_eq!(ring.read(), [], "render {frame}");
} else {
let times = GpuTimes {
renders_ago: 2,
world: ms(u64::from(frame - 2)),
output: ms(1),
};
assert_eq!(ring.read(), [times], "render {frame}");
assert_eq!(times.total(), ms(u64::from(frame - 1)));
}
}
assert_eq!(gpu.made, 8);
}
#[test]
fn no_time_is_read_before_its_result_is_there() {
let (mut ring, mut gpu) = (TimerRing::new(), FakeGpu::default());
let start = Instant::now();
let first = render(&mut ring, &mut gpu, start).unwrap();
let mut timed = Vec::new();
for frame in 1..4 {
timed.push(render(&mut ring, &mut gpu, start + FRAME * frame).unwrap());
assert_eq!(ring.read(), [], "render {frame}");
}
assert_eq!(gpu.asked, [first[0], first[0], timed[0][0]]);
assert_eq!(render(&mut ring, &mut gpu, start + FRAME * 4), None);
assert_eq!(ring.read(), []);
gpu.finish(first, [ms(5), ms(2)]);
for timers in timed {
gpu.finish(timers, [ms(3), ms(1)]);
}
assert!(render(&mut ring, &mut gpu, start + FRAME * 5).is_some());
let ago: Vec<u32> = ring.read().iter().map(|times| times.renders_ago).collect();
assert_eq!(ago, [5, 4, 3, 2]);
assert_eq!(
(ring.read()[0].world, ring.read()[0].output),
(ms(5), ms(2))
);
}
#[test]
fn times_that_come_together_come_oldest_first() {
let (mut ring, mut gpu) = (TimerRing::new(), FakeGpu::default());
let start = Instant::now();
let timers: Vec<_> = (0..3)
.map(|frame| render(&mut ring, &mut gpu, start + FRAME * frame).unwrap())
.collect();
for (timers, world) in timers.iter().zip([1, 2, 3]).rev() {
gpu.finish(*timers, [ms(world), ms(0)]);
}
render(&mut ring, &mut gpu, start + FRAME * 3);
let read: Vec<(u32, Duration)> = ring
.read()
.iter()
.map(|times| (times.renders_ago, times.world))
.collect();
assert_eq!(read, [(3, ms(1)), (2, ms(2))]);
}
#[test]
fn a_time_longer_than_the_time_that_passed_is_dropped() {
let (mut ring, mut gpu) = (TimerRing::new(), FakeGpu::default());
let start = Instant::now();
let wrong = render(&mut ring, &mut gpu, start).unwrap();
gpu.finish(wrong, [Duration::from_secs(43_411), ms(1)]);
let right = render(&mut ring, &mut gpu, start + FRAME).unwrap();
gpu.finish(right, [ms(30), ms(1)]);
render(&mut ring, &mut gpu, start + FRAME * 2);
assert_eq!(ring.read(), [], "hours do not pass in two frames");
render(&mut ring, &mut gpu, start + FRAME * 3);
assert_eq!(ring.read().len(), 1);
assert_eq!(ring.read()[0].total(), ms(31));
}
#[test]
fn a_render_that_did_not_end_is_not_waited_for() {
let (mut ring, mut gpu) = (TimerRing::new(), FakeGpu::default());
let start = Instant::now();
let failed = ring.begin_render(&mut gpu, start).unwrap().unwrap();
for frame in 1..4 {
render(&mut ring, &mut gpu, start + FRAME * frame);
}
assert!(
!gpu.asked.iter().any(|timer| failed.contains(timer)),
"its timers, perhaps never run, are not asked for"
);
assert_eq!(render(&mut ring, &mut gpu, start + FRAME * 4), Some(failed));
}
#[test]
fn a_reset_forgets_the_renders_that_wait_and_keeps_the_timers() {
let (mut ring, mut gpu) = (TimerRing::new(), FakeGpu::default());
let start = Instant::now();
for frame in 0..4 {
let timers = render(&mut ring, &mut gpu, start + FRAME * frame).unwrap();
gpu.finish(timers, [ms(1), ms(1)]);
}
assert_eq!(ring.read().len(), 1);
ring.reset();
assert_eq!(ring.read(), []);
gpu.asked.clear();
render(&mut ring, &mut gpu, start + FRAME * 4);
render(&mut ring, &mut gpu, start + FRAME * 5);
assert_eq!(gpu.asked, [], "nothing waits");
assert_eq!(gpu.made, 8, "no timer made again");
}
}