GPU: sanitize CreateSamplerX LOD range, bias clamp, and invalid enum values #474

Merged
nathanbzrr merged 2 commits from upstream-pr/474 into main 2026-11-25 00:11:03 +00:00
nathanbzrr commented 2026-11-24 03:50:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-24T03:50:17Z.


Problem

Console sampler descriptions translated in crates/victory-gpu/src/sampler.rs (Fields::host) could pass out-of-range, non-finite, or mismatched values into D3D12_SAMPLER_DESC:

  • When mip == 0 (no mip filter), MinLOD/MaxLOD used (self.min_lod, self.min_lod) without sanitizing non-finite (NaN/+/-inf) values.
  • When mip != 0, MaxLOD < MinLOD and non-finite (NaN/+/-inf) LOD values were forwarded verbatim to ID3D12Device::CreateSampler.
  • MipLODBias was forwarded unclamped outside [D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX] ([-16.0, 15.99]) and could pass NaN.
  • AddressU/AddressV/AddressW outside 1..=5 and ComparisonFunc when reduction != 1 (non-comparison filters) or outside valid bounds were forwarded verbatim.

What changed

  • Added address_mode(mode: u32) -> D3D12_TEXTURE_ADDRESS_MODE in crates/victory-gpu/src/sampler.rs mapping 1..=5 directly and falling back to D3D12_TEXTURE_ADDRESS_MODE_CLAMP for 0 or values > 5.
  • Clamped MipLODBias to [D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX] with a 0.0 fallback for NaN.
  • Gated ComparisonFunc on reduction == 1 (whose compares helper guarantees 2..=7), emitting D3D12_COMPARISON_FUNC_NEVER otherwise.
  • Sanitized min_lod via finite_lod(self.min_lod, 0.0) (NaN -> 0.0, infinities clamped to [-D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX]). When mip == 0, kept (min_lod, min_lod) clamping behavior matching main; otherwise sanitized max_lod and clamped max_lod = max_lod.max(min_lod).
  • Added synthetic unit tests mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps and bias_address_and_comparison_stay_in_host_bounds.

Tests (isolated branch e34cb53)

  • rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs: passed (exit 0).
  • cargo test -p victory-gpu -- --test-threads=1: 45 passed, 0 failed, 1 ignored benchmark (includes sampler::tests::console_descriptions_decode, sampler::tests::mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps, and sampler::tests::bias_address_and_comparison_stay_in_host_bounds).
  • cargo clippy -p victory-gpu --all-targets: passed (exit 0; 0 warnings in crates/victory-gpu/src/sampler.rs, 1 pre-existing warning at crates/victory-gpu/src/host.rs:267).
  • cargo xtask policy: asset policy: 151 files ok.

No game files. AI-assisted; reviewed and verified with the tests above.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6574190699 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/474). Originally posted by [nathanbzrr](https://github.com/nathanbzrr) on 2026-11-24T03:50:17Z. --- ## Problem Console sampler descriptions translated in `crates/victory-gpu/src/sampler.rs` (`Fields::host`) could pass out-of-range, non-finite, or mismatched values into `D3D12_SAMPLER_DESC`: - When `mip == 0` (no mip filter), `MinLOD`/`MaxLOD` used `(self.min_lod, self.min_lod)` without sanitizing non-finite (`NaN`/`+/-inf`) values. - When `mip != 0`, `MaxLOD < MinLOD` and non-finite (`NaN`/`+/-inf`) LOD values were forwarded verbatim to `ID3D12Device::CreateSampler`. - `MipLODBias` was forwarded unclamped outside `[D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX]` (`[-16.0, 15.99]`) and could pass `NaN`. - `AddressU`/`AddressV`/`AddressW` outside `1..=5` and `ComparisonFunc` when `reduction != 1` (non-comparison filters) or outside valid bounds were forwarded verbatim. ## What changed - Added `address_mode(mode: u32) -> D3D12_TEXTURE_ADDRESS_MODE` in `crates/victory-gpu/src/sampler.rs` mapping `1..=5` directly and falling back to `D3D12_TEXTURE_ADDRESS_MODE_CLAMP` for `0` or values `> 5`. - Clamped `MipLODBias` to `[D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX]` with a `0.0` fallback for `NaN`. - Gated `ComparisonFunc` on `reduction == 1` (whose `compares` helper guarantees `2..=7`), emitting `D3D12_COMPARISON_FUNC_NEVER` otherwise. - Sanitized `min_lod` via `finite_lod(self.min_lod, 0.0)` (`NaN` -> `0.0`, infinities clamped to `[-D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX]`). When `mip == 0`, kept `(min_lod, min_lod)` clamping behavior matching `main`; otherwise sanitized `max_lod` and clamped `max_lod = max_lod.max(min_lod)`. - Added synthetic unit tests `mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps` and `bias_address_and_comparison_stay_in_host_bounds`. ## Tests (isolated branch `e34cb53`) - `rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs`: passed (exit 0). - `cargo test -p victory-gpu -- --test-threads=1`: 45 passed, 0 failed, 1 ignored benchmark (includes `sampler::tests::console_descriptions_decode`, `sampler::tests::mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps`, and `sampler::tests::bias_address_and_comparison_stay_in_host_bounds`). - `cargo clippy -p victory-gpu --all-targets`: passed (exit 0; 0 warnings in `crates/victory-gpu/src/sampler.rs`, 1 pre-existing warning at `crates/victory-gpu/src/host.rs:267`). - `cargo xtask policy`: `asset policy: 151 files ok`. No game files. AI-assisted; reviewed and verified with the tests above.
mercuryVM commented 2026-11-24 06:53:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T05:57:32Z.

Review: COMMENTED


Tested e6fe8ec on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed).

Most of this is sound hardening:

  • Address modes outside 1..=5 fall back to CLAMP.
  • MipLODBias is clamped to the D3D12 range, with NaN mapped to 0.
  • ComparisonFunc is only used for comparison filters (reduction == 1).
  • LODs become finite, and max >= min.

One behaviour change needs evidence before I can approve: mip == 0 now gives (MinLOD, MaxLOD) = (0.0, 0.0), where main gives (min_lod, min_lod).

  • Main's form clamps sampling to the sampler's min_lod level. The new form always samples the view's most detailed mip and ignores min_lod.
  • The comment "the console samples mip 0 only" was already on the old code, which still applied min_lod, so I read the old choice as deliberate.
  • If RDNA's MIP_FILTER=none still applies the sampler's MIN_LOD clamp, this change samples a sharper mip than the console for any title that sets min_lod > 0 with no mip filter, for example a streamed texture that is clamped while higher mips load.

Could you point to the ISA or a capture that shows MIN_LOD is ignored without a mip filter? Otherwise, keeping (min_lod, min_lod) (made finite) and taking the rest of the PR as is would be safe.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6034396207 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/474#pullrequestreview-6034396207). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T05:57:32Z. **Review:** COMMENTED --- Tested e6fe8ec on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (265 passed, 0 failed). Most of this is sound hardening: - Address modes outside 1..=5 fall back to CLAMP. - `MipLODBias` is clamped to the D3D12 range, with NaN mapped to 0. - `ComparisonFunc` is only used for comparison filters (`reduction == 1`). - LODs become finite, and `max >= min`. One behaviour change needs evidence before I can approve: **`mip == 0` now gives `(MinLOD, MaxLOD) = (0.0, 0.0)`**, where main gives `(min_lod, min_lod)`. - Main's form clamps sampling to the sampler's `min_lod` level. The new form always samples the view's most detailed mip and ignores `min_lod`. - The comment "the console samples mip 0 only" was already on the old code, which still applied `min_lod`, so I read the old choice as deliberate. - If RDNA's MIP_FILTER=none still applies the sampler's MIN_LOD clamp, this change samples a sharper mip than the console for any title that sets `min_lod > 0` with no mip filter, for example a streamed texture that is clamped while higher mips load. Could you point to the ISA or a capture that shows MIN_LOD is ignored without a mip filter? Otherwise, keeping `(min_lod, min_lod)` (made finite) and taking the rest of the PR as is would be safe. AI-assisted (Claude Code).
DaviTostes commented 2026-11-24 06:55:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T03:56:09Z.


