Translator: under the identity domains a runtime sampler's LOD query outside a fragment function reads its minimum level from the sampler's word

A clamped calculate_clamped_lod in a kernel or a vertex function through a runtime sampler
argument was refused on every profile under the identity domains ("a sampler argument whose
minimum level or emulated filter the function specializes"): the query read the slot's
SamplerMinLevel specialization, which no binder chooses there.

The query now reads the word of the resource ID the sampler slot already pushes (SlotGuestBase):
0 where the word states no mip filter, else SamplerWord::LEVEL_MIN, the level a nearest mip
filter reads at lodMinClamp, bounded by the texture's last level as before. A bind-time clamp
(setSamplerState:lodMinClamp:lodMaxClamp:) binds a variant whose word is made of the clamped
descriptor, so it is read too. That is the M3 Max's and M4's measured answer
(metal::sampler::query_min_level) for no mip filter and a nearest one, and for a linear one where
lodMinClamp is a whole level or past the texture's last level. Under a linear mip filter with a
clamp between whole levels Metal answers the clamp on its sixty-fourths grid, which the word does
not hold. MSL 4.1 section 6.13 leaves both queries undefined outside a fragment function ("Only
call the calculate_unclamped_lod and calculate_clamped_lod functions from a fragment function or
a function you call with a fragment function; otherwise, the behavior is undefined."). So, per
the planner's ruling, there is no precision note; docs/API.md's accepted differences states the
answer. The resident-table path keeps its specialization.

SEMANTICS_VERSION 2347. The runtime-sources pin does not move: pin-compute.py gives a92fe0a8...
for this tree, the same as before. docs/REFUSALS.md does not move either: the refusal text
remains for an extended-range sample's linear-mip blend. The runtime README's known-gap entry
names the lowered query. Verified by the lane's red/green job, named in the lane's report.

Landed with SEMANTICS_VERSION 2347 (renumbered above development's 2346: written 2343 on its base,
then 2346 at the lane's rebase onto 0f4114c882, which development's HandleUse also took).
This commit is contained in:
Anees Iqbal committed 2026-12-03 06:46:38 -08:00
1 parent c04212227d
commit 5b3bd800c0
6 files changed
+63 -12

No files matched your search

+4 -1
View File
@@ -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
+1 -1
View File
@@ -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
+31 -1
View File
@@ -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<SValue> {
let u32_const = |fb: &mut FunctionBuilder<'_>, v: u32| -> BResult<SValue> {
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<SValue> {
let f = f32_ty(fb)?;
+8 -2
View File
@@ -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,
@@ -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()),
}
}
+8
View File
@@ -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**