Shaders, GPU: clamp immediate static sampler MipLODBias to D3D12 bounds #3043

Open
github-bot wants to merge 1 commit from github-bot/gh-3043 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 05:10 UTC · original
fuzeofficlal:fix/static-sampler-mip-lod-bias → main · +89 −2 · 3 files · 1 commits · open

Problem

StaticSampler::from_words decodes the hardware's 14-bit signed 6.8 fixed-point LOD_BIAS field faithfully across [-32.0, +31.996]. Dynamic samplers in crates/victory-gpu/src/sampler.rs clamp MipLODBias to D3D12_MIP_LOD_BIAS_MIN..=D3D12_MIP_LOD_BIAS_MAX ([-16.0, 15.99]), but both static-sampler conversion sites (crates/victory-gpu/src/pipeline/host.rs and crates/victory-shader/src/emit/host.rs) copied mip_lod_bias directly into D3D12_STATIC_SAMPLER_DESC::MipLODBias.

When an immediate sampler encodes a bias outside [-16.0, 15.99] (e.g. -24.0 or +24.0), D3D12SerializeVersionedRootSignature fails with E_INVALIDARG and the host pipeline is refused.

Fix

  • Add StaticSampler::mip_lod_bias_for_host(&self) -> f32 mirroring StaticSampler::filter_for_host, clamping to [-16.0, 15.99] (and mapping NaN to 0.0) while leaving the decoded mip_lod_bias field in StaticSampler::from_words unchanged.
  • Use mip_lod_bias_for_host() when filling D3D12_STATIC_SAMPLER_DESC in crates/victory-gpu/src/pipeline/host.rs and crates/victory-shader/src/emit/host.rs.

Verification

  • cargo test --locked -p victory-shader --lib emit::sampler:: (7 passed, including mip_lod_bias_for_host_clamps_to_d3d12_bounds)
  • cargo test --locked -p victory-gpu --lib pipeline::host::tests:: on RTX 5090 and WARP (out_of_range_static_sampler_mip_lod_bias_serializes_for_host confirms clamped -32.0/-24.0/+24.0/+31.996 serialize cleanly while unclamped -24.0 is rejected by D3D12SerializeVersionedRootSignature)
  • cargo clippy --locked -p victory-shader -p victory-gpu --all-targets -- -D warnings
  • rustfmt --edition 2024 --check
  • cargo xtask policy (191 files ok)
**@⁠fuzeofficlal** on GitHub · 2026-11-26 05:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3043) `fuzeofficlal:fix/static-sampler-mip-lod-bias` → `main` · +89 −2 · 3 files · 1 commits · open ### Problem `StaticSampler::from_words` decodes the hardware's 14-bit signed 6.8 fixed-point `LOD_BIAS` field faithfully across `[-32.0, +31.996]`. Dynamic samplers in `crates/victory-gpu/src/sampler.rs` clamp `MipLODBias` to `D3D12_MIP_LOD_BIAS_MIN..=D3D12_MIP_LOD_BIAS_MAX` (`[-16.0, 15.99]`), but both static-sampler conversion sites (`crates/victory-gpu/src/pipeline/host.rs` and `crates/victory-shader/src/emit/host.rs`) copied `mip_lod_bias` directly into `D3D12_STATIC_SAMPLER_DESC::MipLODBias`. When an immediate sampler encodes a bias outside `[-16.0, 15.99]` (e.g. `-24.0` or `+24.0`), `D3D12SerializeVersionedRootSignature` fails with `E_INVALIDARG` and the host pipeline is refused. ### Fix - Add `StaticSampler::mip_lod_bias_for_host(&self) -> f32` mirroring `StaticSampler::filter_for_host`, clamping to `[-16.0, 15.99]` (and mapping `NaN` to `0.0`) while leaving the decoded `mip_lod_bias` field in `StaticSampler::from_words` unchanged. - Use `mip_lod_bias_for_host()` when filling `D3D12_STATIC_SAMPLER_DESC` in `crates/victory-gpu/src/pipeline/host.rs` and `crates/victory-shader/src/emit/host.rs`. ### Verification - `cargo test --locked -p victory-shader --lib emit::sampler::` (7 passed, including `mip_lod_bias_for_host_clamps_to_d3d12_bounds`) - `cargo test --locked -p victory-gpu --lib pipeline::host::tests::` on RTX 5090 and WARP (`out_of_range_static_sampler_mip_lod_bias_serializes_for_host` confirms clamped `-32.0/-24.0/+24.0/+31.996` serialize cleanly while unclamped `-24.0` is rejected by `D3D12SerializeVersionedRootSignature`) - `cargo clippy --locked -p victory-shader -p victory-gpu --all-targets -- -D warnings` - `rustfmt --edition 2024 --check` - `cargo xtask policy` (191 files ok)
The console's immediate sampler descriptor (`SQ_IMG_SAMP_WORD2[13:0]`)
encodes a signed 14-bit 6.8 fixed-point mip LOD bias spanning
`[-32.0, +31.996]`, whereas `D3D12_STATIC_SAMPLER_DESC::MipLODBias` only
accepts `[-16.0, 15.99]` (`D3D12_MIP_LOD_BIAS_MIN..=D3D12_MIP_LOD_BIAS_MAX`).

