Skip to content

Runtime: fences and timing on the Vulkan device (D3D-30) - #423

Merged
ItsNotRexed merged 7 commits into
ProjectLeonida:mainfrom
MrCheese5:claude/d3d-30-fences-timing-7ce86f
Nov 22, 2026
Merged

ItsNotRexed merged 7 commits into
ProjectLeonida:mainfrom
MrCheese5:claude/d3d-30-fences-timing-7ce86f

Conversation

@MrCheese5

Copy link
Copy Markdown
Contributor

D3D-30 from #313 (claimed in #156). Four small device and queue methods that were stubs:

  • SetEventOnMultipleFenceCompletion (ALL and ANY). A null event blocks in one vkWaitSemaphores over the fences' timeline semaphores (VK_SEMAPHORE_WAIT_ANY_BIT for ANY). With an event, each fence's existing wait thread counts down one shared waiter (1 for ANY, the fence count for ALL), so no thread is added. SetEventOnCompletion now uses the same waiter. Values already reached are counted before the call returns. Bad flags, a null array or a fence from elsewhere give E_INVALIDARG.
  • Queue GetClockCalibration. When the device has VK_KHR_calibrated_timestamps or VK_EXT_calibrated_timestamps with the DEVICE and QUERY_PERFORMANCE_COUNTER domains, it is one vkGetCalibratedTimestampsKHR call. The standalone device now enables whichever extension the GPU has, and VulkanDeviceInfo::calibrated_timestamps tells the device a caller enabled it. Without the extension, the queue goes idle, then submits a vkCmdWriteTimestamp and answers with the midpoint of the QPC reads around it. The GPU stamp is in device ticks (1e9 / timestampPeriod per second), the units Runtime: query heaps on Vulkan: occlusion, timestamps, pipeline statistics (D3D-15) #278's GetTimestampFrequency uses.
  • GetAdapterLuid: from VkPhysicalDeviceIDProperties::deviceLUID.
  • GetCustomHeapProperties: the D3D12 docs' table for a NUMA adapter, consistent with the UMA = FALSE that D3D12_FEATURE_ARCHITECTURE reports. CUSTOM and unknown types get an all-zero answer, logged.

docs/d3d12_vulkan.md sections 4.1 and 4.4 describe each answer and how it was checked.

This doesn't overlap #306: VulkanQueue and vulkan_backend.h are untouched. create_command_queue gains one defaulted parameter, and VulkanDeviceInfo gains one field at the end. If #306 lands first, the rebase is a one-line call-site merge.

Tested

New GPU test runtime/test/d3d12_fence_timing_test.cpp, through the Xbox-ABI proxy:

  • Both calibration paths: the fallback on a device without the extension, and calibrated timestamps on the standalone device. The test asserts the queue uses calibrated timestamps whenever the GPU has the extension. Two calibrations 50 ms apart, GPU interval vs CPU interval: 2 µs apart with calibrated timestamps, 20 to 28 µs with the fallback, 0.4 ms with the fallback under validation (the test fails above 2 ms). The CPU stamp falls between QPC reads taken around the call.
  • Multiple-fence waits: ALL and ANY, with an event and blocking, with CPU and GPU (queue) signals, values already reached, a fence released mid-wait, and bad arguments.
  • GetAdapterLuid: IDXGIFactory4::EnumAdapterByLuid finds the RTX 4090.
  • GetCustomHeapProperties: DEFAULT, UPLOAD and READBACK are byte-identical to the host D3D12 runtime's answers.

Passes with and without LEONIDA_VK_VALIDATION=1 (no validation errors). d3d12_vulkan_device_test still passes under validation. ctest: 18/18 on current main. The build is clean with /W4.

Tested on an NVIDIA GeForce RTX 4090, driver 621.14, Windows 11 build 26200, MSVC 19.44, Vulkan SDK 1.4.363.0. No game files involved.

AI-assisted: written with Claude Code (Claude Opus 5.5). I reviewed and ran it.

🤖 Generated with Claude Code

- SetEventOnMultipleFenceCompletion, ALL and ANY: a null event is one
  vkWaitSemaphores (WAIT_ANY_BIT for ANY); with an event, each fence's
  existing wait thread counts down one shared waiter, so no new threads.
