Skip to content

D3D-97: give live D3D12 queues distinct Vulkan queues - #1404

Merged
ItsNotRexed merged 7 commits into
ProjectLeonida:mainfrom
kovaaper:codex/d3d97-separate-queues
Nov 22, 2026
Merged

ItsNotRexed merged 7 commits into
ProjectLeonida:mainfrom
kovaaper:codex/d3d97-separate-queues

Conversation

@kovaaper

@kovaaper kovaaper commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D-97 — #634 (comment)

What and why

A Wait submitted to the shared Vulkan queue can block a Signal from another D3D12 queue behind it. The standalone device now requests up to 16 handles from its graphics-capable queue family and leases a distinct handle to each live copy, compute or direct queue. The lease is returned after the queue is idle; exhausting the pool returns E_OUTOFMEMORY rather than silently sharing a handle. A synthetic test exercises copy/compute and direct/direct Wait-before-Signal ordering.

Caller-owned Vulkan devices with only the legacy queue field keep their existing single-queue behavior; they can supply command_queues to opt into separate handles. Dedicated compute/transfer families remain follow-up work because resources need queue-family ownership transfers. Related: #306. No game files or shaders.

Tested

  • Windows 11, NVIDIA GeForce RTX 3060, driver 621.01; LEONIDA_VK_VALIDATION=1 with Vulkan SDK 1.4.363.0 validation layer.
  • MSVC/Ninja build of d3d12_queue_wait_test, d3d12_com_test and d3d12_vulkan_device_test: passed.
  • After recompiling d3d12_com_test, ctest -R "^(d3d12_queue_wait|d3d12_vulkan_device|d3d12_com)$" --output-on-failure: 3/3 passed. The earlier line 209 failure did not reproduce after the rebuild; no source change was needed for that test.
  • Published six-file diff checked against the locally tested tree; git diff --check passed.

Checklist

  • No game data, keys or decryption, including in logs or reports quoted here.
  • AI-assisted (Codex).

Explain cross-queue Wait-before-Signal progress and the caller-owned legacy limitation. AI-assisted (Codex).
Build a synthetic Wait-before-Signal test. AI-assisted (Codex).
Give each live standalone D3D12 queue its own graphics-family VkQueue so cross-queue Wait-before-Signal can progress. Preserve legacy caller-owned devices. AI-assisted (Codex).
Exercise copy/compute and direct/direct waits with synthetic fences on Vulkan. AI-assisted (Codex).
Keep the published diff limited to the new regression-test target. AI-assisted (Codex).
Keep the published diff limited to queue leasing. AI-assisted (Codex).

@whattheflop whattheflop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent AI-assisted review of exact head bed399b7b615b4406c0ac118556e64b11b6b9e8a (Codex). D3D-97 is kovaaper's existing claim.

Native Windows preflight. Windows 11 Pro build 26200 on Ryzen 7 9800X3D. The stock d3d12_queue_wait_test target built in RelWithDebInfo with CMake 4.4.4, MSVC 19.38.33145, Ninja 1.13.2 and Vulkan SDK 1.4.363.0. CTest enumerated one focused test. Two isolated vulkaninfo inventories and two validation-enabled summaries completed, with one VK_DRIVER_FILES manifest and one physical device per process. The loader confirmed Khronos validation 1.4.363 was loaded in both summaries.

Adapter Driver Vulkan API First graphics family Queue count / head's requested count
Radeon(TM) Graphics (9800X3D iGPU) AMD 25.10.30.02 (32.0.21030.2001) 1.4.315 0 8 / 8
GeForce RTX 3080 NVIDIA 616.64 (32.0.16.1664) 1.4.351 0 16 / 16

Both GPUs report the four required device features and have capacity for all four live queues. Stock execution counts were 0 pass, 0 skip, 0 fail (0 launches). Independent progress and same-device lease reuse remain unverified on hardware.

Submission ordering. VulkanQueue::submit signals the same device-wide retirement timeline from every queue. The mutex serializes host submission and ticket assignment. Once queues have separate handles those tickets can execute out of order. In the stock sequence, the batch with the higher retirement ticket can signal it before the batch waiting on that producer signals its lower ticket. Vulkan requires every signal to exceed the timeline value at execution, and a higher completed ticket cannot establish that smaller tickets have retired. Vulkan VkSubmitInfo valid usage.

Timeout cleanup. The test uses a 3 s wait per event and a 20 s CTest timeout. Its fallback signals the gates at 1, which the fence shim drops after a queue has claimed 2, including if that queue submission fails after the claim. wait_for closes the event even on timeout while the dispatcher can still hold the handle. The fences also go out of scope before the queues idle. I stopped at build and inventory because the test's timeout path does not establish safe cleanup.

@MIGHTYLUV

Copy link
Copy Markdown

Code review of bed399b7 (D3D-97):

  • Queue allocation: Querying queue count via vkGetPhysicalDeviceQueueFamilyProperties with std::min(16u, families[info.queue_family].queueCount) and passing uniform 1.0f priorities safely stays within driver family limits.
  • Lease RAII lifetime: Storing queue_lease_ in CommandQueue ensures the custom deleter returns queue_busy_[slot] = false before Child<CommandQueue>::~Child invokes device_->Release(), so the capturing Device pointer is guaranteed to be valid during the release callback.
  • Graceful fallback: Maintaining single-queue path when state_.info.command_queues.empty() preserves backwards compatibility for caller-owned devices while standalone devices get distinct handles.
  • Error handling: Returning E_OUTOFMEMORY on pool exhaustion rather than silently aliasing handles correctly avoids hidden deadlocks.

