GPU: sanitize CreateSamplerX LOD range, bias clamp, and invalid enum values #474
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/474"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 intoD3D12_SAMPLER_DESC:mip == 0(no mip filter),MinLOD/MaxLODused(self.min_lod, self.min_lod)without sanitizing non-finite (NaN/+/-inf) values.mip != 0,MaxLOD < MinLODand non-finite (NaN/+/-inf) LOD values were forwarded verbatim toID3D12Device::CreateSampler.MipLODBiaswas forwarded unclamped outside[D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX]([-16.0, 15.99]) and could passNaN.AddressU/AddressV/AddressWoutside1..=5andComparisonFuncwhenreduction != 1(non-comparison filters) or outside valid bounds were forwarded verbatim.What changed
address_mode(mode: u32) -> D3D12_TEXTURE_ADDRESS_MODEincrates/victory-gpu/src/sampler.rsmapping1..=5directly and falling back toD3D12_TEXTURE_ADDRESS_MODE_CLAMPfor0or values> 5.MipLODBiasto[D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX]with a0.0fallback forNaN.ComparisonFunconreduction == 1(whosecompareshelper guarantees2..=7), emittingD3D12_COMPARISON_FUNC_NEVERotherwise.min_lodviafinite_lod(self.min_lod, 0.0)(NaN->0.0, infinities clamped to[-D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX]). Whenmip == 0, kept(min_lod, min_lod)clamping behavior matchingmain; otherwise sanitizedmax_lodand clampedmax_lod = max_lod.max(min_lod).mip_none_clamps_to_min_lod_and_inverted_lod_range_clampsandbias_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 (includessampler::tests::console_descriptions_decode,sampler::tests::mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps, andsampler::tests::bias_address_and_comparison_stay_in_host_bounds).cargo clippy -p victory-gpu --all-targets: passed (exit 0; 0 warnings incrates/victory-gpu/src/sampler.rs, 1 pre-existing warning atcrates/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.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T05:57:32Z.
Review: COMMENTED
Tested
e6fe8econ Windows 11, RTX 5070, MSVC:cargo test --workspace --no-fail-fastpasses (265 passed, 0 failed).Most of this is sound hardening:
MipLODBiasis clamped to the D3D12 range, with NaN mapped to 0.ComparisonFuncis only used for comparison filters (reduction == 1).max >= min.One behaviour change needs evidence before I can approve:
mip == 0now gives(MinLOD, MaxLOD) = (0.0, 0.0), where main gives(min_lod, min_lod).min_lodlevel. The new form always samples the view's most detailed mip and ignoresmin_lod.min_lod, so I read the old choice as deliberate.min_lod > 0with 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).
Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T03:56:09Z.
Verified
e6fe8econ 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 thanmain, presumably the new sampler case).Not covered: the title's samplers with these enum/edge combinations (no game data here).
Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-24T06:49:05Z.
Thanks @mercuryVM for catching the unverified
mip == 0change!Updated branch
fix/gpu-console-sampler-validationin commite34cb53to keep finite(min_lod, min_lod)whenmip == 0(matchingmain'smin_lodclamping while still sanitizingNaN/+/-infviafinite_lod), keeping the remaining hardening (MaxLOD >= MinLODwhenmip != 0,MipLODBiasclamp/NaNfallback,AddressU/V/Wfallback toCLAMP, andComparisonFuncgated onreduction == 1).Also updated the unit test (
mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps) and PR description. Re-verified one34cb53:rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs: passedcargo test -p victory-gpu -- --test-threads=1: 45 passed, 0 failed, 1 ignored (all 3sampler::tests::*pass)cargo clippy -p victory-gpu --all-targets: 0 warnings insampler.rscargo xtask policy:asset policy: 151 files okImported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:16:11Z.
Review: APPROVED
Tested
e34cb53on Windows 11, RTX 5070, MSVC:cargo test --workspace --no-fail-fastpasses (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 (becomesD3D12_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).