Skip to content

d3d12: CreateRootSignature returns the live object for bytes it already has - #8830

Open
Maraco91 wants to merge 2 commits into
ItsNotRexed:mainfrom
Maraco91:fix/root-signature-object-identity
Open

Maraco91 wants to merge 2 commits into
ItsNotRexed:mainfrom
Maraco91:fix/root-signature-object-identity

Conversation

@Maraco91

@Maraco91 Maraco91 commented Nov 23, 2026 •

Copy link
Copy Markdown

Claim: #8504 (comment)

@ItsNotRexed: an exe PR, as you asked.

What

ID3D12Device::CreateRootSignature now returns the live root signature object for bytes it already made one from, with one more reference, as the PC runtime does.

Before, every call made a new object. SetGraphicsRootSignature / SetComputeRootSignature keep the bound root arguments only when the same object is set again (bind_root in d3d12_com.cpp, unchanged here); a different object resets them to zero. So a title that creates a signature twice from the same bytes and switches between the two handles kept its arguments on the PC runtime, where both handles are one object, and lost them here.

The device keeps a map from a signature's content (the SHA-256 the object already carries for the PSO cache) to the live object:

  • the map owns nothing: a signature has its own reference count, and its last Release takes its entry out of the map under the map's lock, then deletes the object after the lock is dropped;
  • the caller's reference is taken under that same lock, so a signature the lookup finds is alive and stays alive while the reference is added. (This is the second commit, after review: the first version removed the entry in the destructor, which let the lookup call into an object already being destroyed.)
  • the map is shared between the device and its signatures, so a signature that outlives the device still finds it;
  • the device gets one reference per live object, as on the host (+1 for new bytes, +0 for bytes it already has).

The Xbox proxy forwards device slot 16 to this same function and does not wrap root signatures, so the console path gets the same behaviour (d3d12_compute_test runs through the proxy).

Why

Measured on this machine, the same on WARP and the NVIDIA device, no debug layer:

host D3D12 main
second create, same buffer the same pointer; count 1 → 3 with the third a new object
equal bytes in another buffer the same pointer a new object
same description serialized as 1.0 vs 1.1 different objects different objects
device references added by five creates (three alike) 3 5
private data set through one handle, read through another found not found
releases of the three handles 2, 1, 0; a create after that gives a fresh object with count 1 and no private data each handle its own object

Reach: a run reported in #8186 counts 1,250 root signature creates by the title, one per program (in that run 1,204 of them stopped in the title's own code before reaching the device), so creates from repeated bytes are plausible; I have not measured how many repeat. Two more things are not known: whether the console's D3D12 runtime deduplicates the same way, and whether the title relies on arguments surviving such a switch. If the console does not deduplicate, the title already re-sets its arguments after every switch and this change is invisible to it; if it does, the layer dropped arguments the title expected to keep. Either way the change follows the PC runtime and cannot take arguments away.

Not D3D-879 (the root argument spill, #8278 / #7318).

Tested

Windows 11 (10.0.26200), MSVC 19.42 (VS 2022 Build Tools), RelWithDebInfo, NVIDIA RTX 3080 Ti, driver 616.56, Vulkan SDK 1.4.363.0. No game files.

cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON
cmake --build runtime/build --target d3d12_compute_test
runtime/build/d3d12_compute_test.exe
  • Before (the new test on main@867d922f0's d3d12_vulkan_device.cpp): exit 1.
    root signature identity: got 1 0 0 1 0 0 5 0 0 0 0 1 0, want 1 1 1 3 0 0 3 1 2 1 0 1 0
    line 474: layer == expected
    
    With that check bypassed locally, the dispatch after the rebind also fails on main (element 0: got 6007, 6100 and 0, want 6007, 6100 and 6000): the arguments were zeroed.
  • After: exit 0, root signature identity: 2 of 2 host adapter(s) gave the same 13 answers, and the three dispatches match; 30 of 30 runs in a loop on the current head.
  • What the test does: the 13 answers (pointer identity, counts, device references, private data, the releases, a create after the last release) are taken from the layer and compared with the values measured above; the same calls are also made on the host's own d3d12.dll (default adapter and WARP). A host answer that differs is printed, not failed, the way d3d12_close_parity_test treats WARP, so a d3d12.dll that behaves differently does not break the test; under Wine the host part is skipped (vkd3d makes an object per call).
  • Negative control: the existing "a different root signature clears the arguments" case used two signatures made from one blob, which on the PC runtime are one object. It now uses the same description serialized as 1.1 (different bytes, same layout), asserts the two objects differ, and still expects the switch to zero the arguments. The rebind that must keep them goes through a second create of the first blob's bytes from another buffer.
  • Lifetime: 4 threads each create and release the same bytes 20,000 times and call GetDevice on what they get; no failure, and the bytes then make a fresh object with one reference.
  • Still passing: d3d12_bundle_test (14 scenarios, 25 host comparisons), d3d12_multithread_test, d3d12_pipeline_library_test, d3d12_descriptor_test, d3d12_set0_compute_test, d3d12_draw_test, d3d12_com_test, d3d12_private_data_test, xs_proxy_mock_test, device_proxy_test.
  • python -I scripts/check_no_game_data.py on the two changed files: exit 0.

Not tested, and limits:

  • AMD or Intel GPUs, Linux/Wine, Windows 10's d3d12.dll, and no boot of the title.
  • The stress loop passing does not prove the locking; it was not run under a thread sanitizer. Every Release of a root signature now takes the device's root-signature mutex for the decrement (AddRef stays lock-free).
  • One existing case changes meaning: d3d12_bundle_test's "release right after recording" now receives a signature that something else still holds, so it no longer destroys the object there. It passes.
  • d3d12_vulkan_device_test fails on main and with this PR at the same line (tiles == 1, d3d12_vulkan_device: check PC CreateReservedResource2 behind the Xbox proxy, not through slot 78 #7125), so it says nothing here.
  • A repeated create still parses the blob and registers its static samplers before the lookup, as every create did.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…dy has

The PC runtime keeps one root signature object per serialized signature:
a second CreateRootSignature from the same bytes, from the same buffer
or another one, returns the same pointer with one more reference, and
the last Release destroys it (WARP and an NVIDIA device). The layer made
a new object per call, and SetGraphics/ComputeRootSignature with a
different object zeroes the root arguments, so switching between two
handles made from the same bytes lost arguments the PC runtime keeps.

The device now maps a signature's content hash to the live object. The
map owns nothing; the caller's reference is taken under the lock a dying
signature needs to leave the map, so an entry past its last Release is
never handed out and is replaced instead.

d3d12_compute_test checks identity, counts, device references, private
data and release order against the measured values and prints the
host's own answers, runs four threads creating and releasing the same
bytes, and uses a really different signature (the 1.1 serialization)
for the case where a switch must clear the arguments.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@thehollowedone

Copy link
Copy Markdown

I reviewed the final-release path at eef1f893. I think the cache needs one more lifetime guarantee before merging.

The inherited Release can start destruction as soon as the count reaches zero. The cache lock in ~RootSignature is acquired inside the destructor body, so it serializes the erase with lookup while leaving the start of destruction outside that protection. The AddRef() > 1 check checks the count after calling through the cached pointer. The pointer comparison during erase protects a replacement entry, but lookup still needs to acquire its reference while the object is live. C++ object lifetime

I’d serialize the final reference transition and cache removal with lookup, and release the lock before deleting the object. Keeping hand_out outside the lock also matters because its temporary reference can be the last one. Static review only. AI-assisted.

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Verified head eef1f893f merged onto main (867d922f0) on native Windows 11 (10.0.26300.9974, MSVC 19.44, RelWithDebInfo, Vulkan SDK 1.4.363.0):

  • python -I scripts/check_no_game_data.py on both changed files: clean (exit 0)
  • cmake --build runtime/build --target d3d12_compute_test --parallel: built cleanly
  • LEONIDA_VK_VALIDATION=1 .\runtime\build\d3d12_compute_test.exe on NVIDIA GeForce RTX 5060 Ti (driver 610.88, Vulkan 1.4.341): passed with 0 validation errors (root signature identity: 2 of 2 host adapter(s) gave the same 13 answers; cached PSO: 2 host adapter(s) gave the same 11 answers; compute dispatches matched)

AI-assisted.

@ethandepstein-del

Copy link
Copy Markdown

Independent Windows + RTX 5080 GPU verification of eef1f893f (merge-base 867d922f0) on NVIDIA GeForce RTX 5080 (Blackwell, desktop, NVIDIA driver 617.42, Vulkan API 1.4.351, Vulkan SDK 1.4.363.0, MSVC 19.44.35230.0, Windows 11, Ryzen 7 9800X3D, DISABLE_RTSS_LAYER=1, DISABLE_VULKAN_OBS_CAPTURE=1, no game files):

Build & Tests

  • scripts/build_runtime.ps1 (CMake + Ninja, RelWithDebInfo, -DLEONIDA_REQUIRE_VULKAN=ON): OK (git diff --check clean).
  • ctest -R "d3d12_compute|d3d12_set0_compute|d3d12_m4_compute|root_sig": 6/6 PASSED (1.52s — root_sig, root_signature, root_signature_oracle, d3d12_m4_compute, d3d12_compute, d3d12_set0_compute).
  • Direct d3d12_compute_test.exe: PASS
    • root signature identity: 2 of 2 host adapter(s) gave the same 13 answers (RTX 5080 + WARP)
    • cached PSO: 2 host adapter(s) gave the same 11 answers
    • CBV / SRV / compute root-constant + UAV + descriptor table dispatches all matched (64/64, 32/32, etc.).

Code check

  • Concurrency and lifetime handling in RootSignatures looks solid: shared ownership via std::shared_ptr keeps the table alive through ~RootSignature(), live->AddRef() > 1 under the mutex avoids resurrecting an object already racing into its destructor, ~RootSignature() erases only when it->second == this, and the lock is released before hand_out(). No issues found.

(AI-assisted)

Review of the first commit: the lookup in CreateRootSignature took its
reference through a cached pointer whose object could already be in its
destructor, since Object::Release starts the delete as soon as the count
reaches zero and the map entry was only removed inside the destructor.

RootSignature now has its own count. Release decrements it under the
map's lock and, at zero, removes the entry in the same critical section;
the object is deleted after the lock is dropped. A signature the lookup
finds is therefore alive and stays alive while the lookup adds its
reference, and the "AddRef returned 1" case is gone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Maraco91

Copy link
Copy Markdown
Author

Thanks @thehollowedone, you are right, and it is fixed in f135530d9.

The first commit took the map's lock only inside ~RootSignature, so between the count reaching zero in the inherited Release and the destructor getting that lock, CreateRootSignature could call AddRef through the cached pointer on an object whose destruction had begun. I had argued the AddRef() > 1 check made that harmless; it still called into an object past the end of its lifetime, which is not something to rely on.

What changed, as you suggested:

  • RootSignature has its own count. Release decrements it under the map's lock, and at zero removes the entry in the same critical section. So the step from one reference to none and leaving the map are one step for the lookup.
  • The object is deleted after the lock is dropped (its destructor releases the device).
  • The lookup now takes a plain AddRef under that lock: an entry it finds is alive and cannot start dying until the lock is released. The "AddRef returned 1, replace the entry" branch is gone.
  • hand_out stays outside the lock, since its Release can be the last and now takes the lock itself.

Cost: every Release of a root signature takes the device's root-signature mutex for a decrement; AddRef stays lock-free.

Re-run on the same machine (Windows 11, MSVC 19.42, RTX 3080 Ti 616.56): d3d12_compute_test exit 0, 30 of 30 runs in a loop (each run includes the four-thread create/release stress), root signature identity: 2 of 2 host adapter(s) gave the same 13 answers; d3d12_bundle_test, d3d12_multithread_test, d3d12_pipeline_library_test, d3d12_descriptor_test, d3d12_set0_compute_test, d3d12_draw_test, d3d12_com_test, d3d12_private_data_test, xs_proxy_mock_test and device_proxy_test still pass. Not run under a thread sanitizer.

@smowlyy, your run was on eef1f893f; the test file is unchanged in this commit, only the lifetime in d3d12_vulkan_device.cpp.

AI-assisted (Claude Code).

@marso006

Copy link
Copy Markdown

Tested eef1f893f (based on main 867d922f0) on Windows 11, NVIDIA GeForce RTX 5060 Ti (Blackwell), driver 617.42, VS 2026 (scripts/build_runtime.ps1, RelWithDebInfo), Vulkan SDK 1.4.363.0: pass. Synthetic, no game files.

Independent check, claim. AI-assisted (Claude Code).

@thehollowedone

Copy link
Copy Markdown

Thanks for the update. I rechecked f135530d. Lookup takes its reference under the same lock that covers the last decrement and cache removal. Deletion and hand_out both run outside that lock. That addresses the lifetime concern I raised. Final release · Cache lookup

On Windows 11, RTX 3080 Ti (621.35), MSVC 19.44, and Vulkan SDK 1.4.363, a focused d3d12_compute run matched all 13 root-signature answers against both the default native adapter and Microsoft WARP. All 64 elements matched in each of the three dispatches, including the equal-signature rebind that preserves the arguments and the different-signature rebind that resets them. The shader passed spirv-val --target-env vulkan1.3, with zero test skips or Vulkan core or synchronization validation messages.

The temporary fixture covered these identity and rebind cases. Concurrency was reviewed statically. Synthetic inputs only. 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.

5 participants