Skip to content

d3d12: a static sampler is also keyed by the S# the console's compiler writes (it isn't bit-equal to the layer's) - #4765

Open
rafaelpssz wants to merge 2 commits into
ItsNotRexed:mainfrom
rafaelpssz:fix/static-sampler-console-key
Open

rafaelpssz wants to merge 2 commits into
ItsNotRexed:mainfrom
rafaelpssz:fix/static-sampler-console-key

Conversation

@rafaelpssz

@rafaelpssz rafaelpssz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

New D3D item (claim).

The problem

A root signature's static sampler takes no root argument: the shader holds its S# by value, and the layer finds the Vulkan sampler by those four dwords in the hash table. CreateRootSignature registered the S# under the layer's own encoding of the description (sampler_descriptor + gfx10::encode). The shader holds what the console's compiler baked in, which isn't bit-equal. So the lookup misses, and the shader samples through the fallback sampler.

Measured (my 1.0.2.1 copy, counts and field names only)

None of the game's 26,503 containers carries an RTS0 (docs/d3d12_vulkan.md already says so). The one place where a static sampler and the literal S# that uses it can be compared directly is amd_fidelityfx_upscaler_xbox.dll. Its only sampling program builds its S# from immediates: all 10 sampling instructions use one literal, and its PSV0 sampler binding is space 0, register 0, which is the root signature's single static sampler. Every field that affects sampling matches. The words differ in four places:

field console compiler encode()
DEPTH_COMPARE_FUNC ComparisonFunc − 1 = 3, though the filter doesn't compare 0
MAX_LOD (MaxLOD = FLT_MAX) 0xFFF (saturated) 0xF00 (clamped to 15.0)
MIP_POINT_PRECLAMP (WORD2 bit 28) 1 0
BORDER_COLOR_TYPE (no border address mode) 0 2 (opaque white)

The game's own literals follow the same conventions. All 27 distinct literal S# values (174,796 sampling instructions, #4268's dataflow) have the pre-clamp bit set. A non-zero border type appears only together with the border address mode.

On the compare field: in the game, all 15,155 sampling instructions whose literal S# has a non-zero DEPTH_COMPARE_FUNC are _c comparison ops, and none is a plain sample. The FidelityFX program is the exception: a copied compare function on a plain image_sample_lz.

The change

  • register_static_samplers also binds that console form, through a new bind_sampler_keyed(key_words, words). The key is the console form; the Vulkan sampler is still made from the layer's encoding. This means a plain sample never gets a comparing sampler from the copied compare function. Decoding the console words directly would set compareEnable for a plain filter, which is undefined in Vulkan.
  • It is additive: heap samplers (CreateSampler/CreateSampler2) and the existing static-sampler key are unchanged. When the two forms are equal, nothing extra is bound.
  • The four rules are in console_static_sampler_words, with the measurement in the comment. The copy also drops the layer-only kCompareNever mark. When a comparison and a plain sampler with otherwise equal fields would share a console key, the first keeps it and the clash is logged.

Tests (RTX 3060, driver 616.64, Windows 11, clang-cl)

  • d3d12_descriptor_test gets a case with the FidelityFX description (LINEAR, CLAMP, MaxAnisotropy 16, LESS_EQUAL, OPAQUE_WHITE, MaxLOD FLT_MAX). The expected console words are derived field by field from that description in the test's comment, and match what the compiler wrote. The key isn't in the table before CreateRootSignature and is after it.
  • Fails without the layer change (line 999: sampler_index(console) != 0) and passes with it.
  • d3d12_descriptor_test also passes with LEONIDA_VK_VALIDATION=1 (0 validation errors). d3d12_vulkan_device_test, d3d12_m2_textured_test and root_signature_test pass.

Limits

The four rules come from one direct pair plus the conventions of the game's 27 literals. A MaxLOD between 15.0 and 16.0 keeps the layer's clamp, because I have no measurement of what the compiler writes there. The FidelityFX literal reaches the index only once #4604 folds s_bfm_b32. After that, #2887's literal path registers it by decoding, and this key, made from the description, is the one without the compare bit.

Thanks

  • @Kam471 for the RTX 5070 Ti run (Blackwell).
  • @Atomic258 for the second Blackwell run.
  • @paratustra for the review that found the NEVER-comparison mark and the compare/plain key clash (both handled in a follow-up commit).
  • @Ayinaki for the RTX 5070 run of the review fixes.
  • @gertify for the RTX 4080 run and @memorypressure for the static review of the merge with main.