Verified e6fe8ec on Arch Linux x86_64, Windows MSVC release build under Wine 11.22 (victory-gpu, --test-threads=1): 45 passed, 0 failed, 1 ignored (one more passing test than main, presumably the new sampler case).

Not covered: the title's samplers with these enum/edge combinations (no game data here).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121555988 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/474#issuecomment-7121555988). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T03:56:09Z. --- Verified `e6fe8ec` on **Arch Linux x86_64**, Windows MSVC release build under **Wine 11.22** (`victory-gpu`, `--test-threads=1`): **45 passed, 0 failed, 1 ignored** (one more passing test than `main`, presumably the new sampler case). Not covered: the title's samplers with these enum/edge combinations (no game data here).
nathanbzrr commented 2026-11-24 06:56:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-24T06:49:05Z.


Thanks @mercuryVM for catching the unverified mip == 0 change!

Updated branch fix/gpu-console-sampler-validation in commit e34cb53 to keep finite (min_lod, min_lod) when mip == 0 (matching main's min_lod clamping while still sanitizing NaN/+/-inf via finite_lod), keeping the remaining hardening (MaxLOD >= MinLOD when mip != 0, MipLODBias clamp/NaN fallback, AddressU/V/W fallback to CLAMP, and ComparisonFunc gated on reduction == 1).

Also updated the unit test (mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps) and PR description. Re-verified on e34cb53:

  • rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs: passed
  • cargo test -p victory-gpu -- --test-threads=1: 45 passed, 0 failed, 1 ignored (all 3 sampler::tests::* pass)
  • cargo clippy -p victory-gpu --all-targets: 0 warnings in sampler.rs
  • cargo xtask policy: asset policy: 151 files ok
<!-- greyfinch-sync:Marvisak/VIctory:comment:7124321515 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/474#issuecomment-7124321515). Originally posted by [nathanbzrr](https://github.com/nathanbzrr) on 2026-11-24T06:49:05Z. --- Thanks @mercuryVM for catching the unverified `mip == 0` change! Updated branch `fix/gpu-console-sampler-validation` in commit `e34cb53` to keep finite `(min_lod, min_lod)` when `mip == 0` (matching `main`'s `min_lod` clamping while still sanitizing `NaN`/`+/-inf` via `finite_lod`), keeping the remaining hardening (`MaxLOD >= MinLOD` when `mip != 0`, `MipLODBias` clamp/`NaN` fallback, `AddressU`/`V`/`W` fallback to `CLAMP`, and `ComparisonFunc` gated on `reduction == 1`). Also updated the unit test (`mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps`) and PR description. Re-verified on `e34cb53`: - `rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs`: passed - `cargo test -p victory-gpu -- --test-threads=1`: 45 passed, 0 failed, 1 ignored (all 3 `sampler::tests::*` pass) - `cargo clippy -p victory-gpu --all-targets`: 0 warnings in `sampler.rs` - `cargo xtask policy`: `asset policy: 151 files ok`
mercuryVM commented 2026-11-24 08:02:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:16:11Z.

Review: APPROVED


Tested e34cb53 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (266 passed, 0 failed).

Thanks, this addresses my earlier point. With no mip filter, sampling now clamps to the sanitized min_lod (MaxLOD = MinLOD), as main does. The new cases cover NaN (becomes 0) and +inf (becomes D3D12_FLOAT32_MAX, so the coarsest mip, which is what an infinite min LOD means). The rest of the hardening is unchanged from what I reviewed before.

No issues found.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6035134280 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/474#pullrequestreview-6035134280). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T07:16:11Z. **Review:** APPROVED --- Tested e34cb53 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (266 passed, 0 failed). Thanks, this addresses my earlier point. With no mip filter, sampling now clamps to the sanitized `min_lod` (`MaxLOD = MinLOD`), as main does. The new cases cover NaN (becomes 0) and +inf (becomes `D3D12_FLOAT32_MAX`, so the coarsest mip, which is what an infinite min LOD means). The rest of the hardening is unchanged from what I reviewed before. No issues found. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 01:03:53 +00:00
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!474
No description provided.