d3d12: every title write-back range on one guarded page goes back (RT-TITLE-WRITEBACK-SAME-PAGE-RANGES) #2470
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2470"
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?
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-27T14:43:03Z.
Claimed in the coordination issue as: RT-TITLE-WRITEBACK-SAME-PAGE-RANGES (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7219907123). Found by @73N1X (their finding in #1608, 2026-11-27 11:40).
Depends on: none (builds on #2468, now merged;
upstream/mainmerged in)What and why
DeviceState::write_back_titleshanded each finished range toShadowPages::write_backon its own. When the first range that differs is on a guarded page,write_backreopens that page, andreopen_lockedclearskProtected. Every later disjoint range on that page then fails theheld()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_backtakes all of one tracker's finished ranges in one call (const std::pair<uintptr_t, uintptr_t>*, count, optionalreachedcount). 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_titlessorts 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 fromreached.lock_is taken. A thread that faults on a guarded page waits onlock_inreopen(), and it may hold the heap lock while it waits.How it relates to the other work on this path:
heldonce per page before the copies of one range. This PR now builds on it: the same snapshot, in #2468'sheld_pages_member, covers all of a tracker's ranges, and #2468's tile test stays.note_title_write. This PR does not touch that function and is about disjoint ranges.write_back_titles. This PR changes only the loop after it (the per-range calls). The two edit neighbouring lines, so a textual conflict is possible, but no logic is shared.writer_handling is unchanged.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.ec91d695c: fails 3/3.1/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title'sWrite-back, shadow and title-memory tests (
-R "write_back|writeback|shadow|title_memory|title_va|title_window|placed"): 19/19 pass, includingd3d12_title_window_write_back_fault(#2418),d3d12_placed_write_backandd3d12_shadow_keep_refill(_off).Full suite (
ctest -C Release -j 4): 498/502. The four failures:big_compile_gatetimed out under-j 4and passes alone.xs_proxy_mockfailed under-j 4and passes alone.xbox_present(present_planes) andd3d12_set_descriptor_heaps(recorded_commands) also fail on the base with the originald3d12_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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T15:20:40Z.
Code review location:
runtime/shims/d3d12_vulkan_device.cpp; line 439Good that
held_pagesis allocated beforeAcquireSRWLockExclusive(&lock_)and destroyed afterReleaseSRWLockExclusive(&lock_)on every return path, avoiding the fault-handler process-heap deadlock. Keeping it as a reusableVector<std::uint8_t>(leonida::host_heap) member onShadowPages(as in#2468) would also avoid per-call process-heap allocations when two ranges touch opposite ends of a large tracker.Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T15:20:40Z.
Code review location:
runtime/shims/d3d12_vulkan_device.cpp; line 508Minor:
bool openedis reset per ranger, so when many disjoint ranges share one guarded page (e.g. 130 four-byte ranges on one page), range 0 clearskProtectedand ranges 1..129 still enterreopen_locked(at, at + pages * kPage)and issue aVirtualQueryeach time. Skippingreopen_lockedwhen no page in[at, at + pages * kPage)still haskProtectedset avoids the redundantVirtualQuerycalls.Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T15:20:40Z.
Review: COMMENTED
Reviewed and tested at
27c167376(merged cleanly ontomainec91d695c).Batching all finished ranges per tracker into one
ShadowPages::write_back()call and snapshottingheld_pagesonce 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:
#2468: snapshottingheld_pagesacross 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 inruntime/CMakeLists.txt,runtime/shims/d3d12_vulkan_device.cpp, andruntime/test/d3d12_title_va_test.cpp; if#2470lands, pulling in#2468'shost_heapheld_pages_member and--window-writeback-fault-tiletest covers both issues cleanly.held_pagesallocation and repeatedreopen_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_validationis 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 passedscripts/check_no_game_data.pyandgit diff --check: cleanAI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by djrolfes on 2026-11-27T15:26:29Z.
Independent Linux/Wine + NVIDIA check at head
27c167376(merged cleanly ontoorigin/mainec91d695cin a local integration stack alongside#2467,#2463,#2469,#2461,#2126,#2091, and two local V# patches).7.2.15,wine-11.22,clang-cl+xwincross-build, NVIDIA GeForce RTX 3070 Ti 8 GB, driver620.73.04, Ryzen 7 5800X, 32 GB RAM. No game files.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 seach).d3d12_title_window_write_back_fault: skipped as expected under Wine (write watching is off).held_pagesbefore takinglock_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.
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-27T16:08:40Z.
Code review location:
runtime/shims/d3d12_vulkan_device.cpp; line 439Thanks, done in
d7b81dd43. The merge with main brought in #2468'sheld_pages_(aleonida::host_heapVector member), and this branch now uses it for the snapshot over all ranges. I size it beforelock_is taken (onlywrite_backuses it, and onewrite_backruns at a time undershadow_mutex), so the lock-ordering point still holds and the capacity is reused between calls. AI-assisted (Claude Code).Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-27T16:08:42Z.
Code review location:
runtime/shims/d3d12_vulkan_device.cpp; line 508Good catch, thanks. Done in
d7b81dd43: before callingreopen_lockedfor a run,write_backnow checks whether any page in[at, at + pages * kPage)still haskProtected, and skips the call (and itsVirtualQuery) when an earlier range already reopened them.reopen_lockedonly touches pages of ours, so the result is the same.d3d12_title_window_write_back_rangesand the #2468 tile test still pass. AI-assisted (Claude Code).Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-27T16:08:44Z.
Merged
upstream/main(no rebase) ind7b81dd43. 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):
heldonce per page before the copies, so that a faulting copy's tile reopen on the same thread no longer dropped the later runs of that range. Credit for that snapshot, theheld_pages_member and the--window-writeback-fault-tiletest goes to OreoFR (and to DadaCap, whose review found it).write_backalone before.d3d12_title_window_write_back_fault_tileandd3d12_title_window_write_back_ranges.Also from lixonik's notes: a run whose pages an earlier range already reopened skips
reopen_locked(noVirtualQuery).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).Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-27T18:49:40Z.
Merge test for the #2464 campaign on an 8 GB laptop (profile M). The PR head already contains main
ec91d695c. The numbers were recomputed from the raw logs. AI-assisted (Claude Code, Claude Opus 5.5).d3d12_title_va_testfrom this head, built in RelWithDebInfo: passed (title memory 64/64, all negative controls held).Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-27T18:53:36Z.
Addendum to the RUN above: I built this PR's
runtime/test/d3d12_title_va_test.cppagainst mainec91d695con 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).Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-27T19:06:51Z.
@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 ctestd3d12_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.cppunchanged:--window-writeback-rangesmain(currentupstream/main, which includes #2468; only the test file taken from the PR)1/16 four-byte ranges on one guarded page reached it, fails atline 787: reached == kRangesd7b81dd43)16/16 ... reached it, 48/48 words between them kept the title's, 0 words neitherOn 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_testpasses 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)
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-27T19:38:51Z.
@Tyrannys thanks, that was it. With the flag, on the same machine (RTX 4070 Laptop, NVIDIA 621.40, Windows 11):
d7b81dd43+ mainec91d695cmerged):d3d12_title_va_test --window-writeback-rangesexit 0:16/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's.ec91d695c(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).
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T20:06:20Z.
Review: APPROVED
Re-reviewed
d7b81dd43(merge withmain/ #2468 + review fixes) and tested on RTX 5070:held_pages_(leonida::host_heap) is sized beforeAcquireSRWLockExclusive(&lock_)across[lo, hi), and subsequent ranges on already-reopened pages skipreopen_locked/VirtualQuerywhen no page in the run retainskProtected.VK_LAYER_KHRONOS_validationnot 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).
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T13:09:31Z.
Merged current
main(d48a957bb) in0ebe31d3a. It conflicted with main's retired-window write-back.ShadowPages::write_backkeeps this PR's form: every range of a tracker in one call, withreached. It also takes main'sonlyparameter (a retired window's copy), and runs main's two-pass, byte-level copy for each run of unheld pages. A run reopens its pages only while one of them is still guarded, as before.free_retired_shadowpasses its one range in the new form.d3d12_title_va_test'ssubmit_writetakes bothbase(this PR) andwait(main).Profile M (RTX 4070 SUPER, 621.01, Windows 11), Release:
ctest -R "title|write_back|writeback|retire": 23/23 pass, includingd3d12_title_window_write_back_ranges(16/16 ranges) andd3d12_title_window_orphans(main's retired-window case).-j 14: 509/514 pass.api_ref_state,xs_proxy_mock,d3d12_queue_handoverandd3d12_queue_handover_two_queuesfailed under load and pass when run alone.xbox_presentfails at present withVkResult -11on this machine; it does not use the write-back path changed here.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T13:42:45Z.
Review: APPROVED
Re-verified merged head
0ebe31d3a(merge withmaind48a957bbretired-window write-back).Code audit:
ShadowPages::write_backcleanly preserves the batched multi-rangeheld_pages_snapshot (sized beforeAcquireSRWLockExclusive) together withmain'sonlyparameter and two-pass byte-level retired-window copy, and skips redundantreopen_lockedcalls when no page in the run retainskProtected.Verification at
0ebe31d3a(Windows 11, MSVC 19.44, Ninja Release,LEONIDA_VK_VALIDATION=1):d3d12_title_va_test.ctest -R '^(d3d12_title_window_write_back|d3d12_title_window_orphans|d3d12_title_window_write_back_fault|d3d12_title_window_write_back_fault_tile|d3d12_title_window_write_back_ranges|d3d12_placed_write_back)$': 6/6 passed (0 validation errors), including bothd3d12_title_window_write_back_rangesandd3d12_title_window_orphans.python -I scripts/check_no_game_data.py: passed.GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341,
VK_LAYER_KHRONOS_validation1.4.363).AI-assisted (Antigravity, Gemini).
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T15:33:49Z.
RUN: #2470 at
0ebe31d3a, merged with mainc1ecf04ed: red on main, green with the PR0ebe31d3ae1e6b1071537cf5657b701e52a49fa8, mainc1ecf04edcb827e49b863cb662ecb39fa904d615.git merge --no-ff 0ebe31d3aon main merged clean (no conflict), giving778ccf538(3 files:runtime/CMakeLists.txt+4,runtime/shims/d3d12_vulkan_device.cpp,runtime/test/d3d12_title_va_test.cpp).runtime/test/d3d12_title_va_test.cppand its new ctest entry. The shim is main's.--window-writeback-ranges(ctestd3d12_title_window_write_back_ranges), no longer--window-writeback-same-page.Build: Ninja RelWithDebInfo, MSVC 2022,
-DLEONIDA_REQUIRE_VULKAN=ON, targetd3d12_title_va_test,-j 6.--window-writeback-rangeswrite back ranges: 1/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's, 0 words neitherwrite back ranges: 16/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's, 0 words neither/title window write back ranges: every range on one guarded page reached itctest on green (gta6rt launchers excluded):
It covers
d3d12_title_va,_shadow,d3d12_geometry_views, the 7d3d12_title_window_*tests (write_back,_fault,_fault_tile,_ranges,cap,orphans,orphans_free),d3d12_placed_*(3),d3d12_shadow_keep_refill(_off),d3d12_api_descriptor_copy(_off),d3d12_rebound_*(3),d3d12_title_index_window,d3d12_title_vertex_window,shadow_keepandtitle_va_alias.Verdict: the test fails on main and passes with the PR. With main merged, nothing regressed in the nearby tests.
AI-assisted (Claude Code, Claude Opus 5.5). Follow-up to my RUN at
d7b81dd43, re-run after the merge of maind48a957bb. Tested on Windows 11, RTX 4070 Laptop 8 GB, NVIDIA 621.40. No game files used.Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T23:23:22Z.
#2470 | head
0ebe31d3ae(merged cleanly with mainadfb3821a2— no conflicts on currentmain) |ctest --test-dir runtime/build -C RelWithDebInfo -R "^(d3d12_title_window_write_back_ranges|d3d12_title_window_write_back|d3d12_title_window_write_back_fault|d3d12_title_window_write_back_fault_tile|d3d12_title_window_orphans|d3d12_title_window_orphans_free|d3d12_placed_write_back|d3d12_title_va|d3d12_title_va_shadow)$" --output-on-failure(LEONIDA_VK_VALIDATION=1) | red on main: buildingd3d12_title_va_testagainstmain'sruntime/shims/d3d12_vulkan_device.cppfailsd3d12_title_window_write_back_ranges(line 791: reached == kRanges,write back ranges: 1/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's, 0 words neither) | green on PR+main:9/9CTests (d3d12_title_window_write_back_ranges,d3d12_title_window_write_back,d3d12_title_window_write_back_fault,d3d12_title_window_write_back_fault_tile,d3d12_title_window_orphans,d3d12_title_window_orphans_free,d3d12_placed_write_back,d3d12_title_va,d3d12_title_va_shadow) pass in5.11 son RTX 3090 with0Khronos validation errors;check_no_game_data.py0violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yesImported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-29T01:55:52Z.
Merged, thank you @Tyrannys! The same-page case fails on main and passes with this PR in OreoFR's and zmasi's runs at this head (
0ebe31d3a), the nearby write-back tests stayed green, and it still merges cleanly into today's main. Thanks as well to djrolfes, lixonik, OreoFR and zmasi for testing it.AI-assisted (Claude Code, Claude Opus 5.5), on behalf of DadaCap.