diff --git a/crates/metal2vulkan-conformance/src/cases/rgb10a2_srgb.rs b/crates/metal2vulkan-conformance/src/cases/rgb10a2_srgb.rs index 95e9bf5bb..d6d4d8da1 100644 --- a/crates/metal2vulkan-conformance/src/cases/rgb10a2_srgb.rs +++ b/crates/metal2vulkan-conformance/src/cases/rgb10a2_srgb.rs @@ -738,8 +738,8 @@ impl Case for Rgb10A2Srgb { } /// What a store case's pass announced on MoltenVK, measured: a pass that loads nothing into - /// the tile, and the `half` fragment's output conversion; and what a sample case's pipeline - /// announced on every venue run, its module's filter of the decoded taps. The read and write + /// the tile, and the `half` fragment's output conversion; and what a linear sample case's + /// pipeline announced on every venue run, its module's filter of the decoded taps. The read and write /// cases name no notice of their own. fn allowances(&self) -> Vec { use metal2vulkan_runtime::Degradation as D; @@ -755,14 +755,15 @@ impl Case for Rgb10A2Srgb { notice_allowance(D::DontCareLoadTiles.code()), notice_allowance(D::HalfOutputFastMathBlend.code()), ]), - // The sample goes through a runtime sampler, whose state the module does not know, so - // the module holds the linear filter of the decoded taps whatever the slot's filter, - // and that filter's sum is the device's arithmetic (`bicubic-filter-arithmetic`, - // announced for the four cases on the M3 Max, the RTX 5080 and lavapipe), as - // `two_plane_samples` declares for its own. - Question::Samples { .. } => { - named.push(notice_allowance(D::BicubicFilterArithmetic.code())) - } + // A linear sample filters the decoded taps in the module, and that filter's sum is the + // device's arithmetic (`bicubic-filter-arithmetic`, announced for the two linear + // cases on the M3 Max, the RTX 5080 and lavapipe), as `two_plane_samples` declares for + // its own. The note follows the word of the sampler bound at the runtime slot, so a + // nearest case, whose sample is the decoded texel, announces none. + Question::Samples { + filter: SamplerMinMagFilter::Linear, + .. + } => named.push(notice_allowance(D::BicubicFilterArithmetic.code())), _ => {} } named diff --git a/crates/metal2vulkan-conformance/src/cases/two_plane_samples.rs b/crates/metal2vulkan-conformance/src/cases/two_plane_samples.rs index fae348620..f1827fef3 100644 --- a/crates/metal2vulkan-conformance/src/cases/two_plane_samples.rs +++ b/crates/metal2vulkan-conformance/src/cases/two_plane_samples.rs @@ -21,10 +21,10 @@ //! of each. The comparison then names the sample, the lane and both values, and the digest stays //! beside the value as a projection of it. //! -//! Every result compares exactly. The cases declare one notice of the runtime's: an +//! Every result compares exactly. The linear cases declare one notice of the runtime's: an //! extended-range texture's linear filter blends its decoded taps in the device's arithmetic -//! (`bicubic-filter-arithmetic`), which every case's module holds (its runtime sampler may state a -//! linear filter), and which moves a linear case's values (`known-gaps.tsv`). +//! (`bicubic-filter-arithmetic`), which moves their values (`known-gaps.tsv`). The note follows +//! the word of the sampler bound at the runtime slot, so a nearest case announces none. //! //! Nothing here can fault a GPU: two compute passes a case, one texture, three buffers, no loop. //! The coordinates do reach outside the texture, which is what the address modes are here for and @@ -383,16 +383,18 @@ impl Case for TwoPlaneSamples { fn allowances(&self) -> Vec { // Declarations and no tolerance: the buffer each pass writes its samples into is `Shared`, - // which is what [`crate::replay::shared_memory_notice`] is for, and the module's linear - // filter of the decoded taps is the device's arithmetic (`bicubic-filter-arithmetic`, - // announced on the M3 Max for all sixteen cases). Each loosens the notices site alone, so - // every value this case reads still compares exactly. - vec![ - crate::replay::shared_memory_notice(), - crate::replay::notice_allowance( + // which is what [`crate::replay::shared_memory_notice`] is for, and a linear case's filter + // of the decoded taps is the device's arithmetic (`bicubic-filter-arithmetic`, announced on + // the M3 Max for the eight linear cases; a nearest sampler's word holds no such note). + // Each loosens the notices site alone, so every value this case reads still compares + // exactly. + let mut named = vec![crate::replay::shared_memory_notice()]; + if self.state.starts_with('l') { + named.push(crate::replay::notice_allowance( metal2vulkan_runtime::Degradation::BicubicFilterArithmetic.code(), - ), - ] + )); + } + named } fn shaders(&self) -> Result, String> { diff --git a/crates/metal2vulkan-validation/src/runtime_lane/rgb10a2_srgb.rs b/crates/metal2vulkan-validation/src/runtime_lane/rgb10a2_srgb.rs index 98dc7f725..e9cc31e57 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/rgb10a2_srgb.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/rgb10a2_srgb.rs @@ -373,6 +373,8 @@ fn kernels_read_write_and_sample_rgb10a2_srgb_as_metal_does() { let coords = buffer_with(device, &coords).unwrap(); // Metal's nearest `float` samples, which its `half` ones narrow. let mut nearest_floats = Vec::new(); + let note = Degradation::from_precision_note(PrecisionNote::BicubicFilterArithmetic); + let mut announced = counts(&messages.take_notices()); for (linear, filter) in [SamplerMinMagFilter::Nearest, SamplerMinMagFilter::Linear] .into_iter() .enumerate() @@ -483,6 +485,18 @@ fn kernels_read_write_and_sample_rgb10a2_srgb_as_metal_does() { )); } } + // A nearest sampler's sample is the decoded texel at Metal's position: the filter's + // note follows the bound sampler's word, so the nearest samples announce none. + let samples = counts(&messages.take_notices()); + if linear == 0 { + assert!( + !samples.contains_key(¬e), + "the nearest samples announced bicubic-filter-arithmetic: {samples:?}" + ); + } + for (kind, n) in samples { + *announced.entry(kind).or_insert(0) += n; + } } for line in &wrong { println!("RGB10A2-SRGB-DIFFERS {}: {line}", device.name()); @@ -497,9 +511,7 @@ fn kernels_read_write_and_sample_rgb10a2_srgb_as_metal_does() { "{} answers differ from Metal's", wrong.len() ); - let announced = counts(&messages.take_notices()); assert_counted(&announced, moved, "the kernels' reads, writes and samples"); - let note = Degradation::from_precision_note(PrecisionNote::BicubicFilterArithmetic); assert!( filtered == 0 || announced.contains_key(¬e), "{filtered} linear sample lanes apart, bicubic-filter-arithmetic unannounced" diff --git a/crates/metal2vulkan/src/lower/sampler/linear.rs b/crates/metal2vulkan/src/lower/sampler/linear.rs index b36d5f938..2688cd0be 100644 --- a/crates/metal2vulkan/src/lower/sampler/linear.rs +++ b/crates/metal2vulkan/src/lower/sampler/linear.rs @@ -113,8 +113,8 @@ pub(super) fn filter_note( /// The tests of the word of the sampler bound at runtime sampler slot `n` (`NoteOn::BoundSampler`, /// `SamplerWord`) of which one holds exactly where a sample through it filters, [`unfiltered`]'s /// rule read from the word: a linear minification or magnification filter, anisotropy or a cubic -/// filter (any of their bits set), or a linear mip filter. -fn filtering_word(n: u32) -> [NoteTest; 2] { +/// filter (any of their bits set), or a linear mip filter (the second test). +pub(super) fn filtering_word(n: u32) -> [NoteTest; 2] { let on = NoteOn::BoundSampler(n); let filters = SamplerWord::MIN_LINEAR | SamplerWord::MAG_LINEAR diff --git a/crates/metal2vulkan/src/lower/sampler/select.rs b/crates/metal2vulkan/src/lower/sampler/select.rs index 572052815..4da8d4e57 100644 --- a/crates/metal2vulkan/src/lower/sampler/select.rs +++ b/crates/metal2vulkan/src/lower/sampler/select.rs @@ -644,9 +644,21 @@ fn heap_raw_sample( } // Every sample takes the `float` routine, a `half` one the `half` routine too. crate::lower::texture::heap_raw::plan(cx, group, &[false, half], m)?; - // A linear filter's sum of the decoded taps is the device's arithmetic (`extended::filtered`). - if gather.is_none() && !matches!(state, State::Constant { linear: false, .. }) { - cx.precision.insert(PrecisionNote::BicubicFilterArithmetic); + // A linear filter's sum of the decoded taps is the device's arithmetic (`extended::filtered`): + // a constexpr sampler's that filters, and a runtime sampler slot's where the word bound there + // filters (`linear::filtering_word`), as a word that reads one texel takes that texel alone. + if gather.is_none() { + let note = PrecisionNote::BicubicFilterArithmetic; + match state { + State::Constant { linear: false, .. } => {} + State::Constant { .. } => { + cx.precision.insert(note); + } + State::Slot { sampler: n } => { + let when = any_of(super::linear::filtering_word(n)); + cx.note_when(note, when, m)?; + } + } } Ok(Some(HeapRaw { state, @@ -1612,9 +1624,21 @@ fn select_extended( cx.families.arith.converts(form); let filters = !form.bfloat() && gather.is_none(); // A linear filter's sum of the decoded taps is the device's arithmetic, as a bicubic - // sample's is (its 2D routine), not Metal's datapath. - if filters && !matches!(state, State::Constant { linear: false, .. }) { - cx.precision.insert(PrecisionNote::BicubicFilterArithmetic); + // sample's is (its 2D routine), not Metal's datapath: a constexpr sampler's that filters, and + // a runtime sampler slot's where the word bound there filters (`linear::filtering_word`), as a + // word that reads one texel of one level takes that texel alone. + if filters { + let note = PrecisionNote::BicubicFilterArithmetic; + match state { + State::Constant { linear: false, .. } => {} + State::Constant { .. } => { + cx.precision.insert(note); + } + State::Slot { sampler: n } => { + let when = any_of(super::linear::filtering_word(n)); + cx.note_when(note, when, m)?; + } + } } if cubic.is_some() { cx.families @@ -1623,14 +1647,22 @@ fn select_extended( .insert(super::bicubic::routines::Routine::Plane); cx.precision.insert(PrecisionNote::BicubicFilterArithmetic); } - // A linear mip filter's blend of two levels (not a 3D texture's). - let blends = match level { - Level::Base => false, - Level::Stated { stated, .. } => stated.linear, - Level::Word { .. } => form.blends_levels() && !volume, - }; - if blends { - cx.precision.insert(PrecisionNote::BicubicFilterArithmetic); + // A linear mip filter's blend of two levels (not a 3D texture's): a runtime sampler slot's + // where the word bound there states one. + match level { + Level::Base => {} + Level::Stated { stated, .. } => { + if stated.linear { + cx.precision.insert(PrecisionNote::BicubicFilterArithmetic); + } + } + Level::Word { sampler, .. } => { + if form.blends_levels() && !volume { + let [_, linear_mip] = super::linear::filtering_word(sampler); + let when = any_of([linear_mip]); + cx.note_when(PrecisionNote::BicubicFilterArithmetic, when, m)?; + } + } } let coord = ops .get(TexRole::Coord) diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 55007fda1..adde1370a 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -877,7 +877,12 @@ pub enum PrecisionNote { /// few grid steps (`1/4080` at 8 bits), by the weights' distance times the taps (`2.1e-3` at /// most on an extended-range `_sRGB` texture's taps), and by up to `1/64` of the two levels' /// difference under a linear mip filter. A tap of an 8-bit sRGB texture held as its UNORM twin - /// is the device's decode of its code ([`PrecisionNote::SrgbTexelDecode`]). + /// is the device's decode of its code ([`PrecisionNote::SrgbTexelDecode`]). Through a runtime + /// sampler slot the note is specialized on the word of the sampler bound there + /// (`SpecKey::BoundSamplerWord`, which the runtime reads at each draw or dispatch): it holds + /// where that word filters (a linear minification or magnification filter, anisotropy, a cubic + /// filter or a linear mip filter), and a nearest word's sample, the decoded texel at Metal's + /// position, holds none. /// /// Inferences, taken by family: the signed clamp's lower bound, the treatment of the formats /// no case reads (`RG8UnormSRGB`, `RG8Unorm`, `RG8Snorm`, `RG16Unorm`, `BGRA8Unorm`, diff --git a/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs b/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs index 40c8d6fa7..71d1e99ac 100644 --- a/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs +++ b/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs @@ -469,6 +469,99 @@ fn heap_decodes_record_the_device_pack_conversion() { } } +/// A heap sample the function emulates (`sampler::heap_raw`) records the filter's note +/// (`PrecisionNote::BicubicFilterArithmetic`, the device's sum of the decoded taps) as a slot's +/// sample does, on every committed profile with a sampled heap: through a runtime sampler slot +/// only where the word of the sampler bound there filters (`SpecKey::BoundSamplerWord`: its +/// clauses hold for a linear word and not for a nearest one); through a constexpr linear sampler +/// whatever is bound; and through a constexpr nearest sampler, or as a gather, not at all. +#[test] +fn a_heap_sample_s_filter_note_follows_the_bound_sampler_word() { + use crate::bind::SamplerDescriptor; + use crate::metal::sampler::SamplerWord; + use crate::metal::SamplerMinMagFilter as F; + use crate::pipeline::{PrecisionNote, SpecClause}; + let note = PrecisionNote::BicubicFilterArithmetic; + let sampler = SlotRef { + stage: Stage::Kernel, + class: SlotClass::Sampler, + index: 0, + }; + let word = |filter| { + let mut d = SamplerDescriptor::new(); + (d.min_filter, d.mag_filter) = (filter, filter); + SamplerWord::of(&d).0 + }; + let holds = |clause: &SpecClause, word: u32| { + clause.all.iter().all(|t| { + assert_eq!(t.key, SpecKey::BoundSamplerWord(sampler), "{clause:?}"); + ((word & t.mask) == t.bits) == t.equal + }) + }; + let consts = FunctionConstantValues::defaults(); + let entry = EntryRef::Name("k"); + let inputs = LowerInputs::new(); + // (the sampling, whether it records the note, whether its note tests the bound word). + let cases = [ + (Sampling::Runtime, true, true), + (Sampling::Sample(words(true, false)), true, false), + (Sampling::HalfSample(words(true, false)), true, false), + (Sampling::Sample(words(false, false)), false, false), + (Sampling::Gather(words(true, false)), false, false), + ]; + for (s, recorded, bound) in cases { + let Some(bc) = assemble("heap_filter_note", &sampling_kernel(s)) else { + return; + }; + let mut compiled = 0; + for (stem, p) in committed_profiles().expect("profiles") { + let what = format!("{s:?} on {stem}"); + let a = match compile_bitcode( + &bc, + entry, + &consts, + &p, + &inputs, + &mut meter(), + SPIRV_WORDS, + ) { + Ok(a) => a, + Err(Error::Unsupported(Unsupported { + feature: Feature::ArgumentBufferTextures, + .. + })) => continue, + Err(e) => panic!("{what}: {e}"), + }; + assert_eq!( + a.precision.contains(¬e), + recorded, + "{what}: {:?}", + a.precision + ); + let when: Vec<&SpecClause> = (a.specialized.iter()) + .filter(|s| s.note == note) + .flat_map(|s| &s.when) + .collect(); + assert_eq!(!when.is_empty(), bound, "{what}: {when:?}"); + if bound { + assert!( + when.iter().any(|c| holds(c, word(F::Linear))), + "{what}: a linear word" + ); + assert!( + !when.iter().any(|c| holds(c, word(F::Nearest))), + "{what}: a nearest word" + ); + } + compiled += 1; + } + assert!( + compiled > 0, + "{s:?}: no committed profile has the sampled heap" + ); + } +} + /// A heap `float` read's `DevicePackConversion` holds where its command can name a texture the heap /// holds by its raw texels alone: on every committed profile with a sampled heap, a plan of its /// pipeline gives `SpecKey::HeapRawTextures` 1 exactly where a texture the command makes resident diff --git a/crates/metal2vulkan/src/tests/engine_extended_range/samples.rs b/crates/metal2vulkan/src/tests/engine_extended_range/samples.rs index 48154194c..dc8d349a2 100644 --- a/crates/metal2vulkan/src/tests/engine_extended_range/samples.rs +++ b/crates/metal2vulkan/src/tests/engine_extended_range/samples.rs @@ -1681,3 +1681,142 @@ fn the_samples_of_one_slot_call_one_routine() { } } } + +/// A sample of an extended-range texture through the runtime sampler of slot 0 records the +/// filter's note (`PrecisionNote::BicubicFilterArithmetic`, the device's sum of the decoded taps) +/// only where the word of the sampler bound there filters: specialized on that word +/// (`SpecKey::BoundSamplerWord`), a clause holding for a linear minification or magnification +/// filter, anisotropy, a cubic filter or a linear mip filter, and none for a nearest word, whose +/// sample is the decoded texel at Metal's position. A constexpr sampler keeps its rule: a linear +/// one records the note whatever is bound, a nearest one none. A `bfloat` form's sample reads the +/// nearest texel whatever the filter and records none. +#[test] +fn a_runtime_sampler_s_filter_note_holds_only_where_the_bound_word_filters() { + use crate::metal::{SamplerMinMagFilter as F, SamplerMipFilter as Mip}; + use crate::pipeline::artifact::{PrecisionNote, SpecClause}; + let note = PrecisionNote::BicubicFilterArithmetic; + let sampler = SlotRef { + stage: Stage::Kernel, + class: SlotClass::Sampler, + index: 0, + }; + let desc = |min, mag, mip, anisotropy| { + let mut d = SamplerDescriptor::new(); + (d.min_filter, d.mag_filter, d.mip_filter) = (min, mag, mip); + d.max_anisotropy = anisotropy; + SamplerWord::of(&d).0 + }; + // Each bound sampler's word, and whether a sample through it filters. + let words = [ + ( + "nearest", + desc(F::Nearest, F::Nearest, Mip::NotMipmapped, 1), + false, + ), + ( + "nearest mip filter", + desc(F::Nearest, F::Nearest, Mip::Nearest, 1), + false, + ), + ( + "linear", + desc(F::Linear, F::Linear, Mip::NotMipmapped, 1), + true, + ), + ( + "linear min", + desc(F::Linear, F::Nearest, Mip::NotMipmapped, 1), + true, + ), + ( + "linear mag", + desc(F::Nearest, F::Linear, Mip::NotMipmapped, 1), + true, + ), + ( + "linear mip filter", + desc(F::Nearest, F::Nearest, Mip::Linear, 1), + true, + ), + ( + "anisotropic", + desc(F::Nearest, F::Nearest, Mip::NotMipmapped, 16), + true, + ), + ( + "cubic", + desc(F::Cubic, F::Cubic, Mip::NotMipmapped, 1), + true, + ), + ]; + let holds = |clause: &SpecClause, word: u32| { + clause.all.iter().all(|t| { + assert_eq!(t.key, SpecKey::BoundSamplerWord(sampler), "{clause:?}"); + ((word & t.mask) == t.bits) == t.equal + }) + }; + let forms = FORMS.into_iter().chain([ + ExtendedRange::R10X6, + ExtendedRange::R10X6Srgb, + ExtendedRange::Rg10X12, + ExtendedRange::Rg10X12Srgb, + ExtendedRange::Rgb10A2Srgb, + ExtendedRange::R16Bfloat, + ExtendedRange::Rg16Bfloat, + ExtendedRange::Rgba16Bfloat, + ]); + let mut kernels = Vec::new(); + for half in [false, true] { + for (name, through) in [ + ("runtime", Through::Runtime), + ( + "constexpr linear", + Through::Constexpr(words(true, 1, false, 0)), + ), + ( + "constexpr nearest", + Through::Constexpr(words(false, 1, false, 0)), + ), + ] { + let what = format!("a {name} sampler's sample, half {half}"); + let Some(bc) = assemble(&what, &sample_kernel(through, half, (0, 0), false)) else { + return; + }; + kernels.push((what, bc, name)); + } + } + let mut specialized = 0; + for ((stem, p), form) in profiles() + .into_iter() + .flat_map(|sp| forms.clone().map(move |form| (sp.clone(), form))) + { + let mut inputs = LowerInputs::new(); + inputs.extended_range.insert(SOURCE, form); + for (what, bc, name) in &kernels { + let what = format!("{what} of {form:?} on {stem}"); + let a = validate_entry(bc, "k", &inputs, &p, &what); + let when: Vec<&SpecClause> = (a.specialized.iter()) + .filter(|s| s.note == note) + .flat_map(|s| &s.when) + .collect(); + let recorded = a.precision.contains(¬e); + match (*name, form.bfloat()) { + (_, true) | ("constexpr nearest", _) => { + assert!(!recorded && when.is_empty(), "{what}: {:?}", a.precision); + } + ("constexpr linear", _) => { + assert!(recorded && when.is_empty(), "{what}: {when:?}"); + } + _ => { + assert!(recorded && !when.is_empty(), "{what}: {:?}", a.specialized); + for (word_name, word, filters) in &words { + let held = when.iter().any(|c| holds(c, *word)); + assert_eq!(held, *filters, "{what}: a bound {word_name} sampler"); + } + specialized += 1; + } + } + } + } + assert!(specialized > 0, "no runtime sampler's note was specialized"); +} diff --git a/docs/API.md b/docs/API.md index e972564d5..592d4c136 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3663,7 +3663,10 @@ bound. filter choice, level and clamps are Metal's. Metal's fixed-point weights, exact grid sums, float accumulators and sixty-fourth level fractions are not reproduced: a lane may differ by a few grid steps (`1/4080` at 8 bits), by up to `2.1e-3` on an extended-range sRGB texture's bicubic sample, - and by up to `1/64` of the two levels' difference under a linear mip filter. + and by up to `1/64` of the two levels' difference under a linear mip filter. Through a runtime + sampler slot it is announced at a draw or dispatch only where the bound sampler filters (a linear + minification or magnification filter, anisotropy, a cubic filter or a linear mip filter): a + nearest sampler's sample is the decoded texel at Metal's position. - `metal2vulkan-degrade-sparse-residency` (precision). `PrecisionNote::SparseResidency`: A stage samples or gathers a texture whose residency it observes (`sparse_color::resident()`) through the slot's sparse form, which the module has where the profile enables `shaderResourceResidency`: