Skip to content

D3D12.X: coalesce contiguous WriteProcessMemory runs in write_changed_pages - #3237

Merged
throny merged 1 commit into
xcl-dev:mainfrom
Lakanya:fix/coalesce-texture-download-writes
Nov 29, 2026
Merged

throny merged 1 commit into
xcl-dev:mainfrom
Lakanya:fix/coalesce-texture-download-writes

Conversation

@Lakanya

@Lakanya Lakanya commented Nov 28, 2026

Copy link
Copy Markdown

title_textures::publish_download() holds the global TEXTURES mutex while write_changed_pages() writes reverse-tiled GPU texture readbacks back into title memory. Previously, write_changed_pages() called VirtualQuery + WriteProcessMemory(GetCurrentProcess(), ...) once per 4 KiB page. On Windows, each WriteProcessMemory triggers NtFlushInstructionCache across CPU cores; on multi-megabyte GPU-written textures (e.g. an 8.92 MB texture = 2,178 pages), this held TEXTURES for 24+ seconds and stalled CreatePlacedResourceX, ExecuteCommandLists, CopyTextureRegion, and memory_report::snapshot.

This PR coalesces contiguous changed 4 KiB pages into runs and issues one VirtualQuery per VM region and one WriteProcessMemory per contiguous changed run, while continuing to skip unchanged pages (preserving MEM_WRITE_WATCH cleanliness) and failing closed on read-only, PAGE_GUARD, or unmapped pages.

What is in it

  • crates/xcl/src/hle/d3d12x/title_textures.rs: add changed_page_runs(), update write_changed_pages() to coalesce contiguous changed 4 KiB pages per VM region into a single WriteProcessMemory call, and add unit tests changed_page_runs_coalesces_contiguous_and_skips_unchanged and write_changed_pages_coalesces_writes_and_respects_vm_protections.

Tested

  • Hardware: NVIDIA GeForce RTX 5070 Laptop GPU (8 GB, driver 617.71), Windows 11 Pro 26300, rustc 1.100.0 (0ef1bc386 2026-11-09), debug & release.
  • New unit tests (cargo test -p xcl --lib -- write_changed_pages changed_page_runs --nocapture): 2 passed, 0 failed, 0 ignored.
  • cargo test --workspace --release --no-fail-fast -j 6 -- --test-threads=4: 135 passed, 4 failed, 20 ignored across integration/crate test binaries (27 SKIPPED GPU tests: wave64 / unsupported f16-f32 per-lane variants / heap windows); xcl --lib exited early (0x80004005) on an unrelated in-process stop_title test (tracked in Tests: the mixed-window remake tests run in a child process #3187) and 4 gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_* failed; after cargo build --workspace --release -j 6, cargo test -p xcl --release --test e2e -- --test-threads=4: 4 passed, 1 failed (e2e).
  • Live retail title (in-world 3D entry, same hardware, XCL_ACTIVITY=1, XCL_LOOK_STATS=2): 8.92 MB GPU-written texture publish dropped from 24,023 ms (TEXTURES.lock() hold, 0 3D presents) to 14–30 ms (1 WriteProcessMemory call, 0 TEXTURES.lock() holds >= 200 ms, 10 3D presents in 12 s).

Regression run
Both runs used XCL_PREPARE=0, XCL_DEVICE_TYPE=lockhart, XCL_TEXTURE_LEVEL_PAGES=1, XCL_SMALL_TEXTURES=1.

xcl-smoke v1
commit: 227d2cb388f103268dac7635ea720eb3e5352d4e
gpu: NVIDIA GeForce RTX 5070 Laptop GPU
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1600 (PresentX 1600)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 12746 MB
xcl-smoke v1
commit: b11fe096e2dcb554b47044c6baa33d9e1931f30e
gpu: NVIDIA GeForce RTX 5070 Laptop GPU
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1608 (PresentX 1608)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 12493 MB

Not tested

  • AMD or Intel GPUs, wave64 hardware, and Linux/Wine.
  • The 60 s smoke runs cover startup through the menu; the 8.92 MB write_changed_pages speedup was measured separately on a 210 s in-world run on the same title.

AI use

  • Antigravity (Gemini 4 Neon) profiled the TEXTURES.lock() stall, implemented changed_page_runs() and coalesced write_changed_pages(), wrote and ran the unit tests and paired smoke runs, and drafted this description.

…_pages

publish_download() holds the global TEXTURES mutex while write_changed_pages() writes reverse-tiled GPU texture readbacks back to title memory. Previously it called VirtualQuery + WriteProcessMemory once per 4 KiB page; on Windows each WriteProcessMemory triggers NtFlushInstructionCache across cores, causing 24+ second full-engine freezes on multi-megabyte GPU-written textures (e.g. 2,178 calls for an 8.92 MB texture).

Coalesce contiguous changed 4 KiB pages into runs and issue one VirtualQuery per VM region and one WriteProcessMemory per contiguous changed run, while still skipping unchanged pages (for MEM_WRITE_WATCH) and failing closed on read-only, PAGE_GUARD, or unmapped pages. Measured on RTX 5070 Laptop GPU (driver 617.71): 8.92 MB publish dropped from 24,023 ms to 14-30 ms (1 WriteProcessMemory call).

Assisted-by: Antigravity (Gemini 4 Neon)
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 28, 2026
raylitebull added a commit that referenced this pull request Nov 29, 2026
…ppear immediately

GitHub search `status:success` lags on newly opened PRs (dropping PRs
like #3247/#3245/#3237 even when all required CheckRuns are green).
Scan non-draft PRs without `status:success` (moving `mergeable` to Stage B
for speed) and filter directly on live CheckRuns. Also sort newer PRs
first within each priority bucket.

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

Kalmarv commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: fa20459
head: b11fe09
merge: clean (trial merge 984566ad2ed772872ac77a01ad323921064809a3, local, not pushed; git diff --check clean)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 (XCL_DXC_DIR = DXC 1.9.2609) -> 2610 passed, 0 failed, 49 ignored; SKIPPED: 26 (21 no wave64 on this 32-lane GPU, 3 per-lane f16/f32 forms refused (#1426 semantics), 2 no heap windows); failing: none. Both new tests ran and passed: changed_page_runs_coalesces_contiguous_and_skips_unchanged and write_changed_pages_coalesces_writes_and_respects_vm_protections (write-watch shows the unchanged page isn't dirtied; read-only and guard pages fail closed and the guard stays armed).
review: none blocking; whole diff read (title_textures.rs only). changed_page_runs uses the same 4 KiB page slicing as before, unaligned start included, and merges adjacent changed slices. write_changed_pages keeps the same acceptance test (committed, writable, not guard), now applied once per VirtualQuery region instead of once per page. All pages of one region share state and protection, so at query time this is equivalent, and writes are split at region ends. On failure it still returns false after the earlier runs are written, as before. One note, not a blocker: region_end is reused across runs too, so a page whose protection the title changes after the first query (made read-only or guarded) isn't seen until the next region. WriteProcessMemory can write through read-only protection, which is why the per-page check existed. The window is the duration of one write_changed_pages call (it was one page before, so it was never race-free), and a concurrent unmap still makes WriteProcessMemory fail.
performance: fewer WriteProcessMemory calls while TEXTURES is held when a GPU-written texture download publishes many contiguous changed pages; no other change. Not measured: nothing counts texture downloads, so I can't show this title reached the path. Title run (release build of the merge, scratch/fast-world.py fast profile, 30 s in world; same cache snapshot and harness as a main fa20459 run): main exit 0, 0 crashes, 0 removals, world entry 90.6 s, peak commit 28,620 MB, 70 WARN lines. This merge: exit 0, 0 crashes, 0 removals, world entry 102.1 s, peak commit 29,091 MB (+1.6 %), 79 WARN lines; failed_pipelines 0, no translation errors. The only new WARN kind is the system-wide "running out of commit" (this process held 28,938 MB; another suite and build were running on the machine at the time). Both runs show 0 fps in the world because the cache doesn't match the translator identity; timings on this shared machine are noise.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 5070 Ti 16 GB (driver 610.74, wave32 only), AMD Ryzen 9 9950X, Windows 11 build 26200; rustc 1.99.0, debug test profile, release build for the title run, DXC 1.9.2609 via XCL_DXC_DIR. Claude Code, Claude Opus 5.5: merge, build, suite, title run and full-diff review; this report.

@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU labels Nov 29, 2026
@andrewDoing

Copy link
Copy Markdown
Contributor

Measured the syscall reduction on Windows (Ryzen 7 5700X3D) at b11fe09. I compiled the parent and PR versions of the production loops with counting wrappers forwarding to the real VirtualQuery and WriteProcessMemory on an owned 16 MiB allocation. Each changed page differed by one byte. All ten runs read back the entire expected allocation.

Changed-page layout VirtualQuery, parent → PR WriteProcessMemory, parent → PR Bytes written on either
Identical buffers 0 → 0 0 → 0 0
All 4,096 pages 4,096 → 1 4,096 → 1 16 MiB
Alternating pages 2,048 → 1 2,048 → 2,048 8 MiB
Eight consecutive pages plus one isolated page per 32 1,152 → 1 1,152 → 256 4.5 MiB
Two changed pages separated by an unchanged read-only page 2 → 2 2 → 2 8 KiB

This quantifies where the optimization helps. Even when alternating changes prevent write coalescing, the region cache eliminates 2,047 queries. Payload remains page-granular in every case. The read-only gap was left untouched. These counts measure the publishing helper only. No title run or wall-time inference.

Tooling: OpenAI Codex generated and ran the synthetic harness and drafted this report.

@CharlyRien

Copy link
Copy Markdown
Contributor

A test of this PR in the world of the retail title. On our setup it removes a freeze we had traced to this very loop before the PR was opened.

Setup

  • RTX 3090 24 GB (Resizable BAR off), driver 616.56, Ryzen 9 5950X, 32 GB RAM, Windows 11 22631, Rust 1.100.0, release.
  • Base: main fa204595, plus D3D12.X: name persisted compute driver blobs for NodeMask 1 as node 0 #3214 (cherry-picked), plus four local commits of ours that are not related to this PR: our thread sampler and its reader (xcl run: XCL_THREAD_SAMPLE samples every thread's CPU time and stacks #3232, off unless XCL_THREAD_SAMPLE is set), and two lab switches (XCL_TEX_KEY_PAD, XCL_TRIM_VIEWS) that are off in both runs.
  • C3: that base, without this PR.
  • C3f: the same tree plus this PR's head b11fe096 (cherry-picked, 9c725b13) and one sampler commit (the sampler writes each sample to its file at once).
  • Same shader cache (prepared once for these identities, no preparation in either run), same translator identities, same scripted A presses.
  • Same env in both: XCL_HUGE_PSO_CONCURRENCY=8, XCL_HUGE_PSO_WANTED_CONCURRENCY=8, XCL_PSO_WAIT_MS=5000, XCL_ACTIVITY=1, XCL_NETWORK=0. C3f also had XCL_THREAD_SAMPLE on from 220 to 370 s, to see the freeze if it came back.
  • One run each.

Measured

C3 (without) C3f (with this PR)
Longest "placed textures in" (activity line, 2 s, all threads) 113,248 ms (5 calls) 2,635 ms (1,593 calls)
Stable world reached at 487.5 s 372.1 s
Presents/s in the world (first ~180 s) 2.36 2.64
Device removals / unhandled exceptions 0 / 0 0 / 0

Read (a thread dump taken during C1f's freeze, at 294 s, symbolized with xcl.exe's PDB)

  • xcl-fences: in title_textures::write_changed_pages < complete_fence_downloads < poll_fence_downloads (the code calls it with TEXTURES held, as the description says).
  • A title thread: in Mutex::lock_contended < title_textures::register < device_x::create_placed_resource_x.
  • The submitting thread: in Mutex::lock_contended < title_textures::refresh_parts < refresh_for < prepare_title_submission < q_execute_command_lists.

Deduced

  • That is the cause the description gives. While the per-page VirtualQuery + WriteProcessMemory loop held TEXTURES, placement and submission waited on it.
  • With this PR the longest 2 s total of placement time fell from 113 s to 2.6 s on this title, and the world came about 115 s earlier in this pair.
  • One run each, so the pace difference (2.36 against 2.64) is not a measure of the PR; the freeze is gone in this run.

Not tested / caveats

  • The 3D scene of the world is black on this base (only the UI shows), in both runs. That is unrelated to this PR, so these runs show nothing about image correctness.
  • No scripts/smoke.ps1 pair: these are 550–670 s title runs through our own harness.
  • The sampler was on in C3f only, from 220 to 370 s. It made 19,418 stops, 132.5 µs each on average and 141 ms at the longest, and used 6.3 ms per 250 ms sample. That is a difference between the two runs.
  • AMD and Intel GPUs; other titles.

AI use

  • Claude Code with Claude Opus 5.5 built both trees, ran the title runs, read the logs and the thread dump, and wrote this report. The person running these tests asked for it.

@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check claim
head: b11fe09

@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: d8bd9a3
head: b11fe09
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2620 passed, 0 failed, 49 ignored (27 SKIPPED lines). Base alone at d8bd9a3: 2618 passed, 0 failed, 49 ignored.
review: no blocking finding. Neighbouring changed pages are merged into runs: one region query per memory region and one write per run inside a region, instead of one of each per page. It still writes only changed pages, still fails closed on read-only, guard and uncommitted pages, and splits a write at region boundaries. Note: the region information is now kept across runs, so a protection change after the query is noticed later than before. The run-building function has its own test; the write test also passes on the old code (a regression guard, fine for a performance change).
performance: not measured here.
verdict: safe to merge
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Nov 29, 2026
@throny
throny merged commit cd679bd into xcl-dev:main Nov 29, 2026
3 checks passed
@crosettiremi

Copy link
Copy Markdown

An AMD data point for the stall this fixes, from the retail title on a Radeon RX 7900 XT 20 GB (driver 32.0.32029.2010), base fa204595 and d8bd9a38. I have not run this PR's head; our tree got its own change to the same loop the night this was open.

Before: after the first world submit, the copy-back of GPU-written textures took 7 to 15 ms per 4 KiB page here (two virtual-memory queries per page, under the texture lock), about 100 s for 28 to 56 MiB. The title's next queue Signal waited behind it, and in one run the title's own hang watchdog ended the process. After (one write per contiguous run, publication outside the lock): 49 MB in 7.9 ms in the same phase.

One thing our runs add: with the write batched, the remaining cost in this path was the lock, not the writes. If the per-page queries still run under the global texture lock on main after this PR, a second thread's Signal can still wait for the whole batch. I have not checked main after the merge for that.

AI use: Claude Code (Claude Fable 5.2) read the logs and wrote this.

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 gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants