From 011735ff809d8655f697d5b2749e2a79d5faabb1 Mon Sep 17 00:00:00 2001 From: local Date: Tue, 24 Nov 2026 23:24:56 -0600 Subject: [PATCH] Anisotropic samplers with point mips keep them where the card has them (upstream e45bacd, 23fc8a5, 7dfa86f) Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/pipeline/host.rs | 15 +++++- crates/victory-shader/src/emit/host.rs | 14 ++++- crates/victory-shader/src/emit/sampler.rs | 64 ++++++++++++++++++++++- 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/crates/victory-gpu/src/pipeline/host.rs b/crates/victory-gpu/src/pipeline/host.rs index 9acd1d2..706758d 100644 --- a/crates/victory-gpu/src/pipeline/host.rs +++ b/crates/victory-gpu/src/pipeline/host.rs @@ -437,6 +437,19 @@ impl HostPipeline { } let host = host()?; + let mut options = D3D12_FEATURE_DATA_D3D12_OPTIONS19::default(); + // SAFETY: the feature query writes the structure of the given size. + // Older runtimes may not recognize OPTIONS19; that means no support. + let aniso_point_mip_supported = unsafe { + host.device.CheckFeatureSupport( + D3D12_FEATURE_D3D12_OPTIONS19, + (&raw mut options).cast(), + size_of_val(&options) as u32, + ) + } + .is_ok() + && options.AnisoFilterWithPointMipSupported.as_bool(); + // The root signature the translated shaders' bindings define. let mut params: Vec = Vec::new(); // The ranges table parameters point at, alive until the root @@ -544,7 +557,7 @@ impl HostPipeline { } for (n, s) in t.static_samplers.iter().enumerate() { samplers.push(D3D12_STATIC_SAMPLER_DESC { - Filter: D3D12_FILTER(s.filter as i32), + Filter: D3D12_FILTER(s.filter_for_host(aniso_point_mip_supported) as i32), AddressU: D3D12_TEXTURE_ADDRESS_MODE(s.address[0] as i32), AddressV: D3D12_TEXTURE_ADDRESS_MODE(s.address[1] as i32), AddressW: D3D12_TEXTURE_ADDRESS_MODE(s.address[2] as i32), diff --git a/crates/victory-shader/src/emit/host.rs b/crates/victory-shader/src/emit/host.rs index 8f5407a..4bb533f 100644 --- a/crates/victory-shader/src/emit/host.rs +++ b/crates/victory-shader/src/emit/host.rs @@ -185,12 +185,24 @@ impl Host { ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, }); } + let mut options = D3D12_FEATURE_DATA_D3D12_OPTIONS19::default(); + // SAFETY: the feature query writes the structure of the given size. + // Older runtimes may not recognize OPTIONS19; that means no support. + let aniso_point_mip_supported = unsafe { + self.device.CheckFeatureSupport( + D3D12_FEATURE_D3D12_OPTIONS19, + (&raw mut options).cast(), + size_of_val(&options) as u32, + ) + } + .is_ok() + && options.AnisoFilterWithPointMipSupported.as_bool(); let mut samplers = Vec::new(); for &(s, vis) in stages { for (n, d) in s.static_samplers.iter().enumerate() { let [u, v, w] = d.address.map(|a| D3D12_TEXTURE_ADDRESS_MODE(a as i32)); samplers.push(D3D12_STATIC_SAMPLER_DESC { - Filter: D3D12_FILTER(d.filter as i32), + Filter: D3D12_FILTER(d.filter_for_host(aniso_point_mip_supported) as i32), AddressU: u, AddressV: v, AddressW: w, diff --git a/crates/victory-shader/src/emit/sampler.rs b/crates/victory-shader/src/emit/sampler.rs index e10c0c1..ca010e5 100644 --- a/crates/victory-shader/src/emit/sampler.rs +++ b/crates/victory-shader/src/emit/sampler.rs @@ -34,6 +34,18 @@ pub struct StaticSampler { impl Eq for StaticSampler {} impl StaticSampler { + /// The filter supported by the host device. Anisotropic point-mip + /// filtering is optional (`D3D12_OPTIONS19`); older devices retain the + /// previous linear-mip approximation. Comparison/min/max reduction + /// bits and the sampler's LOD bounds are unchanged. + pub fn filter_for_host(&self, aniso_point_mip_supported: bool) -> u32 { + if !aniso_point_mip_supported && self.filter & !0x180 == 0x54 { + self.filter | 1 + } else { + self.filter + } + } + /// Decodes the four sampler words. pub fn from_words(w: [u32; 4]) -> Self { let bits = |word: u32, at: u32, n: u32| word >> at & ((1 << n) - 1); @@ -47,7 +59,7 @@ impl StaticSampler { _ => 0, }; let filter = if mag >= 2 || min >= 2 { - 0x55 | reduction << 7 + 0x54 | u32::from(mip == 2) | reduction << 7 } else { (min & 1) << 4 | (mag & 1) << 2 | u32::from(mip == 2) | reduction << 7 }; @@ -122,4 +134,54 @@ mod tests { (0x1D5, 16, 15.0) ); } + + #[test] + fn anisotropic_samplers_preserve_mip_point_filtering() { + for (reduction, base) in [(0u32, 0x54u32), (1, 0x154), (2, 0x1D4)] { + let w0 = reduction << 29; + for mip in [0u32, 1] { + let w2 = (mip << 26) | 0x00F0_0000; // mag = 3, min = 3 + let s = StaticSampler::from_words([w0, 0x00FF_F000, w2, 0]); + assert_eq!(s.filter, base, "reduction={reduction} mip={mip}"); + } + let w2 = (2 << 26) | 0x00F0_0000; // mip = 2 (linear) + let s = StaticSampler::from_words([w0, 0x00FF_F000, w2, 0]); + assert_eq!(s.filter, base | 1, "reduction={reduction} mip=2"); + } + // Mixed anisotropic mag (2) with linear min (1) and point mip (1). + let mixed = StaticSampler::from_words([0, 0, (1 << 26) | (1 << 22) | (2 << 20), 0]); + assert_eq!(mixed.filter, 0x54); + } + + #[test] + fn host_filter_falls_back_without_changing_reduction_or_lod() { + for (filter, fallback) in [(0x54, 0x55), (0xD4, 0xD5), (0x154, 0x155), (0x1D4, 0x1D5)] { + for mip in [0u32, 1] { + let s = StaticSampler { + filter, + ..StaticSampler::from_words([0, 0x0054_0100, (mip << 26) | 0x00F0_0000, 0]) + }; + assert_eq!(s.filter_for_host(false), fallback); + assert_eq!(s.filter_for_host(true), filter); + assert_eq!( + (s.min_lod, s.max_lod), + if mip == 0 { (0.0, 0.0) } else { (1.0, 5.25) } + ); + } + } + } + + #[test] + fn host_filter_keeps_other_filters_unchanged() { + for reduction in [0, 0x80, 0x100, 0x180] { + for base in [0, 1, 4, 5, 0x10, 0x11, 0x14, 0x15, 0x55] { + let s = StaticSampler { + filter: base | reduction, + ..StaticSampler::from_words([0; 4]) + }; + assert_eq!(s.filter_for_host(false), s.filter); + assert_eq!(s.filter_for_host(true), s.filter); + } + } + } }