leonida-gpu: plan small cards under their VRAM budget (LEONIDA_VRAM_MARGIN_MB) #3400

Code

Clone this repository

Checkout

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

git fetch -u origin "feat/vram-margin:feat/vram-margin"
git switch "feat/vram-margin"
Open
leonida/mandy
MoonEyes wants to merge 1 commit from feat/vram-margin into main

This pull request has changes conflicting with the target branch.

Merge manually to resolve the conflicts.

Conversation
Reviewers 0 approvals
  • No reviewers
Details

This pull request has changes conflicting with the target branch.

Merge manually to resolve the conflicts.

  • d3d12/docs/findings.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'feat/vram-margin:feat/vram-margin'
git switch 'feat/vram-margin'
Member

Branche : feat/vram-margin (commit 404daa4621), sur main 5286146626.

Change

Host::video_memory now gives its callers DXGI's local budget less a margin on small cards:

  • 512 MB by default, LEONIDA_VRAM_MARGIN_MB=<n> to change it (at most 2048), 0 turns it off;
  • only on cards whose first budget read is under 7,680 MB (8 GB cards and smaller); the decision is taken once, so
    a budget that moves around the line does not make the plan jump, and a 10 GB card whose budget falls later keeps none;
  • never under 2 GiB nor over the system's budget.

Every planner that weighs use against the budget goes through video_memory: the texture copies' room
(resource::room), the card copies of title buffers (shadow.rs), the room kept for render targets, vram_idle_ms,
the panel and the periodic memory line. log_video_memory and the "budget changed" line keep the system's numbers.
One log line at the first reading says what was applied:
card memory margin: N MB off a budget of B MB, planned with P MB (...).

On by default under 7,680 MB, as in Darner: the players it is for (8 GB cards near their budget, where crash #1,
0x887A0005 device removed, shows up most) will not set a variable. Larger cards are unchanged, and
LEONIDA_VRAM_MARGIN_MB=0 gives the old behaviour back (also the A arm of the A/B below).

Docs: d3d12/docs/switches.md regenerated (python -I tools/d3d12_switches.py --out d3d12/docs/switches.md), a note
in d3d12/docs/findings.md (what changed, why, where from, what to measure).

Not ported: Darner's separate cap DARNER_VRAM_BUDGET_MB (LEONIDA_TEXTURE_BUDGET_MB already caps texture copies).

Tests

  • cargo test -p leonida-gpu --locked: 733 passed, 0 failed, 59 ignored; 4 new tests in host::vram_margin_tests
    (the 7,680 MB line read once, the 512 default, 0 off, 2048 cap, 2 GiB floor, unparsable values).
  • python -I tests/test_d3d12_switches.py: 22 tests OK.
  • python -I scripts/check_no_game_data.py on the three touched files: clean.
  • Windows 11, build only; the game was not run.

In-game A/B required

Nothing here is measured on Mandy. Rendering rule: an in-game run of 300 s without device loss before merging, plus
this A/B:

  • PC: an 8 GB card (RTX 3050/4060 8 GB or similar, budget around 7.2-7.4 GB), 16 GB RAM, Series S profile.
  • Arms: A = LEONIDA_VRAM_MARGIN_MB=0, B = variable unset (512 MB). Order A B B A, 600 s each, same save
    and route.
  • Read per run: device losses (crashes.log, 0x887A0005 / 0x8007000E), the start line card memory margin: ...
    (B must say 512 MB off), DXGI use against budget and shared (PC) memory use from the memory lines, frame-time median
    per phase (docs/measuring_fps.md).
  • Expect stability more than average fps; watch for more blur or pop-in (less room for textures).
  • Optional: a Series X profile on the same card, and one run on a 12 GB card to check the start line says 0 MB.

Credit

Ported from Darner (GPL-3.0-or-later, compatible with Mandy's licence): crates/darner-gpu/src/host.rs,
small_card_headroom and planned_budget, themselves from a community patch there. Their figures (one card, a player's
report): RTX 4050 Laptop 6 GB, 3-13 -> 10-18 fps with 512 MB off. Credited in the code comment and the commit.

AI assistance

AI-assisted: written by Claude Opus 5.5 (claude-opus-5-5) in Claude Code; reviewed by a separate Claude Opus 5.5
reviewer agent (code-reviewer), whose two remarks (the line read once, which log lines show the planned budget) were
taken.

🤖 Generated with Claude Code

Branche : `feat/vram-margin` (commit `404daa4621`), sur `main` `5286146626`. ## Change `Host::video_memory` now gives its callers DXGI's local budget less a margin on small cards: - 512 MB by default, `LEONIDA_VRAM_MARGIN_MB=<n>` to change it (at most 2048), `0` turns it off; - only on cards whose **first** budget read is under 7,680 MB (8 GB cards and smaller); the decision is taken once, so a budget that moves around the line does not make the plan jump, and a 10 GB card whose budget falls later keeps none; - never under 2 GiB nor over the system's budget. Every planner that weighs use against the budget goes through `video_memory`: the texture copies' room (`resource::room`), the card copies of title buffers (`shadow.rs`), the room kept for render targets, `vram_idle_ms`, the panel and the periodic memory line. `log_video_memory` and the "budget changed" line keep the system's numbers. One log line at the first reading says what was applied: `card memory margin: N MB off a budget of B MB, planned with P MB (...)`. **On by default under 7,680 MB**, as in Darner: the players it is for (8 GB cards near their budget, where crash #1, 0x887A0005 device removed, shows up most) will not set a variable. Larger cards are unchanged, and `LEONIDA_VRAM_MARGIN_MB=0` gives the old behaviour back (also the A arm of the A/B below). Docs: `d3d12/docs/switches.md` regenerated (`python -I tools/d3d12_switches.py --out d3d12/docs/switches.md`), a note in `d3d12/docs/findings.md` (what changed, why, where from, what to measure). Not ported: Darner's separate cap `DARNER_VRAM_BUDGET_MB` (`LEONIDA_TEXTURE_BUDGET_MB` already caps texture copies). ## Tests - `cargo test -p leonida-gpu --locked`: 733 passed, 0 failed, 59 ignored; 4 new tests in `host::vram_margin_tests` (the 7,680 MB line read once, the 512 default, `0` off, 2048 cap, 2 GiB floor, unparsable values). - `python -I tests/test_d3d12_switches.py`: 22 tests OK. - `python -I scripts/check_no_game_data.py` on the three touched files: clean. - Windows 11, build only; **the game was not run.** ## In-game A/B required Nothing here is measured on Mandy. Rendering rule: an in-game run of 300 s without device loss before merging, plus this A/B: - PC: an 8 GB card (RTX 3050/4060 8 GB or similar, budget around 7.2-7.4 GB), 16 GB RAM, Series S profile. - Arms: **A** = `LEONIDA_VRAM_MARGIN_MB=0`, **B** = variable unset (512 MB). Order **A B B A**, 600 s each, same save and route. - Read per run: device losses (`crashes.log`, 0x887A0005 / 0x8007000E), the start line `card memory margin: ...` (B must say 512 MB off), DXGI use against budget and shared (PC) memory use from the memory lines, frame-time median per phase (`docs/measuring_fps.md`). - Expect stability more than average fps; watch for more blur or pop-in (less room for textures). - Optional: a Series X profile on the same card, and one run on a 12 GB card to check the start line says 0 MB. ## Credit Ported from Darner (GPL-3.0-or-later, compatible with Mandy's licence): `crates/darner-gpu/src/host.rs`, `small_card_headroom` and `planned_budget`, themselves from a community patch there. Their figures (one card, a player's report): RTX 4050 Laptop 6 GB, 3-13 -> 10-18 fps with 512 MB off. Credited in the code comment and the commit. ## AI assistance AI-assisted: written by Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code; reviewed by a separate Claude Opus 5.5 reviewer agent (`code-reviewer`), whose two remarks (the line read once, which log lines show the planned budget) were taken. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Host::video_memory now gives its callers DXGI's local budget less a margin
(512 MB by default, LEONIDA_VRAM_MARGIN_MB, 0 turns it off, at most 2048,
never under 2 GiB) on cards whose first budget read is under 7,680 MB. The
texture room, the card copies of title buffers, the room kept for render
targets and the panel all plan with it; log_video_memory and the "budget
changed" line keep the system's numbers, and one log line at the first
reading says the margin applied.

On by default under 7,680 MB, as in Darner: the players it is for (8 GB
cards near their budget, where crash #1, 0x887A0005, shows up most) set no
variable; cards at 7,680 MB or more are unchanged, the small/large decision
is taken once so a budget moving around the line does not make the plan jump,
and LEONIDA_VRAM_MARGIN_MB=0 restores the old behaviour for the A/B.

Ported from Darner (GPL-3.0-or-later), crates/darner-gpu/src/host.rs,
small_card_headroom and planned_budget (from a community patch there).
Their figures, not measured here: RTX 4050 Laptop 6 GB, 3-13 -> 10-18 fps.
In-game A/B required before merging (recipe in d3d12/docs/findings.md).

Tests: cargo test -p leonida-gpu --locked (733 passed, 4 new),
tests/test_d3d12_switches.py, check_no_game_data.py on the touched files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
OreoFR referenced this pull request from a commit 2026-12-04 01:11:23 +00:00
First-time contributor

TAKEN | Otavmacedo | ETA 2026-12-04 06:00 UTC | AI-assisted: Claude Code (Claude Opus 5.5)

The machine this asks for: RTX 3070 8 GB desktop (DXGI budget about 7.2 GB), driver 621.47, Windows 11, 16 GB RAM with a fixed 24 GB page file, profile M.

  • BUILD: same executable for A and B: current main + #3400, and #3385 (log lines only: it names the removal reason and logs video memory when the device goes, which this card is likely to exercise).
  • RECIPE: Series S, -Dlss quality -NoFrameGen; gate runs from one save with mods/gate_driver idle (LEONIDA_GAME_ACTIONS=1); one discarded warm-up, then A (LEONIDA_VRAM_MARGIN_MB=0) B (=512, set explicitly so the settings line shows it) B A, 600 s in the world each.
  • By hand, not scripts/game-abba.ps1: on main it does not pass the gate's memory stops (#3305), and on this PC the default stops end a run early. Each run uses -StopPrivateMB 32000 -StopCommitLeftMB 1500 -StopOverBudgetSeconds 900 (deviations, also in each RUN).
  • Why it is worth it here: tonight on main with this recipe the card went over its budget in both runs (7.4 and 7.7 GB against 7.2), one gate run stopped for video memory over budget for 62 s, and the next lost the device (0x887A0005) after 306 s in the world, while new pipelines were being made.

SEND BACK, per run: device losses and their reason, the card memory margin start line, video memory in use against budget and shared memory from the memory lines, frame-time median and p5, time in the world.

TAKEN | Otavmacedo | ETA 2026-12-04 06:00 UTC | AI-assisted: Claude Code (Claude Opus 5.5) The machine this asks for: RTX 3070 8 GB desktop (DXGI budget about 7.2 GB), driver 621.47, Windows 11, 16 GB RAM with a fixed 24 GB page file, profile M. - BUILD: same executable for A and B: current `main` + #3400, and #3385 (log lines only: it names the removal reason and logs video memory when the device goes, which this card is likely to exercise). - RECIPE: Series S, `-Dlss quality -NoFrameGen`; gate runs from one save with `mods/gate_driver` idle (`LEONIDA_GAME_ACTIONS=1`); one discarded warm-up, then A (`LEONIDA_VRAM_MARGIN_MB=0`) B (`=512`, set explicitly so the settings line shows it) B A, 600 s in the world each. - By hand, not `scripts/game-abba.ps1`: on `main` it does not pass the gate's memory stops (#3305), and on this PC the default stops end a run early. Each run uses `-StopPrivateMB 32000 -StopCommitLeftMB 1500 -StopOverBudgetSeconds 900` (deviations, also in each RUN). - Why it is worth it here: tonight on `main` with this recipe the card went over its budget in both runs (7.4 and 7.7 GB against 7.2), one gate run stopped for video memory over budget for 62 s, and the next lost the device (0x887A0005) after 306 s in the world, while new pipelines were being made. SEND BACK, per run: device losses and their reason, the `card memory margin` start line, video memory in use against budget and shared memory from the memory lines, frame-time median and p5, time in the world.
First-time contributor

DONE | the A/B taken above | AI-assisted: Claude Code (Claude Opus 5.5)

Short: on this 8 GB card the margin does what it says (it plans under 6,737 MB, and the card spends much less time over its budget, with better low-percentile frames), but it does not keep the device: all four measured runs lost it, two per arm. PROVISIONAL (n=2 per arm).

  • BUILD: same executable for A and B: main 4d9e4d05d + #3400 (404daa462) + #3385 (1bc561dd1), diagnostics,dlss,mimalloc.
  • MACHINE: NVIDIA GeForce RTX 3070 8 GB desktop, driver 621.47, Windows 11 Pro 26200, 16 GB RAM (fixed 24 GB page file, commit limit 39.9 GB), Ryzen 7 5700X, resizable BAR on. Profile M.
  • RECIPE: Series S, -Dlss quality -NoFrameGen; gate runs from one save, mods/gate_driver idle (LEONIDA_GAME_ACTIONS=1); -WorldSeconds 600, memory stops widened (private 32000 MB, commit left 1500 MB, video memory over budget 900 s). Order: a discarded warm-up (B), then A B B A. Caches warm (the pipeline record from earlier runs that evening).
  • The start line, as asked: A card memory margin: 0 MB off a budget of 7249 MB, planned with 7249 MB; B 512 MB off a budget of 7249 MB, planned with 6737 MB.
Run Margin End In the world Frame median fps / p5 fps Frame p95 Video memory peak / budget Over budget Shared peak Longest gap
warm-up 512 time limit (300 s), gate FAIL on its 10.2 s gap 306 s 24.9 / 11.6 86.3 ms 7,235 / 7,169 MB 10.9 s 5,370 MB 10.2 s
A1 0 device lost 507 s 21.3 / 11.3 88.5 ms 7,462 / 7,187 MB 258.9 s 5,692 MB 10.2 s
B1 512 device lost 269 s 26.6 / 14.2 70.6 ms 7,292 / 7,223 MB 97.7 s 5,180 MB 2.9 s
B2 512 device lost 264 s 27.2 / 14.8 67.5 ms 7,343 / 7,136 MB 76.2 s 5,338 MB 2.2 s
A2 0 device lost 300 s 27.8 / 11.9 84.1 ms 7,772 / 7,220 MB 135.7 s 5,741 MB 5.8 s

(fps from presents after 60 s in the world, as summary.json gives them. tools/d3d12_ab_compare.py abba leaves all four out because each run's gate failed, so there is no verdict from the tool; these are the per-run values.)

What the runs say, each tagged:

  • MEASURED (n=2 per arm): with the margin the card is over its budget for 98 and 76 s against 259 and 136 s without it, its peak is lower (7.3 GB against 7.5 and 7.8), the p5 frame is better (14.2-14.8 fps against 11.3-11.9), the p95 frame shorter (68-71 ms against 84-88) and the longest gap shorter (2-3 s against 6-10 s). The medians do not separate (26.6-27.2 against 21.3-27.8).
  • MEASURED (n=4): all four lost the device, with the reason #3385 names as DXGI_ERROR_DEVICE_REMOVED (not HUNG or RESET), after 264-507 s in the world. With two runs a side I do not read anything into B's two losses coming earlier.
  • MEASURED (n=4): three of the four losses were first seen in leonida_gpu::draw while textures picked by index were made ready; the fourth (A2) in the host's depth plane pass. Even with 6,737 MB planned, the system counted 7.3 GB in use at the peak in B, so what goes over the budget is not only what the planners place.
  • ASSUMED: that the loss is the memory pressure itself is not shown: the over-budget time falls from 395 s to 174 s over the two runs of each arm (not in every pair: B1 98 s against A2 136 s) and the losses stay.

Not watched: blur or pop-in (no one looked at the screen during these runs). Earlier the same evening, on main with this recipe, a run of 909 s in the world kept the device but showed a flat grey picture from a story transition on (reported in #2464), and another lost the device at 306 s.

GPUs tested: NVIDIA GeForce RTX 3070 8 GB, 621.47, Windows 11, profile M.

The figures were recomputed from the run records by a separate read-only agent before posting (one rounding corrected).

DONE | the A/B taken above | AI-assisted: Claude Code (Claude Opus 5.5) **Short: on this 8 GB card the margin does what it says (it plans under 6,737 MB, and the card spends much less time over its budget, with better low-percentile frames), but it does not keep the device: all four measured runs lost it, two per arm.** PROVISIONAL (n=2 per arm). - BUILD: same executable for A and B: `main` 4d9e4d05d + #3400 (404daa462) + #3385 (1bc561dd1), `diagnostics,dlss,mimalloc`. - MACHINE: NVIDIA GeForce RTX 3070 8 GB desktop, driver 621.47, Windows 11 Pro 26200, 16 GB RAM (fixed 24 GB page file, commit limit 39.9 GB), Ryzen 7 5700X, resizable BAR on. Profile M. - RECIPE: Series S, `-Dlss quality -NoFrameGen`; gate runs from one save, `mods/gate_driver` idle (`LEONIDA_GAME_ACTIONS=1`); `-WorldSeconds 600`, memory stops widened (private 32000 MB, commit left 1500 MB, video memory over budget 900 s). Order: a discarded warm-up (B), then A B B A. Caches warm (the pipeline record from earlier runs that evening). - The start line, as asked: A `card memory margin: 0 MB off a budget of 7249 MB, planned with 7249 MB`; B `512 MB off a budget of 7249 MB, planned with 6737 MB`. | Run | Margin | End | In the world | Frame median fps / p5 fps | Frame p95 | Video memory peak / budget | Over budget | Shared peak | Longest gap | |---|---|---|---|---|---|---|---|---|---| | warm-up | 512 | time limit (300 s), gate FAIL on its 10.2 s gap | 306 s | 24.9 / 11.6 | 86.3 ms | 7,235 / 7,169 MB | 10.9 s | 5,370 MB | 10.2 s | | A1 | 0 | device lost | 507 s | 21.3 / 11.3 | 88.5 ms | 7,462 / 7,187 MB | 258.9 s | 5,692 MB | 10.2 s | | B1 | 512 | device lost | 269 s | 26.6 / 14.2 | 70.6 ms | 7,292 / 7,223 MB | 97.7 s | 5,180 MB | 2.9 s | | B2 | 512 | device lost | 264 s | 27.2 / 14.8 | 67.5 ms | 7,343 / 7,136 MB | 76.2 s | 5,338 MB | 2.2 s | | A2 | 0 | device lost | 300 s | 27.8 / 11.9 | 84.1 ms | 7,772 / 7,220 MB | 135.7 s | 5,741 MB | 5.8 s | (fps from presents after 60 s in the world, as `summary.json` gives them. `tools/d3d12_ab_compare.py abba` leaves all four out because each run's gate failed, so there is no verdict from the tool; these are the per-run values.) What the runs say, each tagged: - MEASURED (n=2 per arm): with the margin the card is over its budget for 98 and 76 s against 259 and 136 s without it, its peak is lower (7.3 GB against 7.5 and 7.8), the p5 frame is better (14.2-14.8 fps against 11.3-11.9), the p95 frame shorter (68-71 ms against 84-88) and the longest gap shorter (2-3 s against 6-10 s). The medians do not separate (26.6-27.2 against 21.3-27.8). - MEASURED (n=4): all four lost the device, with the reason #3385 names as DXGI_ERROR_DEVICE_REMOVED (not HUNG or RESET), after 264-507 s in the world. With two runs a side I do not read anything into B's two losses coming earlier. - MEASURED (n=4): three of the four losses were first seen in `leonida_gpu::draw` while textures picked by index were made ready; the fourth (A2) in the host's depth plane pass. Even with 6,737 MB planned, the system counted 7.3 GB in use at the peak in B, so what goes over the budget is not only what the planners place. - ASSUMED: that the loss is the memory pressure itself is not shown: the over-budget time falls from 395 s to 174 s over the two runs of each arm (not in every pair: B1 98 s against A2 136 s) and the losses stay. Not watched: blur or pop-in (no one looked at the screen during these runs). Earlier the same evening, on `main` with this recipe, a run of 909 s in the world kept the device but showed a flat grey picture from a story transition on (reported in #2464), and another lost the device at 306 s. GPUs tested: NVIDIA GeForce RTX 3070 8 GB, 621.47, Windows 11, profile M. The figures were recomputed from the run records by a separate read-only agent before posting (one rounding corrected).
First-time contributor

One more data point from the same PC, for reading the result above (AI-assisted: Claude Code, Claude Opus 5.5): on main, the same DLSS recipe with LEONIDA_D3D12_BACKGROUND=0 (Developer mode) kept the device in 2 of 2 runs past 600 s, while spending longer over the video memory budget (329-346 s) than the runs that lost it. Details on #2821 (https://git.leonida.now/leonida/mandy/issues/2821#issuecomment-25224). So on this card the losses track the background shader work more than the budget the margin plans under; the margin's gains in time over budget and low-percentile frames above still stand on their own.

One more data point from the same PC, for reading the result above (AI-assisted: Claude Code, Claude Opus 5.5): on `main`, the same DLSS recipe with `LEONIDA_D3D12_BACKGROUND=0` (Developer mode) kept the device in 2 of 2 runs past 600 s, while spending longer over the video memory budget (329-346 s) than the runs that lost it. Details on #2821 (https://git.leonida.now/leonida/mandy/issues/2821#issuecomment-25224). So on this card the losses track the background shader work more than the budget the margin plans under; the margin's gains in time over budget and low-percentile frames above still stand on their own.
Collaborator

The helpers are pure, tested, keep the 2 GiB floor, and never exceed the system budget. Two details: the constant is 7680 << 20, which is 7,680 MiB, while the comments and the switch table say MB (7,680 MB is 7,324 MiB); and the findings note asks for the A B B A run on an 8 GB card before merging, so this should not land as a default change before that measurement exists. Say MiB, or use 8000 MB if that is the intent.

The helpers are pure, tested, keep the 2 GiB floor, and never exceed the system budget. Two details: the constant is `7680 << 20`, which is 7,680 MiB, while the comments and the switch table say MB (7,680 MB is 7,324 MiB); and the findings note asks for the A B B A run on an 8 GB card before merging, so this should not land as a default change before that measurement exists. Say MiB, or use 8000 MB if that is the intent.
Sign in to join this conversation.
No description provided.