Files
effectcraft/crates/gpu/src/tests/fx_pixel2.rs
T
.CooCoo d6c9ad6643 Cineon Converter: Gamma 1 gives linear output and saved projects keep their render (#2312)
## Behavior

A new Cineon Converter opens with Gamma 1.7. Its Version parameter is 2 by default. A converter saved before Version existed opens as version 1, retaining its Gamma value, keyframes, and expression text and rendering on its original scale. Reset keeps the instance's Version.

In version 2:

- Log to Linear raises the exposure to `1 / gamma` before subtracting the black offset and normalizing: `(E^(1/g) - O^(1/g)) / (1 - O^(1/g))`, with `E = 10^((code - white) * 0.002 / 0.6)` and `O` the exposure at the black point. Gamma 1 gives linear output, and a larger Gamma lightens the midtones. Highlight Rolloff then applies as before, and Linear to Log is the inverse.
- Log to Log returns code values unchanged at Gamma 1 and raises the normalized code value to `1 / gamma` at other values. With custom black or white points, the code values first go through the Gamma 1 conversion.

The CPU and GPU paths match. Version 1 instances render as before, and Interpret Footage ▸ Cineon Settings keeps its existing formula, which agrees with the version 2 Converter at Gamma 1.

The public Cineon Converter documentation (https://helpx.adobe.com/after-effects/using/utility-effects.html) describes Gamma as a midtone control and gives no defaults or formula. The default, the Log to Linear curve and the Log to Log behavior come from running and scripting After Effects 26.5 on my own machine and reading back the rendered pixels. In After Effects, choosing Log to Log also sets Gamma to 1 and Highlight Rolloff to 0; EffectCraft does not do that yet, because no command sets one parameter from another.

Animation preset format 3 records `cineonVersion` for a saved Gamma property and stores effect key times relative to the first key. Presets in formats 1 and 2 without that tag contain version 1 Gamma values; format 2 already has relative effect key times. Applying a whole old Cineon Converter also gives it version 1. Exposed Essential Properties Gamma values convert to the target converter version where their target is unambiguous (the conversion keeps the linear point; other Gamma values follow each version's own curve); enabled expressions on another scale are skipped instead of rewriting their source.

A saved Gamma records the Version its converter has at the current time. Applied to a converter whose Version is keyframed, a Gamma preset keeps those keys: the Gamma is converted to the one version they hold, or skipped when they hold several or an expression sets the Version. A preset that also holds the Version sets it from that property.

The project schema remains 2. `docs/architecture.md` describes the behavior above.

## Validation

Effect tests check version 2 Log to Linear at Gamma 1.7 (codes 95 to 685) and Gamma 2.2 (codes 180 to 620), and Log to Log at Gamma 1 (Rolloff 0 and 20) and Gamma 1.7, against the measured 8-bit values within one or two codes, plus round trips, version 1 rendering and the Interpret Footage agreement at Gamma 1. GPU tests compare the version 2 paths with the CPU. Engine tests cover old projects, Reset, presets, keyframes, and Essential Properties.

Commands run on `74d398c2`:

- `cargo fmt --all -- --check`
- `cargo clippy -p effectcraft-effects -p effectcraft-gpu -p effectcraft-project -p effectcraft-engine --all-targets -- -D warnings`
- `cargo test -p effectcraft-effects`: 546 passed (including `effects_doc_is_current`, so `docs/effects.md` needs no change), 1 ignored, plus 6
- `cargo test -p effectcraft-project`: 47 passed
- `cargo test -p effectcraft-gpu`: 253 passed, 1 ignored
- `cargo test -p effectcraft-engine`: 1,007 passed, 5 ignored, plus 27
2026-11-15 12:11:07 +01:00

304 lines
16 KiB
Rust

//! GPU pixel ports of part C (CC Cross / Radial / Vector Blur, Reduce Interlace Flicker, CC
//! Composite, Color Link, Cineon Converter, HDR Compander, HDR Highlight Compression, Grow
//! Bounds, CC Overbrights, CC Block Load, CC Burn Film, CC Glass, CC HexTile, CC Mr. Smoothie,
//! CC Plastic, Inner/Outer Key, CC Simple Wire Removal, Basic 3D) vs the CPU effects (the
//! oracle): direct on a buffer at full and half resolution, as adjustment, and composited at 8
//! and 32 bpc. Also the helpers for effects that read masks, other layers and audio.
use effectcraft_effects::{Buf, EffectEnv, EffectHost, LayerPixels, MaskShape};
use effectcraft_keyframe::{ShapePath, Value};
use effectcraft_project::build::{self, Ids};
use effectcraft_project::{BitDepth, MaskMode};
use effectcraft_render::RenderOpts;
use effectcraft_time::Tick;
use crate::tests::{Scene, allow_llvmpipe_boundary_flips, c, check, compare_at, diff, effect_case, gpu, n, opts, pattern, set};
pub(crate) fn e(v: u32) -> Value {
Value::Enum(v)
}
pub(crate) fn on() -> Value {
Value::Bool(true)
}
pub(crate) fn off() -> Value {
Value::Bool(false)
}
pub(crate) fn pt(x: f64, y: f64) -> Value {
Value::Vec2([x, y])
}
/// Size of the test layer (layer pixels).
const SIZE: [f64; 2] = [70.0, 44.0];
/// The test layer's masks (layer pixels): a closed pentagon, a closed quad and an open curve.
pub(crate) fn mask_points() -> Vec<(Vec<[f64; 2]>, bool)> {
let pent = (0..5)
.map(|i| {
let a = i as f64 * std::f64::consts::TAU / 5.0 - 1.3;
[33.3 + a.cos() * 17.1, 22.4 + a.sin() * 15.2]
})
.collect();
vec![
(pent, true),
(vec![[6.3, 5.1], [62.2, 7.7], [59.4, 39.6], [9.1, 37.2]], true),
(vec![[4.2, 30.3], [18.7, 12.4], [33.1, 28.9], [47.6, 9.8], [64.2, 25.5]], false),
]
}
/// Another layer (`id` 9) and an audio track for layer parameters.
pub(crate) struct Host;
impl EffectHost for Host {
fn layer(&self, id: u64, _: bool) -> Option<LayerPixels> {
Some(LayerPixels { buf: Buf { img: pattern(id as u32 + 3, 70, 44), offset: [0.0; 2], scale: 1.0 }, size: SIZE })
}
fn audio(&self, _: u64, start: f64, frames: usize, rate: u32) -> Option<Vec<f32>> {
Some(
(0..frames)
.flat_map(|i| {
let t = start + i as f64 / rate as f64;
let l = 0.5 * (t * 440.0 * std::f64::consts::TAU).sin() + 0.2 * (t * 1300.0 * std::f64::consts::TAU).sin();
let r = 0.4 * (t * (200.0 + 300.0 * t) * std::f64::consts::TAU).sin();
[l as f32, r as f32]
})
.collect(),
)
}
}
/// The effect alone on a buffer with the test masks and [`Host`]: GPU vs CPU (32-bit float)
/// at full and half resolution (the half buffer padded and offset), direct and as adjustment.
/// `allow` = fraction of pixels allowed over 1e-3.
pub(crate) fn direct_env(id: &str, vals: &[(&str, Value)], allow: f64) {
let Some(g) = gpu() else { return };
let spec = effectcraft_effects::find(id).unwrap();
let mut params =
effectcraft_effects::Params { values: spec.params.iter().map(|ps| (ps.id.to_string(), effectcraft_effects::default_value(ps, SIZE))).collect() };
for (k, v) in vals {
assert!(params.values.contains_key(*k), "{id}: no {k}");
params.values.insert(k.to_string(), v.clone());
}
let masks: Vec<MaskShape> = mask_points()
.into_iter()
.enumerate()
.map(|(i, (points, closed))| MaskShape { name: format!("Mask {}", i + 1), points, closed, inverted: false })
.collect();
let host = Host;
for adjustment in [false, true] {
for scale in [1.0, 0.5] {
let img = pattern(7, (SIZE[0] * scale) as u32, (SIZE[1] * scale) as u32);
let mut buf = Buf { img, offset: [0.0, 0.0], scale };
if scale < 1.0 {
buf.pad(3);
}
let env = EffectEnv { masks: &masks, host: Some(&host), comp_time: 0.25, frame_rate: 30.0, ..Default::default() };
let ctx = || effectcraft_effects::EffectCtx { params: &params, time: 0.25, layer_size: SIZE, seed: 11, adjustment, env };
let cpu = (spec.render)(&ctx(), buf.clone());
let out =
effectcraft_render::Accelerator::effects(g, &[effectcraft_render::FxStep { spec, ctx: ctx() }], &buf, None).expect("the GPU runs the effect");
assert_eq!((out.offset, out.scale), (cpu.offset, cpu.scale), "{id}: geometry");
let d = diff(&cpu.img, &out.img, 1e-3);
assert!(
d.over as f64 <= allow * d.total as f64,
"{id} {vals:?} adj {adjustment} scale {scale}: {} of {} pixels over 1e-3 (max {}); worst {:?}",
d.over,
d.total,
d.max,
d.worst
);
}
}
}
/// [`direct_env`], then a footage layer with the test masks (mode None) and the effect,
/// composited over a background at 8 and 32 bpc, full and half resolution.
pub(crate) fn env_case(id: &str, vals: &[(&str, Value)]) {
env_case_allow(id, vals, 0.0);
}
/// [`env_case`] allowing a fraction of pixels over the tolerance (documented exceptions).
pub(crate) fn env_case_allow(id: &str, vals: &[(&str, Value)], allow: f64) {
env_case_at(id, vals, allow, Tick::ZERO);
}
/// [`env_case_allow`] with the composited frames at comp time `t`.
pub(crate) fn env_case_at(id: &str, vals: &[(&str, Value)], allow: f64, t: Tick) {
direct_env(id, vals, allow);
for depth in [BitDepth::Bpc8, BitDepth::Bpc32] {
let mut s = Scene::new(depth);
let bg = s.solid([0.15, 0.1, 0.2], 97, 61);
s.push(bg);
let mut l = s.footage(70, 44);
let mut next = s.p.next_id;
for (i, (pts, closed)) in mask_points().into_iter().enumerate() {
let m = build::mask(&mut Ids(&mut next), &format!("Mask {}", i + 1), ShapePath::polygon(&pts, closed), MaskMode::None, [255, 255, 0]);
l.props.sub_mut("masks").unwrap().children.push(m.into());
}
s.p.next_id = next;
s.effect(&mut l, id, vals);
set(&mut l, "transform/rotation", Value::Scalar(8.0));
s.push(l);
check(&format!("{id} {vals:?} {depth:?}"), compare_at(&s, opts(), t), allow);
check(&format!("{id} {vals:?} {depth:?} half"), compare_at(&s, RenderOpts { scale: 0.5, ..opts() }, t), allow);
}
}
#[test]
fn blurs() {
effect_case("ec.blur.cccross", &[]);
effect_case("ec.blur.cccross", &[("radiusX", n(4.0)), ("radiusY", n(0.0)), ("transferMode", e(1))]);
effect_case("ec.blur.cccross", &[("radiusX", n(3.0)), ("radiusY", n(7.0)), ("transferMode", e(2))]);
effect_case("ec.blur.cccross", &[("radiusX", n(6.0)), ("radiusY", n(2.0)), ("transferMode", e(3))]);
effect_case("ec.blur.ccradial", &[]);
effect_case("ec.blur.ccradial", &[("type", e(0)), ("amount", n(-40.0)), ("center", pt(20.0, 30.0))]);
effect_case("ec.blur.ccradial", &[("type", e(2)), ("amount", n(60.0)), ("quality", n(20.0))]);
effect_case("ec.blur.ccradial", &[("type", e(3)), ("amount", n(30.0))]);
effect_case("ec.blur.ccradial", &[("type", e(4)), ("amount", n(-50.0)), ("quality", n(80.0))]);
effect_case("ec.blur.ccvector", &[]);
effect_case("ec.blur.ccvector", &[("type", e(1)), ("amount", n(6.0)), ("angleOffset", n(30.0))]);
effect_case("ec.blur.ccvector", &[("type", e(2)), ("mapSoftness", n(0.0))]);
effect_case("ec.blur.ccvector", &[("type", e(3)), ("amount", n(5.0))]);
effect_case("ec.blur.ccvector", &[("type", e(4)), ("amount", n(12.0)), ("mapSoftness", n(40.0))]);
effect_case("ec.blur.reduceflicker", &[("softness", n(3.0))]);
effect_case("ec.blur.reduceflicker", &[("softness", n(0.5))]);
}
#[test]
fn composites_and_colour() {
for m in [0, 1, 2, 3, 5, 8, 11, 14, 15] {
effect_case("ec.channel.cccomposite", &[("compositeOriginal", e(m)), ("opacity", n(70.0))]);
}
effect_case("ec.channel.cccomposite", &[("compositeOriginal", e(4)), ("rgbOnly", on())]);
effect_case("ec.color.colorlink", &[]);
effect_case("ec.color.colorlink", &[("sampleSource", e(2)), ("clip", n(10.0)), ("blendingMode", e(3)), ("opacity", n(60.0))]);
effect_case("ec.color.colorlink", &[("sampleSource", e(1)), ("blendingMode", e(11)), ("stencilOriginalAlpha", off())]);
effect_case("ec.color.colorlink", &[("sampleSource", e(9)), ("clip", n(20.0))]);
effect_case("ec.color.colorlink", &[("sampleSource", e(6)), ("opacity", n(50.0)), ("stencilOriginalAlpha", off())]);
env_case("ec.color.colorlink", &[("sourceLayer", Value::Layer(Some(9))), ("sampleSource", e(4)), ("clip", n(5.0))]);
effect_case("ec.utility.cineon", &[]);
effect_case("ec.utility.cineon", &[("conversionType", e(0)), ("gamma", n(2.2))]);
effect_case("ec.utility.cineon", &[("version", n(1.0)), ("gamma", n(2.2))]);
effect_case("ec.utility.cineon", &[("version", n(1.0)), ("gamma", n(0.0)), ("conversionType", e(0))]);
effect_case("ec.utility.cineon", &[("conversionType", e(2)), ("highlightRolloff", n(60.0)), ("tenBitWhitePoint", n(800.0))]);
effect_case("ec.utility.cineon", &[("conversionType", e(1)), ("gamma", n(1.7)), ("highlightRolloff", n(0.0))]);
effect_case("ec.utility.cineon", &[("conversionType", e(1)), ("gamma", n(2.2)), ("highlightRolloff", n(0.0))]);
effect_case("ec.utility.cineon", &[("conversionType", e(2)), ("gamma", n(1.0)), ("highlightRolloff", n(0.0))]);
effect_case("ec.utility.cineon", &[("conversionType", e(2)), ("gamma", n(1.7))]);
env_case_allow("ec.utility.cineon", &[("version", n(1.0)), ("conversionType", e(2)), ("gamma", n(2.2))], 0.005);
effect_case("ec.utility.hdrcompander", &[("gain", n(4.0)), ("gamma", n(2.0))]);
effect_case("ec.utility.hdrcompander", &[("mode", e(1)), ("gain", n(3.0)), ("gamma", n(1.5))]);
effect_case("ec.utility.hdrcompression", &[]);
effect_case("ec.utility.hdrcompression", &[("amount", n(40.0))]);
effect_case("ec.utility.hdrcompression", &[("amount", n(90.0))]);
// A NaN Amount keeps the layer (the CPU returns it unchanged).
effect_case("ec.utility.hdrcompression", &[("amount", n(f64::NAN))]);
effect_case("ec.utility.growbounds", &[("pixels", n(9.0))]);
effect_case("ec.utility.ccoverbrights", &[]);
for ch in 1..=5 {
effect_case("ec.utility.ccoverbrights", &[("channel", e(ch)), ("highlightColor", c(0.2, 0.9, 0.4))]);
}
}
/// HDR Highlight Compression on over-range input (HDR Compander expanding first), where the
/// compressed channel clips at 1 before the Amount blend.
#[test]
fn hdr_compression_over_range() {
for amount in [100.0, 60.0] {
let mut s = Scene::new(BitDepth::Bpc32);
let bg = s.solid([0.15, 0.1, 0.2], 97, 61);
s.push(bg);
let mut l = s.footage(70, 44);
s.effect(&mut l, "ec.utility.hdrcompander", &[("mode", e(1)), ("gain", n(4.0)), ("gamma", n(1.0))]);
s.effect(&mut l, "ec.utility.hdrcompression", &[("amount", n(amount))]);
s.push(l);
check(&format!("hdr compression over range {amount}"), compare_at(&s, opts(), Tick::ZERO), 0.0);
}
}
#[test]
fn cineon_version_2_paths_match_cpu() {
for vals in [
&[("conversionType", e(2)), ("gamma", n(1.0)), ("highlightRolloff", n(0.0))][..],
&[("conversionType", e(2)), ("gamma", n(1.0)), ("highlightRolloff", n(20.0))][..],
&[("conversionType", e(2)), ("gamma", n(1.7)), ("highlightRolloff", n(20.0))][..],
&[("conversionType", e(1)), ("gamma", n(2.2)), ("highlightRolloff", n(0.0))][..],
] {
env_case_allow("ec.utility.cineon", vals, 0.005);
}
let Some(g) = gpu() else { return };
let spec = effectcraft_effects::find("ec.utility.cineon").unwrap();
let mut params =
effectcraft_effects::Params { values: spec.params.iter().map(|ps| (ps.id.to_string(), effectcraft_effects::default_value(ps, [1.0, 1.0]))).collect() };
params.values.insert("gamma".into(), n(1.7));
params.values.insert("highlightRolloff".into(), n(0.0));
let mut img = effectcraft_raster::Image::new(1, 1);
img.set(0, 0, [400.0 / 1023.0, 400.0 / 1023.0, 400.0 / 1023.0, 1.0]);
let buf = Buf { img, offset: [0.0; 2], scale: 1.0 };
let ctx = effectcraft_effects::EffectCtx { params: &params, time: 0.0, layer_size: [1.0, 1.0], seed: 0, adjustment: false, env: EffectEnv::default() };
let out = effectcraft_render::Accelerator::effects(g, &[effectcraft_render::FxStep { spec, ctx }], &buf, None).expect("the GPU runs the effect");
let got = (out.img.get(0, 0)[0].clamp(0.0, 1.0) * 255.0).round() as i32;
assert!((got - 57).abs() <= 1, "code 400: {got} vs 57");
}
#[test]
fn stylize() {
let _flips = allow_llvmpipe_boundary_flips();
for (done, flags) in
[(30.0, [true, true, true, false]), (55.0, [false, true, false, true]), (70.0, [true, false, false, false]), (10.0, [false, false, true, true])]
{
effect_case(
"ec.stylize.ccblockload",
&[
("completion", n(done)),
("scanlines", Value::Bool(flags[0])),
("smoothing", Value::Bool(flags[1])),
("startCleared", Value::Bool(flags[2])),
("bilinear", Value::Bool(flags[3])),
],
);
}
effect_case("ec.stylize.ccburnfilm", &[("burn", n(30.0))]);
effect_case("ec.stylize.ccburnfilm", &[("burn", n(60.0)), ("center", pt(20.0, 15.0)), ("randomSeed", n(4.0))]);
effect_case("ec.stylize.ccglass", &[]);
effect_case("ec.stylize.ccglass", &[("lightType", e(1)), ("displacement", n(-60.0)), ("property", e(4)), ("softness", n(4.0))]);
effect_case("ec.stylize.ccglass", &[("height", n(0.0)), ("lightColor", c(1.0, 0.8, 0.5)), ("metal", n(60.0))]);
effect_case("ec.stylize.ccplastic", &[]);
effect_case("ec.stylize.ccplastic", &[("lightType", e(1)), ("cutMin", n(20.0)), ("cutMax", n(70.0)), ("property", e(5))]);
env_case("ec.stylize.ccplastic", &[("bumpLayer", Value::Layer(Some(9))), ("property", e(0)), ("roughness", n(0.2))]);
effect_case("ec.stylize.cchextile", &[("radius", n(9.3))]);
effect_case("ec.stylize.cchextile", &[("radius", n(7.7)), ("render", e(1)), ("rotate", n(17.0)), ("smearing", n(30.0))]);
effect_case("ec.stylize.cchextile", &[("radius", n(12.1)), ("render", e(2)), ("center", pt(31.3, 19.7))]);
effect_case("ec.stylize.ccmrsmoothie", &[]);
effect_case("ec.stylize.ccmrsmoothie", &[("property", e(6)), ("smoothness", n(5.0)), ("colorLoop", e(2)), ("phase", n(40.0))]);
env_case("ec.stylize.ccmrsmoothie", &[("flowLayer", Value::Layer(Some(9))), ("property", e(1))]);
}
#[test]
fn keys_and_3d() {
env_case("ec.key.innerouter", &[]);
env_case("ec.key.innerouter", &[("foreground", e(1))]);
direct_env("ec.key.innerouter", &[("foreground", e(1)), ("background", e(2))], 0.0);
env_case("ec.key.innerouter", &[("foreground", e(1)), ("background", e(2)), ("edgeFeather", n(3.0)), ("edgeThin", n(1.5))]);
env_case("ec.key.innerouter", &[("foreground", e(1)), ("singleMaskHighlightRadius", n(4.0)), ("invertExtraction", on()), ("blendWithOriginal", n(20.0))]);
env_case(
"ec.key.innerouter",
&[
("foreground", e(2)),
("edgeThin", n(-2.3)),
("edgeThreshold", n(30.0)),
("cleanupForeground/cleanupForeground1/path", e(3)),
("cleanupBackground/cleanupBackground1/path", e(1)),
],
);
effect_case("ec.key.ccsimplewireremoval", &[]);
effect_case("ec.key.ccsimplewireremoval", &[("removalStyle", e(0)), ("thickness", n(8.0)), ("slope", n(50.0))]);
effect_case("ec.key.ccsimplewireremoval", &[("removalStyle", e(1)), ("mirrorBlend", n(60.0)), ("pointB", pt(60.0, 10.0))]);
effect_case("ec.key.ccsimplewireremoval", &[("removalStyle", e(1)), ("frameOffset", n(-2.0)), ("thickness", n(6.0))]);
effect_case("ec.key.ccsimplewireremoval", &[("removalStyle", e(3)), ("mirrorBlend", n(30.0)), ("slope", n(20.0))]);
effect_case("ec.obsolete.basic3d", &[("swivel", n(25.0))]);
effect_case("ec.obsolete.basic3d", &[("tilt", n(-30.0)), ("swivel", n(10.0)), ("specularHighlight", on())]);
effect_case("ec.obsolete.basic3d", &[("distanceToImage", n(20.0)), ("preview/drawPreviewWireframe", on()), ("tilt", n(15.0))]);
}