Clean implementation and solid test coverage. AI-assisted.

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested bed399b on an NVIDIA GeForce RTX 3070, driver 610.88, Windows 11 (26300), MSVC 19.44 (VS generator, RelWithDebInfo, /W4), Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1: passes on this GPU. This adds the hardware run @whattheflop's review stopped short of, plus two points below.

  • ctest: 63/63, including d3d12_queue_wait.
  • d3d12_queue_wait_test: copy/compute and direct/direct Wait-before-Signal completed, 0 validation messages. With core validation on, no timeline-semaphore errors either. That doesn't rule out the retirement-ticket ordering @whattheflop described, since it depends on execution order.
  • d3d12_com_test passes here: the line 209 failure you saw doesn't reproduce on this machine. d3d12_vulkan_device_test (both the host-image-copy and the staging paths) and vk_command_replay_test: exit 0, 0 validation messages.
  • Negative control: with d3d12_vulkan_device.cpp from the merge base f85582e (one shared VkQueue), the new test fails as intended: cross-queue Wait-before-Signal did not complete, exit 1. So the test catches the deadlock.
  • That control also ran the test's timeout path, and it confirms @whattheflop's cleanup concern. After the fallback Signal(1), teardown hit VUID-vkDestroySemaphore-semaphore-05149 8 times: semaphores destroyed while still in use by the queue.

1. The pool is finite, and exhausting it fails queue creation. The pool is min(16, queueCount) of the graphics family: 16 here, and 16 and 8 in @whattheflop's NVIDIA and AMD inventories. D3D12 itself has no such limit. To see what happens when the pool runs out, I changed std::min(16u, to std::min(1u, and reran:

  • The second CreateCommandQueue failed with [rt] d3d12: no independent Vulkan queue available (E_OUTOFMEMORY), where main would hand out a shared handle.
  • d3d12_queue_wait_test returns 77 when a CreateCommandQueue fails, so ctest reports a skip in that case rather than a failure.

Would it work to fall back to sharing a handle (today's behaviour) with a one-time log once the pool is exhausted, rather than failing? That's for a title that creates more queues than the family has, or a driver that exposes very few.

2. Staging shares handle 0 with the first D3D12 queue. info.queue = command_queues[0], and staged_copy submits on info.queue, then calls staging_queue->idle(). VulkanQueue::idle() (and ~VulkanQueue) hold the context-wide submit_mutex_ across vkQueueWaitIdle. So suppose the first D3D12 queue has a pending Wait that another queue will Signal later. A WriteToSubresource/ReadFromSubresource that takes the staging path would then block in vkQueueWaitIdle while holding submit_mutex_, and the other queue's Signal submit would wait on that lock forever. A CPU ID3D12Fence::Signal would still unblock it, since vkSignalSemaphore doesn't take the lock.

The staging path only runs without VK_EXT_host_image_copy. This GPU has it, so this comes from reading the code, not a reproduction. Leasing D3D12 queues from index 1 when there is more than one handle, keeping 0 for staging, would avoid it. So would waiting on the queue's own timeline instead of vkQueueWaitIdle under the lock.

The lease's lifetime itself looks right to me, as @MIGHTYLUV also found.

AI-assisted (Claude Code).

@ianneobot-cyber

Copy link
Copy Markdown

Tested bed399b on Windows 11 (native, MSVC 17.14, Vulkan SDK 1.4.363), NVIDIA RTX 3070 Ti Laptop (621.14), LEONIDA_VK_VALIDATION=1: pass

One robustness point from the code read: every lease comes from info.queue_family, capped at its queueCount, and once they're used up CreateCommandQueue returns E_OUTOFMEMORY (no independent Vulkan queue available). Current drivers have room (16 on NVIDIA here; 8 on recent AMD Windows drivers), but the graphics family has a single queue on older AMD Windows drivers, e.g. RX 6800 XT on 2.0.258 (gpuinfo 20200) vs 8 on 2.0.353 (42800), and on Polaris's final driver 2.0.283 (40823). There, the second D3D12 queue would fail where main succeeds. Falling back to sharing a handle (the pre-PR behaviour) when no free queue is left, or leasing COMPUTE/COPY queues from the compute-only and transfer-only families, would avoid that.

Related: d3d12_queue_wait_test returns 77 when a CreateCommandQueue fails (lines 29-34), so on such a device ctest would report Skipped rather than the failure.

The CI failure on this PR is d3d12_present from main c2967a5 on a runner without a GPU, which #908 addresses; not this change.

@ItsNotRexed
ItsNotRexed merged commit 35804cf into ProjectLeonida:main Nov 22, 2026
9 of 10 checks passed
@kapitche

kapitche commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): pass. ctest: 68/68 on the RTX 4080 SUPER. On the Intel UHD 770 the queue tests pass too. The Intel UHD 770 failures are the known ones (vk_binary_occlusion fixed by #1321, d3d12_descriptor min-LOD, d3d12_copy_xfmt).

@kovaaper

Copy link
Copy Markdown
Contributor Author

Follow-up validation after the merge: I rebuilt d3d12_com_test from the current D3D-97 tree and reran the selected tests on Windows 11 / RTX 3060 (driver 621.01) with LEONIDA_VK_VALIDATION=1. d3d12_com, d3d12_queue_wait, and d3d12_vulkan_device all pass (3/3). The earlier line 209 failure did not reproduce after recompilation, so I corrected the PR description and coordination report; no source change was needed for that test. I am separately investigating the retirement-timeline and staging-queue concerns raised in review. AI-assisted (Codex).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants