| 1 | //! `OCIOColorSpace` and `OCIODisplay`, built on the `ColorSpaceTransform` colorimetry and transfer curves. |
| 2 | |
| 3 | use super::*; |
| 4 | use crate::eval::color_lut::ColorLutOp; |
| 5 | use crate::eval::color_lut::colorimetry::{ |
| 6 | CatMethod, P_AP0, P_AP1, P_AWG3, P_AWG4, P_DWG, P_P3_D60, P_P3_D65, P_P3_DCI, P_REC709, P_REC2020, P_RWG, |
| 7 | P_SGAMUT3, P_SGAMUT3_CINE, Primaries, parse_color_space, parse_gamma_id, rgb_to_rgb_matrix, |
| 8 | }; |
| 9 | use std::sync::Arc; |
| 10 | |
| 11 | /// An OCIO color space as `(gamut, transfer id)`; `gamut == None` is CIE XYZ. |
| 12 | type SpaceDef = (Option<Primaries>, i32); |
| 13 | |
| 14 | /// Resolves an OCIO space name onto the `ColorSpaceTransform` vocabulary; `None` for `Raw`/data spaces. |
| 15 | fn ocio_space(name: &str) -> Option<SpaceDef> { |
| 16 | let s = squash(name); |
| 17 | if s == "raw" || s.contains("data") || s.contains("passthrough") { |
| 18 | return None; |
| 19 | } |
| 20 | let gamut = if s.contains("acescc") || s.contains("acescg") || s.contains("ap1") { |
| 21 | Some(P_AP1) |
| 22 | } else if s.contains("aces") || s.contains("ap0") { |
| 23 | Some(P_AP0) |
| 24 | } else if s.contains("logc4") || s.contains("awg4") { |
| 25 | Some(P_AWG4) |
| 26 | } else if s.contains("logc") || s.contains("alexa") || s.contains("awg") { |
| 27 | Some(P_AWG3) |
| 28 | } else if s.contains("slog") || s.contains("sgamut") { |
| 29 | Some(if s.contains("cine") { P_SGAMUT3_CINE } else { P_SGAMUT3 }) |
| 30 | } else if s.contains("log3g10") || s.contains("rwg") || s.contains("redwidegamut") { |
| 31 | Some(P_RWG) |
| 32 | } else if s.contains("davinci") || s.contains("dwg") { |
| 33 | Some(P_DWG) |
| 34 | } else { |
| 35 | parse_color_space(Some(name)) |
| 36 | }; |
| 37 | let xfer = if s.starts_with("linear") || s == "lin" { |
| 38 | 0 |
| 39 | } else if s.contains("intermediate") { |
| 40 | 15 |
| 41 | } else if s.contains("displayp3") || s.contains("srgb") { |
| 42 | 1 |
| 43 | } else { |
| 44 | parse_gamma_id(Some(&s)) |
| 45 | }; |
| 46 | Some((gamut, xfer)) |
| 47 | } |
| 48 | |
| 49 | /// `ColorSpaceTransform` shader parameters taking `from`-encoded RGB to `to`-encoded RGB. |
| 50 | struct Cst { |
| 51 | m: [[f32; 3]; 3], |
| 52 | in_xfer: i32, |
| 53 | out_xfer: i32, |
| 54 | } |
| 55 | |
| 56 | impl Cst { |
| 57 | /// `None` when the conversion is an identity. |
| 58 | fn between(from: SpaceDef, to: SpaceDef) -> Option<Self> { |
| 59 | let m = rgb_to_rgb_matrix(from.0, to.0, true, CatMethod::Bradford); |
| 60 | let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]; |
| 61 | (m != identity || from.1 != to.1).then_some(Self { m, in_xfer: from.1, out_xfer: to.1 }) |
| 62 | } |
| 63 | |
| 64 | fn uniforms(&self, fx: Option<&Effect>) -> UniformParams { |
| 65 | let mut u = UniformParams::default(); |
| 66 | for (row, m) in u.p.iter_mut().zip(&self.m) { |
| 67 | *row = [m[0], m[1], m[2], 0.0]; |
| 68 | } |
| 69 | u.p[3] = [self.in_xfer as f32, self.out_xfer as f32, 0.0, 0.0]; |
| 70 | u.p[4] = [100.0, 100.0, 0.9, 1.0]; |
| 71 | u.p[5] = [1.0, 1.0, 0.0, 0.0]; |
| 72 | if let Some(fx) = fx { |
| 73 | u.p[5] = [1.0, fx.blend, b2f(fx.mask.is_some()), b2f(fx.mask_inv)]; |
| 74 | u.p[6] = [b2f(fx.mbm), 0.0, 0.0, 0.0]; |
| 75 | u.p[7] = fx.skip; |
| 76 | } |
| 77 | u |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Display gamut and curve id (see `ocio_display_op`) plus whether the device implies an ACES view. |
| 82 | fn parse_display_device(device: &str) -> (Primaries, i32, bool) { |
| 83 | let s = squash(device); |
| 84 | let aces = s.contains("aces"); |
| 85 | let (gamut, curve) = if s.contains("2100") && s.contains("hlg") { |
| 86 | (P_REC2020, 6) |
| 87 | } else if s.contains("2100") || s.contains("pq") { |
| 88 | (P_REC2020, 5) |
| 89 | } else if s.contains("displayp3") { |
| 90 | (P_P3_D65, 1) |
| 91 | } else if s.contains("p3") { |
| 92 | let gamut = if s.contains("dci") { |
| 93 | P_P3_DCI |
| 94 | } else if s.contains("d60") { |
| 95 | P_P3_D60 |
| 96 | } else { |
| 97 | P_P3_D65 |
| 98 | }; |
| 99 | (gamut, 4) |
| 100 | } else if s.contains("2020") { |
| 101 | (P_REC2020, 3) |
| 102 | } else if s.contains("709") || s.contains("1886") { |
| 103 | (P_REC709, 3) |
| 104 | } else { |
| 105 | (P_REC709, 1) |
| 106 | }; |
| 107 | (gamut, curve, aces) |
| 108 | } |
| 109 | |
| 110 | fn parse_view_transform(view: Option<&str>, aces_device: bool) -> i32 { |
| 111 | match view.map(squash).as_deref() { |
| 112 | Some(v) if v.contains("aces") || v.contains("filmic") => 1, |
| 113 | Some(v) if v.contains("reinhard") => 2, |
| 114 | Some(v) if !v.is_empty() => 0, |
| 115 | _ => i32::from(aces_device), |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl Engine { |
| 120 | /// Dispatches one `color_lut` op over `src` with an optional packed LUT texture (binding 1), |
| 121 | /// caching the result under a key of its inputs; the result keeps `src`'s DoD and aux channels. |
| 122 | #[allow(clippy::too_many_arguments)] |
| 123 | pub(super) fn run_cl_filter( |
| 124 | &mut self, |
| 125 | node: &Node, |
| 126 | tag: &'static str, |
| 127 | op: ColorLutOp, |
| 128 | src: &CachedTexture, |
| 129 | lut: Option<(&Arc<wgpu::TextureView>, u64)>, |
| 130 | mask: Option<&CachedTexture>, |
| 131 | u: &UniformParams, |
| 132 | depth: BitDepth, |
| 133 | frame_i: i64, |
| 134 | enc: &mut wgpu::CommandEncoder, |
| 135 | stats: &mut EvalStats, |
| 136 | ) -> CachedTexture { |
| 137 | let lut_hash = lut.map_or(0, |l| l.1); |
| 138 | let mask_hash = mask.map_or(0, |m| m.content_hash); |
| 139 | let key = self.make_key(node, tag, src.size, &[src.content_hash, mask_hash, lut_hash, depth as u64], bytemuck::cast_slice(&u.p)); |
| 140 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 141 | stats.cache_hits += 1; |
| 142 | return hit; |
| 143 | } |
| 144 | stats.cache_misses += 1; |
| 145 | stats.nodes_evaluated += 1; |
| 146 | let mut dst = self.alloc_tex_like(src, depth, key); |
| 147 | let views = [ |
| 148 | &*src.view, |
| 149 | lut.map_or(&*self.dummy.view, |l| &**l.0), |
| 150 | &*self.dummy.view, |
| 151 | mask.map_or(&*self.dummy.view, |m| &*m.view), |
| 152 | ]; |
| 153 | self.dispatch_cl(enc, op, views, &dst, u); |
| 154 | dst.set_aux(src.aux.clone()); |
| 155 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 156 | dst |
| 157 | } |
| 158 | |
| 159 | /// `OCIOColorSpace` — converts between named OCIO color spaces (`SourceSpace` -> `OutputSpace`). |
| 160 | #[allow(clippy::too_many_arguments)] |
| 161 | pub(in crate::eval) fn eval_ocio_color_space( |
| 162 | &mut self, |
| 163 | comp: &Comp, |
| 164 | node: &Node, |
| 165 | time: f64, |
| 166 | frame_i: i64, |
| 167 | enc: &mut wgpu::CommandEncoder, |
| 168 | visiting: &mut HashSet<(NodeId, i64)>, |
| 169 | stats: &mut EvalStats, |
| 170 | ) -> CachedTexture { |
| 171 | let Some(src_id) = conn_any(comp, node, &["Input", "Image", "Background"]) else { |
| 172 | return self.dummy.clone(); |
| 173 | }; |
| 174 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 175 | let (src, fx) = match self.effect_prologue(comp, node, time, src, enc, visiting, stats) { |
| 176 | Ok(v) => v, |
| 177 | Err(src) => return src, |
| 178 | }; |
| 179 | let from = ocio_space(str_any(node, &["SourceSpace", "InputSpace"]).unwrap_or("scene_linear")); |
| 180 | let to = ocio_space(str_any(node, &["OutputSpace", "DestSpace"]).unwrap_or("scene_linear")); |
| 181 | let cst = from.zip(to).and_then(|(from, to)| Cst::between(from, to)); |
| 182 | let Some(cst) = cst else { |
| 183 | return if fx.mbm && fx.mask.is_some() { self.mask_only(node, &src, &fx, frame_i, enc, stats) } else { src }; |
| 184 | }; |
| 185 | let depth = self.node_depth(comp, node, time, Some(src.depth)); |
| 186 | let u = cst.uniforms(Some(&fx)); |
| 187 | self.run_cl_filter(node, "OCIOColorSpace", ColorLutOp::ColorSpaceTransform, &src, None, fx.mask.as_ref(), &u, depth, frame_i, enc, stats) |
| 188 | } |
| 189 | |
| 190 | /// `MultiplyByMask` on an identity transform: zeroes outside the EffectMask via the `FileLUT` identity path. |
| 191 | pub(super) fn mask_only( |
| 192 | &mut self, |
| 193 | node: &Node, |
| 194 | src: &CachedTexture, |
| 195 | fx: &Effect, |
| 196 | frame_i: i64, |
| 197 | enc: &mut wgpu::CommandEncoder, |
| 198 | stats: &mut EvalStats, |
| 199 | ) -> CachedTexture { |
| 200 | let mut u = UniformParams::default(); |
| 201 | u.p[2] = [1.0, 1.0, 1.0, 1.0]; |
| 202 | u.p[4] = [fx.blend, b2f(fx.mask.is_some()), b2f(fx.mask_inv), b2f(fx.mbm)]; |
| 203 | u.p[5] = fx.skip; |
| 204 | self.run_cl_filter(node, "OCIOIdentityMask", ColorLutOp::FileLut, src, None, fx.mask.as_ref(), &u, src.depth, frame_i, enc, stats) |
| 205 | } |
| 206 | |
| 207 | /// `OCIODisplay` — `InputSpace` -> display gamut, then exposure, view transform, display curve and post gamma. |
| 208 | #[allow(clippy::too_many_arguments)] |
| 209 | pub(in crate::eval) fn eval_ocio_display( |
| 210 | &mut self, |
| 211 | comp: &Comp, |
| 212 | node: &Node, |
| 213 | time: f64, |
| 214 | frame_i: i64, |
| 215 | enc: &mut wgpu::CommandEncoder, |
| 216 | visiting: &mut HashSet<(NodeId, i64)>, |
| 217 | stats: &mut EvalStats, |
| 218 | ) -> CachedTexture { |
| 219 | let Some(src_id) = conn_any(comp, node, &["Input", "Image", "Background"]) else { |
| 220 | return self.dummy.clone(); |
| 221 | }; |
| 222 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 223 | let (src, fx) = match self.effect_prologue(comp, node, time, src, enc, visiting, stats) { |
| 224 | Ok(v) => v, |
| 225 | Err(src) => return src, |
| 226 | }; |
| 227 | let (gamut, curve, aces_device) = parse_display_device(str_any(node, &["DisplayDevice", "Display"]).unwrap_or("sRGB")); |
| 228 | let view = parse_view_transform(str_any(node, &["ViewTransform", "View"]), aces_device); |
| 229 | let exposure = comp.eval_number(node, "Exposure", time, 0.0, &self.expr); |
| 230 | let gamma = comp.eval_number(node, "Gamma", time, 1.0, &self.expr).max(1e-3); |
| 231 | let depth = self.node_depth(comp, node, time, Some(src.depth)); |
| 232 | |
| 233 | let from = ocio_space(str_any(node, &["InputSpace", "SourceSpace"]).unwrap_or("scene_linear")); |
| 234 | let linear_depth = if depth.is_float() { depth } else { BitDepth::Float16 }; |
| 235 | let lin = match from.and_then(|from| Cst::between(from, (Some(gamut), 0))) { |
| 236 | Some(cst) => { |
| 237 | self.run_cl_filter(node, "OCIODisplayInput", ColorLutOp::ColorSpaceTransform, &src, None, None, &cst.uniforms(None), linear_depth, frame_i, enc, stats) |
| 238 | } |
| 239 | None => src.clone(), |
| 240 | }; |
| 241 | |
| 242 | let mut u = UniformParams::default(); |
| 243 | u.p[0] = [2f64.powf(exposure) as f32, (1.0 / gamma) as f32, curve as f32, view as f32]; |
| 244 | let tex_b = &src; |
| 245 | self.run_lo_filter(node, "OCIODisplay", LayerOcioOp::OcioDisplay, &lin, Some(tex_b), None, &fx, depth, u, frame_i, enc, stats) |
| 246 | } |
| 247 | } |