A sample of a BGR10_XR, BGRA10_XR or RGB10A8_2P_XR10 texture (or an sRGB twin) through a constexpr bicubic sampler, or through a runtime sampler whose bound word states Cubic, was refused (NotImplemented(SamplerBicubicFilter) in the lowering, NotImplemented(Format) in the binder). It is now Metal's bicubic filter of the decoded texels, measured on an Apple M3 Max (macOS 26.7) through the owned fixture xr_bicubic: random codes of each of the six formats at 16x16, 7x5, 3x3, 2x6 and 1x1, 2,304 coordinates each (every texel's centre, then random ones up to 0.15 of the texture past its edges), through 12 kernels (a runtime Cubic sampler with float and half results; constexpr samplers under clamp to edge with float and half results, repeat, mirrored repeat, clamp to zero, the opaque-white and opaque-black borders, pixel coordinates, and an offset under clamp to edge and repeat): 3,317,760 lanes. What Metal does, and what the lowering now does: the footprint, weights, addressing, a mirrored axis and a level below four texels are the other formats' bicubic filter's, each tap the decoded texel or the border texel through the format's channels; a level below four texels sums as the extended-range linear filter (terms truncated); otherwise each tap's step is weighed by the product of Metal's weights and summed exactly (no term truncated, unlike the linear filter), the step rounded half up, clamped into the steps of the codes 0 and 1023 (a color lane's (0-384)/510 and (1023-384)/510, an sRGB twin's table steps, alpha [0, 1]), and divided by the grid exactly; a half result rounds as the linear filter's does. The clamp is applied to the rounded result, which equals clamping the step because each rounding is monotonic. Every lane but 4,050 matches that model; each of the 4,050 is one step from Metal's (1/8160 a color lane, 1/8192 an sRGB color lane, 1/4080 alpha), its exact sum within 0.014 of a step of a rounding tie that Metal rounds the other way. No model of Metal's datapath tried closes that residue, so the sample records PrecisionNote::BicubicFilterArithmetic, and docs/FAST-PATHS.md and docs/API.md say so. A bfloat or padded form's bicubic sample, a swizzled texture's and a gather through a bicubic sampler were not measured and are refused. How: select_extended takes the bicubic state of a constexpr sampler or of the slot's cubic-sampler variant (LoweringInputs::cubic_samplers) and plans Metal's bicubic filter; the extended sampler emits it through bicubic::filter with Texels::Extended, which fetches each tap's raw texel and decodes it, through the exact routines alone, then clamps. The binder takes a cubic word at an extended-range slot as the cubic-sampler variant's, and gives such a slot the neutral BicubicTexels value in the plan of the pipeline as compiled, which binds the neutral there (it refused the format before naming the variant: the device lane found that). SEMANTICS_VERSION 1693; RUNTIME_VERSION 1184, as a run of an existing artifact with a cubic sampler at an extended-range slot is no longer refused; the runtime-sources hash re-pinned. The table: xr_bicubic.tsv (2,067 samples of the probe's, per texture and kernel the middle texel's centre, four random coordinates, the first sample with a lane at a clamp bound and the first with a lane at a tie), shared through metal2vulkan_devtools::xr_bicubic with the probe's texture generator. Verified on homelab-gem10 (debug builds): - tests::engine_extended_range::bicubic, on the CPU interpreter on the lavapipe and MoltenVK-M3 profiles: every lane of the table through the same 12 kernels, compiled under the six forms' variants (the runtime ones under the cubic-sampler word the binder plans), is Metal's bits but the 136 lanes the table marks, each one step away; each compile validates and its estimate bounds it. A padded or bfloat form's bicubic sample, a swizzled texture's and a gather are refused as above (a two-plane gather as Format, under any sampler); on every committed profile the pipeline as compiled plans the variants with the XR texture and a Cubic sampler bound, and the variant's pipeline plans the same key. - runtime_lane::extended_range_bicubic (new): the 8,268 lanes on the Radeon 780M (RADV), the RTX 3090 and lavapipe, textures written as the probe wrote them: 0 differ from Metal (136 at a tie, one step away), and the state announces bicubic-filter-arithmetic. The extended_range_samples, two_plane_samples and two_plane_copies lanes, whose helpers this shares, pass on all three. - Release build, 12 kernels x 6 forms x 5 profiles (lavapipe, 780M, RTX 5080 Laptop, Arc ANV, MoltenVK-M3): 360 translations, mean 1.31 ms, slowest 1.97 ms (xrb_runtime, BGRA10_XR sRGB, 780M), budget ledger peak at most 935 KiB, process peak RSS 15.6 MB; the linear XR kernels take 0.81 ms on average. - clippy -D warnings (workspace, all features, all targets; validation also under m2v_gpu), rustdoc -D warnings and the doctests pass. Suites: metal2vulkan, -air, -runtime and -devtools 4,566 of 4,567, validation 1,293 of 1,293. The one failure is tests::golden_hashes, whose 380 stale cells are the two new fixtures' alone (smw_writers 7 and xr_bicubic 12 entries x 20 profiles), left to be blessed on development.
159 lines
7.4 KiB
Rust
159 lines
7.4 KiB
Rust
//! External-tool helpers for metal2vulkan's own tests and its validation tools: the fixture
|
|
//! assemblers (`llvm-as`, `xcrun metal-as`, `spirv-as`), the committed fixture directories, a
|
|
//! synthetic metallib writer ([`metallib`]), a tool's identity, the MoltenVK toolchain the MSL
|
|
//! checks reproduce, and the generated-program fuzzers ([`fuzz`]) the product's CPU-interpreter
|
|
//! tests and validation's device tests both run, the extended-range blend and bicubic tables both
|
|
//! check ([`xr_blends`], [`xr_bicubic`]), and the store-buffering litmus kernel both compile
|
|
//! ([`litmus`]).
|
|
//!
|
|
//! The product runs no external tool and takes this crate for its tests alone: it is the
|
|
//! product's path-only dev-dependency, which `cargo publish` drops, and a dependency of
|
|
//! `metal2vulkan-validation`. The tool runner ([`tools`]) reads the `METAL2VULKAN_<TOOL>`
|
|
//! overrides the product's `env_vars` registers (`tools::tool_path_override`, which spells them as
|
|
//! the product does; the product's tests hold the two together): each tool resolves through its
|
|
//! override, then the usual LLVM install directories, then `PATH`. [`spirv`] runs `spirv-val` and
|
|
//! `spirv-dis`, the L3 checks the product's tests and validation share.
|
|
|
|
use std::ffi::OsStr;
|
|
use std::io::Write;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Stdio;
|
|
|
|
pub mod fuzz;
|
|
pub mod litmus;
|
|
pub mod metallib;
|
|
pub mod spirv;
|
|
pub mod tools;
|
|
pub mod xr_bicubic;
|
|
pub mod xr_blends;
|
|
|
|
/// The validation crate's `fixtures/public` beside this crate
|
|
/// (`crates/metal2vulkan-validation/fixtures/public`, read-only).
|
|
pub fn fixtures_dir() -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.parent()
|
|
.expect("the dev-tools crate lies in the workspace's crates/")
|
|
.join("metal2vulkan-validation/fixtures/public")
|
|
}
|
|
|
|
/// [`fixtures_dir`]'s `engine` (read-only; the engine's own fixtures).
|
|
pub fn engine_dir() -> PathBuf {
|
|
fixtures_dir().join("engine")
|
|
}
|
|
|
|
/// Why an external tool could not assemble a fixture or compile a module.
|
|
#[derive(Debug)]
|
|
pub enum AssembleError {
|
|
/// The tool is not installed (a test skips visibly).
|
|
Missing(String),
|
|
/// The tool ran and rejected the input (a test fails).
|
|
Rejected(String),
|
|
}
|
|
|
|
/// The bitcode `cmd` (after `pre`) assembles from the text on `input`: a function of the text and
|
|
/// the tool alone, wherever the text lies.
|
|
///
|
|
/// An LLVM assembler names a module after the file it reads, and a text without a
|
|
/// `source_filename` line takes that name as its `source_filename`, a record the bitcode carries.
|
|
/// Read from its path, one text assembled to different bytes in each directory: a census row
|
|
/// assembled in scratch directories named for process ids and counters carried them, so its work
|
|
/// units and ledger peak (the bitstream reader meters a record by its operands) and the detail of
|
|
/// a rejection changed from run to run. So the text goes in on standard input, where the module is
|
|
/// `<stdin>`, and the bitcode comes back on standard output.
|
|
fn assemble(cmd: &str, pre: &[&OsStr], input: Stdio) -> Result<Vec<u8>, AssembleError> {
|
|
let mut args: Vec<&OsStr> = pre.to_vec();
|
|
args.extend(["-", "-o", "-"].map(OsStr::new));
|
|
match tools::run_with_stdin(cmd, &args, input) {
|
|
Ok((bitcode, _)) => Ok(bitcode),
|
|
Err(tools::ToolError::Launch { executable, .. }) => Err(AssembleError::Missing(format!(
|
|
"{cmd} not found ({})",
|
|
executable.display()
|
|
))),
|
|
Err(e) => Err(AssembleError::Rejected(e.to_string())),
|
|
}
|
|
}
|
|
|
|
/// [`assemble`] of the text in the file `input`.
|
|
fn assemble_file(cmd: &str, pre: &[&OsStr], input: &Path) -> Result<Vec<u8>, AssembleError> {
|
|
let text = std::fs::File::open(input)
|
|
.map_err(|e| AssembleError::Rejected(format!("read {}: {e}", input.display())))?;
|
|
assemble(cmd, pre, text.into())
|
|
}
|
|
|
|
/// Opaque-pointer bitcode of an `.ll` fixture (`llvm-as`).
|
|
pub fn llvm_assemble(ll: &Path) -> Result<Vec<u8>, AssembleError> {
|
|
assemble_file("llvm-as", &[], ll)
|
|
}
|
|
|
|
/// Opaque-pointer bitcode of the LLVM text `text` (`llvm-as`), which a test generates rather than
|
|
/// reads from a fixture: the same bitcode [`llvm_assemble`] makes of that text in a file.
|
|
pub fn llvm_assemble_text(text: &str) -> Result<Vec<u8>, AssembleError> {
|
|
let (input, mut feed) =
|
|
std::io::pipe().map_err(|e| AssembleError::Rejected(format!("pipe: {e}")))?;
|
|
std::thread::scope(|scope| {
|
|
// Written beside the tool's run, so neither its input nor its output pipe fills while the
|
|
// other waits. The writer's end closes when the text is written, which ends the input.
|
|
let writer = scope.spawn(move || feed.write_all(text.as_bytes()));
|
|
let bitcode = assemble("llvm-as", &[], input.into());
|
|
match (bitcode, writer.join().expect("the input writer")) {
|
|
(Ok(_), Err(e)) => Err(AssembleError::Rejected(format!(
|
|
"llvm-as stopped reading its input: {e}"
|
|
))),
|
|
(bitcode, _) => bitcode,
|
|
}
|
|
})
|
|
}
|
|
|
|
/// The SPIR-V words of a `.spvasm` fixture (`spirv-as`, SPIR-V 1.5), every numeric id kept as the
|
|
/// text writes it (`--preserve-numeric-ids`), so a module `spirv-dis --raw-id` wrote assembles back
|
|
/// to its own words but the header's generator.
|
|
pub fn spirv_assemble(spvasm: &Path) -> Result<Vec<u32>, AssembleError> {
|
|
let args = ["--preserve-numeric-ids", "--target-env", "spv1.5"].map(OsStr::new);
|
|
let bytes = assemble_file("spirv-as", &args, spvasm)?;
|
|
if bytes.len() % 4 != 0 {
|
|
return Err(AssembleError::Rejected(format!(
|
|
"spirv-as wrote {} bytes for {}, not whole words",
|
|
bytes.len(),
|
|
spvasm.display()
|
|
)));
|
|
}
|
|
Ok(bytes
|
|
.as_chunks::<4>()
|
|
.0
|
|
.iter()
|
|
.map(|word| u32::from_le_bytes(*word))
|
|
.collect())
|
|
}
|
|
|
|
/// Typed-pointer AIR bitcode of an `.air.ll` companion (`xcrun metal-as`, macOS only).
|
|
pub fn metal_assemble(air_ll: &Path) -> Result<Vec<u8>, AssembleError> {
|
|
if !cfg!(target_os = "macos") {
|
|
return Err(AssembleError::Missing("metal-as needs macOS".to_owned()));
|
|
}
|
|
assemble_file("xcrun", &["metal-as".as_ref()], air_ll)
|
|
}
|
|
|
|
/// The `--version` output of external tool `cmd`, resolved as [`llvm_assemble`] and the L3 check
|
|
/// resolve it (`METAL2VULKAN_<TOOL>`, the Homebrew LLVM prefixes, then `PATH`): what a validation
|
|
/// cache keys that tool's behavior by. `Missing` when it cannot be launched, `Rejected` when it
|
|
/// runs and fails.
|
|
pub fn tool_version(cmd: &str) -> Result<String, AssembleError> {
|
|
match tools::run(cmd, &["--version"]) {
|
|
Ok((stdout, _)) => Ok(String::from_utf8_lossy(&stdout).into_owned()),
|
|
Err(tools::ToolError::Launch { executable, .. }) => Err(AssembleError::Missing(format!(
|
|
"{cmd} not found ({})",
|
|
executable.display()
|
|
))),
|
|
Err(e) => Err(AssembleError::Rejected(e.to_string())),
|
|
}
|
|
}
|
|
|
|
/// The pipeline creation the product tests' `pipeline::testing::moltenvk_msl_args` reproduces:
|
|
/// MoltenVK 1.4.1 (the Homebrew bottle, built with Xcode 26.2) on an Apple M3 Max under macOS 26,
|
|
/// the device of the committed `moltenvk-m3` profile.
|
|
pub const MOLTENVK_MSL_TARGET: &str = "MoltenVK 1.4.1 on Apple M3 Max, macOS 26: MSL 4.0";
|
|
|
|
/// The SPIRV-Cross commit MoltenVK 1.4.1 links (its `ExternalRevisions/SPIRV-Cross_repo_revision`).
|
|
/// A `spirv-cross` built from another commit converts as that commit does.
|
|
pub const MOLTENVK_SPIRV_CROSS_REVISION: &str = "adec7acbf41a988713cdb85f93f26c8ca5ea863e";
|