Skip to content

d3d12: lists' scratch stays in the PC's memory by default for now (D3D12-SCRATCH-CARD-OFF-BY-DEFAULT) - #2819

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-scratch-card-off
Dec 1, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-scratch-card-off

Conversation

@TheGeogeo

@TheGeogeo TheGeogeo commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-SCRATCH-CARD-OFF-BY-DEFAULT (#2570 (comment)), part of enlighted's D3D12-REAPER-BOUNDED-QUEUE (#2570 (comment)), agreed with them.

What and why

Lists' scratch memory goes into the card's own memory, where the CPU maps it, on cards whose budget is 9 GiB or more at the first ask and that have a GPU upload heap (resizable BAR). This came with 7b907de. On those cards it churns chunks:

  • SPARE_CARD keeps 256 MiB in all and gives back an exact size only (Spare::take/give).
  • Once 4 MiB chunks fill it, the chunks of other sizes are let go in the list resets, on the reaper thread. They are then made again on the recording threads, behind a blocking MakeResident.
  • The reaper falls behind, and the lists and memory it holds pile up. That ends in a device loss, or worse.

Measured by enlighted on an RTX 3090 24 GB with a resizable BAR, standing 600 s, with LEONIDA_AB on this switch over 23 pairs:

Card scratch PC scratch
Frame median 64.6 ms 33.3 ms
Chunks made a second 1,158 0
MakeResident per frame 35.4 ms 0.01 ms
Card busy per frame 34.3 ms 32.9 ms

This turns it off by default for now, until enlighted's pool that keeps the card's spare chunks by size lands. That pool will turn it back on.

  • LEONIDA_SCRATCH_CARD=1 still puts the scratch in the card, and LEONIDA_AB still switches it.
  • A card that used to get it by default says so once in the log, with the switch to use.
  • The doc comment says the change is provisional. It cites both measurements: the one above, and the gain card scratch was made for (-0.9 to -1.7 ms of card time on a 4060 Ti 16 GB on PCIe 4.0 x8), which is lost meanwhile.
  • d3d12/docs/switches.md is regenerated. Only that switch's row changes.

Tested

On Windows 11 Pro 10.0, Rust 1.98.1 pinned:

  • cargo test -p leonida-gpu --lib --locked scratch_card: host::scratch_card_tests::card_scratch_is_off_unless_asked_for passes. It checks that nothing, 0 or another value means off, and that 1 means on.
  • Red on main: the test needs the new scratch_card_chosen, so it does not build there. The behaviour it pins is the default flip, read in scratch_card(). The behavioural proof is enlighted's gate on the 3090, which they will run on this head.
  • cargo clippy -p leonida-gpu --all-targets --locked adds no warning in the changed lines; the existing warnings elsewhere are as on main.
  • tools/d3d12_switches.py --check d3d12/docs/switches.md, the docs tests and scripts/check_no_game_data.py are clean. It merges cleanly with current main.

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.2140: build and unit test only. This card has no GPU upload heap, so the change has no effect on it in game.

AI-assisted: yes

Checklist

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

…D12-SCRATCH-CARD-OFF-BY-DEFAULT)

Part of enlighted's D3D12-REAPER-BOUNDED-QUEUE, agreed with them. Card scratch, on by default where the
card's budget is 9 GiB or more and it has a GPU upload heap, churns chunks: SPARE_CARD keeps 256 MiB in all
and gives back an exact size only, so chunks of other sizes are let go in the list resets on the reaper and
made again on the recording threads. The reaper falls behind and the memory it holds piles up. Measured on
an RTX 3090 with a resizable BAR (23 pairs): frame median 64.6 ms with card scratch against 33.3 ms without.
It is off by default until the card's spare chunks are kept by size; LEONIDA_SCRATCH_CARD=1 still puts it
there and LEONIDA_AB still switches it. A card that used to get it says once that it is off now.
switches.md is regenerated for that row.
@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR on macOS 27.2.1, Apple M5, head f8ad7e245, main d920d850d. This is a type-check plus a code read: leonida-gpu does not build natively on macOS, so the Mac can't run the new unit test or a gate.

MEASURED

  • git merge-tree onto main gives no conflicts.
  • cargo check --target x86_64-pc-windows-msvc (no MSVC; the cc build steps are replaced with stand-ins), main vs main + this PR, exit 0 on both sides. The sorted warning lists are byte-identical: 19 / 19 default, 11 / 11 diagnostics, 24 / 24 --tests (this compiles scratch_card_tests), and 19 / 19 for leonida-hle --tests.

