Skip to content

D3D12.X: the per-draw mirror sync writes only mirror pages whose bytes differ - #2533

Open
KOLESNiii wants to merge 5 commits into
xcl-dev:mainfrom
KOLESNiii:d3d12x-mirror-sync-compare
Open

KOLESNiii wants to merge 5 commits into
xcl-dev:mainfrom
KOLESNiii:d3d12x-mirror-sync-compare

Conversation

@KOLESNiii

@KOLESNiii KOLESNiii commented Nov 25, 2026 •

Copy link
Copy Markdown

Stacked on #2437 (itself on #2416); only the top three commits (from D3D12.X: the per-draw mirror sync writes only mirror pages whose bytes differ up) belong here. It is an alternative to #2478, not on top of it.

sync_title_buffers copies the drawn range of every mirrored buffer at every draw and scans every index again. The title measured in #2389 rewrites these buffers every frame with the same bytes, so the write-watch reports them written (#2478 saved little that way) while their content stays the same. This change compares content instead: a copy writes only the mirror pages whose bytes differ, and an indexed draw reuses the vertex range it scanned while no mirror was written.

Part of #2393. Part of #2389.

What is in it

  • crates/xcl/src/hle/d3d12x/list_x.rs:
    • Copies: update_mirror 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. A chunk with a page the title freed or protected falls back to copy_committed_region (D3D12.X: the per-draw mirror sync checks its source with readable() at every draw #2392). Unchanged pages are no longer written into the mirror, so the sparse look no longer sees them dirty (D3D12.X: buffer mirrors in sparse windows are written each draw and uploaded again #2394).
    • Index scan: an indexed draw whose indices are in a mirror keeps its vertex range per thread, keyed by (index buffer, bytes, format, first, count, base vertex). It scans again after a write into the mirror its indices are in. An index buffer without a mirror is scanned at every draw, as before.
    • Lock: each mirror has a lock, held while the sync writes into it and counts the write; for the index buffer, from the copy through the range lookup or scan. Without it, another list that has written new indices but not yet bumped the count is seen as "nothing differs" and the old range is reused. Vertex copies take it too, since a vertex view and the index view can share a mirror.
    • Tests: a_mirror_is_written_only_where_the_title_bytes_differ (with a write-watch on the mirror, and a page made unreadable), a_vertex_range_is_scanned_again_only_after_a_mirror_write, the_mirror_sync_follows_changed_indices, a_vertex_range_is_not_reused_while_another_list_writes_the_indices (a test-only hook pauses list B between its index write and the bump).
  • crates/xcl/src/host.rs: the XCL_ACTIVITY mirror line shows the KB written next to the KB compared.
  • D3D12.X: a mirror chunk with an unreadable page is compared, not rewritten: a 64 KiB chunk that ReadProcessMemory cannot read whole is read around the page into the scratch buffer (filled first with the mirror's bytes) and compared like any other. Before, it was copied into the mirror whole and counted as written at every draw.
  • D3D12.X: a cached index range is checked against its own mirror's writes: each mirror keeps its own count of writes (in its lock), replacing the one MIRROR_WRITES for all mirrors, so a draw that writes new vertices no longer makes every indexed draw scan its unchanged indices again. Test: a_vertex_range_is_kept_while_only_other_mirrors_are_written.

Tested

  • Hardware: NVIDIA RTX 2070 SUPER (driver 32.0.16.2140), AMD Ryzen 5 3600XT, Windows 11 build 26300, Rust 1.100.0. Tests in debug, runs in release.

  • Suite on 80a99a0 (rebased onto D3D12.X: XCL_ACTIVITY counts and times the per-draw buffer mirror sync #2416's review fix): cargo test --workspace -- --test-threads=4: 1432 passed, 0 failed, 46 ignored; 25 SKIPPED (21 no wave64, 3 f16/f32 programs refused for wave32 (gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426), 1 no heap windows, as on main).

  • New tests: the first three fail with the change undone (every page written; the cached range never rescanned); the fourth fails with the index mirror's lock removed (list A reuses its old range); the extended a_mirror_is_written_only_where_the_title_bytes_differ fails with the old fallback (16384 bytes written where 12288 changed); a_vertex_range_is_kept_while_only_other_mirrors_are_written fails with one count for all mirrors (3 scans instead of 1).

  • Measurement (before the last three changes, not rerun since): retail game, menus, 120 s with its learned coherent window removed so the two mirrors are made (at ~31–32 s). One run each, means per 2 s of the XCL_ACTIVITY lines once the mirrors exist (43–44 windows).

    D3D12.X: the per-draw mirror sync copies without asking readable() #2437 (bb9bea2) this PR (0c62a58)
    draws synced / copies 775 / 1,193 771 / 1,186
    KB compared / KB written into mirrors – / 10,700 10,665 / 5
    indices scanned 1.97 M 2,021
    time in the sync: median, mean, max 4.80, 5.27, 17.4 ms 3.40, 3.84, 12.9 ms
    frames shown (120 s) / device removals / crashes 2834 / 0 / 0 2789 / 0 / 0

    Only the window in which the mirrors were made wrote anything (219 KB); every later window wrote 0 KB and scanned 0 indices. What remains is the read and the compare. Both runs held 60 presents per 2 s, so frames do not show the saving.

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: 80a99a0b7d408fe12b6cbd425559f106de56613a
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1512 (PresentX 1512)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16144 MB

That run had the 4095 MB coherent window in place and made no mirrors, so it does not reach the changed path; see Measurement for runs that do.

Not tested

  • The sync's cost after the mirror lock and the last two commits: the measurement above is from 0c62a58, before them.
  • Smoke frames: an earlier run of cf9498e showed 896 frames (a slow start, 30 presents in one 2 s window); a rerun showed 1438, and the run of 80a99a0 above 1512. That run makes no mirrors, so it does not reach this code.
  • Other titles: one whose mirrored buffers do change, where each changed page is now read, compared and written. Its index ranges are scanned as before whenever any mirror was written.
  • The world and draws with instances (none in these menus).
  • Repeat runs: one run of each commit.
  • Other hardware: AMD and Intel GPUs, wave64.

AI use

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

🤖 Generated with Claude Code

https://claude.ai/code/session_015csBcd63KUE2T1kW2P15nC

@KOLESNiii
KOLESNiii marked this pull request as ready for review November 25, 2026 21:49
@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@emirduran707

Copy link
Copy Markdown
Contributor

Independent AMD RDNA4 (native wave64) test report on exact head 0c62a58dc895919aee3c6f502420f74c20abc2a5 (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: 1277 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::a_mirror_is_written_only_where_the_title_bytes_differ and a_vertex_range_is_scanned_again_only_after_a_mirror_write.
    • 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: 0c62a58dc895919aee3c6f502420f74c20abc2a5
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1621 (PresentX 1621)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13940 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.

@KOLESNiii
KOLESNiii force-pushed the d3d12x-mirror-sync-compare branch from 0c62a58 to 5f61e93 Compare November 25, 2026 23:05
@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

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, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
Targeted run of a_mirror_is_written_only_where_the_title_bytes_differ, a_vertex_range_is_scanned_again_only_after_a_mirror_write, the_mirror_sync_follows_changed_indices: 3 passed (plus the_mirror_sync_copies_around_a_page_the_title_made_unreadable re-confirmed).

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

@KOLESNiii KOLESNiii mentioned this pull request Nov 25, 2026
@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

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

Could the index-mirror update and cached range lookup share synchronization? Two command lists can use the same mirror. MIRRORS only protects the address lookup, and the copy changes the mirror before incrementing MIRROR_WRITES. Suppose thread A has cached the range for index 0 and the title changes that index to 9 before either thread records its next draw. Thread B copies index 9 into the mirror and pauses before publishing the generation. A then sees matching source and mirror bytes, writes nothing, and hits its previous range under the old generation. The draw reads index 9, but A only refreshes the vertex data for index 0. Main rescans the mirror at this point.

A per-mirror lock covering copy and index-range lookup would close the gap. A focused test can pause B after the copy and before generation publication, then check that A cannot reuse its old range. Exact-head source review of 5f61e932b631987e3ec65dc6f61b0acf7e8fc6c6. No concurrent GPU run. AI-assisted review with Codex.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
xcl-dev#2478 + xcl-dev#2533)

(cherry picked from commit 0af1f31de2adc6acb4a407176142bcab777fabd8)
@KOLESNiii
KOLESNiii force-pushed the d3d12x-mirror-sync-compare branch from 5f61e93 to 26ccb47 Compare November 26, 2026 10:03
@KOLESNiii

Copy link
Copy Markdown
Author

@luckyteagirl Confirmed, thanks. Each mirror now has a lock, held while the sync writes into it and bumps MIRROR_WRITES. For the index buffer it is held from the copy through the generation read and the cached-range lookup or scan. Vertex copies take it too, since a vertex view and the index view can share a mirror.

Added a_vertex_range_is_not_reused_while_another_list_writes_the_indices. A test-only hook pauses list B after its index write and before the bump, and the test checks that list A, which has a range cached from before the change, still copies the vertex the changed index reaches. On an RTX 2070 SUPER it fails with the index mirror's lock removed and passes with it.

Squashed into the top commit, now 26ccb47. Full suite: 1430 passed, 0 failed, 46 ignored, 25 SKIPPED as before. Smoke on the new head is in the description.

(Claude Code, Claude Opus 5.5)

@KOLESNiii

Copy link
Copy Markdown
Author

Two more commits from a review pass of my own:

Each has a test that fails without it: 16384 bytes written where 12288 changed, and 3 index scans instead of 1. Suite on cf9498e: 1431 passed, 0 failed, 46 ignored, 25 SKIPPED as before. Smoke on the new head is in the description.

(Claude Code, Claude Opus 5.5)

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.

A row is logged whenever any of these moved, also with no draw synced:
the counters of a sync still running at the sample (or stalled for over
2 s) land before its draw is counted, and its slowest readable() would
otherwise be dropped with the skipped row.

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>
KOLESNiii and others added 4 commits November 26, 2026 11:16
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>
…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`).

Each mirror has a lock, held while the sync writes into it and bumps
`MIRROR_WRITES`. For the index buffer it is held from the copy through
the range lookup or scan: otherwise another list that has written new
indices but not yet bumped the count is seen as "nothing differs", and
the old range is reused. Vertex copies take it too, since a vertex view
and the index view can share a mirror.

`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>
Claude-Session: https://claude.ai/code/session_015csBcd63KUE2T1kW2P15nC
…itten

When `ReadProcessMemory` failed for a 64 KiB chunk (the title freed or
protected a page of it, xcl-dev#2392), `update_mirror` copied the chunk straight
into the mirror with `copy_committed_region` and counted all of it as
written. From then on every draw reading that range rewrote the whole
chunk, the sparse look uploaded it again and `MIRROR_WRITES` threw away
the cached index ranges: the per-draw rewrite xcl-dev#2393 removes elsewhere.

The chunk is now read into the scratch buffer, filled first with the
mirror's bytes so that the unreadable page compares equal, and goes
through the same page-by-page compare as a readable chunk.

Part of xcl-dev#2393.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015csBcd63KUE2T1kW2P15nC
The vertex range of an indexed draw was scanned again after a write into
any mirror (`MIRROR_WRITES`, one count for all of them). A title that
writes new vertices before its draws (a ring it fills through the frame)
writes the vertex mirror at nearly every draw, so its unchanged indices
were scanned again at nearly every draw.

Each mirror now keeps its own count of the sync's writes, in the lock
the sync already holds while it writes into that mirror, and a cached
range is checked against the count of the mirror its indices are in.
`MIRROR_WRITES` is gone.

Part of xcl-dev#2393.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015csBcd63KUE2T1kW2P15nC
@KOLESNiii
KOLESNiii force-pushed the d3d12x-mirror-sync-compare branch from cf9498e to 80a99a0 Compare November 26, 2026 11:20
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.

5 participants