d3d12: CPU subresource copies wait for their own submission only (STAGED-COPY-OWN-WAIT) #407

Open
github-bot wants to merge 1 commit from github-bot/gh-407 into main AGit
Owner

@⁠Kokonotcu on GitHub · 2026-11-24 04:45 UTC · original
Kokonotcu:pr/staged-copy-wait → main · +231 −11 · 4 files · 1 commits · open

Claim: STAGED-COPY-OWN-WAIT

What was wrong

WriteToSubresource / ReadFromSubresource on a texture the host cannot copy (no VK_EXT_host_image_copy, or not in GENERAL) replay a CopyTextureRegion on a staging VulkanQueue and wait for it. The wait was VulkanQueue::idle(): vkQueueWaitIdle on info.queue (the VkQueue the first D3D12 direct queue also uses), held under the device's submit lock. Every such CPU copy waited for all the work queued on that queue, and no other queue could submit meanwhile.

What changed

  • VulkanQueue::wait_submitted() waits on the queue's own progress timeline for what it submitted, without the submit lock (100 ms slices, wakes on device loss). The staging copy uses it instead of idle().
  • The staging queue sits on a VkQueue the game's first queues are not on (CreateCommandQueue hands out each family's VkQueues from the first, so the last one): the copy family's for colour textures, whose engine runs beside queued graphics work; the graphics family's for depth/stencil and rgb96 textures (their copies may run the D24/rgb96 compute pass). A second graphics VkQueue alone did not help on NVIDIA: the timeline wait still took the full 32 ms. With one VkQueue in the family the copy shares info.queue as before.
  • No waits on other queues: D3D12 requires the application to have seen the texture's GPU work complete before a CPU copy (it waited on a fence its queue signalled); that host wait followed by this submission orders the work before the copy and makes its writes visible.

Testing

RTX 3080 Laptop GPU, i7-11800H, Windows 10 19045, NVIDIA driver, MSVC 2022 RelWithDebInfo, Vulkan SDK 1.4.350.

  • New d3d12_staged_copy_wait_test (standalone device): a staged 64x64 WriteToSubresource while the direct queue has ~32 ms of buffer copies queued: 31.8 ms before, 0.1-0.6 ms after (the direct queue still busy after it); the bytes round-trip. Fails before the change (its check that the write takes under half the queued work).
  • d3d12_subresource_copy (both paths, 109 steps against the host runtime and Microsoft's recorded answers): 0 mismatches. d3d12_rgb96: pass.
  • All three clean under LEONIDA_VK_VALIDATION=1 (0 validation errors).

🤖 Generated with Claude Code

**@⁠Kokonotcu** on GitHub · 2026-11-24 04:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/407) `Kokonotcu:pr/staged-copy-wait` → `main` · +231 −11 · 4 files · 1 commits · open Claim: STAGED-COPY-OWN-WAIT ## What was wrong `WriteToSubresource` / `ReadFromSubresource` on a texture the host cannot copy (no `VK_EXT_host_image_copy`, or not in GENERAL) replay a `CopyTextureRegion` on a staging `VulkanQueue` and wait for it. The wait was `VulkanQueue::idle()`: `vkQueueWaitIdle` on `info.queue` (the VkQueue the first D3D12 direct queue also uses), held under the device's submit lock. Every such CPU copy waited for all the work queued on that queue, and no other queue could submit meanwhile. ## What changed - `VulkanQueue::wait_submitted()` waits on the queue's own progress timeline for what it submitted, without the submit lock (100 ms slices, wakes on device loss). The staging copy uses it instead of `idle()`. - The staging queue sits on a VkQueue the game's first queues are not on (CreateCommandQueue hands out each family's VkQueues from the first, so the last one): the copy family's for colour textures, whose engine runs beside queued graphics work; the graphics family's for depth/stencil and rgb96 textures (their copies may run the D24/rgb96 compute pass). A second graphics VkQueue alone did not help on NVIDIA: the timeline wait still took the full 32 ms. With one VkQueue in the family the copy shares `info.queue` as before. - No waits on other queues: D3D12 requires the application to have seen the texture's GPU work complete before a CPU copy (it waited on a fence its queue signalled); that host wait followed by this submission orders the work before the copy and makes its writes visible. ## Testing RTX 3080 Laptop GPU, i7-11800H, Windows 10 19045, NVIDIA driver, MSVC 2022 RelWithDebInfo, Vulkan SDK 1.4.350. - New `d3d12_staged_copy_wait_test` (standalone device): a staged 64x64 `WriteToSubresource` while the direct queue has ~32 ms of buffer copies queued: 31.8 ms before, 0.1-0.6 ms after (the direct queue still busy after it); the bytes round-trip. Fails before the change (its check that the write takes under half the queued work). - `d3d12_subresource_copy` (both paths, 109 steps against the host runtime and Microsoft's recorded answers): 0 mismatches. `d3d12_rgb96`: pass. - All three clean under `LEONIDA_VK_VALIDATION=1` (0 validation errors). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
WriteToSubresource/ReadFromSubresource without host image copy replay a
CopyTextureRegion on a staging VulkanQueue and wait for it. The wait was
VulkanQueue::idle(): vkQueueWaitIdle on info.queue, the VkQueue the first
D3D12 direct queue also uses, under the device's submit lock. Every such
CPU copy waited for all the game work queued there, and no queue could
submit meanwhile.

