Rule 1: where the device has a native form it is the only form. The emulated filters reproduced Metal's datapath bit for bit: a 2^-16 Catmull-Rom weight table, two-word integer sums truncated onto grids and rounded half up, exact quotients, ties-away half rounding, a 12-bit sRGB grid snap, 64ths of a level and 256ths of a blend, and the anisotropic filter's piecewise log2 and chord exp2. All of it is gone. Bicubic (lower/sampler/bicubic.rs, bicubic/routines.rs): - One routine, `Plane::sum`: Catmull-Rom weights computed in f32 from the fraction (vec4 Horner), the sixteen taps summed by the device's multiply and add, rows then columns; Metal's bilinear rule (32-bit float formats, a level under four texels) is the device's FMix of the four inner taps already fetched; UNORM/SNORM clamps. - 1D: `Line::sum`, the device's FMix of its two taps. - An 8-bit sRGB twin's taps are the device's transfer, not the 1/4095 grid. A half result is the device's narrowing (`narrow::round`), not ties-away. - Kept: Metal's footprint, addressing, mirror pin, filter selection, `Quad::exact` (in-place YCbCr's filter, owner form H, not touched). - Deleted: the weight table routine, the exact/half/accumulated plane and row routines, the block and float-quad routines, their words and specials; metal::bicubic's test-only table and its test. Extended range (lower/sampler/extended.rs): - Linear, 3D and the linear mip filter's level blend decode their taps through xrconv's functions as they are, then FMix in f32 at the f32 fractions; the level weight is the f32 fraction, not 64ths; a half result is the device's narrowing. Bicubic clamps into the format's decoded endpoints. The in-place decode keeps `Quad::exact`. - Deleted: the step tables' callers (steps_of, step, level_taps, half_of) and, in xrconv's extended_range.rs and texel_round.rs, the helpers only they used (STEPS_WORDS, has_steps, steps, step_grids, ratio_by_255_to_f32, normal_half_bits; minimal edits in another lane's files). Anisotropic (lower/sampler/anisotropic.rs): - The device's Log2 and Exp2 replace Metal's piecewise log2, chord exp2 and truncations; the level and spacing are not rounded to 256ths, 4096ths or a 6-bit mantissa. - Each tap is one explicit-level sample: under a linear mip filter the fractional level, which the device's own linear mip filter blends. The two-samples-a-tap form existed because an RTX 3090's blend of a fractional level is not Metal's (1.03 codes off without anisotropy); under rule 1 that blend is the device's. A nearest mip filter's level stays whole (the 3090's anisotropic sampler blends the next level at a fractional level whatever its mipmap mode). - Kept: Metal's length, tap count thresholds, floor(256/n) weights, kappa fit and direction. Notes: PrecisionNote::BicubicFilterArithmetic is reused, broadened to the extended-range linear, 3D and level blends (select.rs records it); AnisotropicFilter's doc names the device's logarithm and blend. No new variant, no ARTIFACT_FORMAT_VERSION bump. FAST-PATHS: the two "not built yet / none known" bicubic rows are now one device-gap row (the device's sum is the form); the known-gaps bullet loses the sRGB grid snap. API.md's catalogue entries rewritten. SEMANTICS_VERSION 1922 -> 1923. Tests: the plane routine's interpreter differential against an f32 Catmull-Rom reference in the routine's order (fails on the old table and integer sum), the line routine's FMix, the routines' word bounds; engine_bicubic against the polynomial within 2^-18 (+ half ulp); extended-range engine tests compare within the filter's bound (`filtered_close`); xr_bicubic's `near` adds Metal's table's deviation from the polynomial (WEIGHTS_BOUND 2.1e-3); anisotropic and bias-reach tests count one sample a tap and two Exp2 per emulation. Lanes and the conformance suite: - runtime_lane::extended_range_samples compares linear samples within `xr_bicubic::filtered_close` (moved to devtools from the engine test, which re-exports it); two_plane_samples holds the digest of nearest cases only (a linear one's values are compared by the conformance suite's xr-two-plane-samples cases). - runtime_lane::anisotropic_filter's bounds re-measured on the M3 Max with samplerAnisotropy withheld: linear 10.375 codes max (mean 0.21), nearest mean 0.82 and 6.7% of channels a code or more off, clamp to edge 6.531 max. Other devices not measured. - conformance: the XR two-plane-samples and extended-range/padded extended-swizzles cases declare bicubic-filter-arithmetic; 32 known-gaps rows (Apple M3 Max) for the linear sites the device's FMix moves. - runtime-sources.sha256 re-pinned: metal/bicubic.rs lost its test-only table and docs; no run's behaviour depends on it. Verified (this M3 Max, macOS 27.0.1): metal2vulkan + devtools suites, 3302/3303 passed (golden_hashes_match_the_table fails: 1001 cells moved, not blessed). Module words of a bicubic sample (ANV Iris Xe profile): xr_bicubic xrb_edge 12990 -> 2093, a2_bicubic_weights 15731 -> 3858, bcx_rules 30727 -> 11388; anisotropic emulation (vk12-floor) 2705 -> 1884. Also in this packet: the padded 10-bit formats' linear samples join the known gaps (d326b293c0), and the conformance coverage document is regenerated for the filter rows. Anisotropic emulation (devices without samplerAnisotropy only) now takes the device's Log2/Exp2 and level blend, and Metal's spacing roundings (r' to 256ths, g*S to 4096ths, S to a 6-bit mantissa) are gone with them: on the M3 with samplerAnisotropy withheld, linear reads up to 10.4 codes from Metal (mean <= 0.21), bounded in runtime_lane/anisotropic_filter.rs. One work packet, landed as 2 commits on archive/development-linear-2026-10-29: abc366a2fa, then 8f506cb2fd (the re-pin).
214 lines
9.3 KiB
Rust
214 lines
9.3 KiB
Rust
//! The extended-range bicubic table: Metal's bicubic samples of each of Apple's extended-range
|
|
//! formats through the owned fixture `xr_bicubic`'s kernels, taken on an Apple M3 Max under macOS
|
|
//! 26.7 and committed as `validation/fixtures/public/engine/xr_bicubic.tsv` ([`table`]).
|
|
//!
|
|
//! The probe filled a texture of each raw format at each of [`SIZES`] with random codes
|
|
//! ([`textures`], the probe's generator, so both sides hold the same texels; a two-plane texture's
|
|
//! alpha bytes written by a kernel's `float` write of `b / 255`, then the color words by
|
|
//! `replaceRegion`), and sampled it through each kernel at [`COORDINATES`] normalized
|
|
//! coordinates: each texel's centre, then random ones in `[-0.15, 1.15)` on each axis. The
|
|
//! runtime kernels' sampler has `Cubic` minification and magnification, clamps to edge on `s` and
|
|
//! `t`, and takes normalized coordinates (`MTLSamplerDescriptor`'s other defaults). The product's
|
|
//! unit test runs the table through the CPU interpreter, and validation's lane on a device.
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
/// The texture sizes the probe sampled, in its order: width, height.
|
|
pub const SIZES: [Size; 5] = [(16, 16), (7, 5), (3, 3), (2, 6), (1, 1)];
|
|
|
|
/// The coordinates the probe sampled each texture at, its texel centres first.
|
|
pub const COORDINATES: u32 = 2304;
|
|
|
|
/// The raw formats the probe sampled: 550 `RGB10A8_2P_XR10`, 551 its `_sRGB` twin, 552
|
|
/// `BGRA10_XR`, 553 its twin, 554 `BGR10_XR`, 555 its twin.
|
|
pub const RAW_FORMATS: std::ops::RangeInclusive<u32> = 550..=555;
|
|
|
|
/// The fixture's kernels: through the runtime sampler (`float` and `half` results), and through
|
|
/// constexpr bicubic samplers (clamp to edge with `float` and `half` results; repeat; mirrored
|
|
/// repeat; clamp to zero; the opaque-white and opaque-black borders; pixel coordinates; a constant
|
|
/// offset `(1, -2)` under clamp to edge and under repeat).
|
|
pub const KERNELS: [&str; 12] = [
|
|
"xrb_runtime",
|
|
"xrb_runtime_half",
|
|
"xrb_edge",
|
|
"xrb_edge_half",
|
|
"xrb_repeat",
|
|
"xrb_mirror",
|
|
"xrb_zero",
|
|
"xrb_white",
|
|
"xrb_black",
|
|
"xrb_pixel",
|
|
"xrb_edge_offset",
|
|
"xrb_repeat_offset",
|
|
];
|
|
|
|
/// A texture's width and height.
|
|
pub type Size = (u32, u32);
|
|
|
|
/// The table's samples by the raw format, the size and the kernel ([`table`]).
|
|
pub type Table = BTreeMap<(u32, Size, String), Vec<Sample>>;
|
|
|
|
/// The textures' codes by the raw format and the size ([`textures`]).
|
|
pub type Textures = BTreeMap<(u32, Size), Vec<[u32; 4]>>;
|
|
|
|
/// One of Metal's samples: its coordinate and the result's lanes as bits (`half` lanes for a
|
|
/// `_half` kernel).
|
|
pub struct Sample {
|
|
pub at: [f32; 2],
|
|
pub lanes: [u32; 4],
|
|
}
|
|
|
|
/// The committed table, one sample per line: the raw format, the texture's width and height, the
|
|
/// kernel, the coordinate's `u` and `v` as `f32` bits, the result's lanes as bits, and the lanes at
|
|
/// which Metal's exact sum lies at a rounding tie (`-` for none; read by no check). Of the 2,304 samples the probe took of each texture through each kernel,
|
|
/// it holds the middle texel's centre, four random coordinates, the first sample with a color lane
|
|
/// at a clamp bound and the first with a lane at a tie. By the raw format, the size and the kernel.
|
|
pub fn table() -> Table {
|
|
let path = crate::engine_dir().join("xr_bicubic.tsv");
|
|
let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
|
|
let mut out = BTreeMap::<_, Vec<Sample>>::new();
|
|
for line in text.lines() {
|
|
let f: Vec<&str> = line.split(' ').collect();
|
|
let [raw, w, h, kernel, u, v, lanes, _ties] = f[..] else {
|
|
panic!("xr_bicubic.tsv: {line}");
|
|
};
|
|
let number = |s: &str| s.parse::<u32>().unwrap_or_else(|e| panic!("{line}: {e}"));
|
|
let bits = |s: &str| u32::from_str_radix(s, 16).unwrap_or_else(|e| panic!("{line}: {e}"));
|
|
let lanes: Vec<u32> = lanes.split(',').map(bits).collect();
|
|
out.entry((number(raw), (number(w), number(h)), kernel.to_string()))
|
|
.or_default()
|
|
.push(Sample {
|
|
at: [f32::from_bits(bits(u)), f32::from_bits(bits(v))],
|
|
lanes: lanes
|
|
.try_into()
|
|
.unwrap_or_else(|_| panic!("{line}: four lanes")),
|
|
});
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The codes of each texture the probe sampled (red, green, blue, alpha; row by row), by raw
|
|
/// format and size: splitmix64 from the state `0x9E3779B97F4A7C15`, size by size in [`SIZES`]'
|
|
/// order and within a size raw format by raw format, each texel's red, green and blue codes the top
|
|
/// ten bits of a draw each, its alpha the top eight (a two-plane alpha byte) or ten (`BGRA10_XR`)
|
|
/// bits of the next (`BGR10_XR` draws none, its alpha 0 here), then two draws for each random
|
|
/// coordinate.
|
|
pub fn textures() -> Textures {
|
|
let mut state = 0x9E37_79B9_7F4A_7C15u64;
|
|
let mut next = || {
|
|
state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
|
|
let mut z = state;
|
|
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
|
|
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
|
|
z ^ (z >> 31)
|
|
};
|
|
let mut out = BTreeMap::new();
|
|
for size in SIZES {
|
|
for raw in RAW_FORMATS {
|
|
let texels = size.0 * size.1;
|
|
let codes = (0..texels)
|
|
.map(|_| {
|
|
let [r, g, b] = [(); 3].map(|_| (next() >> 54) as u32);
|
|
let a = match raw {
|
|
550 | 551 => (next() >> 56) as u32,
|
|
552 | 553 => (next() >> 54) as u32,
|
|
_ => 0,
|
|
};
|
|
[r, g, b, a]
|
|
})
|
|
.collect();
|
|
out.insert((raw, size), codes);
|
|
for _ in texels..COORDINATES {
|
|
next();
|
|
next();
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The grid of lane `lane` (red to alpha) of a sample of raw format `raw`: `16 · 510` steps a
|
|
/// unit of a color lane, `8192` of an `_sRGB` twin's, and `8 · 510` of alpha.
|
|
pub fn grid(raw: u32, lane: usize) -> f64 {
|
|
match (lane, raw % 2 == 1) {
|
|
(3, _) => 4080.0,
|
|
(_, true) => 8192.0,
|
|
(_, false) => 8160.0,
|
|
}
|
|
}
|
|
|
|
/// How far Metal's fixed-point bicubic weights move a sum from the Catmull-Rom polynomial's: per
|
|
/// axis the four weights miss the polynomial by at most `4.83e-4` together (their table rounds the
|
|
/// cube to 16 units of `2^-17` over most fractions), so a 2D footprint's weights by at most
|
|
/// `1.21e-3` together, times the largest decoded tap (an `_sRGB` twin's `1.674`).
|
|
pub const WEIGHTS_BOUND: f64 = 2.1e-3;
|
|
|
|
/// Whether the lane `got` is Metal's `want` (`half` lanes where `half`) within the device's
|
|
/// bicubic arithmetic: the decoded taps summed by the device's `f32` multiply and add under the
|
|
/// Catmull-Rom weights computed from the fraction, where Metal sums its fixed-point weights
|
|
/// ([`WEIGHTS_BOUND`]) exactly onto the lane's `grid` (two steps), and the result's rounding (a
|
|
/// `half` result's narrowing).
|
|
pub fn near(got: u32, want: u32, grid: f64, half: bool) -> bool {
|
|
let value = |bits: u32| match half {
|
|
true => half_value(bits as u16),
|
|
false => f64::from(f32::from_bits(bits)),
|
|
};
|
|
let (g, w) = (value(got), value(want));
|
|
let exponent = g.abs().max(w.abs()).log2().floor();
|
|
let ulp = match half {
|
|
true => 2f64.powf(exponent.max(-14.0) - 10.0),
|
|
false => 2f64.powf(exponent.max(-126.0) - 23.0),
|
|
};
|
|
got == want || (g - w).abs() <= 2.0 / grid + WEIGHTS_BOUND + ulp
|
|
}
|
|
|
|
/// The finite `half` of `bits`.
|
|
fn half_value(bits: u16) -> f64 {
|
|
let magnitude = match (bits >> 10) & 0x1f {
|
|
0 => f64::from(bits & 0x3ff) * 2f64.powi(-24),
|
|
e => (1.0 + f64::from(bits & 0x3ff) / 1024.0) * 2f64.powi(i32::from(e) - 15),
|
|
};
|
|
if bits & 0x8000 != 0 {
|
|
-magnitude
|
|
} else {
|
|
magnitude
|
|
}
|
|
}
|
|
|
|
/// Whether a filtered sample's lanes `got` (`f32` bits) are Metal's `want` within the device's
|
|
/// filter arithmetic (`PrecisionNote::BicubicFilterArithmetic`): the decoded taps summed by the
|
|
/// device's multiply and add, where Metal sums them exactly on the lane's grid (a step of `1/8160`,
|
|
/// `1/8192` or `1/4080`), plus `weight` times the larger lane's magnitude for a blend weight the
|
|
/// device takes in `f32` where Metal truncates it (a linear mip filter's `1/64`), and a `half`
|
|
/// result's narrowing (one `half` ulp).
|
|
pub fn filtered_close(got: &[u32], want: &[u32], half: bool, weight: f64) -> bool {
|
|
// A lane's value: `f32` bits, or a `half` result's bits.
|
|
let value = |bits: u32| match half {
|
|
false => f64::from(f32::from_bits(bits)),
|
|
true => {
|
|
let h = bits as u16;
|
|
let m = match (h >> 10) & 0x1f {
|
|
0 => f64::from(h & 0x3ff) * 2f64.powi(-24),
|
|
e => (1.0 + f64::from(h & 0x3ff) / 1024.0) * 2f64.powi(i32::from(e) - 15),
|
|
};
|
|
if h & 0x8000 != 0 {
|
|
-m
|
|
} else {
|
|
m
|
|
}
|
|
}
|
|
};
|
|
got.len() == want.len()
|
|
&& got.iter().zip(want).all(|(&g, &w)| {
|
|
if g == w {
|
|
return true;
|
|
}
|
|
let (g, w) = (value(g), value(w));
|
|
let ulp = match half {
|
|
true => 2f64.powf(w.abs().max(2f64.powi(-14)).log2().floor() - 10.0),
|
|
false => 0.0,
|
|
};
|
|
(g - w).abs() <= 2.5e-4 + weight * w.abs().max(1.0) * 2.0 + ulp
|
|
})
|
|
}
|