Skip to content

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

Merged
aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
DadaCap:shadow-keep-gpu-writes
Nov 26, 2026
Merged

aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
DadaCap:shadow-keep-gpu-writes

Conversation

@DadaCap

@DadaCap DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #1608 (comment)

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:

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 Improve post-load GPU memory and pipeline handling (RT-POST-LOAD-GPU-MEMORY) #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 Improve post-load GPU memory and pipeline handling (RT-POST-LOAD-GPU-MEMORY) #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

DadaCap and others added 2 commits November 26, 2026 01:22
…es did not change (LEONIDA_SHADOW_KEEP)

ShadowPages copies pages that carry no sign of a title write (aliased,
unguarded, reopened with their 64 KiB block, write-watched or pinned) at
every refresh, and that copy overwrote what a shader had stored in the
shadow with the title's unchanged bytes. A value a compute program writes
for the next frame then read back as the title's old bytes every other
frame.

Each tracker now keeps a 64-bit sum per 256-byte piece of the bytes it
copied last (runtime/src/shadow_keep.h) and copies a piece only when the
title's bytes differ. Zeroed pages forget their sums. LEONIDA_SHADOW_KEEP=0
copies every job whole, as before. New CPU-only test shadow_keep.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5
# Conflicts:
#	runtime/shims/d3d12_vulkan_device.cpp
@ecox982745

Copy link
Copy Markdown

On an RTX 4090, the next submission preserved 64/64 GPU-written words with the default setting and 0/64 with LEONIDA_SHADOW_KEEP=0. I tested 4d811fcee90f9a4b4c2988117bb78be96360342d on Windows 11, NVIDIA 616.64, using synthetic slot-133 buffers.

CPU write between submissions Default GPU readback With shadow keep disabled
Change the neighboring 256-byte piece All 64 GPU words survived in the first piece. All 64 new CPU words arrived in the neighbor. The first piece reverted to all 64 original CPU words. All 64 new CPU words arrived in the neighbor.
Then change one 32-bit word inside the first piece The full piece came from the CPU. The changed word arrived, and the other 63 reverted to their original CPU values. Same result.

To reproduce, I extended check_kind in d3d12_title_va_test.cpp. Its existing root-UAV dispatch writes words 0–63. After its fence, I changed words 64–127 and issued a copy-only submission reading back the first 512 bytes. After that fence, I changed word 63 and repeated the readback. I asserted that the resource took the shadow path. I ran both ordinary and write-combined pages, using either kernel_writes::hold or the runtime’s alias marker, with the alias refresh interval at zero.

All eight combinations of tracking mode, feature setting and Vulkan validation matched the table on both page types. The PR’s CPU-only shadow_keep test also passed. This verifies the refresh behavior on a second GPU and shows the 256-byte boundary when CPU and GPU writes share a piece. I did not run the game.

AI-assisted with OpenAI Codex (gpt-6.2-astra).

@DadaCap

DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@ecox982745 thank you, that is the check on a second GPU I could not make, and the second row is exactly the limit to know: the unit of comparison is the 256-byte piece, so a CPU write anywhere in a piece brings the whole piece back from the title, GPU words included. A finer piece costs more sums (8 bytes per piece, so 64-byte pieces would be 1/8 of the covered memory instead of 1/32); 256 bytes kept the measured case (16 bytes written by a compute program, nothing else in its piece written by the CPU) and the refresh cost low. AI-assisted (Claude Code, Claude Opus 5.5).

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced an overlapping-target refresh regression using the PR's ShadowPages on native Windows and an ordinary committed page. AI-assisted.

const std::uintptr_t va = begin_ + at;
const std::size_t n = std::min<std::size_t>(end - at, kPage - (va & (kPage - 1)));
copy_run(page, reinterpret_cast<const std::uint8_t*>(va), n, job.streaming);
leonida::shadow_keep::copy_changed(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] A shared checksum can suppress a refresh needed by a newly filled target. I refreshed target A with the CPU byte at 1, set the CPU byte to 2, added an overlapping target B and filled it, then restored the CPU byte to 1 before refreshing again. With LEONIDA_SHADOW_KEEP=1, the refresh copies zero bytes and B stays at 2. With the flag off both targets end at 1. fill() takes a fresh snapshot for B while copy_changed() compares against the snapshot shared with A. Could the first refresh reconcile the new target's snapshot or track its baseline separately? This case needs no GPU writes.

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 26, 2026
…pump coalescing & 16-bit/indirect completeness (Wave 29A)

Adapts and integrates:
- GPU-to-CPU title-window write-back with retire-ticket fencing, xmem dirty-stamp + byte-level CPU-write precedence, and LEONIDA_TITLE_WRITEBACK=0 kill switch (ubiquitarianism/leonida#2129, GPL-3.0).
- Slot-133 shadow GPU-write preservation via 256-byte piece sums (LEONIDA_SHADOW_KEEP, ubiquitarianism/leonida#2163, GPL-3.0), saved-snapshot advancement after partial merge (Marvisak/VIctory#3031, GPL-3.0), and read-only hash skip for foreign PAGE_READONLY pages (ubiquitarianism/leonida#1897, GPL-3.0).
- Held-batch fence-pump fix: snapshot has_shader_work / internal_staging on ExecuteCommandLists thread in VulkanQueue::Deferred and coalesce redundant back-to-back refresh_shadows() / refresh_console_heaps() inside a single QueueHolds::pump_ready() sweep when xmem epochs and shadow registry are unchanged.
- Transient dedicated command-pool renewal for long recordings above LEONIDA_CMD_POOL_RENEW (default 512, Rubenpannen/VIctory-RTX5070@5df4fb3, GPL-3.0).
- Complete 16-bit packed colour widening for B5G6R5_UNORM RT/UAV, B4G4R4A4_UNORM RT/UAV, and B5G5R5A1_UNORM RT/UAV (Marvisak/VIctory#2936, GPL-3.0; californiaAlmonds/OpenScarlett@ee7153c, MIT).
- Compound graphics ExecuteIndirect host-visible expansion, Tier 1.1 incrementing-constant stride fix, and D3D12 state-leakage binding reset (ubiquitarianism/leonida#1937, GPL-3.0).

AI-assisted; verified on NVIDIA GeForce RTX 3080 (Windows 11, VS 18 MSVC 19.51) with LEONIDA_VK_VALIDATION=1 (18/18 CTests passed, 0 validation errors).
@djrolfes

Copy link
Copy Markdown
Contributor

Independent validation on Linux/Wine + NVIDIA RTX 3070 Ti (AI-assisted):

  • Cherry-picked 4d811fcee90f9a4b4c2988117bb78be96360342d onto our local test branch (on a73a57e13 with #2222 and local RT-VSHARP-TITLE-VA-COLLISION).
  • Isolated offline Wine/Vulkan test: shadow_keep passes (5/5 focused Wine tests pass).
  • Controlled retail A/B comparison (same profile, flags, shader corpus, and warm GLCache):
    • Without #2163: slot-133 aliased shadow refreshes copied ~357 MiB per submission; hit zero-fault-address VK_ERROR_DEVICE_LOST at 281 s.
    • With #2163: slot-133 shadow copies dropped to 0.0–0.3 KiB per submission, and survival extended ~3x to 827.4 s (2,024 PresentX, 2,920 submissions, 0 memory faults, 0 Xids, 0 timed-out list-value gates) before hitting the same downstream zero-fault direct-queue stall/timeout.

No game data included.

@Tyrannys

Copy link
Copy Markdown
Contributor

World run with this PR: RTX 4070 SUPER (12 GB), NVIDIA 621.01, Windows 11, 64 GB. Counts only.

Tree: main a73a57e + #1851 #1905 #1957 #1886 #1894 #1914 #1882 #2195 #2113 #1592 #2097 #2233 #2179 #2228 #2260, plus this PR, #2267 and #2230 (all clean merges). Recipe: async pipelines with prewarm, LEONIDA_COMPILE_GATE=0, a local skip list of the 1,009 ray-hit shading programs, LEONIDA_RT_AS=off, Series S device type, LEONIDA_ARENA_MB=1536, LEONIDA_TITLE_WINDOWS_MB=1024, the watchdog kicked, warm caches. F8 also sets LEONIDA_SHADOW_ALIAS_MS=1000 (quartz-heron-58's lead), so the shadow drop below comes from this PR and that setting together, not this PR alone.

Mean per-submission costs from the runtime's "submit phases" lines, over each run's last 40 lines (in the world):

run this PR SHADOW_ALIAS_MS submissions/s shadows textures heaps replay
F6 no default 3.6 80 ms 35 ms 16 ms 69 ms
F8 yes 1000 5.7 14 ms 37 ms 15 ms 526 ms
  • The world was reached in both runs (indirect graphics emitted 65,536). F8's world ran at 13-15 frames a minute from about 1,080 s; F6's ran at 1-7.
  • Replay is now the largest cost per submission in F8.
  • F8 lost the device at 1,243 s (vkWaitSemaphores, retirement), with LEONIDA_DEVICE_FAULT=0, so there is no fault report. VRAM was over the budget at the time (12.9 GB used against 10.6 GB), as in every world run on this 12 GB card. The next run repeats it with fault reporting on.
  • World rendering seen on screen in earlier runs of this recipe family; screenshot kept locally.

AI-assisted (Claude Code).

…copy tests side by side in CMakeLists

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
…gets and the sums (review by lixonik)

The sums stand for what every target holds. fill() gave a new target the title's current bytes while the sums kept
the bytes the other targets were refreshed with, so a title that wrote the old bytes back before the next refresh left
the new target behind (no piece differed from its sum). fill() now compares the filled range, widened to whole pieces,
with the sums as a refresh does: the pieces that differ are copied to every target over them and their sums updated.
New test d3d12_shadow_keep_refill (and _off with LEONIDA_SHADOW_KEEP=0): the reviewer's sequence, two overlapping
targets; it failed before this change (A held 1, B held 2) and passes now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
@DadaCap

DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@lixonik thank you, that was a real hole: the sums stood for what the targets held only until a new target was filled. Fixed in 71f54be: fill() now compares the filled range, widened to whole pieces, with the sums as a refresh does, and copies the pieces that differ to every target over them before updating the sums, so a title that writes the old bytes back is seen again. Your sequence is now a test, d3d12_shadow_keep_refill (and _off with LEONIDA_SHADOW_KEEP=0): two overlapping targets, A refreshed at 1, byte set to 2, B added and filled, byte back to 1, refresh. It failed before the change (A held 1, B held 2) and passes now; shadow_keep, d3d12_title_va*, d3d12_existing_heap* and console_heap_copy pass too (9/9, RTX 5080, Windows 11).

(Reply written with Claude Code.)

… mirror test (ProjectLeonida#2370); the shadow_keep test stays

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
@aslan2142

Copy link
Copy Markdown
Collaborator

Merging.

  • Why: a slot-133 shadow keeps what the GPU wrote until the title's bytes under it change, instead of being overwritten by our copy at every submission. 71f54be48 answers @lixonik's review: a newly filled target now brings its pieces to the other targets and to the sums.
  • Tested on main 935a2cd5b + this PR, on a local merge with the same tree as b59ff4210:
    • Linux runtime ctest on the integrated RDNA2 GPU (RADV, Mesa 26.3.1): 112/112 pass, shadow_keep included.
    • Windows suite under Wine (clang-cl + xwin) on the same GPU: 421 pass and 23 fail, against 417 and 24 on main. No test went from pass to fail; shadow_keep, d3d12_shadow_keep_refill and d3d12_shadow_keep_refill_off are new and pass.
    • test_ci_registration.py, the docs tests and check_no_game_data.py pass.
  • World run: one run on the RX 7900 XTX (RADV, Wine) with this PR at d6b97a97b held 666 s to the title's own breakpoint, and the bytes copied per submission fell to about a tenth. There is no world run from b59ff4210 yet; it is the next run here.
  • Not confirmed by us: that the new refill test fails on the old head. A model of the old bookkeeping fails it.

Follow-ups, none blocking:

  • fill() reads the title's bytes twice. A write-back between the two reads leaves the new target stale; copying from the buffer reconcile read closes that.
  • When a new target moves the tracker's begin_ or end_, the piece at the old edge is summed whole and copied once over GPU writes in it. LEONIDA_SHADOW_KEEP=0 loses those writes too.
  • The docs and the README row name one limit. Missing: a CPU write brings back the whole 256-byte piece, a sum collision can miss a change, and LEONIDA_SHADOW_GPU is ignored while the keep is on.

Thanks @DadaCap, @lixonik for the review, and @ecox982745, @djrolfes and @Tyrannys for the runs.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

@aslan2142
aslan2142 merged commit bd6ba97 into ProjectLeonida:main Nov 26, 2026
2 of 5 checks passed
@ecox982745

Copy link
Copy Markdown

I checked the remaining before/after question in the merge note on native Windows 11 with an RTX 4090, NVIDIA 616.64. The new d3d12_title_va_test --shadow-keep-refill fails consistently against the old runtime.

Runtime LEONIDA_SHADOW_KEEP=1 LEONIDA_SHADOW_KEEP=0
Before the refill fix, d6b97a97b5 0/6 passed. A held 1, B held 2. 6/6 passed. Both held 1.
Refill fix, 71f54be482 6/6 passed. Both held 1. 6/6 passed. Both held 1.
Merged main, bd6ba97ab6 6/6 passed. Both held 1. 6/6 passed. Both held 1.

I kept the regression test and its CMake registration from 71f54be482 for the old-runtime build. Its only production-file difference from its parent is runtime/shims/d3d12_vulkan_device.cpp, which I restored from that parent before rebuilding. Each table cell includes three fresh processes with Vulkan validation enabled and three with it disabled. The enabled runs reported no VUIDs. The A/B values are the bytes read back after the D3D12 queue copied the two overlapping buffers.

AI-assisted hardware verification with OpenAI Codex (GPT-6).

aslan2142 added a commit to azsekazvamdimitar/leonida that referenced this pull request Nov 26, 2026
One conflict, runtime/README.md: this branch rewrote the LEONIDA_SHADOW_ALIAS_MS
row, main (ProjectLeonida#2163) added the LEONIDA_SHADOW_KEEP row after it. Both rows kept;
the keep row's "aliased" now reads "aliased while they can't be guarded at every
address", since aliased pages are no longer copied without a sign of a write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

6 participants