Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2989,7 +2989,11 @@ queue they came on (one `VkQueue` behind all of them).
`WaitFrameEventX` returns at the next origin with the next token (1, 2, 3, ...; 0 is the null token) and does not
return while `latency` presented frames are still waiting for their vblank. A frame presented before the origin event
is armed is not paced (`PresentX` does not hold it for a vblank), so it is shown at once and never waits for a vblank:
blank presents of a loading screen do not delay the first wait. A wait that comes late returns at once
blank presents of a loading screen do not delay the first wait. At most `latency` frames are in flight once the event
is armed: a present into a full queue (several presents with no wait between them) replaces the newest queued frame
and takes its vblank, like a mailbox, so the replaced frame is never shown (no display statistic) and the older ones
keep their vblanks. However many presents come between two waits, the next wait passes when the oldest frame is
shown. A wait that comes late returns at once
with the newest passed origin; the missed ones are skipped. A finite timeout gives
`HRESULT_FROM_WIN32(WAIT_TIMEOUT)`; nothing registered, or suspended, gives `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
`LEONIDA_PRESENT_UNCAP` (any non-empty value other than "0", read when the object is made) takes the frame-rate
Expand All @@ -3005,8 +3009,11 @@ queue they came on (one `VkQueue` behind all of them).
`SuspendX`/`ResumeX` so suspended pauses are excluded), computing nearest-rank `p50`/`p95`/`p99` and the mean drift
against the active target interval (`FramePacer::pacing_summary()`). On teardown after at least one present,
`XboxPresentation` logs a single `[rt] present: pacing summary ...` line on stderr, which `tools/boot_trace.py`
surfaces beside the D3D12 call counts. Confirmed on a fake clock by `frame_pacer_test` (known 100-frame distribution,
positive and negative drift, ring-buffer wrap-around, suspend/resume exclusion, uncapped mode) and by
surfaces beside the D3D12 call counts. The intervals are between `PresentX` calls, not between frames shown: a frame
replaced in a full queue (see the latency limit above) still counts as a present with its real interval, so the
percentiles describe the title's present cadence, and a burst of presents between two waits shows up as short
intervals. Confirmed on a fake clock by `frame_pacer_test` (known 100-frame distribution,
positive and negative drift, ring-buffer wrap-around, suspend/resume exclusion, uncapped mode, replaced frames) and by
`tests/test_boot_trace.py`.
- *`PresentX`*: 0 planes presents a black frame (no token needed). 1 plane blits plane 0's first resource (a
single-sample 2D colour texture in `PRESENT`) to the swapchain image, limited to `pSrcViewRects[0]` when set
Expand Down Expand Up @@ -3226,7 +3233,8 @@ queue they came on (one `VkQueue` behind all of them).
(placement, point/bilinear, alpha overlay, HDR10 and PQ conversions, refusals; against a CPU model), token errors,
statistics, suspend/resume, `ImmediateThresholdPercent` with a late frame at several thresholds (immediate mode
chosen, or not for 0, below the lateness, on time) and with `LEONIDA_PRESENT_NO_TEAR=1`, 30 frames at 60 Hz in real
time; skipped, exit 77, without a Vulkan device). `frame_pacer_test` also sweeps `decide_present_sync`.
time; skipped, exit 77, without a Vulkan device). `frame_pacer_test` also sweeps `decide_present_sync`, and checks
that 1000 presents between two waits leave at most `latency` frames in flight (latency 1, 2 and 3).

### 4.6 Queries

Expand Down
40 changes: 30 additions & 10 deletions runtime/src/frame_pacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
// * A frame is "in flight" from PresentX until the vblank that shows it. Frames show one per vblank, in order.
// WaitFrameEventX does not return while `latency` frames are in flight. A frame presented while the origin event
// is not armed is not paced (PresentX does not hold it for a vblank): it shows at once and is never in flight.
// * At most `latency` frames are in flight. A present into a full queue (several presents with no wait between
// them) replaces the newest queued frame and takes its vblank, like a mailbox: the replaced frame is never shown,
// and the older ones keep their vblanks. So the queue, and the next wait, never reach further than `latency`
// vblanks ahead, however many presents came in between.
// * A wait that comes late (after the next origin already passed) returns at once with the newest passed origin;
// the missed origins are skipped, not queued.
// * Tokens are 1, 2, 3, ... (0 is the null token), one per successful wait, never reused, across suspend/resume.
Expand Down Expand Up @@ -247,8 +251,9 @@ class FramePacer {

// GetFrameStatisticsX(token, type_mask, &count, array). `count` is the room in `out` on entry and the number of
// entries written on return. S_OK when every producible requested type was written (event, present, display),
// S_FALSE with count 0 or fewer when they are not (there yet, or the token is unknown or has aged out of the
// last 64). With a null `out`, count only receives how many entries are available.
// S_FALSE with count 0 or fewer when they are not (there yet, the frame was replaced in a full queue and never
// shown, or the token is unknown or has aged out of the last 64). With a null `out`, count only receives how many
// entries are available.
std::int32_t get_frame_statistics(std::uint64_t token, std::uint32_t type_mask, std::uint32_t* count,
FrameStatistic* out) {
if (!count) return pacer_hr::invalid_arg;
Expand Down Expand Up @@ -340,12 +345,10 @@ class FramePacer {
if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; }
return;
}
// The vblank after now, and not before the one after the previous queued frame.
const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0;
std::uint64_t flip = epoch_us_ + (since / period_us_ + 1) * period_us_;
if (!pending_.empty()) flip = std::max(flip, pending_.back().flip_us + period_us_);
const bool queued_before = !pending_.empty();
std::uint64_t flip = queue_flip_locked(now);
float percent = 0.0f;
if (sync == PresentSync::immediate && pending_.empty()) {
if (sync == PresentSync::immediate && !queued_before) {
flip = now;
std::uint64_t late_us = 0;
for (const Record& r : records_)
Expand All @@ -367,9 +370,7 @@ class FramePacer {
record_present_timing_locked(now, 0);
if (!interval_set_ || !any_armed_locked() || uncapped_) return;
retire_locked(now);
const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0;
std::uint64_t flip = epoch_us_ + (since / period_us_ + 1) * period_us_;
if (!pending_.empty()) flip = std::max(flip, pending_.back().flip_us + period_us_);
const std::uint64_t flip = queue_flip_locked(now);
pending_.push_back(Pending{0, flip, 0.0f});
cv_.notify_all();
}
Expand Down Expand Up @@ -464,6 +465,25 @@ class FramePacer {
bool presented = false, displayed = false;
};

// The vblank a frame presented now is queued for: the vblank after now, and not before the one after the previous
// queued frame. At most `latency_` frames are in flight, so with the queue full the newest queued frame is
// replaced (dropped: never shown) and the new one takes its vblank; the older ones keep theirs. A queue longer
// than the latency (after SetFrameIntervalX lowered it) is cut back the same way, from the newest end.
std::uint64_t queue_flip_locked(std::uint64_t now) {
if (pending_.size() >= latency_) { // latency_ is at least 1, so the queue is not empty
std::uint64_t flip = 0;
while (pending_.size() >= latency_) {
flip = pending_.back().flip_us;
pending_.pop_back();
}
return flip;
}
const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0;
std::uint64_t flip = epoch_us_ + (since / period_us_ + 1) * period_us_;
if (!pending_.empty()) flip = std::max(flip, pending_.back().flip_us + period_us_);
return flip;
}

void record_present_timing_locked(std::uint64_t now, std::uint64_t token) {
++presents_recorded_;
if (has_last_present_ && now >= last_present_us_) {
Expand Down
143 changes: 143 additions & 0 deletions runtime/test/frame_pacer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ bool blocked(std::future<Result>& f) {
return f.wait_for(std::chrono::milliseconds(40)) == std::future_status::timeout;
}

// True when the wait has returned; otherwise the pacer is suspended, which lets the waiter go, so that a failure ends
// the test instead of hanging it on the fake clock.
bool finishes(std::future<Result>& f, FramePacer& pacer) {
if (f.wait_for(std::chrono::seconds(10)) == std::future_status::ready) return true;
pacer.suspend();
return false;
}

// Display time of `token`'s frame, or no_deadline when the pacer has none (not shown, or replaced and never shown).
std::uint64_t display_time(FramePacer& pacer, std::uint64_t token) {
std::uint32_t count = 1;
FrameStatistic stat{};
if (pacer.get_frame_statistics(token, stat_display, &count, &stat) != pacer_hr::ok || count != 1)
return no_deadline;
return stat.time_us;
}

bool setup(FramePacer& pacer, std::uint32_t latency, std::uint32_t offset = 0) {
return pacer.set_frame_interval(period, latency, 0) == pacer_hr::ok &&
pacer.schedule_frame_event(frame_event_origin, offset, 0) == pacer_hr::ok;
Expand Down Expand Up @@ -212,6 +229,94 @@ int main() {
EXPECT(pacer.frames_in_flight() == 1);
}

// Presents beyond the latency while the event is armed (several presents per wait): at most `latency` frames are
// in flight. A present into a full queue replaces the newest queued frame (mailbox) and the older ones keep their
// vblanks, so the next wait passes when the oldest is shown, within one period and not 999. Latency 1, 2 and 3.
for (std::uint32_t latency = 1; latency <= 3; ++latency) {
ManualFrameClock clock;
FramePacer pacer(clock);
EXPECT(setup(pacer, latency));
clock.advance_us(period); // origin 1
std::uint64_t token = 0;
EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &token) == pacer_hr::ok && token == 1);

// 1000 blank presents with no wait between them: they queue for vblanks 2 .. latency + 1, and no further.
for (int i = 0; i < 1000; ++i) pacer.note_untokened_present();
EXPECT(pacer.frames_in_flight() == latency);
auto wait = wait_async(pacer, infinite_timeout_ms);
EXPECT(blocked(wait));
clock.advance_us(period); // vblank 2 shows the oldest: the wait passes with origin 2, not 999 periods later
EXPECT(finishes(wait, pacer));
Result r = wait.get();
EXPECT(r.hr == pacer_hr::ok && r.token == 2);
EXPECT(pacer.frames_in_flight() == latency - 1);
clock.advance_us(std::uint64_t(period) * (latency - 1)); // vblank latency + 1 shows the newest
EXPECT(pacer.frames_in_flight() == 0);

// Tokened frames: latency + 1 tokens handed out with nothing in flight, then all presented at once.
std::uint64_t tokens[4] = {};
for (std::uint32_t i = 0; i <= latency; ++i) {
clock.advance_us(period);
EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &tokens[i]) == pacer_hr::ok);
EXPECT(tokens[i] == 3 + i);
}
const std::uint64_t now = std::uint64_t(period) * (2 * latency + 2);
for (std::uint32_t i = 0; i < latency; ++i) { // `latency` presents fill the queue: nothing is replaced
EXPECT(pacer.check_present_token(tokens[i]) == pacer_hr::ok);
pacer.commit_present_token(tokens[i], 1);
}
EXPECT(pacer.frames_in_flight() == latency);
pacer.commit_present_token(tokens[latency], 1); // replaces the newest queued frame and takes its vblank
EXPECT(pacer.frames_in_flight() == latency);
wait = wait_async(pacer, infinite_timeout_ms);
EXPECT(blocked(wait));
clock.advance_us(period);
EXPECT(finishes(wait, pacer));
r = wait.get();
EXPECT(r.hr == pacer_hr::ok && r.token == latency + 4);
clock.advance_us(std::uint64_t(period) * (latency - 1));
EXPECT(pacer.frames_in_flight() == 0);
for (std::uint32_t i = 0; i + 1 < latency; ++i) // the older frames keep their vblanks
EXPECT(display_time(pacer, tokens[i]) == now + std::uint64_t(period) * (i + 1));
EXPECT(display_time(pacer, tokens[latency - 1]) == no_deadline); // replaced: never shown
EXPECT(display_time(pacer, tokens[latency]) == now + std::uint64_t(period) * latency);

// One present per wait, each a quarter period after its origin: nothing is replaced, each frame is shown at the
// vblank after its origin, and that vblank is the next origin.
token = r.token;
clock.advance_us(period); // back on an origin
for (int frame = 0; frame < 8; ++frame) {
const std::uint64_t origin = clock.now_us();
std::uint64_t next = 0;
EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &next) == pacer_hr::ok && next == token + 1);
token = next;
clock.advance_us(period / 4);
pacer.commit_present_token(token, 1);
EXPECT(pacer.frames_in_flight() == 1);
clock.advance_us(period - period / 4);
EXPECT(pacer.frames_in_flight() == 0);
EXPECT(display_time(pacer, token) == origin + period);
}
}

// SetFrameIntervalX lowering the latency below the frames already queued: the next present cuts the queue back
// to the new latency from the newest end, and the oldest frame keeps its vblank.
{
ManualFrameClock clock;
FramePacer pacer(clock);
EXPECT(setup(pacer, 3));
for (int i = 0; i < 3; ++i) pacer.note_untokened_present(); // vblanks 1, 2 and 3
EXPECT(pacer.frames_in_flight() == 3);
EXPECT(pacer.set_frame_interval(period, 1, 0) == pacer_hr::ok);
EXPECT(pacer.frames_in_flight() == 3); // already queued: left alone until the next present
pacer.note_untokened_present();
EXPECT(pacer.frames_in_flight() == 1);
clock.advance_us(period - 1);
EXPECT(pacer.frames_in_flight() == 1);
clock.advance_us(1); // vblank 1, the oldest frame's
EXPECT(pacer.frames_in_flight() == 0);
}

// Tokens: only one that was never handed out is refused. Null, stale and repeated ones are accepted and reported
// as such, so a title that presents with Token = 0, twice with one token, or out of order does not freeze.
{
Expand Down Expand Up @@ -735,6 +840,44 @@ int main() {
EXPECT(uncap.target_interval_us == 0 && near(uncap.drift_us, 0.0) && uncap.p50_us == period);
}

// BL-6 with the bounded queue: the statistics are of PresentX calls, so a frame replaced in a full queue (never
// shown) still counts as a present, with its real present-to-present interval; the replacement adds no interval,
// removes none, and the frames that are shown keep their vblanks. Latency 1, so every extra present replaces.
{
auto near = [](double a, double b) { return (a > b ? a - b : b - a) < 1e-6; };
ManualFrameClock clock;
FramePacer pacer(clock);
EXPECT(setup(pacer, 1));
clock.advance_us(period); // origin 1
std::uint64_t tok1 = 0;
EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &tok1) == pacer_hr::ok && tok1 == 1);
clock.advance_us(1000);
pacer.commit_present_token(tok1, 1); // latency 1000; queued for vblank 2
clock.advance_us(2000);
pacer.note_untokened_present(); // interval 2000; replaces tok1 and takes vblank 2
clock.advance_us(4000);
pacer.note_untokened_present(); // interval 4000; replaces the blank frame
EXPECT(pacer.frames_in_flight() == 1);
clock.advance_us(period - 7000); // vblank 2, origin 2
EXPECT(pacer.frames_in_flight() == 0);
EXPECT(display_time(pacer, tok1) == no_deadline); // replaced: never shown
std::uint64_t tok2 = 0;
EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &tok2) == pacer_hr::ok && tok2 == 2);
clock.advance_us(8000);
pacer.commit_present_token(tok2, 1); // interval period + 1000; latency 8000; vblank 3
clock.advance_us(period - 8000);
EXPECT(display_time(pacer, tok2) == 3 * std::uint64_t(period));

// 4 presents (2 of them replaced), 3 intervals {2000, 4000, 17000}: nearest-rank p50 4000, p95 and p99 17000.
const FramePacingSummary s = pacer.pacing_summary();
EXPECT(s.frames == 4 && s.samples == 3 && s.target_interval_us == period);
EXPECT(near(s.mean_us, (2000.0 + 4000.0 + period + 1000.0) / 3.0));
EXPECT(near(s.drift_us, s.mean_us - double(period)));
EXPECT(s.p50_us == 4000 && s.p95_us == period + 1000 && s.p99_us == period + 1000);
// Origin-to-present latencies of the tokened presents, the replaced tok1 included: {1000, 8000}.
EXPECT(s.latency_samples == 2 && s.latency_p50_us == 1000 && s.latency_p99_us == 8000);
}

std::printf("frame_pacer_test: all cases pass\n");
return 0;
}
5 changes: 3 additions & 2 deletions runtime/test/xbox_present_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,9 @@ int main() {
}
EXPECT(grab(frame));
EXPECT(region_is(frame, 0, 0, width, height, {0, 0, 0}, 0));
// (The blank frame is a frame in flight like any other: with latency 1 the next wait needs it shown first.)
EXPECT(xbox.pacer().frames_in_flight() == 2); // the first frame and the blank one
// (The blank frame is a frame in flight like any other, but at most `latency` frames are: with latency 1 it
// replaces the first frame, which still waits for its vblank, and the next wait needs it shown first.)
EXPECT(xbox.pacer().frames_in_flight() == 1);
clock.advance_us(period);
std::printf("0 planes (blank): ok\n");

Expand Down
Loading