d3d12: order tile initialization on the GPU instead of waiting on the CPU #676

Open
gtb-sbe wants to merge 1 commit from upstream-pr/676 into main
gtb-sbe commented 2026-12-03 16:09:12 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by gtb-sbe on 2026-12-03T16:09:12Z.


Problem

Creating a view or buffer in a GPU reservation binds its tiles on the device's init queue and then waits for that queue on the CPU (InitTransition::wait_for_queue), once per call. refresh_cpu_pages does the same before every ExecuteCommandLists. On amdgpu each of those signals is a full CS that walks every userptr BO (#669), so the title's streaming thread spends much of the load blocked in SRV and CBV creation. JeffreyBytes measured 113.5 s of such waits in a ~250 s load on an RX 6700 XT (#669); on my RX 9060 XT the streaming thread had 33.8 % of its samples in wait_for_queue < initialize_gpu_tiles.

Change

  • initialize_gpu_tiles and reset_gpu_tiles only record the UpdateTileMappings and mark the init queue as unsignalled. The resource and any tiles the update replaces are kept until the init fence reaches the value that covers it.
  • wait_for_gpu_tiles signals the init queue once if anything is pending and makes the given queue Wait for it on the GPU. It runs in execute_locked right before placement::submit, in present after gpu_mappings.refresh, and in the page-mapping command before its live binds. Many view creations between two submissions then cost one signal instead of one each.
  • bind_published (the page-mapping worker and both inline paths) finishes the device's init queue on the CPU before its own binds, so they can't land ahead of a deferred initialization of the same buffer. The device's init slot is shared (Arc) so the worker job can reach it.
  • apply (initial state transitions) and map_reserved_tiles keep their CPU wait; it now also covers pending updates, since they use the same queue.
  • TUXBOX_TILE_INIT=wait restores the per-call CPU wait (docs/ENVIRONMENT.md).

The audit of which queues need the ordering is on #669 (JeffreyBytes' list and mine): the guest queues, present and the page-mapping worker do; the upload, readback, lazy-publication, BLAS and detile queues only touch their own or committed memory.

Testing

On my RX 9060 XT (RADV / Mesa 26.3.1), Proton Experimental for the gate and GE-Proton11-15 for the game:

  • python tuxbox.py check (full gate): Evidence: TuxBox check of 149841d3d003, no uncommitted changes, GPU 1002:7590: 15 passed, 2 failed, 0 not run. Lanes and fixtures on the GPU and the D3D12 regression controls pass. The two failures are known on this card and unrelated: the translator alu check (23 of 372 instructions, signalling-NaN payloads through multiply-add, where RADV quiets the NaN) and the Python GuestWatch test, which fails under nixpkgs' Python wrapper (fixed in #675, not in this base).
  • Lanes self-test 33/33; queue-page-mappings (placement while a mapping is queued, CPU-map resets) 5 of 5 runs with the change and 1 with TUXBOX_TILE_INIT=wait.
  • There is no Lanes scenario that fails without this change: it only removes a CPU wait, so the old path is correct, just slow. queue-page-mappings' ordering checks are what guard the new path.
  • ProjectScarlett 1.0.2.1, 106120ca + #667, one run each with and without the change, same warm shader cache: Game Running to the intro SRL 485 s -> 174 s, streaming thread samples in tile initialization 33.8 % -> 0 %. Both runs still end in the #669 stall (intro prestream, exit 5), so this doesn't fix #669 itself.
  • Still missing: an NVIDIA card, and RDNA 2 (JeffreyBytes offered to test).

Relation to the XMem/D3D12 rework

I know submission and tile mapping are being reworked. If this clashes with that design or the rework replaces this path, please close this instead of merging it. The measurements are on #669, so nothing is lost.

AI use

I made this branch with an AI coding agent (Claude Code, following AGENTS.md). The agent read the code paths, wrote the change, built it, and ran the checks, the Lanes scenarios and the two game runs listed above on my machine; I started the game runs and pressed through the account picker.

<!-- greyfinch-sync:brcly/TuxBox:issue:5630236393 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/676). Originally posted by [gtb-sbe](https://github.com/gtb-sbe) on 2026-12-03T16:09:12Z. --- ## Problem Creating a view or buffer in a GPU reservation binds its tiles on the device's init queue and then waits for that queue on the CPU (`InitTransition::wait_for_queue`), once per call. `refresh_cpu_pages` does the same before every `ExecuteCommandLists`. On amdgpu each of those signals is a full CS that walks every userptr BO (#669), so the title's streaming thread spends much of the load blocked in SRV and CBV creation. JeffreyBytes measured 113.5 s of such waits in a ~250 s load on an RX 6700 XT (#669); on my RX 9060 XT the streaming thread had 33.8 % of its samples in `wait_for_queue` < `initialize_gpu_tiles`. ## Change - `initialize_gpu_tiles` and `reset_gpu_tiles` only record the `UpdateTileMappings` and mark the init queue as unsignalled. The resource and any tiles the update replaces are kept until the init fence reaches the value that covers it. - `wait_for_gpu_tiles` signals the init queue once if anything is pending and makes the given queue `Wait` for it on the GPU. It runs in `execute_locked` right before `placement::submit`, in present after `gpu_mappings.refresh`, and in the page-mapping command before its live binds. Many view creations between two submissions then cost one signal instead of one each. - `bind_published` (the page-mapping worker and both inline paths) finishes the device's init queue on the CPU before its own binds, so they can't land ahead of a deferred initialization of the same buffer. The device's init slot is shared (`Arc`) so the worker job can reach it. - `apply` (initial state transitions) and `map_reserved_tiles` keep their CPU wait; it now also covers pending updates, since they use the same queue. - `TUXBOX_TILE_INIT=wait` restores the per-call CPU wait (`docs/ENVIRONMENT.md`). The audit of which queues need the ordering is on #669 (JeffreyBytes' list and mine): the guest queues, present and the page-mapping worker do; the upload, readback, lazy-publication, BLAS and detile queues only touch their own or committed memory. ## Testing On my RX 9060 XT (RADV / Mesa 26.3.1), Proton Experimental for the gate and GE-Proton11-15 for the game: - `python tuxbox.py check` (full gate): `Evidence: TuxBox check of 149841d3d003, no uncommitted changes, GPU 1002:7590: 15 passed, 2 failed, 0 not run`. Lanes and fixtures on the GPU and the D3D12 regression controls pass. The two failures are known on this card and unrelated: the translator `alu` check (23 of 372 instructions, signalling-NaN payloads through multiply-add, where RADV quiets the NaN) and the Python `GuestWatch` test, which fails under nixpkgs' Python wrapper (fixed in #675, not in this base). - Lanes self-test 33/33; `queue-page-mappings` (placement while a mapping is queued, CPU-map resets) 5 of 5 runs with the change and 1 with `TUXBOX_TILE_INIT=wait`. - There is no Lanes scenario that fails without this change: it only removes a CPU wait, so the old path is correct, just slow. `queue-page-mappings`' ordering checks are what guard the new path. - ProjectScarlett 1.0.2.1, `106120ca` + #667, one run each with and without the change, same warm shader cache: `Game Running` to the intro SRL 485 s -> 174 s, streaming thread samples in tile initialization 33.8 % -> 0 %. Both runs still end in the #669 stall (intro prestream, exit 5), so this doesn't fix #669 itself. - Still missing: an NVIDIA card, and RDNA 2 (JeffreyBytes offered to test). ## Relation to the XMem/D3D12 rework I know submission and tile mapping are being reworked. If this clashes with that design or the rework replaces this path, please close this instead of merging it. The measurements are on #669, so nothing is lost. ## AI use I made this branch with an AI coding agent (Claude Code, following `AGENTS.md`). The agent read the code paths, wrote the change, built it, and ran the checks, the Lanes scenarios and the two game runs listed above on my machine; I started the game runs and pressed through the account picker.
JeffreyBytes commented 2026-12-03 16:24:09 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by JeffreyBytes on 2026-12-03T16:24:09Z.


Will run 6700 XT tests on this soon.

This PR is set up to auto close #669, will you edit that in the development sidebar so it only references?

<!-- greyfinch-sync:brcly/TuxBox:comment:7384779817 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/676#issuecomment-7384779817). Originally posted by [JeffreyBytes](https://github.com/JeffreyBytes) on 2026-12-03T16:24:09Z. --- Will run 6700 XT tests on this soon. This PR is set up to auto close #669, will you edit that in the development sidebar so it only references?
NickStallman commented 2026-12-03 16:57:57 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-12-03T16:57:57Z.


NVIDIA numbers for this change, from one 8-minute run each on an RTX 5090 (driver 620.70, GE-Proton11-15), default settings, into gameplay after the intro. Base is 106120ca plus three local fixes that keep the device alive that long (#677 among them); the second run adds this PR's head 149841d3, which merged without a code conflict.

Both runs had TUXBOX_SUBMIT_PROFILE=1, TUXBOX_STACK_SAMPLE=1, TUXBOX_GPU_TIMING=1 and TUXBOX_STALL_DUMP=1. Figures are for the time after 200 s, when the game is in the playable scene.

without with this PR
Title-thread samples in InitTransition::wait_for_queue present in two stacks (register_buffer and bind_published -> reset_native_gpu_tiles) none
Mean frame time 18.3 s (14 frames in 256 s) 20.5 s (13 frames in 257 s)
Submission CPU time per frame 6.2 s (placement 3.7 s, host 2.5 s) 8.3 s (placement 4.7 s, host 3.6 s)
GPU memory waits per frame, summed over queues 57 s 67 s
Texture readback 11.7 MiB/s 10.7 MiB/s
Timed pipeline work per second (gpu timing) 17 ms 18 ms
Device lost no no

So on this card the change does what it says: the title's threads no longer wait in wait_for_queue. It does not make the frames faster here. They stay at about 20 s with the GPU nearly idle, and the differences between the two columns are within what I'd expect from one run each.

What is left in the samples:

  • address_srv -> Placements::mapped_texture, waiting for the submissions mutex. Submission preparation holds the same mutex, so these threads are waiting for submissions whose placement step takes 0.5-1.8 s a batch.
  • Inside a submission, placement::submit -> queue_use::Batch::execute -> memory_wait::submit -> QueuedCopies::record, with the innermost frames in a vkd3d call into the driver.

For comparison, a build from before the page-mapping rework runs the same scene on this machine at about 0.7 s a frame with 0.37 s of submission time, of which the host call is 1-2 ms.

I haven't run the GPU checks on this PR's head.

🤖 Generated with Claude Code

<!-- greyfinch-sync:brcly/TuxBox:comment:7385741153 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/676#issuecomment-7385741153). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-12-03T16:57:57Z. --- NVIDIA numbers for this change, from one 8-minute run each on an RTX 5090 (driver 620.70, GE-Proton11-15), default settings, into gameplay after the intro. Base is `106120ca` plus three local fixes that keep the device alive that long (#677 among them); the second run adds this PR's head `149841d3`, which merged without a code conflict. Both runs had `TUXBOX_SUBMIT_PROFILE=1`, `TUXBOX_STACK_SAMPLE=1`, `TUXBOX_GPU_TIMING=1` and `TUXBOX_STALL_DUMP=1`. Figures are for the time after 200 s, when the game is in the playable scene. | | without | with this PR | |---|---|---| | Title-thread samples in `InitTransition::wait_for_queue` | present in two stacks (`register_buffer` and `bind_published -> reset_native_gpu_tiles`) | none | | Mean frame time | 18.3 s (14 frames in 256 s) | 20.5 s (13 frames in 257 s) | | Submission CPU time per frame | 6.2 s (placement 3.7 s, host 2.5 s) | 8.3 s (placement 4.7 s, host 3.6 s) | | GPU memory waits per frame, summed over queues | 57 s | 67 s | | Texture readback | 11.7 MiB/s | 10.7 MiB/s | | Timed pipeline work per second (`gpu timing`) | 17 ms | 18 ms | | Device lost | no | no | So on this card the change does what it says: the title's threads no longer wait in `wait_for_queue`. It does not make the frames faster here. They stay at about 20 s with the GPU nearly idle, and the differences between the two columns are within what I'd expect from one run each. What is left in the samples: - `address_srv -> Placements::mapped_texture`, waiting for the `submissions` mutex. Submission preparation holds the same mutex, so these threads are waiting for submissions whose placement step takes 0.5-1.8 s a batch. - Inside a submission, `placement::submit -> queue_use::Batch::execute -> memory_wait::submit -> QueuedCopies::record`, with the innermost frames in a vkd3d call into the driver. For comparison, a build from before the page-mapping rework runs the same scene on this machine at about 0.7 s a frame with 0.37 s of submission time, of which the host call is 1-2 ms. I haven't run the GPU checks on this PR's head. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
JeffreyBytes commented 2026-12-03 17:54:11 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by JeffreyBytes on 2026-12-03T17:54:11Z.


RDNA 2 run on the head (149841d3). RX 6700 XT 12 GB (1002:73df), Mesa 26.3.1 RADV, GE-Proton11-15, full check:

Evidence: TuxBox check of 149841d3d003, no uncommitted changes, GPU default adapter: 17 passed, 0 failed, 0 not run

No amdgpu faults or resets in the kernel log during the run. Game runs against #667 alone are next; I'll post them when they're done.

<!-- greyfinch-sync:brcly/TuxBox:comment:7387255817 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/676#issuecomment-7387255817). Originally posted by [JeffreyBytes](https://github.com/JeffreyBytes) on 2026-12-03T17:54:11Z. --- RDNA 2 run on the head (149841d3). RX 6700 XT 12 GB (`1002:73df`), Mesa 26.3.1 RADV, GE-Proton11-15, full check: ```text Evidence: TuxBox check of 149841d3d003, no uncommitted changes, GPU default adapter: 17 passed, 0 failed, 0 not run ``` No amdgpu faults or resets in the kernel log during the run. Game runs against #667 alone are next; I'll post them when they're done.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/676:upstream-pr/676'
git switch 'upstream-pr/676'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/676'
git switch 'upstream-pr/676'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/676'
git switch 'upstream-pr/676'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/676'
git switch 'main'
git merge --squash 'upstream-pr/676'
git switch 'main'
git merge --ff-only 'upstream-pr/676'
git switch 'main'
git merge 'upstream-pr/676'
git push origin 'main'
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!676
No description provided.