Translator: a 4:2:2 decode clamps its taps to the edge, fetches each row once, and holds a constexpr sampler's filter alone
A camera preview's state re-made for a 4:2:2 texture at its fragment slot (41 decoded sample sites) took 1438 ms to create on the Radeon 780M against 437 ms for its own content (boot 88012): each site decoded 20 fetches behind the branch on its sampler's word, with every address mode's arithmetic at each tap, about 1,111 instructions a site. - Addressing: the decode clamps every tap to the edge, whatever the sampler states. Metal's MTLPixelFormat.h says of both GBGR422 and BGRG422 "When sampling, ClampToEdge is the only usable wrap mode". The other modes' arithmetic (the word's per-tap remainders, mirrors and border texel, its position by mode and offset rule) is gone from the decode; a bicubic sample's taps clamp too. - Rows: a linear footprint's or a gather's two taps of a row share six fetches (the first tap's pair and the pairs on either side of it; lower::texture::chroma::row), each texel's codes converted once, each texel's arithmetic and values as before: 12 fetches for the 2x2 where 16 were. - Static filter: a constexpr sampler's magnification filter is known in the translator, so its sample holds that filter's body alone, with no branch; a runtime, argument buffer's or constexpr array's sampler keeps both bodies behind the branch on its word. The coordinates' pixel flag is likewise read here for a constexpr sampler. - One position per axis, shared by both bodies. Values are unchanged for ClampToEdge: the same codes, integer arithmetic, correctly rounded values and FMix order. SEMANTICS_VERSION 2397. Verified: tests::engine_chroma_pairs::a_four_two_two_decode_fetches_each_footprint_row_once, red at the previous commit; the device and conformance values and the 780M's make time follow in this lane's report.
This commit is contained in:
1 parent
8e73a338c1
commit
269ba10df0
4 files changed
+365
-170
No files matched your search
Vendored
+1
-1
@@ -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 = 2396;
|
||||
pub const SEMANTICS_VERSION: u32 = 2397;
|
||||
|
||||
/// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the
|
||||
/// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another
|
||||
|
||||
@@ -278,7 +278,7 @@ pub(super) fn plan(
|
||||
/// Scalar and vector types and constants the emission uses.
|
||||
pub(super) struct Tys {
|
||||
pub(super) f32_: TypeId,
|
||||
i32_: TypeId,
|
||||
pub(super) i32_: TypeId,
|
||||
pub(super) u32_: TypeId,
|
||||
pub(super) vec4: TypeId,
|
||||
}
|
||||
@@ -323,7 +323,7 @@ fn f32_value(fb: &mut FunctionBuilder<'_>, t: &Tys, x: f32) -> BResult<SValue> {
|
||||
value(fb, c)
|
||||
}
|
||||
|
||||
fn i32_value(fb: &mut FunctionBuilder<'_>, t: &Tys, x: i32) -> BResult<SValue> {
|
||||
pub(super) fn i32_value(fb: &mut FunctionBuilder<'_>, t: &Tys, x: i32) -> BResult<SValue> {
|
||||
let c = scalar(fb, t.i32_, u64::from(x as u32))?;
|
||||
value(fb, c)
|
||||
}
|
||||
@@ -468,7 +468,7 @@ fn placed_offset(
|
||||
/// A nearest filter's texel `floor(x)` plus `offset` (an `int` value) of texel-space position
|
||||
/// `x` (an `f32`), its floor clamped into `±(2^31 - 128)` before the conversion as [`quantize`]'s
|
||||
/// is (a position past it, or a non-finite one, reads an unspecified texel within the level).
|
||||
fn floor_index(
|
||||
pub(super) fn floor_index(
|
||||
fb: &mut FunctionBuilder<'_>,
|
||||
t: &Tys,
|
||||
x: SValue,
|
||||
|
||||
@@ -7,19 +7,24 @@
|
||||
//! `LoweringInputs::chroma_handles`), computes Metal's answer from the decoded texels:
|
||||
//!
|
||||
//! - **The sampler** is read as a runtime sampler's word (`metal::sampler::SamplerWord`): a runtime
|
||||
//! or argument buffer's sampler's own, a constexpr sampler's the word of its state
|
||||
//! (`constexpr::word`), a constexpr sampler array's its element's. The footprint, its addressing
|
||||
//! under the word's address modes and coordinates, a constant or dynamic texel offset placed as
|
||||
//! Metal places it, and a tap outside a border axis reading the border texel through the
|
||||
//! format's channels are the emulated filters' (`bicubic::footprint_of_image`, `position`).
|
||||
//! or argument buffer's sampler's own, a constexpr sampler array's its element's, read in the
|
||||
//! function; a constexpr sampler's the word of its state (`constexpr::word`), read here. Its
|
||||
//! coordinates (normalized or pixel) place the footprint: Metal's position on a clamping axis
|
||||
//! (`position::position`), the quantized or floored base texel (`bicubic::quantize`), and a
|
||||
//! constant or dynamic texel offset added as Metal adds it on a clamping axis.
|
||||
//! - **The addressing** is clamp-to-edge on both axes, whatever the sampler states: Metal's
|
||||
//! `MTLPixelFormat.h` says of both `GBGR422` and `BGRG422` "When sampling, ClampToEdge is the
|
||||
//! only usable wrap mode". Each tap is its index clamped into the level, so the two taps of an
|
||||
//! axis are one texel or two neighbours.
|
||||
//! - **The filter** is the word's magnification filter, at every level and bias (module docs of
|
||||
//! `lower::texture::chroma`): nearest reads the decoded texel at `floor` of Metal's position,
|
||||
//! linear blends the four decoded taps of Metal's footprint by the device's `FMix` at the
|
||||
//! fractions `k / 256`, a merged axis's fraction 0 (`linear::merged`). Metal's own sum (on the
|
||||
//! codes' grid, each term truncated) is not reproduced: the sample records
|
||||
//! `PrecisionNote::BicubicFilterArithmetic`, the emulated filters' note. A sampler's mip filter,
|
||||
//! level clamps, anisotropy and reduction read nothing of a texture of one level sampled at
|
||||
//! its base level.
|
||||
//! `PrecisionNote::BicubicFilterArithmetic`, the emulated filters' note. A constexpr sampler's
|
||||
//! filter is known here, and its sample holds that filter's body alone; a sampler read in the
|
||||
//! function holds both, behind the branch on its word. A sampler's mip filter, level clamps,
|
||||
//! anisotropy and reduction read nothing of a texture of one level sampled at its base level.
|
||||
//! - **A gather** of a component reads the four decoded taps of the linear footprint, that
|
||||
//! component of each in Vulkan's order `(x0, y1)`, `(x1, y1)`, `(x1, y0)`, `(x0, y0)`: Metal's
|
||||
//! values exactly.
|
||||
@@ -29,26 +34,24 @@
|
||||
//! under the slot's format bits, `RGBX8Unorm`'s treatment (`metal::bicubic::texels`), with the
|
||||
//! emulated filters' note.
|
||||
//!
|
||||
//! The decoded taps of a footprint are each four fetches (`lower::texture::chroma::texel`), so a
|
||||
//! linear sample or a gather reads sixteen texels where the device's access reads four, and a
|
||||
//! bicubic sample sixty-four.
|
||||
//! A decoded texel is four fetches (`lower::texture::chroma::texel`), and the two taps of a
|
||||
//! footprint's row share six (`lower::texture::chroma::row`), so a linear sample or a gather reads
|
||||
//! twelve texels where the device's access reads four, a nearest sample four, a sample through a
|
||||
//! sampler read in the function sixteen in its two bodies, and a bicubic sample sixty-four.
|
||||
|
||||
use crate::lower::emit::EmitCx;
|
||||
use crate::lower::texture::chroma::{self, Bound, Image};
|
||||
use crate::lower::texture::TexelClass;
|
||||
use crate::metal::bicubic::BicubicTexels;
|
||||
use crate::metal::sampler::SamplerWord;
|
||||
use crate::metal::{PixelFormat, TextureSwizzleChannels};
|
||||
use crate::pipeline::sym::SamplerRef;
|
||||
use crate::spirv::builder::constructs::{ArmEnd, ArmFn, Merged};
|
||||
use crate::spirv::builder::FunctionBuilder;
|
||||
use crate::spirv::error::BResult;
|
||||
use crate::spirv::ids::Value as SValue;
|
||||
use crate::spirv::ir::{BinOp, ImageOperands, ImageQuery};
|
||||
use crate::spirv::ir::{BinOp, ConvOp, GlslOp, ImageOperands, ImageQuery, Type as SType};
|
||||
|
||||
use super::bicubic::{
|
||||
footprint_of_image, footprint_on, scalar, value, Addressing, Bits, Footprint, Tap, TapAddress,
|
||||
TapBase, Taps, Texels, Tys,
|
||||
footprint_on, scalar, value, Bicubic, Footprint, Tap, TapAddress, Texels, Tys,
|
||||
};
|
||||
use super::{BindingKey, CombinedImage, HeapKey, Offset, Pixel, SampleOp, Sampling, Source};
|
||||
|
||||
@@ -106,19 +109,46 @@ pub(super) fn sampled(
|
||||
}
|
||||
}
|
||||
|
||||
/// The word of a 4:2:2 decode's sampler (`metal::sampler::SamplerWord`): a constexpr sampler's,
|
||||
/// known here, or one read in the function (a `u32`).
|
||||
#[derive(Clone, Copy)]
|
||||
enum Word {
|
||||
Known(u32),
|
||||
Read(SValue),
|
||||
}
|
||||
|
||||
/// A flag of a sampler's word: known here, or read in the function (a `bool`).
|
||||
#[derive(Clone, Copy)]
|
||||
enum Flag {
|
||||
Known(bool),
|
||||
Read(SValue),
|
||||
}
|
||||
|
||||
impl Word {
|
||||
/// Whether the word sets `bit`.
|
||||
fn flag(self, fb: &mut FunctionBuilder<'_>, t: &Tys, bit: u32) -> BResult<Flag> {
|
||||
Ok(match self {
|
||||
Word::Known(w) => Flag::Known(w & bit != 0),
|
||||
Word::Read(word) => {
|
||||
let b = scalar(fb, t.u32_, u64::from(bit))?;
|
||||
let b = value(fb, b)?;
|
||||
let set = fb.binary(BinOp::BitwiseAnd, word, b)?;
|
||||
let zero = scalar(fb, t.u32_, 0)?;
|
||||
let zero = value(fb, zero)?;
|
||||
Flag::Read(fb.binary(BinOp::INotEqual, set, zero)?)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The word of the sampler sampling `s` reads (`metal::sampler::SamplerWord`): a runtime or
|
||||
/// argument buffer's sampler's, a constexpr sampler's of its state, a constexpr sampler array's
|
||||
/// its element's.
|
||||
fn sampler_word(
|
||||
cx: &mut EmitCx<'_>,
|
||||
fb: &mut FunctionBuilder<'_>,
|
||||
s: &Sampling,
|
||||
) -> BResult<SValue> {
|
||||
let constant = |cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, words: [u64; 2]| {
|
||||
let w = super::constexpr::word(words)
|
||||
.map_err(|e| cx.fail(format!("a 4:2:2 sample's constexpr sampler: {e}")))?;
|
||||
let c = fb.u32_const(w.0)?;
|
||||
fb.module().const_value(c)
|
||||
/// argument buffer's sampler's, a constexpr sampler array's its element's, read in the function; a
|
||||
/// constexpr sampler's of its state, known here.
|
||||
fn sampler_word(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, s: &Sampling) -> BResult<Word> {
|
||||
let known = |cx: &mut EmitCx<'_>, words: [u64; 2]| {
|
||||
super::constexpr::word(words)
|
||||
.map(|w| w.0)
|
||||
.map_err(|e| cx.fail(format!("a 4:2:2 sample's constexpr sampler: {e}")))
|
||||
};
|
||||
match s.source {
|
||||
Source::Combined(BindingKey { sampler, .. })
|
||||
@@ -126,12 +156,18 @@ fn sampler_word(
|
||||
key: HeapKey { sampler, .. },
|
||||
..
|
||||
} => match sampler {
|
||||
SamplerRef::Slot(n) => super::emit::sampler_word(cx, fb, Pixel::Spec(n)),
|
||||
SamplerRef::Constexpr(words) => constant(cx, fb, words),
|
||||
SamplerRef::Slot(n) => Ok(Word::Read(super::emit::sampler_word(
|
||||
cx,
|
||||
fb,
|
||||
Pixel::Spec(n),
|
||||
)?)),
|
||||
SamplerRef::Constexpr(words) => Ok(Word::Known(known(cx, words)?)),
|
||||
},
|
||||
Source::HeapSampler { sampler, .. } => {
|
||||
super::emit::sampler_word(cx, fb, Pixel::Heap(sampler))
|
||||
}
|
||||
Source::HeapSampler { sampler, .. } => Ok(Word::Read(super::emit::sampler_word(
|
||||
cx,
|
||||
fb,
|
||||
Pixel::Heap(sampler),
|
||||
)?)),
|
||||
Source::HeapArray { sampler, key, .. } => {
|
||||
let samplers = usize::try_from(key.array)
|
||||
.ok()
|
||||
@@ -143,7 +179,8 @@ fn sampler_word(
|
||||
let (element, _) = super::emit::array_element(cx, fb, sampler, key)?;
|
||||
let mut word = None;
|
||||
for (k, words) in (0u32..).zip(samplers) {
|
||||
let w = constant(cx, fb, words)?;
|
||||
let w = fb.u32_const(known(cx, words)?)?;
|
||||
let w = fb.module().const_value(w)?;
|
||||
word = Some(match word {
|
||||
None => w,
|
||||
Some(v) => {
|
||||
@@ -154,23 +191,12 @@ fn sampler_word(
|
||||
}
|
||||
});
|
||||
}
|
||||
word.ok_or_else(|| cx.fail("a sampler array without samplers"))
|
||||
word.map(Word::Read)
|
||||
.ok_or_else(|| cx.fail("a sampler array without samplers"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The format bits a footprint's border texel reads: `RGBX8Unorm`'s treatment, which a 4:2:2
|
||||
/// format shares (`metal::bicubic::texels`), with its border texels unswizzled.
|
||||
fn border_bits(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>) -> BResult<Bits> {
|
||||
let Some(b) = crate::metal::bicubic::texels(PixelFormat::GBGR422) else {
|
||||
return Err(cx.fail("a 4:2:2 format without its texel treatment"));
|
||||
};
|
||||
let b: BicubicTexels = b.with_border(PixelFormat::GBGR422, TextureSwizzleChannels::DEFAULT);
|
||||
let t = Tys::new(fb)?;
|
||||
let c = scalar(fb, t.u32_, u64::from(b.0))?;
|
||||
Ok(Bits(value(fb, c)?))
|
||||
}
|
||||
|
||||
/// The base level's operands of a decode's fetches.
|
||||
fn base_level(fb: &mut FunctionBuilder<'_>) -> BResult<ImageOperands> {
|
||||
let zero = fb.u32_const(0)?;
|
||||
@@ -182,7 +208,7 @@ fn base_level(fb: &mut FunctionBuilder<'_>) -> BResult<ImageOperands> {
|
||||
}
|
||||
|
||||
/// The decoded value at `site` (a tap of each plane axis) of footprint `fp` over 4:2:2 image `im`,
|
||||
/// or the border texel where the tap lies outside a border axis.
|
||||
/// or the border texel where the tap lies outside a border axis: a bicubic sample's tap.
|
||||
pub(super) fn tap(
|
||||
fb: &mut FunctionBuilder<'_>,
|
||||
fp: &Footprint,
|
||||
@@ -213,6 +239,114 @@ fn offsets<'a>(
|
||||
})
|
||||
}
|
||||
|
||||
/// One plane axis of a 4:2:2 footprint: Metal's texel-space position (an `f32`), the base level's
|
||||
/// size (a `u32`), and the texel offset added to its base texel (an `int`), where one is.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Axis {
|
||||
x: SValue,
|
||||
n: SValue,
|
||||
offset: Option<SValue>,
|
||||
}
|
||||
|
||||
/// The plane axes (`s`, then `t`) of sampling `s` at coordinate `coord` (a `vec2`) over image
|
||||
/// `im`, its coordinates in pixels where `pixel` (module docs), its offset `(constant, dynamic)`.
|
||||
fn axes(
|
||||
fb: &mut FunctionBuilder<'_>,
|
||||
t: &Tys,
|
||||
(coord, pixel): (SValue, Flag),
|
||||
im: &Image,
|
||||
(constant, dynamic): (Option<&[i32]>, Option<(SValue, u8)>),
|
||||
) -> BResult<[Axis; 2]> {
|
||||
let axis = |fb: &mut FunctionBuilder<'_>, a: u32| -> BResult<Axis> {
|
||||
let c = fb.composite_extract(coord, &[a])?;
|
||||
let n = fb.composite_extract(im.size, &[a])?;
|
||||
let x = match pixel {
|
||||
Flag::Known(true) => c,
|
||||
Flag::Known(false) => super::position::position(fb, c, n, TapAddress::Edge)?.0,
|
||||
Flag::Read(p) => {
|
||||
let x = super::position::position(fb, c, n, TapAddress::Edge)?.0;
|
||||
fb.select(p, c, x)?
|
||||
}
|
||||
};
|
||||
let offset = match (dynamic, constant.and_then(|k| k.get(a as usize))) {
|
||||
(Some((v, 1)), _) if a == 0 => Some(v),
|
||||
(Some((v, lanes)), _) if a < u32::from(lanes) => Some(fb.composite_extract(v, &[a])?),
|
||||
(Some(_), _) | (None, None | Some(&0)) => None,
|
||||
(None, Some(&k)) => Some(super::bicubic::i32_value(fb, t, k)?),
|
||||
};
|
||||
Ok(Axis { x, n, offset })
|
||||
};
|
||||
Ok([axis(fb, 0)?, axis(fb, 1)?])
|
||||
}
|
||||
|
||||
/// Index `i` (an `int`) clamped into an axis of `n` texels (a `u32`), as a `u32`.
|
||||
fn clamped(fb: &mut FunctionBuilder<'_>, t: &Tys, i: SValue, n: SValue) -> BResult<SValue> {
|
||||
let zero = super::bicubic::i32_value(fb, t, 0)?;
|
||||
let one = super::bicubic::i32_value(fb, t, 1)?;
|
||||
let extent = fb.convert(ConvOp::Bitcast, n, t.i32_)?;
|
||||
let last = fb.binary(BinOp::ISub, extent, one)?;
|
||||
let at = fb.glsl(GlslOp::SClamp, &[i, zero, last])?;
|
||||
fb.convert(ConvOp::Bitcast, at, t.u32_)
|
||||
}
|
||||
|
||||
/// A linear footprint's taps on axis `a`, `i` and `i + 1` clamped into the level (`u32`s), and
|
||||
/// its fraction's 1/256 steps (a `u32`), 0 where both taps are one texel (`linear::merged`).
|
||||
fn linear_axis(
|
||||
fb: &mut FunctionBuilder<'_>,
|
||||
t: &Tys,
|
||||
a: Axis,
|
||||
) -> BResult<(SValue, SValue, SValue)> {
|
||||
let (i, k) = super::bicubic::quantize(fb, t, a.x, a.offset)?;
|
||||
let one = super::bicubic::i32_value(fb, t, 1)?;
|
||||
let next = fb.binary(BinOp::IAdd, i, one)?;
|
||||
let lo = clamped(fb, t, i, a.n)?;
|
||||
let hi = clamped(fb, t, next, a.n)?;
|
||||
let k = super::linear::merged(fb, t, k, [(lo, None), (hi, None)])?;
|
||||
Ok((lo, hi, k))
|
||||
}
|
||||
|
||||
/// The decoded texel of the nearest footprint of `axes` over image `im`: each axis's `floor` of
|
||||
/// Metal's position, clamped into the level.
|
||||
fn nearest(fb: &mut FunctionBuilder<'_>, t: &Tys, axes: [Axis; 2], im: &Image) -> BResult<SValue> {
|
||||
let mut at = Vec::with_capacity(2);
|
||||
for a in axes {
|
||||
let i = super::bicubic::floor_index(fb, t, a.x, a.offset)?;
|
||||
at.push(clamped(fb, t, i, a.n)?);
|
||||
}
|
||||
let uv2 = fb.module().ty(SType::Vector {
|
||||
elem: t.u32_,
|
||||
len: 2,
|
||||
})?;
|
||||
let at = fb.composite_construct(uv2, &at)?;
|
||||
let ops = base_level(fb)?;
|
||||
chroma::texel(fb, im, at, &ops)
|
||||
}
|
||||
|
||||
/// The decoded texels of the linear footprint of `axes` over image `im`, `(i, j)`, `(i + 1, j)`,
|
||||
/// `(i, j + 1)`, `(i + 1, j + 1)`, each row's two from its shared fetches
|
||||
/// (`lower::texture::chroma::row`), and the fractions' 1/256 steps of the `s` and `t` axes.
|
||||
fn quad(
|
||||
fb: &mut FunctionBuilder<'_>,
|
||||
t: &Tys,
|
||||
[s, tt]: [Axis; 2],
|
||||
im: &Image,
|
||||
) -> BResult<([SValue; 4], [SValue; 2])> {
|
||||
let (x0, x1, kx) = linear_axis(fb, t, s)?;
|
||||
let (y0, y1, ky) = linear_axis(fb, t, tt)?;
|
||||
let ops = base_level(fb)?;
|
||||
let [t00, t01] = chroma::row(fb, im, (x0, x1), y0, &ops)?;
|
||||
let [t10, t11] = chroma::row(fb, im, (x0, x1), y1, &ops)?;
|
||||
Ok(([t00, t01, t10, t11], [kx, ky]))
|
||||
}
|
||||
|
||||
/// Metal's linear sample of the footprint of `axes` over image `im` (module docs).
|
||||
fn linear(fb: &mut FunctionBuilder<'_>, t: &Tys, axes: [Axis; 2], im: &Image) -> BResult<SValue> {
|
||||
let (taps, [kx, ky]) = quad(fb, t, axes, im)?;
|
||||
let fx = super::extended::fraction(fb, t, kx)?;
|
||||
let fy = super::extended::fraction(fb, t, ky)?;
|
||||
super::extended::bilinear(fb, taps, (fx, fy))
|
||||
}
|
||||
|
||||
/// Metal's answer of sampling `s` of 4:2:2 image `image` (`BGRG422` where `bgrg`), in the open
|
||||
/// block of the decode's branch (module docs).
|
||||
fn decoded(
|
||||
@@ -229,6 +363,11 @@ fn decoded(
|
||||
let t = Tys::new(fb)?;
|
||||
let coord = super::emit::plane_coord(cx, fb, s)?;
|
||||
if let SampleOp::Bicubic(b) = &s.op {
|
||||
// The taps clamp to the edge, whatever the sampler states (module docs).
|
||||
let b = &Bicubic {
|
||||
address: vec![TapAddress::Edge; b.address.len()],
|
||||
..b.clone()
|
||||
};
|
||||
let bits = super::bicubic::format_bits(cx, fb, b.texture)?;
|
||||
let half = super::bicubic::half_result(cx, s)?;
|
||||
let span = b.span();
|
||||
@@ -240,8 +379,6 @@ fn decoded(
|
||||
let x = super::bicubic::filter(cx, fb, (&t, &fp, size), b, bits, Texels::Chroma(&im))?;
|
||||
return super::bicubic::narrowed(fb, x, half);
|
||||
}
|
||||
let bits = border_bits(cx, fb)?;
|
||||
let edges = [TapAddress::Edge; 2];
|
||||
let (word, offset) = match &s.op {
|
||||
SampleOp::Sample { offset, .. } | SampleOp::Gather { offset, .. } => {
|
||||
(Some(sampler_word(cx, fb, s)?), offset.as_ref())
|
||||
@@ -249,113 +386,48 @@ fn decoded(
|
||||
SampleOp::PixelFetch { offset } => (None, offset.as_ref()),
|
||||
_ => return Err(cx.fail("a 4:2:2 sampling of another operation")),
|
||||
};
|
||||
let addressing = match word {
|
||||
Some(word) => Addressing::Word { word, axes: 2 },
|
||||
// A pixel fetch's sampler: nearest, pixel coordinates, clamping to the edge.
|
||||
None => Addressing::Modes {
|
||||
address: &edges,
|
||||
border: crate::air::abi::BorderColor::TransparentBlack,
|
||||
pixel: true,
|
||||
},
|
||||
};
|
||||
let (constant, dynamic) = offsets(cx, fb, offset, s.lanes)?;
|
||||
let t = &t;
|
||||
let footprint = |fb: &mut FunctionBuilder<'_>, span: &[i32], base: TapBase| {
|
||||
let taps = Taps {
|
||||
addressing,
|
||||
offset: constant,
|
||||
span,
|
||||
base,
|
||||
level: None,
|
||||
};
|
||||
footprint_of_image(fb, t, (coord, None, dynamic), (img, image), bits, &taps)
|
||||
// A pixel fetch's sampler: nearest and pixel coordinates.
|
||||
let pixel = match word {
|
||||
Some(w) => w.flag(fb, &t, SamplerWord::PIXEL_COORDINATES)?,
|
||||
None => Flag::Known(true),
|
||||
};
|
||||
let offset = offsets(cx, fb, offset, s.lanes)?;
|
||||
let axes = axes(fb, &t, (coord, pixel), &im, offset)?;
|
||||
match (&s.op, word) {
|
||||
(SampleOp::Gather { component, .. }, _) => {
|
||||
let fp = footprint(fb, &[0, 1], TapBase::Quantized)?;
|
||||
let [x0, x1, y0, y1] = quad(cx, &fp)?;
|
||||
let ([t00, t01, t10, t11], _) = quad(fb, &t, axes, &im)?;
|
||||
let mut lanes = Vec::with_capacity(4);
|
||||
for site in [[x0, y1], [x1, y1], [x1, y0], [x0, y0]] {
|
||||
let v = tap(fb, &fp, &im, &site)?;
|
||||
for v in [t10, t11, t01, t00] {
|
||||
lanes.push(fb.composite_extract(v, &[*component])?);
|
||||
}
|
||||
fb.composite_construct(t.vec4, &lanes)
|
||||
}
|
||||
// The magnification filter decides, at every level.
|
||||
(SampleOp::Sample { .. }, Some(word)) => {
|
||||
// The magnification filter decides, at every level.
|
||||
let bit = scalar(fb, t.u32_, u64::from(SamplerWord::MAG_LINEAR))?;
|
||||
let bit = value(fb, bit)?;
|
||||
let set = fb.binary(BinOp::BitwiseAnd, word, bit)?;
|
||||
let zero = scalar(fb, t.u32_, 0)?;
|
||||
let zero = value(fb, zero)?;
|
||||
let linear = fb.binary(BinOp::INotEqual, set, zero)?;
|
||||
let filtered: ArmFn<'_, EmitCx<'_>> = Box::new(move |fb, cx| {
|
||||
let fp = footprint(fb, &[0, 1], TapBase::Quantized)?;
|
||||
let [x0, x1, y0, y1] = quad(cx, &fp)?;
|
||||
let [(kx, _), (ky, _)] = &fp.axes[..] else {
|
||||
return Err(cx.fail("a 4:2:2 linear sample without two plane axes"));
|
||||
};
|
||||
let (kx, ky) = (*kx, *ky);
|
||||
let mut taps = Vec::with_capacity(4);
|
||||
for site in [[x0, y0], [x1, y0], [x0, y1], [x1, y1]] {
|
||||
taps.push(tap(fb, &fp, &im, &site)?);
|
||||
match word.flag(fb, &t, SamplerWord::MAG_LINEAR)? {
|
||||
Flag::Known(true) => linear(fb, &t, axes, &im),
|
||||
Flag::Known(false) => nearest(fb, &t, axes, &im),
|
||||
Flag::Read(magnifies) => {
|
||||
let t = &t;
|
||||
let filtered: ArmFn<'_, EmitCx<'_>> = Box::new(move |fb, _| {
|
||||
Ok(ArmEnd::FallThrough(vec![linear(fb, t, axes, &im)?]))
|
||||
});
|
||||
let point: ArmFn<'_, EmitCx<'_>> = Box::new(move |fb, _| {
|
||||
Ok(ArmEnd::FallThrough(vec![nearest(fb, t, axes, &im)?]))
|
||||
});
|
||||
let Merged(m) = fb.if_(cx, magnifies, filtered, Some(point))?;
|
||||
match m[..] {
|
||||
[v] => Ok(v),
|
||||
_ => Err(cx.fail("a 4:2:2 sample without its merged filter")),
|
||||
}
|
||||
}
|
||||
let kx = super::linear::merged(fb, t, kx, [x0, x1])?;
|
||||
let ky = super::linear::merged(fb, t, ky, [y0, y1])?;
|
||||
let fx = super::extended::fraction(fb, t, kx)?;
|
||||
let fy = super::extended::fraction(fb, t, ky)?;
|
||||
let [t00, t01, t10, t11] = taps[..] else {
|
||||
return Err(cx.fail("a 4:2:2 linear sample without its four taps"));
|
||||
};
|
||||
let v = super::extended::bilinear(fb, [t00, t01, t10, t11], (fx, fy))?;
|
||||
Ok(ArmEnd::FallThrough(vec![v]))
|
||||
});
|
||||
let nearest: ArmFn<'_, EmitCx<'_>> = Box::new(move |fb, cx| {
|
||||
let fp = footprint(fb, &[0], TapBase::Floor)?;
|
||||
let v = nearest(cx, fb, &fp, &im)?;
|
||||
Ok(ArmEnd::FallThrough(vec![v]))
|
||||
});
|
||||
let Merged(m) = fb.if_(cx, linear, filtered, Some(nearest))?;
|
||||
match m[..] {
|
||||
[v] => Ok(v),
|
||||
_ => Err(cx.fail("a 4:2:2 sample without its merged filter")),
|
||||
}
|
||||
}
|
||||
(SampleOp::PixelFetch { .. }, None) => {
|
||||
let fp = footprint(fb, &[0], TapBase::Floor)?;
|
||||
nearest(cx, fb, &fp, &im)
|
||||
}
|
||||
(SampleOp::PixelFetch { .. }, None) => nearest(fb, &t, axes, &im),
|
||||
_ => Err(cx.fail("a 4:2:2 sampling of another operation")),
|
||||
}
|
||||
}
|
||||
|
||||
/// The decoded texel of a nearest footprint `fp` (one tap per plane axis) of image `im`.
|
||||
fn nearest(
|
||||
cx: &mut EmitCx<'_>,
|
||||
fb: &mut FunctionBuilder<'_>,
|
||||
fp: &Footprint,
|
||||
im: &Image,
|
||||
) -> BResult<SValue> {
|
||||
let [(_, xs), (_, ys)] = &fp.axes[..] else {
|
||||
return Err(cx.fail("a 4:2:2 nearest sample without two plane axes"));
|
||||
};
|
||||
let (&[x], &[y]) = (xs.as_slice(), ys.as_slice()) else {
|
||||
return Err(cx.fail("a 4:2:2 nearest axis without its tap"));
|
||||
};
|
||||
tap(fb, fp, im, &[x, y])
|
||||
}
|
||||
|
||||
/// The two taps of each plane axis of a linear footprint `fp`: `x0`, `x1`, `y0`, `y1`.
|
||||
fn quad(cx: &mut EmitCx<'_>, fp: &Footprint) -> BResult<[Tap; 4]> {
|
||||
let [(_, xs), (_, ys)] = &fp.axes[..] else {
|
||||
return Err(cx.fail("a 4:2:2 footprint without two plane axes"));
|
||||
};
|
||||
let (&[x0, x1], &[y0, y1]) = (xs.as_slice(), ys.as_slice()) else {
|
||||
return Err(cx.fail("a 4:2:2 footprint axis without two taps"));
|
||||
};
|
||||
Ok([x0, x1, y0, y1])
|
||||
}
|
||||
|
||||
/// The taps at image coordinates `at` (`uvec2`s) of candidate `s` of a shared bicubic emulation
|
||||
/// (`super::shared`), whose image is `img` and whose device fetches are `native`: each tap decoded
|
||||
/// where the function decodes the candidate's slot (module docs), else `native`. A candidate that
|
||||
|
||||
@@ -26,7 +26,11 @@
|
||||
//! code `k` (within far less than half a step of `k / 255` on every device), Metal's arithmetic
|
||||
//! on the codes in integers, and each code's value `k / 255` correctly rounded
|
||||
//! (`words::unorm_values`), so a read, a nearest sample and a gather are Metal's values exactly.
|
||||
//! A linear and a bicubic sample filter the decoded texels by fetches (`lower::sampler::chroma`).
|
||||
//! Two texels of one row a texel apart, or one texel twice ([`row`], a linear footprint's row and
|
||||
//! a gather's), share their fetches: the pair of the first and the pairs on either side of it,
|
||||
//! six fetches, each texel's codes converted once, and each texel's arithmetic and values as
|
||||
//! [`texel`]'s. A linear and a bicubic sample filter the decoded texels by fetches
|
||||
//! (`lower::sampler::chroma`).
|
||||
//!
|
||||
//! **Where a function decodes.** The function is lowered for its bindings
|
||||
//! (`LoweringInputs::chroma_pairs` and `LoweringInputs::chroma_handles`), and a function lowered
|
||||
@@ -216,12 +220,13 @@ pub(crate) fn marked(
|
||||
fb.select(named, marked, h)
|
||||
}
|
||||
|
||||
/// What a decode reads of its image: the image, whether its format is `BGRG422`, and its last
|
||||
/// pair's index on the base level, `W / 2 - 1` (`u32`s).
|
||||
/// What a decode reads of its image: the image, whether its format is `BGRG422`, its base
|
||||
/// level's size (a `uvec2`) and its last pair's index on the base level, `W / 2 - 1` (`u32`s).
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct Image {
|
||||
pub(crate) img: SValue,
|
||||
bgrg: SValue,
|
||||
pub(crate) size: SValue,
|
||||
last_pair: SValue,
|
||||
}
|
||||
|
||||
@@ -238,11 +243,54 @@ impl Image {
|
||||
Ok(Image {
|
||||
img,
|
||||
bgrg,
|
||||
size,
|
||||
last_pair,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The codes `floor(255 v + 1/2)` (a `uvec4`) of the four values `l` (`f32`s) of the device's
|
||||
/// texels (module docs).
|
||||
fn codes(fb: &mut FunctionBuilder<'_>, l: &[SValue]) -> BResult<SValue> {
|
||||
let u = u32_ty(fb)?;
|
||||
let f = f32_ty(fb)?;
|
||||
let uv4 = fb.module().ty(SType::Vector { elem: u, len: 4 })?;
|
||||
let fv4 = fb.module().ty(SType::Vector { elem: f, len: 4 })?;
|
||||
let v = fb.composite_construct(fv4, l)?;
|
||||
let k255 = kf(fb, 255.0)?;
|
||||
let k255 = fb.composite_construct(fv4, &[k255; 4])?;
|
||||
let half = kf(fb, 0.5)?;
|
||||
let half = fb.composite_construct(fv4, &[half; 4])?;
|
||||
let scaled = fb.binary(BinOp::FMul, v, k255)?;
|
||||
let nudged = fb.binary(BinOp::FAdd, scaled, half)?;
|
||||
fb.convert(ConvOp::ConvertFToU, nudged, uv4)
|
||||
}
|
||||
|
||||
/// Each lane's chroma `(3c + n + 2) >> 2` of its pair's code `c` and the code `n` of the pair on
|
||||
/// its side (`u32`s, or `uvec4`s of each lane's).
|
||||
fn mixed(fb: &mut FunctionBuilder<'_>, c: SValue, n: SValue, lanes: usize) -> BResult<SValue> {
|
||||
let three = ku(fb, 3)?;
|
||||
let two = ku(fb, 2)?;
|
||||
let (three, two) = match lanes {
|
||||
1 => (three, two),
|
||||
_ => {
|
||||
let u = u32_ty(fb)?;
|
||||
let uv = fb.module().ty(SType::Vector {
|
||||
elem: u,
|
||||
len: lanes as u8,
|
||||
})?;
|
||||
(
|
||||
fb.composite_construct(uv, &vec![three; lanes])?,
|
||||
fb.composite_construct(uv, &vec![two; lanes])?,
|
||||
)
|
||||
}
|
||||
};
|
||||
let c3 = fb.binary(BinOp::IMul, c, three)?;
|
||||
let s = fb.binary(BinOp::IAdd, c3, n)?;
|
||||
let s = fb.binary(BinOp::IAdd, s, two)?;
|
||||
fb.binary(BinOp::ShiftRightLogical, s, two)
|
||||
}
|
||||
|
||||
/// Metal's decoded texel at `at` (a `uvec2`, within the base level) of 4:2:2 image `im`, a `vec4`
|
||||
/// of `f32` (module docs): four fetches with operands `ops` (the base level's), the codes, and
|
||||
/// their values.
|
||||
@@ -253,10 +301,8 @@ pub(crate) fn texel(
|
||||
ops: &ImageOperands,
|
||||
) -> BResult<SValue> {
|
||||
let u = u32_ty(fb)?;
|
||||
let f = f32_ty(fb)?;
|
||||
let uv2 = fb.module().ty(SType::Vector { elem: u, len: 2 })?;
|
||||
let uv4 = fb.module().ty(SType::Vector { elem: u, len: 4 })?;
|
||||
let fv4 = fb.module().ty(SType::Vector { elem: f, len: 4 })?;
|
||||
let x = fb.composite_extract(at, &[0])?;
|
||||
let y = fb.composite_extract(at, &[1])?;
|
||||
let one = ku(fb, 1)?;
|
||||
@@ -285,16 +331,6 @@ pub(crate) fn texel(
|
||||
seconds.push(fb.composite_extract(t, &[1])?);
|
||||
}
|
||||
// Each texel's first and second bytes' codes, `floor(255 v + 1/2)`.
|
||||
let codes = |fb: &mut FunctionBuilder<'_>, l: &[SValue]| -> BResult<SValue> {
|
||||
let v = fb.composite_construct(fv4, l)?;
|
||||
let k255 = kf(fb, 255.0)?;
|
||||
let k255 = fb.composite_construct(fv4, &[k255; 4])?;
|
||||
let half = kf(fb, 0.5)?;
|
||||
let half = fb.composite_construct(fv4, &[half; 4])?;
|
||||
let scaled = fb.binary(BinOp::FMul, v, k255)?;
|
||||
let nudged = fb.binary(BinOp::FAdd, scaled, half)?;
|
||||
fb.convert(ConvOp::ConvertFToU, nudged, uv4)
|
||||
};
|
||||
let first = codes(fb, &firsts)?;
|
||||
let second = codes(fb, &seconds)?;
|
||||
// `GBGR422`'s texels are `(Y, C)`, `BGRG422`'s `(C, Y)`.
|
||||
@@ -312,21 +348,108 @@ pub(crate) fn texel(
|
||||
let cr = lane(fb, chroma, 1)?;
|
||||
let cb_n = lane(fb, chroma, 2)?;
|
||||
let cr_n = lane(fb, chroma, 3)?;
|
||||
let three = ku(fb, 3)?;
|
||||
let two = ku(fb, 2)?;
|
||||
let mixed = |fb: &mut FunctionBuilder<'_>, c: SValue, n: SValue| -> BResult<SValue> {
|
||||
let c3 = fb.binary(BinOp::IMul, c, three)?;
|
||||
let s = fb.binary(BinOp::IAdd, c3, n)?;
|
||||
let s = fb.binary(BinOp::IAdd, s, two)?;
|
||||
fb.binary(BinOp::ShiftRightLogical, s, two)
|
||||
};
|
||||
let red = mixed(fb, cr, cr_n)?;
|
||||
let blue = mixed(fb, cb, cb_n)?;
|
||||
let red = mixed(fb, cr, cr_n, 1)?;
|
||||
let blue = mixed(fb, cb, cb_n, 1)?;
|
||||
let opaque = ku(fb, 255)?;
|
||||
let codes = fb.composite_construct(uv4, &[red, luma, blue, opaque])?;
|
||||
super::words::unorm_values(fb, codes, [8; 4])
|
||||
}
|
||||
|
||||
/// Metal's decoded texels at columns `x0` and `x1` (`u32`s within the base level, `x1` either
|
||||
/// `x0` or `x0 + 1`) of row `y` (a `u32`) of 4:2:2 image `im`, each a `vec4` of `f32` (module
|
||||
/// docs): six fetches with operands `ops` (the base level's), the pair `b` of `x0` and the pairs
|
||||
/// on either side of it (the pair itself at the edges), whose texels' codes are converted once.
|
||||
/// `x0` takes its own pair and the left one where even, the right one where odd; `x1` is `x0`, or
|
||||
/// `x0 + 1`, which takes pair `b` and the right one where odd, the right one and pair `b` where
|
||||
/// even. Each texel's codes and values are [`texel`]'s.
|
||||
pub(crate) fn row(
|
||||
fb: &mut FunctionBuilder<'_>,
|
||||
im: &Image,
|
||||
(x0, x1): (SValue, SValue),
|
||||
y: SValue,
|
||||
ops: &ImageOperands,
|
||||
) -> BResult<[SValue; 2]> {
|
||||
let u = u32_ty(fb)?;
|
||||
let uv2 = fb.module().ty(SType::Vector { elem: u, len: 2 })?;
|
||||
let uv4 = fb.module().ty(SType::Vector { elem: u, len: 4 })?;
|
||||
let one = ku(fb, 1)?;
|
||||
let zero = ku(fb, 0)?;
|
||||
// The pair of `x0`, and the pairs on its left and on its right.
|
||||
let b = fb.binary(BinOp::ShiftRightLogical, x0, one)?;
|
||||
let floor = fb.glsl(GlslOp::UMax, &[b, one])?;
|
||||
let left = fb.binary(BinOp::ISub, floor, one)?;
|
||||
let up = fb.binary(BinOp::IAdd, b, one)?;
|
||||
let right = fb.glsl(GlslOp::UMin, &[up, im.last_pair])?;
|
||||
// The six texels: the left pair's two, `b`'s, then the right pair's.
|
||||
let (mut firsts, mut seconds) = (Vec::with_capacity(6), Vec::with_capacity(6));
|
||||
for pair in [left, b, right] {
|
||||
let twice = fb.binary(BinOp::ShiftLeftLogical, pair, one)?;
|
||||
let odd = fb.binary(BinOp::BitwiseOr, twice, one)?;
|
||||
for column in [twice, odd] {
|
||||
let site = fb.composite_construct(uv2, &[column, y])?;
|
||||
let t = fb.fetch(im.img, site, ops)?;
|
||||
firsts.push(fb.composite_extract(t, &[0])?);
|
||||
seconds.push(fb.composite_extract(t, &[1])?);
|
||||
}
|
||||
}
|
||||
// Each texel's first and second bytes' codes, four lanes a conversion: the first four
|
||||
// texels' firsts and seconds, then the right pair's firsts and seconds.
|
||||
let first = codes(fb, &firsts[..4])?;
|
||||
let second = codes(fb, &seconds[..4])?;
|
||||
let edge = codes(fb, &[firsts[4], firsts[5], seconds[4], seconds[5]])?;
|
||||
// `GBGR422`'s texels are `(Y, C)`, `BGRG422`'s `(C, Y)`.
|
||||
let bt = fb.module().ty(SType::Bool)?;
|
||||
let bv4 = fb.module().ty(SType::Vector { elem: bt, len: 4 })?;
|
||||
let order = fb.composite_construct(bv4, &[im.bgrg; 4])?;
|
||||
let luma = fb.select(order, second, first)?;
|
||||
let chroma = fb.select(order, first, second)?;
|
||||
let swapped = fb.vector_shuffle(edge, edge, &[2, 3, 0, 1])?;
|
||||
let edge_luma = fb.select(order, swapped, edge)?;
|
||||
let edge_chroma = fb.select(order, edge, swapped)?;
|
||||
let lane = |fb: &mut FunctionBuilder<'_>, v: SValue, i: u32| fb.composite_extract(v, &[i]);
|
||||
// Each pair's `Cb` and `Cr`, and the lumas of `b` and of the right pair's first texel.
|
||||
let (cb_l, cr_l) = (lane(fb, chroma, 0)?, lane(fb, chroma, 1)?);
|
||||
let (cb_b, cr_b) = (lane(fb, chroma, 2)?, lane(fb, chroma, 3)?);
|
||||
let (cb_r, cr_r) = (lane(fb, edge_chroma, 0)?, lane(fb, edge_chroma, 1)?);
|
||||
let (y0_b, y1_b) = (lane(fb, luma, 2)?, lane(fb, luma, 3)?);
|
||||
let y0_r = lane(fb, edge_luma, 0)?;
|
||||
let parity = |fb: &mut FunctionBuilder<'_>, x: SValue| -> BResult<SValue> {
|
||||
let p = fb.binary(BinOp::BitwiseAnd, x, one)?;
|
||||
fb.binary(BinOp::INotEqual, p, zero)
|
||||
};
|
||||
// `x0`: pair `b`, and the right pair where odd, the left one where even.
|
||||
let odd0 = parity(fb, x0)?;
|
||||
let luma0 = fb.select(odd0, y1_b, y0_b)?;
|
||||
let cr_side0 = fb.select(odd0, cr_r, cr_l)?;
|
||||
let cb_side0 = fb.select(odd0, cb_r, cb_l)?;
|
||||
// `x1`: as `x0` where it lies in pair `b`; else the right pair's first texel, whose side is
|
||||
// pair `b`.
|
||||
let odd1 = parity(fb, x1)?;
|
||||
let p1 = fb.binary(BinOp::ShiftRightLogical, x1, one)?;
|
||||
let same = fb.binary(BinOp::IEqual, p1, b)?;
|
||||
let luma_b1 = fb.select(odd1, y1_b, y0_b)?;
|
||||
let luma1 = fb.select(same, luma_b1, y0_r)?;
|
||||
let cr_own1 = fb.select(same, cr_b, cr_r)?;
|
||||
let cb_own1 = fb.select(same, cb_b, cb_r)?;
|
||||
let cr_even1 = fb.select(same, cr_l, cr_b)?;
|
||||
let cb_even1 = fb.select(same, cb_l, cb_b)?;
|
||||
let cr_side1 = fb.select(odd1, cr_r, cr_even1)?;
|
||||
let cb_side1 = fb.select(odd1, cb_r, cb_even1)?;
|
||||
// Both texels' red and blue at once: each lane's own pair's code and its side's.
|
||||
let own = fb.composite_construct(uv4, &[cr_b, cb_b, cr_own1, cb_own1])?;
|
||||
let side = fb.composite_construct(uv4, &[cr_side0, cb_side0, cr_side1, cb_side1])?;
|
||||
let m = mixed(fb, own, side, 4)?;
|
||||
let opaque = ku(fb, 255)?;
|
||||
let mut texels = [zero; 2];
|
||||
for (k, l) in [luma0, luma1].into_iter().enumerate() {
|
||||
let red = lane(fb, m, 2 * k as u32)?;
|
||||
let blue = lane(fb, m, 2 * k as u32 + 1)?;
|
||||
let codes = fb.composite_construct(uv4, &[red, l, blue, opaque])?;
|
||||
texels[k] = super::words::unorm_values(fb, codes, [8; 4])?;
|
||||
}
|
||||
Ok(texels)
|
||||
}
|
||||
|
||||
/// A sampled read's texel `native` (the device's fetch, a `vec4` of `f32`) at `at` (a `uvec2`)
|
||||
/// with operands `ops` of image `img`, whose texture is `bound`: Metal's decoded texel where the
|
||||
/// function decodes there ([`held`]), else `native`.
|
||||
|
||||
Reference in new issue
Block a user