upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 22:51:02 +0800
committerRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 22:51:02 +0800
commit2d9987f3cd488b1c466b156d24948d7cc14ab7ef (patch)
tree0187e06cb4ab4f4ea5650210fc4ff1e88f64019e /runtime/src
parent7cee65bc7c53b6a90ed04ca7f37c778373ff397f (diff)
parentd0da7dade8b688f2e0f7f23f4155d6cbb438809e (diff)
Merge the upstream offline services, frame events and queue ABI into the Windows UI integration
Retain the first-run origin-token poll and shared tokens alongside standard origin, vblank and flip events. Route attached presenter callbacks before the fallback window and dispatch local speech through the Windows provider with the upstream offline fallback. The offline pipeline checker now enables the runtime mesh query feature for cache-producing runs. A controlled GPU comparison with the same compute shader changed from a miss to an application cache hit.
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/frame_pacer.h163
1 files changed, 100 insertions, 63 deletions
diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h
index 6e14c60d5..e157e39a3 100644
--- a/runtime/src/frame_pacer.h
+++ b/runtime/src/frame_pacer.h
@@ -1,4 +1,6 @@
1#pragma once 1#pragma once
2#include <utility>
3#include <memory>
2 4
3// Frame pacing for the Xbox presentation path (docs/d3d12_vulkan.md section 4.5): what SetFrameIntervalX, 5// Frame pacing for the Xbox presentation path (docs/d3d12_vulkan.md section 4.5): what SetFrameIntervalX,
4// ScheduleFrameEventX, WaitFrameEventX and GetFrameStatisticsX do, and the token check and flip bookkeeping that 6// ScheduleFrameEventX, WaitFrameEventX and GetFrameStatisticsX do, and the token check and flip bookkeeping that
@@ -37,9 +39,7 @@
37#include <deque> 39#include <deque>
38#include <functional> 40#include <functional>
39#include <limits> 41#include <limits>
40#include <memory>
41#include <mutex> 42#include <mutex>
42#include <utility>
43 43
44namespace leonida { 44namespace leonida {
45 45
@@ -104,10 +104,15 @@ private:
104 std::function<void()> wake_; 104 std::function<void()> wake_;
105}; 105};
106 106
107// Provisional definitions of ours: the real constants and layouts of the console API are unmeasured. 107// Provisional definitions of ours: the real constants and layouts of the console API are unmeasured. Frame event
108// kinds (0: origin, 1: vblank, 2: flip) and wait flags.
108inline constexpr std::uint32_t frame_event_origin = 0; 109inline constexpr std::uint32_t frame_event_origin = 0;
109// WaitFrameEventX flag 2 with timeout 0 is the retail title's poll for the current token, which it asserts succeeds. 110inline constexpr std::uint32_t frame_event_vblank = 1;
110inline constexpr std::uint32_t wait_flag_current_token = 0x2; 111inline constexpr std::uint32_t frame_event_flip = 2;
112inline constexpr std::uint32_t frame_event_count = 3;
113// Issue a consecutive frame token even when WaitFrameEventX(origin, ...) times out (returns S_FALSE).
114inline constexpr std::uint32_t frame_wait_flag_token_on_timeout = 2;
115inline constexpr std::uint32_t wait_flag_current_token = frame_wait_flag_token_on_timeout;
111// Statistics types (bit mask). Interval and render entries are never produced here. 116// Statistics types (bit mask). Interval and render entries are never produced here.
112inline constexpr std::uint32_t stat_frame_interval = 0x01; 117inline constexpr std::uint32_t stat_frame_interval = 0x01;
113inline constexpr std::uint32_t stat_frame_event = 0x02; 118inline constexpr std::uint32_t stat_frame_event = 0x02;
@@ -158,8 +163,10 @@ private:
158 163
159class FramePacer { 164class FramePacer {
160public: 165public:
161 explicit FramePacer(FrameClock& clock, std::shared_ptr<FrameTokenSource> tokens = nullptr) 166 explicit FramePacer(FrameClock& clock, std::shared_ptr<FrameTokenSource> tokens = nullptr,
162 : clock_(clock), tokens_(tokens ? std::move(tokens) : std::make_shared<FrameTokenSource>()) { 167 bool current_origin_poll = false)
168 : clock_(clock), tokens_(tokens ? std::move(tokens) : std::make_shared<FrameTokenSource>()),
169 current_origin_poll_(current_origin_poll) {
163 clock_.set_wake([this] { 170 clock_.set_wake([this] {
164 { std::lock_guard guard(mutex_); } 171 { std::lock_guard guard(mutex_); }
165 cv_.notify_all(); 172 cv_.notify_all();
@@ -191,38 +198,44 @@ public:
191 latency_ = std::max(latency, 1u); 198 latency_ = std::max(latency, 1u);
192 epoch_us_ = clock_.now_us(); 199 epoch_us_ = clock_.now_us();
193 interval_set_ = true; 200 interval_set_ = true;
194 offset_us_ = std::min(offset_us_, period_us - 1); 201 for (std::uint32_t k = 0; k < frame_event_count; ++k) {
195 last_origin_ = 0; // origin 0 is the epoch itself, which has passed 202 event_offset_us_[k] = std::min(event_offset_us_[k], period_us - 1);
203 last_event_tick_[k] = 0; // tick 0 is the epoch itself, which has passed
204 }
196 cv_.notify_all(); 205 cv_.notify_all();
197 return pacer_hr::ok; 206 return pacer_hr::ok;
198 } 207 }
199 208
200 // ScheduleFrameEventX(event, offset, ..., flags). Only the origin event exists here. 209 // ScheduleFrameEventX(event, offset, ..., flags). Supports origin (0), vblank (1), and flip (2).
201 std::int32_t schedule_frame_event(std::uint32_t event, std::uint32_t offset_us, std::uint32_t flags) { 210 std::int32_t schedule_frame_event(std::uint32_t event, std::uint32_t offset_us, std::uint32_t flags) {
202 (void)flags; 211 (void)flags;
203 if (event != frame_event_origin) return pacer_hr::invalid_arg; 212 if (event >= frame_event_count) return pacer_hr::invalid_arg;
204 std::lock_guard guard(mutex_); 213 std::lock_guard guard(mutex_);
205 if (suspended_ || !interval_set_) return pacer_hr::invalid_state; 214 if (suspended_ || !interval_set_) return pacer_hr::invalid_state;
206 if (offset_us >= period_us_) return pacer_hr::invalid_arg; 215 if (offset_us >= period_us_) return pacer_hr::invalid_arg;
207 offset_us_ = offset_us; 216 event_offset_us_[event] = offset_us;
208 armed_ = true; 217 event_armed_[event] = true;
209 // The first origin is the next one that has not come yet: origin n is due at epoch + n * period + offset. 218 // The first event is the next one that has not come yet: tick n is due at epoch + n * period + offset.
210 const std::uint64_t now = clock_.now_us(); 219 const std::uint64_t now = clock_.now_us();
211 last_origin_ = now >= epoch_us_ + offset_us_ ? std::int64_t((now - epoch_us_ - offset_us_) / period_us_) : 0; 220 last_event_tick_[event] =
221 now >= epoch_us_ + offset_us ? std::int64_t((now - epoch_us_ - offset_us) / period_us_) : 0;
212 cv_.notify_all(); 222 cv_.notify_all();
213 return pacer_hr::ok; 223 return pacer_hr::ok;
214 } 224 }
215 225
216 // WaitFrameEventX(event, timeout, ..., flags, &token). Blocks until the next origin, and while `latency` frames 226 // WaitFrameEventX(event, timeout, ..., flags, &token). Origin waits block until the next origin tick and while
217 // are in flight. pacer_hr::timeout when `timeout_ms` passes first (infinite_timeout_ms: never); the token is left 227 // `latency` frames are in flight, issuing a consecutive token on success (or on timeout with
218 // alone then. invalid_state when nothing is registered, the pacer is suspended, or one of those happens while waiting. 228 // frame_wait_flag_token_on_timeout, returning S_FALSE). Vblank/flip waits pace independently per event kind and
229 // leave `*token` untouched.
219 std::int32_t wait_frame_event(std::uint32_t event, std::uint32_t timeout_ms, std::uint32_t flags, 230 std::int32_t wait_frame_event(std::uint32_t event, std::uint32_t timeout_ms, std::uint32_t flags,
220 std::uint64_t* token) { 231 std::uint64_t* token) {
221 if (event != frame_event_origin || !token) return pacer_hr::invalid_arg; 232 if (event >= frame_event_count) return pacer_hr::invalid_arg;
233 if (event == frame_event_origin && !token) return pacer_hr::invalid_arg;
222 std::unique_lock lock(mutex_); 234 std::unique_lock lock(mutex_);
223 if (flags & wait_flag_current_token) return current_token_locked(token); 235 if (current_origin_poll_ && event == frame_event_origin && timeout_ms == 0 && (flags & wait_flag_current_token))
236 return current_token_locked(token);
224 ++waiters_; 237 ++waiters_;
225 const std::int32_t result = wait_locked(lock, timeout_ms, token); 238 const std::int32_t result = wait_locked(lock, event, timeout_ms, flags, token);
226 if (--waiters_ == 0) cv_.notify_all(); 239 if (--waiters_ == 0) cv_.notify_all();
227 return result; 240 return result;
228 } 241 }
@@ -316,7 +329,7 @@ public:
316 const std::uint64_t now = clock_.now_us(); 329 const std::uint64_t now = clock_.now_us();
317 retire_locked(now); 330 retire_locked(now);
318 last_presented_ = std::max(last_presented_, token); 331 last_presented_ = std::max(last_presented_, token);
319 if (!interval_set_ || !armed_ || uncapped_) { // not paced or uncapped: shows at once 332 if (!interval_set_ || !any_armed_locked() || uncapped_) { // not paced or uncapped: shows at once
320 for (Record& r : records_) 333 for (Record& r : records_)
321 if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; } 334 if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; }
322 return; 335 return;
@@ -344,7 +357,7 @@ public:
344 // the origin event not armed or the cap off it is not paced: it shows at once and is not counted. 357 // the origin event not armed or the cap off it is not paced: it shows at once and is not counted.
345 void note_untokened_present() { 358 void note_untokened_present() {
346 std::lock_guard guard(mutex_); 359 std::lock_guard guard(mutex_);
347 if (!interval_set_ || !armed_ || uncapped_) return; 360 if (!interval_set_ || !any_armed_locked() || uncapped_) return;
348 const std::uint64_t now = clock_.now_us(); 361 const std::uint64_t now = clock_.now_us();
349 retire_locked(now); 362 retire_locked(now);
350 const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0; 363 const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0;
@@ -358,7 +371,8 @@ public:
358 void suspend() { 371 void suspend() {
359 std::lock_guard guard(mutex_); 372 std::lock_guard guard(mutex_);
360 suspended_ = true; 373 suspended_ = true;
361 interval_set_ = armed_ = false; 374 interval_set_ = false;
375 for (bool& a : event_armed_) a = false;
362 pending_.clear(); 376 pending_.clear();
363 cv_.notify_all(); 377 cv_.notify_all();
364 } 378 }
@@ -366,7 +380,8 @@ public:
366 void resume() { 380 void resume() {
367 std::lock_guard guard(mutex_); 381 std::lock_guard guard(mutex_);
368 suspended_ = false; 382 suspended_ = false;
369 interval_set_ = armed_ = false; 383 interval_set_ = false;
384 for (bool& a : event_armed_) a = false;
370 cv_.notify_all(); 385 cv_.notify_all();
371 } 386 }
372 387
@@ -381,7 +396,7 @@ public:
381 // Frames that were flipped mid-scan because of ImmediateThresholdPercent. 396 // Frames that were flipped mid-scan because of ImmediateThresholdPercent.
382 std::uint64_t immediate_presents() const { std::lock_guard guard(mutex_); return immediate_presents_; } 397 std::uint64_t immediate_presents() const { std::lock_guard guard(mutex_); return immediate_presents_; }
383 std::uint32_t period_us() const { std::lock_guard guard(mutex_); return period_us_; } 398 std::uint32_t period_us() const { std::lock_guard guard(mutex_); return period_us_; }
384 bool registered() const { std::lock_guard guard(mutex_); return interval_set_ && armed_; } 399 bool registered() const { std::lock_guard guard(mutex_); return interval_set_ && any_armed_locked(); }
385 400
386 // The fps cap off (LEONIDA_PRESENT_UNCAP; see the top comment): waits never block and presents count as shown 401 // The fps cap off (LEONIDA_PRESENT_UNCAP; see the top comment): waits never block and presents count as shown
387 // at once. Registration and suspension keep meaning what they did; the flag survives suspend/resume. 402 // at once. Registration and suspension keep meaning what they did; the flag survives suspend/resume.
@@ -414,64 +429,85 @@ private:
414 } 429 }
415 } 430 }
416 431
417 // Never blocks: a fresh token for the newest passed origin, with no wait for the next origin or the latency.
418 std::int32_t current_token_locked(std::uint64_t* token) { 432 std::int32_t current_token_locked(std::uint64_t* token) {
419 if (shutdown_ || suspended_) return pacer_hr::invalid_state; 433 if (shutdown_ || suspended_) return pacer_hr::invalid_state;
420 *token = tokens_->issue(); 434 if (!interval_set_ || !event_armed_[frame_event_origin]) {
421 if (!interval_set_ || !armed_) return pacer_hr::ok; 435 *token = tokens_->issue();
422 const std::uint64_t now = clock_.now_us(); 436 return pacer_hr::ok;
437 }
438 const auto now = clock_.now_us();
423 retire_locked(now); 439 retire_locked(now);
424 const std::uint64_t first = epoch_us_ + offset_us_; 440 const auto offset = event_offset_us_[frame_event_origin];
425 const std::int64_t newest = now >= first ? std::int64_t((now - first) / period_us_) : 0; 441 const auto first = epoch_us_ + offset;
426 last_origin_ = std::max(last_origin_, newest); 442 const auto newest = now >= first ? static_cast<std::int64_t>((now - first) / period_us_) : 0;
443 auto& tick = last_event_tick_[frame_event_origin];
444 tick = std::max(tick, newest);
445 const auto origin = first + static_cast<std::uint64_t>(tick) * period_us_;
446 *token = issue_token_locked(origin, origin - offset + period_us_);
447 return pacer_hr::ok;
448 }
449
450 bool any_armed_locked() const {
451 return event_armed_[0] || event_armed_[1] || event_armed_[2];
452 }
453
454 std::uint64_t issue_token_locked(std::uint64_t origin_us, std::uint64_t due_us) {
455 const std::uint64_t tok = tokens_->issue();
427 Record record; 456 Record record;
428 record.token = *token; 457 record.token = tok;
429 record.origin_us = first + std::uint64_t(last_origin_) * period_us_; 458 record.origin_us = origin_us;
430 record.due_us = record.origin_us - offset_us_ + period_us_; 459 record.due_us = due_us;
431 records_.push_back(record); 460 records_.push_back(record);
432 if (records_.size() > 64) records_.pop_front(); 461 if (records_.size() > 64) records_.pop_front();
433 return pacer_hr::ok; 462 return tok;
434 } 463 }
435 464
436 std::int32_t wait_locked(std::unique_lock<std::mutex>& lock, std::uint32_t timeout_ms, std::uint64_t* token) { 465 std::int32_t wait_locked(std::unique_lock<std::mutex>& lock, std::uint32_t event, std::uint32_t timeout_ms,
466 std::uint32_t flags, std::uint64_t* token) {
467 const bool is_origin = (event == frame_event_origin);
468 const bool token_on_timeout = (flags & frame_wait_flag_token_on_timeout) != 0;
437 const std::uint64_t start = clock_.now_us(); 469 const std::uint64_t start = clock_.now_us();
438 const std::uint64_t give_up = timeout_ms == infinite_timeout_ms ? no_deadline 470 const std::uint64_t give_up = timeout_ms == infinite_timeout_ms ? no_deadline
439 : start + std::uint64_t(timeout_ms) * 1000; 471 : start + std::uint64_t(timeout_ms) * 1000;
440 for (;;) { 472 for (;;) {
441 if (shutdown_ || suspended_ || !interval_set_ || !armed_) return pacer_hr::invalid_state; 473 if (shutdown_ || suspended_ || !interval_set_) return pacer_hr::invalid_state;
474 if (is_origin ? !event_armed_[frame_event_origin]
475 : (!event_armed_[event] && !event_armed_[frame_event_origin])) {
476 return pacer_hr::invalid_state;
477 }
442 const std::uint64_t now = clock_.now_us(); 478 const std::uint64_t now = clock_.now_us();
443 retire_locked(now); 479 retire_locked(now);
444 if (uncapped_) { // no vblank grid and no latency throttle: a token at once, due now 480 if (uncapped_) { // no vblank grid and no latency throttle
445 *token = tokens_->issue(); 481 if (is_origin) *token = issue_token_locked(now, now);
446 Record record;
447 record.token = *token;
448 record.origin_us = now;
449 record.due_us = now;
450 records_.push_back(record);
451 if (records_.size() > 64) records_.pop_front();
452 return pacer_hr::ok; 482 return pacer_hr::ok;
453 } 483 }
454 const std::uint64_t first = epoch_us_ + offset_us_; // origin 0 484 const std::uint32_t offset = event_offset_us_[event];
455 const std::int64_t next = last_origin_ + 1; 485 const std::uint64_t first = epoch_us_ + offset; // tick 0
486 const std::int64_t next = last_event_tick_[event] + 1;
456 const std::uint64_t next_us = first + std::uint64_t(next) * period_us_; 487 const std::uint64_t next_us = first + std::uint64_t(next) * period_us_;
457 std::uint64_t wake = next_us; 488 std::uint64_t wake = next_us;
458 if (now >= next_us) { 489 if (now >= next_us) {
459 if (pending_.size() < latency_) { 490 if (!is_origin || pending_.size() < latency_) {
460 const std::int64_t newest = std::int64_t((now - first) / period_us_); 491 const std::int64_t newest = std::int64_t((now - first) / period_us_);
461 const std::int64_t origin = std::max(next, newest); 492 const std::int64_t tick = std::max(next, newest);
462 last_origin_ = origin; 493 last_event_tick_[event] = tick;
463 *token = tokens_->issue(); 494 if (is_origin) {
464 Record record; 495 const std::uint64_t origin_us = first + std::uint64_t(tick) * period_us_;
465 record.token = *token; 496 const std::uint64_t due_us = origin_us - offset + period_us_;
466 record.origin_us = first + std::uint64_t(origin) * period_us_; 497 *token = issue_token_locked(origin_us, due_us);
467 record.due_us = record.origin_us - offset_us_ + period_us_; 498 }
468 records_.push_back(record);
469 if (records_.size() > 64) records_.pop_front();
470 return pacer_hr::ok; 499 return pacer_hr::ok;
471 } 500 }
472 wake = pending_.front().flip_us; // blocked by the latency: until the oldest frame is shown 501 wake = pending_.front().flip_us; // blocked by the latency: until the oldest frame is shown
473 } 502 }
474 if (give_up != no_deadline && now >= give_up) return pacer_hr::timeout; 503 if (give_up != no_deadline && now >= give_up) {
504 if (is_origin && token_on_timeout) {
505 // Frame started early on timeout: issue a consecutive token without consuming the tick.
506 *token = issue_token_locked(now, next_us - offset + period_us_);
507 return pacer_hr::false_;
508 }
509 return token_on_timeout ? pacer_hr::false_ : pacer_hr::timeout;
510 }
475 clock_.wait(lock, cv_, give_up == no_deadline ? wake : std::min(wake, give_up)); 511 clock_.wait(lock, cv_, give_up == no_deadline ? wake : std::min(wake, give_up));
476 } 512 }
477 } 513 }
@@ -484,14 +520,15 @@ private:
484 520
485 bool suspended_ = false; 521 bool suspended_ = false;
486 bool interval_set_ = false; 522 bool interval_set_ = false;
487 bool armed_ = false; 523 bool event_armed_[frame_event_count] = {false, false, false};
488 bool uncapped_ = false; 524 bool uncapped_ = false;
489 std::uint32_t period_us_ = 0; 525 std::uint32_t period_us_ = 0;
490 std::uint32_t latency_ = 1; 526 std::uint32_t latency_ = 1;
491 std::uint32_t offset_us_ = 0; 527 std::uint32_t event_offset_us_[frame_event_count] = {0, 0, 0};
492 std::uint64_t epoch_us_ = 0; 528 std::uint64_t epoch_us_ = 0;
493 std::int64_t last_origin_ = 0; // origin 0 is the epoch itself, which has passed 529 std::int64_t last_event_tick_[frame_event_count] = {0, 0, 0}; // tick 0 is the epoch itself, which has passed
494 std::shared_ptr<FrameTokenSource> tokens_; 530 std::shared_ptr<FrameTokenSource> tokens_;
531 bool current_origin_poll_ = false;
495 std::uint64_t last_presented_ = 0; 532 std::uint64_t last_presented_ = 0;
496 std::uint64_t immediate_presents_ = 0; 533 std::uint64_t immediate_presents_ = 0;
497 std::deque<Pending> pending_; 534 std::deque<Pending> pending_;