leonida-gpu: debounce the frame-generation shown count, so one bad poll cannot latch 5 fps (#3260) #3458

Code

Clone this repository

Checkout

From your project repository, check out a new branch and test the changes.

git fetch -u  "d3d12/fg-shown-debounce:KelboLive-d3d12/fg-shown-debounce"
git switch "KelboLive-d3d12/fg-shown-debounce"
Open
leonida/mandy
KelboLive wants to merge 2 commits from KelboLive/mandy:d3d12/fg-shown-debounce into main

This pull request can be merged automatically.

Conversation
Reviewers 0 approvals
  • No reviewers
Details
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'd3d12/fg-shown-debounce:KelboLive-d3d12/fg-shown-debounce'
git switch 'KelboLive-d3d12/fg-shown-debounce'
First-time contributor

What/why

#3260: the frame rate can lock to exactly 5.0 fps after a few minutes. The audit found it is the FG auto-limiter's shown-frame divisor, not load:

  • a single dlssg_get_state poll with per != 1 forced shown = 1 (old fg.rs poll path);
  • present_start then sent frameLimitUs = rendered_us / shown while DLSS-G still showed 2, so Reflex paced two shown frames;
  • with SLOWEST_US = 100_000 that is 200 ms = 5.0 fps, and it self-sustains because every later poll agrees with the locked state.

The attached log fits arithmetically: sent 86,207 us -> measured 172.2 ms (2x), sent 100,000 us -> measured 200.0 ms (2x).

Change

Debounce the adopted shown count instead of changing the arithmetic:

  • a small pure Shown { current, candidate, votes } with polled(per); a differing poll only becomes a candidate, and the value is adopted on the second consecutive agreeing poll;
  • per is clamped to 1..=6, raw 0 treated as 1; a poll equal to current clears the candidate; reset_shown() restores 1 immediately when generation is turned off;
  • present_end feeds every poll (including the forced 1 on failure), present_start and the HUD read the adopted value; the rendered_us / shown arithmetic and the again send path are unchanged;
  • the diagnostic state line now prints shown and whether the poll was adopted.

A real generation stop is sustained, so it is still followed after ~20 presents (~1.5 s at 13 fps); a one-frame hitch can no longer double the limit.

Behaviour

  • 2,1,2 -> stays 2; 2,1,1 -> 1; 1,2,2 -> 2; 2,2,1,2,2 -> never changes; sustained 1 -> changes once.

Tests

  • cargo test -p leonida-gpu --lib fg::: 26 passed; full serial: 760 passed, 0 failed, 60 ignored.
  • Red/green: with immediate adoption restored, 4 of the 5 new tests fail (2,1,2, alternating, sustained-first-sample, zero); green with the debounce.
  • clippy, no-game-data and diff clean.

Interaction

  • #3387 changes only Follow's slow window; no semantic overlap.
  • #3389 rewrites the limit-send block around ReflexLimit::due(...), which still divides by shown; expect a textual overlap in present_start, resolved by feeding shown_now() into due(...) and keeping the retry. Complementary: debouncing stops a flapping count from producing spurious limit changes.

Not done

No game run. The one-run falsification from the issue still applies before merge: same save/settings with LEONIDA_FG_LIMIT_FPS=0 should remove the 200.0 ms flatline if this is the cause, and with this branch the flatline should not appear at all. A run with FG on and the fix is needed to confirm the latch is gone.

Item: D3D12-FG-SHOWN-DIVISOR-DEBOUNCE, claimed in #3312. Depends on: none.
AI-assisted: DeepSeek V4.1 Flash (OpenCode); the diagnosis and the state machine were reviewed separately.

## What/why #3260: the frame rate can lock to **exactly 5.0 fps** after a few minutes. The audit found it is the FG auto-limiter's shown-frame divisor, not load: - a single `dlssg_get_state` poll with `per != 1` forced `shown = 1` (old `fg.rs` poll path); - `present_start` then sent `frameLimitUs = rendered_us / shown` while DLSS-G still showed **2**, so Reflex paced two shown frames; - with `SLOWEST_US = 100_000` that is 200 ms = 5.0 fps, and it self-sustains because every later poll agrees with the locked state. The attached log fits arithmetically: sent 86,207 us -> measured 172.2 ms (2x), sent 100,000 us -> measured 200.0 ms (2x). ## Change Debounce the adopted shown count instead of changing the arithmetic: - a small pure `Shown { current, candidate, votes }` with `polled(per)`; a differing poll only becomes a candidate, and the value is adopted on the **second consecutive** agreeing poll; - `per` is clamped to `1..=6`, raw `0` treated as `1`; a poll equal to `current` clears the candidate; `reset_shown()` restores 1 immediately when generation is turned off; - `present_end` feeds every poll (including the forced `1` on failure), `present_start` and the HUD read the adopted value; the `rendered_us / shown` arithmetic and the `again` send path are unchanged; - the diagnostic state line now prints `shown` and whether the poll was adopted. A real generation stop is sustained, so it is still followed after ~20 presents (~1.5 s at 13 fps); a one-frame hitch can no longer double the limit. ## Behaviour - `2,1,2` -> stays 2; `2,1,1` -> 1; `1,2,2` -> 2; `2,2,1,2,2` -> never changes; sustained 1 -> changes once. ## Tests - `cargo test -p leonida-gpu --lib fg::`: **26 passed**; full serial: **760 passed, 0 failed, 60 ignored**. - Red/green: with immediate adoption restored, **4 of the 5 new tests fail** (`2,1,2`, alternating, sustained-first-sample, zero); green with the debounce. - clippy, no-game-data and diff clean. ## Interaction - #3387 changes only `Follow`'s slow window; no semantic overlap. - #3389 rewrites the limit-send block around `ReflexLimit::due(...)`, which still divides by `shown`; expect a textual overlap in `present_start`, resolved by feeding `shown_now()` into `due(...)` and keeping the retry. Complementary: debouncing stops a flapping count from producing spurious limit changes. ## Not done No game run. The one-run falsification from the issue still applies before merge: same save/settings with `LEONIDA_FG_LIMIT_FPS=0` should remove the 200.0 ms flatline if this is the cause, and with this branch the flatline should not appear at all. A run with FG on and the fix is needed to confirm the latch is gone. Item: D3D12-FG-SHOWN-DIVISOR-DEBOUNCE, claimed in #3312. Depends on: none. AI-assisted: DeepSeek V4.1 Flash (OpenCode); the diagnosis and the state machine were reviewed separately.
A single dlssgGetState poll that reports generation not working forced SHOWN to 1, so the followed frame limit sent rendered_us / 1 while DLSS-G still showed two frames and, at SLOWEST_US, pinned the screen at exactly 5 fps (#3260).

Adopt a new shown count only after two consecutive polls agree; keep the arithmetic and the again path unchanged. A sustained stop is still followed within about 20 presents (polls every 10).
Collaborator

Read the diff and the surrounding present_start/present_end. The state machine is total, the tests cover the alternating and sustained cases, and the lock order is the same in both paths (state then SHOWN), so no deadlock; shown_per_frame() is called once per frame (host.rs:13965, menu.rs:151, panel.rs:167), not per draw item.

Two questions before this goes in:

  1. Startup transient: current starts at 1 and adopts 2 only on the second agreeing poll, i.e. 10-20 presents after generation turns on. In that window present_start divides rendered_us by 1 while two frames are shown, so a 60 fps cap briefly aims at 60 rendered / 120 shown (about 0.3 s at 60 Hz). Is that trade-off intended, against adopting on the first poll?
  2. The same window on a sustained stop halves the cap for about 20 presents. The comment argues that the 5 fps pin of #3260 is worse; do you have the measurement that made two votes the right number rather than three?

One nit: shown_per_frame() now takes the mutex on every call where it was a relaxed load. That is once per frame today, but a comment on the trade-off would keep the next reader from "optimising" it back.

The logic itself looks right to me.

Read the diff and the surrounding `present_start`/`present_end`. The state machine is total, the tests cover the alternating and sustained cases, and the lock order is the same in both paths (`state` then `SHOWN`), so no deadlock; `shown_per_frame()` is called once per frame (`host.rs:13965`, `menu.rs:151`, `panel.rs:167`), not per draw item. Two questions before this goes in: 1. Startup transient: `current` starts at 1 and adopts 2 only on the second agreeing poll, i.e. 10-20 presents after generation turns on. In that window `present_start` divides `rendered_us` by 1 while two frames are shown, so a 60 fps cap briefly aims at 60 rendered / 120 shown (about 0.3 s at 60 Hz). Is that trade-off intended, against adopting on the first poll? 2. The same window on a sustained stop halves the cap for about 20 presents. The comment argues that the 5 fps pin of #3260 is worse; do you have the measurement that made two votes the right number rather than three? One nit: `shown_per_frame()` now takes the mutex on every call where it was a relaxed load. That is once per frame today, but a comment on the trade-off would keep the next reader from "optimising" it back. The logic itself looks right to me.
The debounce was symmetric, but only a fall is dangerous. A rise delayed by two polls left Follow under its limit, and with a truly alternating per a symmetric latch never adopted the higher count: from current = 1 it stayed pinned at the dangerous underestimate that is the #3260 5 fps path, where main would have adopted. Adopt per > current at once; keep the two-poll agreement for a fall so a lone bogus poll still cannot pin the limit at rendered_us / 1.

The zero-clamp test's comment said present_end passes 0 for not working when it passes 1; corrected. shown_now() is unchanged and still feeds the followed limit's shown read.
Collaborator

Ran the gate on this branch (head e581ff2b2) with the game: diag build diagnostics,dlss,mimalloc, RTX 5070, driver 32.0.16.2101, 1440p, default play switches (frame generation on, LEONIDA_FG_LIMIT_FPS=auto).

PASS: 302.7 s in the world, no device loss, no panic, exit 0 (clean), median 20.6 fps, p5 11.9, longest present gap 1.64 s, cache saved.

The debounce does what it says in the log:

  • index=6000/6600/7200: status 0, presented 2, per 2, shown 2;
  • index=7800: presented 1, per 1, shown still 2, adopted=false — the first poll alone does not move the count;
  • index=8400: shown 1.

The followed limit follows it: rendered_fps=22.8 shown_fps=45.7 while generation worked, 11.9/11.9 after it stopped.

One detail from the run: generation dropped from 2 to 1 around index=7800 with status=0 still, so it was not a failing poll but Streamline genuinely presenting one frame (the gate's window is not focused, which is the case the comment in present_end already names). That is exactly the flapping this debounce has to absorb, and it did.

My question 2 stays open in the only sense that matters: 2 votes versus 3 would need an A/B build. If you want that number, say how many votes and I will run both back to back on this machine and post the two summaries.

Ran the gate on this branch (head `e581ff2b2`) with the game: `diag` build `diagnostics,dlss,mimalloc`, RTX 5070, driver 32.0.16.2101, 1440p, default play switches (frame generation on, `LEONIDA_FG_LIMIT_FPS=auto`). **PASS**: 302.7 s in the world, no device loss, no panic, exit 0 (clean), median 20.6 fps, p5 11.9, longest present gap 1.64 s, cache saved. The debounce does what it says in the log: - `index=6000/6600/7200`: status 0, presented 2, per 2, shown 2; - `index=7800`: presented 1, per 1, **shown still 2**, `adopted=false` — the first poll alone does not move the count; - `index=8400`: shown 1. The followed limit follows it: `rendered_fps=22.8 shown_fps=45.7` while generation worked, `11.9/11.9` after it stopped. One detail from the run: generation dropped from 2 to 1 around `index=7800` with `status=0` still, so it was not a failing poll but Streamline genuinely presenting one frame (the gate's window is not focused, which is the case the comment in `present_end` already names). That is exactly the flapping this debounce has to absorb, and it did. My question 2 stays open in the only sense that matters: 2 votes versus 3 would need an A/B build. If you want that number, say how many votes and I will run both back to back on this machine and post the two summaries.
KelboLive force-pushed d3d12/fg-shown-debounce from 354c8fc0d3 to c035e97e3d 2026-12-04 15:10:37 +00:00 Compare
Author
First-time contributor

Rebased onto main 44c480a5f (head c035e97e3). No conflicts. Re-ran: 769 passed, 0 failed, 60 ignored. The one known conflict with #3389 is still the single fg.rs hunk; this branch is unchanged in that area.

Rebased onto `main` `44c480a5f` (head `c035e97e3`). No conflicts. Re-ran: 769 passed, 0 failed, 60 ignored. The one known conflict with #3389 is still the single `fg.rs` hunk; this branch is unchanged in that area.
Author
First-time contributor

Thanks for the read and the run.

1. The startup transient is intended. The harmful direction is only the fall: 2 shown / divisor 1 is the #3260 200 ms frame (the 100 ms floor divided by one). The rise from 1 to 2 while two frames are actually shown just means the cap aims at the refresh for ~10-20 presents instead of half of it - it cannot produce a long frame. Adopting the first poll would reintroduce exactly the case your run caught at index=7800: one poll saying 1 while Streamline is not generating, and a 200 ms frame on every flap. So: a benign 0.3 s under-limit at startup for no 200 ms hitches is the trade.

2. Two votes, not three. The poll cadence is one per present, and the failure case is a single stale reading. Two agreeing polls is the smallest number that rejects one stale poll while keeping the fall inside two presents; a third vote would add another present dividing by 1 while two frames are shown - the direction we can least afford. That is a code-derived choice, not a measured optimum, so if your A/B shows a sustained 1 exposed for more than two polls (not a one-poll flap), that is the evidence that would make it 3, and I would take it. Otherwise I would not spend a run on it.

The nit is fair - shown_per_frame() is once per frame today and the lock is uncontended. I will add the comment so nobody "optimises" it back to a relaxed load that cannot carry the two fields.

Thanks for the read and the run. **1. The startup transient is intended.** The harmful direction is only the fall: 2 shown / divisor 1 is the #3260 `200 ms` frame (the 100 ms floor divided by one). The rise from 1 to 2 while two frames are actually shown just means the cap aims at the refresh for ~10-20 presents instead of half of it - it cannot produce a long frame. Adopting the first poll would reintroduce exactly the case your run caught at `index=7800`: one poll saying 1 while Streamline is not generating, and a 200 ms frame on every flap. So: a benign 0.3 s under-limit at startup for no 200 ms hitches is the trade. **2. Two votes, not three.** The poll cadence is one per present, and the failure case is a single stale reading. Two agreeing polls is the smallest number that rejects one stale poll while keeping the fall inside two presents; a third vote would add another present dividing by 1 while two frames are shown - the direction we can least afford. That is a code-derived choice, not a measured optimum, so if your A/B shows a sustained 1 exposed for more than two polls (not a one-poll flap), that is the evidence that would make it 3, and I would take it. Otherwise I would not spend a run on it. The nit is fair - `shown_per_frame()` is once per frame today and the lock is uncontended. I will add the comment so nobody "optimises" it back to a relaxed load that cannot carry the two fields.
KelboLive force-pushed d3d12/fg-shown-debounce from c035e97e3d to aa0f5c7f05 2026-12-04 15:57:09 +00:00 Compare
Author
First-time contributor

Replayed onto main bba262b94 (head aa0f5c7f0). No conflicts. Re-ran: 788 passed, 0 failed, 60 ignored.

Replayed onto `main` `bba262b94` (head `aa0f5c7f0`). No conflicts. Re-ran: 788 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from aa0f5c7f05 to a6d90ba397 2026-12-04 16:15:59 +00:00 Compare
Author
First-time contributor

Replayed onto main e1be896b2 (head a6d90ba39). No conflicts. 788 passed, 0 failed, 60 ignored.

Replayed onto `main` `e1be896b2` (head `a6d90ba39`). No conflicts. 788 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from a6d90ba397 to 4087afa09a 2026-12-04 16:32:36 +00:00 Compare
Author
First-time contributor

Replayed onto main 00e245be5 (head 4087afa09). No conflicts. 798 passed, 0 failed, 60 ignored.

Replayed onto `main` `00e245be5` (head `4087afa09`). No conflicts. 798 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from 4087afa09a to 076714f11a 2026-12-04 17:15:16 +00:00 Compare
Author
First-time contributor

Replayed onto main f22b9fa64 (head 076714f11). No conflicts. 801 passed, 0 failed, 60 ignored.

Replayed onto `main` `f22b9fa64` (head `076714f11`). No conflicts. 801 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from 076714f11a to dfe69d85e4 2026-12-04 18:30:25 +00:00 Compare
Author
First-time contributor

Replayed onto main 6491bc1a1 (head dfe69d85e). No conflicts. 801 passed, 0 failed, 60 ignored.

Replayed onto `main` `6491bc1a1` (head `dfe69d85e`). No conflicts. 801 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from dfe69d85e4 to 8f9d3f609b 2026-12-04 18:44:17 +00:00 Compare
Author
First-time contributor

Replayed onto main 6b0498d37 (head 8f9d3f609). No conflicts. 801 passed, 0 failed, 60 ignored.

Replayed onto `main` `6b0498d37` (head `8f9d3f609`). No conflicts. 801 passed, 0 failed, 60 ignored.
First-time contributor

RUN — 8f9d3f609 (rebased on main 6b0498d37), NVIDIA RTX 5070 12 GB, Windows 11.

Static & unit checks

  • cargo test -p leonida-gpu --lib --locked fg::: 32/32 PASS (0 failed)
  • cargo clippy -p leonida-gpu --all-targets --locked -- -D warnings: 0 warnings in fg.rs (only pre-existing Rust deprecation lints in untouched files)
  • python -I scripts/check_no_game_data.py: PASS

Live 335 s run with DLSS Frame Gen (-SeriesS -FrameGen 2 -NoPrecompile -AutoMenu)

  • World telemetry (38 samples with >= 1000 draws, 180 s in world, max_draws = 5,483): 3000-4999 draws (31 samples) median 21.8 fps (23.7-25.0 fps steady-state in the final 40 s after the auto-limiter finished climbing from the initial 10.0 fps world-entry stall; overall world median 18.2 fps, 54.9 ms).
  • Runtime health: 0 ERROR lines prior to clean window close, device-lost.txt = false, peak video memory 8,373 / 10,941 MB.
  • leonida_gpu::fg telemetry:
    • Streamline sl.dlss_g initialized cleanly (frames=2, max=5, status=0, tagged=true on world entry, result=sl::Result::eOk (0)).
    • The new diagnostic state fields (per, shown, adopted) logged cleanly on every poll interval, and the auto-limiter never latched to 5.0 fps (steady-state LEONIDA_FG_LIMIT_FPS=auto settled at 23.7-26.8 fps).
    • Note: because this was an unattended background run without foreground window focus, DLSS-G stayed at presented=1, per=1, shown=1, adopted=false throughout (so the live run verified the Streamline/Reflex/polling integration and steady-state path, while the 2 -> 1 fall-debounce and immediate-rise transitions are verified by the 32/32 unit tests).

AI-assisted.

RUN — `8f9d3f609` (rebased on `main` `6b0498d37`), `NVIDIA RTX 5070 12 GB`, `Windows 11`. ### Static & unit checks - `cargo test -p leonida-gpu --lib --locked fg::`: `32/32` PASS (`0` failed) - `cargo clippy -p leonida-gpu --all-targets --locked -- -D warnings`: `0` warnings in `fg.rs` (only pre-existing Rust deprecation lints in untouched files) - `python -I scripts/check_no_game_data.py`: PASS ### Live 335 s run with DLSS Frame Gen (`-SeriesS -FrameGen 2 -NoPrecompile -AutoMenu`) - World telemetry (`38` samples with `>= 1000` draws, `180 s` in world, `max_draws = 5,483`): `3000-4999` draws (`31` samples) median `21.8 fps` (`23.7-25.0 fps` steady-state in the final 40 s after the auto-limiter finished climbing from the initial `10.0 fps` world-entry stall; overall world median `18.2 fps`, `54.9 ms`). - Runtime health: `0` `ERROR` lines prior to clean window close, `device-lost.txt = false`, peak video memory `8,373 / 10,941 MB`. - `leonida_gpu::fg` telemetry: - Streamline `sl.dlss_g` initialized cleanly (`frames=2`, `max=5`, `status=0`, `tagged=true` on world entry, `result=sl::Result::eOk (0)`). - The new diagnostic `state` fields (`per`, `shown`, `adopted`) logged cleanly on every poll interval, and the auto-limiter never latched to `5.0 fps` (steady-state `LEONIDA_FG_LIMIT_FPS=auto` settled at `23.7-26.8 fps`). - Note: because this was an unattended background run without foreground window focus, DLSS-G stayed at `presented=1, per=1, shown=1, adopted=false` throughout (so the live run verified the Streamline/Reflex/polling integration and steady-state path, while the `2 -> 1` fall-debounce and immediate-rise transitions are verified by the `32/32` unit tests). AI-assisted.
KelboLive force-pushed d3d12/fg-shown-debounce from 8f9d3f609b to f278abcae7 2026-12-04 20:53:00 +00:00 Compare
Author
First-time contributor

Replayed onto main 0036195a7 (head f278abcae). No conflicts. 801 passed, 0 failed, 60 ignored.

Replayed onto `main` `0036195a7` (head `f278abcae`). No conflicts. 801 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from f278abcae7 to 7f4c458338 2026-12-04 23:20:24 +00:00 Compare
Author
First-time contributor

Replayed onto main f4b0404e9 (head 7f4c45833). No conflicts. 801 passed, 0 failed, 60 ignored.

Replayed onto `main` `f4b0404e9` (head `7f4c45833`). No conflicts. 801 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from 7f4c458338 to 0ae4a1ae80 2026-12-05 05:34:49 +00:00 Compare
Author
First-time contributor

Replayed onto main 93b9d90a2 (head 0ae4a1ae8). No conflicts. 801 passed, 0 failed, 60 ignored.

Replayed onto `main` `93b9d90a2` (head `0ae4a1ae8`). No conflicts. 801 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from 0ae4a1ae80 to 317ba51bf3 2026-12-05 08:44:26 +00:00 Compare
Author
First-time contributor

Replayed onto main c5355dd25 (head 317ba51bf). No conflicts. 802 passed, 0 failed, 60 ignored.

Replayed onto `main` `c5355dd25` (head `317ba51bf`). No conflicts. 802 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from 317ba51bf3 to 4bf57e1532 2026-12-05 08:53:18 +00:00 Compare
Author
First-time contributor

Replayed onto main e1b0248c1 (head 4bf57e153). No conflicts. 802 passed, 0 failed, 60 ignored.

Replayed onto `main` `e1b0248c1` (head `4bf57e153`). No conflicts. 802 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fg-shown-debounce from 4bf57e1532 to 2a23246f00 2026-12-05 09:59:43 +00:00 Compare
Author
First-time contributor

Replayed onto main d99bb4d7a (head 2a23246f0), which adds #3295's frame-generation-limit test in the same fg.rs. No conflict; 805 passed, 0 failed, 60 ignored.

Replayed onto `main` `d99bb4d7a` (head `2a23246f0`), which adds #3295's frame-generation-limit test in the same `fg.rs`. No conflict; 805 passed, 0 failed, 60 ignored.
Sign in to join this conversation.
No description provided.