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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3043 into main
fuzeofficlal commented 2026-11-26 05:10:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T05:10:28Z.


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)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6619980440 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3043). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T05:10:28Z. --- ### 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)
kotekompotek commented 2026-11-26 06:03:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T05:26:45Z.

Review: APPROVED


Verified: the console's signed 14-bit 6.8 field spans [-32, +31.996] while D3D12 static samplers accept only [-16, 15.99] — an unclamped bias makes D3D12SerializeVersionedRootSignature fail the whole signature. mip_lod_bias_for_host is applied at both build sites (pipeline host + emit host), NaN maps to 0, and the host test proves D3D12 itself rejects the raw -24.0 while all four clamped values serialize.

<!-- greyfinch-sync:Marvisak/VIctory:review:6068974032 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3043#pullrequestreview-6068974032). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T05:26:45Z. **Review:** APPROVED --- Verified: the console's signed 14-bit 6.8 field spans [-32, +31.996] while D3D12 static samplers accept only [-16, 15.99] — an unclamped bias makes `D3D12SerializeVersionedRootSignature` fail the whole signature. `mip_lod_bias_for_host` is applied at both build sites (pipeline host + emit host), NaN maps to 0, and the host test proves D3D12 itself rejects the raw -24.0 while all four clamped values serialize.
Dukestar69 commented 2026-11-26 06:03:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T05:28:23Z.

Review: APPROVED


Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • Diff review: Adding StaticSampler::mip_lod_bias_for_host (mapping NaN to 0.0 and clamping to D3D12's [-16.0, 15.99] static-sampler bounds) and using it in both victory-gpu/src/pipeline/host.rs and victory-shader/src/emit/host.rs cleanly prevents D3D12SerializeVersionedRootSignature failures for console 6.8 signed mip LOD biases outside D3D12's range.
  • cargo test -p victory-shader emit::sampler::tests: 7 passed, 0 failed (including mip_lod_bias_for_host_clamps_to_d3d12_bounds).
  • cargo test -p victory-gpu pipeline::host::tests -- --test-threads=1 (RTX 5090): 5 passed, 0 failed (including out_of_range_static_sampler_mip_lod_bias_serializes_for_host).
  • VICTORY_WARP=1 cargo test -p victory-gpu pipeline::host::tests -- --test-threads=1 (WARP): 5 passed, 0 failed.
  • cargo clippy -p victory-shader -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6068985173 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3043#pullrequestreview-6068985173). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T05:28:23Z. **Review:** APPROVED --- Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP): - Diff review: Adding `StaticSampler::mip_lod_bias_for_host` (mapping `NaN` to `0.0` and clamping to D3D12's `[-16.0, 15.99]` static-sampler bounds) and using it in both `victory-gpu/src/pipeline/host.rs` and `victory-shader/src/emit/host.rs` cleanly prevents `D3D12SerializeVersionedRootSignature` failures for console 6.8 signed mip LOD biases outside D3D12's range. - `cargo test -p victory-shader emit::sampler::tests`: 7 passed, 0 failed (including `mip_lod_bias_for_host_clamps_to_d3d12_bounds`). - `cargo test -p victory-gpu pipeline::host::tests -- --test-threads=1` (RTX 5090): 5 passed, 0 failed (including `out_of_range_static_sampler_mip_lod_bias_serializes_for_host`). - `VICTORY_WARP=1 cargo test -p victory-gpu pipeline::host::tests -- --test-threads=1` (WARP): 5 passed, 0 failed. - `cargo clippy -p victory-shader -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Marvisak commented 2026-11-26 07:01:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:17: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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184275345 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3043#issuecomment-7184275345). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T06:17:00Z. --- <!-- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3043:upstream-pr/3043'
git switch 'upstream-pr/3043'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3043'
git switch 'upstream-pr/3043'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3043'
git switch 'upstream-pr/3043'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3043'
git switch 'main'
git merge --squash 'upstream-pr/3043'
git switch 'main'
git merge --ff-only 'upstream-pr/3043'
git switch 'main'
git merge 'upstream-pr/3043'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3041
No description provided.