Skip to content

d3d12: the title write-back asks the write watch first and copies back only the bytes a copy changed (RT-TITLE-WRITEBACK-STALE-BYTES) - #2404

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
OreoFR:fix/title-writeback-asks-first
Nov 26, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
OreoFR:fix/title-writeback-asks-first

Conversation

@OreoFR

@OreoFR OreoFR commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-TITLE-WRITEBACK-STALE-BYTES (released to me by @DadaCap)

What and why

Since #2350 (which brings #2129, the title write-back), the story load crashes on Windows/NVIDIA. The title faults at a different place on every run: null writes, a non-pointer read, its own int3. Turning the write-back off removes these faults, but it is still needed for #1537.

The cause is in ShadowPages::write_back (runtime/shims/d3d12_vulkan_device.cpp):

  • write_back_titles() runs at the start of the shadow refresh, before ShadowPages::refresh() asks the write watch. A hot page (write-watched, never guarded) still carries kClean from the last asking. Every write the title made to it since the last submission was therefore copied over with the older shadow. Hot pages are the ones the title writes most.
  • A guarded page was opened (reopen_locked) before a memcpy of the whole range. A title thread writing it in between did not fault, and its write was lost.
  • Every binding reached was copied back whole, read-only ones too (a V# has no read/write bit). Where two copies covered the same bytes, the last one won.

This change keeps the write-back on and does three things:

  • write_back asks the write watch first. The asking moves into ask_written_locked(), which refresh() uses too, so a hot page the title wrote is held.
  • It holds pages whose mapping changed since the last refresh (from the title_memory_map journal; nothing goes back when the journal no longer reaches back), pages pinned for a kernel write, and pages xmem maps at another address.
  • It compares in 256-byte pieces and writes only those where the copy differs from the title's bytes. A read-only view writes nothing and its pages stay guarded. Of two copies of the same bytes, only the one that differs is written.

docs/d3d12_vulkan.md describes the new behaviour.

Tested

GPUs tested: NVIDIA GeForce RTX 4070 Laptop 8 GB, driver 621.40, Windows 11.

AI-assisted: yes (Claude Code, Claude 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: runtime only)

🤖 Generated with Claude Code

OreoFR and others added 2 commits November 26, 2026 22:06
…k only the bytes a copy changed

write_back_titles() runs at the start of the shadow refresh, before ShadowPages::refresh() asks the write watch,
so a hot page still carried kClean from the last asking and the title's writes since then were copied over
with the older copy. write_back() now asks first (the asking moves into ask_written_locked(), shared with
refresh()), holds pages whose mapping changed, that a kernel write may be filling, or that xmem aliases, and
copies back only the 256-byte pieces where a copy differs from the title's bytes: a read-only binding writes
nothing and its pages stay guarded, and of two copies of the same bytes only the one that differs is written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the pieces that differ

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142

Copy link
Copy Markdown
Collaborator

Merging.

  • Why: write_back ran before refresh() asked the write watch, so a write-watched page still counted as clean and the title's newer bytes could be overwritten by an older copy. The write watch is asked first now, an unchanged view is compared in 256-byte pieces and not written, and pages whose mapping changed since the last refresh are held back.
  • Tested on main 55e2bd9e9 + this PR (46aeaaefa): the Windows suite under Wine (clang-cl + xwin) on the integrated RDNA2 GPU (RADV, Mesa 26.3.1): 425 pass and 23 fail, the same as without the PR; no test changed. The merge is clean and the changed files carry no game data.
  • Not tested: no game run from this build, and the PR has no test of its own. Under Wine these views have no write watch (our runs log write-watched 0.0), so on Linux only the 256-byte comparison and the held-back pages apply.

Follow-ups, for whoever takes them:

  • GPU writes dropped after a tracker merge. When absorb merges two trackers, the writes due back on that tracker at the next write-back are dropped, with no log line. It needs a counter at least, and a decision whether dropping is right.
  • The race is narrower, not gone. A whole run of pages is guarded at the first differing piece, so a title write to a later piece of that run is still lost.
  • A test that fails without the fix, so the order of the write watch and the write-back cannot regress.
  • With two copies of the same bytes the last one still wins, depending on their order; that is no worse than before.

Thanks @OreoFR.

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

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

HasiKe commented Nov 26, 2026

Copy link
Copy Markdown

@OreoFR, I found the same stale kClean order independently today (#1608) and had a narrower fix: ask in write_back once per pass. Yours covers more, so I dropped mine. Two contributions instead. AI-assisted (Claude Code).

1. A regression test for the hot-page case, which d3d12_title_va does not reach today. It is in commit c9c73f48c on HasiKe/leonida:writeback-hot-test: 2 files, runtime/test/d3d12_title_va_test.cpp and runtime/CMakeLists.txt. Cherry-pick it if you want it.

  • The test is d3d12_placed_write_back_hot. It runs the placed-buffer write-back check over a MEM_WRITE_WATCH reservation recorded with title_write_watch::add.
  • The title's first write faults and makes the block hot. The next submission copies the GPU's result back. Then the title writes again, with no fault, and one more submission must keep those words.
  • On main fac8b4bb9: 0/64 words survive (line 645: kept_hot == kElements).
  • With this PR: 64/64. The existing d3d12_placed_write_back still passes too.
  • The test skips with 77 where write watching is off.

2. Three retail boots with this PR on Windows/NVIDIA: RTX 2070 SUPER, 616.56. Build: main fac8b4bb9 + this PR only. Play-folder recipe: warm caches, gate on, LEONIDA_GEOMETRY_REMAP_RELEASE=1, LEONIDA_HEAP_MIRROR=1, LEONIDA_DEVICE_FAULT=2. 600 s each; the input script enters the world at 314 s.

Boot End
1 device lost at about 205 s, then the title's int3
2 full 600 s, no exception
3 0xc0000005 at about 150 s

These are not a clean A/B against my earlier tables. This build lacks #2306, so there is no fault site in the crash log, and it lacks #2233/#2275/#2365. Both failures came before the world, where my earlier faults at 80-160 s also came on plain main. So I can't tell yet whether the remaining CPU fault is a second cause or this build's base.

One thing stood out: all three boots ran at 6-14 submissions/s and about 3-7 frames/s. My stack without this PR ran the same recipe at 110-175 submissions/s and 55-88 frames/s. Could the 256-byte comparison over every reached range be that expensive per submission? The shadows column of the submit-phase lines would show it.

Next, I'll rerun with #2306 added, so the faults have sites, and with the submit-phase breakdown.

@HasiKe

HasiKe commented Nov 26, 2026

Copy link
Copy Markdown

Follow-up with numbers on the slowdown, from the submit-phase lines of the same boots. AI-assisted (Claude Code).

Build Average shadows time per submission (max) Submissions/s
main + #2365 #2233 #2275 #2276 #2306, without this PR, both switches 1 2.2-2.3 ms (3) 172-178
main fac8b4bb9 + this PR, same switches 3.3 ms at 63 s, 8.0 ms at 139 s, then 56 ms (58) from about 210 s 13.6

From about 210 s on, the shadow refresh costs about 25 times as much per submission, and the frame rate drops with it. The other phases stay where they were: heaps 0.5 ms, textures 16 ms, up from 2.4 ms. So the 256-byte comparison, or the asking per range, scales with something that grows during the load: the number of reached ranges, or the write-watched bytes. The old unconditional copy did not grow like that. Asking once per tracker per write-back pass, instead of once per write_back call, was enough in my narrower version to keep the cost flat. That may be the cheap part of it here too.

@HasiKe

HasiKe commented Nov 26, 2026

Copy link
Copy Markdown

Correction to my slowdown numbers above: they do not hold up as a cost of this PR. AI-assisted (Claude Code).

I ran the same recipe again: RTX 2070 SUPER, NVIDIA 616.56, warm caches, gate on, LEONIDA_GEOMETRY_REMAP_RELEASE=1, LEONIDA_HEAP_MIRROR=1, LEONIDA_DEVICE_FAULT=2, 600 s. The build was merged main e5a7c8942 (this PR, the same commits I tested before) plus #2365 #2233 #2275 #2276 #2306. In all three boots shadows stayed at 2.1-2.5 ms per submission through 300 s (0.25-0.30 ms in one boot), the same as without this PR.

So the 56 ms in the earlier boots came from something about that build or the scene it ended up in, not from this change by itself. Sorry for the noise.

What this build does show, three of three boots:

  • No CPU fault at all. Before this PR, that recipe faulted in title code in 3 of 3 boots after entering the world.
  • One boot ran the full 600 s.
  • Two boots lost the device in the world, at 439 s and 449 s, followed by the title's int3. That is a different problem; I'm on it.

Thanks for the fix.

aslan2142 pushed a commit that referenced this pull request Nov 27, 2026
…n in the fault handler

ShadowPages::write_back copies a window's GPU writes back to the title's pages under the tracker's
SRW lock (#2129, and the 256-byte pieces of #2404). It reopens its own guard first, but the page can
still be read-only because of another guard (the geometry watch, another view's shadow). The copy
then faults, shadow_write_fault asks every tracker over the address, and this one's reopen() took
lock_ again on the thread that already held it. SRW locks are not recursive, so the title's
submitting thread hung for good: on an RTX 4070 Laptop (NVIDIA 621.40) the main loop froze at
209-309 s with no device loss, and the thread dumps showed the submitting thread inside
write_back -> (fault) -> shadow_write_fault -> reopen, waiting on the SRW lock, in four runs.

write_back records its thread in writer_ while it holds lock_; reopen() and reopen_range() skip
taking the lock on that thread (they run under it). Other threads still wait on the lock as before.

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.

3 participants