Repository navigation
D3D12.X: coalesce contiguous WriteProcessMemory runs in write_changed_pages - #3237
Conversation
…_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)
…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)
|
merge-check |
|
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.
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. |
|
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
Measured
Read (a thread dump taken during C1f's freeze, at 294 s, symbolized with xcl.exe's PDB)
Deduced
Not tested / caveats
AI use
|
|
merge-check claim |
|
merge-check |
|
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 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 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 AI use: Claude Code (Claude Fable 5.2) read the logs and wrote this. |
title_textures::publish_download()holds the globalTEXTURESmutex whilewrite_changed_pages()writes reverse-tiled GPU texture readbacks back into title memory. Previously,write_changed_pages()calledVirtualQuery+WriteProcessMemory(GetCurrentProcess(), ...)once per 4 KiB page. On Windows, eachWriteProcessMemorytriggersNtFlushInstructionCacheacross CPU cores; on multi-megabyte GPU-written textures (e.g. an 8.92 MB texture = 2,178 pages), this heldTEXTURESfor 24+ seconds and stalledCreatePlacedResourceX,ExecuteCommandLists,CopyTextureRegion, andmemory_report::snapshot.This PR coalesces contiguous changed 4 KiB pages into runs and issues one
VirtualQueryper VM region and oneWriteProcessMemoryper contiguous changed run, while continuing to skip unchanged pages (preservingMEM_WRITE_WATCHcleanliness) and failing closed on read-only,PAGE_GUARD, or unmapped pages.What is in it
crates/xcl/src/hle/d3d12x/title_textures.rs: addchanged_page_runs(), updatewrite_changed_pages()to coalesce contiguous changed 4 KiB pages per VM region into a singleWriteProcessMemorycall, and add unit testschanged_page_runs_coalesces_contiguous_and_skips_unchangedandwrite_changed_pages_coalesces_writes_and_respects_vm_protections.Tested
617.71), Windows 11 Pro 26300,rustc 1.100.0 (0ef1bc386 2026-11-09), debug & release.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 (27SKIPPEDGPU tests: wave64 / unsupported f16-f32 per-lane variants / heap windows);xcl --libexited early (0x80004005) on an unrelated in-processstop_titletest (tracked in Tests: the mixed-window remake tests run in a child process #3187) and 4gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_*failed; aftercargo build --workspace --release -j 6,cargo test -p xcl --release --test e2e -- --test-threads=4: 4 passed, 1 failed (e2e).XCL_ACTIVITY=1,XCL_LOOK_STATS=2): 8.92 MB GPU-written texture publish dropped from24,023 ms(TEXTURES.lock()hold,03D presents) to14–30 ms(1WriteProcessMemorycall,0TEXTURES.lock()holds>= 200 ms,103D 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.Not tested
write_changed_pagesspeedup was measured separately on a 210 s in-world run on the same title.AI use
TEXTURES.lock()stall, implementedchanged_page_runs()and coalescedwrite_changed_pages(), wrote and ran the unit tests and paired smoke runs, and drafted this description.