Skip to content

Runtime: unblock independent Vulkan queue submissions during calibration - #3841

Open
pmilewsskiedu-gif wants to merge 2 commits into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/d3d12-clock-calibration-upstream-integration-20261122
Open

pmilewsskiedu-gif wants to merge 2 commits into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/d3d12-clock-calibration-upstream-integration-20261122

Conversation

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor

Claimed in current coordinator: #3796 (comment)

What and why

The fallback ID3D12CommandQueue::GetClockCalibration (Vulkan device without calibrated timestamp domains) took a device-GLOBAL submit mutex across vkQueueWaitIdle(Q1). If Q1 had a submitted GPU wait on unsignaled fence F, Q2 could not submit the Signal(F) needed to unblock Q1 because Q2 also needed that same global mutex. This creates a genuine circular deadlock between otherwise independent queues, distinct from #2951's held Wait-before-Signal batches.

Introduce per-PHYSICAL-VkQueue host synchronization shared among VulkanQueue, D3D12 calibration, native presenter and DXGI swapchain. Vulkan submits / sparse binds / queue labels acquire per-queue host mutex BEFORE brief device-global submit/ticket critical section. Blocking QueueWaitIdle acquires only the queue's mutex and leaves the global mutex free for independent queues. Queue teardown retires tickets under the global lock AFTER idle, retaining correct ticket bookkeeping. Same physical VkQueue remains externally synchronized. No game assets.

Deterministic regression and WARP parity

New CTest uses two distinct VkQueue handles from vktest::Device (two graphics queues), a COM D3D12 device and an intercepted vkGetDeviceProcAddr. Test-only vkQueueWaitIdle hook parks Q1 for at most 700ms, while an independent thread calls Q2::Signal(F,1), testing whether Q2 actually enters vkQueueSubmit before the watchdog. Hook never blocks the physical GPU indefinitely.

  • Original upstream f847748 negative control: Q2 could not submit until the 700ms watchdog; logged elapsed=700ms, watchdog=1, exit 1. Khronos validation 0 errors. This is a synthetic wait hold, not an actual device deadlock induced on RTX.
  • Patched initial base: Q2 submitted after ~1ms, watchdog=0, 0 validation errors, exit 0; repeated 12/12 validation GPU runs with 0 watchdog trips.
  • Native WARP reference: two queues with Q1 waiting on future fence and Q2 releasing it: Q2::Signal returned in 0ms, GetClockCalibration returned successfully, GPU queue completed, exit 0.

Current upstream integration and tests

Original exact commit c85bbe8 was built and tested against f847748. Upstream main subsequently advanced to 4c52ce2 and merged #2951; rather than overwrite or force-push old tested branch, this PR is the fresh isolated integration 7306e58 based on current main. Resolved the only source conflict by keeping #2951's held-operation scheduling and applying host mutex to its relocated VulkanQueue::idle().

