| 1 | //! 2D Warp & Sky Generators (`Drip`, `Dent`, `PerspectivePositioner`, `DaySky`, `GridWarp`). |
| 2 | |
| 3 | use super::transform::{dod_from_px_bounds, transform_should_expand_dod, unwrap_passthrough}; |
| 4 | use super::*; |
| 5 | |
| 6 | impl Engine { |
| 7 | #[allow(clippy::too_many_arguments)] |
| 8 | pub(super) fn eval_drip( |
| 9 | &mut self, |
| 10 | comp: &Comp, |
| 11 | node: &Node, |
| 12 | time: f64, |
| 13 | frame_i: i64, |
| 14 | enc: &mut wgpu::CommandEncoder, |
| 15 | visiting: &mut HashSet<(NodeId, i64)>, |
| 16 | stats: &mut EvalStats, |
| 17 | ) -> CachedTexture { |
| 18 | let Some(src_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) else { |
| 19 | return self.dummy.clone(); |
| 20 | }; |
| 21 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 22 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 23 | let amp = comp.eval_number(node, "Amplitude", time, 0.1, &self.expr) as f32; |
| 24 | if blend <= 1e-6 || (amp.abs() <= 1e-7 && !self.multiplies_by_mask(comp, node, time)) { |
| 25 | return src; |
| 26 | } |
| 27 | let src = self.crop_to_canvas(enc, src); |
| 28 | let center = comp.eval_point(node, "Center", time, [0.5, 0.5], &self.expr); |
| 29 | let freq = comp.eval_number(node, "Frequency", time, 10.0, &self.expr) as f32; |
| 30 | let aspect = comp.eval_number(node, "Aspect", time, 1.0, &self.expr).max(1e-4) as f32; |
| 31 | let phase = comp.eval_number(node, "Phase", time, 0.0, &self.expr) as f32; |
| 32 | let dampening = comp.eval_number(node, "Dampening", time, 0.0, &self.expr).max(0.0) as f32; |
| 33 | let shape = comp.eval_number(node, "Shape", time, 0.0, &self.expr).round() as f32; |
| 34 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 35 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 36 | let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5; |
| 37 | |
| 38 | let mut u = UniformParams::default(); |
| 39 | u.p[0] = [center[0] as f32, center[1] as f32, 0.0, amp]; |
| 40 | u.p[1] = [freq, aspect, phase, dampening]; |
| 41 | u.p[2] = [shape, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }]; |
| 42 | u.p[3][0] = if mbm { 1.0 } else { 0.0 }; |
| 43 | let skip = self.process_skip(comp, node, time); |
| 44 | u.p[4] = skip; |
| 45 | |
| 46 | let key = self.make_key( |
| 47 | node, |
| 48 | "Drip", |
| 49 | src.size, |
| 50 | &[src.content_hash, mask_hash], |
| 51 | bytemuck::cast_slice(&u.p[..5]), |
| 52 | ); |
| 53 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 54 | stats.cache_hits += 1; |
| 55 | return hit; |
| 56 | } |
| 57 | stats.cache_misses += 1; |
| 58 | stats.nodes_evaluated += 1; |
| 59 | let clear_skip = mbm && skip.iter().any(|&s| s > 0.5); |
| 60 | if clear_skip { |
| 61 | u.p[4] = [0.0; 4]; |
| 62 | } |
| 63 | let mask_dod = if mbm { Self::polyline_mask_dod(comp, node, time, src.canvas_size, &self.expr) } else { None }; |
| 64 | let warped_dst = self.alloc_tex_like(&src, src.depth, if mask_dod.is_some() || clear_skip { 0 } else { key }); |
| 65 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 66 | self.dispatch_op(enc, ShaderOp::Drip, &src.view, &self.dummy.view, mv, &warped_dst, &u); |
| 67 | let dst = if clear_skip { |
| 68 | self.clear_skipped_channels(enc, &warped_dst, skip, if mask_dod.is_some() { 0 } else { key }) |
| 69 | } else { |
| 70 | warped_dst |
| 71 | }; |
| 72 | let dst = match mask_dod { |
| 73 | Some((origin, size)) => { |
| 74 | let cropped = self.alloc_tex_dod(size, origin, src.canvas_size, src.depth, key); |
| 75 | let mut crop = UniformParams::default(); |
| 76 | crop.p[0] = [origin[0] as f32, (src.canvas_size[1] as i32 - size[1] as i32 - origin[1]) as f32, 1.0, 1.0]; |
| 77 | self.dispatch_op(enc, ShaderOp::Crop, &dst.view, &self.dummy.view, &self.dummy.view, &cropped, &crop); |
| 78 | cropped |
| 79 | } |
| 80 | None => dst, |
| 81 | }; |
| 82 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 83 | dst |
| 84 | } |
| 85 | |
| 86 | /// Under `MultiplyByMask = 1`, Fusion clears any channel whose `Process*` toggle is off to `0.0`. |
| 87 | pub(super) fn clear_skipped_channels( |
| 88 | &self, |
| 89 | enc: &mut wgpu::CommandEncoder, |
| 90 | src: &CachedTexture, |
| 91 | skip: [f32; 4], |
| 92 | out_key: u64, |
| 93 | ) -> CachedTexture { |
| 94 | let mut dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, out_key); |
| 95 | dst.tex_scale = src.tex_scale; |
| 96 | let mut uc = UniformParams::default(); |
| 97 | uc.p[0] = [ |
| 98 | 12.0, // Clear -> 0.0 |
| 99 | if skip[0] > 0.5 { 0.0 } else { 4.0 }, |
| 100 | if skip[1] > 0.5 { 0.0 } else { 4.0 }, |
| 101 | if skip[2] > 0.5 { 0.0 } else { 4.0 }, |
| 102 | ]; |
| 103 | uc.p[1] = [if skip[3] > 0.5 { 0.0 } else { 4.0 }, 1.0, 0.0, 0.0]; |
| 104 | self.dispatch_op(enc, ShaderOp::ChannelBoolean, &src.view, &self.dummy.view, &self.dummy.view, &dst, &uc); |
| 105 | dst |
| 106 | } |
| 107 | |
| 108 | /// Fusion crops a `MultiplyByMask` result to the mask's own domain: its covered bounds grown by three `SoftEdge` sigmas. |
| 109 | fn polyline_mask_dod(comp: &Comp, node: &Node, time: f64, canvas: [u32; 2], expr: &ExprEngine) -> Option<([i32; 2], [u32; 2])> { |
| 110 | let mask = comp.node(comp.find_node(node.connected_node("EffectMask")?)?); |
| 111 | if mask.kind != "PolylineMask" || comp.eval_number(mask, "Invert", time, 0.0, expr) > 0.5 { |
| 112 | return None; |
| 113 | } |
| 114 | let center = comp.eval_point(mask, "Center", time, [0.5, 0.5], expr); |
| 115 | let angle = comp.eval_number(mask, "Angle", time, 0.0, expr) as f32; |
| 116 | let points = Self::polyline_points(comp, mask, time); |
| 117 | let px = rasterize_polyline_mask(canvas, &points, [center[0] as f32, center[1] as f32], angle, false, 1.0); |
| 118 | let [w, h] = canvas.map(|v| v as usize); |
| 119 | let covered = |i: usize| px[i * 4 + 3] > 0; |
| 120 | let xs = (0..w).filter(|&x| (0..h).any(|y| covered(y * w + x))); |
| 121 | let ys = (0..h).filter(|&y| (0..w).any(|x| covered(y * w + x))); |
| 122 | let (x0, x1) = (xs.clone().next()?, xs.last()? + 1); |
| 123 | let (y0, y1) = (ys.clone().next()?, ys.last()? + 1); |
| 124 | let margin = 3.0 * comp.eval_number(mask, "SoftEdge", time, 0.0, expr).max(0.0) * canvas[0] as f64; |
| 125 | let lo = |v: usize, max: usize| ((v as f64 - margin).floor().max(0.0) as usize).min(max); |
| 126 | let hi = |v: usize, max: usize| (((v as f64 + margin).floor() + 1.0).min(max as f64)) as usize; |
| 127 | let (lx, ly, hx, hy) = (lo(x0, w), lo(y0, h), hi(x1, w), hi(y1, h)); |
| 128 | Some(([lx as i32, ly as i32], [(hx - lx).max(1) as u32, (hy - ly).max(1) as u32])) |
| 129 | } |
| 130 | |
| 131 | #[allow(clippy::too_many_arguments)] |
| 132 | pub(super) fn eval_dent( |
| 133 | &mut self, |
| 134 | comp: &Comp, |
| 135 | node: &Node, |
| 136 | time: f64, |
| 137 | frame_i: i64, |
| 138 | enc: &mut wgpu::CommandEncoder, |
| 139 | visiting: &mut HashSet<(NodeId, i64)>, |
| 140 | stats: &mut EvalStats, |
| 141 | ) -> CachedTexture { |
| 142 | let Some(src_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) else { |
| 143 | return self.dummy.clone(); |
| 144 | }; |
| 145 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 146 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 147 | let strength = comp.eval_number(node, "Strength", time, 1.0, &self.expr) as f32; |
| 148 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 149 | if (blend <= 1e-6 && !mbm_flag) || (strength.abs() <= 1e-6 && !mbm_flag) { |
| 150 | return src; |
| 151 | } |
| 152 | let skip = self.process_skip(comp, node, time); |
| 153 | if skip.iter().all(|&s| s > 0.5) && !mbm_flag { |
| 154 | return src; |
| 155 | } |
| 156 | let src = self.crop_to_canvas(enc, src); |
| 157 | let center = comp.eval_point(node, "Center", time, [0.5, 0.5], &self.expr); |
| 158 | let size = comp.eval_number(node, "Size", time, 0.5, &self.expr).max(1e-4) as f32; |
| 159 | let dent_type = comp.eval_number(node, "Type", time, 0.0, &self.expr).round() as f32; |
| 160 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 161 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 162 | let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5; |
| 163 | |
| 164 | let mut u = UniformParams::default(); |
| 165 | u.p[0] = [center[0] as f32, center[1] as f32, size * 0.5, strength]; |
| 166 | u.p[1] = [dent_type, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }]; |
| 167 | u.p[2][0] = if mbm { 1.0 } else { 0.0 }; |
| 168 | u.p[3] = skip; |
| 169 | |
| 170 | let key = self.make_key( |
| 171 | node, |
| 172 | "Dent", |
| 173 | src.size, |
| 174 | &[src.content_hash, mask_hash], |
| 175 | bytemuck::cast_slice(&u.p[..4]), |
| 176 | ); |
| 177 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 178 | stats.cache_hits += 1; |
| 179 | return hit; |
| 180 | } |
| 181 | stats.cache_misses += 1; |
| 182 | stats.nodes_evaluated += 1; |
| 183 | let any_skip = skip.iter().any(|&s| s > 0.5); |
| 184 | // When both MultiplyByMask and Process{R,G,B,A}=0 are active, Fusion still multiplies the |
| 185 | // untouched source channels by the EffectMask; apply MaskApply after ChannelBoolean. |
| 186 | if mbm && any_skip { |
| 187 | u.p[2][0] = 0.0; |
| 188 | } |
| 189 | let dent_dst = self.alloc_tex_like(&src, src.depth, if any_skip { 0 } else { key }); |
| 190 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 191 | self.dispatch_op(enc, ShaderOp::Dent, &src.view, &self.dummy.view, mv, &dent_dst, &u); |
| 192 | let dst = if any_skip { |
| 193 | let merged = self.alloc_tex_like(&src, src.depth, if mbm { 0 } else { key }); |
| 194 | let mut uc = UniformParams::default(); |
| 195 | uc.p[0] = [ |
| 196 | 0.0, |
| 197 | if skip[0] > 0.5 { 4.0 } else { 0.0 }, |
| 198 | if skip[1] > 0.5 { 4.0 } else { 1.0 }, |
| 199 | if skip[2] > 0.5 { 4.0 } else { 2.0 }, |
| 200 | ]; |
| 201 | uc.p[1] = [if skip[3] > 0.5 { 4.0 } else { 3.0 }, 1.0, 0.0, 0.0]; |
| 202 | self.dispatch_op(enc, ShaderOp::ChannelBoolean, &src.view, &dent_dst.view, &self.dummy.view, &merged, &uc); |
| 203 | if mbm { |
| 204 | let masked = self.alloc_tex_like(&src, src.depth, key); |
| 205 | let mut um = UniformParams::default(); |
| 206 | um.p[0][0] = if mask_inv { 1.0 } else { 0.0 }; |
| 207 | self.dispatch_op(enc, ShaderOp::MaskApply, &merged.view, &self.dummy.view, mv, &masked, &um); |
| 208 | masked |
| 209 | } else { |
| 210 | merged |
| 211 | } |
| 212 | } else { |
| 213 | dent_dst |
| 214 | }; |
| 215 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 216 | dst |
| 217 | } |
| 218 | |
| 219 | #[allow(clippy::too_many_arguments)] |
| 220 | pub(super) fn eval_perspective_positioner( |
| 221 | &mut self, |
| 222 | comp: &Comp, |
| 223 | node: &Node, |
| 224 | time: f64, |
| 225 | frame_i: i64, |
| 226 | enc: &mut wgpu::CommandEncoder, |
| 227 | visiting: &mut HashSet<(NodeId, i64)>, |
| 228 | stats: &mut EvalStats, |
| 229 | ) -> CachedTexture { |
| 230 | let Some(src_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) else { |
| 231 | return self.dummy.clone(); |
| 232 | }; |
| 233 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 234 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 235 | if blend <= 1e-6 && !self.multiplies_by_mask(comp, node, time) { |
| 236 | return src; |
| 237 | } |
| 238 | let bl = comp.eval_point(node, "BottomLeft", time, [0.0, 0.3], &self.expr); |
| 239 | let br = comp.eval_point(node, "BottomRight", time, [1.0, 0.1], &self.expr); |
| 240 | let tr = comp.eval_point(node, "TopRight", time, [0.9, 1.0], &self.expr); |
| 241 | let tl = comp.eval_point(node, "TopLeft", time, [0.1, 0.9], &self.expr); |
| 242 | let mapping_type = comp.eval_number(node, "MappingType", time, 0.0, &self.expr).round() as i32; |
| 243 | let h_fwd = quad_forward_homography(bl, br, tr, tl); |
| 244 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.canvas_size), enc, visiting, stats); |
| 245 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 246 | let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5; |
| 247 | |
| 248 | let mut u = UniformParams::default(); |
| 249 | u.p[0] = [bl[0] as f32, bl[1] as f32, br[0] as f32, br[1] as f32]; |
| 250 | u.p[1] = [tr[0] as f32, tr[1] as f32, tl[0] as f32, tl[1] as f32]; |
| 251 | u.p[2] = [h_fwd[0], h_fwd[1], h_fwd[2], h_fwd[3]]; |
| 252 | u.p[3] = [h_fwd[4], h_fwd[5], h_fwd[6], h_fwd[7]]; |
| 253 | u.p[4] = [ |
| 254 | h_fwd[8], |
| 255 | if mapping_type == 1 { 1.0 } else { 0.0 }, |
| 256 | blend, |
| 257 | if mask.is_some() { 1.0 } else { 0.0 }, |
| 258 | ]; |
| 259 | u.p[5] = [if mask_inv { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }, 0.0, 0.0]; |
| 260 | // Fusion renders computed sources only inside the corner bounding box (unless an EffectMask |
| 261 | // pulls the whole input), so Blend < 1 fades the rest toward transparent; Loader and sRender |
| 262 | // sources are always complete. |
| 263 | let src_kind = comp.node(unwrap_passthrough(comp, src_id)).kind.as_str(); |
| 264 | if mask.is_none() && !matches!(src_kind, "Loader" | "MediaIn" | "sRender") { |
| 265 | let xs = [bl[0], br[0], tr[0], tl[0]]; |
| 266 | let ys = [bl[1], br[1], tr[1], tl[1]]; |
| 267 | let (x0, x1) = xs.iter().fold((f64::MAX, f64::MIN), |(lo, hi), &v| (lo.min(v), hi.max(v))); |
| 268 | let (y0, y1) = ys.iter().fold((f64::MAX, f64::MIN), |(lo, hi), &v| (lo.min(v), hi.max(v))); |
| 269 | u.p[5][2] = 1.0; |
| 270 | u.p[6] = [x0 as f32, y0 as f32, x1 as f32, y1 as f32]; |
| 271 | } |
| 272 | |
| 273 | // The output extends past the canvas wherever the corner mapping still lands inside the source domain. |
| 274 | let canvas = src.canvas_size.map(|v| v.max(1) as f64); |
| 275 | let src_rect = [ |
| 276 | src.origin[0] as f64 / canvas[0], |
| 277 | 1.0 - (src.origin[1] + src.size[1] as i32) as f64 / canvas[1], |
| 278 | (src.origin[0] + src.size[0] as i32) as f64 / canvas[0], |
| 279 | 1.0 - src.origin[1] as f64 / canvas[1], |
| 280 | ]; |
| 281 | let preimage = positioner_preimage(mapping_type, [bl, br, tr, tl], h_fwd, src_rect) |
| 282 | .or_else(|| positioner_preimage(mapping_type, [bl, br, tr, tl], h_fwd, [0.0, 0.0, 1.0, 1.0])); |
| 283 | let (dst_origin, dst_size) = match preimage { |
| 284 | Some(fu) if !fu.is_empty() && mask.is_none() && transform_should_expand_dod(comp, &node.name) => { |
| 285 | let (min_x, max_x, min_y, max_y) = fu.iter().fold( |
| 286 | (0.0_f32, canvas[0] as f32, 0.0_f32, canvas[1] as f32), |
| 287 | |(lx, hx, ly, hy), &[x, y]| { |
| 288 | let px = (x * canvas[0]) as f32; |
| 289 | let py = ((1.0 - y) * canvas[1]) as f32; |
| 290 | (lx.min(px), hx.max(px), ly.min(py), hy.max(py)) |
| 291 | }, |
| 292 | ); |
| 293 | let max_dim = self.device.limits().max_texture_dimension_2d.min(4096); |
| 294 | dod_from_px_bounds(min_x, max_x, min_y, max_y, src.canvas_size, max_dim) |
| 295 | } |
| 296 | _ => ([0, 0], src.canvas_size), |
| 297 | }; |
| 298 | if dst_origin != [0, 0] || dst_size != src.canvas_size { |
| 299 | u.p[7] = [dst_origin[0] as f32, dst_origin[1] as f32, src.canvas_size[0] as f32, src.canvas_size[1] as f32]; |
| 300 | } |
| 301 | if src.has_dod() { |
| 302 | u.p[8] = [src.origin[0] as f32, src.origin[1] as f32, src.canvas_size[0] as f32, src.canvas_size[1] as f32]; |
| 303 | } |
| 304 | let skip = self.process_skip(comp, node, time); |
| 305 | u.p[9] = skip; |
| 306 | |
| 307 | let key = self.make_key( |
| 308 | node, |
| 309 | "PerspectivePositioner", |
| 310 | dst_size, |
| 311 | &[src.content_hash, mask_hash], |
| 312 | bytemuck::cast_slice(&u.p[..10]), |
| 313 | ); |
| 314 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 315 | stats.cache_hits += 1; |
| 316 | return hit; |
| 317 | } |
| 318 | stats.cache_misses += 1; |
| 319 | stats.nodes_evaluated += 1; |
| 320 | let clear_skip = mbm && skip.iter().any(|&s| s > 0.5); |
| 321 | if clear_skip { |
| 322 | u.p[9] = [0.0; 4]; |
| 323 | } |
| 324 | let mut warped_dst = self.alloc_tex_dod(dst_size, dst_origin, src.canvas_size, src.depth, if clear_skip { 0 } else { key }); |
| 325 | warped_dst.tex_scale = src.tex_scale; |
| 326 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 327 | self.dispatch_op(enc, ShaderOp::PerspectivePos, &src.view, &self.dummy.view, mv, &warped_dst, &u); |
| 328 | let dst = if clear_skip { |
| 329 | self.clear_skipped_channels(enc, &warped_dst, skip, key) |
| 330 | } else { |
| 331 | warped_dst |
| 332 | }; |
| 333 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 334 | dst |
| 335 | } |
| 336 | |
| 337 | #[allow(clippy::too_many_arguments)] |
| 338 | pub(super) fn eval_day_sky( |
| 339 | &mut self, |
| 340 | comp: &Comp, |
| 341 | node: &Node, |
| 342 | time: f64, |
| 343 | frame_i: i64, |
| 344 | comp_dims: [u32; 2], |
| 345 | enc: &mut wgpu::CommandEncoder, |
| 346 | visiting: &mut HashSet<(NodeId, i64)>, |
| 347 | stats: &mut EvalStats, |
| 348 | ) -> CachedTexture { |
| 349 | let use_ff = comp.eval_number(node, "UseFrameFormatSettings", time, 0.0, &self.expr) > 0.5; |
| 350 | let w = if use_ff { |
| 351 | comp.width |
| 352 | } else { |
| 353 | comp.eval_number(node, "Width", time, comp.width as f64, &self.expr).round().max(1.0) as u32 |
| 354 | }; |
| 355 | let h = if use_ff { |
| 356 | comp.height |
| 357 | } else { |
| 358 | comp.eval_number(node, "Height", time, comp.height as f64, &self.expr).round().max(1.0) as u32 |
| 359 | }; |
| 360 | let size = if w == comp.width && h == comp.height { comp_dims } else { self.scaled_dims(w, h) }; |
| 361 | let dst_depth = self.node_depth(comp, node, time, None); |
| 362 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 363 | |
| 364 | let lat_deg = comp.eval_number(node, "Latitude", time, 41.38, &self.expr); |
| 365 | let lon_deg = comp.eval_number(node, "Longitude", time, 2.15, &self.expr); |
| 366 | let month = parse_month_index(node, comp, time, &self.expr); |
| 367 | let day = node |
| 368 | .static_str("Day") |
| 369 | .and_then(|s| s.parse::<f64>().ok()) |
| 370 | .unwrap_or_else(|| comp.eval_number(node, "Day", time, 1.0, &self.expr)); |
| 371 | let hour = comp.eval_number(node, "Time", time, 12.0, &self.expr); |
| 372 | let turbidity_in = comp.eval_number(node, "Turbidity", time, 0.0, &self.expr).max(0.0); |
| 373 | let do_tm = comp.eval_number(node, "DoToneMapping", time, 1.0, &self.expr) > 0.5; |
| 374 | let exposure = comp.eval_number(node, "Exposure", time, 0.72, &self.expr).max(0.0); |
| 375 | let hb = comp.eval_number(node, "Horizon.Brightness", time, 1.0, &self.expr).max(0.0); |
| 376 | let hl = comp.eval_number(node, "Horizon.Luminance", time, 1.0, &self.expr).max(0.0); |
| 377 | let ci = comp.eval_number(node, "Circumsolar.Intensity", time, 1.0, &self.expr).max(0.0); |
| 378 | let cw = comp.eval_number(node, "Circumsolar.Width", time, 1.0, &self.expr).max(0.0); |
| 379 | let bs = comp.eval_number(node, "BackscatteredLight", time, 1.0, &self.expr).max(0.0); |
| 380 | |
| 381 | let (sun_dir, ts) = solar_direction(lat_deg, lon_deg, month, day, hour); |
| 382 | // Calibrated to Fusion 21.1 DaySky: Turbidity in [0,1] maps to Preetham T = 2.0 + 4.0 * turb. |
| 383 | let t_preetham = 2.0 + turbidity_in * 4.0; |
| 384 | let (mut xz, mut yz, _yz_lum) = preetham_zenith_xyy(t_preetham, ts); |
| 385 | let mut night_lum_scale = 1.0_f64; |
| 386 | // When the sun is well below the horizon, Preetham's zenith cubic pushes yz > 0.65, which |
| 387 | // archive_fx.wgsl's [0.05, 0.65] chromaticity clamp would clip into an overly red hue. |
| 388 | // For out-of-gamut (B <= 0) night/twilight skies, project (xz, yz) into [0.05, 0.55] while |
| 389 | // preserving zenith R/G ratio and G luminance so the shader's clamp never clips. |
| 390 | if yz > 0.60 && do_tm { |
| 391 | let xt = xz / yz; |
| 392 | let zt = (1.0 - xz - yz) / yz; |
| 393 | let rt = 3.2404542 * xt - 1.5371385 - 0.4985314 * zt; |
| 394 | let gt = -0.9692660 * xt + 1.8760108 + 0.0415560 * zt; |
| 395 | let bt = 0.0556434 * xt - 0.2040259 + 1.0572252 * zt; |
| 396 | if bt <= 0.0 && gt > 1e-4 && rt > 0.0 { |
| 397 | let ratio = rt / gt; |
| 398 | let yz_cap = 0.55_f64; |
| 399 | let xc = (ratio * (1.8344548 * yz_cap + 0.0415560) + (1.0386071 * yz_cap + 0.4985314)) |
| 400 | / (3.7389856 + ratio * 1.0108220); |
| 401 | let g_proj = (-1.0108220 * xc + 1.8344548 * yz_cap + 0.0415560) / yz_cap; |
| 402 | if (0.05..=0.60).contains(&xc) && g_proj > 1e-4 { |
| 403 | xz = xc; |
| 404 | yz = yz_cap; |
| 405 | night_lum_scale = gt / g_proj; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | let c_y0 = [ |
| 410 | 0.1787 * t_preetham - 1.4630, |
| 411 | -0.3554 * t_preetham + 0.4275, |
| 412 | -0.0227 * t_preetham + 5.3251, |
| 413 | 0.1206 * t_preetham - 2.5771, |
| 414 | -0.0670 * t_preetham + 0.3703, |
| 415 | ]; |
| 416 | let c_x0 = [ |
| 417 | -0.0193 * t_preetham - 0.2592, |
| 418 | -0.0665 * t_preetham + 0.0008, |
| 419 | -0.0004 * t_preetham + 0.2125, |
| 420 | -0.0641 * t_preetham - 0.8989, |
| 421 | -0.0033 * t_preetham + 0.0452, |
| 422 | ]; |
| 423 | let c_y_c0 = [ |
| 424 | -0.0167 * t_preetham - 0.2608, |
| 425 | -0.0950 * t_preetham + 0.0092, |
| 426 | -0.0079 * t_preetham + 0.2102, |
| 427 | -0.0441 * t_preetham - 1.6537, |
| 428 | -0.0109 * t_preetham + 0.0529, |
| 429 | ]; |
| 430 | |
| 431 | let scale_perez = |base: [f64; 5]| -> [f64; 5] { |
| 432 | let a = if hb <= 1e-4 || hl <= 1e-4 { 0.0 } else { base[0] * hb }; |
| 433 | let b = base[1] * hl; |
| 434 | let c = if cw <= 1e-4 { 0.0 } else { base[2] * ci }; |
| 435 | let d = base[3] * cw; |
| 436 | let e = base[4] * bs; |
| 437 | [a, b, c, d, e] |
| 438 | }; |
| 439 | let c_y = scale_perez(c_y0); |
| 440 | let c_x = scale_perez(c_x0); |
| 441 | let c_yc = scale_perez(c_y_c0); |
| 442 | |
| 443 | // Log-average Perez luminance normalization: geom_mean(Y_hdr) == Exposure. |
| 444 | // When Horizon.Luminance pushes fY <= 0 (log NaN), Fusion renders black across the sky. |
| 445 | const N_TH: usize = 32; |
| 446 | const N_PH: usize = 32; |
| 447 | let mut sum_log = 0.0_f64; |
| 448 | let mut invalid_fy = false; |
| 449 | 'dome: for iy in 0..N_TH { |
| 450 | let th = (iy as f64 + 0.5) / N_TH as f64 * std::f64::consts::FRAC_PI_2; |
| 451 | let (st, ct) = th.sin_cos(); |
| 452 | for ix in 0..N_PH { |
| 453 | let ph = (ix as f64 + 0.5) / N_PH as f64 * std::f64::consts::TAU; |
| 454 | let (sp, cp) = ph.sin_cos(); |
| 455 | let cos_g = (st * cp * sun_dir[0] + st * sp * sun_dir[1] + ct * sun_dir[2]).clamp(-1.0, 1.0); |
| 456 | let fy = eval_perez(th, cos_g.acos(), c_y); |
| 457 | if fy <= 0.0 || !fy.is_finite() { |
| 458 | invalid_fy = true; |
| 459 | break 'dome; |
| 460 | } |
| 461 | sum_log += fy.ln(); |
| 462 | } |
| 463 | } |
| 464 | let geomean_fy = (sum_log / (N_TH * N_PH) as f64).exp().max(1e-5); |
| 465 | let lum_gain = if invalid_fy { |
| 466 | 0.0 |
| 467 | } else if !do_tm { |
| 468 | 1000.0 |
| 469 | } else { |
| 470 | (exposure * night_lum_scale / geomean_fy) as f32 |
| 471 | }; |
| 472 | let f_y0 = 1.0_f64; |
| 473 | let f_x0 = eval_perez(0.0, ts, c_x); |
| 474 | let f_yc0 = eval_perez(0.0, ts, c_yc); |
| 475 | let yz_scaled = lum_gain; |
| 476 | |
| 477 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); |
| 478 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 479 | |
| 480 | let mut u = UniformParams::default(); |
| 481 | u.p[0] = [sun_dir[0] as f32, sun_dir[1] as f32, sun_dir[2] as f32, yz_scaled]; |
| 482 | u.p[1] = [xz as f32, yz as f32, f_y0 as f32, f_x0 as f32]; |
| 483 | u.p[2] = [ |
| 484 | f_yc0 as f32, |
| 485 | if !do_tm && !invalid_fy { 1.0 } else { blend }, |
| 486 | if !do_tm && !invalid_fy { 0.0 } else if mask.is_some() { 1.0 } else { 0.0 }, |
| 487 | if mask_inv { 1.0 } else { 0.0 }, |
| 488 | ]; |
| 489 | u.p[3] = [c_y[0] as f32, c_y[1] as f32, c_y[2] as f32, c_y[3] as f32]; |
| 490 | u.p[4] = [c_y[4] as f32, c_x[0] as f32, c_x[1] as f32, c_x[2] as f32]; |
| 491 | u.p[5] = [c_x[3] as f32, c_x[4] as f32, c_yc[0] as f32, c_yc[1] as f32]; |
| 492 | u.p[6] = [c_yc[2] as f32, c_yc[3] as f32, c_yc[4] as f32, if do_tm { 1.0 } else { 0.0 }]; |
| 493 | |
| 494 | let key = self.make_key( |
| 495 | node, |
| 496 | "DaySky", |
| 497 | size, |
| 498 | &[mask_hash, dst_depth as u64], |
| 499 | bytemuck::cast_slice(&u.p[..7]), |
| 500 | ); |
| 501 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 502 | stats.cache_hits += 1; |
| 503 | return hit; |
| 504 | } |
| 505 | stats.cache_misses += 1; |
| 506 | stats.nodes_evaluated += 1; |
| 507 | let boost_unmapped = !do_tm && !invalid_fy; |
| 508 | let sky_dst = self.alloc_tex(size, dst_depth, if boost_unmapped { 0 } else { key }); |
| 509 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 510 | self.dispatch_op(enc, ShaderOp::DaySky, &self.dummy.view, &self.dummy.view, mv, &sky_dst, &u); |
| 511 | let dst = if boost_unmapped { |
| 512 | let gained = self.alloc_tex(size, dst_depth, if mask.is_some() { 0 } else { key }); |
| 513 | let mut ug = UniformParams::default(); |
| 514 | ug.p[0] = [100.0, 100.0, 100.0, 0.0]; |
| 515 | ug.p[1] = [0.0, 0.0, 0.0, blend]; |
| 516 | ug.p[2] = [1.0, 1.0, 1.0, 1.0]; |
| 517 | ug.p[4] = [0.0, 1.0, 1.0, 0.0]; |
| 518 | ug.p[9] = [1.0, 0.0, 1.0, 0.0]; |
| 519 | self.dispatch_op(enc, ShaderOp::ColorGain, &sky_dst.view, &self.dummy.view, &self.dummy.view, &gained, &ug); |
| 520 | if mask.is_some() { |
| 521 | let masked = self.alloc_tex(size, dst_depth, key); |
| 522 | let mut um = UniformParams::default(); |
| 523 | um.p[0][0] = if mask_inv { 1.0 } else { 0.0 }; |
| 524 | self.dispatch_op(enc, ShaderOp::MaskApply, &gained.view, &self.dummy.view, mv, &masked, &um); |
| 525 | masked |
| 526 | } else { |
| 527 | gained |
| 528 | } |
| 529 | } else { |
| 530 | sky_dst |
| 531 | }; |
| 532 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 533 | dst |
| 534 | } |
| 535 | |
| 536 | #[allow(clippy::too_many_arguments)] |
| 537 | pub(super) fn eval_grid_warp( |
| 538 | &mut self, |
| 539 | comp: &Comp, |
| 540 | node: &Node, |
| 541 | time: f64, |
| 542 | frame_i: i64, |
| 543 | enc: &mut wgpu::CommandEncoder, |
| 544 | visiting: &mut HashSet<(NodeId, i64)>, |
| 545 | stats: &mut EvalStats, |
| 546 | ) -> CachedTexture { |
| 547 | let Some(src_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) else { |
| 548 | return self.dummy.clone(); |
| 549 | }; |
| 550 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 551 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 552 | if blend <= 1e-6 { |
| 553 | return src; |
| 554 | } |
| 555 | let src_mesh = comp.eval_grid_mesh(node, "SrcGridChange", time); |
| 556 | let dst_mesh = comp.eval_grid_mesh(node, "DstGridChange", time); |
| 557 | let (Some(src_mesh), Some(dst_mesh)) = (src_mesh, dst_mesh) else { |
| 558 | return src; |
| 559 | }; |
| 560 | if src_mesh == dst_mesh { |
| 561 | return src; |
| 562 | } |
| 563 | let src = self.crop_to_canvas(enc, src); |
| 564 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 565 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 566 | |
| 567 | let mut u = UniformParams::default(); |
| 568 | u.p[0] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, 0.0]; |
| 569 | |
| 570 | let mut mesh_params: Vec<f32> = Vec::with_capacity(4 + (src_mesh.points.len() + dst_mesh.points.len()) * 10); |
| 571 | mesh_params.extend_from_slice(&[blend, src_mesh.cols as f32, src_mesh.rows as f32, dst_mesh.cols as f32]); |
| 572 | for m in [&src_mesh, &dst_mesh] { |
| 573 | for p in &m.points { |
| 574 | mesh_params.extend_from_slice(&[ |
| 575 | p.x as f32, |
| 576 | p.y as f32, |
| 577 | p.l_cx.unwrap_or(f32::NAN as f64) as f32, |
| 578 | p.l_cy.unwrap_or(0.0) as f32, |
| 579 | p.r_cx.unwrap_or(f32::NAN as f64) as f32, |
| 580 | p.r_cy.unwrap_or(0.0) as f32, |
| 581 | p.t_cx.unwrap_or(0.0) as f32, |
| 582 | p.t_cy.unwrap_or(f32::NAN as f64) as f32, |
| 583 | p.b_cx.unwrap_or(0.0) as f32, |
| 584 | p.b_cy.unwrap_or(f32::NAN as f64) as f32, |
| 585 | ]); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | let key = self.make_key(node, "GridWarp", src.size, &[src.content_hash, mask_hash], &mesh_params); |
| 590 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 591 | stats.cache_hits += 1; |
| 592 | return hit; |
| 593 | } |
| 594 | stats.cache_misses += 1; |
| 595 | stats.nodes_evaluated += 1; |
| 596 | |
| 597 | let uv_f16 = rasterize_grid_warp_uv_f16(&src_mesh, &dst_mesh, src.size); |
| 598 | let uv_tex = create_texture_dod_fmt( |
| 599 | &self.device, |
| 600 | src.size, |
| 601 | [0, 0], |
| 602 | src.size, |
| 603 | BitDepth::Float16, |
| 604 | wgpu::TextureFormat::Rgba16Float, |
| 605 | 0, |
| 606 | ); |
| 607 | self.queue.write_texture( |
| 608 | uv_tex.texture.as_image_copy(), |
| 609 | bytemuck::cast_slice(&uv_f16), |
| 610 | wgpu::TexelCopyBufferLayout { |
| 611 | offset: 0, |
| 612 | bytes_per_row: Some(src.size[0] * 8), |
| 613 | rows_per_image: Some(src.size[1]), |
| 614 | }, |
| 615 | wgpu::Extent3d { |
| 616 | width: src.size[0], |
| 617 | height: src.size[1], |
| 618 | depth_or_array_layers: 1, |
| 619 | }, |
| 620 | ); |
| 621 | |
| 622 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 623 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 624 | self.dispatch_op(enc, ShaderOp::GridWarp, &src.view, &uv_tex.view, mv, &dst, &u); |
| 625 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 626 | dst |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | fn quad_forward_homography(bl: [f64; 2], br: [f64; 2], tr: [f64; 2], tl: [f64; 2]) -> [f32; 9] { |
| 631 | let dx1 = br[0] - tr[0]; |
| 632 | let dy1 = br[1] - tr[1]; |
| 633 | let dx2 = tl[0] - tr[0]; |
| 634 | let dy2 = tl[1] - tr[1]; |
| 635 | let sx = bl[0] - br[0] + tr[0] - tl[0]; |
| 636 | let sy = bl[1] - br[1] + tr[1] - tl[1]; |
| 637 | let denom = dx1 * dy2 - dx2 * dy1; |
| 638 | if denom.abs() < 1e-10 { |
| 639 | return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]; |
| 640 | } |
| 641 | let g = (sx * dy2 - sy * dx2) / denom; |
| 642 | let h = (dx1 * sy - dy1 * sx) / denom; |
| 643 | let a = br[0] - bl[0] + g * br[0]; |
| 644 | let b = tl[0] - bl[0] + h * tl[0]; |
| 645 | let c = bl[0]; |
| 646 | let d = br[1] - bl[1] + g * br[1]; |
| 647 | let e = tl[1] - bl[1] + h * tl[1]; |
| 648 | let f = bl[1]; |
| 649 | [a as f32, b as f32, c as f32, d as f32, e as f32, f as f32, g as f32, h as f32, 1.0] |
| 650 | } |
| 651 | |
| 652 | /// Output-space corners (Fusion UV) of `src_rect` (`u0, v0, u1, v1`) pulled back through the corner mapping, or |
| 653 | /// `None` when the mapping is degenerate or the pullback reaches infinity. |
| 654 | fn positioner_preimage(mapping_type: i32, quad: [[f64; 2]; 4], h: [f32; 9], src_rect: [f64; 4]) -> Option<Vec<[f64; 2]>> { |
| 655 | let [bl, br, tr, tl] = quad; |
| 656 | let [u0, v0, u1, v1] = src_rect; |
| 657 | let source_corners = [[u0, v0], [u1, v0], [u1, v1], [u0, v1]]; |
| 658 | if mapping_type == 1 { |
| 659 | let [a, b, c, d, e, f, g, hh, i] = h.map(f64::from); |
| 660 | let inv = [ |
| 661 | [e * i - f * hh, c * hh - b * i, b * f - c * e], |
| 662 | [f * g - d * i, a * i - c * g, c * d - a * f], |
| 663 | [d * hh - e * g, b * g - a * hh, a * e - b * d], |
| 664 | ]; |
| 665 | let det = a * inv[0][0] + b * inv[1][0] + c * inv[2][0]; |
| 666 | if det.abs() < 1e-12 { |
| 667 | return None; |
| 668 | } |
| 669 | let row = |r: [f64; 3], s: [f64; 2]| (r[0] * s[0] + r[1] * s[1] + r[2]) / det; |
| 670 | let center = [0.25 * (bl[0] + br[0] + tr[0] + tl[0]), 0.25 * (bl[1] + br[1] + tr[1] + tl[1])]; |
| 671 | let w_center = row(inv[2], center); |
| 672 | if w_center.abs() < 1e-12 { |
| 673 | return None; |
| 674 | } |
| 675 | let sign = w_center.signum(); |
| 676 | let eps = 1e-3 * w_center.abs(); |
| 677 | let mut clipped = Vec::with_capacity(6); |
| 678 | for idx in 0..4 { |
| 679 | let s0 = source_corners[idx]; |
| 680 | let s1 = source_corners[(idx + 1) % 4]; |
| 681 | let d0 = row(inv[2], s0) * sign - eps; |
| 682 | let d1 = row(inv[2], s1) * sign - eps; |
| 683 | if d0 >= 0.0 { |
| 684 | clipped.push(s0); |
| 685 | } |
| 686 | if (d0 >= 0.0) != (d1 >= 0.0) { |
| 687 | let t = d0 / (d0 - d1); |
| 688 | clipped.push([s0[0] + (s1[0] - s0[0]) * t, s0[1] + (s1[1] - s0[1]) * t]); |
| 689 | } |
| 690 | } |
| 691 | if clipped.is_empty() { |
| 692 | return None; |
| 693 | } |
| 694 | let out = clipped |
| 695 | .into_iter() |
| 696 | .map(|s| { |
| 697 | let w = row(inv[2], s); |
| 698 | [row(inv[0], s) / w, row(inv[1], s) / w] |
| 699 | }) |
| 700 | .collect(); |
| 701 | return Some(out); |
| 702 | } |
| 703 | let mut out = Vec::with_capacity(4); |
| 704 | // Bilinear: s(x, y) = bl + (br - bl) x + (tl - bl) y + (bl - br - tl + tr) x y, solved by Newton. |
| 705 | let ex = [br[0] - bl[0], br[1] - bl[1]]; |
| 706 | let ey = [tl[0] - bl[0], tl[1] - bl[1]]; |
| 707 | let exy = [bl[0] - br[0] - tl[0] + tr[0], bl[1] - br[1] - tl[1] + tr[1]]; |
| 708 | for t in source_corners { |
| 709 | let (mut x, mut y) = (0.5, 0.5); |
| 710 | let mut converged = false; |
| 711 | for _ in 0..64 { |
| 712 | let r = [0, 1].map(|k| bl[k] + ex[k] * x + ey[k] * y + exy[k] * x * y - t[k]); |
| 713 | let jx = [0, 1].map(|k| ex[k] + exy[k] * y); |
| 714 | let jy = [0, 1].map(|k| ey[k] + exy[k] * x); |
| 715 | let det = jx[0] * jy[1] - jy[0] * jx[1]; |
| 716 | if det.abs() < 1e-12 { |
| 717 | return None; |
| 718 | } |
| 719 | let (dx, dy) = ((r[0] * jy[1] - jy[0] * r[1]) / det, (jx[0] * r[1] - r[0] * jx[1]) / det); |
| 720 | x -= dx; |
| 721 | y -= dy; |
| 722 | if dx.abs() + dy.abs() < 1e-10 { |
| 723 | converged = true; |
| 724 | break; |
| 725 | } |
| 726 | } |
| 727 | if !converged { |
| 728 | return None; |
| 729 | } |
| 730 | out.push([x, y]); |
| 731 | } |
| 732 | Some(out) |
| 733 | } |
| 734 | |
| 735 | fn parse_month_index(node: &Node, comp: &Comp, time: f64, expr: &ExprEngine) -> usize { |
| 736 | if let Some(m) = node.static_str("Month") { |
| 737 | let low = m.to_ascii_lowercase(); |
| 738 | let names = [ |
| 739 | "january", "february", "march", "april", "may", "june", |
| 740 | "july", "august", "september", "october", "november", "december", |
| 741 | ]; |
| 742 | if let Some(idx) = names.iter().position(|&n| low.starts_with(&n[..3])) { |
| 743 | return idx + 1; |
| 744 | } |
| 745 | if let Ok(v) = m.parse::<usize>() { |
| 746 | return v.clamp(1, 12); |
| 747 | } |
| 748 | } |
| 749 | (comp.eval_number(node, "Month", time, 0.0, expr).round() as usize + 1).clamp(1, 12) |
| 750 | } |
| 751 | |
| 752 | fn solar_direction(lat_deg: f64, lon_deg: f64, month: usize, day: f64, hour: f64) -> ([f64; 3], f64) { |
| 753 | const CUM_DAYS: [i32; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; |
| 754 | let doy = CUM_DAYS[month.saturating_sub(1).min(11)] as f64 + day; |
| 755 | let b = 2.0 * std::f64::consts::PI * (doy - 81.0) / 365.0; |
| 756 | let eot_min = 9.87 * (2.0 * b).sin() - 7.53 * b.cos() - 1.5 * b.sin(); |
| 757 | let decl = 23.44_f64.to_radians() * ((2.0 * std::f64::consts::PI / 365.0) * (doy - 81.0)).sin(); |
| 758 | let std_meridian = (lon_deg / 15.0).trunc() * 15.0; |
| 759 | let solar_hour = hour + (std_meridian - lon_deg) / 15.0 + eot_min / 60.0; |
| 760 | let hour_angle = ((solar_hour - 12.0) * 15.0).to_radians(); |
| 761 | let lat = lat_deg.to_radians(); |
| 762 | let sin_alt = (lat.sin() * decl.sin() + lat.cos() * decl.cos() * hour_angle.cos()).clamp(-1.0, 1.0); |
| 763 | let ts = (std::f64::consts::FRAC_PI_2 - sin_alt.asin()).max(0.0); |
| 764 | // Azimuth measured from South (0=South, pi/2=East morning, pi=North, 3*pi/2=West afternoon) |
| 765 | let cos_az_south = (-(decl.sin() * lat.cos() - decl.cos() * lat.sin() * hour_angle.cos()) |
| 766 | / ts.sin().max(1e-5)) |
| 767 | .clamp(-1.0, 1.0); |
| 768 | let mut phi_s = cos_az_south.acos(); |
| 769 | if hour_angle > 0.0 { |
| 770 | phi_s = 2.0 * std::f64::consts::PI - phi_s; |
| 771 | } |
| 772 | let dir = [ts.sin() * phi_s.cos(), ts.sin() * phi_s.sin(), ts.cos()]; |
| 773 | (dir, ts) |
| 774 | } |
| 775 | |
| 776 | fn preetham_zenith_xyy(t: f64, ts: f64) -> (f64, f64, f64) { |
| 777 | let t2 = t * t; |
| 778 | let ts2 = ts * ts; |
| 779 | let ts3 = ts2 * ts; |
| 780 | let chi = (4.0 / 9.0 - t / 120.0) * (std::f64::consts::PI - 2.0 * ts); |
| 781 | let yz_lum = ((4.0453 * t - 4.9710) * chi.tan() - 0.2155 * t + 2.4192).max(0.05); |
| 782 | let xz = (0.00166 * ts3 - 0.00375 * ts2 + 0.00209 * ts) * t2 |
| 783 | + (-0.02903 * ts3 + 0.06377 * ts2 - 0.03202 * ts + 0.00394) * t |
| 784 | + (0.11693 * ts3 - 0.21196 * ts2 + 0.06052 * ts + 0.25886); |
| 785 | let yz = (0.00275 * ts3 - 0.00610 * ts2 + 0.00317 * ts) * t2 |
| 786 | + (-0.04214 * ts3 + 0.08970 * ts2 - 0.04153 * ts + 0.00516) * t |
| 787 | + (0.15346 * ts3 - 0.26756 * ts2 + 0.06670 * ts + 0.26688); |
| 788 | (xz, yz, yz_lum) |
| 789 | } |
| 790 | |
| 791 | fn eval_perez(theta: f64, gamma: f64, c: [f64; 5]) -> f64 { |
| 792 | let ct = theta.cos().max(0.02); |
| 793 | let cg = gamma.cos(); |
| 794 | (1.0 + c[0] * (c[1] / ct).exp()) * (1.0 + c[2] * (c[3] * gamma).exp() + c[4] * cg * cg) |
| 795 | } |
| 796 | |
| 797 | #[derive(Clone, Copy)] |
| 798 | struct CoonsPt { |
| 799 | p: [f32; 2], |
| 800 | l: [f32; 2], |
| 801 | r: [f32; 2], |
| 802 | t: [f32; 2], |
| 803 | b: [f32; 2], |
| 804 | } |
| 805 | |
| 806 | fn mesh_coons_cells(mesh: &model::comp::GridMesh) -> Vec<[CoonsPt; 4]> { |
| 807 | let cols = mesh.cols; |
| 808 | let rows = mesh.rows; |
| 809 | if cols < 2 || rows < 2 || mesh.points.len() < cols.saturating_mul(rows) { |
| 810 | return Vec::new(); |
| 811 | } |
| 812 | let dc = (mesh.delta_c as f32) / 3.0; |
| 813 | let dr = (mesh.delta_r as f32) / 3.0; |
| 814 | let pts: Vec<CoonsPt> = mesh |
| 815 | .points |
| 816 | .iter() |
| 817 | .map(|mp| { |
| 818 | let p = [mp.x as f32, mp.y as f32]; |
| 819 | let l = [p[0] + mp.l_cx.map_or(-dc, |v| v as f32), p[1] + mp.l_cy.unwrap_or(0.0) as f32]; |
| 820 | let r = [p[0] + mp.r_cx.map_or(dc, |v| v as f32), p[1] + mp.r_cy.unwrap_or(0.0) as f32]; |
| 821 | let t = [p[0] + mp.t_cx.unwrap_or(0.0) as f32, p[1] + mp.t_cy.map_or(-dr, |v| v as f32)]; |
| 822 | let b = [p[0] + mp.b_cx.unwrap_or(0.0) as f32, p[1] + mp.b_cy.map_or(dr, |v| v as f32)]; |
| 823 | CoonsPt { p, l, r, t, b } |
| 824 | }) |
| 825 | .collect(); |
| 826 | let mut cells = Vec::with_capacity((cols - 1) * (rows - 1)); |
| 827 | for r in 0..(rows - 1) { |
| 828 | for c in 0..(cols - 1) { |
| 829 | cells.push([ |
| 830 | pts[r * cols + c], |
| 831 | pts[r * cols + c + 1], |
| 832 | pts[(r + 1) * cols + c], |
| 833 | pts[(r + 1) * cols + c + 1], |
| 834 | ]); |
| 835 | } |
| 836 | } |
| 837 | cells |
| 838 | } |
| 839 | |
| 840 | fn bez3_2d(p0: [f32; 2], p1: [f32; 2], p2: [f32; 2], p3: [f32; 2], t: f32) -> [f32; 2] { |
| 841 | let u = 1.0 - t; |
| 842 | let (w0, w1, w2, w3) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t); |
| 843 | [ |
| 844 | w0 * p0[0] + w1 * p1[0] + w2 * p2[0] + w3 * p3[0], |
| 845 | w0 * p0[1] + w1 * p1[1] + w2 * p2[1] + w3 * p3[1], |
| 846 | ] |
| 847 | } |
| 848 | |
| 849 | fn eval_coons_cell(cell: &[CoonsPt; 4], u: f32, v: f32) -> [f32; 2] { |
| 850 | let [p00, p10, p01, p11] = *cell; |
| 851 | let bot = bez3_2d(p00.p, p00.r, p10.l, p10.p, u); |
| 852 | let top = bez3_2d(p01.p, p01.r, p11.l, p11.p, u); |
| 853 | let lft = bez3_2d(p00.p, p00.b, p01.t, p01.p, v); |
| 854 | let rgt = bez3_2d(p10.p, p10.b, p11.t, p11.p, v); |
| 855 | let bi0 = (1.0 - u) * (1.0 - v) * p00.p[0] + u * (1.0 - v) * p10.p[0] + (1.0 - u) * v * p01.p[0] + u * v * p11.p[0]; |
| 856 | let bi1 = (1.0 - u) * (1.0 - v) * p00.p[1] + u * (1.0 - v) * p10.p[1] + (1.0 - u) * v * p01.p[1] + u * v * p11.p[1]; |
| 857 | [ |
| 858 | (1.0 - v) * bot[0] + v * top[0] + (1.0 - u) * lft[0] + u * rgt[0] - bi0, |
| 859 | (1.0 - v) * bot[1] + v * top[1] + (1.0 - u) * lft[1] + u * rgt[1] - bi1, |
| 860 | ] |
| 861 | } |
| 862 | |
| 863 | fn rasterize_grid_warp_uv_f16( |
| 864 | src_mesh: &model::comp::GridMesh, |
| 865 | dst_mesh: &model::comp::GridMesh, |
| 866 | dims: [u32; 2], |
| 867 | ) -> Vec<u16> { |
| 868 | let [w, h] = dims; |
| 869 | let (wf, hf) = (w as f32, h as f32); |
| 870 | let mut uv_buf = vec![0u16; (w as usize) * (h as usize) * 4]; |
| 871 | let src_cells = mesh_coons_cells(src_mesh); |
| 872 | let dst_cells = mesh_coons_cells(dst_mesh); |
| 873 | let sub = 24usize; |
| 874 | let one_f16 = crate::media::f32_to_f16_bits(1.0); |
| 875 | |
| 876 | for (sc, dc) in src_cells.iter().zip(dst_cells.iter()) { |
| 877 | for j in 0..sub { |
| 878 | let (v0, v1) = (j as f32 / sub as f32, (j + 1) as f32 / sub as f32); |
| 879 | for i in 0..sub { |
| 880 | let (u0, u1) = (i as f32 / sub as f32, (i + 1) as f32 / sub as f32); |
| 881 | let da = eval_coons_cell(dc, u0, v0); |
| 882 | let db = eval_coons_cell(dc, u1, v0); |
| 883 | let dc2 = eval_coons_cell(dc, u1, v1); |
| 884 | let dd = eval_coons_cell(dc, u0, v1); |
| 885 | let sa = eval_coons_cell(sc, u0, v0); |
| 886 | let sb = eval_coons_cell(sc, u1, v0); |
| 887 | let sc2 = eval_coons_cell(sc, u1, v1); |
| 888 | let sd = eval_coons_cell(sc, u0, v1); |
| 889 | |
| 890 | for ([pa, pb, pc], [qa, qb, qc]) in [([da, db, dc2], [sa, sb, sc2]), ([da, dc2, dd], [sa, sc2, sd])] { |
| 891 | let (ax, ay) = ((pa[0] + 0.5) * wf, (0.5 - pa[1]) * hf); |
| 892 | let (bx, by) = ((pb[0] + 0.5) * wf, (0.5 - pb[1]) * hf); |
| 893 | let (cx, cy) = ((pc[0] + 0.5) * wf, (0.5 - pc[1]) * hf); |
| 894 | let area = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax); |
| 895 | if area.abs() < 1e-6 { |
| 896 | continue; |
| 897 | } |
| 898 | let inv_area = 1.0 / area; |
| 899 | let x0 = ax.min(bx).min(cx).floor().max(0.0) as u32; |
| 900 | let x1 = (ax.max(bx).max(cx).ceil() as u32).min(w.saturating_sub(1)); |
| 901 | let y0 = ay.min(by).min(cy).floor().max(0.0) as u32; |
| 902 | let y1 = (ay.max(by).max(cy).ceil() as u32).min(h.saturating_sub(1)); |
| 903 | for py in y0..=y1 { |
| 904 | let qy = py as f32 + 0.5; |
| 905 | for px in x0..=x1 { |
| 906 | let qx = px as f32 + 0.5; |
| 907 | let w0 = ((bx - qx) * (cy - qy) - (by - qy) * (cx - qx)) * inv_area; |
| 908 | let w1 = ((cx - qx) * (ay - qy) - (cy - qy) * (ax - qx)) * inv_area; |
| 909 | let w2 = 1.0 - w0 - w1; |
| 910 | if w0 >= -1e-4 && w1 >= -1e-4 && w2 >= -1e-4 { |
| 911 | let su = w0 * qa[0] + w1 * qb[0] + w2 * qc[0] + 0.5; |
| 912 | let sv = w0 * qa[1] + w1 * qb[1] + w2 * qc[1] + 0.5; |
| 913 | let idx = (py as usize * w as usize + px as usize) * 4; |
| 914 | uv_buf[idx] = crate::media::f32_to_f16_bits(su); |
| 915 | uv_buf[idx + 1] = crate::media::f32_to_f16_bits(sv); |
| 916 | uv_buf[idx + 2] = one_f16; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | uv_buf |
| 925 | } |
| 926 | |
| 927 | #[cfg(test)] |
| 928 | mod tests { |
| 929 | use super::*; |
| 930 | |
| 931 | #[test] |
| 932 | fn positioner_preimage_of_a_zoomed_quad_spans_twice_the_frame() { |
| 933 | // Corners cover the middle half of the source, so the whole source lands at [-0.5, 1.5]^2. |
| 934 | let quad = [[0.25, 0.25], [0.75, 0.25], [0.75, 0.75], [0.25, 0.75]]; |
| 935 | let h = quad_forward_homography(quad[0], quad[1], quad[2], quad[3]); |
| 936 | for mapping_type in [0, 1] { |
| 937 | let corners = positioner_preimage(mapping_type, quad, h, [0.0, 0.0, 1.0, 1.0]).expect("finite pullback"); |
| 938 | let (lo, hi) = corners.iter().flatten().fold((f64::MAX, f64::MIN), |(lo, hi), &v| (lo.min(v), hi.max(v))); |
| 939 | assert!((lo + 0.5).abs() < 1e-6 && (hi - 1.5).abs() < 1e-6, "type {mapping_type}: {lo}..{hi}"); |
| 940 | } |
| 941 | let flat = [[0.0, 0.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0]]; |
| 942 | assert!(positioner_preimage(0, flat, quad_forward_homography(flat[0], flat[1], flat[2], flat[3]), [0.0, 0.0, 1.0, 1.0]).is_none()); |
| 943 | } |
| 944 | use crate::Engine; |
| 945 | |
| 946 | #[test] |
| 947 | fn test_drip_concentric_ripple_displacement() { |
| 948 | let src = r#"Composition { |
| 949 | CurrentTime = 0, |
| 950 | RenderRange = { 0, 1 }, |
| 951 | GlobalRange = { 0, 1 }, |
| 952 | Tools = ordered() { |
| 953 | BG1 = Background { |
| 954 | Inputs = { |
| 955 | Width = Input { Value = 128, }, |
| 956 | Height = Input { Value = 128, }, |
| 957 | Type = Input { Value = FuID { "Horizontal" }, }, |
| 958 | TopLeftRed = Input { Value = 1.0, }, |
| 959 | TopLeftGreen = Input { Value = 0.0, }, |
| 960 | TopLeftBlue = Input { Value = 0.0, }, |
| 961 | TopRightRed = Input { Value = 0.0, }, |
| 962 | TopRightGreen = Input { Value = 0.0, }, |
| 963 | TopRightBlue = Input { Value = 1.0, }, |
| 964 | }, |
| 965 | }, |
| 966 | Drip1 = Drip { |
| 967 | Inputs = { |
| 968 | Input = Input { SourceOp = "BG1", Source = "Output", }, |
| 969 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 970 | Strength = Input { Value = 1.0, }, |
| 971 | Amplitude = Input { Value = 0.12, }, |
| 972 | Frequency = Input { Value = 4.0, }, |
| 973 | Phase = Input { Value = 0.0, }, |
| 974 | Shape = Input { Value = 0, }, |
| 975 | }, |
| 976 | }, |
| 977 | }, |
| 978 | }"#; |
| 979 | let comp = Comp::from_fusion_str(src).expect("parse Drip comp"); |
| 980 | let Ok(mut eng) = Engine::new_headless() else { |
| 981 | return; |
| 982 | }; |
| 983 | let bg_id = comp.find_node("BG1").unwrap(); |
| 984 | let drip_id = comp.find_node("Drip1").unwrap(); |
| 985 | let (bg_tex, _) = eng.evaluate(&comp, bg_id, 0.0); |
| 986 | let (drip_tex, stats) = eng.evaluate(&comp, drip_id, 0.0); |
| 987 | assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes); |
| 988 | let bg_px = eng.readback_rgba(&bg_tex).expect("readback bg"); |
| 989 | let drip_px = eng.readback_rgba(&drip_tex).expect("readback drip"); |
| 990 | |
| 991 | let diff_sum: u64 = bg_px |
| 992 | .iter() |
| 993 | .zip(drip_px.iter()) |
| 994 | .map(|(&a, &b)| a.abs_diff(b) as u64) |
| 995 | .sum(); |
| 996 | assert!(diff_sum > 10_000, "Drip should visibly displace pixels, got diff_sum={diff_sum}"); |
| 997 | } |
| 998 | |
| 999 | #[test] |
| 1000 | fn test_dent_radial_distortion() { |
| 1001 | let src = r#"Composition { |
| 1002 | CurrentTime = 0, |
| 1003 | RenderRange = { 0, 1 }, |
| 1004 | GlobalRange = { 0, 1 }, |
| 1005 | Tools = ordered() { |
| 1006 | BG1 = Background { |
| 1007 | Inputs = { |
| 1008 | Width = Input { Value = 128, }, |
| 1009 | Height = Input { Value = 128, }, |
| 1010 | Type = Input { Value = FuID { "Horizontal" }, }, |
| 1011 | TopLeftRed = Input { Value = 1.0, }, |
| 1012 | TopLeftGreen = Input { Value = 0.0, }, |
| 1013 | TopLeftBlue = Input { Value = 0.0, }, |
| 1014 | TopRightRed = Input { Value = 0.0, }, |
| 1015 | TopRightGreen = Input { Value = 1.0, }, |
| 1016 | TopRightBlue = Input { Value = 0.0, }, |
| 1017 | }, |
| 1018 | }, |
| 1019 | Dent1 = Dent { |
| 1020 | Inputs = { |
| 1021 | Input = Input { SourceOp = "BG1", Source = "Output", }, |
| 1022 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 1023 | Size = Input { Value = 0.6, }, |
| 1024 | Strength = Input { Value = 0.8, }, |
| 1025 | Type = Input { Value = 0, }, |
| 1026 | }, |
| 1027 | }, |
| 1028 | }, |
| 1029 | }"#; |
| 1030 | let comp = Comp::from_fusion_str(src).expect("parse Dent comp"); |
| 1031 | let Ok(mut eng) = Engine::new_headless() else { |
| 1032 | return; |
| 1033 | }; |
| 1034 | let bg_id = comp.find_node("BG1").unwrap(); |
| 1035 | let dent_id = comp.find_node("Dent1").unwrap(); |
| 1036 | let (bg_tex, _) = eng.evaluate(&comp, bg_id, 0.0); |
| 1037 | let (dent_tex, stats) = eng.evaluate(&comp, dent_id, 0.0); |
| 1038 | assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes); |
| 1039 | let bg_px = eng.readback_rgba(&bg_tex).expect("readback bg"); |
| 1040 | let dent_px = eng.readback_rgba(&dent_tex).expect("readback dent"); |
| 1041 | |
| 1042 | // Corners outside radius=0.3 must remain unchanged; interior must be distorted! |
| 1043 | assert_eq!(&bg_px[0..4], &dent_px[0..4], "corner pixel outside dent radius should match"); |
| 1044 | let mut interior_diff = 0u64; |
| 1045 | for y in 44..84 { |
| 1046 | for x in 44..84 { |
| 1047 | let idx = (y * 128 + x) * 4; |
| 1048 | interior_diff += bg_px[idx].abs_diff(dent_px[idx]) as u64; |
| 1049 | interior_diff += bg_px[idx + 1].abs_diff(dent_px[idx + 1]) as u64; |
| 1050 | } |
| 1051 | } |
| 1052 | assert!(interior_diff > 2_000, "Dent interior should visibly distort, got {interior_diff}"); |
| 1053 | } |
| 1054 | |
| 1055 | #[test] |
| 1056 | fn test_drip_shapes_and_strength_independence() { |
| 1057 | let make_comp = |strength: f64, shape: i32| { |
| 1058 | format!( |
| 1059 | r#"Composition {{ |
| 1060 | CurrentTime = 0, |
| 1061 | RenderRange = {{ 0, 1 }}, |
| 1062 | GlobalRange = {{ 0, 1 }}, |
| 1063 | Tools = ordered() {{ |
| 1064 | BG1 = Background {{ |
| 1065 | Inputs = {{ |
| 1066 | Width = Input {{ Value = 128, }}, |
| 1067 | Height = Input {{ Value = 128, }}, |
| 1068 | Type = Input {{ Value = FuID {{ "Horizontal" }}, }}, |
| 1069 | TopLeftRed = Input {{ Value = 1.0, }}, |
| 1070 | TopRightBlue = Input {{ Value = 1.0, }}, |
| 1071 | }}, |
| 1072 | }}, |
| 1073 | Drip1 = Drip {{ |
| 1074 | Inputs = {{ |
| 1075 | Input = Input {{ SourceOp = "BG1", Source = "Output", }}, |
| 1076 | Center = Input {{ Value = {{ 0.4, 0.6 }}, }}, |
| 1077 | Strength = Input {{ Value = {strength}, }}, |
| 1078 | Amplitude = Input {{ Value = 0.08, }}, |
| 1079 | Frequency = Input {{ Value = 4.0, }}, |
| 1080 | Phase = Input {{ Value = 0.75, }}, |
| 1081 | Shape = Input {{ Value = {shape}, }}, |
| 1082 | }}, |
| 1083 | }}, |
| 1084 | }}, |
| 1085 | }}"# |
| 1086 | ) |
| 1087 | }; |
| 1088 | let Ok(mut eng) = Engine::new_headless() else { |
| 1089 | return; |
| 1090 | }; |
| 1091 | let comp_a = Comp::from_fusion_str(&make_comp(0.25, 1)).unwrap(); |
| 1092 | let comp_b = Comp::from_fusion_str(&make_comp(1.0, 1)).unwrap(); |
| 1093 | let (tex_a, _) = eng.evaluate(&comp_a, comp_a.find_node("Drip1").unwrap(), 0.0); |
| 1094 | let (tex_b, _) = eng.evaluate(&comp_b, comp_b.find_node("Drip1").unwrap(), 0.0); |
| 1095 | let px_a = eng.readback_rgba(&tex_a).unwrap(); |
| 1096 | let px_b = eng.readback_rgba(&tex_b).unwrap(); |
| 1097 | assert_eq!(px_a, px_b, "Fusion Drip ignores Strength and uses Amplitude directly"); |
| 1098 | |
| 1099 | // Center pixel (0.4*128=51, (1-0.6)*128=51) must remain smooth (raw_rel=0 -> disp=0) |
| 1100 | let (bg_tex, _) = eng.evaluate(&comp_a, comp_a.find_node("BG1").unwrap(), 0.0); |
| 1101 | let bg_px = eng.readback_rgba(&bg_tex).unwrap(); |
| 1102 | let diff_sum: u64 = bg_px.iter().zip(px_a.iter()).map(|(&a, &b)| a.abs_diff(b) as u64).sum(); |
| 1103 | assert!(diff_sum > 5_000, "Square Drip should displace pixels, got {diff_sum}"); |
| 1104 | } |
| 1105 | |
| 1106 | #[test] |
| 1107 | fn test_dent_type0_signed_inversion() { |
| 1108 | let src = r#"Composition { |
| 1109 | CurrentTime = 0, |
| 1110 | RenderRange = { 0, 1 }, |
| 1111 | GlobalRange = { 0, 1 }, |
| 1112 | Tools = ordered() { |
| 1113 | BG1 = Background { |
| 1114 | Inputs = { |
| 1115 | Width = Input { Value = 128, }, |
| 1116 | Height = Input { Value = 128, }, |
| 1117 | Type = Input { Value = FuID { "Horizontal" }, }, |
| 1118 | TopLeftRed = Input { Value = 1.0, }, |
| 1119 | TopLeftGreen = Input { Value = 0.0, }, |
| 1120 | TopLeftBlue = Input { Value = 0.0, }, |
| 1121 | TopRightRed = Input { Value = 0.0, }, |
| 1122 | TopRightGreen = Input { Value = 1.0, }, |
| 1123 | TopRightBlue = Input { Value = 0.0, }, |
| 1124 | }, |
| 1125 | }, |
| 1126 | Dent1 = Dent { |
| 1127 | Inputs = { |
| 1128 | Input = Input { SourceOp = "BG1", Source = "Output", }, |
| 1129 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 1130 | Size = Input { Value = 0.65, }, |
| 1131 | Strength = Input { Value = 0.75, }, |
| 1132 | Type = Input { Value = 0, }, |
| 1133 | }, |
| 1134 | }, |
| 1135 | }, |
| 1136 | }"#; |
| 1137 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1138 | let Ok(mut eng) = Engine::new_headless() else { |
| 1139 | return; |
| 1140 | }; |
| 1141 | let (tex, _) = eng.evaluate(&comp, comp.find_node("Dent1").unwrap(), 0.0); |
| 1142 | let px = eng.readback_rgba(&tex).unwrap(); |
| 1143 | // Just right of center (x=72, r~0.066): new_r = r - 0.75*(0.325 - r) < 0, |
| 1144 | // so it samples the LEFT (red-dominant) side! |
| 1145 | let right_near = (64 * 128 + 72) * 4; |
| 1146 | assert!( |
| 1147 | px[right_near] > px[right_near + 1] + 40, |
| 1148 | "Dent Type=0 should invert radially near center: {:?}", |
| 1149 | &px[right_near..right_near + 4] |
| 1150 | ); |
| 1151 | } |
| 1152 | |
| 1153 | #[test] |
| 1154 | fn test_dent_sine_falls_off_linearly_from_strength_at_center() { |
| 1155 | let src = r#"Composition { |
| 1156 | CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 }, |
| 1157 | Tools = ordered() { |
| 1158 | BG1 = Background { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 128, }, |
| 1159 | Type = Input { Value = FuID { "Horizontal" }, }, |
| 1160 | TopLeftRed = Input { Value = 1, }, TopLeftGreen = Input { Value = 0, }, TopLeftBlue = Input { Value = 0, }, TopLeftAlpha = Input { Value = 1, }, |
| 1161 | TopRightRed = Input { Value = 0, }, TopRightGreen = Input { Value = 1, }, TopRightBlue = Input { Value = 0, }, TopRightAlpha = Input { Value = 1, }, }, }, |
| 1162 | Dent1 = Dent { Inputs = { Input = Input { SourceOp = "BG1", Source = "Output", }, |
| 1163 | Center = Input { Value = { 0.5, 0.5 }, }, Size = Input { Value = 0.5, }, |
| 1164 | Strength = Input { Value = 0.5, }, Type = Input { Value = 1, }, }, }, |
| 1165 | }, |
| 1166 | }"#; |
| 1167 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1168 | let Ok(mut eng) = Engine::new_headless() else { |
| 1169 | return; |
| 1170 | }; |
| 1171 | let (tex, _) = eng.evaluate(&comp, comp.find_node("Dent1").unwrap(), 0.0); |
| 1172 | let px = eng.readback_rgba(&tex).unwrap(); |
| 1173 | // x=72 (r=0.066 of the 0.25 radius): new_r = r - 0.5 * (1 - r / R) = -0.30 -> source at x=0.20, |
| 1174 | // so the gradient reads ~80% red / ~20% green. |
| 1175 | let i = (64 * 128 + 72) * 4; |
| 1176 | assert!(px[i].abs_diff(204) < 8 && px[i + 1].abs_diff(51) < 8, "got {:?}", &px[i..i + 4]); |
| 1177 | } |
| 1178 | |
| 1179 | #[test] |
| 1180 | fn test_drip_out_of_bounds_edges_clamp() { |
| 1181 | let src = r#"Composition { |
| 1182 | CurrentTime = 0, |
| 1183 | RenderRange = { 0, 0 }, |
| 1184 | GlobalRange = { 0, 0 }, |
| 1185 | Tools = ordered() { |
| 1186 | BG1 = Background { |
| 1187 | Inputs = { |
| 1188 | Width = Input { Value = 128, }, |
| 1189 | Height = Input { Value = 128, }, |
| 1190 | TopLeftRed = Input { Value = 1.0, }, |
| 1191 | TopLeftGreen = Input { Value = 1.0, }, |
| 1192 | TopLeftBlue = Input { Value = 1.0, }, |
| 1193 | TopLeftAlpha = Input { Value = 1.0, }, |
| 1194 | }, |
| 1195 | }, |
| 1196 | Drip1 = Drip { |
| 1197 | Inputs = { |
| 1198 | Input = Input { SourceOp = "BG1", Source = "Output", }, |
| 1199 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 1200 | Amplitude = Input { Value = 0.5, }, |
| 1201 | Frequency = Input { Value = 1.0, }, |
| 1202 | Phase = Input { Value = 0.25, }, |
| 1203 | Dampening = Input { Value = 0.0, }, |
| 1204 | Shape = Input { Value = 1, }, |
| 1205 | }, |
| 1206 | }, |
| 1207 | }, |
| 1208 | }"#; |
| 1209 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1210 | let Ok(mut eng) = Engine::new_headless() else { return; }; |
| 1211 | let (tex, _) = eng.evaluate(&comp, comp.find_node("Drip1").unwrap(), 0.0); |
| 1212 | let px = eng.readback_rgba(&tex).unwrap(); |
| 1213 | // Corner samples land outside [0, 1] and duplicate the border (Fusion 21.1 probe: 0.22% vs 2.50% MAE transparent). |
| 1214 | assert_eq!(&px[0..4], &[255, 255, 255, 255], "out-of-bounds Drip UV must clamp to the border"); |
| 1215 | // Center stays white (raw_rel=0 -> disp=0). |
| 1216 | let c = (64 * 128 + 64) * 4; |
| 1217 | assert_eq!(&px[c..c + 4], &[255, 255, 255, 255]); |
| 1218 | } |
| 1219 | |
| 1220 | #[test] |
| 1221 | fn test_perspective_positioner() { |
| 1222 | let src = r#"{ |
| 1223 | Tools = ordered() { |
| 1224 | Bg1 = Background { |
| 1225 | Inputs = { |
| 1226 | Width = Input { Value = 64, }, |
| 1227 | Height = Input { Value = 64, }, |
| 1228 | Type = Input { Value = FuID { "Horizontal" }, }, |
| 1229 | TopLeftRed = Input { Value = 1, }, |
| 1230 | TopRightRed = Input { Value = 0, }, |
| 1231 | TopRightBlue = Input { Value = 1, }, |
| 1232 | }, |
| 1233 | }, |
| 1234 | PP_Lin = PerspectivePositioner { |
| 1235 | Inputs = { |
| 1236 | TopLeft = Input { Value = { 0.25, 0.75 }, }, |
| 1237 | TopRight = Input { Value = { 0.75, 0.75 }, }, |
| 1238 | BottomLeft = Input { Value = { 0.25, 0.25 }, }, |
| 1239 | BottomRight = Input { Value = { 0.75, 0.25 }, }, |
| 1240 | Input = Input { SourceOp = "Bg1", Source = "Output", }, |
| 1241 | }, |
| 1242 | }, |
| 1243 | }, |
| 1244 | }"#; |
| 1245 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1246 | let Ok(mut eng) = Engine::new_headless() else { return; }; |
| 1247 | let (t_pp, _) = eng.evaluate(&comp, comp.find_node("PP_Lin").unwrap(), 0.0); |
| 1248 | let px_pp = eng.readback_rgba(&t_pp).unwrap(); |
| 1249 | let p0 = &px_pp[0..4]; |
| 1250 | assert!(p0[0] > 165 && p0[2] > 40 && p0[2] < 90, "got {p0:?}"); |
| 1251 | } |
| 1252 | |
| 1253 | #[test] |
| 1254 | fn test_day_sky_and_grid_warp() { |
| 1255 | let src = r#"{ |
| 1256 | Tools = ordered() { |
| 1257 | DS1 = DaySky { |
| 1258 | Inputs = { |
| 1259 | Width = Input { Value = 64, }, |
| 1260 | Height = Input { Value = 64, }, |
| 1261 | Latitude = Input { Value = 37.0, }, |
| 1262 | Longitude = Input { Value = 0.0, }, |
| 1263 | Time = Input { Value = 12.0, }, |
| 1264 | Exposure = Input { Value = 1.0, }, |
| 1265 | }, |
| 1266 | }, |
| 1267 | BgW = Background { |
| 1268 | Inputs = { |
| 1269 | Width = Input { Value = 64, }, |
| 1270 | Height = Input { Value = 64, }, |
| 1271 | Type = Input { Value = FuID { "Horizontal" }, }, |
| 1272 | TopLeftRed = Input { Value = 1, }, |
| 1273 | TopRightGreen = Input { Value = 1, }, |
| 1274 | }, |
| 1275 | }, |
| 1276 | GW1 = GridWarp { |
| 1277 | Inputs = { |
| 1278 | SrcGridChange = Input { |
| 1279 | Value = Mesh { |
| 1280 | Columns = 2, Rows = 2, |
| 1281 | DeltaC = 1, DeltaR = 1, |
| 1282 | Center = { 0, 0 }, |
| 1283 | Corners = { -0.5, -0.5, 0.5, 0.5 }, |
| 1284 | Points = { |
| 1285 | { X = -0.5, Y = -0.5 }, { X = 0.5, Y = -0.5 }, |
| 1286 | { X = -0.5, Y = 0.5 }, { X = 0.5, Y = 0.5 }, |
| 1287 | }, |
| 1288 | }, |
| 1289 | }, |
| 1290 | DstGridChange = Input { |
| 1291 | Value = Mesh { |
| 1292 | Columns = 2, Rows = 2, |
| 1293 | DeltaC = 1, DeltaR = 1, |
| 1294 | Center = { 0, 0 }, |
| 1295 | Corners = { -0.5, -0.5, 0.5, 0.5 }, |
| 1296 | Points = { |
| 1297 | { X = -0.5, Y = -0.5 }, { X = 0.5, Y = -0.5 }, |
| 1298 | { X = -0.5, Y = 0.5 }, { X = 0.5, Y = 0.5 }, |
| 1299 | }, |
| 1300 | }, |
| 1301 | }, |
| 1302 | Input = Input { SourceOp = "BgW", Source = "Output", }, |
| 1303 | }, |
| 1304 | }, |
| 1305 | }, |
| 1306 | }"#; |
| 1307 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1308 | let Ok(mut eng) = Engine::new_headless() else { return; }; |
| 1309 | let (t_ds, _) = eng.evaluate(&comp, comp.find_node("DS1").unwrap(), 0.0); |
| 1310 | let px_ds = eng.readback_rgba(&t_ds).unwrap(); |
| 1311 | // Zenith row (y=0) is sky blue (B > R), horizon (y=63) is brighter haze |
| 1312 | let zen = &px_ds[(0 * 64 + 32) * 4..(0 * 64 + 32) * 4 + 4]; |
| 1313 | let hor = &px_ds[(62 * 64 + 32) * 4..(62 * 64 + 32) * 4 + 4]; |
| 1314 | assert!(zen[2] > zen[0] && zen[3] == 255, "zen={zen:?}"); |
| 1315 | assert!(hor[0] > zen[0], "hor={hor:?} zen={zen:?}"); |
| 1316 | |
| 1317 | let (t_bg, _) = eng.evaluate(&comp, comp.find_node("BgW").unwrap(), 0.0); |
| 1318 | let (t_gw, _) = eng.evaluate(&comp, comp.find_node("GW1").unwrap(), 0.0); |
| 1319 | let px_bg = eng.readback_rgba(&t_bg).unwrap(); |
| 1320 | let px_gw = eng.readback_rgba(&t_gw).unwrap(); |
| 1321 | let mae: f64 = px_bg.iter().zip(&px_gw).map(|(a, b)| (*a as f64 - *b as f64).abs()).sum::<f64>() |
| 1322 | / px_bg.len() as f64; |
| 1323 | assert!(mae < 1.5, "identity GridWarp should match source, mae={mae}"); |
| 1324 | } |
| 1325 | |
| 1326 | fn eval_px(src: &str, node: &str, x: usize, y: usize) -> Option<[u8; 4]> { |
| 1327 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1328 | let mut eng = Engine::new_headless().ok()?; |
| 1329 | let (tex, _) = eng.evaluate(&comp, comp.find_node(node).unwrap(), 0.0); |
| 1330 | let rgba = eng.readback_rgba(&tex).unwrap(); |
| 1331 | let i = (y * 64 + x) * 4; |
| 1332 | Some([rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]]) |
| 1333 | } |
| 1334 | |
| 1335 | #[test] |
| 1336 | fn warps_multiply_by_mask_zeroes_outside_effect_mask() { |
| 1337 | let tools = [ |
| 1338 | ("Dent", "Size = Input { Value = 0.1, },"), |
| 1339 | ("Drip", "Amplitude = Input { Value = 0.01, },"), |
| 1340 | ("Vortex", "Size = Input { Value = 0.1, }, Angle = Input { Value = 30, },"), |
| 1341 | ("Displace", "Type = Input { Value = 1, }, Foreground = Input { SourceOp = \"BG\", Source = \"Output\", },"), |
| 1342 | ("CornerPositioner", ""), |
| 1343 | ("DVE", ""), |
| 1344 | ("PerspectivePositioner", ""), |
| 1345 | ]; |
| 1346 | for (kind, extra) in tools { |
| 1347 | let src = |mbm: u8| { |
| 1348 | format!( |
| 1349 | r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{ |
| 1350 | BG = Background {{ Inputs = {{ Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }}, |
| 1351 | TopLeftRed = Input {{ Value = 1, }}, TopLeftAlpha = Input {{ Value = 1, }}, }}, }}, |
| 1352 | Rect = RectangleMask {{ Inputs = {{ Width = Input {{ Value = 0.5, }}, Height = Input {{ Value = 0.5, }}, |
| 1353 | MaskWidth = Input {{ Value = 64, }}, MaskHeight = Input {{ Value = 64, }}, }}, }}, |
| 1354 | X = {kind} {{ Inputs = {{ MultiplyByMask = Input {{ Value = {mbm}, }}, {extra} |
| 1355 | Input = Input {{ SourceOp = "BG", Source = "Output", }}, |
| 1356 | EffectMask = Input {{ SourceOp = "Rect", Source = "Mask", }}, }}, }}, |
| 1357 | }} }}"# |
| 1358 | ) |
| 1359 | }; |
| 1360 | let (Some(off), Some(on_out), Some(on_in)) = |
| 1361 | (eval_px(&src(0), "X", 2, 2), eval_px(&src(1), "X", 2, 2), eval_px(&src(1), "X", 32, 32)) |
| 1362 | else { |
| 1363 | return; |
| 1364 | }; |
| 1365 | assert_eq!(off[3], 255, "{kind}: effect mask alone only blends"); |
| 1366 | assert_eq!(on_out, [0, 0, 0, 0], "{kind}: MultiplyByMask zeroes outside the mask"); |
| 1367 | assert_eq!(on_in[3], 255, "{kind}: inside the mask is kept"); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | #[test] |
| 1372 | fn dent_multiply_by_mask_scales_by_mask_times_blend() { |
| 1373 | let src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 }, Tools = ordered() { |
| 1374 | BG = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, |
| 1375 | TopLeftRed = Input { Value = 1, }, TopLeftAlpha = Input { Value = 1, }, }, }, |
| 1376 | Rect = RectangleMask { Inputs = { Width = Input { Value = 2, }, Height = Input { Value = 2, }, Level = Input { Value = 0.5, }, |
| 1377 | MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, }, }, }, |
| 1378 | X = Dent { Inputs = { MultiplyByMask = Input { Value = 1, }, Blend = Input { Value = 0.5, }, Size = Input { Value = 0.1, }, |
| 1379 | Input = Input { SourceOp = "BG", Source = "Output", }, |
| 1380 | EffectMask = Input { SourceOp = "Rect", Source = "Mask", }, }, }, |
| 1381 | } }"#; |
| 1382 | let Some(px) = eval_px(src, "X", 2, 2) else { return }; |
| 1383 | // 255 * mask(0.5) * blend(0.5) |
| 1384 | assert!(px[0].abs_diff(64) <= 2 && px[3].abs_diff(64) <= 2, "got {px:?}"); |
| 1385 | } |
| 1386 | |
| 1387 | #[test] |
| 1388 | fn perspective_positioner_blend_fades_outside_corner_bbox_for_computed_sources() { |
| 1389 | let src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 }, Tools = ordered() { |
| 1390 | BG = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, |
| 1391 | TopLeftRed = Input { Value = 1, }, TopLeftAlpha = Input { Value = 1, }, }, }, |
| 1392 | PP = PerspectivePositioner { Inputs = { Blend = Input { Value = 0.5, }, |
| 1393 | TopLeft = Input { Value = { 0.25, 0.75 }, }, TopRight = Input { Value = { 0.75, 0.75 }, }, |
| 1394 | BottomLeft = Input { Value = { 0.25, 0.25 }, }, BottomRight = Input { Value = { 0.75, 0.25 }, }, |
| 1395 | Input = Input { SourceOp = "BG", Source = "Output", }, }, }, |
| 1396 | } }"#; |
| 1397 | let (Some(out), Some(inside)) = (eval_px(src, "PP", 2, 2), eval_px(src, "PP", 32, 32)) else { return }; |
| 1398 | assert!(out[3].abs_diff(128) <= 2, "outside the corner bbox only the 0.5 effect term remains: {out:?}"); |
| 1399 | assert_eq!(inside[3], 255, "inside the bbox orig and effect both contribute: {inside:?}"); |
| 1400 | } |
| 1401 | |
| 1402 | #[test] |
| 1403 | fn dent_process_channels_and_multiply_by_mask() { |
| 1404 | let src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 }, Tools = ordered() { |
| 1405 | BG = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, |
| 1406 | Type = Input { Value = FuID { "Horizontal" }, }, |
| 1407 | TopLeftRed = Input { Value = 1, }, TopRightRed = Input { Value = 0, }, TopLeftAlpha = Input { Value = 1, }, }, }, |
| 1408 | Rect = RectangleMask { Inputs = { Width = Input { Value = 0.5, }, Height = Input { Value = 0.5, }, |
| 1409 | MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, }, }, }, |
| 1410 | D = Dent { Inputs = { |
| 1411 | Input = Input { SourceOp = "BG", Source = "Output", }, |
| 1412 | EffectMask = Input { SourceOp = "Rect", Source = "Mask", }, |
| 1413 | MultiplyByMask = Input { Value = 1, }, |
| 1414 | ProcessRed = Input { Value = 0, }, |
| 1415 | Type = Input { Value = 2, }, |
| 1416 | Size = Input { Value = 0.4, }, |
| 1417 | }, }, |
| 1418 | } }"#; |
| 1419 | let (Some(out), Some(in_pt)) = (eval_px(src, "D", 2, 32), eval_px(src, "D", 34, 32)) else { return }; |
| 1420 | assert_eq!(out, [0, 0, 0, 0], "MultiplyByMask=1 must still zero skipped channels outside mask"); |
| 1421 | let expected_r = ((1.0_f32 - 34.5 / 64.0) * 255.0).round() as u8; |
| 1422 | assert!(in_pt[0].abs_diff(expected_r) <= 3, "ProcessRed=0 should keep unwarped red inside mask: got {:?}", in_pt); |
| 1423 | } |
| 1424 | |
| 1425 | #[test] |
| 1426 | fn day_sky_horizon_luminance_and_un_tonemapped_mode() { |
| 1427 | let src = |hl: f64, tm: u8| format!(r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{ |
| 1428 | DS = DaySky {{ Inputs = {{ |
| 1429 | Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }}, |
| 1430 | ["Horizon.Luminance"] = Input {{ Value = {hl}, }}, |
| 1431 | DoToneMapping = Input {{ Value = {tm}, }}, |
| 1432 | }}, }}, |
| 1433 | }} }}"#); |
| 1434 | let (Some(norm), Some(dark_hl), Some(raw)) = ( |
| 1435 | eval_px(&src(1.0, 1), "DS", 32, 16), |
| 1436 | eval_px(&src(0.3, 1), "DS", 32, 16), |
| 1437 | eval_px(&src(1.0, 0), "DS", 32, 16), |
| 1438 | ) else { |
| 1439 | return; |
| 1440 | }; |
| 1441 | assert_eq!(dark_hl, [0, 0, 0, 255], "low Horizon.Luminance invalidates sky dome to black"); |
| 1442 | assert!(norm[2] > norm[0] && norm[3] == 255, "tonemapped sky should be blue-ish: {norm:?}"); |
| 1443 | assert!(raw[0] >= 250 && raw[1] >= 250 && raw[2] >= 250, "DoToneMapping=0 yields un-tonemapped HDR white: {raw:?}"); |
| 1444 | } |
| 1445 | |
| 1446 | #[test] |
| 1447 | fn plasma_multiply_and_divide_operations() { |
| 1448 | let src = |op: u8| format!(r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{ |
| 1449 | P = Plasma {{ Inputs = {{ |
| 1450 | Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }}, |
| 1451 | Operation = Input {{ Value = {op}, }}, |
| 1452 | }}, }}, |
| 1453 | }} }}"#); |
| 1454 | let (Some(mul_px), Some(div_px)) = (eval_px(&src(2), "P", 32, 32), eval_px(&src(3), "P", 32, 32)) else { |
| 1455 | return; |
| 1456 | }; |
| 1457 | assert_ne!(mul_px, [0, 0, 0, 0]); |
| 1458 | assert!(div_px[3] > 0, "Divide operation should produce non-degenerate output: {div_px:?}"); |
| 1459 | } |
| 1460 | |
| 1461 | #[test] |
| 1462 | fn drip_aspect_less_than_one_scales_y_and_preserves_x() { |
| 1463 | let make_comp = |aspect: f64| format!(r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{ |
| 1464 | BG = Background {{ Inputs = {{ |
| 1465 | Width = Input {{ Value = 128, }}, Height = Input {{ Value = 128, }}, |
| 1466 | Type = Input {{ Value = FuID {{ "Corner" }}, }}, |
| 1467 | TopLeftRed = Input {{ Value = 0.0, }}, TopLeftGreen = Input {{ Value = 1.0, }}, |
| 1468 | TopRightRed = Input {{ Value = 1.0, }}, TopRightGreen = Input {{ Value = 1.0, }}, |
| 1469 | BottomLeftRed = Input {{ Value = 0.0, }}, BottomLeftGreen = Input {{ Value = 0.0, }}, |
| 1470 | BottomRightRed = Input {{ Value = 1.0, }}, BottomRightGreen = Input {{ Value = 0.0, }}, |
| 1471 | }}, }}, |
| 1472 | D = Drip {{ Inputs = {{ |
| 1473 | Input = Input {{ SourceOp = "BG", Source = "Output", }}, |
| 1474 | Aspect = Input {{ Value = {aspect}, }}, |
| 1475 | Amplitude = Input {{ Value = 0.15, }}, |
| 1476 | Frequency = Input {{ Value = 10.0, }}, |
| 1477 | }}, }}, |
| 1478 | }} }}"#); |
| 1479 | let Ok(mut eng) = Engine::new_headless() else { |
| 1480 | return; |
| 1481 | }; |
| 1482 | let c1 = Comp::from_fusion_str(&make_comp(1.0)).unwrap(); |
| 1483 | let c05 = Comp::from_fusion_str(&make_comp(0.5)).unwrap(); |
| 1484 | let (t1, _) = eng.evaluate(&c1, c1.find_node("D").unwrap(), 0.0); |
| 1485 | let (t05, _) = eng.evaluate(&c05, c05.find_node("D").unwrap(), 0.0); |
| 1486 | let p1 = eng.readback_rgba(&t1).unwrap(); |
| 1487 | let p05 = eng.readback_rgba(&t05).unwrap(); |
| 1488 | // Along the horizontal centerline y=64 (raw_rel.y ≈ 0), Aspect < 1 must keep X displacement unchanged. |
| 1489 | let mut x_axis_diff = 0u32; |
| 1490 | for x in 0..128 { |
| 1491 | let i = (64 * 128 + x) * 4; |
| 1492 | x_axis_diff += p1[i].abs_diff(p05[i]) as u32; |
| 1493 | } |
| 1494 | // Along the vertical centerline x=64, Aspect=0.5 halves Y frequency, producing clear differences. |
| 1495 | let mut y_axis_diff = 0u32; |
| 1496 | for y in 0..128 { |
| 1497 | let i = (y * 128 + 64) * 4; |
| 1498 | y_axis_diff += p1[i + 1].abs_diff(p05[i + 1]) as u32; |
| 1499 | } |
| 1500 | assert!(x_axis_diff <= 16, "Aspect < 1 must preserve X-axis ripples, got x_axis_diff={x_axis_diff}"); |
| 1501 | assert!(y_axis_diff > 200, "Aspect=0.5 must stretch Y-axis ripples, got y_axis_diff={y_axis_diff}"); |
| 1502 | } |
| 1503 | |
| 1504 | #[test] |
| 1505 | fn day_sky_matches_fusion_noon_night_and_longitude_sign() { |
| 1506 | let render_256 = |inputs: &str| -> Option<Vec<u8>> { |
| 1507 | let src = format!( |
| 1508 | r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{ |
| 1509 | DS = DaySky {{ Inputs = {{ Width = Input {{ Value = 256, }}, Height = Input {{ Value = 256, }}, {inputs} }}, }}, |
| 1510 | }} }}"# |
| 1511 | ); |
| 1512 | let comp = Comp::from_fusion_str(&src).unwrap(); |
| 1513 | let mut eng = Engine::new_headless().ok()?; |
| 1514 | let (tex, _) = eng.evaluate(&comp, comp.find_node("DS").unwrap(), 0.0); |
| 1515 | eng.readback_rgba(&tex).ok() |
| 1516 | }; |
| 1517 | |
| 1518 | // 1. Default noon sky matches Fusion 21.1 T = 2.0 + 4*turb & unmodified Preetham B_Y. |
| 1519 | if let Some(px) = render_256("") { |
| 1520 | for (x, y, want) in [ |
| 1521 | (128usize, 0usize, [39u8, 69, 141, 255]), |
| 1522 | (128, 128, [40, 80, 161, 255]), |
| 1523 | (128, 220, [99, 146, 222, 255]), |
| 1524 | ] { |
| 1525 | let i = (y * 256 + x) * 4; |
| 1526 | for ch in 0..4 { |
| 1527 | assert!(px[i + ch].abs_diff(want[ch]) <= 2, "noon ({x},{y}) ch{ch}: got {:?} want {want:?}", &px[i..i + 4]); |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | // 2. Sun well below horizon (Time = 22.45137) preserves warm amber G/R without 0.65 chromaticity clipping. |
| 1533 | if let Some(px) = render_256("Time = Input { Value = 22.45137, },") { |
| 1534 | for (x, y, want) in [ |
| 1535 | (128usize, 0usize, [111u8, 76, 0, 255]), |
| 1536 | (128, 128, [142, 97, 0, 255]), |
| 1537 | (192, 192, [208, 139, 0, 255]), |
| 1538 | ] { |
| 1539 | let i = (y * 256 + x) * 4; |
| 1540 | for ch in 0..4 { |
| 1541 | assert!(px[i + ch].abs_diff(want[ch]) <= 5, "night ({x},{y}) ch{ch}: got {:?} want {want:?}", &px[i..i + 4]); |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | // 3. Longitude uses trunc(lon/15)*15 meridian & (SM - Lon) sign: 143.9376 in September sits East of South. |
| 1547 | let (dir, _) = solar_direction(41.38, 143.9376, 9, 1.0, 12.0); |
| 1548 | assert!(dir[1] > 0.0, "expected East-of-South azimuth (sun_dir.y > 0), got {dir:?}"); |
| 1549 | if let Some(px) = render_256(r#"Longitude = Input { Value = 143.9376, }, Month = Input { Value = FuID { "September" }, },"#) { |
| 1550 | let i_east = (94 * 256 + 11) * 4; |
| 1551 | let i_west = (94 * 256 + 245) * 4; |
| 1552 | assert!(px[i_east] > px[i_west], "East peak at x=11 should be brighter than x=245: {:?} vs {:?}", &px[i_east..i_east + 3], &px[i_west..i_west + 3]); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | #[test] |
| 1557 | fn vortex_and_kscope_early_outs_preserve_mask_and_channels() { |
| 1558 | let vortex_src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 }, Tools = ordered() { |
| 1559 | BG = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, |
| 1560 | Type = Input { Value = FuID { "Horizontal" }, }, |
| 1561 | TopLeftRed = Input { Value = 0.2, }, TopRightRed = Input { Value = 0.9, }, TopLeftAlpha = Input { Value = 1, }, }, }, |
| 1562 | Rect = RectangleMask { Inputs = { Width = Input { Value = 0.5, }, Height = Input { Value = 0.5, }, |
| 1563 | MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, }, }, }, |
| 1564 | V0 = Vortex { Inputs = { Angle = Input { Value = 0, }, MultiplyByMask = Input { Value = 1, }, |
| 1565 | ProcessRed = Input { Value = 0, }, Blend = Input { Value = 0.5, }, |
| 1566 | Input = Input { SourceOp = "BG", Source = "Output", }, EffectMask = Input { SourceOp = "Rect", Source = "Mask", }, }, }, |
| 1567 | } }"#; |
| 1568 | if let (Some(out), Some(in_pt)) = (eval_px(vortex_src, "V0", 2, 2), eval_px(vortex_src, "V0", 32, 32)) { |
| 1569 | assert_eq!(out, [0, 0, 0, 0], "Vortex Angle=0 with MultiplyByMask must zero outside mask"); |
| 1570 | assert!(in_pt[3].abs_diff(128) <= 2, "Vortex Angle=0 inside mask scales processed alpha by blend: {in_pt:?}"); |
| 1571 | } |
| 1572 | |
| 1573 | let kscope_src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 }, Tools = ordered() { |
| 1574 | BG = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, |
| 1575 | TopLeftRed = Input { Value = 0.4, }, TopLeftGreen = Input { Value = 0.7, }, TopLeftAlpha = Input { Value = 1, }, }, }, |
| 1576 | K0 = Fuse.KScope { Inputs = { Blend = Input { Value = 0, }, Input = Input { SourceOp = "BG", Source = "Output", }, }, }, |
| 1577 | K1 = Fuse.KScope { Inputs = { Blend = Input { Value = 1, }, Slices = Input { Value = 6, }, Input = Input { SourceOp = "BG", Source = "Output", }, }, }, |
| 1578 | } }"#; |
| 1579 | if let (Some(k0), Some(k1)) = (eval_px(kscope_src, "K0", 16, 16), eval_px(kscope_src, "K1", 16, 16)) { |
| 1580 | assert_eq!(k0, k1, "solid input with Blend=0 and Blend=1 should match identically"); |
| 1581 | } |
| 1582 | } |
| 1583 | } |