- VulkanQueue::wait_submitted waits on the queue's own progress timeline
  for what it submitted, without the submit lock (bounded slices, wakes on
  device loss). The staging copy uses it instead of idle().
- The staging queue is on a VkQueue the game's first queues are not on
  (CreateCommandQueue hands each family's out from the first, so the
  last): the copy family's for colour textures, whose engine runs beside
  queued graphics work; the graphics family's for depth/stencil and rgb96
  textures, whose copies may run the D24/rgb96 compute pass. A second
  graphics VkQueue alone did not help on NVIDIA: the timeline wait still
  took the whole 32 ms, the copy ran behind the busy queue. Where the
  family has one VkQueue the copy shares info.queue as before.
- No waits on other queues are attached: D3D12 requires the application
  to have seen the texture's GPU work complete before a CPU copy (it waits
  on a fence its queue signalled), and that host wait followed by this
  submission orders the work before the copy and makes its writes visible.
  Pending work of other queues would only be waited for needlessly.

Measured (d3d12_staged_copy_wait_test, RTX 3080 Laptop, standalone
device): a staged 64x64 WriteToSubresource while the direct queue has 32 ms
of buffer copies queued took 31.8 ms before, 0.10 ms now (the direct queue
still busy after it); the bytes round-trip. d3d12_subresource_copy (both
paths, 109 steps against the host and Microsoft's answers), d3d12_rgb96 and
the new test are clean under LEONIDA_VK_VALIDATION=1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:58:21 +00:00
Author
Owner

@⁠DadaCap on GitHub · 2026-11-25 00:44 UTC · original

A measurement from a retail run that supports this change (not a test of this branch: I ran into the same problem in a local stack and only then found this pull request).

Retail 1.0.2.1, Windows 11, RTX 5080 (NVIDIA 621.24). In my stack the textures the title places on its own pages are uploaded with these staged copies from one worker thread, several hundred waiting at a time while the world loads. With the wait being VulkanQueue::idle():

  • the title's submitting thread sat in VulkanQueue::acquire > completed_retire_ticket, waiting for submit_mutex_, in 4 of 4 stack samples, while the worker was inside vkQueueWaitIdle;
  • the time inside ExecuteCommandLists was 19.6 s of a 19.2 s window for 31 submissions (about 0.6 s each), and the title presented one frame in that window.

With the wait changed to the queue object's own timeline semaphore and no submit lock (the first bullet of this pull request, written independently, same idea): 114 ms inside ExecuteCommandLists in a 15 s window of the same phase.

So the lock half of the description ("no other queue could submit meanwhile") is not only a 32 ms effect: with a caller that copies in a loop it stops the title's frame loop. I did not try the second half (the staging queue on another VkQueue), so nothing to report on that.

Counts and frame names only, no game data. AI-assisted (Claude Code).

**@⁠DadaCap** on GitHub · 2026-11-25 00:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/407#issuecomment-7148826215) A measurement from a retail run that supports this change (not a test of this branch: I ran into the same problem in a local stack and only then found this pull request). Retail 1.0.2.1, Windows 11, RTX 5080 (NVIDIA 621.24). In my stack the textures the title places on its own pages are uploaded with these staged copies from one worker thread, several hundred waiting at a time while the world loads. With the wait being `VulkanQueue::idle()`: - the title's submitting thread sat in `VulkanQueue::acquire` > `completed_retire_ticket`, waiting for `submit_mutex_`, in 4 of 4 stack samples, while the worker was inside `vkQueueWaitIdle`; - the time inside `ExecuteCommandLists` was 19.6 s of a 19.2 s window for 31 submissions (about 0.6 s each), and the title presented one frame in that window. With the wait changed to the queue object's own timeline semaphore and no submit lock (the first bullet of this pull request, written independently, same idea): 114 ms inside `ExecuteCommandLists` in a 15 s window of the same phase. So the lock half of the description ("no other queue could submit meanwhile") is not only a 32 ms effect: with a caller that copies in a loop it stops the title's frame loop. I did not try the second half (the staging queue on another VkQueue), so nothing to report on that. Counts and frame names only, no game data. AI-assisted (Claude Code).
This pull request has changes conflicting with the target branch.
  • runtime/shims/d3d12_vulkan_device.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/407/head:github-bot/gh-407'
git switch 'github-bot/gh-407'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-407'
git switch 'github-bot/gh-407'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-407'
git switch 'github-bot/gh-407'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-407'
git switch 'main'
git merge --squash 'github-bot/gh-407'
git switch 'main'
git merge --ff-only 'github-bot/gh-407'
git switch 'main'
git merge 'github-bot/gh-407'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.