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

Manually merged
github merged 2 commits from gh-pr/2404/head into gh-pr/2404/base 2026-11-27 00:35:13 +00:00
Owner

@⁠OreoFR on GitHub · 2026-11-26 21:23 UTC · original
OreoFR:fix/title-writeback-asks-first → main · +93 −44 · 2 files · 2 commits · merged 2026-11-26 21:49 UTC

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

  • d3d12_title_va and d3d12_title_va_shadow (the write-back tests from #2129) pass. tests/test_docs_env_vars.py passes, and scripts/check_no_game_data.py is clean on both changed files.
  • Retail story load on main fac8b4bb9, 600 s, shaders retranslated, write-back on (default):
    • before (main, and 2d43417d4 the same apart from #2401): the title faulted at 95-170 s in 5 of 5 runs, each time somewhere else. One run on fac8b4bb9 reached the watchdog stop at 186 s first.
    • with this change: no title fault in the load. 52 KiB went back in 313 s, against about 26 MiB by 186 s before.
  • The run then stops at 313 s on the title's hang watchdog (+0x23cf683), after the title polled a fence value that stopped arriving for about 50 s. The same stop happens with LEONIDA_TITLE_WRITEBACK=0, so it is a separate issue and not part of this change.
  • Likely the random faults of #2396 (left open: that run may also meet the watchdog stop above). Others: nicoco23 (RTX 5070) and HasiKe (RTX 2070 SUPER, in #1608) see the random faults go away with LEONIDA_TITLE_WRITEBACK=0.

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** on GitHub · 2026-11-26 21:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2404) `OreoFR:fix/title-writeback-asks-first` → `main` · +93 −44 · 2 files · 2 commits · merged 2026-11-26 21:49 UTC Claimed in the coordination issue as: [RT-TITLE-WRITEBACK-STALE-BYTES](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) (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 - `d3d12_title_va` and `d3d12_title_va_shadow` (the write-back tests from #2129) pass. `tests/test_docs_env_vars.py` passes, and `scripts/check_no_game_data.py` is clean on both changed files. - Retail story load on `main` `fac8b4bb9`, 600 s, shaders retranslated, write-back on (default): - before (`main`, and `2d43417d4` the same apart from #2401): the title faulted at 95-170 s in 5 of 5 runs, each time somewhere else. One run on `fac8b4bb9` reached the watchdog stop at 186 s first. - with this change: no title fault in the load. 52 KiB went back in 313 s, against about 26 MiB by 186 s before. - The run then stops at 313 s on the title's hang watchdog (`+0x23cf683`), after the title polled a fence value that stopped arriving for about 50 s. The same stop happens with `LEONIDA_TITLE_WRITEBACK=0`, so it is a separate issue and not part of this change. - Likely the random faults of #2396 (left open: that run may also meet the watchdog stop above). Others: nicoco23 (RTX 5070) and HasiKe (RTX 2070 SUPER, in #1608) see the random faults go away with `LEONIDA_TITLE_WRITEBACK=0`. GPUs tested: NVIDIA GeForce RTX 4070 Laptop 8 GB, driver 621.40, Windows 11. AI-assisted: yes (Claude Code, Claude 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: runtime only) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:35:12 +00:00
github manually merged commit 46aeaaefab into gh-pr/2404/base 2026-11-27 00:35:13 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-26 21:49 UTC · original

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** on GitHub · 2026-11-26 21:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2404#issuecomment-7204092744) 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)
Author
Owner

@⁠HasiKe on GitHub · 2026-11-26 21:53 UTC · original

@⁠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** on GitHub · 2026-11-26 21:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2404#issuecomment-7204155253) @⁠OreoFR, I found the same stale `kClean` order independently today ([#1608](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/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](https://github.com/HasiKe/leonida/commit/c9c73f48c): 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.
Author
Owner

@⁠HasiKe on GitHub · 2026-11-26 21:53 UTC · original

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** on GitHub · 2026-11-26 21:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2404#issuecomment-7204161515) 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.
Author
Owner

@⁠HasiKe on GitHub · 2026-11-26 22:29 UTC · original

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.

**@⁠HasiKe** on GitHub · 2026-11-26 22:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2404#issuecomment-7204730004) Correction to [my slowdown numbers above](https://git.gtahub.st/github/ubiquitarianism--leonida/pulls/2404): 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.