Vulkan: Correct page-mirror memory placement accounting #3160

Open
BadgersMC wants to merge 1 commit from upstream-pr/3160 into main
BadgersMC commented 2026-12-02 15:15:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by BadgersMC on 2026-12-02T15:15:59Z.


Claimed in the coordination issue as: RT-VULKAN-MIRROR-ACTUAL-PLACEMENT-ACCOUNTING

What and why

Page-mirror accounting used the preferred placement rather than the allocation's actual memory flags, and counted allocations even when mapping subsequently failed. Record only successfully mapped units, using the selected memory type's DEVICE_LOCAL flag. Preserve memory type selection, allocation/free behavior and synchronization. The placement-transition message now distinguishes actual placement from policy preference. This is a telemetry correctness fix, not a measured RAM/VRAM reduction.

Tested

  • Scratch callback mock red/green: baseline 3/6, candidate 6/6. Integrated callback with the actual helper: 6/6 (allocation success/failure, fallback and map failures).
  • MSVC 2022 x64, Windows: compiled and ran d3d12_state_map_test.cpp directly with Vulkan headers; normal and --bounded-index-view modes both exited 0. Added committed helper regression tests for actual flags, successful transitions and failed maps. Scratch harness is not committed.
  • git diff --check and python -I scripts/check_no_game_data.py on all three changed paths passed.
  • Independent read-only Claude Opus 5.5 review approved code; remaining validation requests: full MSVC runtime/shim build and contributor GPU validation. No full runtime build or production sparse mirror test claimed; see additional validation below.

GPUs tested: RTX 3060 Ti (standalone actual Vulkan allocation/mapping; not production sparse mirror).

AI-assisted: yes ΓÇö GPT-6.2 (implementation and testing), Claude Opus 5.5 (independent read-only review).

Contributor testing requested

If available, please test NVIDIA/AMD with ReBAR on/off, especially constrained VRAM that causes host fallback. Compare mirror unit counts with actual placement transition logs and total allocated mirror units. Include GPU, driver, OS, commit, and sanitized synthetic results only. A compile/CI check of the shim would also help. Do not include game data or raw game-derived logs.

Checklist

  • Based on current main at preparation (83046a713f).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption.
  • Translator rule not applicable (no translator changes).

Additional validation (rebased head 16d3fb288)

All five CI checks passed after the rebase incorporating #3162. Thank you to @merzleckeier and @fiestorra for the independent Linux and Windows checks.

Standalone synthetic Vulkan allocation probe, Windows / RTX 3060 Ti / NVIDIA 610.88 (raw driver 0x98960000), ReBAR predicate false: one 32 MiB allocation at a time, actual vkAllocateMemory and vkMapMemory, CPU page-stride write/readback. Using the committed memory_type_order and MirrorUnitAccounting helpers, three cases passed: local preference -> type 5 (flags 0x7); deliberately restricted compatible types with local preference -> host type 3 (0x6), which the old preferred-policy counter would misclassify; host preference -> type 4 (0xe). Failed-map behavior was tested synthetically, not by making an invalid Vulkan call.

Limitations: these allocations were tested with ordinary buffers (mask 0x3b), not the production mirror callback or sparse binding. The separately queried synthetic sparse buffer (32 MiB, broad-use flags) reported mask 0x2 and no host-visible coherent compatible type. No game boot, sparse bind, GPU shader execution, or memory reduction claimed. No code change since the rebase.

Remaining requests: explicit full C++ Vulkan runtime/shim build confirmation; a compatible GPU/driver with host-visible coherent sparse buffer types to validate the actual mirror path (please report buffer requirements mask, selected memory flags, successful unit counts, OS/driver, and synthetic test results). Please do not exhaust VRAM or include game data. AMD/NVIDIA with and without ReBAR are useful comparisons. AI-assisted: GPT-6.2.

Independent Windows regression verification (16d3fb288)

@fiestorra confirmed a fresh native MSVC 17.14 Ninja Release build and execution of the state-map test. The committed test passed (exit 0); a deliberate negative control ignoring map results and counting every unit as local failed at the new accounting assertion (exit 1). This verifies regression sensitivity, not a full shim build or a production Vulkan sparse mirror run. Moving to ready for review with those validation requests still open; no merge or measured memory savings claimed. AI-assisted: GPT-6.2.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5609332049 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3160). Originally posted by [BadgersMC](https://github.com/BadgersMC) on 2026-12-02T15:15:59Z. --- Claimed in the coordination issue as: [RT-VULKAN-MIRROR-ACTUAL-PLACEMENT-ACCOUNTING](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7349641340) ### What and why Page-mirror accounting used the preferred placement rather than the allocation's actual memory flags, and counted allocations even when mapping subsequently failed. Record only successfully mapped units, using the selected memory type's DEVICE_LOCAL flag. Preserve memory type selection, allocation/free behavior and synchronization. The placement-transition message now distinguishes actual placement from policy preference. This is a telemetry correctness fix, not a measured RAM/VRAM reduction. ### Tested - Scratch callback mock red/green: baseline 3/6, candidate 6/6. Integrated callback with the actual helper: 6/6 (allocation success/failure, fallback and map failures). - MSVC 2022 x64, Windows: compiled and ran `d3d12_state_map_test.cpp` directly with Vulkan headers; normal and `--bounded-index-view` modes both exited 0. Added committed helper regression tests for actual flags, successful transitions and failed maps. Scratch harness is not committed. - `git diff --check` and `python -I scripts/check_no_game_data.py` on all three changed paths passed. - Independent read-only Claude Opus 5.5 review approved code; remaining validation requests: full MSVC runtime/shim build and contributor GPU validation. No full runtime build or production sparse mirror test claimed; see additional validation below. GPUs tested: RTX 3060 Ti (standalone actual Vulkan allocation/mapping; not production sparse mirror). AI-assisted: yes ΓÇö GPT-6.2 (implementation and testing), Claude Opus 5.5 (independent read-only review). ### Contributor testing requested If available, please test NVIDIA/AMD with ReBAR on/off, especially constrained VRAM that causes host fallback. Compare mirror unit counts with actual placement transition logs and total allocated mirror units. Include GPU, driver, OS, commit, and sanitized synthetic results only. A compile/CI check of the shim would also help. Do not include game data or raw game-derived logs. ### Checklist - [x] Based on current `main` at preparation (83046a713f555db1a8f6d47d2c5746a48409568c). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption. - [ ] Translator rule not applicable (no translator changes). ### Additional validation (rebased head 16d3fb288) All five CI checks passed after the rebase incorporating #3162. Thank you to @merzleckeier and @fiestorra for the independent Linux and Windows checks. Standalone synthetic Vulkan allocation probe, Windows / RTX 3060 Ti / NVIDIA 610.88 (raw driver 0x98960000), ReBAR predicate false: one 32 MiB allocation at a time, actual vkAllocateMemory and vkMapMemory, CPU page-stride write/readback. Using the committed memory_type_order and MirrorUnitAccounting helpers, three cases passed: local preference -> type 5 (flags 0x7); deliberately restricted compatible types with local preference -> host type 3 (0x6), which the old preferred-policy counter would misclassify; host preference -> type 4 (0xe). Failed-map behavior was tested synthetically, not by making an invalid Vulkan call. Limitations: these allocations were tested with ordinary buffers (mask 0x3b), not the production mirror callback or sparse binding. The separately queried synthetic sparse buffer (32 MiB, broad-use flags) reported mask 0x2 and no host-visible coherent compatible type. No game boot, sparse bind, GPU shader execution, or memory reduction claimed. No code change since the rebase. Remaining requests: explicit full C++ Vulkan runtime/shim build confirmation; a compatible GPU/driver with host-visible coherent sparse buffer types to validate the actual mirror path (please report buffer requirements mask, selected memory flags, successful unit counts, OS/driver, and synthetic test results). Please do not exhaust VRAM or include game data. AMD/NVIDIA with and without ReBAR are useful comparisons. AI-assisted: GPT-6.2. ### Independent Windows regression verification (16d3fb288) @fiestorra [confirmed a fresh native MSVC 17.14 Ninja Release build and execution of the state-map test](https://github.com/ProjectLeonida/mandy/pull/3160#issuecomment-7362520264). The committed test passed (exit 0); a deliberate negative control ignoring map results and counting every unit as local failed at the new accounting assertion (exit 1). This verifies regression sensitivity, not a full shim build or a production Vulkan sparse mirror run. Moving to ready for review with those validation requests still open; no merge or measured memory savings claimed. AI-assisted: GPT-6.2.
BadgersMC commented 2026-12-02 15:32:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by BadgersMC on 2026-12-02T15:32:47Z.


CI triage: the Windows check reported 1 failed file out of 660: tests/test_xcl_compare.py, specifically OutputEOF.test_live_title_after_eof_returns_hung_check_for_restart. The traceback is a TemporaryDirectory cleanup failure (WinError 32 followed by WinError 5), before the functional assertions, not a Vulkan assertion.

Both the test (blob 37667fed) and tools/xcl_compare.py (blob 2acec8f4) are byte-identical at base 83046a7 and head fd9e0c2. The exact base Windows CI passed this file (20 tests). Local Windows Python 3.12.10 A/B: 20/20 passed at both revisions, with five additional targeted repetitions passing on base. This suggests an intermittent Windows cleanup/handle-release issue; the specific handle owner is unconfirmed.

No source or test changes were made in response. Could a maintainer rerun the failed Windows job on the same head? Linux and semantic jobs passed. Keeping this PR scoped to mirror accounting.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7350507900 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3160#issuecomment-7350507900). Originally posted by [BadgersMC](https://github.com/BadgersMC) on 2026-12-02T15:32:47Z. --- CI triage: the Windows check reported 1 failed file out of 660: `tests/test_xcl_compare.py`, specifically `OutputEOF.test_live_title_after_eof_returns_hung_check_for_restart`. The traceback is a TemporaryDirectory cleanup failure (WinError 32 followed by WinError 5), before the functional assertions, not a Vulkan assertion. Both the test (blob `37667fed`) and `tools/xcl_compare.py` (blob `2acec8f4`) are byte-identical at base `83046a7` and head `fd9e0c2`. The exact base Windows CI passed this file (20 tests). Local Windows Python 3.12.10 A/B: 20/20 passed at both revisions, with five additional targeted repetitions passing on base. This suggests an intermittent Windows cleanup/handle-release issue; the specific handle owner is unconfirmed. No source or test changes were made in response. Could a maintainer rerun the failed Windows job on the same head? Linux and semantic jobs passed. Keeping this PR scoped to mirror accounting.
merzleckeier commented 2026-12-02 15:45:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-02T15:45:07Z.


Independent CPU-only check of fd9e0c2 against main 3c106eb2f. The PR merges cleanly. I compiled runtime/test/d3d12_state_map_test.cpp with Clang on Linux and ran normal and --bounded-index-view modes on both trees. All four runs exited 0. The PR's new assertions cover actual host placement and failed maps. In the production callback, the actual allocation flags feed the accounting and a failed map still frees the allocation. This check covered the standalone C++ suite and source, with no GPU or full shim build.

I confirmed the Windows WinError 32 in the fixture's directory teardown and opened #3162 separately. All 20 XCL comparison tests passed there on Linux and Windows CPython 3.12.10 under Wine. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7350857480 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3160#issuecomment-7350857480). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-02T15:45:07Z. --- Independent CPU-only check of `fd9e0c2` against `main` `3c106eb2f`. The PR merges cleanly. I compiled `runtime/test/d3d12_state_map_test.cpp` with Clang on Linux and ran normal and `--bounded-index-view` modes on both trees. All four runs exited 0. The PR's new assertions cover actual host placement and failed maps. In the production callback, the actual allocation flags feed the accounting and a failed map still frees the allocation. This check covered the standalone C++ suite and source, with no GPU or full shim build. I confirmed the Windows `WinError 32` in the fixture's directory teardown and opened #3162 separately. All 20 XCL comparison tests passed there on Linux and Windows CPython 3.12.10 under Wine. AI-assisted with OpenAI Codex.
fiestorra commented 2026-12-02 16:18:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T16:18:01Z.


Built it and ran its test on this machine: the change compiles and d3d12_state_map_test passes.

Build (the change applied on main, MSVC, RelWithDebInfo, 4.8 minutes):

[237/448] Building CXX object CMakeFiles\d3d12_state_map_test.dir\test\d3d12_state_map_test.cpp.obj
[424/448] Linking CXX executable d3d12_state_map_test.exe
exit 0

Test (built from your tree):

d3d12_state_map_test.exe     exit 0
  state map, enhanced barriers, subresource decoding, layout tracking, discard ranges, block formats, BAR d...
  [rt] indirect graphics: recorded=0 emitted=9 record_skips=0 replay_skips=16 [topology=1, first_instance=1 ...

What that establishes: the accounting change compiles against the current tree and does not disturb the state-map suite - state map, enhanced barriers, subresource decoding, layout tracking, discard ranges, block formats all ran, and the executable exits 0.

What it does not establish, and I would rather say it than let the green line stand for more:

  • that the new accounting is right. The test passes on the tree with the change; it was not run against main alone (your branch also changes the test), so this is a green, not a red and green;
  • where the preferred-placement reading and the actual flags differ in practice - that needs a device with a memory type where they differ, and the assertion is about our own bookkeeping, not about a card's behaviour;
  • a game run: none.

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

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7351740808 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3160#issuecomment-7351740808). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T16:18:01Z. --- **Built it and ran its test on this machine: the change compiles and `d3d12_state_map_test` passes.** **Build** (the change applied on `main`, MSVC, `RelWithDebInfo`, 4.8 minutes): ``` [237/448] Building CXX object CMakeFiles\d3d12_state_map_test.dir\test\d3d12_state_map_test.cpp.obj [424/448] Linking CXX executable d3d12_state_map_test.exe exit 0 ``` **Test** (built from your tree): ``` d3d12_state_map_test.exe exit 0 state map, enhanced barriers, subresource decoding, layout tracking, discard ranges, block formats, BAR d... [rt] indirect graphics: recorded=0 emitted=9 record_skips=0 replay_skips=16 [topology=1, first_instance=1 ... ``` **What that establishes**: **the accounting change compiles against the current tree and does not disturb the state-map suite** - state map, enhanced barriers, subresource decoding, layout tracking, discard ranges, block formats all ran, and the executable exits 0. **What it does not establish**, and I would rather say it than let the green line stand for more: - **that the new accounting is right.** The test passes on the tree **with** the change; **it was not run against `main` alone** (your branch also changes the test), **so this is a green, not a red and green**; - **where the preferred-placement reading and the actual flags differ in practice** - that needs a device with a memory type where they differ, and **the assertion is about our own bookkeeping, not about a card's behaviour**; - **a game run**: none. **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
fiestorra commented 2026-12-02 23:52:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T23:52:04Z.


VERIFY-3160 — Windows nativo, C++ de verdad: build, ejecución y control

Own commit 16d3fb288 — runtime/src/vulkan_memory.h (+16), runtime/shims/d3d12_vulkan_device.cpp (41), runtime/test/d3d12_state_map_test.cpp (+19)

Built and ran it here (fresh Ninja/Release build in a worktree at the head, MSVC Build Tools 17.14):

cmake -S runtime -B build -G Ninja -DCMAKE_BUILD_TYPE=Release      Configuring done (13.7s)
cmake --build build --target d3d12_state_map_test                  [3/3] Linking CXX executable
build\d3d12_state_map_test.exe                                     exit 0

Negative control: I made MirrorUnitAccounting::record ignore the map result and count every unit as device-local ((void)map_result; const bool local = true;), rebuilt and re-ran:

exit 1
line 996: accounting.units_local == 1 && accounting.units_host == 0 && accounting.last_local

It bites on the exact line the pull request adds, and that is the point: the accounting must count what the driver actually gave (a local preference may fall back to host memory) and must not report a placement transition on a failed map.

What the change is: the mirror units are counted by actual memory placement, not by the preferred kind, and a failed vkMapMemory no longer counts as a transition. Both are "the counter says a mirror and the memory is somewhere else" — which is the bug this replaces.

Why this one is worth the build: it is the only C++ change I have verified tonight by running the binary, and d3d12_state_map_test.exe is host-side, so it needs no Vulkan device to pass — which is why the control works here. On a machine with no GPU driver it would still be meaningful.

What I could not test

  • The shim against a real Vulkan device: this test is the accounting, not a device.
  • Any title — no game.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7362520264 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3160#issuecomment-7362520264). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T23:52:04Z. --- ## VERIFY-3160 — Windows nativo, **C++ de verdad**: build, ejecución y control | | | |---|---| | Own commit | `16d3fb288` — `runtime/src/vulkan_memory.h` (+16), `runtime/shims/d3d12_vulkan_device.cpp` (41), `runtime/test/d3d12_state_map_test.cpp` (+19) | **Built and ran it here** (fresh `Ninja`/`Release` build in a worktree at the head, MSVC Build Tools 17.14): ``` cmake -S runtime -B build -G Ninja -DCMAKE_BUILD_TYPE=Release Configuring done (13.7s) cmake --build build --target d3d12_state_map_test [3/3] Linking CXX executable build\d3d12_state_map_test.exe exit 0 ``` **Negative control**: I made `MirrorUnitAccounting::record` ignore the map result and count every unit as device-local (`(void)map_result; const bool local = true;`), rebuilt and re-ran: ``` exit 1 line 996: accounting.units_local == 1 && accounting.units_host == 0 && accounting.last_local ``` **It bites on the exact line the pull request adds**, and that is the point: the accounting must **count what the driver actually gave** (a local preference may fall back to host memory) **and must not report a placement transition on a failed map**. **What the change is**: the mirror units are counted **by actual memory placement**, not by the preferred kind, and **a failed `vkMapMemory` no longer counts as a transition**. **Both are "the counter says a mirror and the memory is somewhere else"** — which is the bug this replaces. **Why this one is worth the build**: **it is the only C++ change I have verified tonight by running the binary**, and `d3d12_state_map_test.exe` is host-side, so it needs no Vulkan device to pass — **which is why the control works here**. On a machine with no GPU driver it would still be meaningful. ## What I could not test - **The shim against a real Vulkan device**: this test is the accounting, not a device. - **Any title** — no game. Written with an AI assistant.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3160:upstream-pr/3160'
git switch 'upstream-pr/3160'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3160'
git switch 'upstream-pr/3160'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3160'
git switch 'upstream-pr/3160'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3160'
git switch 'main'
git merge --squash 'upstream-pr/3160'
git switch 'main'
git merge --ff-only 'upstream-pr/3160'
git switch 'main'
git merge 'upstream-pr/3160'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!3160
No description provided.