Effects: OCIO custom configs read Blender's v2 transforms and warn (#410)

Cause: the config reader didn't know ColorSpaceTransform or
ExponentWithLinearTransform and passed colours through them silently.
Blender's Linear Rec.709 -> sRGB therefore stayed in its E-white XYZ
reference space (the blue cast) without the sRGB curve. Display
colorspaces, view transforms, v2 views, looks, AllocationTransform,
BuiltinTransform and .spi1d / .spimtx files were missing too, and
nothing told the user.

Fix (from the OCIO v2 documentation and the CLF "moncurve", not OCIO's
source): ExponentWithLinear, ColorSpace, Look, DisplayView, Allocation
and Builtin transforms (identity, ACES AP0/AP1 matrices, ACEScct /
ACEScc, the sRGB / P3 / Rec.1886 / gamma / PQ displays). RangeTransform
now clamps and takes one-sided ranges. Display colorspaces meet the
scene reference through the default view transform. Added v2 views,
view looks, the looks section and .spi1d / .spi3d / .spimtx files. A
conversion is built once as a flat list of steps (Config::path /
display_path) for both the CPU effect and the GPU colour program (new
moncurve and clamp ops and the ACEScc curve; a program over the
kernel's 255 ops renders on the CPU). Named-transform resolution is
limited to 16 levels and 4096 steps, and YAML nesting to 64 levels, so
hostile or looping configs stop. Transforms that are still unsupported
(Grading Primary / Tone, ACES output, HLG…), unreadable configs and
unknown spaces are now reported: a warning under the effect in Effect
Controls, and the new effect.warning query.

Tests: ocio_config::blender_like_config_converts_linear_rec709_to_srgb
(CST, display colorspaces, v1 / v2 views, allocation, looks, builtin,
range, loops), moncurve_matches_srgb_and_inverts, builtin_styles,
yaml_nesting_is_bounded, ocio::spi_files_parse,
tests_effects::ocio_effect_warns_about_unsupported_config_transforms,
gpu tests_fx_lut::ocio_custom_config_v2 (GPU = CPU).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic
2026-10-09 23:57:12 +07:00
co-authored by Claude Opus 5.5
parent d6de68b20d
commit 492fa6de0a
11 changed files with 1483 additions and 233 deletions
+20 -2
View File
@@ -10,8 +10,10 @@
//! iterative 3D inverse): hardware 3D-texture filtering would round the interpolation weights
//! and cannot do tetrahedral interpolation. The CPU evaluates transfer functions, CDLs and
//! Color Profile Converter's matrix in f64; the kernel in f32 (within the 1e-3 tolerance).
//! Custom `.ocio` configurations compile their colour spaces' transforms (matrix and offset,
//! exponent, log / log-affine, range, CDL, file LUTs, groups, inverses) to the same program.
//! Custom `.ocio` configurations compile their conversions (matrix and offset, exponent,
//! exponent with linear segment, log / log-affine, allocation, range with its clamp, CDL, file
//! LUTs and matrices, built-in curves, with colour space / look / display-view transforms
//! resolved) to the same program; one longer than the kernel's 255 ops renders on the CPU.
use effectcraft_effects::{ColorOp, EffectCtx, Lut, Straight, Tf};
@@ -57,9 +59,13 @@ fn tf_code(t: Tf) -> [f32; 2] {
Tf::Gamma(g) => [2.0, g as f32],
Tf::AcesCct => [3.0, 0.0],
Tf::Pq => [4.0, 0.0],
Tf::AcesCc => [5.0, 0.0],
}
}
/// Most ops `fxl_run` interprets (its loop guard); longer programs render on the CPU.
const MAX_OPS: usize = 255;
/// A LUT in the layout `fxl_lut_*` read at the returned offset: n1, n3, domain min (3), domain
/// max (3), the 1D table (n1 × RGB), then the lattice (n3³ × RGB, red fastest).
pub(crate) fn push_lut(d: &mut Vec<f32>, lut: &Lut) -> f32 {
@@ -75,6 +81,9 @@ pub(crate) fn push_lut(d: &mut Vec<f32>, lut: &Lut) -> f32 {
/// Program layout: a stream of (opcode, arguments…) ending in 0, followed by LUT data.
fn encode(ops: &[ColorOp]) -> Option<Vec<f32>> {
if ops.len() > MAX_OPS {
return None;
}
let mut prog: Vec<f32> = vec![];
let mut tables: Vec<Vec<f32>> = vec![];
// LUT offsets are patched once the program's length is known.
@@ -123,6 +132,15 @@ fn encode(ops: &[ColorOp]) -> Option<Vec<f32>> {
prog.extend(log_slope.iter().chain(log_offset).chain(lin_slope).chain(lin_offset));
prog.extend([*inverse as u32 as f32, f64::MIN_POSITIVE.ln() as f32]);
}
ColorOp::MonCurve { gamma, offset, inverse, mirror } => {
prog.push(12.0);
prog.extend(gamma.iter().chain(offset));
prog.extend([*inverse as u32 as f32, *mirror as u32 as f32]);
}
ColorOp::Clamp { lo, hi } => {
prog.push(13.0);
prog.extend(lo.iter().chain(hi));
}
ColorOp::Lut { lut, shaper, interp, inverse } => {
prog.extend([8.0, 0.0, *interp as f32, *inverse as u32 as f32, -1.0]);
let mut t = vec![];
+67
View File
@@ -266,6 +266,16 @@ fn fxl_decode(tf: u32, g: f32, v: f32) -> f32 {
let l = powz(max(e - FXL_PQ_C1, 0.0) / (FXL_PQ_C2 - FXL_PQ_C3 * e), 1.0 / FXL_PQ_M1);
return l * 100.0;
}
case 5u: {
// ACEScc.
if (v < (-15.0 + 9.72) / 17.52) {
return (exp2(v * 17.52 - 9.72) - exp2(-16.0)) * 2.0;
}
if (v < (log2(65504.0) + 9.72) / 17.52) {
return exp2(v * 17.52 - 9.72);
}
return 65504.0;
}
default: {
return v;
}
@@ -297,6 +307,16 @@ fn fxl_encode(tf: u32, g: f32, v: f32) -> f32 {
let y = powz(max(v / 100.0, 0.0), FXL_PQ_M1);
return powz((FXL_PQ_C1 + FXL_PQ_C2 * y) / (1.0 + FXL_PQ_C3 * y), FXL_PQ_M2);
}
case 5u: {
// ACEScc.
if (v <= 0.0) {
return (-16.0 + 9.72) / 17.52;
}
if (v < exp2(-15.0)) {
return (log2(exp2(-16.0) + v * 0.5) + 9.72) / 17.52;
}
return (log2(v) + 9.72) / 17.52;
}
default: {
return v;
}
@@ -383,6 +403,37 @@ fn fxl_tonemap(v: f32, inverse: bool) -> f32 {
return (-b + sqrt(b * b - 4.0 * a * c)) / (2.0 * a);
}
// ExponentWithLinearTransform (ocio_config::moncurve): forward decodes; a gamma of at most 1 or
// a non-positive offset is a plain power curve.
fn fxl_moncurve(v0: f32, g: f32, o: f32, inv: bool, mirror: bool) -> f32 {
var v = v0;
var sign = 1.0;
if (mirror && v < 0.0) {
v = -v;
sign = -1.0;
}
if (g <= 1.0 || o <= 0.0) {
var p = g;
if (inv) {
p = 1.0 / max(g, 1e-9);
}
return sign * powz(max(v, 0.0), p);
}
let xb = o / (g - 1.0);
let yb = powz((xb + o) / (1.0 + o), g);
let slope = yb / xb;
if (!inv) {
if (v >= xb) {
return sign * powz((v + o) / (1.0 + o), g);
}
return sign * v * slope;
}
if (v >= yb) {
return sign * ((1.0 + o) * powz(v, 1.0 / g) - o);
}
return sign * v / slope;
}
// Run the colour program at data[0..] on a straight colour.
fn fxl_run(c0: vec3<f32>) -> vec3<f32> {
var c = c0;
@@ -485,6 +536,22 @@ fn fxl_run(c0: vec3<f32>) -> vec3<f32> {
}
pc += 16u;
}
case 12u: {
// ExponentWithLinearTransform: gamma (3), offset (3), inverse, mirror.
let g = fxl_d3(pc + 1u);
let o = fxl_d3(pc + 4u);
let inv = data[pc + 7u] != 0.0;
let mirror = data[pc + 8u] != 0.0;
for (var i = 0u; i < 3u; i++) {
c[i] = fxl_moncurve(c[i], g[i], o[i], inv, mirror);
}
pc += 9u;
}
case 13u: {
// Clamp (a clamping RangeTransform): low (3), high (3).
c = min(max(c, fxl_d3(pc + 1u)), fxl_d3(pc + 4u));
pc += 7u;
}
default: {
return c;
}
+102
View File
@@ -241,3 +241,105 @@ fn ocio_custom_config() {
}
effect_case("ec.color.ociocolorspace", &[("config", e(1)), ("configFile", s("/missing.ocio"))]);
}
/// A config with the OCIO v2 transforms of Blender's (#410): an E-white XYZ reference,
/// ColorSpaceTransform, ExponentWithLinearTransform, display-referred spaces through the
/// default view transform, AllocationTransform, BuiltinTransform (a matrix, the PQ and ACEScc
/// curves), a minimum-only RangeTransform and a look in a view.
const CONFIG_V2: &str = r#"ocio_profile_version: 2
roles:
reference: XYZ E
scene_linear: Linear Rec.709
default_view_transform: Standard
view_transforms:
- !<ViewTransform>
name: Standard
from_scene_reference: !<MatrixTransform> {matrix: [0.9531, -0.0266, 0.0239, 0, -0.0382, 1.0288, 0.0094, 0, 0.0026, -0.0030, 1.0893, 0, 0, 0, 0, 1]}
displays:
sRGB:
- !<View> {name: Standard, view_transform: Standard, display_colorspace: sRGB Display}
- !<View> {name: Log, colorspace: Log, looks: +Lift}
- !<View> {name: PQ, view_transform: Standard, display_colorspace: PQ Display}
display_colorspaces:
- !<ColorSpace>
name: XYZ D65
- !<ColorSpace>
name: sRGB Display
from_display_reference: !<GroupTransform>
children:
- !<ColorSpaceTransform> {src: XYZ D65, dst: Linear Rec.709}
- !<ExponentWithLinearTransform> {gamma: 2.4, offset: 0.055, direction: inverse}
- !<ColorSpace>
name: PQ Display
from_display_reference: !<BuiltinTransform> {style: DISPLAY - CIE-XYZ-D65_to_REC.2100-PQ}
colorspaces:
- !<ColorSpace>
name: XYZ E
- !<ColorSpace>
name: Linear Rec.709
from_scene_reference: !<GroupTransform>
children:
- !<MatrixTransform> {matrix: [0.9531, -0.0266, 0.0239, 0, -0.0382, 1.0288, 0.0094, 0, 0.0026, -0.0030, 1.0893, 0, 0, 0, 0, 1]}
- !<MatrixTransform> {matrix: [3.2410, -1.5374, -0.4986, 0, -0.9692, 1.8760, 0.0416, 0, 0.0556, -0.2040, 1.0570, 0, 0, 0, 0, 1]}
- !<ColorSpace>
name: sRGB
from_scene_reference: !<GroupTransform>
children:
- !<ColorSpaceTransform> {src: XYZ E, dst: Linear Rec.709}
- !<ExponentWithLinearTransform> {gamma: 2.4, offset: 0.055, direction: inverse}
- !<ColorSpace>
name: Log
from_scene_reference: !<GroupTransform>
children:
- !<ColorSpaceTransform> {src: XYZ E, dst: Linear Rec.709}
- !<AllocationTransform> {allocation: lg2, vars: [-10, 6.5]}
- !<ColorSpace>
name: ACEScg
from_scene_reference: !<GroupTransform>
children:
- !<ColorSpaceTransform> {src: XYZ E, dst: XYZ D65}
- !<BuiltinTransform> {style: "UTILITY - ACES-AP1_to_CIE-XYZ-D65_BFD", direction: inverse}
- !<ColorSpace>
name: ACEScc
to_scene_reference: !<GroupTransform>
children:
- !<BuiltinTransform> {style: ACEScc_to_ACES2065-1}
- !<MatrixTransform> {matrix: [0.9525, 0, 0.0001, 0, 0.3439, 0.7281, -0.0721, 0, 0, 0, 1.0088, 0, 0, 0, 0, 1]}
- !<ColorSpace>
name: Floor
from_scene_reference: !<GroupTransform>
children:
- !<ColorSpaceTransform> {src: XYZ E, dst: sRGB}
- !<RangeTransform> {min_in_value: 0.1, min_out_value: 0.1}
- !<ColorSpace>
name: Uniform
from_scene_reference: !<AllocationTransform> {allocation: uniform, vars: [-0.5, 2]}
looks:
- !<Look>
name: Lift
process_space: Log
transform: !<CDLTransform> {offset: [0.05, 0.02, 0], sat: 0.8}
"#;
#[test]
fn ocio_custom_config_v2() {
let cfg = [("config", e(1)), ("configFile", s(CONFIG_V2))];
for (a, z) in [
("Linear Rec.709", "sRGB"),
("Linear Rec.709", "sRGB Display"),
("sRGB", "Log"),
("ACEScg", "sRGB"),
("ACEScc", "Linear Rec.709"),
("sRGB", "Floor"),
("Linear Rec.709", "Uniform"),
] {
for dir in 0..2 {
let vals = [cfg.as_slice(), &[("sourceName", s(a)), ("destinationName", s(z)), ("direction", e(dir))]].concat();
effect_case("ec.color.ociocolorspace", &vals);
}
}
for (view, dir) in [("Standard", 0), ("Standard", 1), ("Log", 0), ("Log", 1), ("PQ", 0)] {
let vals = [cfg.as_slice(), &[("sourceName", s("Linear Rec.709")), ("displayName", s("sRGB")), ("viewName", s(view)), ("direction", e(dir))]].concat();
effect_case("ec.color.ociodisplay", &vals);
}
}