By @rafaelpssz, with Claude Code (AI-assisted).

…r writes

A shader that uses a root signature's static sampler holds the S# the
console's compiler baked in, and CreateRootSignature registered only the
layer's own encoding of the description. On the one real pair in the
package (amd_fidelityfx_upscaler_xbox.dll: its sampling program's literal
S# is its root signature's single static sampler) every sampling field
matches, but the words differ in four places the hash table key covers:
DEPTH_COMPARE_FUNC copied for a plain filter, MaxLOD FLT_MAX saturated to
0xFFF instead of clamped to 0xF00, MIP_POINT_PRECLAMP set, and the border
colour zeroed when no address mode uses it. All 27 distinct literal S# in
the game's own shaders follow the same conventions.

register_static_samplers now also binds that form (bind_sampler_keyed),
with the Vulkan sampler still made from the layer's encoding, so a plain
sample never gets a comparing sampler from the copied compare function.
The descriptor test pins it with the FidelityFX description; it fails
without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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_descriptor_test at afbd39ebc on Windows 11, RTX 5070 Ti: exit 0 — descriptors/copies matched, a T# and an S# were found in the bindless set by a shader, 64 RGBA8/RGB9E5 texels matched, the unknown/destroyed fallbacks, UINT/float borders and B8G8R8X8 alpha all checked.

The static sampler is now also keyed by the S# the console's compiler writes (which is not bit-equal to the layer's), so a shader whose S# comes from the game still finds the sampler; the added cases cover both key forms. git diff --check clean. No blocking issue.

@Atomic258

Copy link
Copy Markdown
Contributor

Independent verification on NVIDIA hardware (Blackwell).

  • Head SHA: afbd39ebc960985e9e4a0fb82ab6c1051d0b1adf
  • OS: Windows 11 x64; GPU: NVIDIA GeForce RTX 5090 (driver 617.42); Vulkan SDK 1.4.363.0; MSVC 14.44 + Ninja, RelWithDebInfo
  • Built d3d12_descriptor_test, d3d12_vulkan_device_test, d3d12_m2_textured_test, root_signature_test: exit 0, no warnings in the touched files (d3d12_descriptor_test.cpp, d3d12_vulkan_device.cpp). Pre-existing C4456 shadow warnings in d3d12_vulkan_device_test.cpp (lines 6743/6750) are unrelated to this PR — they are what tests: remove two MSVC shadow warnings in d3d12_vulkan_device #4741 addresses.

Commands / results:

  • .\d3d12_descriptor_test.exe — exit 0. New case ran: static sampler: the console compiler's encoding of it is in the hash table too; static samplers: 4 of 4 root signatures registered their S#; all descriptor/copy/MSAA/border checks matched.
  • LEONIDA_VK_VALIDATION=1 .\d3d12_descriptor_test.exe — exit 0, device: NVIDIA GeForce RTX 5090 (validation on), 0 validation errors.
  • .\d3d12_vulkan_device_test.exe — exit 0 (all comparisons matched; 0 mismatches on resolve/MSAA checks).
  • .\d3d12_m2_textured_test.exe — exit 0 (M2: ... matched all 4096 pixels (error 0); translated PS sampled a T#/S# found in the hash table).
  • .\root_signature_test.exe — exit 0 (all PASS).

Red control: reverted only runtime/shims/d3d12_vulkan_device.cpp to origin/main, rebuilt d3d12_descriptor_test — exit 1 at line 999: sampler_index(console) != 0, exactly as described. Restored and rebuilt — exit 0.

AI-assisted (Devin).

@paratustra

Copy link
Copy Markdown
Contributor

Code read at afbd39e. One encoding the new key misses, and one possible collision:

  1. A COMPARISON static sampler with ComparisonFunc = NEVER can never match. gfx10::encode marks it with the layer-private kCompareNever (1u << 12 in WORD3, gfx10_descriptors.h:493/521). That is "an otherwise unused bit in layer-created samplers", so the console's compiler doesn't write it. console_static_sampler_words clears only WORD3 bits 0-11 and 30-31, and only when no address mode uses the border (d3d12_vulkan_device.cpp ~5901). So bit 12 survives into the console key, the literal S# in the shader never equals it, and that sampler falls back without a log line. Clearing kCompareNever from w[3] in the console copy, in both border branches, would fix it.
  2. Possible collision (from the code, not run): the console key copies ComparisonFunc - 1 into DEPTH_COMPARE_FUNC whatever the filter. So COMPARISON_MIN_MAG_MIP_LINEAR and MIN_MAG_MIP_LINEAR with the same other fields and the same ComparisonFunc get the same key. bind_sampler_keyed keeps the first one registered, so the second static sampler would sample with the other one's Vulkan sampler: comparing where it shouldn't, or the reverse. The comment "a plain sample never gets a comparing sampler" holds only while those keys don't collide. Refusing or logging a duplicate console key would at least make this visible.

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

… clash is logged

From @paratustra's review of ItsNotRexed#4765:
- encode() marks a NEVER comparison with kCompareNever (WORD3 bit 12), which
  the console's compiler never writes, so that key could not match. The
  console copy now clears it; a second descriptor-test case (a POINT NEVER
  comparison) pins it and fails without the change.
- The console encoding has no compare bit of its own, so a comparison and a
  plain static sampler with the same other fields share one key, and one
  Vulkan sampler can't be both. The first one keeps it and a different one
  is logged instead of taking it silently; the same sampler again (27 root
  signatures of the FidelityFX DLL repeat one) is not a clash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rafaelpssz

Copy link
Copy Markdown
Contributor Author

Thanks @paratustra, both points are right. Fixed in 5969148e:

  1. NEVER comparison. The console copy now clears kCompareNever (WORD3 bit 12), the layer's own mark. A second case in d3d12_descriptor_test (POINT, ComparisonFunc NEVER, CLAMP, MaxLOD FLT_MAX: expected 0x92, 0xFFF000, 0x14000000, 0) pins it. It fails without the change (line 1024) and passes with it.

  2. Collision. You're right that the console encoding has no compare bit of its own: the instruction compares, not the S#. So a comparison sampler and a plain one with the same other fields share one key, and one Vulkan sampler can't be both.

    • Now the first one registered keeps the key, and a different sampler that maps to the same key is logged instead of taking it silently.
    • The same sampler again is not a clash: the FidelityFX DLL's 27 root signatures repeat one sampler. Each console key remembers which layer encoding it was registered for.

    Picking per instruction would need the translator to tell the layer which ops use a key. Nothing in the game's shaders needs that so far: all 15,155 compare-function literals are used by _c ops.

d3d12_descriptor_test (with and without LEONIDA_VK_VALIDATION=1, 0 errors), d3d12_vulkan_device_test, d3d12_m2_textured_test and root_signature_test pass on an RTX 3060. You're in Thanks.

@Ayinaki

Ayinaki commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 5969148 on RTX 5070, driver 616.86, Windows 10.0.26200: pass.

Built runtime with MSVC 14.52 (Visual Studio 18 2026, x64, RelWithDebInfo) and ran d3d12_descriptor_test with LEONIDA_VK_DEVICE="RTX 5070". The process exits 0; its last checks report the depth SRVs path (6 valid D16/D32 views sampled their cleared depth values, invalid colour casts rejected as null descriptors) with 0 validation errors.

@memorypressure

Copy link
Copy Markdown

Static review on the merge with current main — no Python-side tests exist for this change and the C++ half cannot build on this CPU-only host. The console-encoding derivation only changes the hash-table key (the VkSampler is still built from the layer's encode() words), the compare-vs-plain key clash is logged instead of silently shared, and the mutex-guarded key map looks race-clean; the two new d3d12_descriptor_test.cpp cases are field-derived word listings, no game data in the diff. CI checks (change-scope, no-game-data, linux-gpu-tools, windows) were all pending when verified — the descriptor test on a real adapter carries the functional verdict. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 5969148 on NVIDIA GeForce RTX 4080, driver 617.42, Windows 11 (clang-cl cross-build): d3d12_descriptor_test exits 0. It prints no check summary on a pass, so the exit code is the only signal. The test's control without the layer change and the LEONIDA_VK_VALIDATION=1 run were not done here.

AI-assisted.

@rafaelpssz

Copy link
Copy Markdown
Contributor Author

Thanks @gertify. For the record, a pass does print two lines from the new cases: static sampler: the console compiler's encoding of it is in the hash table too and static sampler: a NEVER comparison's console encoding has no layer mark. Without the layer change, it fails at line 999 / line 1024. You and @memorypressure are in Thanks.

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