Skip to content

fix(gpu): by_address leaks its cap and inflates WINDOW_CARD_BYTES - #2981

Open
Luislariosss wants to merge 1 commit into
ProjectLeonida:mainfrom
Luislariosss:fix/p2a-bvh-accounting
Open

Luislariosss wants to merge 1 commit into
ProjectLeonida:mainfrom
Luislariosss:fix/p2a-bvh-accounting

Conversation

@Luislariosss

Copy link
Copy Markdown

Claimed in the coordination issue as: D3D12-GPU-BY-ADDRESS-ACCOUNTING (#2570 (comment))

What and why

shadow::by_address() kept the card-memory cap (LEONIDA_MEMORY_CARD_MB) in a function-local USED counter:

  • it was incremented before the allocation and never rolled back when the run went over the cap or when uav_buffer() returned None, so headroom leaked for the rest of the session;
  • it was never decremented when the arena went;
  • every arena, card or PC shared fallback alike, was added to WINDOW_CARD_BYTES, so by-address copies ate LEONIDA_CARD_WINDOWS_MB and the window room card_room_kept() keeps for real windows.

This moves the counter to module scope (BY_ADDRESS_USED), reserves it with fetch_update + checked_add (rollback only when the allocation fails) and releases it on drop. Card::account (CardAccount) picks which counter Drop releases, so a shadow cannot be in two accounting buckets at once. By-address arenas no longer touch WINDOW_CARD_BYTES; BY_ADDRESS_USED is included in live_span_bytes() and card_stats().

Behaviour change, both behind opt-in switches: by-address arenas no longer consume LEONIDA_CARD_WINDOWS_MB nor card_room_kept(), and PC-memory fallbacks drop out of the span and the card stats.

Tested

  • cargo test -p leonida-gpu --offline --lib -- --test-threads=1: 647 passed, 0 failed, 59 ignored.
  • cargo test -p leonida-gpu --offline --lib --features diagnostics -- --test-threads=1: 728 passed, 0 failed, 59 ignored.
  • cargo clippy -p leonida-gpu --all-targets --offline: clean (one pre-existing warning outside this file).

New tests: cap boundary (used + len == most), u64::MAX overflow, rollback, all four CardAccount variants on release and drop; land_tests now balances CARD_BYTES[0]; the shared counter tests take one lock so they can run in parallel.

GPUs tested: n/a (unit tests only; no in-game run).

AI-assisted: yes — implementation and review by coding agents, reviewed by the submitter.

Checklist

  • Based on current main and merges cleanly (rebased on c4ca5761).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption.
  • No new translator rule.

by_address() kept its card-memory cap in a function-local USED counter
that was incremented on every attempt, including over the cap and when
uav_buffer() returned None, and was never decremented when the arena
went: cap headroom leaked for the rest of the session. It also added
every arena, card or PC fallback alike, to WINDOW_CARD_BYTES.

- BY_ADDRESS_USED: a single counter, reserved with fetch_update and
  checked_add and rolled back only when the allocation fails; released
  on drop.
- Card::account (CardAccount) picks the counter Drop releases, so a
  shadow cannot be in two accounting buckets at once.
- by_address no longer touches WINDOW_CARD_BYTES; BY_ADDRESS_USED is
  included in live_span_bytes() and card_stats().
- Behaviour: by-address arenas no longer eat LEONIDA_CARD_WINDOWS_MB nor
  the window room kept in card_room_kept(), and PC-memory fallbacks drop
  out of the span and the card stats.
- Tests: cap boundary, u64 overflow, rollback, all four accounts on
  release and drop; land_tests now balances CARD_BYTES[0]; the shared
  counter tests take one lock so they can run in parallel.
@andrew-ai

Copy link
Copy Markdown

Verified the cap and fallback through by_address() with actual D3D12 allocations on an RTX 5070, driver 616.92, at b020637. I set LEONIDA_MEMORY_CARD_MB=1 in a fresh process and requested three 1 MiB arenas backed by distinct synthetic CPU ranges.

The first arena used card memory. The second used the shared fallback while the first was held. Dropping the first returned BY_ADDRESS_USED, live_span_bytes() and card_stats()[8] to zero even with the fallback still live. The third arena then used card memory. Dropping the fallback left all three counters at 1 MiB, and dropping the third returned them to zero. WINDOW_CARD_BYTES stayed zero throughout. I checked the actual CardAccount selected for each allocation and resolved the card name from the D3D12 device LUID.

This covers exact-cap allocation, fallback, and reuse on hardware. The test used small allocations to exercise the configured cap. Codex-assisted for #2570.

@merzleckeier

Copy link
Copy Markdown
Contributor

Real by-address allocations regain the device heap after dropping the previous allocation.

I called production by_address with LEONIDA_MEMORY_CARD_MB=1 on main c4ca5761 and b020637ac09182f12fe0e8f70e279666e27b3676 integrated with #2975. The tested shadow.rs is identical with #2981 alone, and the D3D12 trees match after merging current main 1abef08d. Three fresh runs per tree and GPU agreed on RTX 4070 SUPER (620.73.4) and AMD Raphael (RADV 26.3.1), with Wine 11.22 and VKD3D-Proton 3.1.0.

Allocation Shader-side heap on main Shader-side heap with #2981
1 MiB at the cap DEFAULT DEFAULT
256 KiB while the first allocation is live CUSTOM L0 CUSTOM L0
1 MiB after both drop CUSTOM L0 DEFAULT
1.25 MiB with no live allocations CUSTOM L0 CUSTOM L0
256 KiB after the oversized allocation drops CUSTOM L0 DEFAULT

With the first two live, main added 1,310,720 bytes to both window and total live-span accounting. The PR added zero window bytes and counted 1,048,576 bytes in the aggregate. Oversized shared fallback added no card bytes on the PR, and all deltas were zero after drops. The three stock by-address tests passed on each GPU.

The test uses actual D3D12 resource heap properties at a small configured cap, with the separate DXGI video-memory budget check satisfied. AI-assisted, independently checked against the raw results.

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.

3 participants