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:
authorandretuta <andreviniciusmerlia@gmail.com>2026-11-24 01:43:53 -0300
committerandretuta <andreviniciusmerlia@gmail.com>2026-11-24 01:43:53 -0300
commitddafa07b463c5405eeb1c31cf5e42dca5f8a6be0 (patch)
treea6261df921f91dacaa85925d4f8baddc27a814a1 /runtime/src
parentece4ba5b31ebc6f345c8e29eae1261f2f780057a (diff)
d3d12_xs: multi-event FramePacer TOKEN_ON_TIMEOUT and XboxPresentation proxy wiring
- Support origin (0), vblank (1), and flip (2) frame events and frame_wait_flag_token_on_timeout (flag 2, returning S_FALSE with a fresh consecutive token on timeout) in runtime/src/frame_pacer.h. - Add a decoupled PresentationHandler interface to d3d12_xs_device.{h,cpp} routing device slots 78-81 and queue PresentX/SuspendX/ResumeX slots (default 30/31/32, env-overridable via LEONIDA_XS_*_SLOT). - Add attach_xbox_presentation / detach_xbox_presentation in runtime/shims/xbox_present.{h,cpp}.
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/frame_pacer.h125
1 files changed, 79 insertions, 46 deletions
diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h
index fa9c20df1..41b7c5b83 100644
--- a/runtime/src/frame_pacer.h
+++ b/runtime/src/frame_pacer.h
@@ -102,8 +102,13 @@ private:
102 std::function<void()> wake_; 102 std::function<void()> wake_;
103}; 103};
104 104
105// Provisional definitions of ours: the real constants and layouts of the console API are unmeasured. 105// Frame event kinds (0: origin, 1: vblank, 2: flip) and wait flags.
106inline constexpr std::uint32_t frame_event_origin = 0; 106inline constexpr std::uint32_t frame_event_origin = 0;
107inline constexpr std::uint32_t frame_event_vblank = 1;
108inline constexpr std::uint32_t frame_event_flip = 2;
109inline constexpr std::uint32_t frame_event_count = 3;
110// Issue a consecutive frame token even when WaitFrameEventX(origin, ...) times out (returns S_FALSE).
111inline constexpr std::uint32_t frame_wait_flag_token_on_timeout = 2;
107// Statistics types (bit mask). Interval and render entries are never produced here. 112// Statistics types (bit mask). Interval and render entries are never produced here.
108inline constexpr std::uint32_t stat_frame_interval = 0x01; 113inline constexpr std::uint32_t stat_frame_interval = 0x01;
109inline constexpr std::uint32_t stat_frame_event = 0x02; 114inline constexpr std::uint32_t stat_frame_event = 0x02;
@@ -177,38 +182,42 @@ public:
177 latency_ = std::max(latency, 1u); 182 latency_ = std::max(latency, 1u);
178 epoch_us_ = clock_.now_us(); 183 epoch_us_ = clock_.now_us();
179 interval_set_ = true; 184 interval_set_ = true;
180 offset_us_ = std::min(offset_us_, period_us - 1); 185 for (std::uint32_t k = 0; k < frame_event_count; ++k) {
181 last_origin_ = 0; // origin 0 is the epoch itself, which has passed 186 event_offset_us_[k] = std::min(event_offset_us_[k], period_us - 1);
187 last_event_tick_[k] = 0; // tick 0 is the epoch itself, which has passed
188 }
182 cv_.notify_all(); 189 cv_.notify_all();
183 return pacer_hr::ok; 190 return pacer_hr::ok;
184 } 191 }
185 192
186 // ScheduleFrameEventX(event, offset, ..., flags). Only the origin event exists here. 193 // ScheduleFrameEventX(event, offset, ..., flags). Supports origin (0), vblank (1), and flip (2).
187 std::int32_t schedule_frame_event(std::uint32_t event, std::uint32_t offset_us, std::uint32_t flags) { 194 std::int32_t schedule_frame_event(std::uint32_t event, std::uint32_t offset_us, std::uint32_t flags) {
188 (void)flags; 195 (void)flags;
189 if (event != frame_event_origin) return pacer_hr::invalid_arg; 196 if (event >= frame_event_count) return pacer_hr::invalid_arg;
190 std::lock_guard guard(mutex_); 197 std::lock_guard guard(mutex_);
191 if (suspended_ || !interval_set_) return pacer_hr::invalid_state; 198 if (suspended_ || !interval_set_) return pacer_hr::invalid_state;
192 if (offset_us >= period_us_) return pacer_hr::invalid_arg; 199 if (offset_us >= period_us_) return pacer_hr::invalid_arg;
193 offset_us_ = offset_us; 200 event_offset_us_[event] = offset_us;
194 armed_ = true; 201 event_armed_[event] = true;
195 // The first origin is the next one that has not come yet: origin n is due at epoch + n * period + offset. 202 // The first event is the next one that has not come yet: tick n is due at epoch + n * period + offset.
196 const std::uint64_t now = clock_.now_us(); 203 const std::uint64_t now = clock_.now_us();
197 last_origin_ = now >= epoch_us_ + offset_us_ ? std::int64_t((now - epoch_us_ - offset_us_) / period_us_) : 0; 204 last_event_tick_[event] =
205 now >= epoch_us_ + offset_us ? std::int64_t((now - epoch_us_ - offset_us) / period_us_) : 0;
198 cv_.notify_all(); 206 cv_.notify_all();
199 return pacer_hr::ok; 207 return pacer_hr::ok;
200 } 208 }
201 209
202 // WaitFrameEventX(event, timeout, ..., flags, &token). Blocks until the next origin, and while `latency` frames 210 // WaitFrameEventX(event, timeout, ..., flags, &token). Origin waits block until the next origin tick and while
203 // are in flight. pacer_hr::timeout when `timeout_ms` passes first (infinite_timeout_ms: never); the token is left 211 // `latency` frames are in flight, issuing a consecutive token on success (or on timeout with
204 // alone then. invalid_state when nothing is registered, the pacer is suspended, or one of those happens while waiting. 212 // frame_wait_flag_token_on_timeout, returning S_FALSE). Vblank/flip waits pace independently per event kind and
213 // leave `*token` untouched.
205 std::int32_t wait_frame_event(std::uint32_t event, std::uint32_t timeout_ms, std::uint32_t flags, 214 std::int32_t wait_frame_event(std::uint32_t event, std::uint32_t timeout_ms, std::uint32_t flags,
206 std::uint64_t* token) { 215 std::uint64_t* token) {
207 (void)flags; 216 if (event >= frame_event_count) return pacer_hr::invalid_arg;
208 if (event != frame_event_origin || !token) return pacer_hr::invalid_arg; 217 if (event == frame_event_origin && !token) return pacer_hr::invalid_arg;
209 std::unique_lock lock(mutex_); 218 std::unique_lock lock(mutex_);
210 ++waiters_; 219 ++waiters_;
211 const std::int32_t result = wait_locked(lock, timeout_ms, token); 220 const std::int32_t result = wait_locked(lock, event, timeout_ms, flags, token);
212 if (--waiters_ == 0) cv_.notify_all(); 221 if (--waiters_ == 0) cv_.notify_all();
213 return result; 222 return result;
214 } 223 }
@@ -302,7 +311,7 @@ public:
302 const std::uint64_t now = clock_.now_us(); 311 const std::uint64_t now = clock_.now_us();
303 retire_locked(now); 312 retire_locked(now);
304 last_presented_ = std::max(last_presented_, token); 313 last_presented_ = std::max(last_presented_, token);
305 if (!interval_set_ || !armed_ || uncapped_) { // not paced or uncapped: shows at once 314 if (!interval_set_ || !any_armed_locked() || uncapped_) { // not paced or uncapped: shows at once
306 for (Record& r : records_) 315 for (Record& r : records_)
307 if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; } 316 if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; }
308 return; 317 return;
@@ -330,7 +339,7 @@ public:
330 // the origin event not armed or the cap off it is not paced: it shows at once and is not counted. 339 // the origin event not armed or the cap off it is not paced: it shows at once and is not counted.
331 void note_untokened_present() { 340 void note_untokened_present() {
332 std::lock_guard guard(mutex_); 341 std::lock_guard guard(mutex_);
333 if (!interval_set_ || !armed_ || uncapped_) return; 342 if (!interval_set_ || !any_armed_locked() || uncapped_) return;
334 const std::uint64_t now = clock_.now_us(); 343 const std::uint64_t now = clock_.now_us();
335 retire_locked(now); 344 retire_locked(now);
336 const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0; 345 const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0;
@@ -344,7 +353,8 @@ public:
344 void suspend() { 353 void suspend() {
345 std::lock_guard guard(mutex_); 354 std::lock_guard guard(mutex_);
346 suspended_ = true; 355 suspended_ = true;
347 interval_set_ = armed_ = false; 356 interval_set_ = false;
357 for (bool& a : event_armed_) a = false;
348 pending_.clear(); 358 pending_.clear();
349 cv_.notify_all(); 359 cv_.notify_all();
350 } 360 }
@@ -352,7 +362,8 @@ public:
352 void resume() { 362 void resume() {
353 std::lock_guard guard(mutex_); 363 std::lock_guard guard(mutex_);
354 suspended_ = false; 364 suspended_ = false;
355 interval_set_ = armed_ = false; 365 interval_set_ = false;
366 for (bool& a : event_armed_) a = false;
356 cv_.notify_all(); 367 cv_.notify_all();
357 } 368 }
358 369
@@ -366,7 +377,7 @@ public:
366 // Frames that were flipped mid-scan because of ImmediateThresholdPercent. 377 // Frames that were flipped mid-scan because of ImmediateThresholdPercent.
367 std::uint64_t immediate_presents() const { std::lock_guard guard(mutex_); return immediate_presents_; } 378 std::uint64_t immediate_presents() const { std::lock_guard guard(mutex_); return immediate_presents_; }
368 std::uint32_t period_us() const { std::lock_guard guard(mutex_); return period_us_; } 379 std::uint32_t period_us() const { std::lock_guard guard(mutex_); return period_us_; }
369 bool registered() const { std::lock_guard guard(mutex_); return interval_set_ && armed_; } 380 bool registered() const { std::lock_guard guard(mutex_); return interval_set_ && any_armed_locked(); }
370 381
371 // The fps cap off (LEONIDA_PRESENT_UNCAP; see the top comment): waits never block and presents count as shown 382 // The fps cap off (LEONIDA_PRESENT_UNCAP; see the top comment): waits never block and presents count as shown
372 // at once. Registration and suspension keep meaning what they did; the flag survives suspend/resume. 383 // at once. Registration and suspension keep meaning what they did; the flag survives suspend/resume.
@@ -399,45 +410,67 @@ private:
399 } 410 }
400 } 411 }
401 412
402 std::int32_t wait_locked(std::unique_lock<std::mutex>& lock, std::uint32_t timeout_ms, std::uint64_t* token) { 413 bool any_armed_locked() const {
414 return event_armed_[0] || event_armed_[1] || event_armed_[2];
415 }
416
417 std::uint64_t issue_token_locked(std::uint64_t origin_us, std::uint64_t due_us) {
418 const std::uint64_t tok = ++issued_;
419 Record record;
420 record.token = tok;
421 record.origin_us = origin_us;
422 record.due_us = due_us;
423 records_.push_back(record);
424 if (records_.size() > 64) records_.pop_front();
425 return tok;
426 }
427
428 std::int32_t wait_locked(std::unique_lock<std::mutex>& lock, std::uint32_t event, std::uint32_t timeout_ms,
429 std::uint32_t flags, std::uint64_t* token) {
430 const bool is_origin = (event == frame_event_origin);
431 const bool token_on_timeout = (flags & frame_wait_flag_token_on_timeout) != 0;
403 const std::uint64_t start = clock_.now_us(); 432 const std::uint64_t start = clock_.now_us();
404 const std::uint64_t give_up = timeout_ms == infinite_timeout_ms ? no_deadline 433 const std::uint64_t give_up = timeout_ms == infinite_timeout_ms ? no_deadline
405 : start + std::uint64_t(timeout_ms) * 1000; 434 : start + std::uint64_t(timeout_ms) * 1000;
406 for (;;) { 435 for (;;) {
407 if (shutdown_ || suspended_ || !interval_set_ || !armed_) return pacer_hr::invalid_state; 436 if (shutdown_ || suspended_ || !interval_set_) return pacer_hr::invalid_state;
437 if (is_origin ? !event_armed_[frame_event_origin]
438 : (!event_armed_[event] && !event_armed_[frame_event_origin])) {
439 return pacer_hr::invalid_state;
440 }
408 const std::uint64_t now = clock_.now_us(); 441 const std::uint64_t now = clock_.now_us();
409 retire_locked(now); 442 retire_locked(now);
410 if (uncapped_) { // no vblank grid and no latency throttle: a token at once, due now 443 if (uncapped_) { // no vblank grid and no latency throttle
411 *token = ++issued_; 444 if (is_origin) *token = issue_token_locked(now, now);
412 Record record;
413 record.token = *token;
414 record.origin_us = now;
415 record.due_us = now;
416 records_.push_back(record);
417 if (records_.size() > 64) records_.pop_front();
418 return pacer_hr::ok; 445 return pacer_hr::ok;
419 } 446 }
420 const std::uint64_t first = epoch_us_ + offset_us_; // origin 0 447 const std::uint32_t offset = event_offset_us_[event];
421 const std::int64_t next = last_origin_ + 1; 448 const std::uint64_t first = epoch_us_ + offset; // tick 0
449 const std::int64_t next = last_event_tick_[event] + 1;
422 const std::uint64_t next_us = first + std::uint64_t(next) * period_us_; 450 const std::uint64_t next_us = first + std::uint64_t(next) * period_us_;
423 std::uint64_t wake = next_us; 451 std::uint64_t wake = next_us;
424 if (now >= next_us) { 452 if (now >= next_us) {
425 if (pending_.size() < latency_) { 453 if (!is_origin || pending_.size() < latency_) {
426 const std::int64_t newest = std::int64_t((now - first) / period_us_); 454 const std::int64_t newest = std::int64_t((now - first) / period_us_);
427 const std::int64_t origin = std::max(next, newest); 455 const std::int64_t tick = std::max(next, newest);
428 last_origin_ = origin; 456 last_event_tick_[event] = tick;
429 *token = ++issued_; 457 if (is_origin) {
430 Record record; 458 const std::uint64_t origin_us = first + std::uint64_t(tick) * period_us_;
431 record.token = *token; 459 const std::uint64_t due_us = origin_us - offset + period_us_;
432 record.origin_us = first + std::uint64_t(origin) * period_us_; 460 *token = issue_token_locked(origin_us, due_us);
433 record.due_us = record.origin_us - offset_us_ + period_us_; 461 }
434 records_.push_back(record);
435 if (records_.size() > 64) records_.pop_front();
436 return pacer_hr::ok; 462 return pacer_hr::ok;
437 } 463 }
438 wake = pending_.front().flip_us; // blocked by the latency: until the oldest frame is shown 464 wake = pending_.front().flip_us; // blocked by the latency: until the oldest frame is shown
439 } 465 }
440 if (give_up != no_deadline && now >= give_up) return pacer_hr::timeout; 466 if (give_up != no_deadline && now >= give_up) {
467 if (is_origin && token_on_timeout) {
468 // Frame started early on timeout: issue a consecutive token without consuming the tick.
469 *token = issue_token_locked(now, next_us - offset + period_us_);
470 return pacer_hr::false_;
471 }
472 return token_on_timeout ? pacer_hr::false_ : pacer_hr::timeout;
473 }
441 clock_.wait(lock, cv_, give_up == no_deadline ? wake : std::min(wake, give_up)); 474 clock_.wait(lock, cv_, give_up == no_deadline ? wake : std::min(wake, give_up));
442 } 475 }
443 } 476 }
@@ -450,13 +483,13 @@ private:
450 483
451 bool suspended_ = false; 484 bool suspended_ = false;
452 bool interval_set_ = false; 485 bool interval_set_ = false;
453 bool armed_ = false; 486 bool event_armed_[frame_event_count] = {false, false, false};
454 bool uncapped_ = false; 487 bool uncapped_ = false;
455 std::uint32_t period_us_ = 0; 488 std::uint32_t period_us_ = 0;
456 std::uint32_t latency_ = 1; 489 std::uint32_t latency_ = 1;
457 std::uint32_t offset_us_ = 0; 490 std::uint32_t event_offset_us_[frame_event_count] = {0, 0, 0};
458 std::uint64_t epoch_us_ = 0; 491 std::uint64_t epoch_us_ = 0;
459 std::int64_t last_origin_ = 0; // origin 0 is the epoch itself, which has passed 492 std::int64_t last_event_tick_[frame_event_count] = {0, 0, 0}; // tick 0 is the epoch itself, which has passed
460 std::uint64_t issued_ = 0; 493 std::uint64_t issued_ = 0;
461 std::uint64_t last_presented_ = 0; 494 std::uint64_t last_presented_ = 0;
462 std::uint64_t immediate_presents_ = 0; 495 std::uint64_t immediate_presents_ = 0;