d3d12: mirror section-backed imports through anonymous shadow heaps on RADV #200
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/brcly--TuxBox!200
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/200/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?
@DrArroway on GitHub · 2026-11-25 21:24 UTC · original
DrArroway:fix/radv-mapped-buffer-shadow→main· +748 −26 · 8 files · 2 commits · merged 2026-11-26 01:30 UTCSummary
Addresses #191 (and builds on the same 1-time probe idea as #199, going a step further to keep section-backed buffer imports working on RADV without
LD_PRELOADor libdrm changes).On AMD RADV,
vkd3d-proton'sOpenExistingHeapFromAddress*logsvkd3d_import_host_memory: Failed to import host memory, hr 0x8007000e(AMDGPU_GEM_USERPTR_ANONONLY) internally, silently substitutes a zeroed dummy heap, and still returnsS_OK. BecauseOpenExistingHeapFromAddress*returnsS_OK, fallback checks onErr(...)never trigger andd3d12-buffer-remapreads back zeros.This PR adds
runtime/d3d12/src/device/heap/shadow.rs:PagePools::supports_mapped_import): On the first committedMEM_MAPPED(PAGE_READWRITE) import, maps a temporary 64 KiB section with sentinel0x54555842, imports it, and copies 4 bytes to readback. Direct imports are preserved wheneverOpenExistingHeapFromAddress*returns0x54555842; on unexpected probe-setup failures it safely keeps the existing direct path (zero impact on NVIDIA/Windows).VirtualAllocshadow heaps (ShadowPages): When the host rejects or zero-substitutesMEM_MAPPEDpages, copies the committed section range into a private anonymousVirtualAlloc(MEM_RESERVE | MEM_COMMIT,PAGE_READWRITE) region and imports that heap instead.ImportedHeapdropsnative: ID3D12Heapand_pin: ImportPinbefore releasing the shadow mapping. Strict rejection forMEM_FREE/MEM_RESERVE/MEM_PRIVATE(buffer-import-rejectandTUXBOX_REQUIRE_BUFFER_IMPORT=1) is unchanged.ExecuteCommandLists,sync_shadow_imports()diffs live shadows against their last-synced baseline in 64-byte chunks (reusing a thread-local scratch buffer) and copies only CPU-modified chunks, preserving GPU writes in untouched chunks. OnceCopyPageMappingsretires anImportPin, further syncing stops so pre-remap contents stay intact.Scope & Limitations
CopyPageMappingsretirement are mirrored cleanly while preserving GPU-written chunks.MEM_MAPPEDpages and cross-VA GPU-write aliasing are not mirrored back yet.AtomicU32fast path).Verified on Lanes
GPU, driver and backend: AMD Radeon RX 6900 XT (
1002:73bf), Mesa RADV26.3.1-arch3.4, CachyOS (kernel7.3.6-1-cachyos), Proton Experimental (1794497090 experimental-11.0-20261112-x86_64,vkd3d-proton 3.2.0).python tuxbox.py check --backend proton --adapter 1002:73bfpassed on this branch (9fa9cdf):(67/67 GPU gates passed, including
lanes-selftest-descriptor-reclaimunpinned.)Buffer-Import Controls (
d3d12-buffer-import.exe, RX 6900 XT,TUXBOX_REQUIRE_BUFFER_IMPORT=1)Validated with
tools/check_d3d12_regressions.py::validate():buffer-remap(--remap): PASS (d3d12-buffer-remap PASS pci=1002:73bf words=768/768 identical=1 replacement=1,vkd3d_import_host_memorylogged once for the one-time probe vs twice per placement before)buffer-import: PASS (d3d12-buffer-import PASS pci=1002:73bf pages=2 words=1536/1536 passes=2 aliases=2)buffer-import-reject(--reject): PASS (d3d12-buffer-import-reject PASS pci=1002:73bf rejected=1)buffer-layout(--layout): PASS (d3d12-buffer-layout PASS pci=1002:73bf slot=133 words=512/512 VA-offsets=0,4)Additional Checks
python -m unittest tests.test_repository_hygiene -v: 4/4 PASScargo test --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12(under Wine): 137/137 PASS, 0 compiler warnings (includes 3 new tests inruntime/d3d12/src/tests/device/heap_shadow.rs)AI-assisted PR; all unit tests,
d3d12-buffer-importcontrols, andtuxbox.py checkabove were run and verified on the RX 6900 XT listed.buffer-remapregression control fails on RADV, with host memory imports refused #191@JeffreyBytes on GitHub · 2026-11-25 21:52 UTC · original
Tested
9fa9cdfon an RX 6700 XT (1002:73df), Mesa 26.3.1 RADV, Intel i5-12600K, Linux 7.3.6, Proton Experimental 11.0-20261112. Baseline ismainat1b32189.Regression controls.
buffer-remapbuffer-importbuffer-import-rejectbuffer-layoutmainremap readback differs)words=768/768 identical=1 replacement=1)The log has the
mirroring through anonymous shadow heapsline.ProjectScarlett, offline, warm shader cache,
TUXBOX_TRACE_PRESENT=120:mainFailed to import host memorylit=0.000So it works. In run 3 the Account Picker drew with the title's own prompts (
lit=0.998 mean=(32,32,44)), keyboard input was accepted, and the intro video played in full colour (lit=1.000 mean=(88,83,51)), with no preload. I stopped it by hand at 412 s.Two things from these runs:
main. The picker and the video both hold about 16 presents per 10 s here; theLD_PRELOADfrom #191 held about 255 at the picker onf489d3c.game_scarlett_master.exe+0x2215C35, within two seconds of each other. I have seen that address onmainbefore (682a0e4, two of three runs, at 123 to 136 s), so it may be the known intermittent crash and not this change; themainrun alongside these went the full 300 s. Three runs is not enough to say whether the slower loading makes it more likely.Happy to rerun on a new head.
buffer-remapregression control fails on RADV, with host memory imports refused #191@DrArroway on GitHub · 2026-11-25 22:12 UTC · original
Thank you for testing this on your RX 6700 XT and catching that ~25x slowdown, @JeffreyBytes!
Pushed
42af674to address both the submit overhead and the probe fence lifetime:ReadProcessMemoryIPC fromsync_shadow_imports(): Under Wine,ReadProcessMemory(GetCurrentProcess(), ...)was making awineserverIPC + socket copy for every live shadow on everyExecuteCommandLists. It now verifies readability in-process withVirtualQuery(matchingviews/mirrors.rs) and scans guest memory directly.diff_copy_chunksnow skips unchanged 4 KiB pages with a single slice compare before scanning 64-byte chunks, andImportPinexposes anArc<AtomicBool>liveness flag sosync_shadow_importsdoesn't lockGPU_APERTUREper shadow (or allocateliveVecs across steady-state submits).probe_mapped_importnow createsfenceandeventbeforeExecuteCommandListsand retains the probe section and COM objects if a post-submit wait fails or times out.Re-verified
42af674on RX 6900 XT (1002:73bf, Mesa RADV26.3.1, Proton Experimental11.0-20261112):buffer-remap,buffer-import,buffer-import-reject,buffer-layout: all 4 PASS (validate=True)tuxbox.py check --backend proton --adapter 1002:73bf: 67/67 GPU gates + all CI checks PASStuxbox-d3d12Wine unit tests: 137/137 PASSHappy to hear how loading/frame rate looks on your RX 6700 XT whenever you have time to rerun!
@gtb-sbe on GitHub · 2026-11-25 22:28 UTC · original
Nice work, especially getting this running without a preload. My branch from #191 takes the other route, so here is how I think the two relate.
What mine does. A small
LD_PRELOADlibrary repeats a userptr request withoutANONONLYafter the kernel refused it. The launcher andtuxbox.py checkload it automatically on amdgpu. The GPU then uses the title's own pages, so there is nothing to copy before a submit. GPU writes should reach the CPU and other aliases as well, but I have no control that tests that direction yet.What speaks against it. It preloads a library into the game process and bypasses a flag that libdrm sets on purpose. It needs a C compiler on the host, and it is still a workaround. Yours needs none of that.
Together. From reading the diff, not from a test: with the preload active, your probe should read its sentinel and keep the direct imports. Your mirroring could then be the fallback for hosts where the preload is not available or not wanted.
Questions from reading
42af674. I have not run it, so please correct me if I misread something.sync_cpu_writeschecks the range withVirtualQueryand then reads it directly. What keeps another thread from unmapping it in between? Similarly,Mappings::applyremaps the pages before it clearslive. Can a submit on another queue copy the new contents into the retired shadow in that gap?importedand accepted underTUXBOX_REQUIRE_BUFFER_IMPORT=1. Should strict mode refuse them, or trace them separately?One conflict on my side. My branch makes the
buffer-*regression controls fail when vkd3d-proton logs a refused import. Your probe causes one such line on RADV, so that check would reject your branch when it runs without the preload. I will change that before opening my PR.Tested. RX 9060 XT (
1002:7590), Mesa 26.2.6 RADV, Linux 6.18.60, Proton Experimental 11.0-20261112, base1b32189: 67/67 guest checks, 17/17 regression controls includingbuffer-remap, and the translator checks pass. I have not tested your branch. A ProjectScarlett run with mine is in progress and I have no result from it yet.@brcly, would you accept a preload in the launcher, with #200 as the fallback? I would rather know before I open a PR for it.
@JeffreyBytes on GitHub · 2026-11-25 22:35 UTC · original
Reran on the RX 6700 XT with
42af674, same setup as before (Proton Experimental 11.0-20261112, Linux 7.3.6, Mesa 26.3.1, warm shader cache,TUXBOX_TRACE_PRESENT=120, each run stopped at 300 s).9fa9cdf(3 runs)42af674(3 runs)LD_PRELOADfrom #191 (2 runs)main: ~1,440)So loading is about twice as fast as before, the picker is slower, and the crash is unchanged. Every exit 5 is the same access violation at
game_scarlett_master.exe+0x2215C35. The fourmainruns alongside these tests (f489d3cand1b32189, this afternoon) did not hit it, though682a0e4did this morning in two of three runs, as noted above. Somaincan crash here too; what stands out with this branch is how often, and that three of the four land within two seconds of 108 s.I have not looked for the cause. The preload column is there because it is the only other way I have got this card past the black screen, measured on the same machine; it is not a vote on which approach should land.
@brcly on GitHub · 2026-11-25 22:36 UTC · original
I would like to avoid a preload where possible, it shouldnt be necessary.
I'll hold off pulling this for now until we can agree on the best approach.
I'll review other PRs first that won't affect this while we brainstorm.
@DrArroway on GitHub · 2026-11-25 22:44 UTC · original
Thanks for the thorough read and for laying out how the two approaches fit together, @gtb-sbe!
Confirming your "Together" read: when the preload is active and RADV accepts section-backed userptr,
probe_mapped_importreads back the0x5455_5842sentinel on device init and keeps direct zero-copy imports with zero shadow allocations or submit overhead. So the two compose cleanly as primary + automatic fallback if @brcly wants both.On your five questions from
42af674— you read all five accurately:VirtualQueryTOCTOU &Mappings::applyremap gap:Mappings::apply—(self.apply)(...)runs beforelive.store(false, Ordering::Release), which does leave a narrow window where a concurrent submit on another queue could copy post-remap bytes into a retiring shadow. Clearinglivebefore(self.apply)closes that gap; happy to include that fix on the next push.VirtualQuery+ direct read insync_cpu_writes, you're right that there's no guard against a concurrent unmap in between. That matches the existing pattern inviews/mirrors.rs(traded against the ~25×wineserverIPC overhead fromReadProcessMemory), though a lightweight fault guard around the copy could harden it without bringing back IPC if needed.TUXBOX_REQUIRE_BUFFER_IMPORT=1/ tracing: Shadowed imports intentionally trace asimportedand pass strict mode right now socheck_d3d12_regressions.py(buffer-remap) passes out of the box and because strict mode was aimed at rejecting the zeroeddevice_standin(with the one-timemirroring through anonymous shadow heapslog line signaling fallback mode). Happy to add a distinct trace marker or have strict mode reject shadows if @brcly prefers.ExecuteCommandListswon't be mirrored until the next submit. That's an inherent limitation of pre-submit mirroring compared to real shared userptr pages.9fa9cdfreaching the Account Picker and intro video), so I don't know yet whether deeper gameplay in ProjectScarlett hits cross-alias GPU visibility.Holding off on further edits until @brcly weighs in on the preferred direction (and @JeffreyBytes has a chance to check
42af674if they want to).buffer-remapregression control fails on RADV, with host memory imports refused #191@gtb-sbe on GitHub · 2026-11-26 00:33 UTC · original · edited 2026-11-26 00:49 UTC
@DrArroway, on cross-alias visibility in ProjectScarlett: I measured which views its refused imports come from, see #191. Many pages are mapped at 2 to 8 GPU addresses, so separate shadows per alias would probably diverge once the GPU writes through one of them. The CPU never uses more than one view of a page.
@brcly on GitHub · 2026-11-26 00:45 UTC · original · edited 2026-11-26 01:17 UTC
Ok, I'm gonna be implementing a solution that will negate the need for the preload library and the other PR.
I'd consider a preload library a dirty fix, not the right way forward.
I do thank you for your work looking into it; it's helped shape a new direction I'm taking to help both AMD and Nvidia users going forward.
EDIT: For now, until a better solution for certain AMD issues are solved, I'm gonna build the new solution with this PR, and then AMD users can figure out how to better handle the memory issue from that point onwards
@brcly on GitHub · 2026-11-26 01:45 UTC · original
My changes are now merged.
Please test rigorously to make sure things haven't broken, but AMD users should now at least have a better time, as should NVIDIA users.
I've merged this PR for now as a "quick fix" until a more permanent solution has been created (which may take a while). As I can't test personally, I will be relying on collaborators like yourselves to help solve it.
Looking for help: let RADV read game memory directly
mainnow has a fallback from #200: when the host profile shows the GPU can't read imported game memory, TuxBox copies it. It passesbuffer-remap, but GPU writes stay in the copy and copying costs speed. We'd like to replace it with direct imports, without a preload library.What we know
runtime/xmem/src/shared_pages.rs,CreateFileMappingW+MapViewOfFile3), which Wine maps as shared memory.Ideas to test (none proven)
How to tell it works
section-import=aliasedon RADV with no override.python tuxbox.py checkpasses on AMD, includingbuffer-remap, and still passes on NVIDIA.TUXBOX_HOST_PROFILE=section-import=aliasedforces direct imports, which reproduces the failure on RADV.buffer-remapregression control fails on RADV, with host memory imports refused #191