- Queue GetClockCalibration: vkGetCalibratedTimestampsKHR (or the EXT
  alias) over the DEVICE and QueryPerformanceCounter domains when the
  device has the extension (the standalone device now enables it);
  otherwise a submitted vkCmdWriteTimestamp bracketed by QPC reads.
- GetAdapterLuid from VkPhysicalDeviceIDProperties::deviceLUID.
- GetCustomHeapProperties per the D3D12 docs' NUMA table, matching the
  UMA = FALSE the device reports.

d3d12_fence_timing_test covers both calibration paths, the LUID against
DXGI's adapter list and the heap answers against the host runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MrCheese5
MrCheese5 force-pushed the claude/d3d-30-fences-timing-7ce86f branch from 539c114 to 25b8b0f Compare November 22, 2026 00:12
@MrCheese5

Copy link
Copy Markdown
Contributor Author

Rebased on main 4abd545 (now 25b8b0f) and re-tested on the RTX 4090: ctest 19/19; d3d12_fence_timing_test and d3d12_vulkan_device_test pass with LEONIDA_VK_VALIDATION=1.

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested 25b8b0f on NVIDIA GeForce RTX 3070 (driver 610.88), Windows 11 (build 26300), MSVC 14.44: pass

  • Clean RelWithDebInfo build of runtime/ (VS 2022 generator, Vulkan SDK 1.4.363): no warnings. ctest -C RelWithDebInfo: 19/19.
  • d3d12_fence_timing_test with LEONIDA_VK_VALIDATION=1: exit 0, 0 VUID lines. Both clock paths ran:
    • calibrated timestamps are available on this GPU and used by the queue; between two calibrations 50 ms apart, GPU and CPU agree to 0.6 us;
    • the submitted timestamp-query fallback (test device without the extension) agrees to 82.8 us, within the half round trip the code comment allows for.
    • GetAdapterLuid names the DXGI adapter of the same GPU, and GetCustomHeapProperties for DEFAULT, UPLOAD and READBACK matches the host runtime. The multiple-fence ALL/ANY checks pass.
  • d3d12_com_test, d3d12_vulkan_device_test and vk_command_replay_test (parts A to D) also pass under validation with 0 VUID lines.
  • Code read of the shim changes: the shared FenceWaiter countdown handles ANY and ALL cleanly (later hits after the event is set do nothing, and satisfied ANY waits are dropped from other fences' lists), and the blocking path maps ANY/ALL straight onto vkWaitSemaphores. Nothing to change from my side.

Synthetic fixtures only; AI-assisted testing (Claude Code).

@NRXM

NRXM commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 25b8b0f on an NVIDIA RTX 4090 (621.24) and an AMD Radeon iGPU (2.0.353), Windows 11 (build 26300), MSVC 14.44, Vulkan SDK 1.4.363.0, clean Ninja build of runtime/: pass on both.

  • ctest 19/19 on each GPU
  • new d3d12_fence_timing_test passes on both GPUs (multiple-fence waits, clock calibration, adapter LUID and custom heap properties)
  • d3d12_com_test, d3d12_descriptor_test, d3d12_uint_clear_test, d3d12_vulkan_device_test and vk_command_replay_test exit 0 on each GPU
  • everything ran with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~: no validation-layer errors on either GPU (only loader notices); the AMD run used VK_LOADER_DRIVERS_SELECT=*amd*

(AI-assisted test run.)

…timing-7ce86f

# Conflicts:
#	docs/d3d12_vulkan.md
#	runtime/CMakeLists.txt
#	runtime/shims/d3d12_com.h
#	runtime/shims/d3d12_vulkan_device.cpp
@MrCheese5

Copy link
Copy Markdown
Contributor Author

Merged main c432cdc in (now 49408da; calibrated timestamps join main's standalone-device extension list) and re-tested on the RTX 4090: ctest 27/27; d3d12_fence_timing_test and d3d12_vulkan_device_test pass with LEONIDA_VK_VALIDATION=1.

@matheuskshn

Copy link
Copy Markdown
Contributor

Code review at 49408da. The event-backed multi-fence wait narrows the public UINT count to int for its ALL countdown, while the blocking Vulkan path preserves the unsigned count. An unsigned counter or an explicit checked bound would keep those paths consistent. The current tests cover both ALL and ANY with ordinary counts. AI-assisted code review.

MrCheese5 and others added 3 commits November 22, 2026 02:55
SetEventOnMultipleFenceCompletion narrowed its UINT count to int for the
ALL countdown, so a count past INT_MAX started negative, looked done and
never set the event. The waiter now counts down a std::atomic<UINT> that
stops at zero, so later ANY hits do not wrap. It moves to
shims/d3d12_fence_waiter.h, and the new ctest d3d12_fence_waiter checks
counts past INT_MAX, ALL, and hits past zero without a GPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…timing-7ce86f

# Conflicts:
#	runtime/shims/d3d12_com.cpp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MrCheese5

Copy link
Copy Markdown
Contributor Author

Thanks @matheuskshn, good catch: int(count) made any count past INT_MAX start negative, so the waiter looked done at once and the event was never set. Fixed in d8397aa (now ca9a76b, with main 67204bf merged in):

  • The waiter counts down a std::atomic<UINT>, the API's own type, so both the event path and the blocking vkWaitSemaphores path take the full unsigned count. A compare-exchange stops it at zero, so the extra hits from the other fences under ANY don't wrap it back to UINT_MAX.
  • It moved to runtime/shims/d3d12_fence_waiter.h. A new ctest, d3d12_fence_waiter (no GPU), covers counts of INT_MAX + 1 and UINT_MAX (not done early, counts down one per hit, the event is set only on the last hit), ALL over three fences, and hits past zero under ANY.

Re-tested on the RTX 4090 (621.14): ctest 30/30. d3d12_fence_timing_test and d3d12_vulkan_device_test pass with LEONIDA_VK_VALIDATION=1, and the timing test also passes without it. AI-assisted.

@quackottu

Copy link
Copy Markdown
Contributor

Tested ca9a76b on NVIDIA GeForce RTX 5070, driver 621.24, Windows 11 Pro (26300), VS 2022 Build Tools, Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1 and LEONIDA_D3D12=vulkan: pass

Implicit Vulkan layers (overlays) were turned off with an empty VK_IMPLICIT_LAYER_PATH. A stale overlay entry in the registry otherwise makes every test report "2 validation errors" from the loader's Failed to open JSON file.

…timing-7ce86f

# Conflicts:
#	runtime/shims/d3d12_com.cpp
#	runtime/shims/d3d12_vulkan_device.cpp
@MrCheese5

Copy link
Copy Markdown
Contributor Author

Thanks @quackottu for the RTX 5070 pass. Merged main 8df277f in, now b74c7f2. The only overlap was main's new d3d12_trace wrapper around CreateCommandQueue, which now passes the queue clock through. Re-tested on the RTX 4090 (621.14): ctest 32/32. d3d12_fence_waiter_test and d3d12_fence_timing_test pass with LEONIDA_VK_VALIDATION=1, and the timing test also passes without it.

Note: d3d12_vulkan_device_test now exits 1 under validation because of 10 synchronization WRITE_AFTER_WRITE hazards between back-to-back vkCmdCopyImage / vkCmdCopyBufferToImage calls. Plain main 8df277f, built on its own, gives the same exit and the same 10 messages, so this PR didn't introduce them. All of that test's comparisons still match the host. AI-assisted.

…timing-7ce86f

# Conflicts:
#	runtime/shims/d3d12_com.cpp
#	runtime/shims/d3d12_vulkan_device.cpp
@MrCheese5

Copy link
Copy Markdown
Contributor Author

Merged main 4a36bb1 in, now eb2928a: GetClockCalibration sits next to #278's GetTimestampFrequency and shares its context_. RTX 4090: ctest 33/33; the fence tests pass with LEONIDA_VK_VALIDATION=1. AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit e28b1d6 into ProjectLeonida:main Nov 22, 2026
10 checks passed
@JavierBlanco23

Copy link
Copy Markdown
Contributor

Tested #423 at eb2928a on Windows 11, RTX 5080 (Blackwell), driver 616.56, Vulkan 1.4.x, MSVC 19.44.

ctest: 33/33 PASS with LEONIDA_VK_VALIDATION=1. No failures.

D3D-30 (fences and timing: SetEventOnMultipleFenceCompletion, GetClockCalibration, GetAdapterLuid, GetCustomHeapProperties) checks pass on this adapter. No validation errors from this PR.

AI-assisted.

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