D3D12.X: opt-in XCL_COPY_BACK=fault for GPU-written kept textures #3601
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3601"
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 ivall/xcl. Originally posted by iggyvilla on 2026-12-01T18:15:00Z.
Stacked on #3597 and #3675; only the top commit (
97098ee9,+538/-1across 5 files) belongs here.Adds opt-in
XCL_COPY_BACK=fault(default off; requiresXCL_GUEST_WRITE_GUARD=1from #3597) for GPU-written kepttitle_textures(#2663), split per review out of the previous monolithic commit (read-back helpers are now in #3675; GPU-owned RT/DS/UAV tracking will follow in a separate stacked PR on this one).TL;DR — Before vs After
origin/amd-dev41aa60ce)97098ee9,XCL_COPY_BACKunset)1,685lines (monolithic)+538 / -1(431runtime +107test lines)arm())protect_rw_runsran beforeinstall()/ANY_ARMEDinstall()andANY_ARMED = truerun underSTATEbeforeprotect_rw_runs1,654/15,902 MB(0crashes)1,646/16,607 MB(0crashes)What is in it (top commit
97098ee9only)crates/xcl/src/hle/d3d12x/copy_back.rs:XCL_COPY_BACK=faultstate machine for kept textures: records list writes (note_write), arms whole committed 4 KiB title pagesPAGE_NOACCESSbehind a native per-queue fence (after_submit→signal→arm), skips imported coherent UMA (!uma.shadowed() && uma.in_committed_window) and guarded views upfront, serializesVirtualProtectagainst pinned writes viaguest_write::while_unpinned, installs the VEH handler and setsANY_ARMEDunderSTATEbefore protecting pages, downloads viatitle_textures::read_back_afteron fault, and disarms overlapping ranges (disarm_overlapping) before eagerpublish_downloadwrites.crates/xcl/src/hle/d3d12x/list_x.rs,present.rs,title_textures.rs,mod.rs: wiresnote_write,list_reset,after_submit,forget,forget_queue, anddisarm_overlapping(all no-ops when off), plus GPU fault + fence-wait +disarm_overlappingtestcopy_back_a_cpu_read_of_a_gpu_written_texture_sees_the_gpu_texels.Tested
32.0.21045.29003), AMD Ryzen 7 5700X, Windows 11 x64,rustc 1.100.0 (0ef1bc386 2026-11-09), debug tests + release smoke.XCL_ADAPTER=warp cargo test -p xcl --lib -- hle::d3d12x::title_textures::tests -- --test-threads=4(3 consecutive runs on97098ee9):copy_back_a_cpu_read_of_a_gpu_written_texture_sees_the_gpu_texelspassed 3/3 cleanly (fixed test setup clearingpending+ callingTable::mark_gpu_writtenso concurrentrefresh()cannot readPAGE_NOACCESSpages duringround=1's 200 ms fence wait).cargo test --workspace --no-fail-fast -- --test-threads=4: 2,960 passed, 16 pre-existing failed, 49 ignored, 7 SKIPPED (same pre-existing failures as cleanorigin/amd-dev41aa60ce).Regression run
Base (
main, required bypr_check.py):Base (
origin/amd-devmerge base):PR head (
97098ee9,XCL_COPY_BACKunset):Not tested
AI use
install()/ANY_ARMEDordering race inarm(), ran the GPU tests and 60 s smoke, and updated this description.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T20:34:02Z.
Review (head
6950d2e5, diff read, not a merge-check):1b6f6748) and this PR's own commit is 1,685 lines, with no reason given for going over ~800. Please split it along CONTRIBUTING.md and stack the parts on #3597, each under ~400 lines, for example: (1)gpu::texture::downloadplustitle_textures::write_subresources/write_writableand their round-trip tests, not yet called; (2)copy_back.rsfault mode for kept textures with theafter_submit/list_reset/forgethooks and the kept-texture GPU test; (3) the GPU-owned RT/DS/UAV registry (register_gpu_owned,note_transitions, the clear /OMSetRenderTargets/ UAV-barrier hooks indevice_x.rs,real.rs,list_x.rs) with its test; (4) thedisarm_overlappingcall inpublish_download; (5) the census andline()diagnostics insubmit_cost.rs.guest_write::while_unpinnedbelongs with #3597 or in its own PR.copy_back.rsarm,protect_rw_runsmakes the pagesPAGE_NOACCESSat ~469, butinstall()only runs at ~477 andANY_ARMEDis set at ~480, andhandlerreturnsEXCEPTION_CONTINUE_SEARCHwhileANY_ARMEDis false (~616). On the first arm, or whenever nothing else is armed, a touch of those pages in that window is an unhandled access violation, includingguest_write::open_with, which waits exactly untilwhile_unpinned's protect returns and thentouches the page (guest_write.rs~330-338). Installing the handler and settingANY_ARMEDbefore protecting fixes it;faultalready waits onSTATEuntil the entry is inserted.Not reviewed line by line because of the size. Fine: everything is behind
copy_back::on()and stays off unless bothXCL_COPY_BACK=faultandXCL_GUEST_WRITE_GUARD=1are set; pages are reopened after a failed read-back;downloadkeeps its objects alive when the wait fails.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by iggyvilla on 2026-12-02T06:50:48Z.
Split and updated to
4cd5273daddressing @Foniass's review:\n- Split into small stacked PRs: movedguest_write::while_unpinned/meets_guarded_viewinto #3597 (07d529ea), splitgpu::texture::download+title_texturesread-back helpers into standalone #3675 (effdc0a8, 384 lines), narrowed this PR strictly to kept-texture fault mode +disarm_overlapping(own commit+532/-1, 431 runtime + 101 test lines, down from 1,685), and deferred GPU-owned RT/DS/UAV tracking to a follow-up stacked PR.\n- Fixed first-arm VEH race:arm()now callsinstall()and setsANY_ARMED = trueunderSTATEbeforeprotect_rw_runsmakes pagesPAGE_NOACCESS, so any concurrent touch immediately entershandlerand waits onSTATE.\n- Added whole-workspace GPU test counts and41aa60cemerge-base smoke.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T06:56:36Z.
Follow-up review (head
4cd5273d, since6950d2e5, diff read, not a merge-check):effdc0a8), #3597 (f601ec43, the same patch as its head07d529ea) and this PR's own4cd5273d, +532/-1 (about 430 runtime).armcallsinstall()and setsANY_ARMED(copy_back.rs~200-201) beforeprotect_rw_runs(~205), underSTATE, so a concurrent touch faults and waits infaultuntil the entry is in (~214).New:
faultremoves the entry and clearsANY_ARMED(~349-350), then waits inread_back_after(~359) for the fence and the download before opening the pages. A second thread reading the same pages in that window getsEXCEPTION_CONTINUE_SEARCHfromhandler: it returns at ~328 whenANY_ARMEDis false, and otherwisefaultfinds no entry andaccessibleseesPAGE_NOACCESS(~340), so the access violation is unhandled.forget(~309) anddisarm_overlapping(~291) leave the same window. The earlier head serialized faults with a lock, so a late fault found the page already open; that lock is gone.open_withlost wakeup reachable. This is the first caller ofguest_write::register(~203) andwhile_unpinned(~234), so withXCL_COPY_BACK=faultaReadFileor DirectStorage write over a texture being armed can spin forever atguest_write.rs~336 (see the follow-up on #3597).fault→read_back_after→download_and_write_afterqueues anative_queue_waitfor the title-queue fence on the shared upload queue (title_textures.rs~4323), so every XCL upload sent after it waits for the title queue;write_writable(~4260) skips unwritable pages and still reportsOk; aTEXTURESlock held over 200 ms (~4346) turns the fault into an error, marks the texturerefused, and the title reads stale bytes.copy_back::note_write/after_submitdirectly with a made-up list key (0x7E57_0001,title_textures.rs~8821), so nothing goes through the newlist_x.rshooks; no smoke run setsXCL_COPY_BACK=fault; and Tested reports the whole suite only ond8d9399b(22 failed), not on4cd5273d.Fine: off unless both
XCL_COPY_BACK=faultandXCL_GUEST_WRITE_GUARD=1are set, with every hook returning early when off; imported coherent UMA and guarded views are skipped before anything is protected; armed ranges cannot overlap;publish_downloaddisarms before reading title bytes; the lock order isTEXTURES→STATE.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T07:59:37Z.
Follow-up review (head
97098ee9, since4cd5273d, diff read, not a merge-check):4cd5273donly the GPU test changed: it now marks the texture GPU-written directly underTEXTURESinstead of going throughcapture_gpu_write/publish_submission_effects(title_textures.rs~8836-8847). Still open: a second thread touching an armed texture during a read-back gets an unhandled access violation (copy_back.rs~349-366), #3597'sopen_withlost wakeup is reachable through this PR'sregister/while_unpinnedcalls, the fault path's reliance on #3675's helpers, and no run withXCL_COPY_BACK=faultor through thelist_x.rshooks.New: none.
AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T08:14:41Z.
Follow-up review (head
06f79319, since97098ee9, diff read, not a merge-check; checks red):97098ee9only the GPU test changed, now also decrementingPENDINGwhen it clears a texture'spendingflag (title_textures.rs~8843). The open points from the previous review stand.New: none.
AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T13:35:47Z.
merge-check
base:
41aa60ce84head:
06f7931990merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2972 passed, 9 failed, 49 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on amd-dev); failing: gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_fract_f32_edges, hle::d3d12x::sparse::tests::private_physical_pages_reach_the_gpu_on_mapping, hle::d3d12x::title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, hle::d3d12x::title_textures::tests::remapped_pages_are_compared_with_what_they_held, hle::d3d12x::title_textures::tests::stream_recheck_keeps_the_whole_upload_baseline_over_partial_uploads, hle::d3d12x::title_textures::tests::stream_recheck_partial_upload_without_an_entry_queues_a_whole_upload, hle::d3d12x::title_textures::tests::stream_recheck_schedule_catches_late_writes_and_resets_milestones (the same tests fail on amd-dev
41aa60cealone on this card, so none is from this PR)review: read by one reviewer at this same head; the review is at https://github.com/xcl-dev/xcl/pull/3601#issuecomment-7339189925 and its findings stand. Most serious:
fault()(copy_back.rs~345) removes the armed entry beforeread_back_after, so a second thread touching the same pages meanwhile finds no entry and gets an unhandled access violation.performance: 60 s smoke of a retail title, amd-dev
41aa60cevs this merge (06f79319): 1671 -> 1691 frames shown (101 %; last presents per 2 s: 60 60 60), peak commit 16722 -> 16857 MB, 0 device removals, 0 crashes.verdict: not yet:
fault()(copy_back.rs~345) removes the armed entry beforeread_back_after, so a second thread touching the same pages meanwhile finds no entry and gets an unhandled access violation (see the review)hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
AI-written and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Code review location:
crates/xcl/src/hle/d3d12x/copy_back.rs; line 349Critical:
fault()removes the armed entry from STATE before the long fence wait and readback, so a second thread touching the same still-PAGE_NOACCESS pages finds no entry,accessible()is false, and the handler returns EXCEPTION_CONTINUE_SEARCH: an unhandled access violation. Thread A reads texture T and faults;fault()takes T's entry and blocks inread_back_afteron the queue fence. Thread B (another title worker, orguest_write::open'stouch()) reads T before A'sbefore_writeopens the pages, so B'sfault()returns false and the process crashes. Keeping the entry (marked in-progress) until the pages are opened, and having other faulters wait on it, would fix it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Code review location:
crates/xcl/src/hle/d3d12x/copy_back.rs; line 359Critical: Not certain about this one: the VEH handler blocks the faulting thread until the title queue's native fence completes (
native_queue_waitplustexture::download). If that GPU work waits on something the faulting thread itself will signal later, the thread deadlocks inside the exception handler. Say the title submits a copy into kept texture T on a queue that first doesqueue.Wait(fenceX, N), where fenceX is signalled from the CPU by thread A after it fills data. If A writes into one of T's pages before signalling, it faults and waits forever on a fence queued behind Wait(fenceX, N), so the title hangs. The test's round=1 avoids this only because a different thread signals the gate. A bounded wait that opens the pages with stale data (and logs) would avoid the hang.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Code review location:
crates/xcl/src/hle/d3d12x/copy_back.rs; line 234High: Armed pages are PAGE_NOACCESS for reads too, but guest_write only brackets kernel and XCL writes. Kernel reads (a title WriteFile/send from texture memory) and XCL's ReadProcessMemory-based
read_committed/with_title_bytesnever fault, so they fail or read zero-filled holes instead of triggering copy-back. A title saving a GPU-rendered kept texture with WriteFile(buffer = texture VA) gets ERROR_NOACCESS. An XCL refresh or upload that reads an overlapping CPU-watched resource gets zeros for armed pages and can upload them over the GPU copy.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Code review location:
crates/xcl/src/hle/d3d12x/copy_back.rs; line 231High:
arm()during an in-flightfault()ordisarm_overlapping()(entry already removed, pages not yet reopened) finds the pages PAGE_NOACCESS, andprotect_rw_runsskips them as non-RW, so the new GPU write is never tracked or copied back. If thread A faults on T and blocks on fence v1 while the title submits another copy into T,after_submit->arm(T)finds no T in by_res and every page NOACCESS, sorunsis empty and nothing is registered. A then reads back only up to v1 and opens the pages, so the v2 write never reaches CPU memory and later reads see stale texels with no further fault.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Code review location:
crates/xcl/src/hle/d3d12x/copy_back.rs; line 176High: Re-arming an already-armed texture replaces its (fence, value) with the latest submit's, so when the latest write is on a different queue the readback waits only on that queue and can read before an earlier write on the first queue finishes. If a long compute-queue copy writes T (armed with computeFence=5) and a quick direct-queue copy then writes another subresource of T (replaced with directFence=9), a CPU read faults, waits only for directFence >= 9, and downloads T while the compute write is still running. Keeping one (fence, value) per queue and waiting on all of them would fix it.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Code review location:
crates/xcl/src/hle/d3d12x/copy_back.rs; line 181High: A new texture whose range partially overlaps an armed one is silently not armed, and an exact-range overlap re-labels the entry to the new res, so aliased placed textures either lose copy-back or read back the wrong resource's texels. With placed A=[0x10000,0x30000) and B=[0x20000,0x40000) aliasing one heap, the GPU writes A (armed) then B, and
arm(B)sees the overlap and returns. A CPU read in B's range faults on A's entry and re-tiles A's stale GPU contents over the shared pages, B's newer texels never reach the CPU, and B's non-overlapping pages are never protected.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Code review location:
crates/xcl/src/hle/d3d12x/copy_back.rs; line 280High:
disarm_overlappingdrops every armed entry overlapping the download target and reopens its whole range without reading back, so GPU writes of an overlapping texture outside the eager download's bytes are lost. If kept texture T (armed, GPU-written, 1 MiB) overlaps a smaller eagerpublish_downloadtarget at T's first 64 KiB,disarm_overlappingreopens all of T's runs, the download fills only the 64 KiB, and CPU reads of T's other pages return pre-GPU contents with no fault. Reading back T first, or reopening only the download's pages, would avoid it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Code review location:
crates/xcl/src/hle/d3d12x/copy_back.rs; line 378Medium: Any
read_back_aftererror, including the transient "texture registry stayed busy" after 200 ms of TEXTURES contention, puts the texture inrefused, which onlyforget()clears, so one contended fault disables copy-back for that texture for good. If another thread holds TEXTURES for more than 200 ms (refresh scanning a large title) when a title thread faults on T, T is opened with stale CPU data and refused, every later GPU write to T is ignored byarm(), and the title reads stale texels for the rest of the session. Retrying transient errors would avoid it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:29Z.
Review: COMMENTED
Review of
06f79319: 8 finding(s) (2 critical, 5 high, 1 medium), most severe first.crates/xcl/src/hle/d3d12x/copy_back.rs:349:fault()removes the armed entry from STATE before the long fence wait and readback, so a second thread touching the same still-PAGE_NOACCESS pages finds no entry,accessible()is false, and the handler returns EXCEPTION_CONTINUE_SEAR.... (inline)crates/xcl/src/hle/d3d12x/copy_back.rs:359: Not certain about this one: the VEH handler blocks the faulting thread until the title queue's native fence completes (native_queue_waitplustexture::download). (inline)crates/xcl/src/hle/d3d12x/copy_back.rs:234: Armed pages are PAGE_NOACCESS for reads too, but guest_write only brackets kernel and XCL writes. (inline)crates/xcl/src/hle/d3d12x/copy_back.rs:231:arm()during an in-flightfault()ordisarm_overlapping()(entry already removed, pages not yet reopened) finds the pages PAGE_NOACCESS, andprotect_rw_runsskips them as non-RW, so the new GPU write is never tracked or copied back. (inline)crates/xcl/src/hle/d3d12x/copy_back.rs:176: Re-arming an already-armed texture replaces its (fence, value) with the latest submit's, so when the latest write is on a different queue the readback waits only on that queue and can read before an earlier write on the first queue finishes. (inline)crates/xcl/src/hle/d3d12x/copy_back.rs:181: A new texture whose range partially overlaps an armed one is silently not armed, and an exact-range overlap re-labels the entry to the new res, so aliased placed textures either lose copy-back or read back the wrong resource's texels. (inline)crates/xcl/src/hle/d3d12x/copy_back.rs:280:disarm_overlappingdrops every armed entry overlapping the download target and reopens its whole range without reading back, so GPU writes of an overlapping texture outside the eager download's bytes are lost. (inline)crates/xcl/src/hle/d3d12x/copy_back.rs:378: Anyread_back_aftererror, including the transient "texture registry stayed busy" after 200 ms of TEXTURES contention, puts the texture inrefused, which onlyforget()clears, so one contended fault disables copy-back for that text.... (inline)These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:45:32Z.
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.