Skip to content

D3D12.X: opt-in XCL_COPY_BACK=fault for GPU-written kept and RT/DS/UAV textures - #3601

Open
iggyvilla wants to merge 2 commits into
xcl-dev:amd-devfrom
iggyvilla:amd-copy-back-fault
Open

iggyvilla wants to merge 2 commits into
xcl-dev:amd-devfrom
iggyvilla:amd-copy-back-fault

Conversation

@iggyvilla

Copy link
Copy Markdown
Collaborator

Adds opt-in fault-driven GPU → CPU texture copy-back behind XCL_COPY_BACK=fault (off by default, requires XCL_GUEST_WRITE_GUARD=1 from stacked PR #3597) to address #2663 for GPU-owned RT/DS/UAV textures without eager per-submit GPU downloads.

Stacked dependency: Built cleanly on top of #3597 (1b6f6748, amd-guest-write-guard, currently open against amd-dev 41aa60ce). Contains two commits: 1b6f6748 (#3597) and 6950d2e5 (this PR). Does not duplicate amd-dev's existing Copy*/Resolve* fence-tied download path (#3444) or open main PRs #3549 / #3550 — instead it covers direct RT/DS/UAV writes (where eager per-submit downloads would transfer hundreds of MB/frame) and coexists deadlock-free via copy_back::disarm_overlapping before title_textures::publish_download.

TL;DR — Before vs After (RX 6650 XT 8 GB, origin/amd-dev 41aa60ce)

Scenario Metric Before (XCL_COPY_BACK off) After (XCL_COPY_BACK=fault) Delta
CPU read of GPU-cleared RT / UAV texture (vram::place) Title memory contents (copy_back_gpu_owned_rt_and_uav_texture_reads_back_on_cpu_touch) Stale 0x5A bytes (#2663) Exact GPU texels re-tiled into console layout (downloads from tracked post-submit state on fault) #2663 RT/UAV staleness fixed
CPU read of kept texture written without Signal / on COPY queue Title memory contents (copy_back_a_cpu_read_of_a_gpu_written_texture_sees_the_gpu_texels) Stale 0x5A bytes Exact GPU texels after waiting on writing submit fence Fault-driven fallback works
Untouched GPU-owned RT/DS/UAV textures (retail title) GPU downloads / submit overhead 0 downloads / baseline 0 downloads, +0.01 ms/submit, 0 failed, 0 crashes (imported coherent UMA skipped in O(1)) Zero download cost when untouched
60 s smoke (6950d2e5, default off vs main d8bd9a38) Frames shown / Peak commit 1,618 / 15,479 MB (main) 1,648 / 18,038 MB (off) +1.9% frames / 1.165x commit
60 s smoke (6950d2e5, opt-in on) Frames shown / Peak commit 1,648 / 18,038 MB (off) 1,659 / 18,045 MB (=fault) +0.7% frames / +7 MB / 0 crashes

What is in it

  • crates/xcl/src/hle/d3d12x/copy_back.rs (new):
    • Opt-in XCL_COPY_BACK=fault (default off; refuses to activate and logs a warning if XCL_GUEST_WRITE_GUARD=1 is not set).
    • GPU-owned RT/DS/UAV registry (register_gpu_owned): tracks single-plane non-MSAA vram::place textures (reusing xg::Texture::new(&x) in device_x.rs), their CPU-accessible title VA (mirror_source_of), and per-subresource D3D12_RESOURCE_STATES.
    • Write & transition tracking: records GPU writes from ClearRenderTargetView, OMSetRenderTargets (RTV + DSV), ClearDepthStencilView, ClearUnorderedAccessView*, D3D12_RESOURCE_BARRIER_TYPE_UAV, resource transitions touching RENDER_TARGET | UNORDERED_ACCESS | DEPTH_WRITE | COPY_DEST | RESOLVE_DEST, and Copy*/Resolve*.
    • Post-submit arming (after_submit): signals a native per-queue fence (real::native_queue_signal, bypassing title Signal hooks), skips unsupported (multi-plane / MSAA) and imported coherent UMA ranges (!uma.shadowed() && uma.in_committed_window, where Windows dxgkrnl rejects VirtualProtect(..., PAGE_NOACCESS) with error 87 and VirtualQuery walks ~500k PTEs), registers whole committed pages with guest_write, and marks them PAGE_NOACCESS via guest_write::while_unpinned.
    • Vectored fault handler (fault): on CPU read/write access violation inside an armed texture, GPU-waits for the writing submit's fence on XCL's upload queue (real::native_queue_wait), downloads all subresources from tracked post-submit states (gpu::texture::download), restores PAGE_READWRITE, and re-tiles changed 4 KiB pages into console layout (title_textures::write_subresources). Pages always reopen even on failure so the title continues.
    • Coexistence with eager Copy*/Resolve* downloads: disarm_overlapping(va, len) (taking HANDLER → STATE lock order) opens any overlapping armed pages before title_textures::publish_download reads title bytes or calls write_changed_pages, preventing fence deadlocks.
    • Periodic diagnostics (submit_cost.rs): logs armed count, faults, MB read back, skip breakdown (uma/unsup/small/overlap/prot), and live GPU-owned VA census every 1024 submits when enabled.
  • crates/xcl/src/gpu/texture.rs: adds texture::download (using real::native_queue_execute and retaining objects on failed wait).
  • crates/xcl/src/hle/d3d12x/title_textures.rs: adds write_subresources, write_writable, download_and_write_after, read_back_after, read_back, copy_back_range, mirror_source_of, disarm_overlapping hook in publish_download, and 5 unit/GPU tests.
  • crates/xcl/src/hle/kernelx/guest_write.rs: adds while_unpinned (serialized across protectors without holding PINS across VirtualProtect, with open_with wait synchronization), meets_guarded_view, and a unit test.
  • crates/xcl/src/hle/d3d12x/{device_x,list_x,real,present,submit_cost,mod}.rs: opt-in hooks gated behind copy_back::on() (single relaxed atomic load when off) plus forget on resource release and forget_queue on queue retirement.

Tested

  • Hardware / OS / Rust: AMD Radeon RX 6650 XT (8 GB RDNA2, driver 32.0.21045.29003), AMD Ryzen 7 5700X, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09).
  • cargo test -p xcl --lib -- copy_back read_back written_subresources write_changed_pages download guest_write --test-threads=1 (debug): 25 passed, 0 failed, 0 ignored (includes end-to-end adopted-hook GPU RT clear + transition test, write_changed_pages coexistence via disarm_overlapping, multi-plane DS skip, imported coherent UMA skip, kept-texture fence-wait test, unmapped/RO page handling, and guest_write::while_unpinned).
  • cargo test -p xcl --test guest_write_probe -- --include-ignored --test-threads=1 (debug): 2 passed, 0 failed.
  • Matched 60 s retail smoke on 6950d2e5 (release):
    • Default off: 1,648 frames (60 60 60), 18,038 MB peak commit, exit: 0x0, 0 device removals, 0 crashes.
    • XCL_GUEST_WRITE_GUARD=1 XCL_COPY_BACK=fault: 1,659 frames (60 60 60), 18,045 MB peak commit, exit: 0x0, 0 device removals, 0 crashes, 0 guest write: warnings, 0 failed read-backs; live census observed 919 live GPU-owned textures (418 / 840.9 MB in imported coherent UMA skipped cleanly in O(1), 504 / 3,036.6 MB outside imported coherent UMA, 20 multi-plane/MSAA skipped).

Regression run

Baseline (main):

xcl-smoke v1
commit: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15479 MB

PR head (6950d2e5, default off):

xcl-smoke v1
commit: 6950d2e5fc225c3aac564d87f957b10176881816
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1648 (PresentX 1648)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 18038 MB

PR head (6950d2e5, XCL_GUEST_WRITE_GUARD=1 XCL_COPY_BACK=fault):

xcl-smoke v1
commit: 6950d2e5fc225c3aac564d87f957b10176881816
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1659 (PresentX 1659)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 18045 MB

Not tested

  • NVIDIA or Intel GPUs, Wine/Linux, or multi-GPU systems.
  • Kernel reads (WriteFile / send) directly from armed PAGE_NOACCESS texture pages (not hooked by guest_write).
  • GPU-owned textures placed inside Windows imported coherent UMA (OpenExistingHeapFromAddress), where dxgkrnl rejects VirtualProtect(..., PAGE_NOACCESS) with error 87; these are skipped upfront and logged in the census (handling CPU reads of coherent-UMA RT/UAV targets would require a separate non-fault path or placing GPU-owned textures outside the imported coherent window).
  • Retail gameplay scenes where the title actually reads a GPU-written RT/UAV texture on the CPU (the 60 s retail smoke tests arming/skipping/coexistence/overhead with 0 faults; actual fault-driven GPU → CPU RT/UAV read-back is verified end-to-end by the GPU tests).

AI use

  • Claude Code (Claude Opus 5.5) drafted the initial XCL_COPY_BACK=fault prototype (a86dd8fa) for kept sampled textures and unit/GPU test harness; Antigravity (Gemini 4 Neon) extended it with GPU-owned RT/DS/UAV tracking, upfront imported coherent-UMA skip, title_textures::publish_download coexistence (disarm_overlapping), native queue signal/wait integration on amd-dev 41aa60ce, end-to-end RT GPU tests, and matched 60 s smoke verification.

…V textures

Adds fault-driven GPU -> CPU texture copy-back behind opt-in XCL_COPY_BACK=fault (default off, requires XCL_GUEST_WRITE_GUARD=1 from xcl-dev#3597) to address xcl-dev#2663 for GPU-owned RT/DS/UAV textures without paying eager per-submit download cost.

- Tracks GPU-owned placed textures (vram::place: RTV/DSV/UAV binds, clears, UAV barriers, and resource state transitions touching RENDER_TARGET|UNORDERED_ACCESS|DEPTH_WRITE|COPY_DEST|RESOLVE_DEST) alongside kept sampled textures, arming whole committed title pages PAGE_NOACCESS behind a native per-queue fence after submission.
- On CPU access violation, waits for the writing submit fence on XCL's upload queue, downloads from tracked post-submit resource states (gpu::texture::download), opens the pages, and re-tiles changed 4 KiB pages into console layout (title_textures::write_subresources).
- Skips imported coherent UMA ranges (!uma.shadowed() && uma.in_committed_window) upfront in O(1), avoiding Windows OpenExistingHeapFromAddress VirtualProtect error 87 and ~20 ms VirtualQuery PTE walks.
- Coexists deadlock-free with title_textures's eager Copy/Resolve download publisher via copy_back::disarm_overlapping before write_changed_pages, and serializes page-protection against pinned kernel writes via guest_write::while_unpinned.
- Adds unit and GPU tests covering kept textures, end-to-end adopted-hook GPU-owned RT clear + state transitions, write_changed_pages coexistence, multi-plane DS skip, coherent UMA skip, and guest_write::while_unpinned.
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:directstorage DirectStorage queues, requests, decompression area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:tests Unit, integration, e2e tests, or testtitle gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 6950d2e, diff read, not a merge-check):

  • Too large to review as one PR. 2,703 changed lines, none of them generated or test data; 1,018 are KernelX, D3D12.X, DirectStorage: opt-in XCL_GUEST_WRITE_GUARD for XCL-protected title pages #3597 (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 KernelX, D3D12.X, DirectStorage: opt-in XCL_GUEST_WRITE_GUARD for XCL-protected title pages #3597, each under ~400 lines, for example: (1) gpu::texture::download plus title_textures::write_subresources / write_writable and their round-trip tests, not yet called; (2) copy_back.rs fault mode for kept textures with the after_submit / list_reset / forget hooks 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 in device_x.rs, real.rs, list_x.rs) with its test; (4) the disarm_overlapping call in publish_download; (5) the census and line() diagnostics in submit_cost.rs. guest_write::while_unpinned belongs with KernelX, D3D12.X, DirectStorage: opt-in XCL_GUEST_WRITE_GUARD for XCL-protected title pages #3597 or in its own PR.
  • Pages become no-access before the handler accepts faults on them. In copy_back.rs arm, protect_rw_runs makes the pages PAGE_NOACCESS at ~469, but install() only runs at ~477 and ANY_ARMED is set at ~480, and handler returns EXCEPTION_CONTINUE_SEARCH while ANY_ARMED is 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, including guest_write::open_with, which waits exactly until while_unpinned's protect returns and then touches the page (guest_write.rs ~330-338). Installing the handler and setting ANY_ARMED before protecting fixes it; fault already waits on STATE until the entry is inserted.

Not reviewed line by line because of the size. Fine: everything is behind copy_back::on() and stays off unless both XCL_COPY_BACK=fault and XCL_GUEST_WRITE_GUARD=1 are set; pages are reopened after a failed read-back; download keeps 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:directstorage DirectStorage queues, requests, decompression area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:tests Unit, integration, e2e tests, or testtitle gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants