alpha2-render 0.10.0

Render graph and effect registry for alpha2.
Documentation
//! Internal effects behind clip shape masks, track mattes and key mixes.
//!
//! The timeline engine inserts these from a clip's `masks` and `matte`. Mask outlines are drawn
//! as coverage by the vector pass and feathered by the Gaussian blur; these passes combine the
//! coverages and apply them to the clip.

use std::fmt::Write as _;

use alpha2_gpu::CapabilityTier;

use crate::{EffectDef, EffectKind, InputSpec, ParamSpec, ParamType, ParamUnit, ParamValue};

/// Internal identifier of the track-matte pass.
pub const TRACK_MATTE_EFFECT_ID: &str = "alpha2.internal.track-matte";

/// Internal identifier of the key-mix pass whose coverage is its opacity times a mask's alpha.
pub const KEY_MIX_EFFECT_ID: &str = "alpha2.internal.key-mix";

/// Internal identifier of the key-mix pass whose coverage is its opacity alone.
pub const KEY_MIX_UNMASKED_EFFECT_ID: &str = "alpha2.internal.key-mix-unmasked";

/// Named input of a key-mix pass holding the picture its source replaces.
pub const KEY_MIX_BACKDROP: &str = "backdrop";

/// Named input of [`KEY_MIX_EFFECT_ID`] whose alpha scales the coverage.
pub const KEY_MIX_MASK: &str = "mask";

/// Parameter of a key-mix pass scaling the coverage, in `0..=1`.
pub const KEY_MIX_OPACITY: &str = "opacity";

/// Most masks one mask-combine pass takes, each a named input.
pub const MAX_SHAPE_MASKS: usize = 8;

const _: () = assert!(MAX_SHAPE_MASKS <= crate::MAX_EFFECT_INPUTS);

/// Enum values of a mask's `mode{i}` parameter.
pub const MASK_MODES: [&str; 3] = ["add", "subtract", "intersect"];

/// Mask-combine pass ids; the pass for `n` masks is `MASK_COMBINE_EFFECT_IDS[n - 1]`.
pub const MASK_COMBINE_EFFECT_IDS: [&str; MAX_SHAPE_MASKS] = [
    "alpha2.internal.mask-combine-1",
    "alpha2.internal.mask-combine-2",
    "alpha2.internal.mask-combine-3",
    "alpha2.internal.mask-combine-4",
    "alpha2.internal.mask-combine-5",
    "alpha2.internal.mask-combine-6",
    "alpha2.internal.mask-combine-7",
    "alpha2.internal.mask-combine-8",
];

/// Names of one mask's coverage input and parameters in [`mask_combine_definition`].
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct MaskCombineNames {
    /// Named input with the mask's coverage in alpha: `m{i}`.
    pub input: &'static str,
    /// `invert{i}`.
    pub invert: &'static str,
    /// `mode{i}`.
    pub mode: &'static str,
}

macro_rules! mask_combine_names {
    ($($index:literal),*) => {
        [$(MaskCombineNames {
            input: concat!("m", $index),
            invert: concat!("invert", $index),
            mode: concat!("mode", $index),
        }),*]
    };
}

/// Each mask's names by index, static so callers need not format them.
pub const MASK_COMBINE_NAMES: [MaskCombineNames; MAX_SHAPE_MASKS] =
    mask_combine_names!(0, 1, 2, 3, 4, 5, 6, 7);

fn param(name: String, param_type: ParamType, default: ParamValue) -> ParamSpec {
    ParamSpec {
        name,
        param_type,
        default,
        min: None,
        max: None,
        step: None,
        unit: match param_type {
            ParamType::Bool => ParamUnit::Bool,
            ParamType::Enum => ParamUnit::Enum,
            _ => ParamUnit::Normalized,
        },
        enum_values: if param_type == ParamType::Enum {
            MASK_MODES.map(str::to_owned).to_vec()
        } else {
            Vec::new()
        },
    }
}

/// The pass combining `count` masks (1 to [`MAX_SHAPE_MASKS`]) into its source's coverage.
///
/// Mask `i` is input `m{i}` (coverage in alpha), inverted when `invert{i}` is set, and combined
/// by `mode{i}`: coverage starts empty when the first mask adds and full otherwise; add is
/// `c + m - c m`, subtract `c (1 - m)`, intersect `c m`. The source is multiplied by the result.
///
/// # Panics
/// When `count` is zero or more than [`MAX_SHAPE_MASKS`].
#[must_use]
pub fn mask_combine_definition(count: usize) -> EffectDef {
    assert!(
        (1..=MAX_SHAPE_MASKS).contains(&count),
        "a mask-combine pass takes 1 to {MAX_SHAPE_MASKS} masks"
    );
    let names = &MASK_COMBINE_NAMES[..count];
    let mut params = Vec::with_capacity(count * 2);
    let mut steps = String::new();
    for names in names {
        params.push(param(
            names.invert.into(),
            ParamType::Bool,
            ParamValue::Bool(false),
        ));
        params.push(param(
            names.mode.into(),
            ParamType::Enum,
            ParamValue::Enum(0),
        ));
        writeln!(
            steps,
            "    coverage = builtin_mask_step(coverage, sample_{input}(uv).a, params.{invert}, params.{mode});",
            input = names.input,
            invert = names.invert,
            mode = names.mode,
        )
        .expect("writing to a String cannot fail");
    }
    EffectDef {
        id: MASK_COMBINE_EFFECT_IDS[count - 1].into(),
        version: 1,
        kind: EffectKind::Effect,
        inputs: names
            .iter()
            .map(|names| InputSpec {
                name: names.input.into(),
            })
            .collect(),
        params,
        resources: Vec::new(),
        wgsl_body: format!(
            r"fn builtin_mask_step(coverage: f32, mask: f32, invert: u32, mode: u32) -> f32 {{
    var m = clamp(mask, 0.0, 1.0);
    if (invert != 0u) {{ m = 1.0 - m; }}
    if (mode == 1u) {{ return coverage * (1.0 - m); }}
    if (mode == 2u) {{ return coverage * m; }}
    return coverage + m - coverage * m;
}}

fn effect(uv: vec2f, src: vec4f) -> vec4f {{
    // Empty when the first mask adds, full otherwise.
    var coverage = select(1.0, 0.0, params.mode0 == 0u);
{steps}    return src * clamp(coverage, 0.0, 1.0);
}}
"
        ),
        tier_min: CapabilityTier::TierB,
        license: Some(crate::builtins::ALPHA2_EFFECT_LICENSE.into()),
    }
}

/// Every mask-combine pass, for one to [`MAX_SHAPE_MASKS`] masks.
#[must_use]
pub fn mask_combine_definitions() -> Vec<EffectDef> {
    (1..=MAX_SHAPE_MASKS).map(mask_combine_definition).collect()
}

/// The track-matte pass: keeps the first input where the second's alpha (or luminance over
/// black, BT.709 weights on linear light times alpha) says, optionally inverted.
///
/// A two-input transition that ignores progress; pass a value inside `(0, 1)` so its body runs.
#[must_use]
pub fn track_matte_definition() -> EffectDef {
    EffectDef {
        id: TRACK_MATTE_EFFECT_ID.into(),
        version: 1,
        kind: EffectKind::Transition,
        inputs: Vec::new(),
        params: vec![
            ParamSpec {
                unit: ParamUnit::Bool,
                ..param("luma".into(), ParamType::Bool, ParamValue::Bool(false))
            },
            ParamSpec {
                unit: ParamUnit::Bool,
                ..param("inverted".into(), ParamType::Bool, ParamValue::Bool(false))
            },
        ],
        resources: Vec::new(),
        wgsl_body: r"fn builtin_matte_linear(value: f32) -> f32 {
    if (value <= 0.04045) { return value / 12.92; }
    return pow((value + 0.055) / 1.055, 2.4);
}

fn transition(uv: vec2f) -> vec4f {
    let picture = from_color(uv);
    let matte = to_color(uv);
    var coverage = matte.a;
    if (params.luma != 0u) {
        let straight = clamp(matte.rgb / max(matte.a, 0.000001), vec3f(0.0), vec3f(1.0));
        let linear = vec3f(
            builtin_matte_linear(straight.r),
            builtin_matte_linear(straight.g),
            builtin_matte_linear(straight.b),
        );
        coverage = dot(linear, vec3f(0.2126, 0.7152, 0.0722)) * matte.a;
    }
    if (params.inverted != 0u) { coverage = 1.0 - coverage; }
    return picture * clamp(coverage, 0.0, 1.0);
}
"
        .into(),
        tier_min: CapabilityTier::TierB,
        license: Some(crate::builtins::ALPHA2_EFFECT_LICENSE.into()),
    }
}

/// A key-mix pass: the `backdrop` input replaced by the source by `k = opacity x mask.a`
/// (`k = opacity` when not `masked`), as `(1 - k) backdrop + k source` on premultiplied values,
/// exactly the backdrop at `k = 0` and the source at `k = 1`.
#[must_use]
pub fn key_mix_definition(masked: bool) -> EffectDef {
    let (id, coverage) = if masked {
        (
            KEY_MIX_EFFECT_ID,
            format!("params.{KEY_MIX_OPACITY} * sample_{KEY_MIX_MASK}(uv).a"),
        )
    } else {
        (
            KEY_MIX_UNMASKED_EFFECT_ID,
            format!("params.{KEY_MIX_OPACITY}"),
        )
    };
    let inputs = std::iter::once(KEY_MIX_BACKDROP)
        .chain(masked.then_some(KEY_MIX_MASK))
        .map(|name| InputSpec { name: name.into() })
        .collect();
    EffectDef {
        id: id.into(),
        version: 1,
        kind: EffectKind::Effect,
        inputs,
        params: vec![ParamSpec {
            min: Some(0.0),
            max: Some(1.0),
            ..param(
                KEY_MIX_OPACITY.into(),
                ParamType::Float,
                ParamValue::Float(1.0),
            )
        }],
        resources: Vec::new(),
        wgsl_body: format!(
            "{}\nfn effect(uv: vec2f, src: vec4f) -> vec4f {{\n    \
             return builtin_key_mix(sample_{KEY_MIX_BACKDROP}(uv), src, {coverage});\n}}\n",
            alpha2_shaders::KEY_MIX
        ),
        tier_min: CapabilityTier::TierB,
        license: Some(crate::builtins::ALPHA2_EFFECT_LICENSE.into()),
    }
}

/// Both key-mix passes, with and without a mask.
#[must_use]
pub fn key_mix_definitions() -> [EffectDef; 2] {
    [key_mix_definition(true), key_mix_definition(false)]
}

#[cfg(test)]
mod tests {
    use super::{
        MASK_COMBINE_EFFECT_IDS, MASK_COMBINE_NAMES, MAX_SHAPE_MASKS, mask_combine_definition,
    };

    #[test]
    fn each_masks_static_names_are_the_ones_the_pass_declares() {
        for count in 1..=MAX_SHAPE_MASKS {
            let definition = mask_combine_definition(count);
            assert_eq!(definition.id, MASK_COMBINE_EFFECT_IDS[count - 1]);
            let params = definition
                .params
                .iter()
                .map(|spec| spec.name.clone())
                .collect::<Vec<_>>();
            let inputs = definition
                .inputs
                .iter()
                .map(|input| input.name.clone())
                .collect::<Vec<_>>();
            let mut expected_params = Vec::new();
            let mut expected_inputs = Vec::new();
            for (index, names) in MASK_COMBINE_NAMES[..count].iter().enumerate() {
                assert_eq!(names.input, format!("m{index}"));
                assert_eq!(names.invert, format!("invert{index}"));
                assert_eq!(names.mode, format!("mode{index}"));
                expected_inputs.push(names.input.to_owned());
                expected_params.push(names.invert.to_owned());
                expected_params.push(names.mode.to_owned());
            }
            assert_eq!(inputs, expected_inputs);
            assert_eq!(params, expected_params);
        }
    }
}