d3d12: a slot-133 shadow keeps the GPU's writes where the title's bytes did not change (RT-SHADOW-KEEP-GPU-WRITES) #2163
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs rebase
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!2163
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2163"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@DadaCap on GitHub · 2026-11-26 00:40 UTC · original
DadaCap:shadow-keep-gpu-writes→main· +374 −4 · 7 files · 5 commits · merged 2026-11-26 17:26 UTCClaimed in the coordination issue as: #1608
Depends on: none. It merges cleanly with current
main(which now includes #2098), and conflicts with #1897 and #1867. #1897 movesShadowPagestoruntime/src/shadow_pages.h, so itsd3d12_vulkan_device.cpphunks conflict. #1867 conflicts only in itsruntime/README.mdrow next to this one. Whichever lands second redoes those lines. It merges cleanly with #2032 and #2129.What and why
ShadowPagescopies 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_changedcompares 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.forgetclears sums so their pieces get copied again.ShadowPageskeeps the sums (sums_, per piece from its first page; grown byextend, carried over byabsorb). WithLEONIDA_SHADOW_KEEPon (the default), every copy job reads the title's page once into a buffer (still through the guardedcopy_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=0restores the old behaviour (every job copied whole). There is a row for it inruntime/README.mdand a paragraph indocs/d3d12_vulkan.mdunder the copies.Limits:
Tested
New
shadow_keep_test(ctestshadow_keep, CPU only, no GPU and no game data). The title and the copy are arrays with made-up bytes:It passes with the change (
shadow_keep: OK). Withcopy_changedchanged 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 withctest. 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_boundsandvk_host_at_aliasing. Thed3d12_*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.pyon 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
mainplus 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
mainwith #2098, plus this change), the same retail scene, 420 s each, one run per setting, over the same 150-300 s window:LEONIDA_SHADOW_KEEP0(as before)On
mainmost 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; withLEONIDA_SHADOW_KEEP=0that path is as onmain. 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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator rule)🤖 Generated with Claude Code
@ecox982745 on GitHub · 2026-11-26 00:47 UTC · original
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 tested4d811fcee90f9a4b4c2988117bb78be96360342don Windows 11, NVIDIA 616.64, using synthetic slot-133 buffers.To reproduce, I extended
check_kindind3d12_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 eitherkernel_writes::holdor 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_keeptest 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 on GitHub · 2026-11-26 00:49 UTC · original
@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 on GitHub · 2026-11-26 06:13 UTC · original · reviewed
I reproduced an overlapping-target refresh regression using the PR's ShadowPages on native Windows and an ordinary committed page. AI-assisted.
@ -1026,0 +1056,4 @@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(@lixonik on GitHub · 2026-11-26 06:13 UTC · original
[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 whilecopy_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.@djrolfes on GitHub · 2026-11-26 09:19 UTC · original
Independent validation on Linux/Wine + NVIDIA RTX 3070 Ti (AI-assisted):
4d811fcee90f9a4b4c2988117bb78be96360342donto our local test branch (ona73a57e13with#2222and localRT-VSHARP-TITLE-VA-COLLISION).shadow_keeppasses (5/5 focused Wine tests pass).#2163: slot-133 aliased shadow refreshes copied~357 MiBper submission; hit zero-fault-addressVK_ERROR_DEVICE_LOSTat281 s.#2163: slot-133 shadow copies dropped to0.0–0.3 KiBper submission, and survival extended ~3x to827.4 s(2,024PresentX,2,920submissions, 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 on GitHub · 2026-11-26 13:29 UTC · original
World run with this PR: RTX 4070 SUPER (12 GB), NVIDIA 621.01, Windows 11, 64 GB. Counts only.
Tree:
maina73a57e13+ #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 setsLEONIDA_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):
SHADOW_ALIAS_MSvkWaitSemaphores, retirement), withLEONIDA_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.AI-assisted (Claude Code).
@DadaCap on GitHub · 2026-11-26 16:42 UTC · original
@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
71f54be48: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_offwithLEONIDA_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*andconsole_heap_copypass too (9/9, RTX 5080, Windows 11).(Reply written with Claude Code.)
game_scarlett_master.exe+0x23cf683~40 s after twoE_DSTORAGE_DECOMPRESSION_ERRORreads #2019@aslan2142 on GitHub · 2026-11-26 17:26 UTC · original
Merging.
71f54be48answers @lixonik's review: a newly filled target now brings its pieces to the other targets and to the sums.main935a2cd5b+ this PR, on a local merge with the same tree asb59ff4210:cteston the integrated RDNA2 GPU (RADV, Mesa 26.3.1): 112/112 pass,shadow_keepincluded.main. No test went from pass to fail;shadow_keep,d3d12_shadow_keep_refillandd3d12_shadow_keep_refill_offare new and pass.test_ci_registration.py, the docs tests andcheck_no_game_data.pypass.d6b97a97bheld 666 s to the title's own breakpoint, and the bytes copied per submission fell to about a tenth. There is no world run fromb59ff4210yet; it is the next run here.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 bufferreconcileread closes that.begin_orend_, the piece at the old edge is summed whole and copied once over GPU writes in it.LEONIDA_SHADOW_KEEP=0loses those writes too.LEONIDA_SHADOW_GPUis 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)
@ecox982745 on GitHub · 2026-11-26 17:51 UTC · original
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-refillfails consistently against the old runtime.LEONIDA_SHADOW_KEEP=1LEONIDA_SHADOW_KEEP=0d6b97a97b571f54be482bd6ba97ab6I kept the regression test and its CMake registration from
71f54be482for the old-runtime build. Its only production-file difference from its parent isruntime/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).