Skip to content

d3d12: order timestamp resolves after their writes on MoltenVK - #1947

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Jolymmiles:mac/d3d12-timestamp-order-moltenvk
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Jolymmiles:mac/d3d12-timestamp-order-moltenvk

Conversation

@Jolymmiles

@Jolymmiles Jolymmiles commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: item 2 of #1745 (comment) (d3d12_vulkan_device timestamp check on a Mac under Wine)

What and why

On a Mac, d3d12_vulkan_device failed at its timestamp check (t0 != 0 && t0 != 0xcdcd… && t1 >= t0, now around line 4529): t0 was right and t1 was 0. The cause is a MoltenVK bug, reproduced natively without Wine or this runtime.

What MoltenVK does wrong. On Apple GPUs counters can only be sampled at stage boundaries. MoltenVK defers each vkCmdWriteTimestamp to the end of the current Metal encoder and samples it in a separate pass. vkCmdCopyQueryPoolResults then reads the counters with a blit that nothing orders after that pass. So the copy sees 0 for a query never written before, or the previous run's value otherwise. WAIT_BIT should make the copy wait for availability and no barrier is required, so our recording is valid Vulkan.

Native reproduction: vkCmdWriteTimestamp, then vkCmdCopyBuffer, then vkCmdWriteTimestamp, then vkCmdCopyQueryPoolResults(64_BIT|WAIT_BIT), compared with vkGetQueryPoolResults. Apple M3 Max, 200 iterations each:

ordering before the copy MoltenVK 1.4.2 / 1.4.3 (main 5189f8de) KosmicKrisp, llvmpipe
none (what the D3D12 layer records) 0/200 right 200/200
full pipeline barrier 9/200
next command buffer of the same submit 3/200
next vkQueueSubmit, no wait 13/200
vkCmdSetEvent + vkCmdWaitEvents on a fresh event 200/200
next submit waiting on a timeline semaphore 200/200 (500/500)

I re-ran rows 1, 5 and 6 myself through the loader with Homebrew's MoltenVK 1.4.2 ICD and with a MoltenVK main build: 0/200, 200/200, 200/200. KosmicKrisp gave 200/200 on row 1.

The workaround is active only where driverID == VK_DRIVER_ID_MOLTENVK; Wine reports the host driver, and the runtime logs it once. Vulkan has no feature or property for this, so the driver ID is the only possible gate.

  • Before each timestamp resolve that copies from a pool, the queue records vkCmdSetEvent + vkCmdWaitEvents on an event of its own. MoltenVK decides at submit time, from the event's host state, whether the set or wait does anything, so events aren't reused within a submission. Events are reset on the host and returned to a free list once their submission completes.
  • A submission with such a resolve also waits on the queue's timeline semaphore at the previous submission's value, for timestamps written in an earlier ExecuteCommandLists.
  • The values stay real GPU timestamps; nothing is emulated. docs/d3d12_vulkan.md section 4.6 describes this.
  • The test keeps its D3D12 checks and adds one: a timestamp written in one ExecuteCommandLists and resolved in the next, without a host wait.

Reported upstream as KhronosGroup/MoltenVK#2918 (repro source, the table, and pointers to MVKCommandBuffer.mm / MVKQueryPool.mm on 5189f8de).

Tested

Apple M3 Max, macOS 27.3, Wine 11.0 (CrossOver 26.3's loader) with MoltenVK 1.4.3. The Windows build is cross-compiled with clang-cl 23.1.6, lld-link 22.1.8 and the xwin 10.0.26100 SDK.

  • d3d12_vulkan_device under Wine: before, FAIL in every run. After, PASS 10/10 in the delegate's runs and 3/3 in mine, and the log says timestamp resolves wait for the timestamps before them (MoltenVK).
  • Full run_wine_tests.py list (110 tests, same commit 6a6704d): before 81 passed / 6 failed / 23 skipped, after 82 / 5 / 23. Only d3d12_vulkan_device changed status. The remaining failures are separate items: d3d12_descriptor is SHADER-268 (Apply the S#'s LOD bias in the shader where samplers take none (SHADER-268) #1845), d3d12_present is d3d12_present_test: check the presented images through the capture, GDI screen read only where it works #1908, and d3d12_depth_resolve is in progress. d3d12_compute and d3d12_draw also fail and are not yet looked at; d3d12_compute passed in one earlier run, so it is flaky.
  • Native ctest (runtime/build/mac): the same 35/36 before and after. xdxr_build_check fails on plain main too.
  • Native Windows (RTX 3050 Laptop, driver 32.0.16.2101, MSVC): d3d12_vulkan_device passes in a reviewer's run, see the comment below.
  • Not run: Linux Wine/llvmpipe. The workaround is off on every non-MoltenVK driver, so nothing changes there. The validation layer can't load into the x86_64 Wine process, and this test is Windows-only.

No game files. AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator change.)

🤖 Generated with Claude Code

@ezwinxdlmao

Copy link
Copy Markdown
Contributor

Tested 9f8241d on an NVIDIA GeForce RTX 3050 Laptop GPU, driver 32.0.16.2101, Windows 11, native MSVC (VS 2022 Build Tools, Ninja, RelWithDebInfo), with llvm-mc 21.1.8 for the translated-shader tests: pass.

  • ctest -R d3d12_vulkan_device: pass (timestamp resolves ordered after their writes).
  • Full ctest -j3: 115/116. The one failure is d3d12_draw (bundle blend pixel 127,0,127,255; TEST-266, fixed by Tests: wait for the bundle blend draws in d3d12_draw (fence 31, not 21) #1995), which fails identically on unmodified main 4eb6155d (this PR's base) on the same GPU. No build warnings in the files this PR touches.

AI-assisted (Claude Code).

On Apple GPUs MoltenVK samples a vkCmdWriteTimestamp in a blit pass of its own at the end of the current Metal
encoder. Nothing orders the vkCmdCopyQueryPoolResults resolve after that pass. The copy read the old value, or 0 for
a timestamp never written, so d3d12_vulkan_device failed under Wine on a Mac (t1 == 0). A minimal Vulkan program
reproduces it natively on MoltenVK 1.4.3 and 1.4.2: the copy was right 0 to 43 times in 200, in the same command
buffer, the next one or the next submit. KosmicKrisp and llvmpipe were right in every case.

Where driverID is MoltenVK, the queue sets and waits for a fresh VkEvent before each timestamp resolve that copies.
A submission with such a resolve also waits on the queue's timeline for the submission before it. Both measured
200/200 and 500/500. The timestamps are still the GPU's own. The events go back to a pool after a host reset once
their submission completes. d3d12_vulkan_device also checks a resolve in the execution after the write. Documented
in docs/d3d12_vulkan.md section 4.6.
@Jolymmiles

Copy link
Copy Markdown
Contributor Author

Filed upstream: KhronosGroup/MoltenVK#2918.

@FaKL-Code

Copy link
Copy Markdown
Contributor

Tested 6670b2b (merged onto main 4947579) on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: pass

  • Clean configure + build of runtime/ (Visual Studio generator, RelWithDebInfo): no new warnings (same C4201/C4456/C4996 counts as main).
  • ctest -C RelWithDebInfo -j 4: 116/116 passed (loader run on its own; 7 skipped for missing inputs or optional components, gameinput disabled, as on main).
  • command_list, d3d12_com, d3d12_vulkan_device, d3d12_fence_timing, d3d12_descriptor, d3d12_compute and d3d12_state_map also pass with LEONIDA_VK_VALIDATION=1.
  • The new ordering is for MoltenVK; on this host the run only shows that the Windows/NVIDIA path does not regress.

AI-assisted (Claude).

@ItsNotRexed
ItsNotRexed merged commit db317bd into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
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.

4 participants