diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index cc3f9954f..820338702 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -4466,7 +4466,10 @@ cache is answered compile-required on both. - a texture argument sampled through a sampler the identity sampling does not take ("a texture argument sampled through a sampler the identity sampling does not take"), and a sampler argument whose minimum level or emulated filter the function specializes ("a sampler argument - whose minimum level or emulated filter the function specializes"): no test yet; + whose minimum level or emulated filter the function specializes"): no test yet. A clamped LOD + query outside a fragment function through a sampler argument is lowered, its level read from + the word of the sampler's resource ID (docs/API.md, Accepted differences): the translator test + `under_the_identity_domains_a_runtime_samplers_query_reads_its_pushed_word`; - other state of a bound texture or sampler beyond its resource ID ("state of a bound texture or sampler beyond its resource ID (F)", `F` the `RuntimeFact`): no test yet; - program-scope constant data past 64 KiB read through memory ("constant data of N bytes read diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index cee99f77f..45dba24b2 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -60,7 +60,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 2346; +pub const SEMANTICS_VERSION: u32 = 2347; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/lower/sampler/emit.rs b/crates/metal2vulkan/src/lower/sampler/emit.rs index 27326809a..2c3bf969c 100644 --- a/crates/metal2vulkan/src/lower/sampler/emit.rs +++ b/crates/metal2vulkan/src/lower/sampler/emit.rs @@ -2007,7 +2007,8 @@ fn metal_offset( /// Metal's answer to a LOD query outside a fragment function: 0 for the unclamped level (`min` /// `None`), and for the clamped one the sampler's minimum level in sixty-fourths bounded by the -/// last level of `si`'s image. +/// last level of `si`'s image; under the identity domains a runtime sampler's from its word +/// ([`MinLevel::Slot`], [`word_min_level`]). fn query_level( cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, @@ -2017,6 +2018,10 @@ fn query_level( let min = match min { None | Some(MinLevel::Const(0)) => return f32_const(fb, 0), Some(MinLevel::Const(v)) => f32_const(fb, (v as f32 / 64.0).to_bits())?, + Some(MinLevel::Slot(n)) if cx.plan.families.access.identity => { + let word = sampler_word(cx, fb, Pixel::Spec(n))?; + word_min_level(fb, word)? + } Some(MinLevel::Slot(n)) => { let Some(v) = cx.decl.families.sampler.min_levels.get(&n).copied() else { return Err(cx.fail(format!("sampler slot {n} has no minimum level"))); @@ -2037,6 +2042,31 @@ fn query_level( fb.glsl(GlslOp::FMin, &[min, last]) } +/// The level (an `f32`) a clamped LOD query outside a fragment function answers through the +/// runtime sampler of word `word` (a `u32`) before the texture's last level bounds it: 0 where the +/// word states no mip filter, else the level a nearest mip filter reads at its `lodMinClamp` +/// (`SamplerWord::LEVEL_MIN`). The word holds no finer level, so under a linear mip filter with a +/// clamp between whole levels this is not the clamp Metal was measured to answer +/// (`metal::sampler::query_min_level`); MSL leaves the query undefined outside a fragment function +/// (MSL 6.13). +fn word_min_level(fb: &mut FunctionBuilder<'_>, word: SValue) -> BResult { + let u32_const = |fb: &mut FunctionBuilder<'_>, v: u32| -> BResult { + let c = fb.u32_const(v)?; + fb.module().const_value(c) + }; + let shift = u32_const(fb, SamplerWord::LEVEL_MIN_SHIFT)?; + let level = fb.binary(BinOp::ShiftRightLogical, word, shift)?; + let mask = u32_const(fb, SamplerWord::LEVEL_MIN >> SamplerWord::LEVEL_MIN_SHIFT)?; + let level = fb.binary(BinOp::BitwiseAnd, level, mask)?; + let mip = u32_const(fb, SamplerWord::MIP_FILTER)?; + let mip = fb.binary(BinOp::BitwiseAnd, word, mip)?; + let zero = u32_const(fb, 0)?; + let none = fb.binary(BinOp::IEqual, mip, zero)?; + let level = fb.select(none, zero, level)?; + let f = f32_ty(fb)?; + fb.convert(ConvOp::ConvertUToF, level, f) +} + /// The `f32` constant with bits `bits`. fn f32_const(fb: &mut FunctionBuilder<'_>, bits: u32) -> BResult { let f = f32_ty(fb)?; diff --git a/crates/metal2vulkan/src/lower/sampler/mod.rs b/crates/metal2vulkan/src/lower/sampler/mod.rs index 30c5b4514..ce8f7c8cd 100644 --- a/crates/metal2vulkan/src/lower/sampler/mod.rs +++ b/crates/metal2vulkan/src/lower/sampler/mod.rs @@ -560,7 +560,12 @@ pub const OFFSET_ARM_WORDS: u32 = 24; pub enum MinLevel { /// A constexpr sampler's, known when the function is compiled. Const(u32), - /// The runtime sampler of slot `n`'s, its `SpecKey::SamplerMinLevel` specialization. + /// The runtime sampler of slot `n`'s, its `SpecKey::SamplerMinLevel` specialization, or under + /// the identity domains 0 where the word of its pushed resource ID states no mip filter, else + /// the whole level a nearest mip filter reads at its `lodMinClamp` (`SamplerWord::LEVEL_MIN`): + /// Metal's answer under a nearest mip filter and none, and under a linear one where the clamp + /// is a whole level or past the texture's last level. MSL leaves the query undefined outside a + /// fragment function (MSL 6.13). Slot(u32), } @@ -1119,7 +1124,8 @@ pub struct RuntimeSlot { /// A cube's explicit gradients take their footprint by its word (`CubeFootprint::Word`): the /// function reads whether its word is anisotropic. pub anisotropy: bool, - /// A clamped LOD query outside a fragment function reads it ([`SampleOp::QueryLevel`]), or an + /// A clamped LOD query outside a fragment function reads it ([`SampleOp::QueryLevel`]) other + /// than under the identity domains, where the query reads its word ([`MinLevel::Slot`]), or an /// extended-range sample whose linear mip filter blends two levels (`extended::Level::Word`): /// the function reads its `SpecKey::SamplerMinLevel` specialization. pub min_level: bool, diff --git a/crates/metal2vulkan/src/lower/sampler/select.rs b/crates/metal2vulkan/src/lower/sampler/select.rs index e50b746e0..e32b89666 100644 --- a/crates/metal2vulkan/src/lower/sampler/select.rs +++ b/crates/metal2vulkan/src/lower/sampler/select.rs @@ -224,7 +224,8 @@ fn query_level_measured(cx: &SelectCx<'_>, shape: ImageShape, integer: bool) -> /// The level a clamped LOD query outside a fragment function answers through `stat` before the /// texture's last level bounds it: a constexpr sampler's from its state, a runtime slot's by its -/// `SamplerMinLevel` specialization. An argument buffer's sampler states its clamp in no table the +/// `SamplerMinLevel` specialization, or under the identity domains by the word of its pushed +/// resource ID ([`MinLevel::Slot`]). An argument buffer's sampler states its clamp in no table the /// function reads, so its query is not implemented. fn query_min_level( cx: &mut SelectCx<'_>, @@ -237,13 +238,16 @@ fn query_min_level( Some((SamplerRef::Constexpr(words), _)) => Ok(MinLevel::Const( super::constexpr::query_min_level(&super::state_of(words)?)?, )), - Some((SamplerRef::Slot(n), _)) => match cx.families.sampler.slots.get_mut(&n) { - Some(s) => { - s.min_level = true; - Ok(MinLevel::Slot(n)) + Some((SamplerRef::Slot(n), _)) => { + let identity = crate::lower::access::identity(cx.profile); + match cx.families.sampler.slots.get_mut(&n) { + Some(s) => { + s.min_level |= !identity; + Ok(MinLevel::Slot(n)) + } + None => Err(cx.invariant("an unplanned sampler slot")), } - None => Err(cx.invariant("an unplanned sampler slot")), - }, + } None => Err(Unsupported::not_implemented(Feature::ImplicitLodOutsideFragment).into()), } } diff --git a/docs/API.md b/docs/API.md index d2887eff2..751505ddc 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3057,6 +3057,14 @@ named states the rule in full. 0xcc)` through an `RGBA8Unorm` view of `(Blue, Green, Red, Alpha)` leaves `(0xb3, 0x66, 0x33, 0xcc)`, `crates/metal2vulkan-conformance/probes/swizzled_view_stores.m`) and Metal's validation refuses the write, the view reporting no `ShaderWrite` usage. +- **A kernel's or vertex function's `calculate_clamped_lod` through a sampler argument answers + from the sampler's resource ID word** under the identity domains (the runtime): 0 where the + sampler has no mip filter, else the level a nearest mip filter reads at its `lodMinClamp`, + bounded by the texture's last level; `calculate_unclamped_lod` there answers 0. The MSL + specification leaves both undefined outside a fragment function (section 6.13, "otherwise, the + behavior is undefined"). An Apple M3 Max and an M4 were measured to answer the same, except + under a linear mip filter with an `lodMinClamp` between whole levels, where they answer the + clamp truncated onto sixty-fourths of a level (0.3 reads 19/64). **Thread memory**