Skip to content

D3D12.X: the per-draw mirror sync copies only the pages the title wrote - #2478

Open
KOLESNiii wants to merge 3 commits into
xcl-dev:mainfrom
KOLESNiii:d3d12x-mirror-sync-written-pages
Open

KOLESNiii wants to merge 3 commits into
xcl-dev:mainfrom
KOLESNiii:d3d12x-mirror-sync-written-pages

Conversation

@KOLESNiii

@KOLESNiii KOLESNiii commented Nov 25, 2026 •

Copy link
Copy Markdown

Stacked on #2437 (itself on #2416); only the top commit (D3D12.X: the per-draw mirror sync copies only the pages the title wrote) belongs here.

sync_title_buffers copies the drawn range of every mirrored buffer at every draw, whether or not the title wrote it. For a source in write-watched memory (kernelx reserves GPU-visible title memory that way), a draw now copies only the pages written since they were last copied. On the one title measured, this is slower, not faster (see Tested): the title rewrites these pages every frame, so few copies are saved, and the GetWriteWatch call per draw costs more than they did. Opened as a draft for review of the approach and for numbers from other titles.

Part of #2393 (the copies). Part of #2389.

What is in it

  • crates/xcl/src/hle/d3d12x/list_x.rs:
    • Which pages a draw copies: the pages on the write-watch (GetWriteWatch without reset), plus the pages a look took off it since the mirror's last copy (MIRROR_STALE, beside MIRRORS and not in it, to stay clear of D3D12.X: stage GPU-view CopyMemoryToMemoryX sources and shift newer CL1 remaps #1995).
    • When it copies the whole range: when the source is not write-watched, or when a look overlapped the draw (MIRROR_LOOK_SEQ).
    • Test: the_mirror_sync_copies_only_the_pages_the_title_wrote.
  • crates/xcl/src/hle/d3d12x/title_textures.rs: the look watches the mirror sources (a new part of WATCH_CACHE's key) and hands the pages it takes off the write-watch to list_x::mirror_pages_written. Without that, a page written after a draw's copy and reset by the look would never be copied again. The look also runs when mirrors exist with no sparse window and no title texture (look_needed; review below). Tests: a_look_runs_for_buffer_mirrors_alone, and the_look_before_a_submit_lets_the_mirror_sync_skip_unwritten_pages, which goes through refresh.
  • crates/xcl/src/hle/kernelx.rs: reserve_watched is pub(crate), for that test's write-watched title memory.

Tested

Regression run

main:

xcl-smoke v1
commit: e1d84b7d9153f86a97d2ec5d209d196046fc0bea
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1415 (PresentX 1415)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15388 MB

This PR:

xcl-smoke v1
commit: 77e48ea4ba7dd7492d14b237655279f8f083f0dc
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1433 (PresentX 1433)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15136 MB

That run made no mirrors (the 4095 MB coherent window was in place), so it doesn't reach the changed path; see Measurement for runs that do.

Not tested

  • Other titles: a title that does not rewrite its mirrored buffers every frame, where this should save the copies. None was available.
  • Writes the write-watch does not see: GPU writes into the source, and a title that resets the write-watch on its own memory. In both cases a draw would use stale data.
  • Other hardware: AMD and Intel GPUs, wave64.

AI use

  • Claude Code with Claude Opus 5.5: wrote the code, the test, the commit message and this description, and ran the tests and the runs.

🤖 Generated with Claude Code

@KOLESNiii
KOLESNiii marked this pull request as ready for review November 25, 2026 19:57
raylitebull added a commit that referenced this pull request Nov 25, 2026
Port upstream PR #2478 (by KOLESNiii, commit 2bdfb90) cleanly onto amd-dev.

sync_title_buffers previously copied the full drawn range of every mirrored buffer on every draw even when the title never rewrote it (#2389 / #2393).

Track write-watched mirror source pages via GetWriteWatch (without reset) plus MIRROR_STALE bits handed off by title_textures' write-watch look (mirror_look_begin / mirror_pages_written), falling back to full-range copy if a look overlaps the draw or the source is not write-watched. Includes regression test.

Part of #2393.

Assisted-by: Antigravity (Gemini 4 Neon High)

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

P2: the optimization is bypassed for a valid non-sparse UMA configuration with mirrored buffers but no title textures. refresh_once returns at title_textures.rs:722-727 before buffer mirror ranges are added/reset at :731-747. list_x.rs:3547-3565 then calls GetWriteWatch without RESET, so every page ever written stays dirty and is recopied on every draw. The new test calls the look helper directly and bypasses this return. Please run refresh when a mirror exists and add an end-to-end zero-texture/non-sparse coverage case.

@emirduran707

Copy link
Copy Markdown
Contributor

Independent AMD RDNA4 (native wave64) test report on exact head 2bdfb908c671fc488b7846ebbca198951ffb0114 (clean detached checkout, no local changes).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17; tests debug, smoke release.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 1275 passed, 2 failed, 46 ignored; 8 SKIPPED (6 in lower_dxil::tests::{f16,f32}_programs_on_the_gpu refused by per-lane form; heap_window_out_of_memory_falls_back_to_the_switch_form; pso_measurement_compiles_an_exact_synthetic_record). All native wave64 GPU tests ran and passed.
    • Both PR tests passed: hle::d3d12x::list_x::tests::the_mirror_sync_copies_around_a_page_the_title_made_unreadable and the_mirror_sync_copies_only_the_pages_the_title_wrote.
    • The 2 failures (gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 seed 722 and fuzz_validate_and_gpu_3 seed 1599) are pre-existing on unmodified main on this GPU.

Regression run (60 s retail-title smoke, XCL_NETWORK=0, release)

xcl-smoke v1
commit: 2bdfb908c671fc488b7846ebbca198951ffb0114
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1614 (PresentX 1614)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 13922 MB

(Matched main 68a284a baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)

Not tested

  • D3D12 debug layer; Intel GPUs.

AI use

  • Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.

@SlopTart

Copy link
Copy Markdown

gpu_ suite (cold cache): 164 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, and the promoted-UAV-clear driver gap, PR #2490); none caused by this PR. Note gpu_depth_buffer_created_for_unordered_access_is_placed is green on main now, #2466 did its job on this lane.
Targeted run of the two mirror-sync tests (the_mirror_sync_copies_only_the_pages_the_title_wrote, the_mirror_sync_copies_around_a_page_the_title_made_unreadable): 2 passed. Same lane this feature's earlier tests were verified on (#2437).

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

KOLESNiii added a commit to KOLESNiii/xcl that referenced this pull request Nov 25, 2026
sync_title_buffers copied the drawn range of every mirrored buffer at
every draw. On a retail game's menus that was ~1,200 copies and 10.7 MB
per 2 s, of buffers the title never rewrites (xcl-dev#2389).

A mirror's source in write-watched memory (kernelx reserves GPU-visible
title memory that way) now has its written pages copied only: a draw asks
GetWriteWatch without reset for the pages written since the last look,
and adds the pages the look took off the write-watch since the mirror's
last copy. title_textures' look watches the mirror sources and hands
those pages on (list_x::mirror_pages_written), so a page written after a
draw's copy and reset by the look is still copied at the next draw. A
draw that overlaps a look (MIRROR_LOOK_SEQ odd or changed) copies its
whole range, as does one whose source is not write-watched.

The stale-page bits live beside MIRRORS, not in it, so as not to conflict
with xcl-dev#1995, which re-keys MIRRORS.

The look also runs when mirrors exist with no sparse window and no title
texture (it returned at once there, so the sources were never reset and
every draw copied every page ever written; review on xcl-dev#2478).

Part of xcl-dev#2393 (the copies; the index scan is cached in the next change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KOLESNiii
KOLESNiii force-pushed the d3d12x-mirror-sync-written-pages branch from 2bdfb90 to a6ec51e Compare November 25, 2026 20:44
@KOLESNiii

Copy link
Copy Markdown
Author

@bmjask thanks, you're right. With no sparse window and no title texture, refresh_once returned before the look, so mirror sources were never reset and every draw copied every page ever written. Fixed in a6ec51e (squashed into the PR's commit):

  • title_textures::look_needed: the look also runs when a mirror exists (list_x::mirror_sources_gen() != 0).
  • a_look_runs_for_buffer_mirrors_alone covers that condition and fails with the old early return.
  • the_look_before_a_submit_lets_the_mirror_sync_skip_unwritten_pages goes end to end through refresh, on write-watched title memory from kernelx (reserve_watched): unchanged pages are skipped after a look, and a page written before the look is copied.
    • It doesn't cover the zero-texture, non-sparse case on my machine: the test process always has sparse windows here, so it passes with and without the fix. The predicate test is what covers that case. I don't see a way to force a non-sparse UMA in the shared test process without a test-only switch in gpu::uma; tell me if you want one.

cargo test --workspace -- --test-threads=4 on a6ec51e: 1279 passed, 0 failed, 46 ignored, 25 SKIPPED (RTX 2070 SUPER). The regression run for a6ec51e is still to come; the machine was short of commit memory for the one I tried.

Written with Claude Code (Claude Opus 5.5).

KOLESNiii and others added 2 commits November 25, 2026 22:22
Once any buffer mirror exists, sync_title_buffers runs on every draw and
nothing reported what it costs. With XCL_ACTIVITY=1 it now logs, every 2 s
in which it ran: draws synced and those with a mirror, copies (whole
buffers), bytes copied, indices scanned for the vertex range, readable()
calls (those over 16 pages, which can take the VirtualQuery walk), the time
in readable() and its slowest call, and the time in the function.

The slowest readable() is not in xcl-dev#2391's list: measuring on xcl-dev#2389 found
single calls of 16 pages or fewer taking 1-9 ms, which the other counters
average away.

The counters cost nothing without XCL_ACTIVITY: host_main turns them on
(enable_sync_activity) before it starts the activity thread, and until then
sync_title_buffers, sync_readable() and sync_count() check one relaxed
bool and take no time stamps and touch no shared counter.

No change in behaviour: the old body is sync_mirrors, unchanged apart
from the counters, and sync_title_buffers still returns first when no
mirror exists.

Fixes xcl-dev#2391. Part of xcl-dev#2389.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sync_title_buffers checked every copy's source, and the indices it scans,
with gpu::shader::readable. For a range with a page that is not resident,
or over 16 pages, that walks the region with VirtualQuery. Measured on
xcl-dev#2389, in 18 of 43 two-second windows one such call took 1.1-8.8 ms on
the render thread inside a draw.

The copy now uses title_textures::copy_committed_region (ReadProcessMemory,
which cannot fault), so a page the title freed or protected since keeps
what the mirror had. The indices are scanned from the index buffer's
mirror once copied there; an index buffer without a mirror is still
checked.

xcl-dev#2392 proposed checking once per mirror and again after kernelx frees or
reprotects memory. kernelx does not see HeapFree, which releases large
heap blocks inside ntdll, so that still needed a copy that cannot fault,
and with one the check has nothing left to protect.

Fixes xcl-dev#2392. Part of xcl-dev#2389.

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

Copy link
Copy Markdown

Heads-up for whichever of these merges second: #1995 and #2478 (stacked on #2437) both rewrite the per-draw mirror sync in list_x.rs, and git merge-tree reports a conflict between their heads. Beyond the text conflict:

Combined as they are, a GPU-view mirror's GetWriteWatch fails, so the draw copies the whole range every time. That stays correct, but it loses #2478's saving for exactly the buffers #1995 adds. Switching the write-watch and stale-page lookups to cpu_src_start when resolving would keep both. (Read from the diffs a6ec51e / bdfb2fb, not run.)

sync_title_buffers copied the drawn range of every mirrored buffer at
every draw. On a retail game's menus that was ~1,200 copies and 10.7 MB
per 2 s, of buffers the title never rewrites (xcl-dev#2389).

A mirror's source in write-watched memory (kernelx reserves GPU-visible
title memory that way) now has its written pages copied only: a draw asks
GetWriteWatch without reset for the pages written since the last look,
and adds the pages the look took off the write-watch since the mirror's
last copy. title_textures' look watches the mirror sources and hands
those pages on (list_x::mirror_pages_written), so a page written after a
draw's copy and reset by the look is still copied at the next draw. A
draw that overlaps a look (MIRROR_LOOK_SEQ odd or changed) copies its
whole range, as does one whose source is not write-watched.

The stale-page bits live beside MIRRORS, not in it, so as not to conflict
with xcl-dev#1995, which re-keys MIRRORS.

The look also runs when mirrors exist with no sparse window and no title
texture (it returned at once there, so the sources were never reset and
every draw copied every page ever written; review on xcl-dev#2478).

Part of xcl-dev#2393 (the copies; the index scan is cached in the next change).

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

Copy link
Copy Markdown
Author

Thanks, confirmed from the diffs: merged as is, a GPU-view mirror falls back to a full copy per draw (still correct). Whoever rebases second should move the GetWriteWatch range, the MIRROR_STALE key and mirror_sources() to cpu_src_start together. Moving only the copy source would make the stale-page lookup miss, and pages taken off the write-watch by a look would never be copied. I'll do that in #2478's rebase if #1995 lands first.

@KOLESNiii
KOLESNiii force-pushed the d3d12x-mirror-sync-written-pages branch from a6ec51e to 77e48ea Compare November 25, 2026 23:05
KOLESNiii added a commit to KOLESNiii/xcl that referenced this pull request Nov 25, 2026
…s differ

`sync_title_buffers` copied the drawn range of every mirrored buffer at
every draw and scanned every index again. The retail game measured in
xcl-dev#2389 rewrites these buffers every frame with the same bytes, so the
write-watch sees them written (xcl-dev#2478 found no saving that way) while
their content does not change.

Each copy now reads the title's bytes into a per-thread scratch buffer
(64 KiB chunks, `ReadProcessMemory`) and writes only the 4 KiB pages
that differ from the mirror's. A chunk with a page the title freed or
protected is copied with `copy_committed_region` as before (xcl-dev#2392).
Unchanged pages are no longer written into the mirror, so the sparse
look no longer sees them dirty.

An indexed draw whose indices are in a mirror keeps the vertex range it
scanned, per thread, keyed by (index buffer, bytes, format, first,
count, base vertex), and scans again only after any mirror was written
(`MIRROR_WRITES`).

`XCL_ACTIVITY` reports the bytes written next to the bytes compared.

Part of xcl-dev#2393.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KOLESNiii KOLESNiii mentioned this pull request Nov 25, 2026
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 26, 2026
…written pages)

Reconciles copy_committed_region with xcl-dev#751 (zero_unreadable=false for
mirror sync, zero_unreadable=true for reusable TITLE_BYTES) and preserves
xcl-dev#2384 gpu_view::sources in cached_watch_ranges.

Assisted-by: Antigravity (Gemini 4 Neon)
@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

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.

6 participants