[PR #2163] [MERGED] d3d12: a slot-133 shadow keeps the GPU's writes where the title's bytes did not change (RT-SHADOW-KEEP-GPU-WRITES) #2166

Closed
opened 2026-11-26 21:20:16 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2163
Author: @DadaCap
Created: 11/26/2026
Status: ✅ Merged
Merged: 11/26/2026
Merged by: @aslan2142

Base: main ← Head: shadow-keep-gpu-writes


📝 Commits (5)

  • 0ebd428 d3d12: a slot-133 shadow keeps the GPU's writes where the title's bytes did not change (LEONIDA_SHADOW_KEEP)
  • 4d811fc Merge remote-tracking branch 'origin/main' into shadow-keep-gpu-writes
  • d6b97a9 Merge main into shadow-keep-gpu-writes: shadow_keep and console_heap_copy tests side by side in CMakeLists
  • 71f54be shadow keep: a newly filled target brings its pieces to the other targets and the sums (review by lixonik)
  • b59ff42 Merge main into shadow-keep-gpu-writes: main removed the console heap mirror test (#2370); the shadow_keep test stays

📊 Changes

7 files changed (+374 additions, -4 deletions)

View changed files

📝 docs/d3d12_vulkan.md (+9 -0)
📝 runtime/CMakeLists.txt (+12 -0)
📝 runtime/README.md (+1 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+77 -4)
➕ runtime/src/shadow_keep.h (+72 -0)
📝 runtime/test/d3d12_title_va_test.cpp (+62 -0)
➕ runtime/test/shadow_keep_test.cpp (+141 -0)

📄 Description

Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7178046531

Depends on: none. It merges cleanly with current main (which now includes #2098), and conflicts with #1897 and #1867. #1897 moves ShadowPages to runtime/src/shadow_pages.h, so its d3d12_vulkan_device.cpp hunks conflict. #1867 conflicts only in its runtime/README.md row next to this one. Whichever lands second redoes those lines. It merges cleanly with #2032 and #2129.

What and why

ShadowPages copies the title's pages into the slot-133 shadow buffers and title windows before every submission. Some pages are copied at every refresh without any sign that the title wrote them: pages xmem maps at two addresses, pages a guard could not be put on, the 64 KiB block reopened around a written page, write-watched pages and pages pinned for a kernel write. A copy of such a page also overwrote whatever a shader had stored in the shadow since the last refresh, with the title's unchanged bytes.

Seen in the retail title: a 16-byte buffer that a compute program writes for the next frame to read was copied over by the title's old bytes before that frame, so it read back as zero every other frame and half the frames came out black.

The fix:

  • runtime/src/shadow_keep.h (new, portable): a 64-bit sum per 256-byte piece of the title's address space, taken from the bytes last copied. copy_changed compares the title's current bytes with those sums and copies only the pieces that differ. The first and last pieces of a range can be partial. forget clears sums so their pieces get copied again.
  • ShadowPages keeps the sums (sums_, per piece from its first page; grown by extend, carried over by absorb). With LEONIDA_SHADOW_KEEP on (the default), every copy job reads the title's page once into a buffer (still through the guarded copy_run) and passes on only the changed pieces to each target. A page that is zeroed because it went away gets its sums forgotten, so it is copied in full once it comes back. A new target's first copy (fill) is unchanged.
  • LEONIDA_SHADOW_KEEP=0 restores the old behaviour (every job copied whole). There is a row for it in runtime/README.md and a paragraph in docs/d3d12_vulkan.md under the copies.

Limits:

  • A title that writes the same bytes again to reset a value the GPU changed is not seen: the sum still matches, so the GPU's value stays. Fixing that needs the GPU's writes to reach the title's pages (the write-back work in #2032 / #2129, which covers other pages: #2129 drops the GPU's result exactly on the pages that are not guarded or clean, which are the ones this change is about).
  • Every copied page is now read once into a 4 KiB buffer and hashed before the changed pieces are copied out. The sum runs four independent lanes (about 30 GB/s on the test machine's CPU against about 24 GB/s for a plain copy; a first, chained version ran at 13 GB/s and made the refresh five times slower in the game, so the speed matters). The copies are then written only where the title changed, which is most of the gain below.
  • A 64-bit sum can collide, in which case one change is missed until the next one.

Tested

  • New shadow_keep_test (ctest shadow_keep, CPU only, no GPU and no game data). The title and the copy are arrays with made-up bytes:

    • a GPU write into the copy over unchanged title bytes is kept;
    • a title change in one piece copies only that piece, and a GPU write in another piece stays;
    • partial first and last pieces of a range that starts and ends inside pieces;
    • forgotten sums (zeroed copy) copy those pieces once, then nothing;
    • the sum differs for every single-byte change in a piece.

    It passes with the change (shadow_keep: OK). With copy_changed changed back to copying every piece (the old behaviour), 13 checks fail (the kept GPU write, the per-piece copies, the partial pieces and the copy-once after zeroing).

  • Built gta6rt (Windows 11, MSVC, Ninja, RelWithDebInfo) and ran these tests with ctest. All 12 passed: shadow_keep, d3d12_existing_heap, d3d12_existing_heap_no_write_watch, d3d12_existing_heap_write_watch, d3d12_xmem_placed, d3d12_title_va, d3d12_title_va_shadow, d3d12_title_window_cap, title_va_alias, title_window_plan, title_window_plan_bounds and vk_host_at_aliasing. The d3d12_* tests are the existing GPU tests of the shadows. They use hand-made pages, not game data. Not run: the full ctest suite and the translator suite (this changes no shader or translator code).

  • python -I scripts/check_no_game_data.py on the changed files: clean.

  • The game observation above (the GPU-written buffer reading back as the title's bytes every other frame) was made on a local build of main plus other local work, with a diagnostic trace that is not part of this change. There, with the change, the buffer kept its value from frame to frame and the alternating black frames were gone (one run).

  • Cost, measured with this branch's own build (current main with #2098, plus this change), the same retail scene, 420 s each, one run per setting, over the same 150-300 s window:

    LEONIDA_SHADOW_KEEP Refresh, ms a submission Bytes written to the copies a refresh Submissions a second
    0 (as before) 149 about 3.2 GB 3.9
    on (this change) 88 under 5 MB 4.6

    On main most of those bytes are pages mapped at two addresses, which no guard watches, so they were rewritten into the copies at every submission although they had not changed. With this change on, the pieces are compared on the CPU before anything is written, so #2098's optional GPU copy of aliased runs (gpu_copy, not used by default in these runs) is not reached; with LEONIDA_SHADOW_KEEP=0 that path is as on main. No log lines or captures are quoted here.

GPUs tested: NVIDIA GeForce RTX 5080, driver 621.24, Windows 11 (game observation and the GPU-side ctest entries).

AI-assisted: yes (Claude Code, Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator rule)

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2163 **Author:** [@DadaCap](https://github.com/DadaCap) **Created:** 11/26/2026 **Status:** ✅ Merged **Merged:** 11/26/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `shadow-keep-gpu-writes` --- ### 📝 Commits (5) - [`0ebd428`](https://github.com/ubiquitarianism/leonida/commit/0ebd4286ea80b5de20dbf7adf7bf6b970c7492df) d3d12: a slot-133 shadow keeps the GPU's writes where the title's bytes did not change (LEONIDA_SHADOW_KEEP) - [`4d811fc`](https://github.com/ubiquitarianism/leonida/commit/4d811fcee90f9a4b4c2988117bb78be96360342d) Merge remote-tracking branch 'origin/main' into shadow-keep-gpu-writes - [`d6b97a9`](https://github.com/ubiquitarianism/leonida/commit/d6b97a97b5c0f3edf916cb60fb81a158a1274852) Merge main into shadow-keep-gpu-writes: shadow_keep and console_heap_copy tests side by side in CMakeLists - [`71f54be`](https://github.com/ubiquitarianism/leonida/commit/71f54be48267bd397fc879dd8355b081be1ff002) shadow keep: a newly filled target brings its pieces to the other targets and the sums (review by lixonik) - [`b59ff42`](https://github.com/ubiquitarianism/leonida/commit/b59ff421070c5b1f3c3f8531c4da9f4156eedf15) Merge main into shadow-keep-gpu-writes: main removed the console heap mirror test (#2370); the shadow_keep test stays ### 📊 Changes **7 files changed** (+374 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `docs/d3d12_vulkan.md` (+9 -0) 📝 `runtime/CMakeLists.txt` (+12 -0) 📝 `runtime/README.md` (+1 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+77 -4) ➕ `runtime/src/shadow_keep.h` (+72 -0) 📝 `runtime/test/d3d12_title_va_test.cpp` (+62 -0) ➕ `runtime/test/shadow_keep_test.cpp` (+141 -0) </details> ### 📄 Description Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7178046531 Depends on: none. It merges cleanly with current `main` (which now includes #2098), and conflicts with #1897 and #1867. #1897 moves `ShadowPages` to `runtime/src/shadow_pages.h`, so its `d3d12_vulkan_device.cpp` hunks conflict. #1867 conflicts only in its `runtime/README.md` row next to this one. Whichever lands second redoes those lines. It merges cleanly with #2032 and #2129. ### What and why `ShadowPages` copies the title's pages into the slot-133 shadow buffers and title windows before every submission. Some pages are copied at every refresh without any sign that the title wrote them: pages xmem maps at two addresses, pages a guard could not be put on, the 64 KiB block reopened around a written page, write-watched pages and pages pinned for a kernel write. A copy of such a page also overwrote whatever a shader had stored in the shadow since the last refresh, with the title's unchanged bytes. Seen in the retail title: a 16-byte buffer that a compute program writes for the next frame to read was copied over by the title's old bytes before that frame, so it read back as zero every other frame and half the frames came out black. The fix: - `runtime/src/shadow_keep.h` (new, portable): a 64-bit sum per 256-byte piece of the title's address space, taken from the bytes last copied. `copy_changed` compares the title's current bytes with those sums and copies only the pieces that differ. The first and last pieces of a range can be partial. `forget` clears sums so their pieces get copied again. - `ShadowPages` keeps the sums (`sums_`, per piece from its first page; grown by `extend`, carried over by `absorb`). With `LEONIDA_SHADOW_KEEP` on (the default), every copy job reads the title's page once into a buffer (still through the guarded `copy_run`) and passes on only the changed pieces to each target. A page that is zeroed because it went away gets its sums forgotten, so it is copied in full once it comes back. A new target's first copy (`fill`) is unchanged. - `LEONIDA_SHADOW_KEEP=0` restores the old behaviour (every job copied whole). There is a row for it in `runtime/README.md` and a paragraph in `docs/d3d12_vulkan.md` under the copies. Limits: - A title that writes the **same** bytes again to reset a value the GPU changed is not seen: the sum still matches, so the GPU's value stays. Fixing that needs the GPU's writes to reach the title's pages (the write-back work in #2032 / #2129, which covers other pages: #2129 drops the GPU's result exactly on the pages that are not guarded or clean, which are the ones this change is about). - Every copied page is now read once into a 4 KiB buffer and hashed before the changed pieces are copied out. The sum runs four independent lanes (about 30 GB/s on the test machine's CPU against about 24 GB/s for a plain copy; a first, chained version ran at 13 GB/s and made the refresh five times slower in the game, so the speed matters). The copies are then written only where the title changed, which is most of the gain below. - A 64-bit sum can collide, in which case one change is missed until the next one. ### Tested - New `shadow_keep_test` (ctest `shadow_keep`, CPU only, no GPU and no game data). The title and the copy are arrays with made-up bytes: - a GPU write into the copy over unchanged title bytes is kept; - a title change in one piece copies only that piece, and a GPU write in another piece stays; - partial first and last pieces of a range that starts and ends inside pieces; - forgotten sums (zeroed copy) copy those pieces once, then nothing; - the sum differs for every single-byte change in a piece. It passes with the change (`shadow_keep: OK`). With `copy_changed` changed back to copying every piece (the old behaviour), 13 checks fail (the kept GPU write, the per-piece copies, the partial pieces and the copy-once after zeroing). - Built `gta6rt` (Windows 11, MSVC, Ninja, RelWithDebInfo) and ran these tests with `ctest`. All 12 passed: `shadow_keep`, `d3d12_existing_heap`, `d3d12_existing_heap_no_write_watch`, `d3d12_existing_heap_write_watch`, `d3d12_xmem_placed`, `d3d12_title_va`, `d3d12_title_va_shadow`, `d3d12_title_window_cap`, `title_va_alias`, `title_window_plan`, `title_window_plan_bounds` and `vk_host_at_aliasing`. The `d3d12_*` tests are the existing GPU tests of the shadows. They use hand-made pages, not game data. Not run: the full ctest suite and the translator suite (this changes no shader or translator code). - `python -I scripts/check_no_game_data.py` on the changed files: clean. - The game observation above (the GPU-written buffer reading back as the title's bytes every other frame) was made on a local build of `main` plus other local work, with a diagnostic trace that is not part of this change. There, with the change, the buffer kept its value from frame to frame and the alternating black frames were gone (one run). - Cost, measured with this branch's own build (current `main` with #2098, plus this change), the same retail scene, 420 s each, one run per setting, over the same 150-300 s window: | `LEONIDA_SHADOW_KEEP` | Refresh, ms a submission | Bytes written to the copies a refresh | Submissions a second | |---|---|---|---| | `0` (as before) | 149 | about 3.2 GB | 3.9 | | on (this change) | 88 | under 5 MB | 4.6 | On `main` most of those bytes are pages mapped at two addresses, which no guard watches, so they were rewritten into the copies at every submission although they had not changed. With this change on, the pieces are compared on the CPU before anything is written, so #2098's optional GPU copy of aliased runs (`gpu_copy`, not used by default in these runs) is not reached; with `LEONIDA_SHADOW_KEEP=0` that path is as on `main`. No log lines or captures are quoted here. GPUs tested: NVIDIA GeForce RTX 5080, driver 621.24, Windows 11 (game observation and the GPU-side ctest entries). AI-assisted: yes (Claude Code, Opus 5.5) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator rule) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:20:16 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2166
No description provided.