Skip to content

leonida-gpu: the driver's background shader work stays out of the world (D3D12-DRIVER-BACKGROUND-PLAY) - #2847

Open
TheGeogeo wants to merge 6 commits into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-driver-background-commit
Open

TheGeogeo wants to merge 6 commits into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-driver-background-commit

Conversation

@TheGeogeo

@TheGeogeo TheGeogeo commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-DRIVER-BACKGROUND-PLAY (#2570 (comment))

What and why

After pipelines are made or loaded, the driver compiles their shaders again on four threads of its own in this process ("D3D Background Thread"s), and keeps the result in its own cache. Until that is done for the translations in use, those threads take one or two processors. That lasts 9 to 15 minutes of play after each change of translations, and comes back after a session that ended first. On a processor the title already fills, this costs the frame while the card's own work stays the same.

LEONIDA_D3D12_BACKGROUND=world switches that work off while the world is drawn (500 draws a frame or more, as the game gate counts it) and allows it in loading screens and menus. The work still finishes over sessions, without taking the processors play needs.

  • d3d12/crates/leonida-gpu/src/driver_background.rs: one place for the three ways to set the mode. 0 sets it on the title's device when it is made (the call that was in host.rs); world and LEONIDA_AB start a thread that counts the frame line's draws (draw::drawn, the game gate's figure) every 0.5 s, with hysteresis (2 counts to enter the world, 10 to leave it). Unset or 1 start nothing: the driver is left as it is.
  • NVIDIA launcher: world by default where Windows is in Developer mode (below). The runtime's default is unchanged.
  • queue::presented() is available outside diagnostics builds.
  • Docs: findings.md section, switches.md regenerated (it also picks up LEONIDA_PROMPTS_PAD, which main added without regenerating the page).

Windows switches this work off only in Developer mode. Microsoft's page of D3D12_BACKGROUND_PROCESSING_MODE says of DISABLE_BACKGROUND_WORK: "Available only in Developer mode." The PC measured below has it on. Elsewhere the call is refused, so 0 and world change nothing. Since the review, the thread says so once and ends: a WARN line the driver's background shader work stays as the driver likes, with the mode and the call's code, naming Developer mode and where to turn it on. Before, a refused call was asked again every half second without a word, which is what an RTX 4060 without Developer mode showed: allowed=true at the start, no allowed=false in 14 minutes of world. The precondition is written in the module, findings.md, switches.md and the launcher's comment.

The launcher sets world only where Developer mode is on (HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock, AllowDevelopmentWithoutDevLicense = 1, the switch of Settings, System, For developers). Elsewhere it leaves the driver as it is, so players without Developer mode see no change and no warning; -More @{LEONIDA_D3D12_BACKGROUND='world'} still asks for it, and then the log says the driver refused.

world lets the work run only in loading screens and menus. Those are short: after a 900 s world session the defaults' threads still ran 14 minutes, so for a player who seldom sits in menus the work may never finish. On this card that costs nothing measurable (the card's work with shaders the driver never got to: 29.5 ms a frame, against 30.4 ms with its finished ones).

Measured (RTX 3080 10 GB, driver 32.0.16.2140, Windows 11 with Developer mode on, Series S settings, standing, gate runs with the driver mod and a per-thread processor sampler)

  • The slow phase. While the driver's threads ran: 24.4-25.2 fps, card 77 % busy, 8.2 ms a frame idle behind the submitting thread. Once they stopped: 26.4-27.2 fps, 83 %, 5.7 ms. The card's work a frame was about 31 ms in both.

  • A/B within one run. LEONIDA_AB=20,LEONIDA_D3D12_BACKGROUND: 38.1 ms median frame with the work off, 39.6 ms with it allowed (3-8 ms in the phases where the threads burst to 160-235 %). The card's work was the same.

  • Cold shaders, same data folder. LEONIDA_LEGACY_PLAIN=1 gave shader code the driver had never seen. world first, then the defaults:

    fps card's work a frame
    world 26.1 29.5 ms
    defaults, for the 14 minutes their threads ran 24.9 30.5 ms
    defaults, after their work finished 27.0 30.4 ms

    Shaders the driver never got to cost this card nothing measurable. The remaining gap of the world run (26.1 against 27.0) is on the processor side (7.2 against 5.7 ms idle behind the submitting thread). That run was the folder's first, so its pipelines compiled in the game during its first minutes. Its gate verdict was FAIL (present_gap) for that reason, not because of the switch.

  • Tried and dropped:

    • asking the driver to finish early (D3D12_MEASUREMENTS_ACTION_COMMIT_RESULTS_HIGH_PRIORITY): honoured within 5-9 s, but the threads went on at the same rate;
    • the threads at idle priority: they still took about 0.9 processor, 39.3 ms median frame.

Tested

On Windows 11, from d3d12\ with the pinned toolchain, rebased on main 8422def:

  • cargo test -p leonida-gpu --locked --lib: 676 passed, 0 failed, 59 ignored. In 2 of 4 full runs one test failed, texture::alias_cast_tests::committed_and_placed_r32_uint_textures_cast_to_r9g9b9e5_under_legacy_barriers (green alone 3 of 3); on plain main 8422def the same test failed in 1 of 4 full runs, so it is not this change.
  • cargo test -p leonida-gpu --locked --features diagnostics: 747 passed, 0 failed, 59 ignored (on main 393901a before the last rebase; main itself: 739).
  • driver_background::tests, 8 tests: the variable's values; the decision with and without an A/B phase; the world's hysteresis both ways; each_count_judges_only_the_frames_since_the_last (a long stretch of world then a menu); a count without frames; the_world_begins_where_the_game_gate_says (reads WORLD_DRAWS from tools/game_gate.py and checks one draw a frame below it is not the world); a_mode_is_asked_for_only_when_it_changes; a_refused_mode_is_reported_once_and_not_asked_again. On main they do not build (the module is new).
  • Each fix is caught by a test: with the refusal not remembered, with WORLD_DRAWS at 50, and with the counter's mark not moved on, at least one test fails each time.
  • cargo clippy -p leonida-gpu --all-targets --locked: no warning in the changed files (the one warning is in host.rs code from main).
  • rustfmt --config-path d3d12/rustfmt.toml --edition 2024 --check on the module; python -I tools/d3d12_switches.py --check d3d12/docs/switches.md; python -I tests/test_win_launchers.py; python -I scripts/check_no_game_data.py on the changed files: OK. The launcher's Developer mode read checked under Set-StrictMode -Version Latest with the key present and missing.
  • In game, after the rebase and the review: a 300 s gate run (driver mod, Series S settings) of this code (exe of 6b98884; the commit after it changes only the launcher's registry read and docs): PASS, 302 s in the world, median 26.6 fps; the log says allowed=true at start and allowed=false 78 s later, at world entry.

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.2140, Windows 11 (Developer mode on). The refused path (Developer mode off) is covered by the unit test, not by a run here.

AI-assisted: yes. Claude Code with Claude Fable 5.2 and Claude Opus 5.5; the diff was reviewed by a separate Claude Opus 5.5 agent before this push.

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md (no translator change here).

@lixonik

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

RUN

Verdict: same on 16C/32T (Ryzen 9 5950X) -- no statistically clear median frame-time difference in matched alternating 20 s phases, with zero errors, zero device-lost events, and clean cold PSO precompile.

Setup:

  • PR #2847 head 3a9afee52 merged onto main ad7c62edb (features diagnostics,dlss,mimalloc)
  • LEONIDA_AB=20,LEONIDA_D3D12_BACKGROUND (Series S, -NoFrameGen)
  • GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver 621.40, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T)

Unit test:

  • cargo test --locked -p leonida-gpu --lib driver_background passed.

In-run A/B (tools/d3d12_ab_compare.py phases):

  • 5 valid pairs (8 dropped for draw-count jumps): median frame-time delta -0.220 +- 0.220 ms (no clear difference)
  • Overall run: 27.6 fps / 36.2 ms (gpu_wait 0.0 ms, max 4,214 draws, 475.6 s cold precompile on fresh translations, 30 world samples, 0 errors, 0 device lost)

World phases (draws >= 1000):

Phase LEONIDA_D3D12_BACKGROUND Draws Mean (ms) Median (ms) p90 (ms)
20 OFF 1,652 31.6 31.0 35.1
21 ON 2,025 37.3 34.5 41.5
22 OFF 2,621 31.4 29.4 36.7
23 ON 4,110 39.0 37.7 44.9
24 OFF 4,166 38.7 37.6 40.6
25 ON 4,131 37.0 36.5 39.9

With 32 hardware threads on Ryzen 9 5950X, there is ample CPU headroom for driver background threads compared to 6C/8C CPUs, so same median on 16C/32T with zero regressions is expected and confirms stability on RTX 5070 + driver 621.40.

AI-assisted: Antigravity

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

RUN (RTX 4060 8 GB, NVIDIA 610.88, Windows 11 26300, -SeriesS -FrameGen 2, about 860 s in the world, 0 device lost): head 3a9afee52 with six others on main 21e6bbf4b.

The switch never took effect here. LEONIDA_D3D12_BACKGROUND=world was set (launcher default of this pull request), the world drew 3,500 to 4,700 draws a frame by the five-second lines, and the log has one line only, at the start: allowed=true. There is no allowed=false in 14 minutes of world. In start(), a set_mode(false) that fails is tried again every 500 ms without a word, so that is my guess (not checked): SetBackgroundProcessingMode refuses DISABLE_BACKGROUND_WORK here. Could the thread log the HRESULT once, and does the mode need something on the machine (Developer Mode, for example)? Developer Mode is not set in the registry here.

No harm seen: no loss, no panic. But I can not confirm the effect.

AI-assisted (Claude Code).

@merzleckeier

Copy link
Copy Markdown
Contributor

I checked the actual HRESULT for the mode changes in 3a9afee5 + cd0c52fcf. On RTX 4070 SUPER (620.73.04) and AMD Raphael (RADV 26.3.1), Wine 11.22 and VKD3D-Proton exposed ID3D12Device6. SetBackgroundProcessingMode(ALLOWED / DISABLE_BACKGROUND_WORK / ALLOWED, KEEP_ALL, null, null) returned E_NOTIMPL (0x80004001) for all three calls on each GPU. The four policy tests also passed.

For the native Windows question, Microsoft documents that DISABLE_BACKGROUND_WORK is available only in Developer Mode. That fits the reported native pattern of an initial allowed=true without a later successful disable. The native HRESULT is still unmeasured here. A one-time log of the attempted mode and HRESULT would distinguish that case from a missed threshold. Microsoft D3D12_BACKGROUND_PROCESSING_MODE documentation

MEASURED with offscreen synthetic devices under Wine. AI-assisted (Codex).

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The draw-counter and docs side is consistent: WORLD_DRAWS = 500.0 matches tools/game_gate.py:22 and its two-line rule, ENTER/LEAVE 2/10 × 0.5 s matches the text, the frame log stays exact (list.rs:2541-2551, no new atomic on count_draw), and 604 tests pass (the 2 diagnostics failures are the known parallel flakes, green alone).

Blocking: the mechanism is restricted to Windows Developer Mode, and the failure is silent. D3D12_BACKGROUND_PROCESSING_MODE_DISABLE_BACKGROUND_WORK is documented as requiring Developer Mode. set_mode discards the HRESULT (driver_background.rs:153-167), the caller logs only on success (:139-147) and retries every 500 ms. On a default Windows install that produces exactly the allowed=true then silence MoonEyes reported, and matches merzleckeier's E_NOTIMPL — the PR's successful allowed=false run implies a machine where it was permitted. Neither findings.md nor switches.md mentions the precondition, and play-nvidia.ps1:278 now defaults the launcher to world, so for most users the headline feature is a silent no-op that looks like it worked.

Please:

  1. log the first HRESULT once (with the mode and Developer Mode named in the message), instead of retrying into silence;
  2. document the Developer Mode precondition in both findings.md and switches.md;
  3. hold the world launcher default until a run on a non-Developer-Mode install confirms the mode actually changes — or make the default conditional on a successful probe.

Also, WORLD_DRAWS = 500.0 mirrors the gate only by comment: mutating it to 50.0 leaves all four driver_background tests green, so nothing pins the "as the game gate counts it" claim. A test that imports the number from game_gate.py (or asserts the same value) would.

Minor: driver_background.rs:11 says "~9 minutes" while findings/body say "9 to 15", and findings.md:1731 says a repeat run "has none" then that the threads stop about 45 s after the world comes up — reword that pair.

Merge is clean against current main and the merged tree still passes d3d12_switches.py --check; #2888 and #2743 conflict only in findings.md (and #2743 also switches.md).

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2847 — Linux, Fedora 45, Rust 1.98.1 (the pinned toolchain), Wine 11.21, no GPU device, no game files

Verdict: PASS. The switch's rules are readable and the counting they need is there, the new tests pass, and nothing that worked before is broken. The measurements the pull request quotes are a claim about an NVIDIA card and the game, and I could not check them.

Head 3a9afee5, base the current main 85ec1d33, merge base 501b3e6c, 8 files, +310/−7: d3d12/crates/leonida-gpu/src/driver_background.rs (new), d3d12/crates/leonida-gpu/src/list.rs, d3d12/crates/leonida-gpu/src/host.rs, d3d12/crates/leonida-gpu/src/queue.rs, d3d12/crates/leonida-gpu/src/lib.rs, d3d12/docs/findings.md, d3d12/docs/switches.md, d3d12/scripts/play-nvidia.ps1.

1. What it changes

After the pipelines are made or loaded, the driver compiles their shaders again on threads of its own in this process, and until it has done so those threads take one or two processors — on a processor the title already fills, that costs the frame. The new module lets that work be switched off, or paused while the world is drawn:

  • LEONIDA_D3D12_BACKGROUND=0 — never (the mode is set when the device is made);
  • LEONIDA_D3D12_BACKGROUND=world — not while the world is drawn, allowed in loading screens and menus;
  • unset or 1 — the driver's own behaviour, as before (nothing is started);
  • LEONIDA_AB may name the switch, and its phase wins over the policy.

2. What I checked

The policy mapping.

fn policy(variable: Option<&str>) -> Policy {
    match variable.map(str::trim) {
        Some("0") => Policy::Never,
        Some("world") => Policy::OutsideWorld,
        _ => Policy::Always,
    }
}

unset and 1 both give Always, and start() returns early for Always unless LEONIDA_AB names the switch — so the default path starts no thread at all and the driver is left as it was ✓, which is what the documentation says.

The world is recognised with hysteresis.

const ENTER: u32 = 2;   // counts in a row of the world before it is switched off
const LEAVE: u32 = 10;  // counts in a row of no world before it is allowed again

World::count compares draws / frames with WORLD_DRAWS = 500, and a count with no frame (frames == 0) changes nothing ✓ — so a period with no rendering cannot flip the state. The asymmetry (two counts in, ten out) is deliberate and is in the comment: the work is switched off quickly when the world is drawn and allowed again slowly, so a loading screen between two world stretches does not start the work again and stop it again.

The phase wins over the policy.

fn allowed(phase: Option<bool>, policy: Policy, world: bool) -> bool {
    phase.unwrap_or(match policy { Policy::Always => true, Policy::Never => false, Policy::OutsideWorld => !world })
}

phase is LEONIDA_AB's phase for the switch; when the switch is not named in LEONIDA_AB, phase() is None and the policy decides ✓.

The counting it needs is there. list.rs's take_counts() used to reset the draw counter; it now keeps its own mark and returns the difference, and a new draws_so_far() gives the total since the start — which is what the world test needs (draws / frames) ✓.

3. What the tests do here

leonida-gpu does not build on Linux (upstream: "The crates are Windows-only today … Linux follows #2577"), so I cross-compiled it for x86_64-pc-windows-gnu and ran the tests under Wine 11.21.

The pull request changes no test, so the red side cannot be built (there is nothing to take); the base and the head were compared instead:

the merge base 654 tests
the pull request's head 659 tests
the new tests, only on the head 5 — all pass
failing on both sides 3 — the watch tests that need Microsoft's VirtualAlloc/VirtualQuery behaviour, which Wine does not have

One further test, watch::tests::a_write_is_reported_at_every_checked_view, failed in one full-suite run on the head and passed on the base. It is the same family of parallel-run flakes as the three above: run alone it passes 5 times out of 5 on the head, and the same test fails on the base at a comparable rate when the whole suite runs in parallel.

4. What I did not check

  • The measurements. "24.4-25.2 frames a second with the work against 26.4-27.2 without", the 8.2 ms against 5.7 ms idle behind the submitting thread, the D3D12_MEASUREMENTS_ACTION_COMMIT_RESULTS_HIGH_PRIORITY experiment — these are from an RTX 3080 with the game, and neither the card nor the game is here.
  • That the mode reaches the driver's background threads. The 0 path sets D3D12_BACKGROUND_PROCESSING_MODE_DISABLE_BACKGROUND_WORK on the device when it is made (the pre-existing code), and the world path sets it again from the new thread; whether that is what the driver's threads read is a driver property.
  • The world test's own threshold. WORLD_DRAWS = 500 is the game gate's "the world is drawn" measure (tools/game_gate.py); I did not check that the draw counts the module reads are the same ones the gate uses.
  • Windows and MSVC. Everything above is Linux with Wine 11.21 (staging) and no GPU device.

AI-assisted: MiniMax M3.1 Flash.

…d (LEONIDA_D3D12_BACKGROUND=world)

After pipelines are made or loaded the driver compiles their shaders again on
its own threads in this process until it has done so for the translations in
use. Measured on an RTX 3080: those threads take one or two processors for
9 to 15 minutes of play after each change of translations, 2 to 3 ms a frame
with the card's work unchanged; switched in turns of 20 s in one run, 39.6 ms
median frame with the work allowed, 38.1 ms with it off. Asking the driver to
finish early, or giving its threads the idle priority, did not help.

LEONIDA_D3D12_BACKGROUND=world switches that work off while the world is
drawn (500 draws a frame or more, as the game gate counts it) and allows it in
loading screens and menus, so that it still finishes over sessions. Unset or 1
and 0 are as before; LEONIDA_AB can switch it in a run.

The draw counter becomes a running total read beside the frame line's counts
(take_counts keeps its own mark), and the presented-frame count is available
outside diagnostics builds.
… world by default

LEONIDA_D3D12_BACKGROUND=world in play-nvidia.ps1, with what was measured on
an RTX 3080: with shaders the driver had never seen, 26.1 frames a second
against 24.9 for the 14 minutes its threads ran with the defaults, the card's
work a frame the same (29.5 and 30.5 ms). The findings and the switch list say
so; the old comment that the card's cost was unknown is answered.
…in (review)

Windows switches the driver's background work off only in Developer mode: the call is refused elsewhere. The thread logs the refusal once with the call's code and ends, where it asked again every half second without a word. The precondition is in the module, findings.md, switches.md and the launcher's comment. A test reads WORLD_DRAWS from tools/game_gate.py.
…gate counts it (review)

`LEONIDA_D3D12_BACKGROUND=0` now goes through driver_background::start as well: the mode is
set on the title's device when it is made, and a refusal is written once with its code, as
for `world` and `LEONIDA_AB`. host.rs keeps one call. The world is judged on the draws the
frame line counts (draw::drawn, the game gate's figure), so list.rs is back as it was; the
counter keeps its own marks, tested over a long world stretch followed by a menu. The thread
gets the device from start(), so the "no device yet" case is gone. The NVIDIA launcher sets
`world` only where Windows is in Developer mode. findings.md no longer says the work finishes
over a few sessions, and says the refusal line is unit-tested, not yet seen in a run.
…wording (review)

The launcher reads AllowDevelopmentWithoutDevLicense with Registry.GetValue, which gives 0 for a
missing key or value instead of throwing under Set-StrictMode. findings.md says asking once is
unit-tested and the refusal line not yet seen in a run, and that world is the launcher's default
where Developer mode is on.
@TheGeogeo
TheGeogeo force-pushed the d3d12-driver-background-commit branch from 3a9afee to c1bf55d Compare December 2, 2026 03:42
@TheGeogeo

Copy link
Copy Markdown
Contributor Author

Thanks @KelboLive, @MoonEyes, @merzleckeier. Rebased on main 8422def and changed after your review (head c1bf55d):

  • Developer mode, and no more silent retries. Microsoft's page of D3D12_BACKGROUND_PROCESSING_MODE says of DISABLE_BACKGROUND_WORK: "Available only in Developer mode." The PC I measured on has it on; the RTX 4060 run had it off, and the first version wrote only calls that succeeded and asked again every half second, which is why it showed allowed=true and nothing more. Now a refused call is written once at WARN with its code (the driver's background shader work stays as the driver likes, naming Developer mode and where to turn it on), and the thread stops asking. The precondition is in the module, findings.md, switches.md and the launcher.
  • 0 takes the same path. The call that was in host.rs moved into driver_background::start, which host.rs calls once: 0 is still set when the device is made, before anything is made with it, and a refusal is written the same way. One parser for the variable.
  • The launcher's default is conditional. play-nvidia.ps1 sets world only where Developer mode is on (AppModelUnlock\AllowDevelopmentWithoutDevLicense = 1); elsewhere the driver is left as it is, so players without Developer mode see no change and no warning. -More still applies on top.
  • Counted as the gate counts it. The world is now judged on the frame line's draws (draw::drawn, the gate's figure), so the list.rs change is gone. A test reads WORLD_DRAWS from tools/game_gate.py and checks one draw a frame below it is not the world; with the constant at 50 it fails. Another test covers a long world stretch followed by a menu.
  • Wording: "9 to 15 minutes" everywhere; the "has none" / "45 s" pair is one sentence now; the claim that the work "finishes over a few sessions" is gone. Loading screens are short: after a 900 s world session the defaults' threads still ran 14 minutes, so for some players it may never finish. On the 3080 that costs the card nothing measurable (29.5 ms of card work a frame with shaders the driver never got to, 30.4 ms with its finished ones).

Tested on the RTX 3080 (driver 32.0.16.2140, Windows 11, Developer mode on): crate tests and diagnostics tests, three mutations each caught by a test, and a 300 s gate run of this code: PASS, allowed=true at start, allowed=false 78 s later at world entry. Not tested: the refusal on a PC without Developer mode. @MoonEyes, if you run it again on the 4060 with -More @{LEONIDA_D3D12_BACKGROUND='world'}, the log should have one WARN line with the code at world entry and nothing after it.

AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5; a separate Claude Opus 5.5 agent reviewed the diff before this push).

The launcher tests run under PowerShell on Linux too, where Microsoft.Win32.Registry throws
("Registry is not supported on this platform"). The read is now behind $IsWindows.
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants