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