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

Merged
Kokonotcu merged 1 commit from upstream-pr/407 into main 2026-11-27 21:42:11 +00:00
Kokonotcu commented 2026-11-24 04:45:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kokonotcu on 2026-11-24T04:45:13Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5466238607 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/407). Originally posted by [Kokonotcu](https://github.com/Kokonotcu) on 2026-11-24T04:45:13Z. --- 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)
DadaCap commented 2026-11-25 00:44:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-25T00:44:26Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7148826215 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/407#issuecomment-7148826215). Originally posted by [DadaCap](https://github.com/DadaCap) on 2026-11-25T00:44:26Z. --- 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).
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
ProjectLeonida/mandy!407
No description provided.