Skip to content

fix(gpu): P0 stability fixes for 8 GiB VRAM cards (RTX 3060 Ti) - #2929

Open
xVegetax wants to merge 4 commits into
ProjectLeonida:mainfrom
xVegetax:fix/vram-block-guard-8gb
Open

xVegetax wants to merge 4 commits into
ProjectLeonida:mainfrom
xVegetax:fix/vram-block-guard-8gb

Conversation

@xVegetax

@xVegetax xVegetax commented Dec 1, 2026

Copy link
Copy Markdown

Claimed in the coordination issue as: n/a

Depends on: none

Fixes: none

What and why

P0.1 — VRAM guard (pool.rs): refuse new 64 MiB texture heaps when
used + BLOCK > budget × 95%. Prevents over-budget MakeResident delays
(up to 961 ms) and device losses on 8 GiB cards at ~7–9 min.
Configurable via LEONIDA_VRAM_GUARD_PCT (80–99, default 95).

P0.2 — Pipeline stack (pipeline/make.rs): give each pipeline worker
64 MiB stack instead of OS default 2 MiB. nvgpucomp64.dll overflows
at ~7 min on Ampere/Wave32 (RTX 3060 Ti). Matches upstream PR #2588.
Configurable via LEONIDA_PIPELINE_STACK_MIB (2–256, default 64).

Tested

GPUs tested: NVIDIA RTX 3060 Ti, 8 GiB VRAM, Windows 11

AI-assisted: yes

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
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

Before every CreateHeap in the texture pool, check whether adding one
more 64 MiB block would push the card's current use past 95 % of its
DXGI budget (LEONIDA_VRAM_GUARD_PCT, 80–99; default 95). If so, return
None from the block-maker so the pool falls back to the non-pooled path
rather than allocating over budget.

pool::room() already receives [budget, used, ..] from QueryVideoMemoryInfo
once a second; the new VRAM_BUDGET/VRAM_USED atomics cache those values at
zero cost so vram_block_refused() needs no extra API call on the hot path.

On 8 GiB cards (RTX 3060 Ti budget ≈ 7.8 GiB) the guard engages above
~7.4 GiB, preventing the over-budget MakeResident delays of up to 961 ms
documented in pool.rs and the device losses reported on those cards
(driver fault ~7 min, device removal ~9 min into a session).

The six pre-existing compile errors in leonida-gpu (toggled, texture,
host, behind, line_on, host_resource) are unrelated to this change.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KVBEuPKuxL62bomCJgwLg

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Two P0s, two different problems, and neither one does what the title says on its own.

P0.2 (pipeline stack) duplicates open #2588, which already gives pipeline workers a 64 MiB stack and adds a GetCurrentThreadStackLimits test plus a Wine guard. Please drop this half and land #2588 instead — 18 workers × 64 MiB is ~1.1 GiB committed under Wine, which is exactly what #2588's guard avoids.

P0.1 (VRAM guard) does not cap VRAM. A refused block falls straight through to a per-texture committed allocation of the same resource (pool.rs:896-898 → resource.rs:3329, via the miss at :3294-3306), so the bytes are still spent and peak is unchanged — only the allocation path changes. Worse, VRAM_USED is written only in room() (pool.rs:50-52), called once a second from sweep() (host.rs:518-532), and is not incremented when a block is approved: between samples the guard re-approves against the same stale value, so one interval can create many 64 MiB blocks past 95 %. Staging (pool.rs:676), small, shadow/targets and buffers are ungated entirely. And LEONIDA_SWEEP=0 or a failed DXGI query leaves VRAM_BUDGET == 0, silently disabling it.

Neither change targets the failure mode we measured on this machine: the device removal is a driver-requested adapter PnP stop, not memory exhaustion — LEONIDA_D3D12_BACKGROUND=0 roughly doubles survival, while a 95 % threshold and a bigger thread stack do not address it. That does not mean the PR is wrong, but "P0 stability fixes for 8 GiB cards" needs an A/B run (or logs) showing which failure it removes; the 7–9 minute result is one card and two confounded changes.

Two concrete gaps:

  • No test covers vram_block_refused: disabling its decision leaves all 17 pool tests green. A pure threshold test (like spares()) would pin it.
  • d3d12/docs/switches.md was not regenerated: tools/d3d12_switches.py --check reports LEONIDA_PIPELINE_STACK_MIB and LEONIDA_VRAM_GUARD_PCT missing. Run --out before merge.

No image-quality risk in the fallback (identical D3D12_RESOURCE_DESC), and larger cards are safe; the real cost is that a card legitimately near budget abandons the pool it exists to use, with growth_failed throttling retries for 500 ms, so expect stutter rather than reuse at the margin. Merge is textually clean, and #2900/#2851 raise title allocations, which would push this guard into that fallback sooner.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Thanks for looking at 8 GiB cards, that's a real problem (I test on an RTX 4070 Laptop 8 GB). A review before anyone can merge this, read from the diff on head 51818b7ea; no build or run yet:

  1. Claim. The PR says "n/a"; the project asks for a claim in the coordination issue (Coordinating who works on what (continued from #1608) #2570) before a PR, with an item ID in the title.
  2. One topic per PR. The VRAM guard and the pipeline worker stack are two changes; please split them.
  3. The stack part duplicates GPU: pipeline workers get a 64 MB stack for the driver's compiler #2588 (open, same 64 MB stack for the pipeline workers, another author). Better to help that one land (it has review questions waiting) than to carry a second copy.
  4. The VRAM guard may not save VRAM. When vram_block_refused() makes the block maker return None, texture_with_cast returns None, and the caller in resource.rs (right after the pool call) creates the texture on its own in the same default heap. So the texture still lands in VRAM, just outside the pool, as one more allocation. Could you show a before/after measurement (VRAM use, MakeResident stalls, time to a device loss) with the guard on and off in one run (LEONIDA_AB), or keep the texture out of VRAM when the guard trips?
  5. d3d12/docs/switches.md needs regenerating for the two new switches (python -I tools/d3d12_switches.py --out d3d12/docs/switches.md), or the CI check will fail.
  6. Tests: a unit test for the threshold (used + BLOCK > budget * pct, budget 0 means no refusal, the 80-99 clamp) would make it verifiable without a game.

AI-assisted review (Claude Code agent oreofr-agent, for OreoFR).

@merzleckeier

Copy link
Copy Markdown
Contributor

Live texture-pool and worker-stack check on 51818b7e merged into 19d5f83c. The control used the same fixture on main.

  • Texture pool. I created a live 64×64 RGBA8 D3D12 texture on an RTX 4070 SUPER and on AMD RADV (Wine 11.22 / VKD3D-Proton 3.1.0), with a fixed synthetic budget snapshot. With LEONIDA_RIPE_BLOCKS=0, both trees opened a 64 MiB pool block at equality and with an unknown budget. One byte above the configured limit, at both 95% and 90%, main opened a block and the PR opened zero. The same call still returned a live texture through the committed-resource fallback. An existing block was reused on both trees above the limit. With default preallocation, equality let the PR open two 64 MiB blocks against the unchanged snapshot, and one byte above allowed neither block. All 32 cases passed. This measures the allocation path, with actual VRAM pressure and device-loss behavior outside the test.
  • Pipeline workers. The production pool started two regular and two screen-only workers per run on Wine. All four reserved 64 MiB by default here and 2 MiB on both main and the Wine-aware GPU: pipeline workers get a 64 MB stack for the driver's compiler #2588. Requests of 8, bad, 1, and 999 produced 8, 64, 2, and 256 MiB here. The reservation probe ran with an empty pipeline queue.
  • Switch inventory. The combined tree with D3D12-DOCS-SWITCHES-TEST: fail standalone tests when d3d12/docs/switches.md drifts #2934 failed on LEONIDA_PIPELINE_STACK_MIB and LEONIDA_VRAM_GUARD_PCT. Regenerating d3d12/docs/switches.md with python -I tools/d3d12_switches.py --out d3d12/docs/switches.md made the check and all 20 tests pass.

MEASURED with synthetic inputs. AI-assisted with OpenAI Codex and a separate read-only review.

LoadPipeline returns ERROR_INVALID_PARAMETER (0x80070057) for pipeline
state stream subobject types the driver cannot round-trip through the
library (mesh shaders on NVIDIA, some compute variants). Previously
these pipelines were stored anyway via StorePipeline, corrupting the
library file: on the next session LoadPipeline rejects the stored entry
with the same 0x80070057, forcing a recompile, and the driver may read
zeroed pages from a replaced mapped file and remove the device with
DXGI_ERROR_DRIVER_INTERNAL_ERROR (documented in library.rs lines 43-50).

Fix: detect 0x80070057 on load and skip StorePipeline for that pipeline.
The library file stays clean; the driver's own PSO cache handles these
pipelines across sessions as before.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KVBEuPKuxL62bomCJgwLg
@merzleckeier

Copy link
Copy Markdown
Contributor

Cache regression on the updated head with a working pipeline library 686c1124. I compared #2654 (55ac5d33) against #2654 plus this PR so that both arms could create an empty pipeline library on Wine. On bc332322e and again on 1fff69129, the unmodified mesh and depth-bounds tests each passed 3/3 per GPU with #2654 alone. Adding this PR made both tests fail 3/3 per GPU at the second-request library-load assertion. Both GPUs were physical, RTX 4070 SUPER (620.73.4) and Radeon RADV (26.3.1), Win64 Wine 11.22 / VKD3D-Proton 3.1.0.

Recording the actual API results on bc332322e explains the failure. With #2654 alone, the first LoadPipeline returns E_INVALIDARG for the cold name, StorePipeline returns S_OK, and the second load returns S_OK. With this PR, the first load returns the same code, the store is skipped, and the second load returns E_INVALIDARG again. Both patterns reproduced on both GPUs and in both fixtures. Microsoft’s API docs list an absent name as one reason for this HRESULT.

I then restored only pipeline/library.rs to the #2654 version while retaining this PR’s pool change and generated docs. Both tests were back to 3/3 on each GPU. The broad skip-on-E_INVALIDARG branch blocks ordinary cache population here. This covers same-process reuse with synthetic pipelines under Wine.

MEASURED with OpenAI Codex and a separate read-only review.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-checked 686c1124c. Two of the four are resolved:

  • Point 1 fixed. The stack half is gone — make.rs is identical to main, and merge-tree against both origin/main and #2588's 6b4a44e1 is clean, so #2588 is unblocked and nothing is left to reconcile.
  • Point 4 fixed. tools/d3d12_switches.py --check exits 0; LEONIDA_VRAM_GUARD_PCT is listed and the page re-homed LEONIDA_RENDER_SCALE_MIN.

Two are unchanged, and they are the substance:

  • The guard still does not cap peak. pool.rs:590-605 decides from VRAM_USED, written only in room() (pool.rs:52) and refreshed at most once a second by host::sweep, and never incremented when a block is approved — so N blocks approved inside one second all compare the same stale value. A refusal at pool.rs:896 makes take_texture(..)? return None, and resource.rs:3294-3300 falls through to the committed allocation of the same texture at resource.rs:3329, so the bytes are spent anyway, just unpooled (plus a 500 ms growth_failed back-off). Staging, small, shadow and targets remain ungated.
  • vram_block_refused is still untested. Forcing its decision to false leaves all 17 pool:: tests green. VRAM_BUDGET/VRAM_USED are plain statics, so a unit test is easy; the committed fallback also needs the same gate, otherwise the guard only changes which allocation path is used.

Separately, the new pipeline/library.rs change (skip StorePipeline on 0x80070057 and log "not cacheable by library") is an untested behaviour change, and merzleckeier's 21:02 report of a cache regression on this head needs an answer before merge — that is the kind of thing the gate exists to catch.

pipeline/library.rs: the previous fix checked LoadPipeline's E_INVALIDARG
to skip StorePipeline, but LoadPipeline always returns E_INVALIDARG for a
cold miss (name not yet in cache). This meant nothing was ever stored and
the cache was broken. Fix: always call StorePipeline; only skip if
StorePipeline itself returns 0x80070057 (the driver refuses to cache this
subobject stream type, e.g. mesh shaders on NVIDIA).

pool.rs: VRAM_PRESSURE was only triggered by vram_block_refused() when a
new block was requested. But VRAM can be over budget from committed
fallback allocations without any new block request. Fix: room() now also
triggers aggressive eviction when used > budget * pct/100 on every sweep
tick, so sustained over-budget VRAM is drained every second.

Also extracts vram_guard_pct() so the PCT OnceLock is shared between
room() and vram_block_refused().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KVBEuPKuxL62bomCJgwLg
@fiestorra

Copy link
Copy Markdown
Contributor

Independent verification on an RTX 4090, driver 621.24 (Windows 11) - by the AGENTS.md method. Two things to fix before merge, one of them serious.

It applies cleanly (merge-tree no conflicts; merge --squash onto 1fff69129 fine), and what it does run passes:

cargo test -p leonida-gpu --lib -- --test-threads=1 pool::
    ok. 17 passed; 0 failed
python -I tests/test_d3d12_switches.py
    exit 0

1. The second P0 in the body is not in the code.

The body says:

P0.2 - Pipeline stack (pipeline/make.rs): give each pipeline worker 64 MiB stack ... Configurable via LEONIDA_PIPELINE_STACK_MIB (2-256, default 64).

The branch touches no pipeline/make.rs, and LEONIDA_PIPELINE_STACK_MIB does not appear anywhere in it:

git grep LEONIDA_PIPELINE_STACK_MIB refs/remotes/pr/2929 -- d3d12     (nothing)
git diff --name-only <merge-base> refs/remotes/pr/2929
  d3d12/crates/leonida-gpu/src/pipeline/library.rs
  d3d12/crates/leonida-gpu/src/pool.rs
  d3d12/docs/switches.md

That is also the half with the interesting NVIDIA claim (the nvgpucomp64.dll overflow at ~7 min). What is here is P0.1 and a library.rs restructure that changes no behaviour - old and new both ignore StorePipeline's error, and the new match has a first Err arm for 0x80070057 followed by Err(_) that does the same thing, so the specific arm is dead.

2. switches.md was regenerated from a tree older than main, and merging it rewrites 271 of its ~745 lines.

Its own stat says +5/-1 because it is diffed against its base, but against today's main the file differs in 271 lines, and they are real regressions, not formatting:

main:  | `LEONIDA_RENDER_SCALE_MIN` | see the code |  | yes |  | `leonida-loader/hooks.rs` +1 |  |
PR:    | `LEONIDA_RENDER_SCALE_MIN` | see the code |  | yes |  | `leonida-gpu/host/menu.rs` +2 |  |

LEONIDA_RENDER_SCALE_MIN lives in leonida-loader/hooks.rs on main and the PR would move it back to where it was before. This is the shape the repo's own notes call out (a clean merge overwriting what main has in the same file, #2582).

test_d3d12_switches.py does not catch it - it passes here, because it checks that each name is present, not where the row says it lives. So the check that exists is not the check needed for this file.

3. A new clippy warning: pool.rs:76, "this if statement can be collapsed" (the other one, host.rs:7628, is on main already).

Two questions I could not answer without an 8 GiB card, and would like the PR to address:

  • VRAM_BUDGET/VRAM_USED are only refreshed inside room(). If a texture block is taken without room() having run since the last allocation, vram_block_refused() decides on stale numbers - and its doc says it is "called before every CreateHeap", which is not the same as "after every room()". What refreshes them between blocks?
  • The refusal path: the guard returns None inside take_texture, so the texture is not placed. What does the title get instead - a smaller piece, a placed texture elsewhere, or nothing? On 8 GiB that is the difference between a guard and a hole in the frame, and I cannot see it from here.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

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