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).
    • How much of a page: a page it copies is copied as far as it is in the mirror's source, not only the draw's part: its stale bit is cleared there, so another draw on the rest of the page would otherwise read old bytes (review below).
    • 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

  • 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 1e5ab11 (run on the working tree, then committed unchanged): cargo test --workspace -- --test-threads=4: 1429 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_mirror_sync_copies_only_the_pages_the_title_wrote fails on D3D12.X: the per-draw mirror sync copies without asking readable() #2437's code ("unchanged since: nothing copied").
    • a_look_runs_for_buffer_mirrors_alone fails with the old early return.
    • the_look_before_a_submit_lets_the_mirror_sync_skip_unwritten_pages passes with and without the early-return fix on this machine: the test process has a sparse window, so it never reaches that return. It covers the path through the real look.
    • Its two draws on disjoint halves of one page after a look fail without the whole-page copy ("each draw on the page reads the title's bytes").
  • Measurement: retail game, menus, 120 s with its learned coherent window removed so mirrors are made. XCL_ACTIVITY counters from D3D12.X: XCL_ACTIVITY counts and times the per-draw buffer mirror sync #2416, means per 2 s. The machine got slower during the day: D3D12.X: the per-draw mirror sync copies without asking readable() #2437's head gave very different numbers in the morning and the afternoon, so only the afternoon runs compare with each other. These runs are of 2bdfb90, before the review fix. That fix changes only the case with no sparse window, and every run here had three.

    D3D12.X: the per-draw mirror sync copies without asking readable() #2437, morning D3D12.X: the per-draw mirror sync copies without asking readable() #2437, afternoon this PR, afternoon (2 runs)
    presents 60 steady 60, ~35 from 66 s ~35–50 / ~26–46
    copies / KB copied 1,197 / 10,766 871 / 7,833 747 / 6,714 and 720 / 6,473
    time in the sync: median, mean 4.74, 4.74 ms 30, 38 ms 65, 76 ms and 45, 80 ms
  • Write-watch vs. content: a temporary check, run once and not in this commit, compared every mirror with its source after each draw. It found no stale pages. Copies stayed at ~1,100–1,200 per 2 s, because the write-watch reports these pages written every frame, although D3D12.X: every draw re-syncs buffer mirrors on the CPU (VirtualQuery, whole-buffer copies, index scan) #2389 found their bytes unchanged.

  • Device hang: one earlier 120 s run of this commit ended in a device hang (0x887a0006), and the title drew ~200 draws per 2 s instead of ~780 from the moment the mirrors appeared. The author saw the same hang on a run of main, and neither rerun of this commit hung, so it is not counted against this change. The cause is not known.

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: 1e5ab113965e77aed5c6240cead5c57ece77603b
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1455 (PresentX 1456)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16348 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).

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

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

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

list_x.rs:3756–3758: written_pages clears a stale bit for an entire 4 KiB page, but copy_written copies only the current draw's byte range (:3791–3801,3819). After refresh resets write-watch and records that page in MIRROR_STALE, two draws can use disjoint vertex ranges on it: draw A clears the page bit and copies only A; with no new title write, draw B sees neither a write-watch page nor a stale bit and uses old mirror bytes for B. Main's unconditional per-draw copy would update B, so this is a correctness regression from the page optimization even for ordinary CPU-written vertices.

Could the stale page be cleared only after its mirrored bytes have all been refreshed (for example by copying the dirty page's full intersection with the mirror allocation), and could the existing look/submit fixture assert two disjoint draws on one page? This is an exact-head source interleaving at 77e48ea4ba7dd7492d14b237655279f8f083f0dc; I have not run the proposed GPU fixture or a title. 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 added a commit to KOLESNiii/xcl that referenced this pull request Nov 26, 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`).

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
KOLESNiii added a commit to KOLESNiii/xcl that referenced this pull request Nov 26, 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).

A page a draw copies is copied as far as it is in the mirror's source,
not only the draw's part: its stale bit is cleared there, so another draw
on the rest of the page would otherwise read old mirror bytes (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 77e48ea to 1e5ab11 Compare November 26, 2026 10:20
@KOLESNiii

Copy link
Copy Markdown
Author

@luckyteagirl thanks, you're right. Once written_pages cleared a page's stale bit, copy_written copied only the draw's bytes of it, so a second draw on another part of the page read old mirror bytes. Fixed in 1e5ab11 (squashed into the PR's commit):

  • written_pages also returns the mirror's source range. copy_written copies each page it found written or stale as far as the page lies in that range, not only the draw's part. That is the whole-page copy you suggested. The full-range fallback (source not write-watched, or a look overlapped the draw) still copies the draw's range only.
  • the_look_before_a_submit_lets_the_mirror_sync_skip_unwritten_pages now writes two bytes in different halves of one page, runs the look (refresh), and makes two draws over those halves with no write in between. It fails without the fix ("each draw on the page reads the title's bytes") and passes with it.

cargo test --workspace -- --test-threads=4 on 1e5ab11: 1429 passed, 0 failed, 46 ignored, 25 SKIPPED (RTX 2070 SUPER). The regression run in the description is now of 1e5ab11. As before, it made no mirrors, so it doesn't reach the changed path.

Written with 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 2 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>
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).

A page a draw copies is copied as far as it is in the mirror's source,
not only the draw's part: its stale bit is cleared there, so another draw
on the rest of the page would otherwise read old mirror bytes (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 added a commit to KOLESNiii/xcl that referenced this pull request Nov 26, 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`).

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
@KOLESNiii
KOLESNiii force-pushed the d3d12x-mirror-sync-written-pages branch from 1e5ab11 to 7e1d426 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.

7 participants