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

Open
opened 2026-11-26 20:16:46 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/38
Author: @Andretuta
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: old-pr/4765


📝 Commits (2)

  • afbd39e d3d12: a static sampler is also keyed by the S# the console's compiler writes
  • 5969148 d3d12: the console static-sampler key drops the layer's NEVER mark; a clash is logged

📊 Changes

2 files changed (+122 additions, -4 deletions)

View changed files

📝 runtime/shims/d3d12_vulkan_device.cpp (+67 -4)
📝 runtime/test/d3d12_descriptor_test.cpp (+55 -0)

📄 Description

Carried over from old PR 4765 by rafaelpssz in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/4765, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
rafaelpssz: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/38 **Author:** [@Andretuta](https://github.com/Andretuta) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `old-pr/4765` --- ### 📝 Commits (2) - [`afbd39e`](https://github.com/ubiquitarianism/leonida/commit/afbd39ebc960985e9e4a0fb82ab6c1051d0b1adf) d3d12: a static sampler is also keyed by the S# the console's compiler writes - [`5969148`](https://github.com/ubiquitarianism/leonida/commit/5969148ea4499e8de8ce8b6cc46307a0a6683fae) d3d12: the console static-sampler key drops the layer's NEVER mark; a clash is logged ### 📊 Changes **2 files changed** (+122 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+67 -4) 📝 `runtime/test/d3d12_descriptor_test.cpp` (+55 -0) </details> ### 📄 Description > Carried over from **old PR 4765** by `rafaelpssz` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/4765`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `rafaelpssz`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): New D3D item ([claim](https://github.com/ItsNotRexed/Leonida/issues/4683#issuecomment-7091905865)). ### 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#91
No description provided.