Video-memory arenas: D32S8 and MSAA targets get heaps of their own size, let go when unused #225

Manually merged
github merged 1 commit from gh-pr/225/head into gh-pr/225/base 2026-11-26 04:16:33 +00:00
Owner

@⁠Kalmarv on GitHub · 2026-11-23 21:22 UTC · original
Kalmarv:fix/d32s8-exact-heaps → main · +256 −140 · 1 files · 1 commits · merged 2026-11-23 21:29 UTC

This addresses an open item in HANDOFF.md ("Retail game bring-up"): "arenas for D32S8 targets still take 2.8 GB (one per 256 MB of addresses; a D32S8 → R32 + separate stencil split, or exact-size heaps, would cut it)". It takes the exact-size heaps.

What changes (crates/xcl/src/hle/d3d12x/vram.rs only)

  • Arena size. Arenas are what GPU-owned textures fall back to when the driver won't make them reserved, which on this RTX 5070 Ti is D32S8 / R32G8X24 (0x80070057) and MSAA. An arena used to be at least 256 MiB, on a 256 MiB boundary. Now it is a heap of its placement's own size, aligned to 64 KiB, or 4 MiB for MSAA.

  • No unions.

    • A placement inside an existing arena, at an offset the arena's alignment allows, shares it as before. The newest such arena wins.
    • Any other placement gets an arena of its own.

    So a placement that straddles arenas no longer makes a growing union arena while the old ones live on. That was the pattern behind the "15 GB of arenas in 2 s".

  • Release. Arenas count their resources (vram::forget, which res_release already calls). An arena no resource has used for 2 s leaves the list and its heap goes, like the 2 s return of target pages (IDLE). Placed again within 2 s, the same memory is reused, with what it holds.

  • Unchanged:

    • resource descriptions;
    • views: depth SRV plane 0, stencil SRV plane 1;
    • CopyResource, clears and DSVs;
    • the pages path;
    • arena_bytes() for XCL_ACTIVITY.
  • The arena bookkeeping (Arenas<H>) is separate from D3D12, so the CPU tests drive it with fake heaps.

Why not the D32S8 → R32 + separate stencil split: D3D12 has no stencil-only depth format, and a DSV binds depth and stencil of one resource. A separate stencil resource can't be stencil-tested, and plane-1 views, copies, clears and pipeline formats would all need rewriting.

Tested

Windows 11, RTX 5070 Ti, NVIDIA 610.74, MSVC debug. Branch 327c24c on main af20b4b. Controls on main 0cd0a53, whose vram.rs is the same as af20b4b's.

CPU tests (cargo test -p xcl --lib hle::d3d12x::vram::tests, 4/4 pass):

  • an_arena_is_shared_at_its_addresses_and_otherwise_its_placements_size: an arena takes 70 MiB, not 256. The same address or a smaller placement inside shares it. A straddling placement, and an MSAA one needing 4 MiB, get their own.
  • an_arena_no_resource_uses_goes_after_two_seconds: an arena placed again within 2 s is reused; after 2 s it is gone.
  • streamed_targets_stay_bounded: the title's pattern from HANDOFF. Each round places 8 back-to-back 64 MiB targets over 3.5 GB, every other round at the last round's addresses or a third of a target further on, so the targets straddle. A target lives 1.5 s, and 400 rounds are run, 0.5 s apart.
  • tile_ranges_per_heap: unchanged.

The same streaming scenario, with main's plan() and an arena replaced while its resources live keeping its memory, was run as a control (not in this PR):

arena bytes main this PR
peak 7.25 GB 2.15 GB
max, rounds 20–120 3.49 GB 1.68 GB
max, rounds 300–399 3.76 GB (grows) 1.68 GB (flat)
test's bound (7 rounds of targets, 3.76 GB) exceeded held

GPU test d32s8_targets_take_their_own_size_of_video_memory (skips on WARP):

14 D32S8 3840×2160 targets, 256 MiB apart main (same test, control) this PR
arena bytes 3,758,096,384 (14 × 256 MiB): fails 585,367,552 (14 × 41,811,968)
local video memory, increase (QueryVideoMemoryInfo) +3.76 GB +590 MB

The same test also covers:

  • clearing one 64×64 target to depth 0.25 / stencil 0x5A, CopyResource to a second, and reading both planes back by CopyTextureRegion: 0.25 and 0x5A;
  • dropping every target and waiting 2 s, after which only the next placement's arena remains.

