Skip to content

d3d12: fence event handles and invalid descriptor heap types answer as the host does (D3D round 4a) - #4609

Open
kapitche wants to merge 2 commits into
ItsNotRexed:mainfrom
kapitche:kap-d3d4-a
Open

kapitche wants to merge 2 commits into
ItsNotRexed:mainfrom
kapitche:kap-d3d4-a

Conversation

@kapitche

Copy link
Copy Markdown
Contributor

Claimed in #4421 (D3D round 4a): #4421 (comment)

What broke: ID3D12Fence::SetEventOnCompletion and ID3D12Device1::SetEventOnMultipleFenceCompletion never looked at the event handle. Repro: fence->SetEventOnCompletion(1, CreateSemaphoreW(...)), a closed event, GetCurrentProcess() or 0x1234 all returned S_OK and the wait was then never signalled. The host runtime (RTX 4080 SUPER, Intel UHD 770, WARP, all the same) answers E_HANDLE (0x80070006) for a handle that is not an event, before it reads the fence value, and E_ACCESSDENIED (0x80070005) for an event opened without EVENT_MODIFY_STATE. Also GetDescriptorHandleIncrementSize of a heap type past DSV returned 32; the host returns 0.

Fix: check_event_handle in d3d12_com.cpp asks NtQueryObject for the handle's type and granted access (DuplicateHandle cannot be used for the access check, it grants any access) and both wait calls use it; the increment size is 0 for a type outside the enum. The null event keeps its blocking meaning. Microsoft's pages document neither error, so only the host-measured answers are implemented.

Test: new d3d12_object_methods_test (ctest d3d12_object_methods): 74 rows over command queue, fence, resource, descriptor heap, query heap, allocator, list and the device calls without a description, run on the layer and on the hardware and WARP host devices; a row counts only where both hosts agree. Rows the host faults on (a null fence or output) are left out, and so are a CPU or queue Signal below the fence's value (the host lowers the fence; a Vulkan timeline cannot, see the Fence comment), GetGPUVirtualAddress and the shader-visible GPU descriptor start (this test's Vulkan device has no address arena) and ID3D12Resource1 (#2590, #3374).

Tested: Windows 11, MSVC 14.51, worktree built from this commit (only the needed targets). d3d12_object_methods, d3d12_com, d3d12_fence_timing, d3d12_fence_events, d3d12_fence_waiter, d3d12_queue_wait, d3d12_queue_wait_timeout, d3d12_multithread and d3d12_vulkan_device pass on the RTX 4080 SUPER and on the Intel UHD 770 (VK_LOADER_DRIVERS_DISABLE=*nv*; d3d12_queue_wait is skipped on Intel by its own rule). d3d12_queue_wait_timeout timed out once on the RTX during a run while a second build was going and passed on the rerun. Negative control: with d3d12_com.cpp and d3d12_vulkan_device.cpp from upstream/main the test fails with 15 rows differing on both GPUs (for example fence: SetEventOnCompletion invalid handle: layer 0, host 80070006).

Touches one region of each shim and one block in runtime/CMakeLists.txt (before "Fences and timing"); the other two round 4 pull requests add their blocks elsewhere in that file.

AI-assisted.

…ypes answer as the host does (D3D round 4a)

SetEventOnCompletion and SetEventOnMultipleFenceCompletion take the event handle before the fence value: a closed, pseudo or non-event handle is E_HANDLE and an event without EVENT_MODIFY_STATE is E_ACCESSDENIED on the RTX 4080 SUPER, the UHD 770 and WARP; the layer answered S_OK. GetDescriptorHandleIncrementSize of a heap type past DSV is 0 on the host, 32 here. d3d12_object_methods compares 74 queue, fence, resource, descriptor heap, allocator, list and device rows with both host adapters.

@slopradish slopradish left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified locally: built d3d12_object_methods_test from PR #4609's head with the local Vulkan headers, then ran it on this host (RTX 3090, native D3D12 + WARP comparison):

object methods: 74 rows compared with 2 host adapters, 0 differ

The change is sound: NtQueryObject is the right way to check both the handle type and granted access — DuplicateHandle cannot do the access check because it grants whatever access you ask for. Checking before reading the fence value matches host ordering, and keeping the null event's blocking meaning preserves existing behaviour. The descriptor-heap increment returning 0 for out-of-range types is a clean guard.

The test compares against two real host adapters and only counts rows where they agree, which avoids encoding a driver quirk as the expected answer. The two undocumented error codes are explicitly attributed to host measurement rather than claimed from spec.

No game data or keys involved.

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

Built and ran d3d12_object_methods_test at 51e82d26 on Windows 11 25H2 (build 26200), RTX 5070 Ti, driver 621.24, non-elevated with Developer Mode off: exit 1, object methods: 74 rows compared with 2 host adapters, 8 differ. The difference is not in the fence fixes — it is the test's SetStablePowerState rows poisoning the shared host device.

What the run shows (LEONIDA_PROBE=1):

device: SetStablePowerState FALSE | layer 0 | host 80004005 | host 80004005  <-- DIFF
device: SetStablePowerState TRUE  | layer 0 | host 80004005 | host 80004005  <-- DIFF
device1: SetEventOnMultipleFenceCompletion all, already complete | layer 1 | host ffffffffffffffff | ...  <-- DIFF
device1: ... any, one pending            | layer f4242 | host ffffffffffffffff | ...  <-- DIFF
device1: ... all, one pending            | layer f4242 | host ffffffffffffffff | ...  <-- DIFF
device1: ... zero fences                 | layer 80070057 | host 0 | ...  <-- DIFF
device1: ... invalid flags               | layer 80070057 | host ffffffffffffffff | ...  <-- DIFF
device1: ... invalid event handle        | layer 80070006 | host ffffffffffffffff | ...  <-- DIFF

I reproduced the cause with a standalone native-D3D12 probe (no repo code): on a fresh device CreateFence is S_OK; after SetStablePowerState(FALSE) returns 80004005 (no Developer Mode), the next CreateFence returns 887a0005 (DXGI_ERROR_DEVICE_REMOVED), and it stays removed. The test runs the two SetStablePowerState cases before the fence cases on the same host device, so every later fence case hits its -1 early-return sentinel on both host adapters; hosts_agree is true and the sentinel is compared as if it were a host answer. On a fresh device the same native calls return exactly the layer's answers (80070057, 80070057, 80070006), so the fence/descriptor fixes themselves look right.

Two things to fix before this can merge:

  1. SetStablePowerState in d3d12_vulkan_device.cpp:4490 returns S_OK unconditionally; the host answers E_FAIL when Developer Mode/the privilege is absent. Either implement that check or document and skip the row when the host fails.
  2. The test must not let the SetStablePowerState cases remove the shared host device for later rows: run them last, give them a throwaway device, or detect a removed host device (a CreateFence probe) and mark the remaining rows host-unavailable. Treat the -1/-2/-3 setup sentinels as "row not comparable", not as a host answer.

Everything else in the run matched (GetDescriptorHandleIncrementSize out-of-range → 0, the event-handle errors, resource/queue/descriptor rows). Happy to re-run once the rows are isolated.

…ces and is compared only where the host accepts it
@kapitche

Copy link
Copy Markdown
Contributor Author

Thanks, the diagnosis is right: the two SetStablePowerState rows ran on the shared host device, and a refused call leaves it removed, so every later row on that device was the setup sentinel on both adapters.

Pushed ffa8037 (test only). SetStablePowerState is no longer a table row. It runs after the table on a fresh device per host adapter, and the row is compared only when every host accepts the call (Developer Mode on); otherwise the test prints that the row was not compared and leaves it out. A host row with a negative setup sentinel (-1/-2/-3) is now "not comparable" instead of being compared as a host answer. The layer still answers S_OK: where the host accepts, that matches, and the refusal depends on the machine's privilege, which the layer cannot see.

I cannot reproduce the refusal here: Developer Mode is on on this machine (AllowDevelopmentWithoutDevLicense = 1), so the old test also passes here and I could not switch it off. What I ran, Windows 11, MSVC 14.51, only d3d12_object_methods_test: new test on the RTX 4080 SUPER 71 rows compared with 2 host adapters, 0 differ, on the Intel UHD 770 70 rows ... 0 differ. Control for the new row: with the layer's SetStablePowerState returning E_FAIL, the test fails (layer refuses, the hosts accept, 1 differ). A re-run on your machine, with Developer Mode off, would confirm the isolation.

@justr4mzi

Copy link
Copy Markdown
Contributor

Tested ffa80371 on Windows 11 (Developer Mode off — HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock absent) with AMD Radeon RX 6700 XT (+ WARP host reference).

1. SetStablePowerState isolation confirmed (ffa80371)

  • Build (d3d12_object_methods_test, d3d12_fence_timing_test, [13/13]): exit 0.
  • Standard ctest -R "^(d3d12_object_methods|d3d12_fence_timing)$": 2/2 passed (d3d12_object_methods 1.31 sec, d3d12_fence_timing 1.72 sec).
  • Output: SetStablePowerState: a host refuses it (no Developer Mode), row not compared and object methods: 70 rows compared with 2 host adapters, 0 differ.

2. Failure under LEONIDA_VK_VALIDATION=1 (VUID-VkSemaphoreSignalInfo-value-03260)

  • Under LEONIDA_VK_VALIDATION=1, d3d12_fence_timing passed (1.32 sec), but d3d12_object_methods failed (0.94 sec, direct run exits 1 with 1 validation errors, despite 70 rows compared with 2 host adapters, 0 differ):
validation: vkSignalSemaphore(): value of signal is 18446744073709551615, the current semaphore payload is 0, and the difference between them is 18446744073709551615, which is greater than maxTimelineSemaphoreValueDifference (4294967295).
The Vulkan spec states: value must have a value which does not differ from the current value of the semaphore or the value of any outstanding semaphore wait or signal operation on semaphore by more than maxTimelineSemaphoreValueDifference (https://docs.vulkan.org/spec/latest/chapters/synchronization.html#VUID-VkSemaphoreSignalInfo-value-03260)
  • This is triggered by "fence: Signal max value" (runtime/test/d3d12_object_methods_test.cpp:154-158: CreateFence(0) followed by Signal(UINT64_MAX)) calling VulkanFence::signal (runtime/src/vulkan_backend.h:1407-1412), because AMD RX 6700 XT exposes maxTimelineSemaphoreValueDifference = 4294967295 (0xFFFFFFFF) rather than UINT64_MAX.

Hygiene checks

  • tests/test_ctest_registration.py: 4 tests OK (0.425s).
  • scripts/check_no_game_data.py and git diff --check: exited 0.

AI-assisted (Gemini, Antigravity).

@Atomic258

Copy link
Copy Markdown

Tested head ffa80371 — Windows 11, VS 2022 BuildTools + Ninja, NVIDIA RTX 5090 (driver 617.42), no game files: pass. Adds NVIDIA coverage to the existing AMD RX 6700 XT run.

  • cmake --build runtime/build: clean.
  • ctest -R "^(d3d12_object_methods|d3d12_fence_timing)$" --output-on-failure: 2/2 Passed (d3d12_object_methods 1.14s, d3d12_fence_timing 1.81s). On the 5090 SetStablePowerState is a real driver entry point, so the fresh-device isolation at ffa80371 is exercised through the accepted path here, not the refused one.
  • git diff --check and scripts/check_no_game_data.py clean.

AI-assisted (Devin).

@Atomic258

Atomic258 commented Nov 23, 2026 •

Copy link
Copy Markdown

Corroborating data point on the SetStablePowerState row (@Kam471's report): my earlier pass on this PR's head was on a machine with Developer Mode off (no HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock\AllowDevelopmentWithoutDevLicense key) — RTX 5090, driver 617.42, Windows 11. d3d12_object_methods still passed 1/1 including the isolated SetStablePowerState row and every later fence row, so the native call succeeding without Developer Mode is driver/version-dependent (5070 Ti on 621.24 fails and removes the device; 5090 on 617.42 succeeds). Agree the row needs to tolerate both host answers — the layer returning S_OK unconditionally is a separate question.

AI-assisted (Devin).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested ffa8037 on Intel UHD 770 (driver 32.0.101.7088), Windows 11, MSVC 19.44, Vulkan SDK 1.4.357: pass. ctest -R d3d12_object_methods passes with LEONIDA_VK_VALIDATION=1; the test reports 69 rows compared with 2 host adapters, 0 differ. One row is skipped here because a host refuses it without Windows Developer Mode, which may be why this is 69 rather than the 74 in the description. AI-assisted.

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Tested ffa80371 on Windows 11 (26300), RTX 5060 Ti / 621.14, MSVC 14.51, Vulkan SDK 1.4.363.0, RelWithDebInfo, clean build with -DLEONIDA_LLVM_MC. Full LEONIDA_VK_VALIDATION=1 ctest -j4: 145/146. d3d12_object_methods fails every time (3/3), even though every row agrees with the hosts:

validation: vkSignalSemaphore(): value of signal is 18446744073709551615, the current semaphore payload is 0, and the difference between them is 18446744073709551615, which is greater than maxTimelineSemaphoreValueDifference (2147483647).
  (VUID-VkSemaphoreSignalInfo-value-03260)
1 validation errors
object methods: 70 rows compared with 2 host adapters, 0 differ

The row is fence: Signal max value (CreateFence(0) then f->Signal(UINT64_MAX)). The layer passes the value straight to vkSignalSemaphore, and Vulkan forbids a timeline jump larger than maxTimelineSemaphoreValueDifference. NVIDIA reports 2³¹−1 there (vulkaninfo on this card), while drivers that report a larger limit (the Intel UHD 770 run above) don't trip it. The RTX 5090 pass above ran without LEONIDA_VK_VALIDATION, so it didn't see this.

So it isn't the event-handle change. It's a gap in the layer's Fence::Signal that this new row is the first test to reach. The returned value already matches the host (GetCompletedValue() == UINT64_MAX), but the call underneath is invalid on drivers with the 2³¹−1 limit. A few options:

  • the layer handles a CPU signal past current + maxTimelineSemaphoreValueDifference itself: for example, it records the D3D12 value on the CPU side, so GetCompletedValue and event waits see it, and signals the semaphore only as far as the limit allows (it is already ≥ any value a GPU wait can be waiting for in practice);
  • or the row stays and that fix is tracked as a separate item, so the row isn't left failing under validation on NVIDIA in the meantime.

Happy to measure a fix on this card. AI-assisted (Claude Code). No game files.

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