d3d12: mirror section-backed imports through anonymous shadow heaps on RADV #200

Manually merged
github merged 2 commits from gh-pr/200/head into gh-pr/200/base 2026-11-26 06:37:57 +00:00
Owner

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

Summary

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_PRELOAD or libdrm changes).

On AMD RADV, vkd3d-proton's OpenExistingHeapFromAddress* logs vkd3d_import_host_memory: Failed to import host memory, hr 0x8007000e (AMDGPU_GEM_USERPTR_ANONONLY) internally, silently substitutes a zeroed dummy heap, and still returns S_OK. Because OpenExistingHeapFromAddress* returns S_OK, fallback checks on Err(...) never trigger and d3d12-buffer-remap reads back zeros.

This PR adds runtime/d3d12/src/device/heap/shadow.rs:

  • One-time per-device probe (PagePools::supports_mapped_import): On the first committed MEM_MAPPED (PAGE_READWRITE) import, maps a temporary 64 KiB section with sentinel 0x54555842, imports it, and copies 4 bytes to readback. Direct imports are preserved whenever OpenExistingHeapFromAddress* returns 0x54555842; on unexpected probe-setup failures it safely keeps the existing direct path (zero impact on NVIDIA/Windows).
  • Anonymous VirtualAlloc shadow heaps (ShadowPages): When the host rejects or zero-substitutes MEM_MAPPED pages, copies the committed section range into a private anonymous VirtualAlloc (MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) region and imports that heap instead. ImportedHeap drops native: ID3D12Heap and _pin: ImportPin before releasing the shadow mapping. Strict rejection for MEM_FREE / MEM_RESERVE / MEM_PRIVATE (buffer-import-reject and TUXBOX_REQUIRE_BUFFER_IMPORT=1) is unchanged.
  • Chunked CPU->GPU sync & remap retirement: Before 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. Once CopyPageMappings retires an ImportPin, further syncing stops so pre-remap contents stay intact.

Scope & Limitations

  • CPU->GPU writes and CopyPageMappings retirement are mirrored cleanly while preserving GPU-written chunks.
  • GPU->CPU writeback from a shadow heap back into MEM_MAPPED pages and cross-VA GPU-write aliasing are not mirrored back yet.
  • Zero runtime cost on NVIDIA/Windows where the one-time probe succeeds (the global shadow registry uses an AtomicU32 fast path).

Verified on Lanes

GPU, driver and backend: AMD Radeon RX 6900 XT (1002:73bf), Mesa RADV 26.3.1-arch3.4, CachyOS (kernel 7.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:73bf passed on this branch (9fa9cdf):
Summary
  passed   Build the shader compiler (0s)
  passed   Python tests (3s)
  passed   Shader compiler tests (0s)
  passed   DXBC tests (0s)
  passed   XG layout tests (1s)
  passed   D3D12 Windows typecheck (0s)
  passed   Build (20s)
  passed   Lanes and fixtures on the GPU (136s)
  not run: pass --proton, --prefix and --windows-dxc D3D12 regression controls (0s)
  not run: pass --proton, --prefix and --windows-dxc Shader translator on the GPU (0s)

(67/67 GPU gates passed, including lanes-selftest-descriptor-reclaim unpinned.)

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_memory logged 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 PASS
  • cargo test --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12 (under Wine): 137/137 PASS, 0 compiler warnings (includes 3 new tests in runtime/d3d12/src/tests/device/heap_shadow.rs)

AI-assisted PR; all unit tests, d3d12-buffer-import controls, and tuxbox.py check above were run and verified on the RX 6900 XT listed.

**@⁠DrArroway** on GitHub · 2026-11-25 21:24 UTC · [original](https://github.com/brcly/TuxBox/pull/200) `DrArroway:fix/radv-mapped-buffer-shadow` → `main` · +748 −26 · 8 files · 2 commits · merged 2026-11-26 01:30 UTC ## Summary 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_PRELOAD` or libdrm changes). On AMD RADV, `vkd3d-proton`'s `OpenExistingHeapFromAddress*` logs `vkd3d_import_host_memory: Failed to import host memory, hr 0x8007000e` (`AMDGPU_GEM_USERPTR_ANONONLY`) internally, silently substitutes a zeroed dummy heap, and still returns `S_OK`. Because `OpenExistingHeapFromAddress*` returns `S_OK`, fallback checks on `Err(...)` never trigger and `d3d12-buffer-remap` reads back zeros. This PR adds `runtime/d3d12/src/device/heap/shadow.rs`: - **One-time per-device probe (`PagePools::supports_mapped_import`):** On the first committed `MEM_MAPPED` (`PAGE_READWRITE`) import, maps a temporary 64 KiB section with sentinel `0x54555842`, imports it, and copies 4 bytes to readback. Direct imports are preserved whenever `OpenExistingHeapFromAddress*` returns `0x54555842`; on unexpected probe-setup failures it safely keeps the existing direct path (zero impact on NVIDIA/Windows). - **Anonymous `VirtualAlloc` shadow heaps (`ShadowPages`):** When the host rejects or zero-substitutes `MEM_MAPPED` pages, copies the committed section range into a private anonymous `VirtualAlloc` (`MEM_RESERVE | MEM_COMMIT`, `PAGE_READWRITE`) region and imports that heap instead. `ImportedHeap` drops `native: ID3D12Heap` and `_pin: ImportPin` before releasing the shadow mapping. Strict rejection for `MEM_FREE` / `MEM_RESERVE` / `MEM_PRIVATE` (`buffer-import-reject` and `TUXBOX_REQUIRE_BUFFER_IMPORT=1`) is unchanged. - **Chunked CPU->GPU sync & remap retirement:** Before `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. Once `CopyPageMappings` retires an `ImportPin`, further syncing stops so pre-remap contents stay intact. ### Scope & Limitations - CPU->GPU writes and `CopyPageMappings` retirement are mirrored cleanly while preserving GPU-written chunks. - GPU->CPU writeback from a shadow heap back into `MEM_MAPPED` pages and cross-VA GPU-write aliasing are not mirrored back yet. - Zero runtime cost on NVIDIA/Windows where the one-time probe succeeds (the global shadow registry uses an `AtomicU32` fast path). ## Verified on Lanes GPU, driver and backend: AMD Radeon RX 6900 XT (`1002:73bf`), Mesa RADV `26.3.1-arch3.4`, CachyOS (kernel `7.3.6-1-cachyos`), Proton Experimental (`1794497090 experimental-11.0-20261112-x86_64`, `vkd3d-proton 3.2.0`). - [x] `python tuxbox.py check --backend proton --adapter 1002:73bf` passed on this branch (`9fa9cdf`): ```text Summary passed Build the shader compiler (0s) passed Python tests (3s) passed Shader compiler tests (0s) passed DXBC tests (0s) passed XG layout tests (1s) passed D3D12 Windows typecheck (0s) passed Build (20s) passed Lanes and fixtures on the GPU (136s) not run: pass --proton, --prefix and --windows-dxc D3D12 regression controls (0s) not run: pass --proton, --prefix and --windows-dxc Shader translator on the GPU (0s) ``` *(67/67 GPU gates passed, including `lanes-selftest-descriptor-reclaim` unpinned.)* ### 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_memory` logged 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 PASS - `cargo test --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12` (under Wine): 137/137 PASS, 0 compiler warnings (includes 3 new tests in `runtime/d3d12/src/tests/device/heap_shadow.rs`) AI-assisted PR; all unit tests, `d3d12-buffer-import` controls, and `tuxbox.py check` above were run and verified on the RX 6900 XT listed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:37:54 +00:00
github manually merged commit 42af674364 into gh-pr/200/base 2026-11-26 06:37:57 +00:00
Author
Owner

@⁠JeffreyBytes on GitHub · 2026-11-25 21:52 UTC · original

Tested 9fa9cdf on an RX 6700 XT (1002:73df), Mesa 26.3.1 RADV, Intel i5-12600K, Linux 7.3.6, Proton Experimental 11.0-20261112. Baseline is main at 1b32189.

Regression controls.

buffer-remap buffer-import buffer-import-reject buffer-layout
main FAIL (remap readback differs) PASS PASS PASS
this branch PASS (words=768/768 identical=1 replacement=1) PASS PASS PASS

The log has the mirroring through anonymous shadow heaps line.

ProjectScarlett, offline, warm shader cache, TUXBOX_TRACE_PRESENT=120:

main this branch, run 1 run 2 run 3
Failed to import host memory 131 1 1 1
Presents per 10 s while loading ~1,440 ~55 ~55 ~55
Outcome 300 s, every sample lit=0.000 exit 5 at 108.7 s exit 5 at 107.0 s Account Picker, then the intro video

So 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:

  • Speed. Loading presents about 25 times slower than main. The picker and the video both hold about 16 presents per 10 s here; the LD_PRELOAD from #191 held about 255 at the picker on f489d3c.
  • Exit 5 in two of three runs. Both are an access violation at game_scarlett_master.exe+0x2215C35, within two seconds of each other. I have seen that address on main before (682a0e4, two of three runs, at 123 to 136 s), so it may be the known intermittent crash and not this change; the main run 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.

**@⁠JeffreyBytes** on GitHub · 2026-11-25 21:52 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7175641111) Tested `9fa9cdf` on an RX 6700 XT (`1002:73df`), Mesa 26.3.1 RADV, Intel i5-12600K, Linux 7.3.6, Proton Experimental 11.0-20261112. Baseline is `main` at `1b32189`. **Regression controls.** | | `buffer-remap` | `buffer-import` | `buffer-import-reject` | `buffer-layout` | | --- | --- | --- | --- | --- | | `main` | FAIL (`remap readback differs`) | PASS | PASS | PASS | | this branch | PASS (`words=768/768 identical=1 replacement=1`) | PASS | PASS | PASS | The log has the `mirroring through anonymous shadow heaps` line. **ProjectScarlett**, offline, warm shader cache, `TUXBOX_TRACE_PRESENT=120`: | | `main` | this branch, run 1 | run 2 | run 3 | | --- | --- | --- | --- | --- | | `Failed to import host memory` | 131 | 1 | 1 | 1 | | Presents per 10 s while loading | ~1,440 | ~55 | ~55 | ~55 | | Outcome | 300 s, every sample `lit=0.000` | exit 5 at 108.7 s | exit 5 at 107.0 s | Account Picker, then the intro video | So 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: - **Speed.** Loading presents about 25 times slower than `main`. The picker and the video both hold about 16 presents per 10 s here; the `LD_PRELOAD` from #191 held about 255 at the picker on `f489d3c`. - **Exit 5 in two of three runs.** Both are an access violation at `game_scarlett_master.exe+0x2215C35`, within two seconds of each other. I have seen that address on `main` before (`682a0e4`, two of three runs, at 123 to 136 s), so it may be the known intermittent crash and not this change; the `main` run 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.
Author
Owner

@⁠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 42af674 to address both the submit overhead and the probe fence lifetime:

  • Removed ReadProcessMemory IPC from sync_shadow_imports(): Under Wine, ReadProcessMemory(GetCurrentProcess(), ...) was making a wineserver IPC + socket copy for every live shadow on every ExecuteCommandLists. It now verifies readability in-process with VirtualQuery (matching views/mirrors.rs) and scans guest memory directly.
  • 4 KiB page fast-skip & lock-free pin liveness: diff_copy_chunks now skips unchanged 4 KiB pages with a single slice compare before scanning 64-byte chunks, and ImportPin exposes an Arc<AtomicBool> liveness flag so sync_shadow_imports doesn't lock GPU_APERTURE per shadow (or allocate live Vecs across steady-state submits).
  • Pre-submit probe fence: probe_mapped_import now creates fence and event before ExecuteCommandLists and retains the probe section and COM objects if a post-submit wait fails or times out.

Re-verified 42af674 on RX 6900 XT (1002:73bf, Mesa RADV 26.3.1, Proton Experimental 11.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 PASS
  • tuxbox-d3d12 Wine unit tests: 137/137 PASS

Happy to hear how loading/frame rate looks on your RX 6700 XT whenever you have time to rerun!

**@⁠DrArroway** on GitHub · 2026-11-25 22:12 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7176073195) Thank you for testing this on your RX 6700 XT and catching that ~25x slowdown, @⁠JeffreyBytes! Pushed `42af674` to address both the submit overhead and the probe fence lifetime: - **Removed `ReadProcessMemory` IPC from `sync_shadow_imports()`:** Under Wine, `ReadProcessMemory(GetCurrentProcess(), ...)` was making a `wineserver` IPC + socket copy for every live shadow on every `ExecuteCommandLists`. It now verifies readability in-process with `VirtualQuery` (matching `views/mirrors.rs`) and scans guest memory directly. - **4 KiB page fast-skip & lock-free pin liveness:** `diff_copy_chunks` now skips unchanged 4 KiB pages with a single slice compare before scanning 64-byte chunks, and `ImportPin` exposes an `Arc<AtomicBool>` liveness flag so `sync_shadow_imports` doesn't lock `GPU_APERTURE` per shadow (or allocate `live` `Vec`s across steady-state submits). - **Pre-submit probe fence:** `probe_mapped_import` now creates `fence` and `event` before `ExecuteCommandLists` and retains the probe section and COM objects if a post-submit wait fails or times out. Re-verified `42af674` on RX 6900 XT (`1002:73bf`, Mesa RADV `26.3.1`, Proton Experimental `11.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 **PASS** - `tuxbox-d3d12` Wine unit tests: **137/137 PASS** Happy to hear how loading/frame rate looks on your RX 6700 XT whenever you have time to rerun!
Author
Owner

@⁠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_PRELOAD library repeats a userptr request without ANONONLY after the kernel refused it. The launcher and tuxbox.py check load 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_writes checks the range with VirtualQuery and then reads it directly. What keeps another thread from unmapping it in between? Similarly, Mappings::apply remaps the pages before it clears live. Can a submit on another queue copy the new contents into the retired shadow in that gap?
  • Shadowed imports are traced as imported and accepted under TUXBOX_REQUIRE_BUFFER_IMPORT=1. Should strict mode refuse them, or trace them separately?
  • CPU writes are copied at submit. Can the title write data after the submit, while the GPU waits on a fence, and have the GPU miss it?
  • Two addresses mapped to the same page, or overlapping imports of different sizes, seem to get separate shadows. CPU writes reach both, but a GPU write through one would not be visible through the other. You list this as a limitation; does ProjectScarlett hit it?
  • If the GPU overwrites a chunk and the CPU then writes the old value again, the baseline compare sees no change. If the CPU changes only part of a chunk, the copy also replaces the GPU's other bytes in those 64.

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, base 1b32189: 67/67 guest checks, 17/17 regression controls including buffer-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.

**@⁠gtb-sbe** on GitHub · 2026-11-25 22:28 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7176413199) 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_PRELOAD` library repeats a userptr request without `ANONONLY` after the kernel refused it. The launcher and `tuxbox.py check` load 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_writes` checks the range with `VirtualQuery` and then reads it directly. What keeps another thread from unmapping it in between? Similarly, `Mappings::apply` remaps the pages before it clears `live`. Can a submit on another queue copy the new contents into the retired shadow in that gap? - Shadowed imports are traced as `imported` and accepted under `TUXBOX_REQUIRE_BUFFER_IMPORT=1`. Should strict mode refuse them, or trace them separately? - CPU writes are copied at submit. Can the title write data after the submit, while the GPU waits on a fence, and have the GPU miss it? - Two addresses mapped to the same page, or overlapping imports of different sizes, seem to get separate shadows. CPU writes reach both, but a GPU write through one would not be visible through the other. You list this as a limitation; does ProjectScarlett hit it? - If the GPU overwrites a chunk and the CPU then writes the old value again, the baseline compare sees no change. If the CPU changes only part of a chunk, the copy also replaces the GPU's other bytes in those 64. **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, base `1b32189`: 67/67 guest checks, 17/17 regression controls including `buffer-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.
Author
Owner

@⁠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_PRELOAD from #191 (2 runs)
Presents per 10 s while loading (main: ~1,440) ~55 ~120 ~1,440
Presents per 10 s at the Account Picker ~16 ~5 ~255
Reached the Account Picker 1 1 2
Exit 5 2 (107.0 s, 108.7 s) 2 (108.0 s, 214.5 s) 0

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 four main runs alongside these tests (f489d3c and 1b32189, this afternoon) did not hit it, though 682a0e4 did this morning in two of three runs, as noted above. So main can 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.

**@⁠JeffreyBytes** on GitHub · 2026-11-25 22:35 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7176549700) 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_PRELOAD` from #191 (2 runs) | | --- | --- | --- | --- | | Presents per 10 s while loading (`main`: ~1,440) | ~55 | ~120 | ~1,440 | | Presents per 10 s at the Account Picker | ~16 | ~5 | ~255 | | Reached the Account Picker | 1 | 1 | 2 | | Exit 5 | 2 (107.0 s, 108.7 s) | 2 (108.0 s, 214.5 s) | 0 | 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 four `main` runs alongside these tests (`f489d3c` and `1b32189`, this afternoon) did not hit it, though `682a0e4` did this morning in two of three runs, as noted above. So `main` can 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.
Author
Owner

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

**@⁠brcly** on GitHub · 2026-11-25 22:36 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7176581593) 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.
Author
Owner

@⁠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_import reads back the 0x5455_5842 sentinel 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:

  1. VirtualQuery TOCTOU & Mappings::apply remap gap:
    • Good catch on Mappings::apply — (self.apply)(...) runs before live.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. Clearing live before (self.apply) closes that gap; happy to include that fix on the next push.
    • For VirtualQuery + direct read in sync_cpu_writes, you're right that there's no guard against a concurrent unmap in between. That matches the existing pattern in views/mirrors.rs (traded against the ~25× wineserver IPC overhead from ReadProcessMemory), though a lightweight fault guard around the copy could harden it without bringing back IPC if needed.
  2. TUXBOX_REQUIRE_BUFFER_IMPORT=1 / tracing: Shadowed imports intentionally trace as imported and pass strict mode right now so check_d3d12_regressions.py (buffer-remap) passes out of the box and because strict mode was aimed at rejecting the zeroed device_standin (with the one-time mirroring through anonymous shadow heaps log line signaling fallback mode). Happy to add a distinct trace marker or have strict mode reject shadows if @⁠brcly prefers.
  3. CPU writes after submit while the GPU waits on a fence: Yes — writes made after ExecuteCommandLists won't be mirrored until the next submit. That's an inherent limitation of pre-submit mirroring compared to real shared userptr pages.
  4. Separate shadows for aliased VAs / overlapping imports: Yes, GPU writes through one shadow won't be visible through another alias. I only verified synthetic controls on my side (alongside @⁠JeffreyBytes's RX 6700 XT run on 9fa9cdf reaching the Account Picker and intro video), so I don't know yet whether deeper gameplay in ProjectScarlett hits cross-alias GPU visibility.
  5. 64-byte baseline diff edge cases: Both points are accurate. Partial-chunk clobbering could be narrowed to byte/word updates inside dirty 64-byte chunks if needed, whereas the CPU restoring a pre-GPU-write baseline value is fundamental without GPU→CPU readback.

Holding off on further edits until @⁠brcly weighs in on the preferred direction (and @⁠JeffreyBytes has a chance to check 42af674 if they want to).

**@⁠DrArroway** on GitHub · 2026-11-25 22:44 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7176725214) 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_import` reads back the `0x5455_5842` sentinel 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: 1. **`VirtualQuery` TOCTOU & `Mappings::apply` remap gap:** - Good catch on `Mappings::apply` — `(self.apply)(...)` runs before `live.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. Clearing `live` before `(self.apply)` closes that gap; happy to include that fix on the next push. - For `VirtualQuery` + direct read in `sync_cpu_writes`, you're right that there's no guard against a concurrent unmap in between. That matches the existing pattern in `views/mirrors.rs` (traded against the ~25× `wineserver` IPC overhead from `ReadProcessMemory`), though a lightweight fault guard around the copy could harden it without bringing back IPC if needed. 2. **`TUXBOX_REQUIRE_BUFFER_IMPORT=1` / tracing:** Shadowed imports intentionally trace as `imported` and pass strict mode right now so `check_d3d12_regressions.py` (`buffer-remap`) passes out of the box and because strict mode was aimed at rejecting the zeroed `device_standin` (with the one-time `mirroring through anonymous shadow heaps` log line signaling fallback mode). Happy to add a distinct trace marker or have strict mode reject shadows if @⁠brcly prefers. 3. **CPU writes after submit while the GPU waits on a fence:** Yes — writes made after `ExecuteCommandLists` won't be mirrored until the next submit. That's an inherent limitation of pre-submit mirroring compared to real shared userptr pages. 4. **Separate shadows for aliased VAs / overlapping imports:** Yes, GPU writes through one shadow won't be visible through another alias. I only verified synthetic controls on my side (alongside @⁠JeffreyBytes's RX 6700 XT run on `9fa9cdf` reaching the Account Picker and intro video), so I don't know yet whether deeper gameplay in ProjectScarlett hits cross-alias GPU visibility. 5. **64-byte baseline diff edge cases:** Both points are accurate. Partial-chunk clobbering could be narrowed to byte/word updates inside dirty 64-byte chunks if needed, whereas the CPU restoring a pre-GPU-write baseline value is fundamental without GPU→CPU readback. Holding off on further edits until @⁠brcly weighs in on the preferred direction (and @⁠JeffreyBytes has a chance to check `42af674` if they want to).
Author
Owner

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

**@⁠gtb-sbe** on GitHub · 2026-11-26 00:33 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7178884350) · edited 2026-11-26 00:49 UTC @⁠DrArroway, on cross-alias visibility in ProjectScarlett: I measured which views its refused imports come from, see https://git.gtahub.st/github/brcly--TuxBox/issues/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.
Author
Owner

@⁠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 00:45 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7179084759) · 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
Author
Owner

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

main now has a fallback from #200: when the host profile shows the GPU can't read imported game memory, TuxBox copies it. It passes buffer-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

  • XMem physical pages are section views (runtime/xmem/src/shared_pages.rs, CreateFileMappingW + MapViewOfFile3), which Wine maps as shared memory.
  • Contributors found that amdgpu refuses to import shared mappings as host pointers, because libdrm requests anonymous memory only. Private anonymous memory is accepted.
  • vkd3d-proton then substitutes other memory and still reports success.
  • Pages need to appear at more than one address. In ProjectScarlett many are mapped at 2 to 8 GPU addresses, while the CPU used only one view of each page (measurement).

Ideas to test (none proven)

  1. Find what imports. From a Wine process on RADV, try each way of allocating and mapping memory (private, pagefile section, placeholder views) and record which the GPU really reads.
  2. One CPU backing, GPU-side aliases. Back pages with private memory, import each page once, and point every GPU address at that import (placed or reserved resources). Check first whether any title reads a second CPU view.
  3. Private until aliased. Use private memory for pages with a single mapping and sections only for aliased ones. This only helps if enough pages are never aliased, so measure that first.
  4. Upstream. Ask Mesa/libdrm for a supported way to import shared mappings.

How to tell it works

  • The host profile logs section-import=aliased on RADV with no override.
  • python tuxbox.py check passes on AMD, including buffer-remap, and still passes on NVIDIA.
  • Page remapping still behaves: a GPU write through one address is visible through the others and to the CPU.

TUXBOX_HOST_PROFILE=section-import=aliased forces direct imports, which reproduces the failure on RADV.

**@⁠brcly** on GitHub · 2026-11-26 01:45 UTC · [original](https://github.com/brcly/TuxBox/pull/200#issuecomment-7180088522) 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 `main` now has a fallback from #200: when the [host profile](https://github.com/brcly/TuxBox/blob/main/docs/USAGE.md#host-profile) shows the GPU can't read imported game memory, TuxBox copies it. It passes `buffer-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** - XMem physical pages are section views (`runtime/xmem/src/shared_pages.rs`, `CreateFileMappingW` + `MapViewOfFile3`), which Wine maps as shared memory. - Contributors found that amdgpu refuses to import shared mappings as host pointers, because libdrm requests anonymous memory only. Private anonymous memory is accepted. - vkd3d-proton then substitutes other memory and still reports success. - Pages need to appear at more than one address. In ProjectScarlett many are mapped at 2 to 8 GPU addresses, while the CPU used only one view of each page ([measurement](https://git.gtahub.st/github/brcly--TuxBox/issues/191)). **Ideas to test (none proven)** 1. **Find what imports.** From a Wine process on RADV, try each way of allocating and mapping memory (private, pagefile section, placeholder views) and record which the GPU really reads. 2. **One CPU backing, GPU-side aliases.** Back pages with private memory, import each page once, and point every GPU address at that import (placed or reserved resources). Check first whether any title reads a second CPU view. 3. **Private until aliased.** Use private memory for pages with a single mapping and sections only for aliased ones. This only helps if enough pages are never aliased, so measure that first. 4. **Upstream.** Ask Mesa/libdrm for a supported way to import shared mappings. **How to tell it works** - The host profile logs `section-import=aliased` on RADV with no override. - `python tuxbox.py check` passes on AMD, including `buffer-remap`, and still passes on NVIDIA. - Page remapping still behaves: a GPU write through one address is visible through the others and to the CPU. `TUXBOX_HOST_PROFILE=section-import=aliased` forces direct imports, which reproduces the failure on RADV.
This discussion has been locked. Commenting is limited to contributors.
No description provided.