WIP: d3d12: the title's graphics heap as private memory with a write-only twin in the card's memory (LEONIDA_HEAP_TWIN, off by default) #3399

Code

Clone this repository

Checkout

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

git fetch -u origin "feat/heap-twin:feat/heap-twin"
git switch "feat/heap-twin"
Draft
leonida/mandy
MoonEyes wants to merge 1 commit from feat/heap-twin 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/heap-twin:feat/heap-twin'
git switch 'feat/heap-twin'
Member

Branche : feat/heap-twin sur leonidanow (https://git.leonida.now/leonida/mandy), commit 4b9a9c1bdb, base main 312d88d1d4. Comparer : https://git.leonida.now/leonida/mandy/compare/main...feat/heap-twin

What it does

LEONIDA_HEAP_TWIN=1 (experimental, off by default; without the variable nothing changes) ports Darner's heap twin
(GPL-3.0-or-later, as this repository; crates/darner-gpu/src/heap_twin.rs and the private heap of its xmem),
adapted to this layer rather than copied.

Renderer port 1 (LEONIDA_PORT_CONSTANTS) opened the title's 32-bit graphics heap in place for the card and cost
290 ms a frame on an RTX 4060 Ti (d3d12/docs/findings.md, "Frame-rate switches measured on a six-core PC"): the
CPU's 31,000 fills a frame read memory opened for the card. The twin splits the two sides:

  • leonida-hle xmem: with the switch and the 1.0.2.1 heap (allocation type 0x42000, 0xb90_0000 bytes, 32-bit),
    the heap is plain private memory below 4 GiB, committed at once and write-watched. Its parts, splits
    (VirtualFree with MEM_PRESERVE_PLACEHOLDER/MEM_COALESCE_PLACEHOLDERS) and page mappings
    (XMemMapPhysicalPages, ...Scatter) inside it do nothing.
  • leonida-gpu/src/heap_twin.rs (new): the part after the heap's first 0xb0_0000 bytes (the 174 MB buffer at
    0x34e10000) gets a twin of the same size in a GPU upload heap (resizable BAR), made with the first buffer placed
    in it. Title buffers and windows there are placed in the twin through port::place_for, so the existing in-place
    Shadow path applies (nothing refreshed, sent up or fetched back for them).
  • Before each host submission (host.rs, both paths that call descriptor::make_waiting) the 4 KiB parts the CPU
    wrote since the last sync are copied up, from the existing write journal (watch::writes, watch::parts_written),
    by 2 MiB chunks the card reads, never mixing two askings in one copy. A thread of ours does the same each time
    the title submits (queue.rs). Our CPU buffer copies into the heap are also written through to the twin. The CPU
    never reads the card's memory.
  • A per-frame line gives syncs, syncs left out, parts copied, time, placements.

Not ported: the 256-byte constant buffers (DARNER_CBV_ALIGN, a patch of the title's code), the write sites, the
per-list stamps (here a sync is left out only when a complete one began after the title's latest submission: more
syncs, never fewer), the parallel sync, the twin's place in the card budget.

Risks (a wrong assumption shows as a wrong picture, not a crash)

  1. The title rewriting bytes a list still in flight reads: our submissions run behind the title's, and a sync
    copies newer bytes.
  2. A CPU-written 4 KiB part (64 KiB after one of our copies made under a submission's single asking) holding bytes
    shaders wrote and read later: overwritten by the CPU's (for example skinned faces pulled to a point).
  3. A CPU step of ours reading what shaders wrote in the heap: it reads the old value (never copied back).
  4. Needs a resizable BAR (GPU upload heaps); without one the heap stays private memory and is copied as any other.

Tests

  • cargo test -p leonida-gpu --locked (Windows 11, toolchain from rust-toolchain.toml): 738 passed, 0 failed,
    59 ignored. One unrelated test, pool::tests::a_texture_gives_its_piece_back_when_it_goes, failed once in one
    full run and passed in three runs alone and in the next full run (flaky, not touched here).
  • 9 new unit tests in heap_twin.rs: the heap's identity and twin bounds, off without the switch (no hot path
    does anything), range and chunk arithmetic, pieces of one epoch, joined 4 KiB copies, when a sync may be left out,
    and a sync over real write-watched memory that copies only the parts written since the last asking.
  • cargo check -p leonida-hle --locked --tests: clean.
  • python -I tools/d3d12_switches.py --out d3d12/docs/switches.md (one line added), note in d3d12/docs/findings.md.
  • python -I scripts/check_no_game_data.py on every touched file: clean.
  • The game was not run.

In-game A/B required

Not measured here. Darner's own figures (not ours): 71 to 59 ms a frame on an RTX 5080 (Series X, 1440p); its whole
set (twin, 256-byte CBVs, write sites) 14.1 to 16.3 fps on an RTX 5070.

Proposed recipe: resizable BAR on (the log says "card memory the CPU maps" supported = true and "a twin in the
card's memory" once), same save and place, same profile and launcher settings, runs of 600 s each in the order
A B B A (A: LEONIDA_HEAP_TWIN unset; B: LEONIDA_HEAP_TWIN=1; the heap is made once at start, so one run per arm,
not LEONIDA_AB). Compare frame median and p90, the card's busy time and the submitting thread's work per frame,
and the heap twin's per-frame line (syncs, MB copied, ms). Watch the recordings for the faults above (flicker,
objects a frame ahead, stretched skinned meshes). Merge only after a 300 s in-game run without device loss.

Credits and AI assistance

Mechanism from Darner (GPL-3.0-or-later), credited in the code comments and in the commit message.
AI-assisted: written with Claude Code (Claude Opus 5.5, claude-opus-5-5); reviewed by a separate Claude Opus 5.5
reviewer agent (first verdict REFUSE, its four points fixed or documented, then re-reviewed).

🤖 Generated with Claude Code

Branche : `feat/heap-twin` sur `leonidanow` (https://git.leonida.now/leonida/mandy), commit 4b9a9c1bdb, base `main` 312d88d1d4. Comparer : https://git.leonida.now/leonida/mandy/compare/main...feat/heap-twin ## What it does `LEONIDA_HEAP_TWIN=1` (experimental, off by default; without the variable nothing changes) ports Darner's heap twin (GPL-3.0-or-later, as this repository; `crates/darner-gpu/src/heap_twin.rs` and the private heap of its `xmem`), adapted to this layer rather than copied. Renderer port 1 (`LEONIDA_PORT_CONSTANTS`) opened the title's 32-bit graphics heap in place for the card and cost 290 ms a frame on an RTX 4060 Ti (`d3d12/docs/findings.md`, "Frame-rate switches measured on a six-core PC"): the CPU's 31,000 fills a frame read memory opened for the card. The twin splits the two sides: - `leonida-hle` `xmem`: with the switch and the 1.0.2.1 heap (allocation type 0x42000, 0xb90_0000 bytes, 32-bit), the heap is plain private memory below 4 GiB, committed at once and write-watched. Its parts, splits (`VirtualFree` with `MEM_PRESERVE_PLACEHOLDER`/`MEM_COALESCE_PLACEHOLDERS`) and page mappings (`XMemMapPhysicalPages`, `...Scatter`) inside it do nothing. - `leonida-gpu/src/heap_twin.rs` (new): the part after the heap's first 0xb0_0000 bytes (the 174 MB buffer at 0x34e10000) gets a twin of the same size in a GPU upload heap (resizable BAR), made with the first buffer placed in it. Title buffers and windows there are placed in the twin through `port::place_for`, so the existing in-place `Shadow` path applies (nothing refreshed, sent up or fetched back for them). - Before each host submission (`host.rs`, both paths that call `descriptor::make_waiting`) the 4 KiB parts the CPU wrote since the last sync are copied up, from the existing write journal (`watch::writes`, `watch::parts_written`), by 2 MiB chunks the card reads, never mixing two askings in one copy. A thread of ours does the same each time the title submits (`queue.rs`). Our CPU buffer copies into the heap are also written through to the twin. The CPU never reads the card's memory. - A per-frame line gives syncs, syncs left out, parts copied, time, placements. Not ported: the 256-byte constant buffers (`DARNER_CBV_ALIGN`, a patch of the title's code), the write sites, the per-list stamps (here a sync is left out only when a complete one began after the title's latest submission: more syncs, never fewer), the parallel sync, the twin's place in the card budget. ## Risks (a wrong assumption shows as a wrong picture, not a crash) 1. The title rewriting bytes a list still in flight reads: our submissions run behind the title's, and a sync copies newer bytes. 2. A CPU-written 4 KiB part (64 KiB after one of our copies made under a submission's single asking) holding bytes shaders wrote and read later: overwritten by the CPU's (for example skinned faces pulled to a point). 3. A CPU step of ours reading what shaders wrote in the heap: it reads the old value (never copied back). 4. Needs a resizable BAR (GPU upload heaps); without one the heap stays private memory and is copied as any other. ## Tests - `cargo test -p leonida-gpu --locked` (Windows 11, toolchain from `rust-toolchain.toml`): 738 passed, 0 failed, 59 ignored. One unrelated test, `pool::tests::a_texture_gives_its_piece_back_when_it_goes`, failed once in one full run and passed in three runs alone and in the next full run (flaky, not touched here). - 9 new unit tests in `heap_twin.rs`: the heap's identity and twin bounds, off without the switch (no hot path does anything), range and chunk arithmetic, pieces of one epoch, joined 4 KiB copies, when a sync may be left out, and a sync over real write-watched memory that copies only the parts written since the last asking. - `cargo check -p leonida-hle --locked --tests`: clean. - `python -I tools/d3d12_switches.py --out d3d12/docs/switches.md` (one line added), note in `d3d12/docs/findings.md`. - `python -I scripts/check_no_game_data.py` on every touched file: clean. - The game was not run. ## In-game A/B required Not measured here. Darner's own figures (not ours): 71 to 59 ms a frame on an RTX 5080 (Series X, 1440p); its whole set (twin, 256-byte CBVs, write sites) 14.1 to 16.3 fps on an RTX 5070. Proposed recipe: resizable BAR on (the log says "card memory the CPU maps" supported = true and "a twin in the card's memory" once), same save and place, same profile and launcher settings, runs of 600 s each in the order A B B A (A: `LEONIDA_HEAP_TWIN` unset; B: `LEONIDA_HEAP_TWIN=1`; the heap is made once at start, so one run per arm, not `LEONIDA_AB`). Compare frame median and p90, the card's busy time and the submitting thread's work per frame, and the heap twin's per-frame line (syncs, MB copied, ms). Watch the recordings for the faults above (flicker, objects a frame ahead, stretched skinned meshes). Merge only after a 300 s in-game run without device loss. ## Credits and AI assistance Mechanism from Darner (GPL-3.0-or-later), credited in the code comments and in the commit message. AI-assisted: written with Claude Code (Claude Opus 5.5, `claude-opus-5-5`); reviewed by a separate Claude Opus 5.5 reviewer agent (first verdict REFUSE, its four points fixed or documented, then re-reviewed). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Ported from Darner's heap twin (GPL-3.0-or-later, as this repository: crates/darner-gpu/src/heap_twin.rs and
the private heap of its darner-hle xmem), with attribution, adapted to this layer's write journal (watch),
port::place_for and the in-place Shadow rather than copied.

With LEONIDA_HEAP_TWIN=1 and the 1.0.2.1 heap (type 0x42000, 0xb90_0000 bytes), xmem makes the title's 32-bit
graphics heap plain private memory, committed and write-watched; its splits and page mappings do nothing. Its
part after 0xb0_0000 bytes gets a twin in a GPU upload heap (resizable BAR) where title buffers and windows are
placed. Before each host submission, and on a thread of ours as the title submits, the 4 KiB parts the CPU wrote
since are copied up; the CPU never reads the card's memory. Without the variable nothing changes.

Not ported: Darner's 256-byte constant buffers (a patch of the title's code), write sites, per-list stamps,
parallel sync. Not measured in game: an A/B is required (findings.md).

Tests: cargo test -p leonida-gpu --locked (738 passed, 9 new in heap_twin), cargo check -p leonida-hle --tests,
check_no_game_data on the touched files. AI-assisted (Claude Opus 5.5), reviewed by a separate reviewer agent.

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
Collaborator

A/B on an RTX 3090 (NVIDIA 621.40, Windows 11 Pro build 26300, Resizable BAR on), executable of this pull request's head 4b9a9c1bd (diag profile, diagnostics,dlss,mimalloc, msvc target), the same executable in every run. Series X output 2560x1440, DLSS quality, no frame generation, 600 s standing at the save, fresh data for each run. A = default; B = LEONIDA_HEAP_TWIN=1. Run order A1, B1, B2 (A2 dropped: A1 already matches the main reference runs of the same night, 33.0-33.6 frames/s).

frames/s, 150-600 s median frame p99 card busy card's work a frame (median frame minus card idle)
A1, default 34.2 28.7 ms 49.0 ms 98 % 28.2 ms
B1, twin on 20.6 48.5 ms 87.3 ms 99 % 48.2 ms
B2, twin on 20.4 48.8 ms 88.7 ms 99 % 48.5 ms
  • The twin costs about 40 % of the frame rate here, and it is the card that pays it: about +20 ms of card work a frame. It is steady: every 30 s slice from 150 s to 600 s of B1 is between 20.2 and 20.8 frames/s.
  • The twin is made, with the title's first buffer placed in it. Its own line, median over the run: 0.0 syncs a frame (19.3 skipped as covered), about 1,890 pieces of 4 KiB (7.4 MB), 0.49 ms.
  • On the CPU side it does what it says: the time the submitting thread spends on the title's bytes under the writes in flight goes from 3.8 to 2.7 ms a frame, and refreshes from 3.0 to 2.4 ms. But the submitting thread then waits for the card (its work goes from 24.9 to 38 ms a frame).
  • Likely reading: the title's shaders now read that heap from the upload heap (BAR) instead of video memory, which is slower for this card. Not measured per pass (no GPU timing in these runs).
  • Captures at 150, 300 and 450 s, A against B, compared by eye: the same scene, no visible fault (no flicker, nothing a frame ahead, no stretched faces or bodies seen in the stills).
  • 0 device lost, 0 crash, 0 ERROR lines in all three runs.

Verdict: default path fine (A1 is at the main level). With the switch on, a large regression on the RTX 3090; it should stay off by default, at least on this card. (It was taken out of the beta defaults on this measurement.)

GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40.
AI-assisted: runs and report by Claude Code (Claude Opus 5.5).

A/B on an RTX 3090 (NVIDIA 621.40, Windows 11 Pro build 26300, Resizable BAR on), executable of this pull request's head `4b9a9c1bd` (diag profile, `diagnostics,dlss,mimalloc`, msvc target), the same executable in every run. Series X output 2560x1440, DLSS quality, no frame generation, 600 s standing at the save, fresh data for each run. A = default; B = `LEONIDA_HEAP_TWIN=1`. Run order A1, B1, B2 (A2 dropped: A1 already matches the `main` reference runs of the same night, 33.0-33.6 frames/s). | | frames/s, 150-600 s | median frame | p99 | card busy | card's work a frame (median frame minus card idle) | |---|---|---|---|---|---| | A1, default | 34.2 | 28.7 ms | 49.0 ms | 98 % | 28.2 ms | | B1, twin on | 20.6 | 48.5 ms | 87.3 ms | 99 % | 48.2 ms | | B2, twin on | 20.4 | 48.8 ms | 88.7 ms | 99 % | 48.5 ms | - **The twin costs about 40 % of the frame rate here, and it is the card that pays it: about +20 ms of card work a frame.** It is steady: every 30 s slice from 150 s to 600 s of B1 is between 20.2 and 20.8 frames/s. - The twin is made, with the title's first buffer placed in it. Its own line, median over the run: 0.0 syncs a frame (19.3 skipped as covered), about 1,890 pieces of 4 KiB (7.4 MB), 0.49 ms. - On the CPU side it does what it says: the time the submitting thread spends on the title's bytes under the writes in flight goes from 3.8 to 2.7 ms a frame, and refreshes from 3.0 to 2.4 ms. But the submitting thread then waits for the card (its work goes from 24.9 to 38 ms a frame). - Likely reading: the title's shaders now read that heap from the upload heap (BAR) instead of video memory, which is slower for this card. Not measured per pass (no GPU timing in these runs). - Captures at 150, 300 and 450 s, A against B, compared by eye: the same scene, no visible fault (no flicker, nothing a frame ahead, no stretched faces or bodies seen in the stills). - 0 device lost, 0 crash, 0 ERROR lines in all three runs. Verdict: default path fine (A1 is at the `main` level). With the switch on, a large regression on the RTX 3090; it should stay off by default, at least on this card. (It was taken out of the beta defaults on this measurement.) GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40. AI-assisted: runs and report by Claude Code (Claude Opus 5.5).
First-time contributor

Independent RTX 3090 fallback/stability check for head 4b9a9c1bdbf7dea86a2e87bc8393f17a695618a2, applied to main e1a13ab251de832eb07fd78bad9e95a62149bf39.

Host: NVIDIA RTX 3090 24 GiB, driver 610.88, Windows 11, Ryzen 9 5900X, 32 GiB RAM. Rust 1.98.1 MSVC; diagnostics,dlss,mimalloc; Series X, DLSS quality, frame generation/precompile disabled. Both runs used the same candidate executable, separate processes, stock game gate and freeroam driver, matched initial pipeline seeds and the same memory stops. The disabled arm is the PR executable with LEONIDA_HEAP_TWIN=0, rather than a separate plain-main binary.

  • Disabled: stock PASS, 304.010 seconds in world, actual exit 0, target stop, cache saved and title suspended. Native placement confirmed; 22 subsequent marks stayed within 0.2 m, with 216.660 seconds from first mark to stop.
  • Enabled: stock PASS, 305.364 seconds in world, actual exit 0, target stop, cache saved and title suspended. Private write-watched graphics-heap creation and the explicit no-twin fallback were observed. Control/teleport and subsequent placement marks never completed, so this arm does not establish the intended test location.

The runtime reported no usable GPU upload heap. A separate device-only OPTIONS16 query after the game exited returned S_OK and GPUUploadHeapSupported=0 on the RTX 3090, using the same system D3D12Core path as the game (version 10.0.26100.9818). No twin was created and no active-twin placements or copy counters were exercised. The single “not placed” warning is the fallback diagnostic, not an active-twin refusal count; active-path statistics are unavailable. This is fallback stability coverage. It establishes neither twin correctness nor a performance/memory improvement. The differing placement/workload, one run per mode and shared driver shader cache also prevent effect attribution. Neither run proves 300 seconds at the destination. No logged ERROR, crash or device loss in either completed run; no forced termination.

Before live execution: full eight-file diff/build-script audit, tracked/new-file clean-room scans and diff check passed. Local tests passed: heap-twin 9; default GPU 739 (59 ignored); diagnostics GPU 820 (59 ignored, device-backed depth-bounds included); diagnostics XMem 28 (1 ignored); diagnostics replay 1; optimized diagnostic build. The pure heap tests do not validate live alias/remap or GPU-write coherence assumptions.

One earlier preparation attempt was stopped cleanly before reaching world because its actions log inherited a system-drive temp path. It is excluded above; both completed arms used matched scratch-drive TEMP/TMP and verified actual actions-log placement.

AI-assisted: GPT-6.1 Sol xhigh for the testing worker and independent source/numerical verification. No approval or merge recommendation from this limited coverage.

Independent RTX 3090 fallback/stability check for head `4b9a9c1bdbf7dea86a2e87bc8393f17a695618a2`, applied to main `e1a13ab251de832eb07fd78bad9e95a62149bf39`. Host: NVIDIA RTX 3090 24 GiB, driver 610.88, Windows 11, Ryzen 9 5900X, 32 GiB RAM. Rust 1.98.1 MSVC; `diagnostics,dlss,mimalloc`; Series X, DLSS quality, frame generation/precompile disabled. Both runs used the same candidate executable, separate processes, stock game gate and freeroam driver, matched initial pipeline seeds and the same memory stops. The disabled arm is the PR executable with `LEONIDA_HEAP_TWIN=0`, rather than a separate plain-main binary. - Disabled: stock PASS, 304.010 seconds in world, actual exit 0, target stop, cache saved and title suspended. Native placement confirmed; 22 subsequent marks stayed within 0.2 m, with 216.660 seconds from first mark to stop. - Enabled: stock PASS, 305.364 seconds in world, actual exit 0, target stop, cache saved and title suspended. Private write-watched graphics-heap creation and the explicit no-twin fallback were observed. Control/teleport and subsequent placement marks never completed, so this arm does not establish the intended test location. The runtime reported no usable GPU upload heap. A separate device-only OPTIONS16 query after the game exited returned `S_OK` and `GPUUploadHeapSupported=0` on the RTX 3090, using the same system D3D12Core path as the game (version 10.0.26100.9818). No twin was created and no active-twin placements or copy counters were exercised. The single “not placed” warning is the fallback diagnostic, not an active-twin refusal count; active-path statistics are unavailable. This is fallback stability coverage. It establishes neither twin correctness nor a performance/memory improvement. The differing placement/workload, one run per mode and shared driver shader cache also prevent effect attribution. Neither run proves 300 seconds at the destination. No logged ERROR, crash or device loss in either completed run; no forced termination. Before live execution: full eight-file diff/build-script audit, tracked/new-file clean-room scans and diff check passed. Local tests passed: heap-twin 9; default GPU 739 (59 ignored); diagnostics GPU 820 (59 ignored, device-backed depth-bounds included); diagnostics XMem 28 (1 ignored); diagnostics replay 1; optimized diagnostic build. The pure heap tests do not validate live alias/remap or GPU-write coherence assumptions. One earlier preparation attempt was stopped cleanly before reaching world because its actions log inherited a system-drive temp path. It is excluded above; both completed arms used matched scratch-drive TEMP/TMP and verified actual actions-log placement. AI-assisted: GPT-6.1 Sol xhigh for the testing worker and independent source/numerical verification. No approval or merge recommendation from this limited coverage.
MoonEyes changed title from d3d12: the title's graphics heap as private memory with a write-only twin in the card's memory (LEONIDA_HEAP_TWIN, off by default) to WIP: d3d12: the title's graphics heap as private memory with a write-only twin in the card's memory (LEONIDA_HEAP_TWIN, off by default) 2026-12-04 07:14:40 +00:00
Author
Member

Moved to WIP. Measured on an RTX 3090 (Series X 1440p, DLSS quality, resizable BAR on, 600 s runs): with LEONIDA_HEAP_TWIN=1 the frame rate drops by about 40 % (20.4 and 20.6 fps against 34.2 without), about 20 ms more GPU work a frame; no image fault, no device loss. The default path (variable unset) matches main.

Likely cause: the title's descriptor heap is then read by shaders from the upload heap (BAR), which is slow for the GPU. VIctory-SandBar keeps that part in RAM for the same reason. A next version would keep the descriptor heap in RAM and twin only the data the GPU reads rarely. Not to be merged switched on as is.

🤖 Generated with Claude Code

Moved to WIP. Measured on an RTX 3090 (Series X 1440p, DLSS quality, resizable BAR on, 600 s runs): with `LEONIDA_HEAP_TWIN=1` the frame rate drops by about 40 % (20.4 and 20.6 fps against 34.2 without), about 20 ms more GPU work a frame; no image fault, no device loss. The default path (variable unset) matches main. Likely cause: the title's descriptor heap is then read by shaders from the upload heap (BAR), which is slow for the GPU. VIctory-SandBar keeps that part in RAM for the same reason. A next version would keep the descriptor heap in RAM and twin only the data the GPU reads rarely. Not to be merged switched on as is. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No description provided.