READ IN CODE

  • The LEONIDA_AB pick stays first in scratch_card() under diagnostics, so A/B by turns still works both ways.
  • scratch_card_chosen() is a pure function: on only with Some("1"); unset, 0 and any other value give off. Before, 0 was off, 1 on, and anything else meant "by budget". Now anything else means off. The new test pins all four cases.
  • The one-time tracing::info! fires only where the old default would have been on: a budget of at least 9 GiB, and host.mapped_card_memory() (it exists on main, at host.rs:13734). That matches the real old behavior, where without a GPU upload heap the scratch fell back to PC memory anyway (thegeogeo's analysis, seq 2217).
  • The doc comment and the switches.md row say the new default and why, with enlighted's SC-1 figures, and note that the 7b907de gain is given up for now.

ASSUMED

  • No gate run here. The expected effect on a ≥ 9 GiB card with ReBAR is enlighted's SC-1 measurement (3090, 33.3 vs 64.6 ms). Cards under 9 GiB already defaulted to PC memory, so nothing changes for them.

AI-assisted: Claude Code

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified on Windows, for a merge (not the author). AI-assisted (Claude Code agent oreofr-agent). RTX 4070 Laptop 8 GB, NVIDIA 621.40, Core Ultra 7 155H, Windows 11 Pro, Rust 1.98.1 pinned.

Head f8ad7e245 merged into main d920d850d with git merge --no-ff in a scratch worktree (clean, 2 files: host.rs, switches.md). check_no_game_data.py on both: exit 0.

Tests (cargo test --locked -p leonida-gpu, from d3d12/):

  • main + PR: 583 passed, 1 failed; the one failure is only_what_differs_goes_up, already failing on plain main at the time (582 passed, same 1) and fixed since by test: only_what_differs_goes_up knows about LEONIDA_TAKE_BLOCKS #2820. No new failure; +1 is the new card_scratch_is_off_unless_asked_for.
  • Red by mutation: with scratch_card_chosen put back to the old default (on unless 0), the new test fails (host.rs:2494). Reverted, tree clean.

In-game run on the merged build (diagnostics,mimalloc, no DLSS), fresh copy of our gate data, -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, walking: the keys file (LEONIDA_KEYS_FILE) held W with turns (L/J) in a 14 s loop; the game logged 100 scripted key changes.

  • 330 s in the world, then a clean close. 0 device loss, 0 ERROR lines, no crash.
  • Steady part: median 20.5 fps over the last 30 fps lines (min 10.2), private memory 23.6 GB at the end (7.6 GB of RAM free).

Limits: this card's budget is under 9 GiB, so it already had the scratch in the PC's memory before this change: the run shows no regression, not the gain. The gain is enlighted's measured A/B on this switch (RTX 3090, 23 pairs), cited in the description; their 3090 gate on this head was announced.

@fiestorra

Copy link
Copy Markdown
Contributor

Verified on Windows: the new test passes, and the crate is one test greener with nothing else moved. The one failure in the crate is main's, not this pull request's.

main d920d850d, cherry-picked (the branch is at main, merge-base = d920d850d), cargo 1.98.1, warm target:

cargo test -p leonida-gpu --lib card_scratch_is_off_unless_asked_for
  ok. 1 passed; 0 failed; 636 filtered out

cargo test -p leonida-gpu --lib
  FAILED. 583 passed; 1 failed; 53 ignored
    the failure: host::refresh_tests::only_what_differs_goes_up

That one failure is red on main before this diff, and #2820 fixes it - I checked it on a clean main earlier today, so it is not yours. main has 582 passed and this has 583, which is exactly the one test this adds.

What the change is, from the code and the doc line together: scratch_card_chosen(switch) and a test for it, and LEONIDA_SCRATCH_CARD becomes "off unless asked for" in docs/switches.md. A default flipping is the kind of change that needs the test that arrives with it, and it is here.

What I did not run: the title path itself - lists' scratch actually staying in the PC's memory needs a title with lists to allocate, which needs the game. The switch's decision function and its test are what I can measure, and they are green.

One thing worth checking before merge: #2820 makes the crate green, and this one adds a test to it. Whichever lands second will need the other's context to look green in CI - host::refresh_tests::only_what_differs_goes_up is red on main today, so this branch will show that red until #2820 lands.

AI-assisted: yes - DeepSeek v4.1 (flash), which ran both the new test and the crate.

Written with an AI assistant.

@enlight3d

Copy link
Copy Markdown
Contributor

RUN (independent run on Windows, #2819 head f8ad7e2, merged locally into main d920d85 with git merge --no-ff, no conflicts. The head still merges cleanly into main bf19719. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)

Commands, in d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with --locked, at low priority:

  • cargo test -p leonida-gpu --no-fail-fast: main 582 passed / 1 failed / 53 ignored; merge 583 / 1 / 53. card_scratch_is_off_unless_asked_for passes.
  • cargo test -p leonida-gpu --no-fail-fast --features diagnostics: main 663 / 1 / 54; merge 664 / 1 / 54.
  • The one failure is host::refresh_tests::only_what_differs_goes_up (assertion, left []). It fails on main and on the merge alike, in both builds, so it has nothing to do with this PR.
  • Red, build: on main with only the new test module added, it does not compile (E0432, scratch_card_chosen not found). That proves only that the function is new.
  • Red, by assertion: the merge with scratch_card_chosen put back to main's default rule (1 on, 0 off, otherwise on where the card's budget is 9 GiB or more, given this card's 24 GiB). cargo test -p leonida-gpu --lib scratch_card then fails on !scratch_card_chosen(None). So the test pins the new default, and main's rule fails it on a card like the RTX 3090 here.
  • cargo clippy -p leonida-gpu --all-targets (without -D warnings, which already fails on main in leonida-shader and leonida-lua), on main and on the merge: host.rs has the same 5 warnings on both sides, all outside the changed lines. None is new.
  • d3d12/docs/switches.md: python -I tools/d3d12_switches.py --check d3d12/docs/switches.md is clean on the merge. A fresh --out to a scratch file is byte-identical to the committed page, on the merge and on main (each against its own sources). Only the LEONIDA_SCRATCH_CARD row changes.
  • Not covered here: the effect in the game (chunks made, MakeResident, the reaper, frame times). That needs the in-game gate below.

In-game gate on the RTX 3090 (24 GB, NVIDIA 621.40, Windows 11), leonida.exe built from this head (f8ad7e2; d3d12/scripts/build-nvidia.ps1, diag profile, no DLSS files), scripts/play-d3d12.ps1 -SeriesS -NoDlss -NoFrameGen at the launcher's defaults otherwise (no LEONIDA_REAPER=0, no other override), the same save restored, standing still, with LEONIDA_NO_WATCHDOG=1 and a stall-mode timeline; warm-up and a throwaway run first. PASS: 600 s, world at 73 s (527 s in the world), no device loss, no gap over 10 s between frames. The log says scratch stays in the PC's memory by default.

  • The release thread's pile-up is gone on its own: reaper line at most 140 (0 at 180, 300, 420 and 600 s), no mapped scratch in card memory, private memory flat at most 28.9 GB, VRAM at most 12.9 GB. Host lists made ~0.02 a frame, preludes ~0.03 a frame.
  • Frame rate: median 23.3 fps (p5 19.1), frame median 42.9 ms. For comparison, two default runs of main 8f41d7d on this machine gave 18.0 fps (p5 12.9) and 12.1 fps (p5 5.2, piled up: line 398,312, mapped scratch 10.9 GB, 112.7 GB private). Caveat: this gate ran beside a cargo build (total CPU 92-100 %), so its frame rate is if anything pessimistic; a measured in-run A/B of LEONIDA_SCRATCH_CARD on this card (23 pairs, standalone) gave 33.3 ms against 64.6 ms per frame.

Not checked: Series X, walking, picture; cards between 9 and 16 GB.

No runtime/ C++ or Python in this PR, so no link or import check applies.
Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40.

@OreoFR
OreoFR merged commit 90c5144 into ProjectLeonida:main Dec 1, 2026
5 checks passed
@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging. Head f8ad7e245 merges cleanly with main d921489d1 (merge commit 90c514437).
Claim: D3D12-SCRATCH-CARD-OFF-BY-DEFAULT, #2570 (comment) (author TheGeogeo), part of enlighted's D3D12-REAPER-BOUNDED-QUEUE, agreed by them.
Red/green: by oreofr-agent (not the author), RTX 4070 Laptop, Windows 11: red by mutation (old default put back, the new test fails), green on this head. I ran cargo test --locked -p leonida-gpu on this head merged into the current main d921489d1: 589 passed, 0 failed.
In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, 330 s in the world walking, 0 device loss (comment above). This card already had the PC scratch before the change, so it shows no regression, not the gain. The gain is enlighted's measured A/B on this switch (RTX 3090, 23 pairs); OreoFR chose in writing not to wait for their 3090 gate on this head.
CI: green on the head (all 5 checks) and on main (CI, No Game Data; Runtime last green on cd9fe51a9, not triggered since).
AI-assisted: yes, Claude Code agent oreofr-agent, merged on OreoFR's (maintainer) written instruction.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Correction to my merge comment above: enlighted's RTX 3090 gate on this head was posted two minutes before the merge (#2819 (comment)): PASS, 527 s in the world, 0 device loss, at the default settings; the reaper pile-up gone (queue max 140, private memory flat at 28.9 GB), median 23.3 fps against 18.0 and 12.1 on main. So the change was also run on a card where it takes effect. AI-assisted (Claude Code agent oreofr-agent).

ItsNotRexed pushed a commit that referenced this pull request Dec 1, 2026
…, guards against churn, and is on again for big cards

TheGeogeo turned LEONIDA_SCRATCH_CARD off by default (f8ad7e2, #2819) "until the card's spare chunks are kept by
size": the card's pool kept 256 MiB of spare chunks in all, in the order they came, and let go of every chunk given
back past it, so the lists' resets freed chunks the recording threads made again (1,158 a second and 35 ms a frame
of MakeResident on an RTX 3090). Now:
- the card's spare chunks are kept up to a sixteenth of its budget, between 256 MiB and 1 GiB
  (LEONIDA_SPARE_CARD_MB=<MB> says it), and when the pool is full those kept longest go, not the one given back
  (Spare::give_evicting); LEONIDA_SPARE_CARD_GROW=0 keeps the old pool, LEONIDA_AB can name it;
- a guard sends new chunks to the PC's memory for the rest of the run if more than 1,000 are made in the card's
  within 10 s (100 a second; this PC's worst was 48 with the old pool), said once;
- with both, card scratch is on again where the card's budget is 9 GiB or more, as before #2819.
Measured before the change (run d281, RTX 4060 Ti 16 GB, by turns of 4 s standing, the old pool): card scratch on
against off, the frame's median -1.15 +-0.36 ms, the card -1.72 +-0.30, with 48 chunks a second made in the card's
memory and 3.7 ms a frame waited for their residency.

AI-assisted (Claude). Tested on RTX 4060 Ti 16 GB, driver 616.64: cargo check with and without diagnostics, the
two new tests (the default by card and switch, the pool letting the oldest go); in-game A/B to follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
OreoFR pushed a commit that referenced this pull request Dec 1, 2026
The reaper thread's channel had no bound and nothing slowed its senders.
On an RTX 3090 its line grew to hundreds of thousands of things in most
runs; the landed host lists waiting there kept their scratch chunks, and
the runs that were not stopped ended in a device loss as private memory
neared Windows' commit limit, while LEONIDA_REAPER=0 kept memory flat. The lists' scratch was in card
memory; it is in PC memory by default since #2819, where the line should
stay short. This bound is the safety net for LEONIDA_SCRATCH_CARD=1 and
any other cause of a backlog.

Once the line (REAPING) holds LEONIDA_REAPER_LIMIT things (4096 by
default; 0: no bound, as before; LEONIDA_AB can turn it on and off), what
the host is done with is let go of by the thread done with it, as
LEONIDA_REAPER=0 does. land(), bury() and fronts_let_go() go through one
helper (reap), which counts each batch as the reaper thread takes it off;
the check and the count are one atomic step (hand_over),
so the line holds fewer than the bound and one batch however many threads
send. A batch comes off the line also if letting it go panics. The first
time the bound takes hold is logged once; the census memory line counts
what was let go in place (REAPED_IN_PLACE) and says the bound.

Diagnostics only: host lists and preludes made per frame by the title's
threads, the submitting thread and the others, with their time; a SPLIT
slot for preludes made and recorded; HostList::reset split into what the
list held let go and the allocator and list resets.

Claim: D3D12-REAPER-BOUNDED-QUEUE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

4 participants