authoravatarclover caruso<git@paperclover.net>2026-11-30 21:08:53-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-30 21:16:22-08:00
log22b81476d7a6aae5efca63a734822ad5d21da5b8
treeafa9882f16363b583c4f52f722029616429ec9d1
parentb3672ee106de333dc201bbe7e1d8652394308ab5
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Fix particle cycle overflow, Loader timing overrides, DoD masks, tex_scale, and atomic export


9 files changed, 626 insertions(+), 55 deletions(-)

crates/engine/src/av_decode.m+3-1
...@@ -364,8 +364,10 @@ int av_video_decode_frame(...@@ -364,8 +364,10 @@ int av_video_decode_frame(
364 }364 }
365365
366 if (!chosen) {366 if (!chosen) {
367 BOOL atEof = (obj.reader != nil && obj.reader.status == AVAssetReaderStatusCompleted) ||
368 (obj.nbFrames > 0 && frame_idx >= obj.nbFrames - 2);
367 [obj closeReader];369 [obj closeReader];
368 if (obj.hasLastFrame && frame_idx > 0 && obj.lastFrameRgba.length >= required) {370 if (atEof && obj.hasLastFrame && frame_idx > 0 && obj.lastFrameRgba.length >= required) {
369 memcpy(out_rgba, obj.lastFrameRgba.bytes, required);371 memcpy(out_rgba, obj.lastFrameRgba.bytes, required);
370 return 1;372 return 1;
371 }373 }
crates/engine/src/eval/blur_glow.rs+124-12
...@@ -81,7 +81,8 @@ impl Engine {...@@ -81,7 +81,8 @@ impl Engine {
81 (src.clone(), src.origin)81 (src.clone(), src.origin)
82 };82 };
8383
84 let dst = self.alloc_tex_dod(work_src.size, out_origin, src.canvas_size, src.depth, key);84 let mut dst = self.alloc_tex_dod(work_src.size, out_origin, src.canvas_size, src.depth, key);
85 dst.tex_scale *= src.tex_scale;
85 // Fast mode (`!self.hiq`): downsample large blurs to 1/2 or 1/4 pyramid level for 4-16x speedup!86 // Fast mode (`!self.hiq`): downsample large blurs to 1/2 or 1/4 pyramid level for 4-16x speedup!
86 let down = if self.hiq {87 let down = if self.hiq {
87 1u3288 1u32
...@@ -392,7 +393,8 @@ impl Engine {...@@ -392,7 +393,8 @@ impl Engine {
392 };393 };
393394
394 let proc_a_f = if proc_alpha { 1.0 } else { 0.0 };395 let proc_a_f = if proc_alpha { 1.0 } else { 0.0 };
395 let dst = self.alloc_tex_dod(work_src.size, out_origin, src.canvas_size, src.depth, key);396 let mut dst = self.alloc_tex_dod(work_src.size, out_origin, src.canvas_size, src.depth, key);
397 dst.tex_scale *= src.tex_scale;
396 if let Some(bgm) = &blurred_gm {398 if let Some(bgm) = &blurred_gm {
397 let need_final_mix = mask.is_some();399 let need_final_mix = mask.is_some();
398 let glow_dst = if need_final_mix {400 let glow_dst = if need_final_mix {
...@@ -570,10 +572,8 @@ impl Engine {...@@ -570,10 +572,8 @@ impl Engine {
570 let uv_scale = glow_sat * var;572 let uv_scale = glow_sat * var;
571573
572 let is_last = i == iters && mask.is_none() && (blend - 1.0).abs() <= 1e-4;574 let is_last = i == iters && mask.is_none() && (blend - 1.0).abs() <= 1e-4;
573 let next = self.alloc_tex_dod(575 let next = self.alloc_tex_like(
574 src.size,576 &src,
575 src.origin,
576 src.canvas_size,
577 src.depth,577 src.depth,
578 if is_last { key } else { 0 },578 if is_last { key } else { 0 },
579 );579 );
...@@ -587,7 +587,7 @@ impl Engine {...@@ -587,7 +587,7 @@ impl Engine {
587587
588 let glowed = running.unwrap_or_else(|| src.clone());588 let glowed = running.unwrap_or_else(|| src.clone());
589 let dst = if mask.is_some() || (blend - 1.0).abs() > 1e-4 {589 let dst = if mask.is_some() || (blend - 1.0).abs() > 1e-4 {
590 let final_dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);590 let final_dst = self.alloc_tex_like(&src, src.depth, key);
591 let mut ug = UniformParams::default();591 let mut ug = UniformParams::default();
592 ug.p[0] = [1.0, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];592 ug.p[0] = [1.0, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];
593 ug.p[1] = [4.0, 0.0, 0.0, 0.0]; // mode 4 = MixOnly593 ug.p[1] = [4.0, 0.0, 0.0, 0.0]; // mode 4 = MixOnly
...@@ -890,7 +890,8 @@ impl Engine {...@@ -890,7 +890,8 @@ impl Engine {
890 wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(256 * 4), rows_per_image: Some(1) },890 wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(256 * 4), rows_per_image: Some(1) },
891 wgpu::Extent3d { width: 256, height: 1, depth_or_array_layers: 1 },891 wgpu::Extent3d { width: 256, height: 1, depth_or_array_layers: 1 },
892 );892 );
893 let dst = self.alloc_tex(src.canvas_size, src.depth, key);893 let mut dst = self.alloc_tex(src.canvas_size, src.depth, key);
894 dst.tex_scale *= src.tex_scale;
894 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);895 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
895 self.dispatch_op(enc, ShaderOp::XfChroma, &src.view, &grad_tex.view, mv, &dst, &u);896 self.dispatch_op(enc, ShaderOp::XfChroma, &src.view, &grad_tex.view, mv, &dst, &u);
896 self.cache.insert(key, dst.clone(), node.id, frame_i);897 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -937,7 +938,7 @@ impl Engine {...@@ -937,7 +938,7 @@ impl Engine {
937 }938 }
938 stats.cache_misses += 1;939 stats.cache_misses += 1;
939 stats.nodes_evaluated += 1;940 stats.nodes_evaluated += 1;
940 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);941 let dst = self.alloc_tex_like(&src, src.depth, key);
941 let bv = bmap.as_ref().map_or(&self.dummy.view, |b| &b.view);942 let bv = bmap.as_ref().map_or(&self.dummy.view, |b| &b.view);
942 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);943 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
943 self.dispatch_op(enc, ShaderOp::VariBlur, &src.view, bv, mv, &dst, &u);944 self.dispatch_op(enc, ShaderOp::VariBlur, &src.view, bv, mv, &dst, &u);
...@@ -979,7 +980,7 @@ impl Engine {...@@ -979,7 +980,7 @@ impl Engine {
979 }980 }
980 stats.cache_misses += 1;981 stats.cache_misses += 1;
981 stats.nodes_evaluated += 1;982 stats.nodes_evaluated += 1;
982 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);983 let dst = self.alloc_tex_like(&src, src.depth, key);
983 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);984 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
984 self.dispatch_op(enc, ShaderOp::Filter, &src.view, &self.dummy.view, mv, &dst, &u);985 self.dispatch_op(enc, ShaderOp::Filter, &src.view, &self.dummy.view, mv, &dst, &u);
985 self.cache.insert(key, dst.clone(), node.id, frame_i);986 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -1004,13 +1005,19 @@ impl Engine {...@@ -1004,13 +1005,19 @@ impl Engine {
1004 let lock_xy = comp.eval_number(node, "LockAmountXY", time, 1.0, &self.expr) > 0.5;1005 let lock_xy = comp.eval_number(node, "LockAmountXY", time, 1.0, &self.expr) > 0.5;
1005 let x_amt = comp.eval_number(node, "XAmount", time, 1.0, &self.expr) as f32;1006 let x_amt = comp.eval_number(node, "XAmount", time, 1.0, &self.expr) as f32;
1006 let y_amt = if lock_xy { x_amt } else { comp.eval_number(node, "YAmount", time, x_amt as f64, &self.expr) as f32 };1007 let y_amt = if lock_xy { x_amt } else { comp.eval_number(node, "YAmount", time, x_amt as f64, &self.expr) as f32 };
1007 let scale_ref = src.size[0] as f32 / 720.0 * 1.14;1008 let scale_ref = src.canvas_size[0] as f32 / 720.0 * 1.14;
1008 let rx = (x_amt * scale_ref).max(0.0);1009 let rx = (x_amt * scale_ref).max(0.0);
1009 let ry = (y_amt * scale_ref).max(0.0);1010 let ry = (y_amt * scale_ref).max(0.0);
1010 if blend <= 1e-6 || rx.max(ry) < 0.05 {1011 if blend <= 1e-6 || rx.max(ry) < 0.05 {
1011 return src;1012 return src;
1012 }1013 }
1014 let src = if node.connected_node("EffectMask").is_some() {
1015 self.crop_to_canvas(enc, src)
1016 } else {
1017 src
1018 };
1013 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);1019 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);
1020 let mask = mask.map(|m| self.crop_to_canvas(enc, m));
1014 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);1021 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
1015 let key = self.make_key(node, "Sharpen", src.size, &[src.content_hash, mask_hash], &[rx, ry, blend, if mask_inv { 1.0 } else { 0.0 }]);1022 let key = self.make_key(node, "Sharpen", src.size, &[src.content_hash, mask_hash], &[rx, ry, blend, if mask_inv { 1.0 } else { 0.0 }]);
1016 if let Some(hit) = self.cache.get(key, node.id, frame_i) {1023 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
...@@ -1031,7 +1038,7 @@ impl Engine {...@@ -1031,7 +1038,7 @@ impl Engine {
1031 uv.p[1] = [1.0, 0.0, 0.0, 0.0];1038 uv.p[1] = [1.0, 0.0, 0.0, 0.0];
1032 self.dispatch_op(enc, ShaderOp::Blur, &tmp1.view, &self.dummy.view, &self.dummy.view, &tmp2, &uv);1039 self.dispatch_op(enc, ShaderOp::Blur, &tmp1.view, &self.dummy.view, &self.dummy.view, &tmp2, &uv);
10331040
1034 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);1041 let dst = self.alloc_tex_like(&src, src.depth, key);
1035 let mut ug = UniformParams::default();1042 let mut ug = UniformParams::default();
1036 ug.p[0] = [3.0, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];1043 ug.p[0] = [3.0, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];
1037 ug.p[1] = [3.0, 0.0, 0.0, 0.0]; // mode 3 = Sharpen (orig + 3 * (orig - blurred))1044 ug.p[1] = [3.0, 0.0, 0.0, 0.0]; // mode 3 = Sharpen (orig + 3 * (orig - blurred))
...@@ -1523,4 +1530,109 @@ mod tests {...@@ -1523,4 +1530,109 @@ mod tests {
1523 assert_eq!(&ed_px[i..i + 4], &xf_px[i..i + 4], "ErodeDilate mask misaligned at x={x}");1530 assert_eq!(&ed_px[i..i + 4], &xf_px[i..i + 4], "ErodeDilate mask misaligned at x={x}");
1524 }1531 }
1525 }1532 }
1533
1534 #[test]
1535 fn sharpen_dod_effect_mask_and_scale_ref_align_to_canvas() {
1536 let src = r#"Composition {
1537 CurrentTime = 0,
1538 RenderRange = { 0, 0 },
1539 GlobalRange = { 0, 0 },
1540 Tools = ordered() {
1541 Stripe = Background {
1542 Inputs = {
1543 Width = Input { Value = 64, },
1544 Height = Input { Value = 64, },
1545 Type = Input { Value = FuID { "Horizontal" }, },
1546 TopLeftRed = Input { Value = 0.2, },
1547 TopLeftGreen = Input { Value = 0.2, },
1548 TopLeftBlue = Input { Value = 0.2, },
1549 TopLeftAlpha = Input { Value = 1.0, },
1550 TopRightRed = Input { Value = 0.8, },
1551 TopRightGreen = Input { Value = 0.8, },
1552 TopRightBlue = Input { Value = 0.8, },
1553 TopRightAlpha = Input { Value = 1.0, },
1554 },
1555 },
1556 XF = DVE {
1557 Inputs = {
1558 Input = Input { SourceOp = "Stripe", Source = "Output", },
1559 Center = Input { Value = { 0.25, 0.5 }, },
1560 },
1561 },
1562 Mask = RectangleMask {
1563 Inputs = {
1564 MaskWidth = Input { Value = 64, },
1565 MaskHeight = Input { Value = 64, },
1566 Center = Input { Value = { 0.875, 0.5 }, },
1567 Width = Input { Value = 0.25, },
1568 Height = Input { Value = 1.0, },
1569 },
1570 },
1571 ShMasked = Sharpen {
1572 Inputs = {
1573 Input = Input { SourceOp = "XF", Source = "Output", },
1574 EffectMask = Input { SourceOp = "Mask", Source = "Mask", },
1575 XAmount = Input { Value = 40.0, },
1576 },
1577 },
1578 WideDoD = SetDomain {
1579 Inputs = {
1580 Input = Input { SourceOp = "Stripe", Source = "Output", },
1581 Mode = Input { Value = FuID { "Set" }, },
1582 Left = Input { Value = -1.0, },
1583 Right = Input { Value = 2.0, },
1584 Bottom = Input { Value = -1.0, },
1585 Top = Input { Value = 2.0, },
1586 },
1587 },
1588 ShPlain = Sharpen {
1589 Inputs = {
1590 Input = Input { SourceOp = "Stripe", Source = "Output", },
1591 XAmount = Input { Value = 40.0, },
1592 },
1593 },
1594 ShDoD = Sharpen {
1595 Inputs = {
1596 Input = Input { SourceOp = "WideDoD", Source = "Output", },
1597 XAmount = Input { Value = 40.0, },
1598 },
1599 },
1600 },
1601 }"#;
1602 let comp = Comp::from_fusion_str(src).expect("parse sharpen DoD comp");
1603 let Ok(mut eng) = Engine::new_headless() else {
1604 return;
1605 };
1606 let (xf_tex, _) = eng.evaluate(&comp, comp.find_node("XF").unwrap(), 0.0);
1607 let xf_px = eng.readback_rgba(&xf_tex).unwrap();
1608 let (shm_tex, _) = eng.evaluate(&comp, comp.find_node("ShMasked").unwrap(), 0.0);
1609 assert_eq!(shm_tex.size, [64, 64]);
1610 let shm_px = eng.readback_rgba(&shm_tex).unwrap();
1611 // Mask covers [48..64), so x in [0..46) must match XF exactly.
1612 for x in 0..46 {
1613 let i = (32 * 64 + x) * 4;
1614 assert_eq!(&shm_px[i..i + 4], &xf_px[i..i + 4], "Sharpen mask misaligned at x={x}");
1615 }
1616
1617 // Unmasked Sharpen with expanded DoD must compute scale_ref from canvas_size[0], matching ShPlain on canvas.
1618 let (plain_tex, _) = eng.evaluate(&comp, comp.find_node("ShPlain").unwrap(), 0.0);
1619 let (dod_tex, _) = eng.evaluate(&comp, comp.find_node("ShDoD").unwrap(), 0.0);
1620 let plain_px = eng.readback_rgba(&plain_tex).unwrap();
1621 let dod_px = eng.readback_rgba(&dod_tex).unwrap();
1622 let dod_w = dod_tex.size[0] as i32;
1623 for x in 8..56 {
1624 let pi = (32 * 64 + x) * 4;
1625 let dx = (x as i32 - dod_tex.origin[0]) as usize;
1626 let dy = (32 - dod_tex.origin[1]) as usize;
1627 let di = (dy * dod_w as usize + dx) * 4;
1628 for ch in 0..4 {
1629 assert!(
1630 (plain_px[pi + ch] as i32 - dod_px[di + ch] as i32).abs() <= 2,
1631 "Sharpen scale_ref shifted with DoD at x={x} ch={ch}: plain={} dod={}",
1632 plain_px[pi + ch],
1633 dod_px[di + ch]
1634 );
1635 }
1636 }
1637 }
1526}1638}
crates/engine/src/eval/color.rs+70-8
...@@ -236,7 +236,7 @@ impl Engine {...@@ -236,7 +236,7 @@ impl Engine {
236 }236 }
237 stats.cache_misses += 1;237 stats.cache_misses += 1;
238 stats.nodes_evaluated += 1;238 stats.nodes_evaluated += 1;
239 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);239 let dst = self.alloc_tex_like(&src, src.depth, key);
240 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);240 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
241 self.dispatch_op(enc, ShaderOp::Colorize, &src.view, &self.dummy.view, mv, &dst, &u);241 self.dispatch_op(enc, ShaderOp::Colorize, &src.view, &self.dummy.view, mv, &dst, &u);
242 self.cache.insert(key, dst.clone(), node.id, frame_i);242 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -408,7 +408,10 @@ impl Engine {...@@ -408,7 +408,10 @@ impl Engine {
408 stats.cache_misses += 1;408 stats.cache_misses += 1;
409 stats.nodes_evaluated += 1;409 stats.nodes_evaluated += 1;
410410
411 let dst = self.alloc_tex(size, eff_depth, key);411 let mut dst = self.alloc_tex(size, eff_depth, key);
412 if let Some(t) = &img1 {
413 dst.tex_scale *= t.tex_scale;
414 }
412 let v1 = img1.as_ref().map_or(&self.dummy.view, |t| &t.view);415 let v1 = img1.as_ref().map_or(&self.dummy.view, |t| &t.view);
413 let v2 = img2.as_ref().map_or(&self.dummy.view, |t| &t.view);416 let v2 = img2.as_ref().map_or(&self.dummy.view, |t| &t.view);
414 let v3 = img3.as_ref().map_or(&self.dummy.view, |t| &t.view);417 let v3 = img3.as_ref().map_or(&self.dummy.view, |t| &t.view);
...@@ -477,7 +480,7 @@ impl Engine {...@@ -477,7 +480,7 @@ impl Engine {
477 }480 }
478 stats.cache_misses += 1;481 stats.cache_misses += 1;
479 stats.nodes_evaluated += 1;482 stats.nodes_evaluated += 1;
480 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);483 let dst = self.alloc_tex_like(&src, src.depth, key);
481 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);484 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
482 self.dispatch_op(enc, ShaderOp::Invert, &src.view, &self.dummy.view, mv, &dst, &u);485 self.dispatch_op(enc, ShaderOp::Invert, &src.view, &self.dummy.view, mv, &dst, &u);
483 self.cache.insert(key, dst.clone(), node.id, frame_i);486 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -540,7 +543,7 @@ impl Engine {...@@ -540,7 +543,7 @@ impl Engine {
540 }543 }
541 stats.cache_misses += 1;544 stats.cache_misses += 1;
542 stats.nodes_evaluated += 1;545 stats.nodes_evaluated += 1;
543 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);546 let dst = self.alloc_tex_like(&src, src.depth, key);
544 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);547 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
545 self.dispatch_op(enc, ShaderOp::ColorGain, &src.view, &self.dummy.view, mv, &dst, &u);548 self.dispatch_op(enc, ShaderOp::ColorGain, &src.view, &self.dummy.view, mv, &dst, &u);
546 self.cache.insert(key, dst.clone(), node.id, frame_i);549 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -636,7 +639,7 @@ impl Engine {...@@ -636,7 +639,7 @@ impl Engine {
636 stats.nodes_evaluated += 1;639 stats.nodes_evaluated += 1;
637640
638 let lut_tex = self.upload_lut_f16(&lut_f16);641 let lut_tex = self.upload_lut_f16(&lut_f16);
639 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);642 let dst = self.alloc_tex_like(&src, src.depth, key);
640 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);643 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
641 self.dispatch_op(enc, ShaderOp::ColorCurves, &src.view, &lut_tex.view, mv, &dst, &u);644 self.dispatch_op(enc, ShaderOp::ColorCurves, &src.view, &lut_tex.view, mv, &dst, &u);
642 self.cache.insert(key, dst.clone(), node.id, frame_i);645 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -691,7 +694,7 @@ impl Engine {...@@ -691,7 +694,7 @@ impl Engine {
691 }694 }
692 stats.cache_misses += 1;695 stats.cache_misses += 1;
693 stats.nodes_evaluated += 1;696 stats.nodes_evaluated += 1;
694 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);697 let dst = self.alloc_tex_like(&src, src.depth, key);
695 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);698 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
696 self.dispatch_op(enc, ShaderOp::GamutConvert, &src.view, &self.dummy.view, mv, &dst, &u);699 self.dispatch_op(enc, ShaderOp::GamutConvert, &src.view, &self.dummy.view, mv, &dst, &u);
697 self.cache.insert(key, dst.clone(), node.id, frame_i);700 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -774,7 +777,7 @@ impl Engine {...@@ -774,7 +777,7 @@ impl Engine {
774 wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(256 * 4), rows_per_image: Some(3) },777 wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(256 * 4), rows_per_image: Some(3) },
775 wgpu::Extent3d { width: 256, height: 3, depth_or_array_layers: 1 },778 wgpu::Extent3d { width: 256, height: 3, depth_or_array_layers: 1 },
776 );779 );
777 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);780 let dst = self.alloc_tex_like(&src, src.depth, key);
778 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);781 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
779 self.dispatch_op(enc, ShaderOp::HueCurves, &src.view, &lut_tex.view, mv, &dst, &u);782 self.dispatch_op(enc, ShaderOp::HueCurves, &src.view, &lut_tex.view, mv, &dst, &u);
780 self.cache.insert(key, dst.clone(), node.id, frame_i);783 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -832,7 +835,7 @@ impl Engine {...@@ -832,7 +835,7 @@ impl Engine {
832 }835 }
833 stats.cache_misses += 1;836 stats.cache_misses += 1;
834 stats.nodes_evaluated += 1;837 stats.nodes_evaluated += 1;
835 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);838 let dst = self.alloc_tex_like(&src, src.depth, key);
836 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);839 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
837 self.dispatch_op(enc, ShaderOp::FilmGrain, &src.view, &self.dummy.view, mv, &dst, &u);840 self.dispatch_op(enc, ShaderOp::FilmGrain, &src.view, &self.dummy.view, mv, &dst, &u);
838 self.cache.insert(key, dst.clone(), node.id, frame_i);841 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -1306,5 +1309,64 @@ mod tests {...@@ -1306,5 +1309,64 @@ mod tests {
1306 assert!((p[0] as i32 - 128).abs() <= 1, "FilmGrain leaked outside mask at x={x}: {p:?}");1309 assert!((p[0] as i32 - 128).abs() <= 1, "FilmGrain leaked outside mask at x={x}: {p:?}");
1307 }1310 }
1308 }1311 }
1312
1313 #[test]
1314 fn tex_scale_preserved_through_color_gain_transform_blur() {
1315 let comp_src = r#"{
1316 Tools = ordered() {
1317 FN = FastNoise {
1318 Inputs = {
1319 Width = Input { Value = 8192, },
1320 Height = Input { Value = 128, },
1321 },
1322 },
1323 CG = ColorGain {
1324 Inputs = {
1325 Input = Input { SourceOp = "FN", Source = "Output", },
1326 GainRed = Input { Value = 1.5, },
1327 },
1328 },
1329 XF = Transform {
1330 Inputs = {
1331 Input = Input { SourceOp = "CG", Source = "Output", },
1332 Center = Input { Value = { 0.52, 0.5 }, },
1333 },
1334 },
1335 BL = Blur {
1336 Inputs = {
1337 Input = Input { SourceOp = "XF", Source = "Output", },
1338 XBlurSize = Input { Value = 2.0, },
1339 },
1340 },
1341 BG = Background {
1342 Inputs = {
1343 Width = Input { Value = 128, },
1344 Height = Input { Value = 128, },
1345 },
1346 },
1347 M = Merge {
1348 Inputs = {
1349 Background = Input { SourceOp = "BG", Source = "Output", },
1350 Foreground = Input { SourceOp = "BL", Source = "Output", },
1351 },
1352 },
1353 }
1354 }"#;
1355 let comp = Comp::from_fusion_str(comp_src).expect("parse tex_scale comp");
1356 let Ok(mut eng) = Engine::new_headless() else {
1357 return;
1358 };
1359 eng.hiq = false; // caps FastNoise at 4096 -> tex_scale = 0.5
1360 let (fn_tex, _) = eng.evaluate(&comp, comp.find_node("FN").unwrap(), 0.0);
1361 assert!((fn_tex.tex_scale - 0.5).abs() < 1e-4, "expected FastNoise tex_scale=0.5, got {}", fn_tex.tex_scale);
1362 let (cg_tex, _) = eng.evaluate(&comp, comp.find_node("CG").unwrap(), 0.0);
1363 assert!((cg_tex.tex_scale - fn_tex.tex_scale).abs() < 1e-4, "ColorGain dropped tex_scale: {}", cg_tex.tex_scale);
1364 let (xf_tex, _) = eng.evaluate(&comp, comp.find_node("XF").unwrap(), 0.0);
1365 assert!((xf_tex.tex_scale - fn_tex.tex_scale).abs() < 1e-4, "Transform dropped tex_scale: {}", xf_tex.tex_scale);
1366 let (bl_tex, _) = eng.evaluate(&comp, comp.find_node("BL").unwrap(), 0.0);
1367 assert!((bl_tex.tex_scale - fn_tex.tex_scale).abs() < 1e-4, "Blur dropped tex_scale: {}", bl_tex.tex_scale);
1368 let (m_tex, _) = eng.evaluate(&comp, comp.find_node("M").unwrap(), 0.0);
1369 assert!((m_tex.tex_scale - 1.0).abs() < 1e-4, "Merge over 128x128 BG should stay 1.0");
1370 }
1309}1371}
13101372
crates/engine/src/eval/composite.rs+1-1
...@@ -400,7 +400,7 @@ impl Engine {...@@ -400,7 +400,7 @@ impl Engine {
400 };400 };
401401
402 // 3. Composite original OVER shadow (or Shadow Only)402 // 3. Composite original OVER shadow (or Shadow Only)
403 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);403 let dst = self.alloc_tex_like(&src, src.depth, key);
404 let mut us = UniformParams::default();404 let mut us = UniformParams::default();
405 us.p[0] = [sh_r, sh_g, sh_b, sh_a];405 us.p[0] = [sh_r, sh_g, sh_b, sh_a];
406 us.p[1] = [shadow_only, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];406 us.p[1] = [shadow_only, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];
crates/engine/src/eval/io.rs+151-13
...@@ -49,6 +49,43 @@ impl Engine {...@@ -49,6 +49,43 @@ impl Engine {
49 dims49 dims
50 }50 }
5151
52 fn apply_loader_timing_overrides(
53 comp: &Comp,
54 node: &Node,
55 clip: &mut model::ClipSpec,
56 time: f64,
57 expr: &ExprEngine,
58 ) {
59 if node.inputs.contains_key("GlobalIn") {
60 clip.global_start = comp
61 .eval_number(node, "GlobalIn", time, clip.global_start as f64, expr)
62 .round() as i64;
63 }
64 if node.inputs.contains_key("GlobalOut") {
65 clip.global_end = comp
66 .eval_number(node, "GlobalOut", time, clip.global_end as f64, expr)
67 .round() as i64;
68 }
69 if node.inputs.contains_key("Loop") {
70 clip.loop_clip = comp.eval_number(
71 node,
72 "Loop",
73 time,
74 if clip.loop_clip { 1.0 } else { 0.0 },
75 expr,
76 ) > 0.5;
77 }
78 if node.inputs.contains_key("Reverse") {
79 clip.reverse = comp.eval_number(
80 node,
81 "Reverse",
82 time,
83 if clip.reverse { 1.0 } else { 0.0 },
84 expr,
85 ) > 0.5;
86 }
87 }
88
52 pub(super) fn eval_loader(89 pub(super) fn eval_loader(
53 &mut self,90 &mut self,
54 comp: &Comp,91 comp: &Comp,
...@@ -69,29 +106,27 @@ impl Engine {...@@ -69,29 +106,27 @@ impl Engine {
69 node.static_str("Clip1.OpenEXRFormat.BlueName"),106 node.static_str("Clip1.OpenEXRFormat.BlueName"),
70 node.static_str("Clip1.OpenEXRFormat.AlphaName"),107 node.static_str("Clip1.OpenEXRFormat.AlphaName"),
71 ];108 ];
109 // Apply Inspector timing overrides (`GlobalIn`, `GlobalOut`, `Loop`, `Reverse`) on a working copy
110 // before resolving proxy/sequence paths so live Inspector edits take effect immediately.
111 let mut effective_clip = clip.clone();
112 Self::apply_loader_timing_overrides(comp, node, &mut effective_clip, time, &self.expr);
72 // When `use_proxy_clips` is enabled, prefer Loader `ProxyFilename` or `Clip.AlternateFilename`113 // When `use_proxy_clips` is enabled, prefer Loader `ProxyFilename` or `Clip.AlternateFilename`
73 // if the resolved proxy file exists on disk, falling back seamlessly to the primary clip.114 // if the resolved proxy file exists on disk, falling back seamlessly to the primary clip.
74 let effective_clip: std::borrow::Cow<'_, model::ClipSpec> = if self.use_proxy_clips {115 if self.use_proxy_clips {
75 let proxy_candidate = node116 let proxy_candidate = node
76 .static_str("ProxyFilename")117 .static_str("ProxyFilename")
77 .filter(|s| !s.trim().is_empty())118 .filter(|s| !s.trim().is_empty())
78 .or_else(|| clip.alternate_filename.as_deref().filter(|s| !s.trim().is_empty()));119 .or_else(|| clip.alternate_filename.as_deref().filter(|s| !s.trim().is_empty()));
79 if let Some(proxy_name) = proxy_candidate {120 if let Some(proxy_name) = proxy_candidate {
80 let mut proxy_clip = clip.clone();121 let mut proxy_clip = effective_clip.clone();
81 proxy_clip.filename = proxy_name.to_owned();122 proxy_clip.filename = proxy_name.to_owned();
82 if crate::media::resolve_clip_path(&proxy_clip, frame_i)123 if crate::media::resolve_clip_path(&proxy_clip, frame_i)
83 .is_some_and(|p| p.exists())124 .is_some_and(|p| p.exists())
84 {125 {
85 std::borrow::Cow::Owned(proxy_clip)126 effective_clip = proxy_clip;
86 } else {
87 std::borrow::Cow::Borrowed(clip)
88 }127 }
89 } else {
90 std::borrow::Cow::Borrowed(clip)
91 }128 }
92 } else {129 }
93 std::borrow::Cow::Borrowed(clip)
94 };
95 let missing_frames = comp130 let missing_frames = comp
96 .eval_number(node, "MissingFrames", time, 0.0, &self.expr)131 .eval_number(node, "MissingFrames", time, 0.0, &self.expr)
97 .round() as i32;132 .round() as i32;
...@@ -122,6 +157,10 @@ impl Engine {...@@ -122,6 +157,10 @@ impl Engine {
122 self.cache.node_salt(node.id).hash(&mut kh);157 self.cache.node_salt(node.id).hash(&mut kh);
123 "LoaderMissing".hash(&mut kh);158 "LoaderMissing".hash(&mut kh);
124 effective_clip.filename.hash(&mut kh);159 effective_clip.filename.hash(&mut kh);
160 effective_clip.global_start.hash(&mut kh);
161 effective_clip.global_end.hash(&mut kh);
162 effective_clip.loop_clip.hash(&mut kh);
163 effective_clip.reverse.hash(&mut kh);
125 self.proxy_scale.hash(&mut kh);164 self.proxy_scale.hash(&mut kh);
126 comp.width.hash(&mut kh);165 comp.width.hash(&mut kh);
127 comp.height.hash(&mut kh);166 comp.height.hash(&mut kh);
...@@ -171,6 +210,10 @@ impl Engine {...@@ -171,6 +210,10 @@ impl Engine {
171 "Loader".hash(&mut h);210 "Loader".hash(&mut h);
172 resolved.hash(&mut h);211 resolved.hash(&mut h);
173 local_idx.hash(&mut h);212 local_idx.hash(&mut h);
213 effective_clip.global_start.hash(&mut h);
214 effective_clip.global_end.hash(&mut h);
215 effective_clip.loop_clip.hash(&mut h);
216 effective_clip.reverse.hash(&mut h);
174 post_mult.hash(&mut h);217 post_mult.hash(&mut h);
175 exr_channels.hash(&mut h);218 exr_channels.hash(&mut h);
176 target_size.hash(&mut h);219 target_size.hash(&mut h);
...@@ -282,7 +325,9 @@ impl Engine {...@@ -282,7 +325,9 @@ impl Engine {
282 match n.kind.as_str() {325 match n.kind.as_str() {
283 "Loader" => {326 "Loader" => {
284 let clip = n.clips.first()?;327 let clip = n.clips.first()?;
285 let resolved = crate::media::resolve_clip_path(clip, cur_time.round() as i64)?;328 let mut effective_clip = clip.clone();
329 Self::apply_loader_timing_overrides(comp, n, &mut effective_clip, cur_time, &self.expr);
330 let resolved = crate::media::resolve_clip_path(&effective_clip, cur_time.round() as i64)?;
286 if resolved331 if resolved
287 .extension()332 .extension()
288 .and_then(|e| e.to_str())333 .and_then(|e| e.to_str())
...@@ -921,7 +966,8 @@ impl Engine {...@@ -921,7 +966,8 @@ impl Engine {
921 }966 }
922 stats.cache_misses += 1;967 stats.cache_misses += 1;
923 stats.nodes_evaluated += 1;968 stats.nodes_evaluated += 1;
924 let dst = self.alloc_tex_dod(out_size, out_origin, src.canvas_size, src.depth, key);969 let mut dst = self.alloc_tex_dod(out_size, out_origin, src.canvas_size, src.depth, key);
970 dst.tex_scale *= src.tex_scale;
925 self.dispatch_op(enc, ShaderOp::SetDomain, &src.view, &self.dummy.view, &self.dummy.view, &dst, &u);971 self.dispatch_op(enc, ShaderOp::SetDomain, &src.view, &self.dummy.view, &self.dummy.view, &dst, &u);
926 self.cache.insert(key, dst.clone(), node.id, frame_i);972 self.cache.insert(key, dst.clone(), node.id, frame_i);
927 dst973 dst
...@@ -957,7 +1003,7 @@ impl Engine {...@@ -957,7 +1003,7 @@ impl Engine {
957 }1003 }
958 stats.cache_misses += 1;1004 stats.cache_misses += 1;
959 stats.nodes_evaluated += 1;1005 stats.nodes_evaluated += 1;
960 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, dst_depth, key);1006 let dst = self.alloc_tex_like(&src, dst_depth, key);
961 self.dispatch_op(enc, ShaderOp::ChangeDepth, &src.view, &self.dummy.view, &self.dummy.view, &dst, &u);1007 self.dispatch_op(enc, ShaderOp::ChangeDepth, &src.view, &self.dummy.view, &self.dummy.view, &dst, &u);
962 self.cache.insert(key, dst.clone(), node.id, frame_i);1008 self.cache.insert(key, dst.clone(), node.id, frame_i);
963 dst1009 dst
...@@ -1338,4 +1384,96 @@ Composition {{...@@ -1338,4 +1384,96 @@ Composition {{
13381384
1339 let _ = std::fs::remove_dir_all(&dir);1385 let _ = std::fs::remove_dir_all(&dir);
1340 }1386 }
1387
1388 #[test]
1389 fn loader_respects_inspector_timing_overrides_and_invalidates_cache() {
1390 let dir = std::env::temp_dir().join(format!("fission_loader_timing_{}", std::process::id()));
1391 let _ = std::fs::remove_dir_all(&dir);
1392 std::fs::create_dir_all(&dir).expect("create temp dir");
1393
1394 let f0 = dir.join("seq_0000.png");
1395 let f1 = dir.join("seq_0001.png");
1396 let f2 = dir.join("seq_0002.png");
1397 write_solid_png(&f0, 16, 16, [255, 0, 0, 255]);
1398 write_solid_png(&f1, 16, 16, [0, 255, 0, 255]);
1399 write_solid_png(&f2, 16, 16, [0, 0, 255, 255]);
1400
1401 let f0_esc = f0.to_string_lossy().replace('\\', "\\\\");
1402 let comp_src = format!(
1403 r#"
1404Composition {{
1405 CurrentTime = 0,
1406 RenderRange = {{ 0, 20 }},
1407 GlobalRange = {{ 0, 20 }},
1408 Tools = ordered() {{
1409 LD = Loader {{
1410 Clips = {{
1411 Clip {{
1412 ID = "Clip1",
1413 Filename = "{f0_esc}",
1414 FormatID = "PNGFormat",
1415 StartFrame = 0,
1416 Length = 3,
1417 GlobalStart = 0,
1418 GlobalEnd = 2,
1419 }}
1420 }},
1421 Inputs = {{
1422 MissingFrames = Input {{ Value = 1, }},
1423 }},
1424 }},
1425 }},
1426}}
1427"#
1428 );
1429 let mut comp = Comp::from_fusion_str(&comp_src).expect("parse comp");
1430 let ld = comp.find_node("LD").expect("LD");
1431 let Ok(mut eng) = Engine::new_headless() else {
1432 let _ = std::fs::remove_dir_all(&dir);
1433 return;
1434 };
1435
1436 let sample_px = |e: &mut Engine, c: &Comp, t: f64| -> [u8; 4] {
1437 let (tex, _) = e.evaluate(c, ld, t);
1438 let px = e.readback_rgba(&tex).expect("readback");
1439 [px[0], px[1], px[2], px[3]]
1440 };
1441
1442 // Baseline from Clips table: 0=red, 2=blue, 3=missing transparent.
1443 assert_eq!(sample_px(&mut eng, &comp, 0.0), [255, 0, 0, 255]);
1444 assert_eq!(sample_px(&mut eng, &comp, 2.0), [0, 0, 255, 255]);
1445 assert_eq!(sample_px(&mut eng, &comp, 3.0), [0, 0, 0, 0]);
1446
1447 // Override GlobalIn=10, GlobalOut=12 via node.inputs (Inspector write path) without clearing caches.
1448 let set_num = |c: &mut Comp, key: &str, v: f64| {
1449 c.nodes[ld.0].inputs.insert(
1450 key.to_owned(),
1451 model::InputBinding {
1452 value: Some(model::ParamValue::Number(v)),
1453 source_op: None,
1454 source_output: None,
1455 expression: None,
1456 },
1457 );
1458 c.revision += 1;
1459 };
1460 set_num(&mut comp, "GlobalIn", 10.0);
1461 set_num(&mut comp, "GlobalOut", 12.0);
1462 assert_eq!(sample_px(&mut eng, &comp, 0.0), [0, 0, 0, 0]);
1463 assert_eq!(sample_px(&mut eng, &comp, 10.0), [255, 0, 0, 255]);
1464 assert_eq!(sample_px(&mut eng, &comp, 12.0), [0, 0, 255, 255]);
1465
1466 // Override Reverse=1: frame 10 becomes blue, frame 12 becomes red.
1467 set_num(&mut comp, "Reverse", 1.0);
1468 assert_eq!(sample_px(&mut eng, &comp, 10.0), [0, 0, 255, 255]);
1469 assert_eq!(sample_px(&mut eng, &comp, 12.0), [255, 0, 0, 255]);
1470
1471 // Override Reverse=0, Loop=1: frame 13 wraps to red and is no longer missing!
1472 set_num(&mut comp, "Reverse", 0.0);
1473 set_num(&mut comp, "Loop", 1.0);
1474 assert_eq!(sample_px(&mut eng, &comp, 13.0), [255, 0, 0, 255]);
1475 assert_eq!(sample_px(&mut eng, &comp, 14.0), [0, 255, 0, 255]);
1476
1477 let _ = std::fs::remove_dir_all(&dir);
1478 }
1341}1479}
crates/engine/src/eval/keyers.rs+1-1
...@@ -98,7 +98,7 @@ impl Engine {...@@ -98,7 +98,7 @@ impl Engine {
98 }98 }
99 stats.cache_misses += 1;99 stats.cache_misses += 1;
100 stats.nodes_evaluated += 1;100 stats.nodes_evaluated += 1;
101 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);101 let dst = self.alloc_tex_like(&src, src.depth, key);
102 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);102 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
103 self.dispatch_op(enc, ShaderOp::UltraKeyer, &src.view, &self.dummy.view, mv, &dst, &u);103 self.dispatch_op(enc, ShaderOp::UltraKeyer, &src.view, &self.dummy.view, mv, &dst, &u);
104 self.cache.insert(key, dst.clone(), node.id, frame_i);104 self.cache.insert(key, dst.clone(), node.id, frame_i);
crates/engine/src/eval/transform.rs+72-7
...@@ -595,7 +595,7 @@ impl Engine {...@@ -595,7 +595,7 @@ impl Engine {
595 }595 }
596 stats.cache_misses += 1;596 stats.cache_misses += 1;
597 stats.nodes_evaluated += 1;597 stats.nodes_evaluated += 1;
598 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, full_key);598 let dst = self.alloc_tex_like(&src, src.depth, full_key);
599 self.dispatch_op(enc, ShaderOp::Cc, &src.view, &self.dummy.view, &self.dummy.view, &dst, &u);599 self.dispatch_op(enc, ShaderOp::Cc, &src.view, &self.dummy.view, &self.dummy.view, &dst, &u);
600 self.cache.insert(full_key, dst.clone(), node.id, frame_i);600 self.cache.insert(full_key, dst.clone(), node.id, frame_i);
601 Some(dst)601 Some(dst)
...@@ -631,7 +631,7 @@ impl Engine {...@@ -631,7 +631,7 @@ impl Engine {
631 stats.cache_misses += 1;631 stats.cache_misses += 1;
632 stats.nodes_evaluated += 1;632 stats.nodes_evaluated += 1;
633 let lut_tex = self.upload_lut_f16(&lut_f16);633 let lut_tex = self.upload_lut_f16(&lut_f16);
634 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);634 let dst = self.alloc_tex_like(&src, src.depth, key);
635 self.dispatch_op(enc, ShaderOp::ColorCurves, &src.view, &lut_tex.view, &self.dummy.view, &dst, &u);635 self.dispatch_op(enc, ShaderOp::ColorCurves, &src.view, &lut_tex.view, &self.dummy.view, &dst, &u);
636 self.cache.insert(key, dst.clone(), node.id, frame_i);636 self.cache.insert(key, dst.clone(), node.id, frame_i);
637 Some(dst)637 Some(dst)
...@@ -910,7 +910,7 @@ impl Engine {...@@ -910,7 +910,7 @@ impl Engine {
910 }910 }
911 stats.cache_misses += 1;911 stats.cache_misses += 1;
912 stats.nodes_evaluated += 1;912 stats.nodes_evaluated += 1;
913 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);913 let dst = self.alloc_tex_like(&src, src.depth, key);
914 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);914 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
915 self.dispatch_op(enc, ShaderOp::Vortex, &src.view, &self.dummy.view, mv, &dst, &u);915 self.dispatch_op(enc, ShaderOp::Vortex, &src.view, &self.dummy.view, mv, &dst, &u);
916 self.cache.insert(key, dst.clone(), node.id, frame_i);916 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -990,7 +990,7 @@ impl Engine {...@@ -990,7 +990,7 @@ impl Engine {
990 }990 }
991 stats.cache_misses += 1;991 stats.cache_misses += 1;
992 stats.nodes_evaluated += 1;992 stats.nodes_evaluated += 1;
993 let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key);993 let dst = self.alloc_tex_like(&src, src.depth, key);
994 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);994 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
995 self.dispatch_op(enc, ShaderOp::Homography, &src.view, &self.dummy.view, mv, &dst, &u);995 self.dispatch_op(enc, ShaderOp::Homography, &src.view, &self.dummy.view, mv, &dst, &u);
996 self.cache.insert(key, dst.clone(), node.id, frame_i);996 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -1129,7 +1129,8 @@ impl Engine {...@@ -1129,7 +1129,8 @@ impl Engine {
1129 }1129 }
1130 stats.cache_misses += 1;1130 stats.cache_misses += 1;
1131 stats.nodes_evaluated += 1;1131 stats.nodes_evaluated += 1;
1132 let dst = self.alloc_tex_dod(dst_size, dst_origin, src.canvas_size, src.depth, key);1132 let mut dst = self.alloc_tex_dod(dst_size, dst_origin, src.canvas_size, src.depth, key);
1133 dst.tex_scale *= src.tex_scale;
1133 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);1134 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
1134 self.dispatch_op(enc, ShaderOp::Homography, &src.view, &self.dummy.view, mv, &dst, &u);1135 self.dispatch_op(enc, ShaderOp::Homography, &src.view, &self.dummy.view, mv, &dst, &u);
1135 self.cache.insert(key, dst.clone(), node.id, frame_i);1136 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -1195,7 +1196,8 @@ impl Engine {...@@ -1195,7 +1196,8 @@ impl Engine {
1195 let dx = x_dev * strength * wave(1, 1.0);1196 let dx = x_dev * strength * wave(1, 1.0);
1196 let dy = y_dev * strength * wave(2, 1.27);1197 let dy = y_dev * strength * wave(2, 1.27);
1197 let angle = (r_dev * strength * wave(3, 0.83)).to_radians() as f32;1198 let angle = (r_dev * strength * wave(3, 0.83)).to_radians() as f32;
1198 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);1199 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.canvas_size), enc, visiting, stats);
1200 let mask = mask.map(|m| self.crop_to_canvas(enc, m));
1199 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);1201 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
12001202
1201 let mut u = UniformParams::default();1203 let mut u = UniformParams::default();
...@@ -1219,7 +1221,8 @@ impl Engine {...@@ -1219,7 +1221,8 @@ impl Engine {
1219 }1221 }
1220 stats.cache_misses += 1;1222 stats.cache_misses += 1;
1221 stats.nodes_evaluated += 1;1223 stats.nodes_evaluated += 1;
1222 let dst = self.alloc_tex(out_size, src.depth, key);1224 let mut dst = self.alloc_tex(out_size, src.depth, key);
1225 dst.tex_scale *= src.tex_scale;
1223 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);1226 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
1224 self.dispatch_op(enc, ShaderOp::Transform, &src.view, &self.dummy.view, mv, &dst, &u);1227 self.dispatch_op(enc, ShaderOp::Transform, &src.view, &self.dummy.view, mv, &dst, &u);
1225 self.cache.insert(key, dst.clone(), node.id, frame_i);1228 self.cache.insert(key, dst.clone(), node.id, frame_i);
...@@ -1896,4 +1899,66 @@ mod tests {...@@ -1896,4 +1899,66 @@ mod tests {
1896 assert_eq!(&v_px[i..i + 4], &xf_px[i..i + 4], "Vortex mask misaligned at x={x}");1899 assert_eq!(&v_px[i..i + 4], &xf_px[i..i + 4], "Vortex mask misaligned at x={x}");
1897 }1900 }
1898 }1901 }
1902
1903 #[test]
1904 fn camera_shake_dod_input_and_effect_mask_align_to_canvas() {
1905 let src = r#"Composition {
1906 CurrentTime = 2,
1907 RenderRange = { 0, 5 },
1908 GlobalRange = { 0, 5 },
1909 Tools = ordered() {
1910 Stripe = Background {
1911 Inputs = {
1912 Width = Input { Value = 64, },
1913 Height = Input { Value = 64, },
1914 Type = Input { Value = FuID { "Horizontal" }, },
1915 TopLeftRed = Input { Value = 1.0, },
1916 TopLeftAlpha = Input { Value = 1.0, },
1917 TopRightBlue = Input { Value = 1.0, },
1918 TopRightAlpha = Input { Value = 1.0, },
1919 },
1920 },
1921 XF = DVE {
1922 Inputs = {
1923 Input = Input { SourceOp = "Stripe", Source = "Output", },
1924 Center = Input { Value = { 0.25, 0.5 }, },
1925 },
1926 },
1927 Mask = RectangleMask {
1928 Inputs = {
1929 Center = Input { Value = { 0.875, 0.5 }, },
1930 Width = Input { Value = 0.25, },
1931 Height = Input { Value = 1.0, },
1932 },
1933 },
1934 CS = CameraShake {
1935 Inputs = {
1936 Input = Input { SourceOp = "XF", Source = "Output", },
1937 EffectMask = Input { SourceOp = "Mask", Source = "Mask", },
1938 XDeviation = Input { Value = 0.25, },
1939 YDeviation = Input { Value = 0.25, },
1940 RotationDeviation = Input { Value = 25.0, },
1941 OverallStrength = Input { Value = 1.0, },
1942 Speed = Input { Value = 3.0, },
1943 },
1944 },
1945 },
1946 }"#;
1947 let comp = Comp::from_fusion_str(src).expect("parse CameraShake DoD comp");
1948 let Ok(mut eng) = Engine::new_headless() else {
1949 return;
1950 };
1951 let (xf_tex, _) = eng.evaluate(&comp, comp.find_node("XF").unwrap(), 2.0);
1952 let (cs_tex, _) = eng.evaluate(&comp, comp.find_node("CS").unwrap(), 2.0);
1953 assert_eq!(cs_tex.size, [64, 64]);
1954 assert_eq!(cs_tex.origin, [0, 0]);
1955 assert_eq!(cs_tex.canvas_size, [64, 64]);
1956 let xf_px = eng.readback_rgba(&xf_tex).unwrap();
1957 let cs_px = eng.readback_rgba(&cs_tex).unwrap();
1958 // Mask covers [48..64), so x in [0..46) must match XF on canvas exactly.
1959 for x in 0..46 {
1960 let i = (32 * 64 + x) * 4;
1961 assert_eq!(&cs_px[i..i + 4], &xf_px[i..i + 4], "CameraShake mask misaligned at x={x}");
1962 }
1963 }
1899}1964}
crates/engine/src/export.rs+120-3
...@@ -9,7 +9,7 @@ use std::fs::File;...@@ -9,7 +9,7 @@ use std::fs::File;
9use std::io::{BufWriter, Read, Write};9use std::io::{BufWriter, Read, Write};
10use std::path::{Path, PathBuf};10use std::path::{Path, PathBuf};
11use std::process::{Command, Stdio};11use std::process::{Command, Stdio};
12use std::sync::atomic::{AtomicBool, Ordering};12use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
13use std::sync::{Arc, Mutex, mpsc};13use std::sync::{Arc, Mutex, mpsc};
14use std::time::Instant;14use std::time::Instant;
1515
...@@ -558,6 +558,46 @@ fn fit_frame_rgba(...@@ -558,6 +558,46 @@ fn fit_frame_rgba(
558 .into_raw())558 .into_raw())
559}559}
560560
561static EXPORT_TMP_SEQ: AtomicU64 = AtomicU64::new(0);
562
563fn sibling_temp_export_path(out_path: &Path) -> PathBuf {
564 let seq = EXPORT_TMP_SEQ.fetch_add(1, Ordering::Relaxed);
565 let pid = std::process::id();
566 let stem = out_path
567 .file_stem()
568 .and_then(|s| s.to_str())
569 .filter(|s| !s.is_empty())
570 .unwrap_or("export");
571 let file_name = match out_path.extension().and_then(|e| e.to_str()).filter(|e| !e.is_empty()) {
572 Some(ext) => format!("{stem}.fission_tmp_{pid}_{seq}.{ext}"),
573 None => format!("{stem}.fission_tmp_{pid}_{seq}"),
574 };
575 out_path.with_file_name(file_name)
576}
577
578struct TempExportGuard {
579 path: PathBuf,
580 active: bool,
581}
582
583impl TempExportGuard {
584 fn new(path: PathBuf) -> Self {
585 Self { path, active: true }
586 }
587
588 fn disarm(&mut self) {
589 self.active = false;
590 }
591}
592
593impl Drop for TempExportGuard {
594 fn drop(&mut self) {
595 if self.active {
596 let _ = std::fs::remove_file(&self.path);
597 }
598 }
599}
600
561/// Evaluates `opts.target` over `opts.range[0]..=opts.range[1]`, pipes raw RGBA8 frames601/// Evaluates `opts.target` over `opts.range[0]..=opts.range[1]`, pipes raw RGBA8 frames
562/// directly into `ffmpeg` with cooperative cancellation and live progress via `ExportControl`.602/// directly into `ffmpeg` with cooperative cancellation and live progress via `ExportControl`.
563pub fn export_node_video_ffmpeg_with_progress(603pub fn export_node_video_ffmpeg_with_progress(
...@@ -753,7 +793,9 @@ pub fn export_node_video_ffmpeg_with_progress(...@@ -753,7 +793,9 @@ pub fn export_node_video_ffmpeg_with_progress(
753 cmd.arg("-an");793 cmd.arg("-an");
754 }794 }
755795
756 cmd.arg(&opts.out_path);796 let tmp_path = sibling_temp_export_path(&opts.out_path);
797 let mut tmp_guard = TempExportGuard::new(tmp_path.clone());
798 cmd.arg(&tmp_path);
757 cmd.stdin(Stdio::piped())799 cmd.stdin(Stdio::piped())
758 .stdout(Stdio::null())800 .stdout(Stdio::null())
759 .stderr(Stdio::piped());801 .stderr(Stdio::piped());
...@@ -853,7 +895,6 @@ pub fn export_node_video_ffmpeg_with_progress(...@@ -853,7 +895,6 @@ pub fn export_node_video_ffmpeg_with_progress(
853 let stderr_trim = stderr_text.trim();895 let stderr_trim = stderr_text.trim();
854896
855 if cancelled {897 if cancelled {
856 let _ = std::fs::remove_file(&opts.out_path);
857 return Err("Cancelled".to_owned());898 return Err("Cancelled".to_owned());
858 }899 }
859 if let Some(err) = pipe_err {900 if let Some(err) = pipe_err {
...@@ -870,6 +911,10 @@ pub fn export_node_video_ffmpeg_with_progress(...@@ -870,6 +911,10 @@ pub fn export_node_video_ffmpeg_with_progress(
870 });911 });
871 }912 }
872913
914 std::fs::rename(&tmp_path, &opts.out_path)
915 .map_err(|e| format!("failed to finalize '{}': {e}", opts.out_path.display()))?;
916 tmp_guard.disarm();
917
873 let file_bytes = std::fs::metadata(&opts.out_path)918 let file_bytes = std::fs::metadata(&opts.out_path)
874 .map(|m| m.len())919 .map(|m| m.len())
875 .unwrap_or(0);920 .unwrap_or(0);
...@@ -1481,4 +1526,76 @@ mod tests {...@@ -1481,4 +1526,76 @@ mod tests {
14811526
1482 let _ = std::fs::remove_dir_all(&tmp_dir);1527 let _ = std::fs::remove_dir_all(&tmp_dir);
1483 }1528 }
1529
1530 #[test]
1531 fn video_export_cancel_preserves_existing_output_and_removes_temp_file() {
1532 let tmp_dir = std::env::temp_dir().join(format!(
1533 "fission-video-atomic-cancel-{}",
1534 std::process::id()
1535 ));
1536 let _ = std::fs::remove_dir_all(&tmp_dir);
1537 std::fs::create_dir_all(&tmp_dir).expect("create temp dir");
1538
1539 let out_mp4 = tmp_dir.join("existing.mp4");
1540 let sentinel_bytes = b"ORIGINAL_VALUABLE_EXPORT_BYTES_DO_NOT_TRUNCATE";
1541 std::fs::write(&out_mp4, sentinel_bytes).expect("write sentinel existing file");
1542
1543 let comp_src = r#"Composition {
1544 CurrentTime = 0,
1545 RenderRange = { 0, 9 },
1546 GlobalRange = { 0, 9 },
1547 Tools = ordered() {
1548 BG = Background {
1549 Inputs = {
1550 Width = Input { Value = 64, },
1551 Height = Input { Value = 64, },
1552 TopLeftRed = Input { Value = 0.8, },
1553 TopLeftAlpha = Input { Value = 1, },
1554 },
1555 },
1556 },
1557 Prefs = { Comp = { FrameFormat = { Width = 64, Height = 64, Rate = 24, DepthFull = 1 } } },
1558}"#;
1559 let comp = Comp::from_fusion_str(comp_src).expect("parse comp");
1560 let target = comp.find_node("BG").expect("find BG");
1561 let mut engine = Engine::new_headless().expect("headless wgpu engine");
1562 let control = ExportControl::new();
1563 let ctrl_cb = control.clone();
1564
1565 let err = export_node_video_ffmpeg_with_progress(
1566 &mut engine,
1567 &comp,
1568 &VideoExportOptions {
1569 target,
1570 out_path: out_mp4.clone(),
1571 range: [0, 9],
1572 codec: VideoCodec::H264Share,
1573 crf: 26,
1574 max_height: None,
1575 include_audio: false,
1576 hiq: true,
1577 proxy_scale: 1,
1578 },
1579 Some(&control),
1580 |p| {
1581 if p.completed_frames >= 2 {
1582 ctrl_cb.cancel();
1583 }
1584 },
1585 )
1586 .expect_err("cancelled export should return Err");
1587 assert_eq!(err, "Cancelled");
1588
1589 let after_bytes = std::fs::read(&out_mp4).expect("existing output must still exist");
1590 assert_eq!(after_bytes.as_slice(), sentinel_bytes, "existing file must stay untouched");
1591
1592 let entries: Vec<_> = std::fs::read_dir(&tmp_dir)
1593 .expect("read tmp_dir")
1594 .filter_map(|e| e.ok())
1595 .map(|e| e.file_name().to_string_lossy().into_owned())
1596 .collect();
1597 assert_eq!(entries, vec!["existing.mp4".to_owned()], "no temp files should remain");
1598
1599 let _ = std::fs::remove_dir_all(&tmp_dir);
1600 }
1484}1601}
crates/engine/src/particles.rs+84-9
...@@ -387,7 +387,12 @@ fn hash_gradient(grad: &Gradient, h: &mut DefaultHasher) {...@@ -387,7 +387,12 @@ fn hash_gradient(grad: &Gradient, h: &mut DefaultHasher) {
387 }387 }
388}388}
389389
390fn hash_input_binding(comp: &Comp, binding: &InputBinding, h: &mut DefaultHasher) {390fn hash_input_binding(
391 comp: &Comp,
392 binding: &InputBinding,
393 visited_mods: &mut HashSet<String>,
394 h: &mut DefaultHasher,
395) {
391 binding.source_output.hash(h);396 binding.source_output.hash(h);
392 binding.expression.hash(h);397 binding.expression.hash(h);
393 match &binding.value {398 match &binding.value {
...@@ -445,10 +450,22 @@ fn hash_input_binding(comp: &Comp, binding: &InputBinding, h: &mut DefaultHasher...@@ -445,10 +450,22 @@ fn hash_input_binding(comp: &Comp, binding: &InputBinding, h: &mut DefaultHasher
445 }450 }
446 }451 }
447 if let Some(sh) = comp.shakes.get(op) {452 if let Some(sh) = comp.shakes.get(op) {
448 hash_sorted_inputs(comp, &sh.inputs, h);453 if visited_mods.insert(op.clone()) {
454 hash_sorted_inputs(comp, &sh.inputs, visited_mods, h);
455 visited_mods.remove(op);
456 } else {
457 0xC1C1_u16.hash(h);
458 op.hash(h);
459 }
449 }460 }
450 if let Some(ex) = comp.expression_modifiers.get(op) {461 if let Some(ex) = comp.expression_modifiers.get(op) {
451 hash_sorted_inputs(comp, &ex.inputs, h);462 if visited_mods.insert(op.clone()) {
463 hash_sorted_inputs(comp, &ex.inputs, visited_mods, h);
464 visited_mods.remove(op);
465 } else {
466 0xC1C2_u16.hash(h);
467 op.hash(h);
468 }
452 }469 }
453 if let Some(gm) = comp.gradient_modifiers.get(op) {470 if let Some(gm) = comp.gradient_modifiers.get(op) {
454 gm.len().hash(h);471 gm.len().hash(h);
...@@ -460,20 +477,32 @@ fn hash_input_binding(comp: &Comp, binding: &InputBinding, h: &mut DefaultHasher...@@ -460,20 +477,32 @@ fn hash_input_binding(comp: &Comp, binding: &InputBinding, h: &mut DefaultHasher
460 }477 }
461}478}
462479
463fn hash_sorted_inputs(comp: &Comp, inputs: &HashMap<String, InputBinding>, h: &mut DefaultHasher) {480fn hash_sorted_inputs(
481 comp: &Comp,
482 inputs: &HashMap<String, InputBinding>,
483 visited_mods: &mut HashSet<String>,
484 h: &mut DefaultHasher,
485) {
464 let mut keys: Vec<&String> = inputs.keys().collect();486 let mut keys: Vec<&String> = inputs.keys().collect();
465 keys.sort_unstable();487 keys.sort_unstable();
466 keys.len().hash(h);488 keys.len().hash(h);
467 for k in keys {489 for k in keys {
468 k.hash(h);490 k.hash(h);
469 hash_input_binding(comp, &inputs[k], h);491 hash_input_binding(comp, &inputs[k], visited_mods, h);
470 }492 }
471}493}
472494
473/// Content hash of the particle simulation subgraph upstream of `render_node`.495/// Content hash of the particle simulation subgraph upstream of `render_node`.
474/// Unrelated edits outside the particle branch leave this hash unchanged.496/// Unrelated edits outside the particle branch leave this hash unchanged.
475fn particle_subgraph_hash(comp: &Comp, render_node: NodeId, base_start: i64) -> u64 {497fn particle_subgraph_hash(comp: &Comp, render_node: NodeId, base_start: i64) -> u64 {
476 fn dfs(comp: &Comp, node_id: NodeId, is_root: bool, visited: &mut HashSet<NodeId>, h: &mut DefaultHasher) {498 fn dfs(
499 comp: &Comp,
500 node_id: NodeId,
501 is_root: bool,
502 visited: &mut HashSet<NodeId>,
503 visited_mods: &mut HashSet<String>,
504 h: &mut DefaultHasher,
505 ) {
477 if !visited.insert(node_id) {506 if !visited.insert(node_id) {
478 return;507 return;
479 }508 }
...@@ -495,17 +524,18 @@ fn particle_subgraph_hash(comp: &Comp, render_node: NodeId, base_start: i64) ->...@@ -495,17 +524,18 @@ fn particle_subgraph_hash(comp: &Comp, render_node: NodeId, base_start: i64) ->
495 continue;524 continue;
496 }525 }
497 k.hash(h);526 k.hash(h);
498 hash_input_binding(comp, &node.inputs[k], h);527 hash_input_binding(comp, &node.inputs[k], visited_mods, h);
499 }528 }
500 for up_id in particle_stream_inputs(comp, node) {529 for up_id in particle_stream_inputs(comp, node) {
501 dfs(comp, up_id, false, visited, h);530 dfs(comp, up_id, false, visited, visited_mods, h);
502 }531 }
503 }532 }
504533
505 let mut h = DefaultHasher::new();534 let mut h = DefaultHasher::new();
506 base_start.hash(&mut h);535 base_start.hash(&mut h);
507 let mut visited = HashSet::new();536 let mut visited = HashSet::new();
508 dfs(comp, render_node, true, &mut visited, &mut h);537 let mut visited_mods = HashSet::new();
538 dfs(comp, render_node, true, &mut visited, &mut visited_mods, &mut h);
509 h.finish()539 h.finish()
510}540}
511541
...@@ -2521,4 +2551,49 @@ Composition {...@@ -2521,4 +2551,49 @@ Composition {
2521 let img_b_21 = render_particles_2d(&mut fresh_b, &comp_b, rb, 21.0, dims, &expr, &sprites);2551 let img_b_21 = render_particles_2d(&mut fresh_b, &comp_b, rb, 21.0, dims, &expr, &sprites);
2522 assert_eq!(img_a_to_b_21, img_b_21);2552 assert_eq!(img_a_to_b_21, img_b_21);
2523 }2553 }
2554
2555 #[test]
2556 fn cyclic_expression_modifier_does_not_stack_overflow() {
2557 let comp_src = r#"
2558Composition {
2559 GlobalRange = { 0, 20 },
2560 RenderRange = { 0, 20 },
2561 Tools = ordered() {
2562 Expr1 = Expression {
2563 Inputs = {
2564 n1 = Input { SourceOp = "Expr1", Source = "NumberResult", },
2565 NumberExpression = Input { Value = "n1 + 4", },
2566 },
2567 },
2568 pEmitter1 = pEmitter {
2569 Inputs = {
2570 RandomSeed = Input { Value = 7, },
2571 Number = Input { SourceOp = "Expr1", Source = "NumberResult", },
2572 Lifespan = Input { Value = 15, },
2573 Velocity = Input { Value = 0.2, },
2574 },
2575 },
2576 pRender1 = pRender {
2577 Inputs = {
2578 Input = Input { SourceOp = "pEmitter1", Source = "Output", },
2579 },
2580 },
2581 },
2582}
2583"#;
2584 let comp = Comp::from_fusion_str(comp_src).expect("parse cyclic Expr1 comp");
2585 assert!(comp.expression_modifiers.contains_key("Expr1"));
2586 let pr = comp.find_node("pRender1").expect("find pRender1");
2587 let expr = ExprEngine::new();
2588 let sprites = HashMap::new();
2589 let mut cache = ParticleCache::default();
2590
2591 // Both subgraph hashing and particle evaluation must terminate cleanly without overflowing.
2592 let h1 = particle_subgraph_hash(&comp, pr, 0);
2593 let h2 = particle_subgraph_hash(&comp, pr, 0);
2594 assert_eq!(h1, h2);
2595
2596 let img = render_particles_2d(&mut cache, &comp, pr, 5.0, [64, 64], &expr, &sprites);
2597 assert_eq!(img.len(), 64 * 64 * 4);
2598 }
2524}2599}