alpha2-render 0.6.2

Render graph and effect registry for alpha2.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! How long each recent renderer submission kept the GPU busy.
//!
//! Windows resets a GPU whose submission exceeds its timeout, and a reset device's DX12 fence
//! reads complete, so the loss looks like dropped work. The submissions' run times tell the two
//! apart. Each submission's GPU time runs from the later of its submit and the previous
//! submission's end to its own completion callback, so queueing is excluded. Callback latency
//! (poll or event loop) is small beside [`alpha2_gpu::hung_threshold_ms`].

use std::cell::Cell;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

use web_time::Instant;

/// Submissions kept; enough to cover the last frames' submissions.
const KEPT: usize = 4 * crate::submission_planner::MAX_BANDS as usize;

/// The frame a submission belongs to, as its caller knows it.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct FrameMark {
    /// The frame's timeline time, in ticks.
    pub time_ticks: i64,
    /// The frame's index in the export, when it is one.
    pub frame_index: Option<u64>,
}

/// The longest recent submission and what it rendered.
#[derive(Clone, Debug, PartialEq)]
pub struct SubmissionReport {
    /// GPU time in milliseconds; for one still running, the time so far.
    pub gpu_ms: f64,
    /// The frame it rendered, when the caller marked one.
    pub frame: Option<FrameMark>,
    /// The effects it ran, as `id@version`.
    pub effects: Vec<String>,
    /// Predicted GPU time in milliseconds, when the frame was planned.
    pub predicted_ms: Option<f64>,
}

#[derive(Debug)]
struct Entry {
    submitted_at: Instant,
    done_at: Option<Instant>,
    frame: Option<FrameMark>,
    effects: Vec<String>,
    predicted_ms: Option<f64>,
}

#[derive(Debug, Default)]
struct ClockState {
    /// Sequence number of `entries[0]`.
    first: u64,
    recorded: u64,
    completed: u64,
    watched: Vec<(u64, PreviousCompletion)>,
    entries: VecDeque<Entry>,
    frame: Option<FrameMark>,
    effects: Vec<String>,
}

/// The renderer's recent submissions and when each finished.
#[derive(Debug, Default)]
pub(crate) struct SubmissionClock {
    state: Mutex<ClockState>,
}

impl SubmissionClock {
    fn state(&self) -> std::sync::MutexGuard<'_, ClockState> {
        self.state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Marks the frame the next submissions render, until another is marked.
    pub(crate) fn mark_frame(&self, frame: Option<FrameMark>) {
        self.state().frame = frame;
    }

    /// Notes an effect the next submission runs.
    pub(crate) fn note_effect(&self, effect: &crate::EffectId) {
        let name = format!("{}@{}", effect.as_str(), effect.version());
        let mut state = self.state();
        if !state.effects.contains(&name) {
            state.effects.push(name);
        }
    }

    /// Records a submission made at `at` and returns its sequence number for [`Self::complete`],
    /// also watching for the completion of the submission before it when `watch_previous` and
    /// that is still running.
    pub(crate) fn record(
        &self,
        at: Instant,
        predicted_ms: Option<f64>,
        watch_previous: bool,
    ) -> Recorded {
        let mut state = self.state();
        let queue_idle = state.completed == state.recorded;
        let previous = (watch_previous && !queue_idle).then(|| {
            let watch = PreviousCompletion::default();
            let sequence = state.recorded - 1;
            state.watched.push((sequence, watch.clone()));
            watch
        });
        state.recorded += 1;
        let effects = std::mem::take(&mut state.effects);
        let frame = state.frame;
        state.entries.push_back(Entry {
            submitted_at: at,
            done_at: None,
            frame,
            effects,
            predicted_ms,
        });
        if state.entries.len() > KEPT {
            state.entries.pop_front();
            state.first += 1;
        }
        Recorded {
            sequence: state.recorded - 1,
            queue_idle,
            previous,
        }
    }

    /// Records that submission `sequence` finished at `at`, seen `as_it_happens` or not; the
    /// queue completes in order, so all earlier ones have too.
    pub(crate) fn complete(&self, sequence: u64, at: Instant, as_it_happens: bool) {
        let mut state = self.state();
        state.completed = state.completed.max(sequence + 1);
        state.watched.retain(|(watched, completion)| {
            if *watched > sequence {
                return true;
            }
            completion.set(at, as_it_happens && *watched == sequence);
            false
        });
        let Some(position) = sequence.checked_sub(state.first) else {
            return;
        };
        let Ok(position) = usize::try_from(position) else {
            return;
        };
        for entry in state.entries.iter_mut().take(position + 1) {
            entry.done_at.get_or_insert(at);
        }
    }

    /// Each kept submission's GPU time, oldest first: finished ones in full, the oldest unfinished
    /// up to `now`, later unfinished ones not at all.
    fn spans(state: &ClockState, now: Instant) -> Vec<(f64, &Entry)> {
        let mut spans = Vec::new();
        let mut previous_done: Option<Instant> = None;
        for entry in &state.entries {
            let start =
                previous_done.map_or(entry.submitted_at, |done| done.max(entry.submitted_at));
            let Some(done) = entry.done_at else {
                spans.push((milliseconds(now.saturating_duration_since(start)), entry));
                break;
            };
            spans.push((milliseconds(done.saturating_duration_since(start)), entry));
            previous_done = Some(done);
        }
        spans
    }

    /// The kept submission with the longest GPU time, counting a running one up to `now`.
    pub(crate) fn longest_at(&self, now: Instant) -> Option<SubmissionReport> {
        let state = self.state();
        Self::spans(&state, now)
            .into_iter()
            .max_by(|(a, _), (b, _)| a.total_cmp(b))
            .map(|(gpu_ms, entry)| SubmissionReport {
                gpu_ms,
                frame: entry.frame,
                effects: entry.effects.clone(),
                predicted_ms: entry.predicted_ms,
            })
    }

    /// Summed GPU time of `sequences` once all finished and are still kept. Late completions shift
    /// time between consecutive submissions but not out of the sum, so a frame's total holds.
    pub(crate) fn gpu_ms(&self, sequences: std::ops::Range<u64>) -> Option<f64> {
        let state = self.state();
        let first = state.first;
        let spans = Self::spans(&state, Instant::now());
        let mut total = 0.0;
        let mut counted = 0;
        for (position, (ms, entry)) in spans.into_iter().enumerate() {
            let sequence = first + u64::try_from(position).unwrap_or(u64::MAX);
            if sequences.contains(&sequence) {
                entry.done_at?;
                total += ms;
                counted += 1;
            }
        }
        (counted == sequences.end.saturating_sub(sequences.start)).then_some(total)
    }

    /// [`Self::longest_at`] now.
    pub(crate) fn longest(&self) -> Option<SubmissionReport> {
        self.longest_at(Instant::now())
    }

    /// The longest recent submission if it ran at least [`alpha2_gpu::hung_threshold_ms`], which
    /// implicates this renderer's own work in a loss.
    pub(crate) fn hung(&self) -> Option<SubmissionReport> {
        self.longest()
            .filter(|report| report.gpu_ms >= f64::from(alpha2_gpu::hung_threshold_ms()))
    }
}

#[derive(Clone, Debug)]
pub(crate) struct Recorded {
    pub(crate) sequence: u64,
    pub(crate) queue_idle: bool,
    pub(crate) previous: Option<PreviousCompletion>,
}

impl Recorded {
    pub(crate) fn started(&self, at: Instant) -> Option<Instant> {
        if self.queue_idle {
            return Some(at);
        }
        let (done, as_it_happens) = self.previous.as_ref()?.get()?;
        as_it_happens.then(|| done.max(at))
    }
}

#[derive(Clone, Debug, Default)]
pub(crate) struct PreviousCompletion(Arc<Mutex<Option<(Instant, bool)>>>);

impl PreviousCompletion {
    fn set(&self, at: Instant, as_it_happens: bool) {
        *self
            .0
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = Some((at, as_it_happens));
    }

    fn get(&self) -> Option<(Instant, bool)> {
        *self
            .0
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }
}

thread_local! {
    static WAITING: Cell<Option<(usize, u64)>> = const { Cell::new(None) };
}

impl SubmissionClock {
    fn address(self: &Arc<Self>) -> usize {
        Arc::as_ptr(self) as usize
    }

    pub(crate) fn sees_as_it_happens(self: &Arc<Self>, sequence: u64) -> bool {
        WAITING
            .get()
            .is_some_and(|(clock, waited)| clock == self.address() && sequence >= waited)
    }
}

#[cfg(not(target_arch = "wasm32"))]
struct Waiting(Option<(usize, u64)>);

#[cfg(not(target_arch = "wasm32"))]
impl Waiting {
    fn on(clock: usize, sequence: u64) -> Self {
        Self(WAITING.replace(Some((clock, sequence))))
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl Drop for Waiting {
    fn drop(&mut self) {
        WAITING.set(self.0);
    }
}

#[derive(Clone, Debug)]
#[cfg_attr(
    target_arch = "wasm32",
    expect(dead_code, reason = "a browser cannot wait for a submission")
)]
pub(crate) struct ClockedSubmission {
    pub(crate) index: wgpu::SubmissionIndex,
    clock: Arc<SubmissionClock>,
    sequence: u64,
}

impl ClockedSubmission {
    pub(crate) const fn new(
        index: wgpu::SubmissionIndex,
        clock: Arc<SubmissionClock>,
        sequence: u64,
    ) -> Self {
        Self {
            index,
            clock,
            sequence,
        }
    }

    /// # Errors
    /// [`crate::RenderError::GpuTimeout`] past `timeout`, or
    /// [`crate::RenderError::TextureReadback`] when polling fails.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn wait(
        &self,
        gpu: &alpha2_gpu::GpuContext,
        timeout: std::time::Duration,
    ) -> Result<(), crate::RenderError> {
        gpu.device()
            .poll(wgpu::PollType::Poll)
            .map_err(crate::error::poll_error)?;
        let _waiting = Waiting::on(self.clock.address(), self.sequence);
        gpu.device()
            .poll(wgpu::PollType::Wait {
                submission_index: Some(self.index.clone()),
                timeout: Some(timeout),
            })
            .map_err(crate::error::poll_error)?;
        Ok(())
    }
}

fn milliseconds(duration: std::time::Duration) -> f64 {
    duration.as_secs_f64() * 1_000.0
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;

    /// Submissions made at once run back to back; each starts where the previous ended.
    #[test]
    fn gpu_time_excludes_queueing_behind_earlier_submissions() {
        let clock = SubmissionClock::default();
        let start = Instant::now();
        let at = |ms: u64| start + Duration::from_millis(ms);
        let first = clock.record(at(0), None, false).sequence;
        let second = clock.record(at(0), None, false).sequence;
        let third = clock.record(at(0), None, false).sequence;
        clock.complete(first, at(10), false);
        clock.complete(second, at(400), false);
        clock.complete(third, at(405), false);
        let longest = clock.longest_at(at(1_000)).expect("submissions");
        assert!((longest.gpu_ms - 390.0).abs() < 0.5, "{longest:?}");
        // Submitted after the queue went idle, so it starts at its own submit.
        let fourth = clock.record(at(2_000), None, false).sequence;
        clock.complete(fourth, at(2_005), false);
        assert!((clock.longest_at(at(3_000)).expect("kept").gpu_ms - 390.0).abs() < 0.5);
        let _ = clock.record(at(3_000), None, false).sequence;
        assert!((clock.longest_at(at(3_700)).expect("running").gpu_ms - 700.0).abs() < 0.5);
    }

    #[test]
    fn a_completion_finishes_every_earlier_submission() {
        let clock = SubmissionClock::default();
        let start = Instant::now();
        let first = clock.record(start, None, false).sequence;
        let second = clock.record(start, None, false).sequence;
        clock.complete(second, start + Duration::from_millis(8), false);
        let _ = first;
        assert_eq!(
            clock
                .longest_at(start + Duration::from_secs(5))
                .map(|r| r.gpu_ms.round()),
            Some(8.0)
        );
    }

    #[test]
    fn a_frames_gpu_time_survives_completions_seen_together() {
        let clock = SubmissionClock::default();
        let start = Instant::now();
        let at = |ms: u64| start + Duration::from_millis(ms);
        let earlier = clock.record(at(0), None, false).sequence;
        clock.complete(earlier, at(5), false);
        let first = clock.record(at(10), Some(100.0), false).sequence;
        let second = clock.record(at(10), Some(100.0), false).sequence;
        assert_eq!(clock.gpu_ms(first..second + 1), None, "not finished");
        // Both seen at 210 ms: the first takes the whole span, and the sum is unchanged.
        clock.complete(second, at(210), false);
        let total = clock.gpu_ms(first..second + 1).expect("finished");
        assert!((total - 200.0).abs() < 0.5, "{total}");
        assert_eq!(
            clock
                .longest_at(at(300))
                .and_then(|report| report.predicted_ms),
            Some(100.0)
        );
    }

    #[test]
    fn the_clock_keeps_the_frame_and_effects_of_each_submission() {
        let clock = SubmissionClock::default();
        let start = Instant::now();
        clock.mark_frame(Some(FrameMark {
            time_ticks: 4_000,
            frame_index: Some(1),
        }));
        clock.note_effect(&crate::EffectId::from_static("blur", 2));
        let heavy = clock.record(start, None, false).sequence;
        clock.complete(heavy, start + Duration::from_millis(30), false);
        let light = clock
            .record(start + Duration::from_millis(40), None, false)
            .sequence;
        clock.complete(light, start + Duration::from_millis(41), false);
        let report = clock
            .longest_at(start + Duration::from_millis(50))
            .expect("kept");
        assert_eq!(report.effects, ["blur@2"]);
        assert_eq!(report.frame.and_then(|frame| frame.frame_index), Some(1));
    }

    #[test]
    fn a_submission_starts_on_an_idle_queue_only_once_every_earlier_one_completed() {
        let clock = SubmissionClock::default();
        let start = Instant::now();
        let first = clock.record(start, None, false);
        assert!(first.queue_idle);
        let second = clock.record(start, None, false);
        assert!(!second.queue_idle);
        clock.complete(first.sequence, start, false);
        assert!(!clock.record(start, None, false).queue_idle);
        clock.complete(second.sequence + 1, start, false);
        assert!(clock.record(start, None, false).queue_idle);
    }

    #[test]
    fn only_completions_inside_a_wait_on_them_or_earlier_ones_are_seen_as_they_happen() {
        let clock = Arc::new(SubmissionClock::default());
        let other = Arc::new(SubmissionClock::default());
        assert!(!clock.sees_as_it_happens(0));
        WAITING.set(Some((clock.address(), 5)));
        assert!(!clock.sees_as_it_happens(4));
        assert!(clock.sees_as_it_happens(5));
        assert!(clock.sees_as_it_happens(6));
        assert!(!other.sees_as_it_happens(6));
        WAITING.set(None);
    }

    #[test]
    fn a_frame_behind_a_completion_seen_as_it_happened_starts_at_that_completion() {
        let clock = SubmissionClock::default();
        let start = Instant::now();
        let at = |ms: u64| start + Duration::from_millis(ms);
        let first = clock.record(at(0), None, false);
        assert_eq!(first.started(at(0)), Some(at(0)));
        let behind = clock.record(at(1), None, true);
        let late = clock.record(at(2), None, true);
        assert_eq!(behind.started(at(1)), None, "not complete yet");
        clock.complete(first.sequence, at(10), true);
        clock.complete(behind.sequence, at(30), false);
        assert_eq!(behind.started(at(1)), Some(at(10)));
        assert_eq!(late.started(at(2)), None, "seen late");
        clock.complete(late.sequence, at(35), true);
        assert!(clock.record(at(40), None, true).previous.is_none());
    }
}