Dynamic samplers in `victory-gpu/src/sampler.rs` already clamp `MipLODBias`
to D3D12's bounds, but static samplers forwarded `s.mip_lod_bias`
unclamped, causing `D3D12SerializeVersionedRootSignature` to reject the
root signature with `E_INVALIDARG`.

Add `StaticSampler::mip_lod_bias_for_host()` (clamping to `[-16.0, 15.99]`
with NaN mapped to `0.0`, while keeping `StaticSampler::from_words`
faithful to the decoded hardware field) and use it when building
`D3D12_STATIC_SAMPLER_DESC` in both `victory-gpu` and `victory-shader`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:27:24 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 06:17 UTC · original

Automated triage for the maintainers.

StaticSampler::from_words (crates/victory-shader/src/emit/sampler.rs) decodes the 14-bit signed 6.8 bias, which covers [-32, +31.996]. Both static-sampler sites, HostPipeline's root signature in crates/victory-gpu/src/pipeline/host.rs and Host in crates/victory-shader/src/emit/host.rs, passed that value to D3D12_STATIC_SAMPLER_DESC::MipLODBias without clamping. Root signature serialization rejects values outside D3D12's range. The new mip_lod_bias_for_host clamps to [-16, 15.99] and maps NaN to 0. Dynamic samplers already do the same with D3D12_MIP_LOD_BIAS_MIN/MAX in crates/victory-gpu/src/sampler.rs, so static and dynamic samplers now agree. The decoded value is still kept unclamped in the shader cache.

CI: check passing.

A small improvement: use D3D12_MIP_LOD_BIAS_MIN/MAX in the clamp instead of the literals, if victory-shader can reach them.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 06:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/3043#issuecomment-7184275345) <!-- victory-triage v1 head=5437d70bde5e7ab5bd6fdc2adc889ff80f6575eb verdict=merge-candidate at=2026-11-26T06:37:00Z --> Automated triage for the maintainers. `StaticSampler::from_words` (`crates/victory-shader/src/emit/sampler.rs`) decodes the 14-bit signed 6.8 bias, which covers [-32, +31.996]. Both static-sampler sites, `HostPipeline`'s root signature in `crates/victory-gpu/src/pipeline/host.rs` and `Host` in `crates/victory-shader/src/emit/host.rs`, passed that value to `D3D12_STATIC_SAMPLER_DESC::MipLODBias` without clamping. Root signature serialization rejects values outside D3D12's range. The new `mip_lod_bias_for_host` clamps to `[-16, 15.99]` and maps NaN to 0. Dynamic samplers already do the same with `D3D12_MIP_LOD_BIAS_MIN/MAX` in `crates/victory-gpu/src/sampler.rs`, so static and dynamic samplers now agree. The decoded value is still kept unclamped in the shader cache. CI: `check` passing. A small improvement: use `D3D12_MIP_LOD_BIAS_MIN/MAX` in the clamp instead of the literals, if `victory-shader` can reach them. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3043/head:github-bot/gh-3043'
git switch 'github-bot/gh-3043'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3043'
git switch 'github-bot/gh-3043'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3043'
git switch 'github-bot/gh-3043'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3043'
git switch 'main'
git merge --squash 'github-bot/gh-3043'
git switch 'main'
git merge --ff-only 'github-bot/gh-3043'
git switch 'main'
git merge 'github-bot/gh-3043'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
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
github/Marvisak--VIctory!3043
No description provided.