From f5b7d060eaaabef2bf6cc7114b2a579c78f36fcf Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 1 Sep 2026 04:17:59 +0300 Subject: [PATCH] Hold both sources of sampler state to one rule Vulkan has no unnormalized-coordinate sampler with Metal's semantics, so a `coord::pixel` sampler is emulated with shader-side fetches at level zero. Anything the fetch cannot express has to fail translation rather than be silently dropped -- the module would sample the right texture with the wrong filter, anisotropy or border, and validate either way. Sampler state reaches that lowering from two places, and only one of them checked. `RuntimeSamplerState::validate` refused caller-supplied state it could not reproduce; AIR's constexpr encoding went straight to the lowering unchecked. Two copies of a rule drift, and the copy that existed was wrong: it demanded both LOD clamps be exactly zero. Only the minimum can matter. The fetch reads level zero, so a minimum above zero excludes the level the emulation reads, while a maximum cannot -- validation already requires it to be at least the minimum. Metal's default maximum is 65504, the half-precision limit AIR encodes "unclamped" as, and 531 of the 535 pixel-coordinate static samplers over 2880 corpus sources carry exactly that. A caller passing the state its own shaders use got a FALLBACK; the AIR path took the identical state without a word. The constraints now live on `StaticSamplerState`, which is what the lowering actually consumes, and both paths call them. Over the corpus every pixel sampler passes -- all 535 are nearest, unmipped, unaniso, unbiased, weighted average, minimum clamp zero -- so no module changes status and none changes by a byte. --- CHANGELOG.md | 8 +++ src/native/tests/textures.rs | 55 +++++++++++++++- src/passes/stage_output/mod.rs | 6 ++ src/reflect/mod.rs | 112 +++++++++++++++++++-------------- 4 files changed, 133 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 050c69f68..0971dd14b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `with_runtime_sampler` accepts a pixel-coordinate sampler whose LOD maximum is Metal's default. + The emulation fetches level zero, so only a *minimum* above zero can exclude the level it reads -- + a maximum never can, since validation already requires it to be at least the minimum. Demanding + both be zero refused the ordinary case: 531 of the 535 pixel-coordinate static samplers across + 2880 corpus sources carry a maximum of 65504, the half-precision limit AIR encodes "unclamped" as. + The emulation constraints now live on `StaticSamplerState`, the type the lowering consumes, and + the AIR constexpr-sampler path applies them too -- it previously applied none, so a static sampler + the emulation cannot reproduce was lowered anyway. - A vertex function carrying an `air.patch` node the decoder cannot read is refused instead of being emitted as an ordinary vertex shader. The node states a post-tessellation evaluation shader's domain and control-point count; dropping it dropped the `Quads`/`Triangles`/`Isolines`, diff --git a/src/native/tests/textures.rs b/src/native/tests/textures.rs index 5c6933fa0..d8139df4b 100644 --- a/src/native/tests/textures.rs +++ b/src/native/tests/textures.rs @@ -1616,10 +1616,11 @@ fn runtime_pixel_sampler_rejects_unmodeled_pipeline_state() { ), ( crate::reflect::RuntimeSamplerState { - lod_max_clamp: 1.0, + lod_min_clamp: 1.0, + lod_max_clamp: 2.0, ..base }, - "LOD clamps", + "minimum LOD clamp", ), ( crate::reflect::RuntimeSamplerState { @@ -1642,6 +1643,56 @@ fn runtime_pixel_sampler_rejects_unmodeled_pipeline_state() { assert!(error.contains("range 0..16"), "{error}"); } +/// Metal's default LOD maximum is the half-precision limit, not zero, and the fetch the emulation +/// performs reads level zero either way -- a maximum that is never below the minimum cannot exclude +/// it. Refusing this refuses the state 531 of the corpus's 535 pixel-coordinate static samplers +/// carry, and refuses it only from the caller: the AIR path accepted the identical state. +#[test] +fn runtime_pixel_sampler_accepts_the_default_lod_maximum() { + let base = runtime_sampler_state( + crate::reflect::SamplerCoordinates::Pixel, + crate::reflect::SamplerFilter::Nearest, + crate::reflect::SamplerAddressMode::ClampToEdge, + ); + for lod_max_clamp in [0.0, 1.0, 65504.0] { + passes::TransformOptions::default() + .with_runtime_sampler( + 0, + crate::reflect::RuntimeSamplerState { + lod_max_clamp, + ..base + }, + ) + .unwrap_or_else(|error| { + panic!("a maximum LOD of {lod_max_clamp} cannot exclude level zero: {error}") + }); + } +} + +/// AIR-encoded constexpr sampler state answers to the same rules as caller-supplied state. The +/// words are a `coord::pixel`, `filter::nearest`, `mip_filter::none` sampler taken verbatim from a +/// corpus module -- the ordinary shape, carrying Metal's half-precision default LOD maximum. +#[test] +fn air_static_pixel_sampler_with_mixed_filters_is_refused() { + let state = crate::reflect::StaticSamplerState::from_air_words([34901797601050624, 0]) + .expect("decode AIR sampler words"); + assert_eq!(state.coordinates, crate::reflect::SamplerCoordinates::Pixel); + let mixed = crate::reflect::StaticSamplerState { + mag_filter: crate::reflect::SamplerFilter::Linear, + min_filter: crate::reflect::SamplerFilter::Nearest, + ..state + }; + let error = mixed + .validate_lowering() + .expect_err("mixed min/mag filters have no pixel-coordinate lowering"); + assert!(error.contains("mixed min/mag"), "{error}"); + // The decoded state itself is reproducible, including its half-precision default LOD maximum. + state + .validate_lowering() + .expect("a corpus-shaped constexpr sampler lowers"); + assert_eq!(state.lod_max_clamp, 65504.0); +} + #[test] fn runtime_sampler_specialization_uses_fragment_metal_index_not_descriptor_binding() { let ll = r#" diff --git a/src/passes/stage_output/mod.rs b/src/passes/stage_output/mod.rs index 349447ac2..e9c1644d1 100644 --- a/src/passes/stage_output/mod.rs +++ b/src/passes/stage_output/mod.rs @@ -994,9 +994,15 @@ pub(in crate::passes) fn handle_static_sampler(ctx: &mut Ctx) -> Result<(), Stri let pptr = ctx.ty_ptr(StorageClass::UniformConstant, sty); for (old_var, _) in samp_globals { + // The same emulation rules the caller-supplied path enforces, on the state AIR encoded. + // Without this an unreproducible constexpr sampler is lowered anyway, and the module + // samples the right texture with the wrong filter, anisotropy or border. let sampler_state = static_sampler_words(ctx, old_var) .map(StaticSamplerState::from_air_words) .transpose()?; + if let Some(state) = sampler_state { + state.validate_lowering()?; + } // One sampler resource per static sampler. let new_var = ctx.module.fresh_id(); ctx.new_globals.push(Instruction::new( diff --git a/src/reflect/mod.rs b/src/reflect/mod.rs index 301993b40..9ac515a26 100644 --- a/src/reflect/mod.rs +++ b/src/reflect/mod.rs @@ -920,6 +920,9 @@ pub struct RuntimeSamplerState { } impl RuntimeSamplerState { + /// Reject pipeline-provided state the sampler lowering cannot reproduce, before translation + /// mutates anything. The emulation constraints live on [`StaticSamplerState`] -- the type the + /// lowering actually consumes -- so caller-supplied and AIR-encoded state answer to one rule. pub(crate) fn validate(self) -> Result<(), String> { if self.max_anisotropy == 0 { return Err("runtime sampler max_anisotropy must be at least 1".into()); @@ -936,52 +939,7 @@ impl RuntimeSamplerState { self.lod_min_clamp, self.lod_max_clamp )); } - if self.coordinates == SamplerCoordinates::Pixel { - if self.min_filter != self.mag_filter { - return Err( - "pixel-coordinate runtime samplers with mixed min/mag filters are unsupported because AIR does not expose the pipeline derivative state needed to select the filter" - .into(), - ); - } - if self.mip_filter == SamplerMipFilter::Linear { - return Err( - "pixel-coordinate runtime samplers with linear mip filtering are unsupported" - .into(), - ); - } - if self.max_anisotropy > 1 { - return Err( - "pixel-coordinate runtime sampler anisotropy cannot be emulated exactly".into(), - ); - } - if self.lod_bias != 0.0 { - return Err( - "pixel-coordinate runtime sampler LOD bias cannot be emulated exactly".into(), - ); - } - if self.lod_min_clamp != 0.0 || self.lod_max_clamp != 0.0 { - return Err("pixel-coordinate runtime sampler LOD clamps must both be zero".into()); - } - if self.reduction != SamplerReduction::WeightedAverage { - return Err( - "pixel-coordinate runtime sampler min/max reduction is unsupported".into(), - ); - } - if self.border_color != SamplerBorderColor::TransparentBlack - && [ - self.address_mode_s, - self.address_mode_t, - self.address_mode_r, - ] - .contains(&SamplerAddressMode::ClampToBorder) - { - return Err( - "pixel-coordinate runtime samplers support only transparent-black border emulation" - .into(), - ); - } - } - Ok(()) + self.lowering_state().validate_lowering() } /// Project pipeline-provided state into the sampler-lowering representation shared by the @@ -1247,6 +1205,68 @@ impl StaticSamplerState { }) } + /// Reject state the sampler lowering cannot reproduce exactly. + /// + /// Vulkan has no unnormalized-coordinate sampler with Metal's semantics, so a + /// `coord::pixel` sampler is emulated with shader-side image fetches at level zero. Anything + /// the fetch cannot express has to fail the translation rather than be silently dropped: the + /// emitted module would sample the right texture with the wrong filter, the wrong anisotropy, + /// or the wrong border, and validate either way. + /// + /// Both sources of sampler state answer to this: pipeline-provided state through + /// [`RuntimeSamplerState::validate`], and AIR's constexpr encoding where the static sampler is + /// lowered. Two copies of these rules drift, and they had -- the caller's copy refused a LOD + /// maximum the AIR path accepted 531 times over the corpus. + pub(crate) fn validate_lowering(self) -> Result<(), String> { + if self.coordinates != SamplerCoordinates::Pixel { + return Ok(()); + } + if self.min_filter != self.mag_filter { + return Err( + "pixel-coordinate samplers with mixed min/mag filters are unsupported because AIR does not expose the pipeline derivative state needed to select the filter" + .into(), + ); + } + if self.mip_filter == SamplerMipFilter::Linear { + return Err( + "pixel-coordinate samplers with linear mip filtering are unsupported".into(), + ); + } + if self.max_anisotropy > 1 { + return Err("pixel-coordinate sampler anisotropy cannot be emulated exactly".into()); + } + if self.lod_bias != 0.0 { + return Err("pixel-coordinate sampler LOD bias cannot be emulated exactly".into()); + } + // Only the minimum matters. The fetch reads level zero, so a minimum above zero excludes + // the level the emulation reads; a maximum cannot, since it is never below the minimum. + // Metal's default maximum is the half-precision limit rather than zero -- 531 of the 535 + // pixel-coordinate static samplers in the corpus carry exactly that -- so demanding zero + // rejected the ordinary case. + if self.lod_min_clamp != 0.0 { + return Err(format!( + "pixel-coordinate sampler minimum LOD clamp {} excludes the level zero the emulation reads", + self.lod_min_clamp + )); + } + if self.reduction != SamplerReduction::WeightedAverage { + return Err("pixel-coordinate sampler min/max reduction is unsupported".into()); + } + if self.border_color != SamplerBorderColor::TransparentBlack + && [ + self.address_mode_s, + self.address_mode_t, + self.address_mode_r, + ] + .contains(&SamplerAddressMode::ClampToBorder) + { + return Err( + "pixel-coordinate samplers support only transparent-black border emulation".into(), + ); + } + Ok(()) + } + pub(crate) fn uses_pixel_nearest(self) -> bool { self.uses_pixel_coordinates() && self.min_filter == SamplerFilter::Nearest