From 52113199eecc6a6b0b00a8c394fb30247f80d8b6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 22:18:40 -0800 Subject: [PATCH] Translator: the shared bicubic switch reads each arm's image and format bits under the identity domains A switch-at-use whose arms all take a bicubic sample (an array of textures, or textures chosen by a function constant, sampled through one bicubic constexpr sampler) emulates once: a facts dispatch yields the named candidate's size and format bits, and a taps dispatch reads its texels. Both read each arm's combined binding and its BicubicTexels specialization, which the identity domains do not declare, so every such switch under the runtime's domains failed with "internal error: combined binding ... is not declared". Under the identity domains an arm's image is now its texture slot's element of the set-1 array at the index its pushed resource ID holds (emit::identity_image, the image half of the sampled image a candidate's own sample makes), and its bits the slot's PushField::BicubicTexels, which the plan already pushed (bicubic::format_bits, the one reading of a slot's bits the own and the shared emulation share). The resident-table form is unchanged. SEMANTICS 2278 (with the pixel-coordinate form that lands beside it): modules that failed now translate. --- crates/metal2vulkan/src/cache/mod.rs | 2 +- .../metal2vulkan/src/lower/sampler/bicubic.rs | 42 +++++++----- crates/metal2vulkan/src/lower/sampler/emit.rs | 31 +++++---- crates/metal2vulkan/src/lower/sampler/mod.rs | 11 +++ .../metal2vulkan/src/lower/sampler/shared.rs | 68 +++++++++++-------- 5 files changed, 95 insertions(+), 59 deletions(-) diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 818484186..dedd84aab 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 = 2277; +pub const SEMANTICS_VERSION: u32 = 2278; /// 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/bicubic.rs b/crates/metal2vulkan/src/lower/sampler/bicubic.rs index 1bc985200..6f3272a93 100644 --- a/crates/metal2vulkan/src/lower/sampler/bicubic.rs +++ b/crates/metal2vulkan/src/lower/sampler/bicubic.rs @@ -1254,6 +1254,30 @@ pub(super) fn word_field( fb.binary(BinOp::BitwiseAnd, f, m) } +/// The bound format's treatment of texture slot `texture` under the bicubic filter: the slot's +/// `BicubicTexels` specialization, or under the identity domains its push field, which the +/// consumer writes from the bound texture, read in the open block. +pub(super) fn format_bits( + cx: &mut EmitCx<'_>, + fb: &mut FunctionBuilder<'_>, + texture: u32, +) -> BResult { + if let Some(&spec) = cx.decl.families.sampler.bicubic.get(&texture) { + return Ok(Bits::Spec(spec)); + } + if !cx.plan.families.access.identity { + return Err(cx.fail(format!( + "texture slot {texture} has no bicubic specialization" + ))); + } + let Some(slot) = cx.plan.families.texture.slots.get(&texture) else { + return Err(cx.fail(format!("texture slot {texture} is not planned"))); + }; + Ok(Bits::Value( + cx.push_field(fb, PushField::BicubicTexels(slot.slot))?, + )) +} + /// The `vec4` texel the bicubic sample `s` of sampled image `si` (image facts `image`) reads /// (module docs), its `half` result the device's narrowing of the `f32` sum. pub(super) fn emit( @@ -1264,23 +1288,7 @@ pub(super) fn emit( image: CombinedImage, b: &Bicubic, ) -> BResult { - let bits = match cx.decl.families.sampler.bicubic.get(&b.texture) { - Some(&spec) => Bits::Spec(spec), - // Under the identity domains the slot's push field, which the consumer writes from the - // bound texture. - None if cx.plan.families.access.identity => { - let Some(slot) = cx.plan.families.texture.slots.get(&b.texture) else { - return Err(cx.fail(format!("texture slot {} is not planned", b.texture))); - }; - Bits::Value(cx.push_field(fb, PushField::BicubicTexels(slot.slot))?) - } - None => { - return Err(cx.fail(format!( - "texture slot {} has no bicubic specialization", - b.texture - ))) - } - }; + let bits = format_bits(cx, fb, b.texture)?; let half = half_result(cx, s)?; let t = Tys::new(fb)?; let span = b.span(); diff --git a/crates/metal2vulkan/src/lower/sampler/emit.rs b/crates/metal2vulkan/src/lower/sampler/emit.rs index 587afccd9..27326809a 100644 --- a/crates/metal2vulkan/src/lower/sampler/emit.rs +++ b/crates/metal2vulkan/src/lower/sampler/emit.rs @@ -1381,15 +1381,14 @@ fn sampler_id(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, n: u32) -> BRes cx.push_field(fb, crate::pipeline::sym::PushField::SlotGuestBase(slot)) } -/// Under the identity domains, the sampled image of combined binding `key` -/// ([`super::IdentitySampling`]): the texture slot's element of its class's set-1 array at the -/// index its pushed resource ID holds, and its sampler ([`identity_sampler`]), joined by -/// `OpSampledImage`. -fn identity_sampled_image( +/// Under the identity domains, the image of combined binding `key`, loaded in the open block: the +/// texture slot's element of its class's set-1 array at the index its pushed resource ID holds, +/// with the binding's identity sampling ([`super::IdentitySampling`]). +pub(super) fn identity_image( cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, key: super::BindingKey, -) -> BResult<(SValue, CombinedImage)> { +) -> BResult<(SValue, super::IdentitySampling)> { let Some(i) = cx.plan.families.sampler.identity.get(&key).copied() else { return Err(cx.fail(format!("combined binding {key:?} has no identity sampling"))); }; @@ -1409,16 +1408,20 @@ fn identity_sampled_image( let base = fb.module().global_value(images)?; let p = fb.access_chain(base, &[element])?; let img = fb.load(p, MemAccess::default())?; + Ok((img, i)) +} + +/// Under the identity domains, the sampled image of combined binding `key`: its image +/// ([`identity_image`]) and its sampler ([`identity_sampler`]), joined by `OpSampledImage`. +fn identity_sampled_image( + cx: &mut EmitCx<'_>, + fb: &mut FunctionBuilder<'_>, + key: super::BindingKey, +) -> BResult<(SValue, CombinedImage)> { + let (img, i) = identity_image(cx, fb, key)?; let sampler = identity_sampler(cx, fb, key.sampler, i.use_)?; let si = fb.sampled_image(img, sampler)?; - Ok(( - si, - CombinedImage { - plane: super::plane_of(i.image.dim), - arrayed: i.image.arrayed, - texel: i.image.texel, - }, - )) + Ok((si, i.combined())) } /// Under the identity domains, sampler `sampler` in use `use_`: the element of the set-1 sampler diff --git a/crates/metal2vulkan/src/lower/sampler/mod.rs b/crates/metal2vulkan/src/lower/sampler/mod.rs index c72dcade8..7f9bc8675 100644 --- a/crates/metal2vulkan/src/lower/sampler/mod.rs +++ b/crates/metal2vulkan/src/lower/sampler/mod.rs @@ -1253,6 +1253,17 @@ pub struct IdentitySampling { pub use_: SamplerUse, } +impl IdentitySampling { + /// The image facts emission reads of [`IdentitySampling::image`]. + pub fn combined(&self) -> CombinedImage { + CombinedImage { + plane: plane_of(self.image.dim), + arrayed: self.image.arrayed, + texel: self.image.texel, + } + } +} + /// The image facts emission reads for a combined binding. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct CombinedImage { diff --git a/crates/metal2vulkan/src/lower/sampler/shared.rs b/crates/metal2vulkan/src/lower/sampler/shared.rs index e0d778e81..407270f5f 100644 --- a/crates/metal2vulkan/src/lower/sampler/shared.rs +++ b/crates/metal2vulkan/src/lower/sampler/shared.rs @@ -15,6 +15,10 @@ //! - the emulation on those facts (its footprint, weights and routine calls once), whose taps a //! dispatch reads from each arm's image (the null arm's are zero). //! +//! Under the identity domains an arm's image is its texture slot's element of the set-1 array at +//! the index its pushed resource ID holds, and its bits the slot's `PushField::BicubicTexels`, as +//! a candidate's own sample reads them. +//! //! Where a handle names a candidate the facts are that candidate's, so every value is the one its //! own arm computed; elsewhere the switch's null value is selected. The emulation of a null handle //! runs on a size of ones and zero taps, whose value is discarded. @@ -23,11 +27,11 @@ use crate::lower::emit::EmitCx; use crate::lower::texture::{dispatch, with_word, NullLeaf}; use crate::spirv::builder::FunctionBuilder; use crate::spirv::error::BResult; -use crate::spirv::ids::{ConstId, Value as SValue}; +use crate::spirv::ids::Value as SValue; use crate::spirv::ir::{ConstBits, Constant, ImageQuery, Type as SType}; use super::bicubic::Bicubic; -use super::{Binding, CombinedImage, SampleOp, Sampling, Source}; +use super::{BindingKey, CombinedImage, SampleOp, Sampling, Source}; /// The candidates of a switch-at-use that emulates once: the handles' values dispatched on, /// outermost first, and each arm's case literals and sampling. @@ -51,28 +55,45 @@ impl Candidates<'_> { dispatch(cx, fb, &self.sels, &self.arms, &leaf, null) } - /// The image of arm sampling `a`'s binding, loaded in the open block. + /// The image of arm sampling `a`'s binding, loaded in the open block: its combined binding's, + /// or under the identity domains its texture slot's element of the set-1 array + /// (`emit::identity_image`). pub(super) fn image( &self, cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, a: &Sampling, ) -> BResult { - binding(cx, a)?.0.image(fb) + let key = key(cx, a)?; + if cx.plan.families.access.identity { + return Ok(super::emit::identity_image(cx, fb, key)?.0); + } + match cx.decl.families.sampler.combined.get(&key) { + Some(&(b, _)) => b.image(fb), + None => Err(cx.fail(format!("combined binding {key:?} is not declared"))), + } } } -/// The combined binding sampling `a` reads, and its image facts. -fn binding(cx: &mut EmitCx<'_>, a: &Sampling) -> BResult<(Binding, CombinedImage)> { - let Source::Combined(key) = a.source else { - return Err(cx.fail("a shared sampling of a heap element")); - }; - match cx.decl.families.sampler.combined.get(&key) { - Some(&b) => Ok(b), - None => Err(cx.fail(format!("combined binding {key:?} is not declared"))), +/// The combined binding sampling `a` reads. +fn key(cx: &mut EmitCx<'_>, a: &Sampling) -> BResult { + match a.source { + Source::Combined(key) => Ok(key), + _ => Err(cx.fail("a shared sampling of a heap element")), } } +/// The image facts of the binding sampling `a` reads: its combined binding's, or under the +/// identity domains its identity sampling's. +fn image_facts(cx: &mut EmitCx<'_>, a: &Sampling) -> BResult { + let key = key(cx, a)?; + let image = match cx.plan.families.access.identity { + true => (cx.plan.families.sampler.identity.get(&key)).map(|i| i.combined()), + false => (cx.decl.families.sampler.combined.get(&key)).map(|&(_, image)| image), + }; + image.ok_or_else(|| cx.fail(format!("combined binding {key:?} is not declared"))) +} + /// The bicubic sample every arm of a shared switch takes ([`Sampling::shared_shape`]). fn bicubic(s: &Sampling) -> Option<&Bicubic> { match &s.op { @@ -81,16 +102,13 @@ fn bicubic(s: &Sampling) -> Option<&Bicubic> { } } -/// The specialization holding the bound format's bits of arm sampling `a`'s slot, its bicubic -/// sample's `BicubicTexels`. -fn bits(cx: &mut EmitCx<'_>, a: &Sampling) -> BResult { +/// The bound format's bits of arm sampling `a`'s slot, its bicubic sample's, read in the open +/// block (`bicubic::format_bits`). +fn bits(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, a: &Sampling) -> BResult { let Some(b) = bicubic(a) else { return Err(cx.fail("a shared switch's arm without its bicubic sample")); }; - match cx.decl.families.sampler.bicubic.get(&b.texture) { - Some(&c) => Ok(c), - None => Err(cx.fail("a shared switch's arm without its format specialization")), - } + super::bicubic::format_bits(cx, fb, b.texture)?.value(fb) } /// The value of the shared switch over `c` (module docs): its sampling's value where a handle @@ -107,9 +125,9 @@ pub(crate) fn shared_value( let Some(b) = bicubic(first) else { return Err(cx.fail("a shared switch without an emulation")); }; - let (_, image) = binding(cx, first)?; + let image = image_facts(cx, first)?; for &(_, a) in &c.arms { - if binding(cx, a)?.1 != image { + if image_facts(cx, a)? != image { return Err(cx.fail("a shared switch's candidates of different images")); } } @@ -163,12 +181,8 @@ fn facts( let img = c.image(cx, fb, a)?; let level = fb.module().const_value(zero)?; let size = fb.query(ImageQuery::SizeLod(level), img)?; - let bits = bits(cx, a)?; - Ok(vec![ - size, - fb.module().const_value(bits)?, - fb.module().const_value(yes)?, - ]) + let bits = bits(cx, fb, a)?; + Ok(vec![size, bits, fb.module().const_value(yes)?]) }; let null_facts = |_: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>| { Ok(vec![