Current-main Windows MSVC 19.44/Vulkan SDK 1.4.363 focused compile 21/21 actions. RTX 4060 Ti, LEONIDA_VK_VALIDATION=1, Medal capture disabled: 7/7 focused CTests PASS (new D3D12 calibration + WARP variant, d3d12_fence_timing, merged #2951 d3d12_queue_handover, vk_presenter_sync, vk_queue_query_sync, dxgi_swapchain), zero failures, 15.02 seconds; git diff --check clean. Earlier main-based 8/8 focused CTests pass plus 12 repeated synthetic GPU regressions.

Limitations / integration

@CowBoYdu76

Copy link
Copy Markdown
Contributor

Tested 7306e58 (based on main 4c52ce2) on Windows 11 build 26300, MSVC 19.44, Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0, with two GPUs picked by LEONIDA_VK_DEVICE: pass.

RTX 4070 SUPER (NVIDIA 616.64) Intel UHD 770 (101.7092)
d3d12_clock_calibration_unblock_test exit 0: independent Q2 submit after 0 ms; watchdog=0, validation=0 exit 77, silently
10 more runs under VK_LAYER_KHRONOS_validation 10/10 exit 0, Q2 submitted after 0 to 1 ms, watchdog 0 every time, 0 messages
full ctest 155/155 155/155 (the new test skipped)

The 77 on the UHD 770 is the expected skip. Its Vulkan driver exposes a single graphics/compute/transfer queue (queueCount 1), so vk.queues.size() < 2. The test prints nothing before returning, though, so in ctest it looks the same as a missing GPU. A line like SKIP: this device has one queue in its graphics family would make that clear in GPU reports.

AI-assisted (Claude Code). No game files.

@Flyander Flyander 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.

This queue-local lock leaves a device-wide host-synchronization gap with the bindless flush path.


bool idle() {
std::lock_guard lock(context_.submit_mutex());
std::lock_guard lock(context_.queue_host_mutex(queue_));

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.

vkDeviceWaitIdle still runs in runtime/shims/d3d12_vulkan_device.cpp under only submit_mutex, while GetClockCalibration/VulkanQueue::idle() now call vkQueueWaitIdle under this per-queue lock. VulkanQueue::execute() can invoke the bindless prepare callback while another thread is calibrating Q1, so the device-wide idle can overlap host access to that queue. Vulkan requires host access to all queues from the device to be externally synchronized during vkDeviceWaitIdle (spec); before this change both calls shared submit_mutex. Please coordinate device-wide idle with all queue host accesses (while preserving the ability for other queues to submit work needed by pending waits), and add a synthetic regression for bindless flush concurrent with calibration.

@justr4mzi

Copy link
Copy Markdown
Contributor

Tested 7306e588 on AMD Radeon RX 6700 XT (gfx1031, Windows 11 25H2 build 26200.9974, AMD driver 26.11.2, Vulkan 1.4.315, MSVC 19.44.35230.0, CMake + Ninja RelWithDebInfo):

  • Build (d3d12_clock_calibration_unblock_test, d3d12_compute_test, vk_command_replay_test, d3d12_fence_timing_test): exit 0.
  • Standard ctest --output-on-failure -R "d3d12_clock_calibration_unblock|vk_command_replay|d3d12_compute|d3d12_fence_timing": 5/5 passed (3.31 s), including #91 d3d12_clock_calibration_unblock (0.33 s) and #92 d3d12_clock_calibration_unblock_warp (0.06 s).
  • Direct runs:
    • d3d12_clock_calibration_unblock_test.exe on RX 6700 XT: calibration fallback: independent Q2 submit after 16 ms; watchdog=0, validation=0 (exit 0).
    • d3d12_clock_calibration_unblock_test.exe --warp: native WARP GetClockCalibration future wait: q2 Signal in 0 ms, PASS (exit 0).
  • Validation (LEONIDA_VK_VALIDATION=1 ctest --output-on-failure -R "d3d12_clock_calibration_unblock$|vk_command_replay|d3d12_compute|d3d12_fence_timing"): 4/4 passed (2.41 s), 0 validation errors.

AI-assisted (Gemini, Antigravity).

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 2454731 on AMD Radeon(TM) Graphics (gfx1036, driver 26.10.1) and NVIDIA GeForce RTX 5080 (driver 616.92), Windows 11, MSVC RelWithDebInfo: d3d12_clock_calibration_unblock_test (new), d3d12_present_test and d3d12_fence_timing_test, each with LEONIDA_VK_DEVICE=radeon and =5080 (device line confirmed). All six runs exit 0: fallback submit after 17 ms (Radeon) and 1 ms (5080), watchdog=0 and validation=0 on both, bindless flush + calibration parked=1 with no global idle overlap, present test M0 70 frames with swapchain recreation, calibrated timestamps and adapter LUID checks. AI-assisted.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 2454731 merged onto main 03b09a0 on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 157/157.
  • d3d12_clock_calibration_unblock and d3d12_clock_calibration_unblock_warp: pass 20 of 20 runs each, and pass under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

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.

6 participants