d3d12, xmem: check only the placed textures that may have been written #430

Merged
NickStallman merged 15 commits from upstream-pr/430 into main 2026-11-29 01:16:20 +00:00
NickStallman commented 2026-11-28 23:54:51 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-28T23:54:51Z.


Summary

Placed textures are checked against title memory on every submission. After this change, D3D12 checks only textures that may have been written, using the OS write watch on XMem's private pages, so an unchanged texture costs one atomic load instead of a read and digest.

What changes

  • XMem allocates private CPU views and arenas with MEM_WRITE_WATCH. One scan per refresh finds the pages written since the last one. A page's range is marked before its texture is read, so no write is missed between the check and the next scan.
  • D3D12 keeps a watch per texture and digests only those that were written, or that can't be tracked. The digest stays as the filter before any upload. A whole texture is digested in place, with one XMem lookup.
  • Pages under a host import hold are tracked only where the host allows it (a probe, not a vendor check): on the RTX 5090 CPU writes stay visible through an import and GPU writes don't; on RADV every submit marks them, so they stay checked every batch. GPU writes come from D3D12's own write records.
  • Known writers note their writes: DirectStorage page writes, Unmap, and publication write-back.
  • Submission-side costs: a texture's reader list is kept under its own lock, so a submission doesn't wait for a publication job that holds the texture's contents; writes are indexed once per batch; fences are compared by pointer instead of COM equality, which called QueryInterface per unequal fence.
  • TUXBOX_PLACEMENT_WATCH=0 turns it off. TUXBOX_PLACEMENT_LAZY_FLUSH=1 (off by default) scans only the first D3DFlushEntireCpuCache after a submission; it is not exact, and the docs say why.
  • TUXBOX_SUBMIT_PROFILE=1 reports each step.

What it doesn't do: reduce the readback of GPU-written textures. That is the larger cost in the game, and #413 builds on this branch.

Measured

  • Unchanged refresh, release build, RTX 5090, real xmem.dll: BC7 128² 0.140 ms/MiB without the watch, 0.0004 with it; BC1 1024² 0.046–0.057 against 0.00015; RGBA8 2048² 0.040 against 0.00014. A 1 GiB scan of 16 runs takes 0.15 ms.
  • Reader fences, release unit test, AMD iGPU, 33,000 textures: 8 busy queues, 21.6 ms per batch before, 0.8 ms after.
  • In ProjectScarlett, by two testers on their own cards, with this inside #413 (so with its ownership work too, which both found makes no difference): DrArroway, RX 6900 XT, world frames from 1 every 13–16 s to 2 every 13–14 s; 1093i3511, RX 6600, the first world frame after the burst from 22–28 s to 12 s. World frame counts are small, and the larger cost is still the readback.
  • I have no valid game measurement of my own. Our profiled runs used a vkd3d-proton with tracing compiled in, which makes QueryInterface about 12 times slower, and some ran with the desktop locked.

Known limits

  • A section-backed or unmapped page, and memory XMem doesn't manage, stays checked every batch.
  • On RADV, imported pages are always checked, as above. On the RTX 5090, a batch with any write record is assumed to write every imported buffer whose writes can't be named.
  • About 1.5% of parallel d3d12 unit-test runs abort in Wine with double free or corruption. It also happens without this change, on both cards, before any GPU test finishes. I haven't found the cause.
  • Not run on Windows.

Tested

  • The checks relevant to this change passed
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below
  • Where the table calls for AMD and NVIDIA, they passed on both

Tests: XMem (writes at the first and last byte and page boundaries, ReadFile, WriteProcessMemory, a write between scan and re-arm, import holds, section promotion, remaps, merged runs), D3D12 (unchanged textures do no digest, held and section textures are always checked, a texture re-armed before it is read, the reader and write indexes against brute force, the lock split, the fence-compare count). A texture-publication, texture-readback, cross-queue and copy-memory Lanes pass on both cards.

GPU, driver and backend:

  • RTX 5090 (10de:2b85), NVIDIA 620.70.10
  • AMD Raphael iGPU (1002:13c0), Mesa RADV 26.0.8
  • both on GE-Proton11-14

Commands run and their results:

python tuxbox.py check --no-gpu
  all 7 steps passed
python tuxbox.py check --adapter 1002:13c0 --proton GE-Proton11-14 --prefix PREFIX --windows-dxc DXC/bin/x64
  all 11 steps passed
python tuxbox.py check --adapter 10de:2b85 --proton GE-Proton11-14 --prefix PREFIX --windows-dxc DXC/bin/x64
  all 11 steps passed

🤖 Generated with Claude Code

<!-- greyfinch-sync:brcly/TuxBox:issue:5545804789 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/430). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-28T23:54:51Z. --- ## Summary Placed textures are checked against title memory on every submission. After this change, D3D12 checks only textures that may have been written, using the OS write watch on XMem's private pages, so an unchanged texture costs one atomic load instead of a read and digest. **What changes** - **XMem** allocates private CPU views and arenas with `MEM_WRITE_WATCH`. One scan per refresh finds the pages written since the last one. A page's range is marked before its texture is read, so no write is missed between the check and the next scan. - **D3D12** keeps a watch per texture and digests only those that were written, or that can't be tracked. The digest stays as the filter before any upload. A whole texture is digested in place, with one XMem lookup. - **Pages under a host import hold** are tracked only where the host allows it (a probe, not a vendor check): on the RTX 5090 CPU writes stay visible through an import and GPU writes don't; on RADV every submit marks them, so they stay checked every batch. GPU writes come from D3D12's own write records. - **Known writers note their writes:** DirectStorage page writes, `Unmap`, and publication write-back. - **Submission-side costs:** a texture's reader list is kept under its own lock, so a submission doesn't wait for a publication job that holds the texture's contents; writes are indexed once per batch; fences are compared by pointer instead of COM equality, which called `QueryInterface` per unequal fence. - `TUXBOX_PLACEMENT_WATCH=0` turns it off. `TUXBOX_PLACEMENT_LAZY_FLUSH=1` (off by default) scans only the first `D3DFlushEntireCpuCache` after a submission; it is **not** exact, and the docs say why. - `TUXBOX_SUBMIT_PROFILE=1` reports each step. **What it doesn't do:** reduce the readback of GPU-written textures. That is the larger cost in the game, and #413 builds on this branch. ## Measured - **Unchanged refresh,** release build, RTX 5090, real `xmem.dll`: BC7 128² 0.140 ms/MiB without the watch, 0.0004 with it; BC1 1024² 0.046–0.057 against 0.00015; RGBA8 2048² 0.040 against 0.00014. A 1 GiB scan of 16 runs takes 0.15 ms. - **Reader fences,** release unit test, AMD iGPU, 33,000 textures: 8 busy queues, 21.6 ms per batch before, 0.8 ms after. - **In ProjectScarlett,** by two testers on their own cards, with this inside #413 (so with its ownership work too, which both found makes no difference): DrArroway, RX 6900 XT, world frames from 1 every 13–16 s to 2 every 13–14 s; 1093i3511, RX 6600, the first world frame after the burst from 22–28 s to 12 s. World frame counts are small, and the larger cost is still the readback. - I have **no valid game measurement of my own.** Our profiled runs used a vkd3d-proton with tracing compiled in, which makes `QueryInterface` about 12 times slower, and some ran with the desktop locked. ## Known limits - A section-backed or unmapped page, and memory XMem doesn't manage, stays checked every batch. - On RADV, imported pages are always checked, as above. On the RTX 5090, a batch with any write record is assumed to write every imported buffer whose writes can't be named. - About 1.5% of parallel d3d12 unit-test runs abort in Wine with `double free or corruption`. It also happens without this change, on both cards, before any GPU test finishes. I haven't found the cause. - **Not run on Windows.** ## Tested - [x] The checks relevant to this change passed - [x] Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below - [x] Where the table calls for AMD and NVIDIA, they passed on both **Tests:** XMem (writes at the first and last byte and page boundaries, ReadFile, WriteProcessMemory, a write between scan and re-arm, import holds, section promotion, remaps, merged runs), D3D12 (unchanged textures do no digest, held and section textures are always checked, a texture re-armed before it is read, the reader and write indexes against brute force, the lock split, the fence-compare count). A texture-publication, texture-readback, cross-queue and copy-memory Lanes pass on both cards. GPU, driver and backend: - RTX 5090 (10de:2b85), NVIDIA 620.70.10 - AMD Raphael iGPU (1002:13c0), Mesa RADV 26.0.8 - both on GE-Proton11-14 Commands run and their results: ```text python tuxbox.py check --no-gpu all 7 steps passed python tuxbox.py check --adapter 1002:13c0 --proton GE-Proton11-14 --prefix PREFIX --windows-dxc DXC/bin/x64 all 11 steps passed python tuxbox.py check --adapter 10de:2b85 --proton GE-Proton11-14 --prefix PREFIX --windows-dxc DXC/bin/x64 all 11 steps passed ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
brcly/TuxBox!430
No description provided.