d3d12_xs: read placed-texture memory through fault-safe snapshots (fixes #110) #146
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!146
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/146/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@MeNoSkills on GitHub · 2026-11-22 15:39 UTC · original
MeNoSkills:fix/upload-decommit-race→main· +99 −3 · 1 files · 2 commits · closed without mergingFixes #110.
Problem
The texture uploader in
crates/compat/d3d12_xs/src/upload.rsread the title's placed-resource memory directly (hashing, de-tiling, the sparse copy, and the zero watch). On console, CPU and GPU share memory with hardware-managed residency, so a title thread can never pull that memory out from under a reader. On PC the backing is VirtualAlloc'd, and a title that decommits it while the uploader is mid-read causes an access violation with no recovery path. The old module doc already listed this as a known limit ("a decommit racing the read would fault").Change
All reads of title memory now go through
read_title_memory/snapshot_title_memory, built onReadProcessMemoryon the current process. It reports a partial read as a failure instead of faulting.check(): each changed subresource is snapshotted once, then hashed and de-tiled from the private copy (this also removes the window where the hash and the de-tile could see different data). If the snapshot fails, the subresource is counted as unreadable, left without a hash so it is retried on the next pump, and skipped. The existing sparse path keeps its behaviour: unreadable ranges stay zero.first_nonzero(zero watch) reads in 64 KiB chunks; an unreadable chunk counts as "changed", so the subresource is re-examined by the normal path.watch_pendinghashes a snapshot and skips the subresource if it cannot be read.No new dependencies. No behaviour change when memory stays readable, apart from one extra copy per checked subresource.
Tests
decommitted_memory_fails_the_snapshot_instead_of_faulting: commits three pages, decommits the middle one, and checks that a snapshot spanning it fails, that snapshots of the intact pages still succeed, and thatfirst_nonzeroover the decommitted range returns true rather than crashing.cargo test --locked -p d3d12_xs --lib upload: 12 passed, 0 failed, 1 ignored.python .github/scripts/pr_gate.py --base upstream/main --head HEAD: 0 problems.Not verified
I have no title dump, so I have not run this against Fortnite. The cost of the extra copy per subresource is unmeasured on a real texture-streaming run; the per-pump byte budgets are unchanged. A maintainer run with the lobby would confirm the race is gone and the pump time stays within budget.
@omonkeyz on GitHub · 2026-11-22 15:44 UTC · original
Thanks for this, and for the thorough write-up and the decommit test! The race is real, but this is the per-frame texture path, and we can't take an extra copy plus
ReadProcessMemorycalls there without numbers. Two changes, please:Longer term, the root fix for #110 is to validate the title pointer where it's used, not to copy memory on every call. If you'd like to take that on instead, it would be very welcome.
@MeNoSkills on GitHub · 2026-11-22 15:52 UTC · original
Thanks for the review. Both changes are in
0744c01.Off by default. The default path is the original direct reads again (same code as main, plus one cached-switch check per call). The snapshot path is behind TOMBOX_UPLOAD_SAFE_READ=1 (live key
upload_safe_read), so a title that hits #110 can opt in to skip the racing subresource instead of crashing.Cost. I added a micro-bench (
default_reads_are_direct_and_the_safe_path_costs_one_copy) that hashes a buffer through both paths and checks that they produce identical results. Release build, one run on one machine:The extra cost is the copy, which is exactly why it stays opt-in. These are micro-bench numbers, not a draw-heavy workload: I have no title dump, so I can't give per-frame numbers. The default path adds no copy.
I'd be glad to take the root fix (validate the title pointer where it is used, instead of copying). Could you point me to where you'd want that validation to live? My guess is a check against the same VirtualQuery scan already done in
check(), re-run immediately before the read, but that still leaves a window, so I'd like your view before I build it.@MiguelMatMar on GitHub · 2026-11-22 16:03 UTC · original
A second data point for the micro-bench, release build at PR head, 8-core Zen 2 desktop, three runs after one warm-up run:
This matches your table. The 4 MiB overhead is a bit higher here (+131% against +110%). The first, cold run showed +181% at 256 KiB, so single runs are noisy at that size. The results of both paths were identical in every run.
I also checked that this PR and #121 (both touch
upload.rs) merge cleanly with each other and with current main.@omonkeyz on GitHub · 2026-11-22 16:11 UTC · original
Applied in
595eb3aand360cd07(merged in1a1e283) with your authorship kept, thanks for the quick turnaround and the bench! One change: I stripped the two //! doc lines in upload.rs. The repo allows no comment lines at all, including doc comments. #110 stays open for the root fix: checking the title pointer where it's used.@MeNoSkills on GitHub · 2026-11-22 16:11 UTC · original
Thanks for running it on a second machine and for checking the merge with #121. Agreed that single runs at 256 KiB are noisy, which is why the numbers in the description came from steady-state loops. Nothing else changes on my side; happy to follow up on the validate-where-used root fix once you have a view on where it should live.
Pull request closed