GPU runs went through our GPU queue, one test at a time (cargo test -p xcl --lib -- --exact hle::d3d12x::vram::tests::d32s8_targets_take_their_own_size_of_video_memory): 1 passed on 327c24c, 2.3 s.

cargo build -p xcl --lib --tests: no new warnings. Diff: 1 file, +256 / −140.

Not tested

  • AMD. It may refuse more depth formats as reserved, which would put more targets on this path. Aliased depth without an initializing clear may read garbage there, as today.
  • A retail run. The 2.8 GB → about 0.6 GB estimate assumes the title's D32S8 targets are 4K. Per-format counts aren't logged.
  • Aliasing across arenas. A D32S8 overlapping a target on pages, or a placement straddling an arena, doesn't share memory with it. Targets on pages never shared memory with arenas. Straddling arena placements used to share through unions, and that was the growth this removes.
  • The full workspace suite. Only the vram tests were run.

Overlap: no open PR changes vram.rs. The last change to arenas on main is 38b05b7 (target pages).

AI use: Claude Code, Claude Opus 5.5.

🤖 Generated with Claude Code

**@⁠Kalmarv** on GitHub · 2026-11-23 21:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/225) `Kalmarv:fix/d32s8-exact-heaps` → `main` · +256 −140 · 1 files · 1 commits · merged 2026-11-23 21:29 UTC This addresses an open item in HANDOFF.md ("Retail game bring-up"): *"arenas for D32S8 targets still take 2.8 GB (one per 256 MB of addresses; a D32S8 → R32 + separate stencil split, or exact-size heaps, would cut it)"*. It takes the exact-size heaps. ### What changes (`crates/xcl/src/hle/d3d12x/vram.rs` only) - **Arena size.** Arenas are what GPU-owned textures fall back to when the driver won't make them reserved, which on this RTX 5070 Ti is D32S8 / R32G8X24 (0x80070057) and MSAA. An arena used to be at least 256 MiB, on a 256 MiB boundary. Now it is a heap of its placement's own size, aligned to 64 KiB, or 4 MiB for MSAA. - **No unions.** - A placement inside an existing arena, at an offset the arena's alignment allows, shares it as before. The newest such arena wins. - Any other placement gets an arena of its own. So a placement that straddles arenas no longer makes a growing union arena while the old ones live on. That was the pattern behind the "15 GB of arenas in 2 s". - **Release.** Arenas count their resources (`vram::forget`, which `res_release` already calls). An arena no resource has used for 2 s leaves the list and its heap goes, like the 2 s return of target pages (`IDLE`). Placed again within 2 s, the same memory is reused, with what it holds. - **Unchanged:** - resource descriptions; - views: depth SRV plane 0, stencil SRV plane 1; - `CopyResource`, clears and DSVs; - the pages path; - `arena_bytes()` for `XCL_ACTIVITY`. - The arena bookkeeping (`Arenas<H>`) is separate from D3D12, so the CPU tests drive it with fake heaps. Why not the D32S8 → R32 + separate stencil split: D3D12 has no stencil-only depth format, and a DSV binds depth and stencil of one resource. A separate stencil resource can't be stencil-tested, and plane-1 views, copies, clears and pipeline formats would all need rewriting. ### Tested Windows 11, RTX 5070 Ti, NVIDIA 610.74, MSVC debug. Branch `327c24c` on main `af20b4b`. Controls on main `0cd0a53`, whose `vram.rs` is the same as `af20b4b`'s. CPU tests (`cargo test -p xcl --lib hle::d3d12x::vram::tests`, 4/4 pass): - `an_arena_is_shared_at_its_addresses_and_otherwise_its_placements_size`: an arena takes 70 MiB, not 256. The same address or a smaller placement inside shares it. A straddling placement, and an MSAA one needing 4 MiB, get their own. - `an_arena_no_resource_uses_goes_after_two_seconds`: an arena placed again within 2 s is reused; after 2 s it is gone. - `streamed_targets_stay_bounded`: the title's pattern from HANDOFF. Each round places 8 back-to-back 64 MiB targets over 3.5 GB, every other round at the last round's addresses or a third of a target further on, so the targets straddle. A target lives 1.5 s, and 400 rounds are run, 0.5 s apart. - `tile_ranges_per_heap`: unchanged. The same streaming scenario, with main's `plan()` and an arena replaced while its resources live keeping its memory, was run as a control (not in this PR): | arena bytes | main | this PR | |---|---|---| | peak | 7.25 GB | 2.15 GB | | max, rounds 20–120 | 3.49 GB | 1.68 GB | | max, rounds 300–399 | 3.76 GB (grows) | 1.68 GB (flat) | | test's bound (7 rounds of targets, 3.76 GB) | exceeded | held | GPU test `d32s8_targets_take_their_own_size_of_video_memory` (skips on WARP): | 14 D32S8 3840×2160 targets, 256 MiB apart | main (same test, control) | this PR | |---|---|---| | arena bytes | 3,758,096,384 (14 × 256 MiB): **fails** | 585,367,552 (14 × 41,811,968) | | local video memory, increase (`QueryVideoMemoryInfo`) | +3.76 GB | +590 MB | The same test also covers: - clearing one 64×64 target to depth 0.25 / stencil 0x5A, `CopyResource` to a second, and reading both planes back by `CopyTextureRegion`: 0.25 and 0x5A; - dropping every target and waiting 2 s, after which only the next placement's arena remains. GPU runs went through our GPU queue, one test at a time (`cargo test -p xcl --lib -- --exact hle::d3d12x::vram::tests::d32s8_targets_take_their_own_size_of_video_memory`): 1 passed on `327c24c`, 2.3 s. `cargo build -p xcl --lib --tests`: no new warnings. Diff: 1 file, +256 / −140. ### Not tested - **AMD.** It may refuse more depth formats as reserved, which would put more targets on this path. Aliased depth without an initializing clear may read garbage there, as today. - **A retail run.** The 2.8 GB → about 0.6 GB estimate assumes the title's D32S8 targets are 4K. Per-format counts aren't logged. - **Aliasing across arenas.** A D32S8 overlapping a target on pages, or a placement straddling an arena, doesn't share memory with it. Targets on pages never shared memory with arenas. Straddling arena placements used to share through unions, and that was the growth this removes. - The full workspace suite. Only the `vram` tests were run. Overlap: no open PR changes `vram.rs`. The last change to arenas on main is 38b05b7 (target pages). AI use: Claude Code, Claude Opus 5.5. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The arenas (for GPU-owned textures the driver will not make reserved: D32S8 /
R32G8X24, MSAA) were heaps of at least 256 MiB at 256 MiB boundaries, so the
retail game's D32S8 targets, spread over 3.5 GB of its addresses, took
2.8 GB (HANDOFF, "Retail game bring-up", open item). A placement that
straddled arenas made a union arena while the old ones lived on.

Now an arena is a heap of its placement's size (64 KiB, or 4 MiB for MSAA,
alignment). A placement at an arena's addresses (inside it, at an offset its
alignment allows) shares it; any other gets its own, so there are no unions.
Arenas count their resources and one unused for 2 s is let go, as target
pages are.

Tests: the arena bookkeeping on its own (sharing, sizes, the 2 s release,
and the title's streaming pattern of back-to-back targets placed again over
3.5 GB, which stays bounded), and on the GPU 14 D32S8 3840x2160 targets
256 MiB apart, a clear + CopyResource + read of both planes, and the release.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:16:31 +00:00
github manually merged commit 327c24c98e into gh-pr/225/base 2026-11-26 04:16:33 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-23 21:31 UTC · original

Merged (main 0f34407). Checked on an RX 9070 (RDNA4): cargo build --workspace && cargo test --workspace -- --test-threads=4 - 611 unit tests passed, 0 failed; the retail game still reaches its first menu at 30 fps. With it the arenas in the menu went from 3 072 MB to 311 MB and the process's commit from 18.7 GB to 15.2 GB - thanks!

**@⁠ivall** on GitHub · 2026-11-23 21:31 UTC · [original](https://github.com/xcl-dev/xcl/pull/225#issuecomment-7114583694) Merged (main 0f34407). Checked on an RX 9070 (RDNA4): `cargo build --workspace && cargo test --workspace -- --test-threads=4` - 611 unit tests passed, 0 failed; the retail game still reaches its first menu at 30 fps. With it the arenas in the menu went from 3 072 MB to 311 MB and the process's commit from 18.7 GB to 15.2 GB - thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.