Skip to content

d3d12: every title write-back range on one guarded page goes back (RT-TITLE-WRITEBACK-SAME-PAGE-RANGES) - #2470

Open
Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-title-writeback-same-page-ranges
Open

Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-title-writeback-same-page-ranges

Conversation

@Tyrannys

@Tyrannys Tyrannys commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-TITLE-WRITEBACK-SAME-PAGE-RANGES (#1608 (comment)). Found by @73N1X (their finding in #1608, 2026-11-27 11:40).

Depends on: none (builds on #2468, now merged; upstream/main merged in)

What and why

DeviceState::write_back_titles handed each finished range to ShadowPages::write_back on its own. When the first range that differs is on a guarded page, write_back reopens that page, and reopen_locked clears kProtected. Every later disjoint range on that page then fails the held() check (not protected, not clean) and is dropped as if the title had written the page. But only the write-back itself wrote there, so the GPU's later words on that page never reach the title, and the next forward copy writes the title's older bytes over them in the shadow. CPU code that reads GPU results back (the write-back case) sees stale words.

Fix:

  • ShadowPages::write_back takes all of one tracker's finished ranges in one call (const std::pair<uintptr_t, uintptr_t>*, count, optional reached count). It decides which pages are held once for every page the ranges cover, after the journal, pin and write-watch checks and before the first copy. Then it copies range by range as before.
  • write_back_titles sorts the finished ranges (batches can overlap) and gives each shadow the ranges over it in one call. The log line's range count now comes from reached.
  • The per-page decisions are allocated before lock_ is taken. A thread that faults on a guarded page waits on lock_ in reopen(), and it may hold the heap lock while it waits.

How it relates to the other work on this path:

Known limit, unchanged by this PR: a page opened by the write-back stays writable by the title until the next refresh guards it again. Before this PR that was already true for the pieces of a single range after the first.

Tested

New test d3d12_title_window_write_back_ranges (d3d12_title_va_test --window-writeback-ranges). It makes a title window and lets a submission that does not reach the ring guard the pages. Then the GPU writes new words into the window through its arena address, so nothing is noted for that write. Before that submission, the test notes 16 four-byte ranges 16 bytes apart on the first page. The next refresh copies them back.

  • base ec91d695c: fails 3/3. 1/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's
  • with the fix: passes 3/3. 16/16 ranges reach the page, the 48 words between them keep the title's, and no word has any other value.

Write-back, shadow and title-memory tests (-R "write_back|writeback|shadow|title_memory|title_va|title_window|placed"): 19/19 pass, including d3d12_title_window_write_back_fault (#2418), d3d12_placed_write_back and d3d12_shadow_keep_refill(_off).

Full suite (ctest -C Release -j 4): 498/502. The four failures:

  • big_compile_gate timed out under -j 4 and passes alone.
  • xs_proxy_mock failed under -j 4 and passes alone.
  • xbox_present (present_planes) and d3d12_set_descriptor_heaps (recorded_commands) also fail on the base with the original d3d12_vulkan_device.cpp, so they are not from this change.

No game files or shaders were needed.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11

AI-assisted: yes (Claude Code)

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)

🤖 Generated with Claude Code

…-TITLE-WRITEBACK-SAME-PAGE-RANGES)

DeviceState::write_back_titles handed each finished range to
ShadowPages::write_back on its own. The first range that differed reopened
its guarded page (kProtected cleared), and every later disjoint range on that
page then read as written by the title and was dropped, though only the
write-back itself had written there.

Each shadow now takes all of its finished ranges in one call, sorted by
address, and decides which pages are held once for all of them, before the
first copy. The per-page decisions are allocated before the tracker's lock is
taken.

New test d3d12_title_window_write_back_ranges: 16 four-byte ranges 16 bytes
apart on one guarded page of a title window. On main 1/16 reach the page
(3/3 runs); with the fix 16/16, and the words between them keep the title's.

Found by 73N1X in ProjectLeonida#1608.

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

@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.

Reviewed and tested at 27c167376 (merged cleanly onto main ec91d695c).

Batching all finished ranges per tracker into one ShadowPages::write_back() call and snapshotting held_pages once before the first copy cleanly fixes the multi-range-on-one-guarded-page drop, and passes all write-back GPU tests on RTX 5070.

Two coordination / optimization notes:

  • Overlap with #2468: snapshotting held_pages across all ranges in one call also addresses #2468's same-tile reopen bug (including #2468's noted multi-range-in-one-tile limit). Both PRs touch the same lines in runtime/CMakeLists.txt, runtime/shims/d3d12_vulkan_device.cpp, and runtime/test/d3d12_title_va_test.cpp; if #2470 lands, pulling in #2468's host_heap held_pages_ member and --window-writeback-fault-tile test covers both issues cleanly.
  • Two minor perf notes inline on held_pages allocation and repeated reopen_locked() calls for ranges sharing a page.

Verified locally on Windows 11 (MSVC 19.44.35230.0, Ninja Release, NVIDIA GeForce RTX 5070, driver 610.88, Vulkan 1.4.341; note VK_LAYER_KHRONOS_validation is not installed locally so Vulkan validation layers did not run):

  • d3d12_title_window_write_back, d3d12_title_window_write_back_fault, d3d12_title_window_write_back_ranges (16/16 four-byte ranges reached the guarded page, 48/48 surrounding words kept), d3d12_placed_write_back: 4/4 passed
  • scripts/check_no_game_data.py and git diff --check: clean

AI-assisted.

Comment thread runtime/shims/d3d12_vulkan_device.cpp Outdated
if (lo >= hi) return 0;
// Allocated before lock_ is taken: a thread that faults on a guarded page waits on lock_ in reopen(), and may
// hold the heap's lock while it does.
std::vector<std::uint8_t> held_pages((hi - lo) / kPage);

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.

Good that held_pages is allocated before AcquireSRWLockExclusive(&lock_) and destroyed after ReleaseSRWLockExclusive(&lock_) on every return path, avoiding the fault-handler process-heap deadlock. Keeping it as a reusable Vector<std::uint8_t> (leonida::host_heap) member on ShadowPages (as in #2468) would also avoid per-call process-heap allocations when two ranges touch opposite ends of a large tracker.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done in d7b81dd. The merge with main brought in #2468's held_pages_ (a leonida::host_heap Vector member), and this branch now uses it for the snapshot over all ranges. I size it before lock_ is taken (only write_back uses it, and one write_back runs at a time under shadow_mutex), so the lock-ordering point still holds and the capacity is reused between calls. AI-assisted (Claude Code).

Comment thread runtime/shims/d3d12_vulkan_device.cpp Outdated
const std::uint8_t* copy = t.shadow + (c - t.begin);
if (std::memcmp(reinterpret_cast<const void*>(c), copy, e - c) != 0) {
if (!opened) {
reopen_locked(at, at + pages * kPage);

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.

Minor: bool opened is reset per range r, so when many disjoint ranges share one guarded page (e.g. 130 four-byte ranges on one page), range 0 clears kProtected and ranges 1..129 still enter reopen_locked(at, at + pages * kPage) and issue a VirtualQuery each time. Skipping reopen_locked when no page in [at, at + pages * kPage) still has kProtected set avoids the redundant VirtualQuery calls.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks. Done in d7b81dd: before calling reopen_locked for a run, write_back now checks whether any page in [at, at + pages * kPage) still has kProtected, and skips the call (and its VirtualQuery) when an earlier range already reopened them. reopen_locked only touches pages of ours, so the result is the same. d3d12_title_window_write_back_ranges and the #2468 tile test still pass. AI-assisted (Claude Code).

@djrolfes

Copy link
Copy Markdown
Contributor

Independent Linux/Wine + NVIDIA check at head 27c167376 (merged cleanly onto origin/main ec91d695c in a local integration stack alongside #2467, #2463, #2469, #2461, #2126, #2091, and two local V# patches).

  • Environment: Linux 7.2.15, wine-11.22, clang-cl + xwin cross-build, NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Ryzen 7 5800X, 32 GB RAM. No game files.
  • CTest:
    • d3d12_title_window_write_back_ranges: passed (6.06 s) — 16/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's, 0 words neither.
    • d3d12_title_va, d3d12_title_va_shadow, d3d12_title_window_cap, d3d12_title_window_write_back: all passed (~6.0 s each).
    • d3d12_title_window_write_back_fault: skipped as expected under Wine (write watching is off).
  • Code review: allocating held_pages before taking lock_ and computing page hold state once across all sorted ranges per shadow cleanly avoids both the same-page reopen drop and the lock-inversion hazard.

AI-assisted.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 27, 2026
…ck-same-page-ranges

ubiquitarianism#2468 (RT-WRITEBACK-TILE-REOPEN) took write_back's per-page held decision
before the copies for one range. This branch takes it for all of a tracker's
ranges in one call, so the two are now one snapshot over every range, kept in
ubiquitarianism#2468's host-heap member held_pages_ (sized before lock_ is taken). Both tests
stay: d3d12_title_window_write_back_fault_tile and
d3d12_title_window_write_back_ranges; submit_write takes both va2 and base.

Also from lixonik's review: a run whose pages an earlier range already
reopened (none still kProtected) skips reopen_locked and its VirtualQuery.

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

Copy link
Copy Markdown
Contributor Author

Merged upstream/main (no rebase) in d7b81dd. Main now has #2468, so the conflict is resolved.

Thanks @lixonik for the review and the RTX 5070 run, and @djrolfes for the Linux/Wine check.

How this PR relates to #2468 (OreoFR, RT-WRITEBACK-TILE-REOPEN, now merged):

Also from lixonik's notes: a run whose pages an earlier range already reopened skips reopen_locked (no VirtualQuery).

Re-tested after the merge on RTX 4070 SUPER, driver 621.01, Windows 11. ctest -R "write_back|writeback|shadow|title_memory|title_va|title_window|placed": 20/20 pass, including both tests above. AI-assisted (Claude Code).

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 27, 2026
@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Merge test for the #2464 campaign on an 8 GB laptop (profile M). The PR head already contains main ec91d69. The numbers were recomputed from the raw logs. AI-assisted (Claude Code, Claude Opus 5.5).

RUN m2470m | 2026-11-27 | OreoFR | AI-assisted: Claude Code (Claude Opus 5.5)
BASE:        this PR at d7b81dd43 (contains main ec91d695c) | shaders: main translation + #2447 (one-lane) pack
MACHINE:     RTX 4070 Laptop 8 GB, laptop (profile M) | NVIDIA 621.40 | Windows 11 | 40 GB
RECIPE:      documented + DEVIATIONS: ASYNC_PIPELINES=1 ASYNC_MIN_BYTES=1048576 VALUE_WAIT_TIMEOUT_MS=30000 MAP_READBACK=1 DEVICE_TYPE=6 ARENA_MB=4095 DEVICE_FAULT=1
OS/DRIVER:   TdrDelay 60
TARGET:      regression check against regA (main ec91d695c, same recipe and caches) | PREDICTED: world reached, no loss
MEASURED:    world entry ~228 s (regA 223 s) | 0 device lost, 0 exceptions (regA 0 / 0) | gpu-stall lines 2 (regA 10)
END:         stopped by our script 53 s after world entry (t=281 s): the known display stop after world entry is the same on main
CACHES:      warm | RUN 1 of 1
INSTRUMENTS: device fault checkpoints, thread stacks
VERDICT:     no regression seen (n=1)
  • d3d12_title_va_test from this head, built in RelWithDebInfo: passed (title memory 64/64, all negative controls held).
  • I did not rerun the test on main here.

@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Addendum to the RUN above: I built this PR's runtime/test/d3d12_title_va_test.cpp against main ec91d69 on the same machine (RTX 4070 Laptop, 621.40). It also passes on main (exit 0, the same 64/64 and negative-control lines), so here it does not show the same-page bug the PR fixes. Either the same-page case only fails on another GPU or driver, or the test does not reach it yet. A case that fails on main would make the fix provable. AI-assisted (Claude Code, Claude Opus 5.5).

@Tyrannys

Tyrannys commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@OreoFR thanks for the merge run and for checking the test on main. That check matters. The same-page case is not in the default run of d3d12_title_va_test. It sits behind a flag, as the ctest d3d12_title_window_write_back_ranges = d3d12_title_va_test --window-writeback-ranges. A run with no arguments (the 64/64 title-memory run) never reaches it, so it passes on both. Sorry the PR body did not make that clear. (@MoonEyes: the same applies to the title_va variants in your #2477 run. That branch does not have this mode, so none of its 14 variants run it.)

The case: 16 disjoint four-byte ranges (words 0, 4, ..., 60) are noted on one guarded page of a write-watched window. The GPU writes all of them. The first range differs from the title's copy and reopens the page. Every later range on that page must still be copied back.

Rebuilt and run now (profile M: RTX 4070 SUPER, driver 621.01, Windows 11), with this PR's runtime/test/d3d12_title_va_test.cpp unchanged:

--window-writeback-ranges exit
on main (current upstream/main, which includes #2468; only the test file taken from the PR) 1/16 four-byte ranges on one guarded page reached it, fails at line 787: reached == kRanges 1 (3 of 3 runs)
this PR (d7b81dd43) 16/16 ... reached it, 48/48 words between them kept the title's, 0 words neither 0 (3 of 3 runs)

On main only the first range gets back: it reopens the page, and each later range then sees the page as written by the title and drops the GPU's data. The 48 words between the ranges stay the title's in both runs, so nothing is copied that should not be. The plain d3d12_title_va_test passes on both (exit 0), as you saw.

To reproduce on your machine: ctest -R d3d12_title_window_write_back_ranges, or the exe with --window-writeback-ranges.

AI-assisted (Claude Code)

@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

@Tyrannys thanks, that was it. With the flag, on the same machine (RTX 4070 Laptop, NVIDIA 621.40, Windows 11):

  • This PR (d7b81dd + main ec91d69 merged): d3d12_title_va_test --window-writeback-ranges exit 0: 16/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's.
  • Main ec91d69 (this PR's test file only): exit 1: 1/16 four-byte ranges on one guarded page reached it (the 48/48 untouched words are still kept).

So the test fails on main and passes with the PR: the same-page fix is shown here too. This replaces "it also passes on main" in my addendum above. 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.

Re-reviewed d7b81dd43 (merge with main / #2468 + review fixes) and tested on RTX 5070:

  • Both earlier comments are cleanly addressed: held_pages_ (leonida::host_heap) is sized before AcquireSRWLockExclusive(&lock_) across [lo, hi), and subsequent ranges on already-reopened pages skip reopen_locked / VirtualQuery when no page in the run retains kProtected.
  • Tested on NVIDIA GeForce RTX 5070 (Windows 11, driver 610.88, Vulkan 1.4.341, MSVC 19.44; VK_LAYER_KHRONOS_validation not installed locally):
    • ctest -R "^(d3d12_title_window_write_back|d3d12_title_window_write_back_fault|d3d12_title_window_write_back_fault_tile|d3d12_title_window_write_back_ranges|d3d12_placed_write_back)$": 5/5 passed (1.14 s).

Looks good to merge.

AI-assisted (Antigravity, Gemini).

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.

4 participants